当前位置:   article > 正文

MLX90614+STM32标准库_mlx90614 stm32

mlx90614 stm32

MLX90614+STM32f103c8t6标准库,OLED显示。

连线:SCL ——PA8;       SDA——PA9

  1. #include "stm32f10x.h" // Device header
  2. #include "Delay.h"
  3. #include "OLED.h"
  4. #include "mlx90614.h"
  5. #include "stdio.h"
  6. int main(void)
  7. {
  8. float Temperature = 0; //温度数据变量(浮点型)
  9. char TempValue[80] = {0}; //温度值(字符串)
  10. OLED_Init();
  11. SMBus_Init();
  12. while (1)
  13. {
  14. Temperature = SMBus_ReadTemp();
  15. Delay_ms(200);
  16. sprintf(TempValue,"%.1f", Temperature); //浮点型转换成字符串
  17. OLED_ShowString(1,1,TempValue);//在在OLED上显示实际测量的温度
  18. }
  19. }
  1. #ifndef __IIC_H
  2. #define __IIC_H
  3. #include "stm32f10x.h" // Device header
  4. void MyI2C_Init(void);
  5. void MyI2C_Start(void);
  6. void MyI2C_Stop(void);
  7. void MyI2C_SendByte(uint8_t Byte);
  8. uint8_t MyI2C_ReceiveByte(void);
  9. void MyI2C_SendAck(uint8_t AckBit);
  10. uint8_t MyI2C_ReceiveAck(void);
  11. #endif

