找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 10589|回复: 5
收起左侧

MLX90614红外测温模块程序源代码与资料分享

[复制链接]
ID:378239 发表于 2018-7-23 23:43 | 显示全部楼层 |阅读模式
MLX90614ESF+传感器+红外非接触温度测量传感器模块程序源代码,用msp430的单片机

电路原理图如下:
0.png

单片机源程序如下:
  1. //*****************************************************************************
  2. //   MSP430x42x0 Demo - SD16_A, LCD
  3. //*****************************************************************************

  4. //MLX90614 Pin Config

  5. //GND-----6.0.....I/O supply the MLX90614
  6. //SDA-----6.1
  7. //SCL-----6.2
  8. //POW-----6.3.....I/O supply the MLX90614

  9. #include  <msp430x42x0.h>

  10. void LCD_CHECK(void);
  11. void Init_LCD(void);
  12. unsigned int A1,A2,A3,A4,A5;

  13. #define a 0x01
  14. #define b 0x02
  15. #define c 0x04
  16. #define d 0x80
  17. #define e 0x40
  18. #define f 0x10
  19. #define g 0x20
  20. #define h 0x08

  21. const char char_gen[] = {                   // definitions for digits
  22.   a+b+c+d+e+f,                              // Displays "0"
  23.   b+c,                                      // Displays "1"
  24.   a+b+d+e+g,                                // Displays "2"
  25.   a+b+c+d+g,                                // Displays "3"
  26.   b+c+f+g,                                  // Displays "4"
  27.   a+c+d+f+g,                                // Displays "5"
  28.   a+c+d+e+f+g,                              // Displays "6"
  29.   a+b+c,                                    // Displays "7"
  30.   a+b+c+d+e+f+g,                            // Displays "8"
  31.   a+b+c+d+f+g,                              // Displays "9"
  32. };

  33. //*************************************************************
  34. //*************************************************************

  35. void Delay(unsigned int n);
  36. void start_bit();
  37. void stop_bit();
  38. void send_bit(unsigned char bit_out);
  39. unsigned char receive_bit();
  40. unsigned char slave_ack();
  41. void TX_byte(unsigned char TX_buffer);
  42. unsigned char RX_byte(unsigned char ack_nack);
  43. unsigned char PEC_cal(unsigned char pec[],int n);
  44. unsigned long int MEM_READ( unsigned char slave_addR, unsigned char cmdR );                                                
  45. void CALTEMP(unsigned long int TEMP);

  46. void mlx90614_POW_0() { P6OUT &= ~0x08;}  // define P6.3 ---> POW
  47. void mlx90614_POW_1() { P6OUT |= 0x08;}

  48. void mlx90614_GND_0() { P6OUT &= ~0x01;}  // define P6.0 ---> GND
  49. void mlx90614_GND_1() { P6OUT |= 0x01;}

  50. void mlx90614_SCL_0() { P6OUT &= ~0x04;}  // define P6.2 ---> SCL
  51. void mlx90614_SCL_1() { P6OUT |= 0x04;}

  52. void mlx90614_SDA_0() { P6OUT &= ~0x02;}  // define P6.1 ---> SDA
  53. void mlx90614_SDA_1() { P6OUT |= 0x02;}


  54. #define _SDA_OUTPUT P6DIR |=0x02; //Set SDA as Output
  55. #define _SDA_INPUT P6DIR &=~0x02; //Set SDA as Input

  56. #define SDA ((P6IN & BIT1)>>1) //define input pin

  57. //*************************************************************
  58. //*************************************************************

  59. void Delay(unsigned int n)
  60. {
  61.   unsigned int i;
  62.   for(i=0;i<n;i++)
  63.   _NOP();
  64. }

  65. //----------------------------------------------------------------------------------------------------------------------------------------//
  66. //Name: start_bit
  67. //----------------------------------------------------------------------------------------------------------------------------------------//
  68. void start_bit()
  69. {
  70.   _SDA_OUTPUT; //Set SDA as output
  71.   Delay(5);
  72.   mlx90614_SDA_1();
  73.   //Delay(30);
  74.   mlx90614_SCL_1();
  75.   
  76.   Delay(30);
  77.   mlx90614_SDA_0();
  78.   Delay(30);
  79.   mlx90614_SCL_0();
  80.   Delay(30);
  81.   
  82. }

  83. //----------------------------------------------------------------------------------------------------------------------------------------//
  84. //Name: stop_bit
  85. //----------------------------------------------------------------------------------------------------------------------------------------//
  86. void stop_bit()
  87. {
  88.   _SDA_OUTPUT; //Set SDA as output
  89.   Delay(1);
  90.   //mlx90614_SCL_0();
  91.   mlx90614_SDA_0();
  92.   Delay(2);
  93.   mlx90614_SCL_1();
  94.   Delay(2);
  95.   mlx90614_SDA_1();
  96. }

  97. //----------------------------------------------------------------------------------------------------------------------------------------//
  98. //Name: send_bit
  99. //----------------------------------------------------------------------------------------------------------------------------------------//
  100. void send_bit(unsigned char bit_out)
  101. {
  102.   _SDA_OUTPUT; //Set SDA as output
  103.   Delay(5);
  104.   if(bit_out==0) {mlx90614_SDA_0();}else{mlx90614_SDA_1();}
  105.   Delay(5);
  106.   mlx90614_SCL_1();
  107.   Delay(30);
  108.   mlx90614_SCL_0();
  109.   Delay(30);
  110. }

  111. //----------------------------------------------------------------------------------------------------------------------------------------//
  112. //Name: receive_bit
  113. //----------------------------------------------------------------------------------------------------------------------------------------//
  114. unsigned char receive_bit()
  115. {
  116.   unsigned char bit_in;
  117.   _SDA_INPUT; //Set SDA as input
  118.   Delay(5);
  119.   mlx90614_SCL_1();
  120.   Delay(5);
  121.   if(SDA==1){bit_in=1;}else{bit_in=0;}
  122.   Delay(10);
  123.   mlx90614_SCL_0();
  124.   Delay(30);
  125.   return bit_in;
  126. }

  127. //----------------------------------------------------------------------------------------------------------------------------------------//
  128. //Name: slave_ack
  129. //1 - ACK
  130. //0 -NACK
  131. //----------------------------------------------------------------------------------------------------------------------------------------//
  132. unsigned char slave_ack()
  133. {
  134.   unsigned char ack;
  135.   ack=0;
  136.   _SDA_INPUT; //Set SDA as input
  137.   Delay(5);
  138.   mlx90614_SCL_1();
  139.   Delay(10);
  140.   if(SDA==1){ack=0;}else{ack=1;}
  141.   Delay(10);
  142.   mlx90614_SCL_0();
  143.   Delay(30);
  144.   return ack;
  145. }

  146. //----------------------------------------------------------------------------------------------------------------------------------------//
  147. //Name: TX_byte
  148. //----------------------------------------------------------------------------------------------------------------------------------------//
  149. void TX_byte(unsigned char TX_buffer)
  150. {
  151.   unsigned char Bit_counter;
  152.   unsigned char bit_out;
  153.   for(Bit_counter=8;Bit_counter;Bit_counter--)
  154.   {
  155.     if(TX_buffer&0x80){bit_out=1;}else{bit_out=0;}
  156.     send_bit(bit_out); //Send the current bit on SMBus
  157.     TX_buffer<<=1; //Get next bit to check
  158.   }
  159. }

  160. //----------------------------------------------------------------------------------------------------------------------------------------//
  161. //Name: RX_byte
  162. //Parameters: unsigned char ack_nack (acknowledgment bit)
  163. //0 - Master device sends ACK
  164. //1 - Master device sends NACK
  165. //----------------------------------------------------------------------------------------------------------------------------------------//
  166. unsigned char RX_byte(unsigned char ack_nack)
  167. {
  168.         unsigned char RX_buffer;
  169.         unsigned char Bit_counter;
  170.         for(Bit_counter=8;Bit_counter;Bit_counter--)
  171.         {
  172.          if(receive_bit()==1) //Read a bit from the SDA line
  173.           {
  174.            RX_buffer<<=1; //If the bit is HIGH save 1 in RX_buffer
  175.            RX_buffer|=0x01;
  176.           }
  177.          else //If the bit is LOW save 0 in RX_buffer
  178.           {
  179.            RX_buffer<<=1;
  180.            RX_buffer&=0xfe;
  181.           }
  182.         }
  183.         send_bit(ack_nack); //Sends acknowledgment bit
  184.         return RX_buffer;
  185. }


  186. //----------------------------------------------------------------------------------------------------------------------------------------//
  187. //CALCULATE THE PEC PACKET
  188. //----------------------------------------------------------------------------------------------------------------------------------------//
  189. unsigned char PEC_cal(unsigned char pec[],int n)
  190. {
  191.     unsigned char crc[6];
  192.     unsigned char Bitposition=47;
  193.     unsigned char shift;
  194.     unsigned char i;
  195.     unsigned char j;
  196.     unsigned char temp;
  197.   do{
  198.     crc[5]=0; //Load CRC value 0x000000000107
  199.     crc[4]=0;
  200.     crc[3]=0;
  201.     crc[2]=0;
  202.     crc[1]=0x01;
  203.     crc[0]=0x07;
  204.     Bitposition=47; //Set maximum bit position at 47
  205.     shift=0;        //Find first 1 in the transmitted bytes
  206.     i=5; //Set highest index (package byte index)
  207.     j=0; //Byte bit index, from lowest
  208.     while((pec[i]&(0x80>>j))==0 && (i>0))
  209.     {
  210.      Bitposition--;
  211.      if(j<7){ j++;}
  212.      else {j=0x00;i--;}
  213.     }//the position of highest "1" bit in Bitposition is calculated
  214.     shift=Bitposition-8; //Get shift value for CRC value
  215.         
  216.     while(shift)
  217.     {
  218.       for(i=5;i<0xFF;i--)
  219.       {
  220.        if((crc[i-1]&0x80) && (i>0)) //Check if the MSB of the byte lower is "1"
  221.         { //Yes - current byte + 1
  222.          temp=1; //No - current byte + 0
  223.         } //So that "1" can shift between bytes
  224.        else { temp=0;}
  225.       crc[i]<<=1;
  226.       crc[i]+=temp;
  227.       }
  228.     shift--;
  229.     }
  230.     //Exclusive OR between pec and crc
  231.     for(i=0;i<=5;i++) { pec[i]^=crc[i]; }
  232.     }
  233.     while(Bitposition>8);
  234.     return pec[0];
  235.     }
  236.         
  237. //----------------------------------------------------------------------------------------------------------------------------------------//
  238. //READ DATA FROM RAM/EEPROM
  239. //----------------------------------------------------------------------------------------------------------------------------------------//
  240. unsigned long int MEM_READ(unsigned char slave_addR, unsigned char cmdR)
  241. {
  242.   unsigned char DataL; //
  243.   unsigned char DataH; //Data packets from MLX90614
  244.   unsigned char PEC; //
  245.   unsigned long int Data; //Register value returned from MLX90614
  246.   unsigned char Pecreg; //Calculated PEC byte storage
  247.   unsigned char arr[6]; //Buffer for the sent bytes
  248.   unsigned char ack_nack;
  249.   unsigned char SLA;
  250.   SLA=(slave_addR<<1);
  251. begin:
  252.   start_bit(); //Send start bit
  253.   TX_byte(SLA); //Send slave address, write
  254.   if(slave_ack()==0){stop_bit();goto begin;} //Send command
  255.   TX_byte(cmdR);
  256.   if(slave_ack()==0){stop_bit();goto begin;}//Send Repeated start bit
  257.   start_bit(); //Send slave address, read
  258.   TX_byte(SLA+1);
  259.   if(slave_ack()==0){stop_bit();goto begin;}
  260.   DataL=RX_byte(0); //
  261.   //Read two bytes data
  262.   DataH=RX_byte(0); //
  263.   PEC=RX_byte(ack_nack); //Read PEC from MLX90614
  264.   if(ack_nack==1) //Master sends ack or nack
  265.   //This depends on the pec calculation,
  266.   //if the PEC is not correct, send nack and goto begin
  267.   {stop_bit();goto begin;} //Send stop bit
  268.   stop_bit();
  269.   arr[5]=(SLA);
  270.   arr[4]=cmdR;
  271.   arr[3]=(SLA+1);
  272.   arr[2]=DataL;
  273.   arr[1]=DataH;
  274.   arr[0]=0;
  275.   Pecreg=PEC_cal(arr,6); //Calculate CRC
  276.   if(PEC==Pecreg){ ack_nack=0;}
  277.   else{ ack_nack=1;}
  278.   Data=(DataH*256)+DataL;
  279.   return Data;
  280. }

  281. //---------------------------------------
  282. //Name: CALTEMP           
  283. //Temperature data is T=(Data)*0.02-273.15
  284. //---------------------------------------
  285. void CALTEMP(unsigned long int TEMP)
  286. {
  287.    unsigned long int T;
  288.    unsigned int A, B;
  289.    //unsigned int tempb;
  290.    
  291.    T=TEMP*2;
  292.    if(T>=27315)
  293.    {
  294.      T=T-27315;
  295.      A=T/100;
  296.      B=T-A*100;
  297.      if(A>=100){A1=A/100;A=A%100;A2=A/10;A=A%10;A3=A;}
  298.      else if(A>=10){A1=0;A2=A/10;A=A%10;A3=A;}
  299.      else {A1=0;A2=0;A3=A;}
  300.      if(B>=10){A4=B/10;B=B%10;A5=B;}
  301.      else{A4=0;A5=B;}
  302.     }
  303.    else
  304.     {
  305.      T=27315-T;
  306.      A=T/100;
  307.      B=T-A*100;
  308.      A1=9;
  309.      if(A>=10){A2=A/10;A=A%10;A3=A;}
  310.      else{A2=0;A3=A;}
  311.      if(B>=10){ A4=A/10;B=B%10;A5=B;}
  312.      else{A4=0;A5=B;}
  313.     }
  314. }

  315. //------------------------------------------------------------------------------
  316. // LCD Test Code
  317. //------------------------------------------------------------------------------

  318. void LCD_CHECK()  // Clear LCD
  319. {
  320.   int j;
  321.   for( j = 0; j < 8; j ++) { LCDMEM[j] = 0; }
  322. }

  323. //------------------------------------------------------------------------------
  324. // Init_LCD.
  325. //------------------------------------------------------------------------------

  326. void Init_LCD()
  327. {
  328.   P5SEL  = 0x1C;                             // Set COM pins for LCD
  329.   LCDACTL = LCDON + LCD4MUX + LCDFREQ_128;   // 4mux LCD, ACLK/128
  330.   LCDAPCTL0 = 0x0F;                          // Segs S0-S15 = outputs
  331.   LCDAVCTL0 = LCDCPEN;                       // Enable LCDA charge pump
  332.   LCDAVCTL1 = VLCD_3_44;                     // to 3.26V
  333. }


  334. //*************************************************************
  335. //*************************************************************

  336. void main(void)
  337. {
  338.   volatile unsigned int i;                                                         
  339.   WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  340.   
  341.   SCFI0 |= FN_3;                            // Set DCO operating range
  342.   SCFQCTL = 121;                            // (121+1) x 32768 x 2 = 7.99 Mhz
  343.   FLL_CTL0 = DCOPLUS + XCAP14PF;            // DCO+ set so freq = xtal x D x N+1
  344.   for (i = 0; i < 10000; i++);              // Delay for 32 kHz crystal to
  345.   

  346.   P1OUT = 0;                            // All P1.x reset
  347.   P1IES = 0xC0;                         // P1.0, P1.1 hi/low edge
  348.   P1DIR = 0x3F;                         // P1.0/1 = input (switches)
  349.   P6DIR = 0xFF;                         // All P6.x outputs
  350.   P6OUT = 0;                            // All P6.x reset
  351.   P5SEL  = 0x1C;                            // Set COM pins for LCD
  352.   
  353.   mlx90614_GND_0();    //powr supply
  354.   mlx90614_POW_1();
  355.   
  356.   Init_LCD();
  357.   LCD_CHECK();
  358.   while (1)
  359.   {
  360.     unsigned long int i;
  361.     unsigned long int DATA;
  362.                               
  363.     DATA=MEM_READ(0x5a,0x07);
  364.     CALTEMP(DATA);
  365.      
  366.      //start_bit();
  367.      //A4=1; A5=2; A6=3; A7=4; A8=5;
  368.      
  369.      LCDMEM[0] |= a + f + e +d + h;
  370.      
  371.      LCDMEM[1] = char_gen[A5 & 0x0f];
  372.      LCDMEM[2] = char_gen[A4 & 0x0f];
  373.      LCDMEM[3] = char_gen[A3 & 0x0f];
  374.      LCDMEM[3] |= h;
  375.      LCDMEM[4] = char_gen[A2 & 0x0f];
  376.      LCDMEM[5] = char_gen[A1 & 0x0f];
  377.   
  378.      for (i = 0; i < 0x20000; i++);             // Delay  
  379.      for (i = 0; i < 0x20000; i++);             // Delay
  380.      
  381.   }
  382. }
复制代码

所有资料51hei提供下载:
MLX90614ESF 传感器 红外非接触温度测量传感器模块1.zip (14.85 MB, 下载次数: 150)
回复

使用道具 举报

ID:697386 发表于 2020-2-24 14:15 | 显示全部楼层
你好我想学习一下这个软件
回复

使用道具 举报

ID:384489 发表于 2020-2-24 17:18 | 显示全部楼层
仍然感谢分享,虽然最近这个全网无货。
回复

使用道具 举报

ID:697773 发表于 2020-2-25 10:58 | 显示全部楼层
onono 发表于 2020-2-24 17:18
仍然感谢分享,虽然最近这个全网无货。

很应景啊这句话~~~
回复

使用道具 举报

ID:430492 发表于 2020-2-29 18:14 | 显示全部楼层
这个传感器现在炒到几倍价了,还是感谢楼主分享。
回复

使用道具 举报

ID:335836 发表于 2020-5-28 10:03 | 显示全部楼层
这个怎么用啊,程序怎么下单片机怎么接跪求大佬解答一下,来自做设计的卑微大学生
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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