完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
|
|
相关推荐
1个回答
|
|
单片机读取温度通过串口1在串口调试助手上显示温度。
头文件 #ifndef __MLX90614_H #define __MLX90614_H #include "sys.h" void SMBus_StartBit(void); void SMBus_StopBit(void); void SMBus_SendBit(u8); u8 SMBus_SendByte(u8); u8 SMBus_ReceiveBit(void); u8 SMBus_ReceiveByte(u8); void SMBus_Delay(u16); void SMBus_Init(void); u16 SMBus_ReadMemory(u8, u8); u8 PEC_Calculation(u8*); float SMBus_ReadTemp(void); //获取温度值 void SMBus_DisplayTemperature(void); //在LCD第5,6页显示温度 #endif 源文件 #include "mlx90614.h" #define ACK 0 #define NACK 1 #define SA 0x00 //从机地址,单个MLX90614时地址为0x00,多个时地址默认为0x5a #define RAM_ACCESS 0x00 //RAM access command #define EEPROM_ACCESS 0x20 //EEPROM access command #define RAM_TOBJ1 0x07 //To1 address in the eeprom #define SMBUS_PORT GPIOB #define SMBUS_SCK GPIO_Pin_6 #define SMBUS_SDA GPIO_Pin_7 #define RCC_APB2Periph_SMBUS_PORT RCC_APB2Periph_GPIOB #define SMBUS_SCK_H() SMBUS_PORT->BSRR = SMBUS_SCK #define SMBUS_SCK_L() SMBUS_PORT->BRR = SMBUS_SCK #define SMBUS_SDA_H() SMBUS_PORT->BSRR = SMBUS_SDA #define SMBUS_SDA_L() SMBUS_PORT->BRR = SMBUS_SDA #define SMBUS_SDA_PIN() SMBUS_PORT->IDR & SMBUS_SDA //读取引脚电平 /******************************************************************************* * 函数名: SMBus_StartBit * 功能 : 产生起始位 * Input : None * Output : None * Return : None *******************************************************************************/ void SMBus_StartBit(void) { SMBUS_SDA_H(); // Set SDA line SMBus_Delay(5); // Wait a few microseconds SMBUS_SCK_H(); // Set SCL line SMBus_Delay(5); // Generate bus free time between Stop SMBUS_SDA_L(); // Clear SDA line SMBus_Delay(5); // Hold time after (Repeated) Start // Condition. After this period, the first clock is generated. //(Thd:sta=4.0us min) SMBUS_SCK_L(); // Clear SCL line SMBus_Delay(5); // Wait a few microseconds } /******************************************************************************* * 函数名: SMBus_StopBit * 功能: Generate STOP condition on SMBus * Input : None * Output : None * Return : None *******************************************************************************/ void SMBus_StopBit(void) { SMBUS_SCK_L(); // Clear SCL line SMBus_Delay(5); // Wait a few microseconds SMBUS_SDA_L(); // Clear SDA line SMBus_Delay(5); // Wait a few microseconds SMBUS_SCK_H(); // Set SCL line SMBus_Delay(5); // Stop condition setup time(Tsu:sto=4.0us min) SMBUS_SDA_H(); // Set SDA line } /******************************************************************************* * 函数名: SMBus_SendByte * 功能: Send a byte on SMBus * Input : Tx_buffer * Output : None * Return : None *******************************************************************************/ u8 SMBus_SendByte(u8 Tx_buffer) { u8 Bit_counter; u8 Ack_bit; u8 bit_out; for(Bit_counter=8; Bit_counter; Bit_counter--) { if (Tx_buffer&0x80) { bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out } else { bit_out=0; // else clear bit_out } SMBus_SendBit(bit_out); // Send the current bit on SDA Tx_buffer<<=1; // Get next bit for checking } Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit return Ack_bit; } /******************************************************************************* * 函数名: SMBus_SendBit * 功能: Send a bit on SMBus 82.5kHz * Input : bit_out * Output : None * Return : None *******************************************************************************/ void SMBus_SendBit(u8 bit_out) { if(bit_out==0) { SMBUS_SDA_L(); } else { SMBUS_SDA_H(); } SMBus_Delay(2); // Tsu:dat = 250ns minimum SMBUS_SCK_H(); // Set SCL line SMBus_Delay(6); // High Level of Clock Pulse SMBUS_SCK_L(); // Clear SCL line SMBus_Delay(3); // Low Level of Clock Pulse // SMBUS_SDA_H(); // Master release SDA line , return; } /******************************************************************************* * Function Name : SMBus_ReceiveBit * Description : Receive a bit on SMBus * Input : None * Output : None * Return : Ack_bit *******************************************************************************/ u8 SMBus_ReceiveBit(void) { u8 Ack_bit; SMBUS_SDA_H(); //引脚靠外部电阻上拉,当作输入 SMBus_Delay(2); // High Level of Clock Pulse SMBUS_SCK_H(); // Set SCL line SMBus_Delay(5); // High Level of Clock Pulse if (SMBUS_SDA_PIN()) { Ack_bit=1; } else { Ack_bit=0; } SMBUS_SCK_L(); // Clear SCL line SMBus_Delay(3); // Low Level of Clock Pulse return Ack_bit; } /******************************************************************************* * 函数名: SMBus_ReceiveByte * 功能: Receive a byte on SMBus * Input : ack_nack * Output : None * Return : RX_buffer *******************************************************************************/ u8 SMBus_ReceiveByte(u8 ack_nack) { u8 RX_buffer; u8 Bit_Counter; for(Bit_Counter=8; Bit_Counter; Bit_Counter--) { if(SMBus_ReceiveBit()) // Get a bit from the SDA line { RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer RX_buffer |=0x01; } else { RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer RX_buffer &=0xfe; } } SMBus_SendBit(ack_nack); // Sends acknowledgment bit return RX_buffer; } /******************************************************************************* * 函数名: SMBus_Delay * 功能: 延时 一次循环约1us * Input : time * Output : None * Return : None *******************************************************************************/ void SMBus_Delay(u16 time) { u16 i, j; for (i=0; i<4; i++) { for (j=0; j } } /******************************************************************************* * 函数名: SMBus_Init * 功能: SMBus初始化 * Input : None * Output : None * Return : None *******************************************************************************/ void SMBus_Init() { GPIO_InitTypeDef GPIO_InitStructure; /* Enable SMBUS_PORT clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SMBUS_PORT, ENABLE); /*配置SMBUS_SCK、SMBUS_SDA为集电极开漏输出*/ GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SMBUS_PORT, &GPIO_InitStructure); SMBUS_SCK_H(); SMBUS_SDA_H(); } /******************************************************************************* * 函数名: SMBus_ReadMemory * 功能: READ DATA FROM RAM/EEPROM * Input : slaveAddress, command * Return : Data *******************************************************************************/ u16 SMBus_ReadMemory(u8 slaveAddress, u8 command) { u16 data; // Data storage (DataH:DataL) u8 Pec; // PEC byte storage u8 DataL=0; // Low data byte storage u8 DataH=0; // High data byte storage u8 arr[6]; // Buffer for the sent bytes u8 PecReg; // Calculated PEC byte storage u8 ErrorCounter; // Defines the number of the attempts for communication with MLX90614 ErrorCounter=0x00; // Initialising of ErrorCounter slaveAddress <<= 1; //2-7位表示从机地址 do { repeat: SMBus_StopBit(); //If slave send NACK stop comunication --ErrorCounter; //Pre-decrement ErrorCounter if(!ErrorCounter) //ErrorCounter=0? { break; //Yes,go out from do-while{} } SMBus_StartBit(); //Start condition if(SMBus_SendByte(slaveAddress))//Send SlaveAddress 最低位Wr=0表示接下来写命令 { goto repeat; //Repeat comunication again } if(SMBus_SendByte(command)) //Send command { goto repeat; //Repeat comunication again } SMBus_StartBit(); //Repeated Start condition if(SMBus_SendByte(slaveAddress+1)) //Send SlaveAddress 最低位Rd=1表示接下来读数据 { goto repeat; //Repeat comunication again } DataL = SMBus_ReceiveByte(ACK); //Read low data,master must send ACK DataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACK Pec = SMBus_ReceiveByte(NACK); //Read PEC byte, master must send NACK SMBus_StopBit(); //Stop condition arr[5] = slaveAddress; // arr[4] = command; // arr[3] = slaveAddress+1; //Load array arr arr[2] = DataL; // arr[1] = DataH; // arr[0] = 0; // PecReg=PEC_Calculation(arr);//Calculate CRC } while(PecReg != Pec); //If received and calculated CRC are equal go out from do-while{} data = (DataH<<8) | DataL; //data=DataH:DataL return data; } /******************************************************************************* * 函数名: PEC_calculation * 功能 : 数据校验 * Input : pec[] * Output : None * Return : pec[0]-this byte contains calculated crc value *******************************************************************************/ u8 PEC_Calculation(u8 pec[]) { u8 crc[6]; u8 BitPosition=47; u8 shift; u8 i; u8 j; u8 temp; do { /*Load pattern value 0x000000000107*/ crc[5]=0; crc[4]=0; crc[3]=0; crc[2]=0; crc[1]=0x01; crc[0]=0x07; /*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/ BitPosition=47; /*Set shift position at 0*/ shift=0; /*Find first "1" in the transmited message beginning from the MSByte byte5*/ i=5; j=0; while((pec&(0x80>>j))==0 && i>0) { BitPosition--; if(j<7) { j++; } else { j=0x00; i--; } }/*End of while */ /*Get shift value for pattern value*/ shift=BitPosition-8; /*Shift pattern value */ while(shift) { for(i=5; i<0xFF; i--) { if((crc[i-1]&0x80) && (i>0)) { temp=1; } else { temp=0; } crc<<=1; crc+=temp; }/*End of for*/ shift--; }/*End of while*/ /*Exclusive OR between pec and crc*/ for(i=0; i<=5; i++) { pec ^=crc; }/*End of for*/ } while(BitPosition>8); /*End of do-while*/ return pec[0]; } /******************************************************************************* * 函数名: SMBus_ReadTemp * 功能: 计算并返回温度值 * Return : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15 *******************************************************************************/ float SMBus_ReadTemp(void) { return SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15; } /*********************************END OF FILE*********************************/ 主函数 #include "led.h" #include "delay.h" #include "key.h" #include "sys.h" #include "lcd.h" #include "usart.h" #include "adc.h" #include "mlx90614.h" int main(void) { float a; delay_init(); //延时函数初始化 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级 uart_init(115200); //串口初始化为115200 LED_Init(); //LED端口初始化 LCD_Init(); SMBus_Init(); while(1) { a = SMBus_ReadTemp(); printf("温度 = %f℃rn",a); LED0=!LED0; delay_ms(250); } } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1784 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1621 浏览 1 评论
1088 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
729 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1680 浏览 2 评论
1938浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
734浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
570浏览 3评论
596浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
559浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 18:11 , Processed in 0.813809 second(s), Total 47, Slave 39 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号