IIC.C

  1. #include "stm32f10x.h" // Device header
  2. #include "Delay.h"
  3. void MyI2C_W_SCL(uint8_t BitValue)
  4. {
  5. GPIO_WriteBit(GPIOA, GPIO_Pin_8, (BitAction)BitValue);
  6. Delay_us(10);
  7. }
  8. void MyI2C_W_SDA(uint8_t BitValue)
  9. {
  10. GPIO_WriteBit(GPIOA, GPIO_Pin_9, (BitAction)BitValue);
  11. Delay_us(10);
  12. }
  13. uint8_t MyI2C_R_SDA(void)
  14. {
  15. uint8_t BitValue;
  16. BitValue = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9);
  17. Delay_us(10);
  18. return BitValue;
  19. }
  20. void MyI2C_Init(void)
  21. {
  22. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  23. GPIO_InitTypeDef GPIO_InitStructure;
  24. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  25. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  26. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27. GPIO_Init(GPIOA, &GPIO_InitStructure);
  28. GPIO_SetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9);
  29. }
  30. void MyI2C_Start(void)
  31. {
  32. MyI2C_W_SDA(1);
  33. MyI2C_W_SCL(1);
  34. MyI2C_W_SDA(0);
  35. MyI2C_W_SCL(0);
  36. }
  37. void MyI2C_Stop(void)
  38. {
  39. MyI2C_W_SDA(0);
  40. MyI2C_W_SCL(1);
  41. MyI2C_W_SDA(1);
  42. }
  43. void MyI2C_SendByte(uint8_t Byte)
  44. {
  45. uint8_t i;
  46. for (i = 0; i < 8; i ++)
  47. {
  48. MyI2C_W_SDA(Byte & (0x80 >> i));
  49. MyI2C_W_SCL(1);
  50. MyI2C_W_SCL(0);
  51. }
  52. }
  53. uint8_t MyI2C_ReceiveByte(void)
  54. {
  55. uint8_t i, Byte = 0x00;
  56. MyI2C_W_SDA(1);
  57. for (i = 0; i < 8; i ++)
  58. {
  59. MyI2C_W_SCL(1);
  60. if (MyI2C_R_SDA() == 1){Byte |= (0x80 >> i);}
  61. MyI2C_W_SCL(0);
  62. }
  63. return Byte;
  64. }
  65. void MyI2C_SendAck(uint8_t AckBit)
  66. {
  67. MyI2C_W_SDA(AckBit);
  68. MyI2C_W_SCL(1);
  69. MyI2C_W_SCL(0);
  70. }
  71. uint8_t MyI2C_ReceiveAck(void)
  72. {
  73. uint8_t AckBit;
  74. MyI2C_W_SDA(1);
  75. MyI2C_W_SCL(1);
  76. AckBit = MyI2C_R_SDA();
  77. MyI2C_W_SCL(0);
  78. return AckBit;
  79. }
  1. #include "mlx90614.h"
  2. /*******************************************************************************
  3. * 函数名: SMBus_Init
  4. * 功能: SMBus初始化
  5. * Input : None
  6. * Output : None
  7. * Return : None
  8. *******************************************************************************/
  9. void SMBus_Init()
  10. {
  11. MyI2C_Init();
  12. }
  13. /*******************************************************************************
  14. * 函数名: SMBus_ReadMemory
  15. * 功能: READ DATA FROM RAM/EEPROM 从RAM和EEPROM中读取数据
  16. * Input : slaveAddress, command
  17. * Return : Data
  18. * SMBus_ReadMemory(0x00, 0x07) 0x00 表示IIC设备的从地址 从0x07这个寄存器开始读取
  19. *******************************************************************************/
  20. u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
  21. {
  22. u16 data; // Data storage (DataH:DataL)
  23. u8 Pec; // PEC byte storage
  24. u8 DataL=0; // Low data byte storage
  25. u8 DataH=0; // High data byte storage
  26. u8 arr[6]; // Buffer for the sent bytes
  27. u8 PecReg; // Calculated PEC byte storage
  28. u8 ErrorCounter; // Defines the number of the attempts for communication with MLX90614
  29. ErrorCounter=0x00; // Initialising of ErrorCounter
  30. slaveAddress <<= 1; //2-7位表示从机地址 从机地址左移一位,把读写位空出来
  31. do
  32. {
  33. repeat:
  34. MyI2C_Stop(); //If slave send NACK stop comunication
  35. --ErrorCounter; //Pre-decrement ErrorCounter
  36. if(!ErrorCounter) //ErrorCounter=0?
  37. {
  38. break; //Yes,go out from do-while{}
  39. }
  40. MyI2C_Start(); //Start condition
  41. MyI2C_SendByte(slaveAddress);
  42. if(MyI2C_ReceiveAck())//Send SlaveAddress 最低位Wr=0表示接下来写命令
  43. {
  44. goto repeat; //Repeat comunication again
  45. }
  46. MyI2C_SendByte(command);
  47. if(MyI2C_ReceiveAck()) //Send command
  48. {
  49. goto repeat; //Repeat comunication again
  50. }
  51. MyI2C_Start(); //Repeated Start condition
  52. MyI2C_SendByte(slaveAddress+1);
  53. if(MyI2C_ReceiveAck()) //Send SlaveAddress 最低位Rd=1表示接下来读数据
  54. {
  55. goto repeat; //Repeat comunication again
  56. }
  57. DataL = MyI2C_ReceiveByte(); //Read low data,master must send ACK
  58. MyI2C_SendAck(ACK);
  59. DataH = MyI2C_ReceiveByte(); //Read high data,master must send ACK
  60. MyI2C_SendAck(ACK);
  61. Pec = MyI2C_ReceiveByte(); //Read PEC byte, master must send NACK
  62. MyI2C_SendAck(NACK);
  63. MyI2C_Stop(); //Stop condition
  64. arr[5] = slaveAddress; //
  65. arr[4] = command; //
  66. arr[3] = slaveAddress+1; //Load array arr
  67. arr[2] = DataL; //
  68. arr[1] = DataH; //
  69. arr[0] = 0; //
  70. PecReg=PEC_Calculation(arr);//Calculate CRC 数据校验
  71. }
  72. while(PecReg != Pec);//If received and calculated CRC are equal go out from do-while{}
  73. data = (DataH<<8) | DataL; //data=DataH:DataL
  74. return data;
  75. }
  76. /*******************************************************************************
  77. * 函数名: PEC_calculation
  78. * 功能 : 数据校验
  79. * Input : pec[]
  80. * Output : None
  81. * Return : pec[0]-this byte contains calculated crc value
  82. *******************************************************************************/
  83. u8 PEC_Calculation(u8 pec[])
  84. {
  85. u8 crc[6];//存放多项式
  86. u8 BitPosition=47;//存放所有数据最高位,6*8=48 最高位就是47
  87. u8 shift;
  88. u8 i;
  89. u8 j;
  90. u8 temp;
  91. do
  92. {
  93. /*Load pattern value 0x00 00 00 00 01 07*/
  94. crc[5]=0;
  95. crc[4]=0;
  96. crc[3]=0;
  97. crc[2]=0;
  98. crc[1]=0x01;
  99. crc[0]=0x07;
  100. /*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
  101. BitPosition=47;
  102. /*Set shift position at 0*/
  103. shift=0;
  104. /*Find first "1" in the transmited message beginning from the MSByte byte5*/
  105. i=5;
  106. j=0;
  107. while((pec[i]&(0x80>>j))==0 && i>0)
  108. {
  109. BitPosition--;
  110. if(j<7)
  111. {
  112. j++;
  113. }
  114. else
  115. {
  116. j=0x00;
  117. i--;
  118. }
  119. }/*End of while */
  120. /*Get shift value for pattern value*/
  121. shift=BitPosition-8;
  122. /*Shift pattern value */
  123. while(shift)
  124. {
  125. for(i=5; i<0xFF; i--)
  126. {
  127. if((crc[i-1]&0x80) && (i>0))
  128. {
  129. temp=1;
  130. }
  131. else
  132. {
  133. temp=0;
  134. }
  135. crc[i]<<=1;
  136. crc[i]+=temp;
  137. }/*End of for*/
  138. shift--;
  139. }/*End of while*/
  140. /*Exclusive OR between pec and crc*/
  141. for(i=0; i<=5; i++)
  142. {
  143. pec[i] ^=crc[i];
  144. }/*End of for*/
  145. }
  146. while(BitPosition>8); /*End of do-while*/
  147. return pec[0];
  148. }
  149. /*******************************************************************************
  150. * 函数名: SMBus_ReadTemp
  151. * 功能: 计算并返回温度值
  152. * Return : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
  153. *******************************************************************************/
  154. float SMBus_ReadTemp(void)
  155. {
  156. float temp;
  157. temp = SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
  158. return temp;
  159. }
  1. #ifndef __MLX90614_H
  2. #define __MLX90614_H
  3. #include "iic.h"
  4. #include "stm32f10x.h" // Device header
  5. #define ACK 0
  6. #define NACK 1 //不应答或否定的应答
  7. #define SA 0x00 //从机地址,单个MLX90614时地址为0x00,多个时地址默认为0x5a
  8. #define RAM_ACCESS 0x00 //RAM access command
  9. #define EEPROM_ACCESS 0x20 //EEPROM access command
  10. #define RAM_TOBJ1 0x07 //To1 address in the eeprom
  11. void SMBus_Init();
  12. u8 PEC_Calculation(u8 pec[]);
  13. u16 SMBus_ReadMemory(u8 slaveAddress, u8 command);
  14. float SMBus_ReadTemp(void);
  15. #endif

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/560303
推荐阅读
相关标签
  

闽ICP备14008679号