找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5549|回复: 1
打印 上一主题 下一主题
收起左侧

基于stm32的非接触式MLX90614红外测温模块程序

[复制链接]
跳转到指定楼层
楼主
ID:537887 发表于 2019-5-29 09:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
附上可用程序,波特率为115200,接口为b7,b8

单片机源程序如下:

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "mlx90614.h"


  3. /* Private typedef -----------------------------------------------------------*/
  4. /* Private define ------------------------------------------------------------*/
  5. #define ACK         0
  6. #define NACK          1
  7. #define SA                          0x00 //Slave address ??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. #define SMBUS_PORT            GPIOB
  12. #define SMBUS_SCK                GPIO_Pin_6
  13. #define SMBUS_SDA                GPIO_Pin_7

  14. #define RCC_APB2Periph_SMBUS_PORT                RCC_APB2Periph_GPIOB

  15. #define SMBUS_SCK_H()            SMBUS_PORT->BSRR = SMBUS_SCK
  16. #define SMBUS_SCK_L()            SMBUS_PORT->BRR = SMBUS_SCK
  17. #define SMBUS_SDA_H()            SMBUS_PORT->BSRR = SMBUS_SDA
  18. #define SMBUS_SDA_L()            SMBUS_PORT->BRR = SMBUS_SDA

  19. #define SMBUS_SDA_PIN()            SMBUS_PORT->IDR & SMBUS_SDA //??????

  20. /* Private macro -------------------------------------------------------------*/
  21. /* Private variables ---------------------------------------------------------*/


  22. /*******************************************************************************
  23. * Function Name  : SMBus_Init
  24. * Description    : SMBus???
  25. * Input          : None
  26. * Output         : None
  27. * Return         : None
  28. *******************************************************************************/
  29. void SMBus_Init()
  30. {
  31.     GPIO_InitTypeDef    GPIO_InitStructure;

  32.         /* Enable SMBUS_PORT clocks */
  33.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SMBUS_PORT, ENABLE);

  34.     /*??SMBUS_SCK?SMBUS_SDA????????*/
  35.     GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA;
  36.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  37.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38.     GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);

  39.     SMBUS_SCK_H();
  40.     SMBUS_SDA_H();
  41. }





  42. /*******************************************************************************
  43. * Function Name  : SMBus_StartBit
  44. * Description    : Generate START condition on SMBus
  45. * Input          : None
  46. * Output         : None
  47. * Return         : None
  48. *******************************************************************************/
  49. void SMBus_StartBit(void)
  50. {
  51.     SMBUS_SDA_H();                // Set SDA line
  52.     SMBus_Delay(1);            // Wait a few microseconds
  53.     SMBUS_SCK_H();                // Set SCL line
  54.     SMBus_Delay(5);            // Generate bus free time between Stop
  55.     SMBUS_SDA_L();                // Clear SDA line
  56.     SMBus_Delay(10);            // Hold time after (Repeated) Start
  57.     // Condition. After this period, the first clock is generated.
  58.     //(Thd:sta=4.0us min)
  59.     SMBUS_SCK_L();            // Clear SCL line
  60.     SMBus_Delay(2);            // Wait a few microseconds
  61. }

  62. /*******************************************************************************
  63. * Function Name  : SMBus_StopBit
  64. * Description    : Generate STOP condition on SMBus
  65. * Input          : None
  66. * Output         : None
  67. * Return         : None
  68. *******************************************************************************/
  69. void SMBus_StopBit(void)
  70. {
  71.     SMBUS_SCK_L();                // Clear SCL line
  72.     SMBus_Delay(5);            // Wait a few microseconds
  73.     SMBUS_SDA_L();                // Clear SDA line
  74.     SMBus_Delay(5);            // Wait a few microseconds
  75.     SMBUS_SCK_H();                // Set SCL line
  76.     SMBus_Delay(10);            // Stop condition setup time(Tsu:sto=4.0us min)
  77.     SMBUS_SDA_H();                // Set SDA line
  78. }

  79. /*******************************************************************************
  80. * Function Name  : SMBus_SendByte
  81. * Description    : Send a byte on SMBus
  82. * Input          : Tx_buffer
  83. * Output         : None
  84. * Return         : None
  85. *******************************************************************************/
  86. u8 SMBus_SendByte(u8 Tx_buffer)
  87. {
  88.     u8        Bit_counter;
  89.     u8        Ack_bit;
  90.     u8        bit_out;

  91.     for(Bit_counter=8; Bit_counter; Bit_counter--)
  92.     {
  93.         if (Tx_buffer&0x80)
  94.         {
  95.             bit_out=1;   // If the current bit of Tx_buffer is 1 set bit_out
  96.         }
  97.         else
  98.         {
  99.             bit_out=0;  // else clear bit_out
  100.         }
  101.         SMBus_SendBit(bit_out);                // Send the current bit on SDA
  102.         Tx_buffer<<=1;                                // Get next bit for checking
  103.     }

  104.     Ack_bit=SMBus_ReceiveBit();                // Get acknowledgment bit
  105.     return        Ack_bit;
  106. }

  107. /*******************************************************************************
  108. * Function Name  : SMBus_SendBit
  109. * Description    : Send a bit on SMBus
  110. * Input          : bit_out
  111. * Output         : None
  112. * Return         : None
  113. *******************************************************************************/
  114. void SMBus_SendBit(u8 bit_out)
  115. {
  116.     if(bit_out==0)
  117.     {
  118.         SMBUS_SDA_L();
  119.     }
  120.     else
  121.     {
  122.         SMBUS_SDA_H();
  123.     }
  124.     SMBus_Delay(2);                                        // Tsu:dat = 250ns minimum
  125.     SMBUS_SCK_H();                                        // Set SCL line
  126.     SMBus_Delay(10);                            // High Level of Clock Pulse
  127.     SMBUS_SCK_L();                                        // Clear SCL line
  128.     SMBus_Delay(10);                            // Low Level of Clock Pulse
  129. //        SMBUS_SDA_H();                                    // Master release SDA line ,
  130.     return;
  131. }

  132. /*******************************************************************************
  133. * Function Name  : SMBus_ReceiveBit
  134. * Description    : Receive a bit on SMBus
  135. * Input          : None
  136. * Output         : None
  137. * Return         : Ack_bit
  138. *******************************************************************************/
  139. u8 SMBus_ReceiveBit(void)
  140. {
  141.     u8 Ack_bit;

  142.     SMBUS_SDA_H();          //?????????,????
  143.     SMBUS_SCK_H();                        // Set SCL line
  144.     SMBus_Delay(2);                        // High Level of Clock Pulse
  145.     if (SMBUS_SDA_PIN())
  146.     {
  147.         Ack_bit=1;
  148.     }
  149.     else
  150.     {
  151.         Ack_bit=0;
  152.     }
  153.     SMBUS_SCK_L();                        // Clear SCL line
  154.     SMBus_Delay(4);                        // Low Level of Clock Pulse

  155.     return        Ack_bit;
  156. }

  157. /*******************************************************************************
  158. * Function Name  : SMBus_ReceiveByte
  159. * Description    : Receive a byte on SMBus
  160. * Input          : ack_nack
  161. * Output         : None
  162. * Return         : RX_buffer
  163. *******************************************************************************/
  164. u8 SMBus_ReceiveByte(u8 ack_nack)
  165. {
  166.     u8         RX_buffer;
  167.     u8        Bit_Counter;

  168.     for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
  169.     {
  170.         if(SMBus_ReceiveBit())                        // Get a bit from the SDA line
  171.         {
  172.             RX_buffer <<= 1;                        // If the bit is HIGH save 1  in RX_buffer
  173.             RX_buffer |=0x01;
  174.         }
  175.         else
  176.         {
  177.             RX_buffer <<= 1;                        // If the bit is LOW save 0 in RX_buffer
  178.             RX_buffer &=0xfe;
  179.         }
  180.     }
  181.     SMBus_SendBit(ack_nack);                        // Sends acknowledgment bit
  182.     return RX_buffer;
  183. }

  184. /*******************************************************************************
  185. * Function Name  : SMBus_Delay
  186. * Description    : ??  ?????1us
  187. * Input          : time
  188. * Output         : None
  189. * Return         : None
  190. *******************************************************************************/
  191. void SMBus_Delay(u16 time)
  192. {
  193.     u16 i, j;
  194.     for (i=0; i<4; i++)
  195.     {
  196.         for (j=0; j<time; j++);
  197.     }
  198. }

  199. /*******************************************************************************
  200. * Function Name  : SMBus_ReadMemory
  201. * Description    : READ DATA FROM RAM/EEPROM
  202. * Input          : slaveAddress, command
  203. * Output         : None
  204. * Return         : Data
  205. *******************************************************************************/
  206. u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
  207. {
  208.     u16 data;                        // Data storage (DataH:DataL)
  209.     u8 Pec;                                // PEC byte storage
  210.     u8 DataL=0;                        // Low data byte storage
  211.     u8 DataH=0;                        // High data byte storage
  212.     u8 arr[6];                        // Buffer for the sent bytes
  213.     u8 PecReg;                        // Calculated PEC byte storage
  214.     u8 ErrorCounter;        // Defines the number of the attempts for communication with MLX90614

  215.     ErrorCounter=0x00;                                // Initialising of ErrorCounter
  216.         slaveAddress <<= 1;        //2-7???????
  217.         
  218.     do
  219.     {
  220. repeat:
  221.         SMBus_StopBit();                            //If slave send NACK stop comunication
  222.         --ErrorCounter;                                    //Pre-decrement ErrorCounter
  223.         if(!ErrorCounter)                             //ErrorCounter=0?
  224.         {
  225.             break;                                            //Yes,go out from do-while{}
  226.         }

  227.         SMBus_StartBit();                                //Start condition
  228.         if(SMBus_SendByte(slaveAddress))//Send SlaveAddress ???Wr=0????????
  229.         {
  230.             goto        repeat;                            //Repeat comunication again
  231.         }
  232.         if(SMBus_SendByte(command))            //Send command
  233.         {
  234.             goto        repeat;                            //Repeat comunication again
  235.         }

  236.         SMBus_StartBit();                                        //Repeated Start condition
  237.         if(SMBus_SendByte(slaveAddress+1))        //Send SlaveAddress ???Rd=1????????
  238.         {
  239.             goto        repeat;                     //Repeat comunication again
  240.         }

  241.         DataL = SMBus_ReceiveByte(ACK);        //Read low data,master must send ACK
  242.         DataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACK
  243.         Pec = SMBus_ReceiveByte(NACK);        //Read PEC byte, master must send NACK
  244.         SMBus_StopBit();                                //Stop condition

  245.         arr[5] = slaveAddress;                //
  246.         arr[4] = command;                        //
  247.         arr[3] = slaveAddress+1;        //Load array arr
  248.         arr[2] = DataL;                                //
  249.         arr[1] = DataH;                                //
  250.         arr[0] = 0;                                        //
  251.         PecReg=PEC_Calculation(arr);//Calculate CRC
  252.     }
  253.     while(PecReg != Pec);                //If received and calculated CRC are equal go out from do-while{}

  254.         data = (DataH<<8) | DataL;        //data=DataH:DataL
  255.     return data;
  256. }

  257. /*******************************************************************************
  258. * Function Name  : PEC_calculation
  259. * Description    : Calculates the PEC of received bytes
  260. * Input          : pec[]
  261. * Output         : None
  262. * Return         : pec[0]-this byte contains calculated crc value
  263. *******************************************************************************/
  264. u8 PEC_Calculation(u8 pec[])
  265. {
  266.     u8         crc[6];
  267.     u8        BitPosition=47;
  268.     u8        shift;
  269.     u8        i;
  270.     u8        j;
  271.     u8        temp;

  272.     do
  273.     {
  274.         /*Load pattern value 0x000000000107*/
  275.         crc[5]=0;
  276.         crc[4]=0;
  277.         crc[3]=0;
  278.         crc[2]=0;
  279.         crc[1]=0x01;
  280.         crc[0]=0x07;

  281.         /*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
  282.         BitPosition=47;

  283.         /*Set shift position at 0*/
  284.         shift=0;

  285.         /*Find first "1" in the transmited message beginning from the MSByte byte5*/
  286.         i=5;
  287.         j=0;
  288.         while((pec[i]&(0x80>>j))==0 && i>0)
  289.         {
  290.             BitPosition--;
  291.             if(j<7)
  292.             {
  293.                 j++;
  294.             }
  295.             else
  296.             {
  297.                 j=0x00;
  298.                 i--;
  299.             }
  300.         }/*End of while */

  301.         /*Get shift value for pattern value*/
  302.         shift=BitPosition-8;

  303.         /*Shift pattern value */
  304.         while(shift)
  305.         {
  306.             for(i=5; i<0xFF; i--)
  307.             {
  308.                 if((crc[i-1]&0x80) && (i>0))
  309.                 {
  310.                     temp=1;
  311.                 }
  312.                 else
  313.                 {
  314.                     temp=0;
  315.                 }
  316.                 crc[i]<<=1;
  317.                 crc[i]+=temp;
  318.             }/*End of for*/
  319.             shift--;
  320.         }/*End of while*/

  321.         /*Exclusive OR between pec and crc*/
  322.         for(i=0; i<=5; i++)
  323.         {
  324.             pec[i] ^=crc[i];
  325.         }/*End of for*/
  326.     }
  327.     while(BitPosition>8); /*End of do-while*/

  328.     return pec[0];
  329. }

  330. /*******************************************************************************
  331. * Function Name  : SMBus_ReadTemp
  332. * Description    : Calculate and return the temperature
  333. * Input          : None
  334. * Output         : None
  335. * Return         : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
  336. *******************************************************************************/
  337. float SMBus_ReadTemp(void)
  338. {   
  339.     return SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
  340. }

  341. /*********************************END OF FILE*********************************/
复制代码

所有资料51hei提供下载:
mlx90614.7z (186.68 KB, 下载次数: 213)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:593810 发表于 2019-11-16 18:34 | 只看该作者
楼主,你这个程序给串口发送什么数据才能在串口上接受显示?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表