找回密码
 立即注册

QQ登录

只需一步,快速开始

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

MLX90614的MSP430G2553驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:474127 发表于 2019-1-30 14:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. //MLX90614 Pin Config

  2. //GND-----1.5.....I/O supply the MLX90614
  3. //SCL-----2.0
  4. //SCL-----2.1
  5. //POW-----2.2.....I/O supply the MLX90614

  6. #include  <msp430g2553.h> // System define for the micro that I am using

  7. unsigned int A4,A5,A6,A7,A8;
  8. unsigned char dis_code[10] = {48,49,50,51,52,  //段码表  
  9.                             //0  1  2  3  4      对应内容
  10.                               53,54,55,56,57};   
  11.                             //5  6  7  8  9

  12. void Delay(unsigned int n);
  13. void start_bit();
  14. void stop_bit();
  15. void send_bit(unsigned char bit_out);
  16. unsigned char receive_bit();
  17. unsigned char slave_ack();
  18. void TX_byte(unsigned char TX_buffer);
  19. unsigned char RX_byte(unsigned char ack_nack);
  20. unsigned char PEC_cal(unsigned char pec[],int n);
  21. unsigned long int MEM_READ( unsigned char slave_addR, unsigned char cmdR );                                                
  22. void CALTEMP(unsigned long int TEMP);

  23. void mlx90614_POW_0() { P1OUT &= ~0x20;}  // define P1.5 ---> POW
  24. void mlx90614_POW_1() { P1OUT |= 0x20;}

  25. void mlx90614_GND_0() { P2OUT &= ~0x04;}  // define P2.2 ---> GND
  26. void mlx90614_GND_1() { P2OUT |= 0x04;}

  27. void mlx90614_SCL_0() { P2OUT &= ~0x01;}  // define P2.0 ---> SCL
  28. void mlx90614_SCL_1() { P2OUT |= 0x01;}

  29. void mlx90614_SDA_0() { P2OUT &= ~0x02;}  // define P2.1 ---> SDA
  30. void mlx90614_SDA_1() { P2OUT |= 0x02;}


  31. #define _SDA_OUTPUT P2DIR |=0x02; //Set SDA as Output
  32. #define _SDA_INPUT P2DIR &=~0x02; //Set SDA as Input

  33. #define SDA ((P2IN & BIT1)>>1) //define input pin

  34. #define RXD      BIT1
  35. #define TXD      BIT2

  36. void SendByte(unsigned char Data);
  37. void UartTXdata(void);

  38. void main(void)
  39. {
  40.   WDTCTL = WDTPW + WDTHOLD;         // Stop Watch dog timer

  41.   BCSCTL1 = CALBC1_16MHZ;            // Set DCO to 16 MHz
  42.   DCOCTL = CALDCO_16MHZ;

  43.   P1DIR = 0xFF;     // All P1.x outputs
  44.   P1OUT = 0;        // All P1.x reset
  45.   P2DIR = 0xFF;     // All P2.x outputs
  46.   P2OUT = 0;        // All P2.x reset

  47.   P1SEL = RXD + TXD ;                // Select TX and RX functionality for P1.1 & P1.2
  48.   P1SEL2 = RXD + TXD ;              //

  49.   UCA0CTL1 |= UCSSEL_2;             // Have USCI use System Master Clock: AKA core clk 1MHz

  50.   UCA0BR0 = 131;                    // 16MHz 9600, see user manual
  51.   UCA0BR1 = 6;                      //

  52.   UCA0MCTL = UCBRS0;                // Modulation UCBRSx = 1
  53.   UCA0CTL1 &= ~UCSWRST;             // Start USCI state machine

  54.   mlx90614_GND_0();    //powr supply
  55.   mlx90614_POW_1();

  56.   while(1)                          // While 1 is equal to 1 (forever)
  57.   {
  58.    unsigned long int DATA;

  59.    DATA=MEM_READ(0x5a,0x07);
  60.    CALTEMP(DATA);

  61.    //Delay(1); //delay 340nS
  62.    //Delay(10); //delay 4uS
  63.    //Delay(60); //delay 20uS
  64.    //Delay(100); //delay 34uS
  65.    //SendByte(0x55);

  66.     SendByte('T');
  67.     SendByte(':');
  68.     SendByte(32);
  69.     SendByte(dis_code[A4]);
  70.     SendByte(dis_code[A5]);
  71.     SendByte(dis_code[A6]);
  72.     SendByte('.');
  73.     SendByte(dis_code[A7]);
  74.     SendByte(dis_code[A8]);
  75.     SendByte('\n');
  76.     Delay(32000);
  77.     Delay(32000);
  78.     Delay(32000);
  79.     Delay(32000);
  80.     Delay(32000);
  81.     Delay(32000);
  82.     Delay(32000);
  83.     Delay(32000);
  84.     Delay(32000);
  85.   }
  86. }

  87. void SendByte(unsigned char Data)
  88. {
  89.   while ((UCA0STAT & UCBUSY));
  90.   UCA0TXBUF = Data;
  91. }


  92. // USER CODE BEGIN (MAIN_General,10)

  93. void Delay(unsigned int n)
  94. {
  95.   unsigned int i;
  96.   for(i=0;i<n;i++)
  97.   _NOP();
  98. }


  99. //----------------------------------------------------------------------------------------------------------------------------------------//
  100. //Name: start_bit
  101. //----------------------------------------------------------------------------------------------------------------------------------------//
  102. void start_bit()
  103. {
  104.   _SDA_OUTPUT; //Set SDA as output
  105.   Delay(5);
  106.   mlx90614_SDA_1();
  107.   //Delay(30);
  108.   mlx90614_SCL_1();

  109.   Delay(30);
  110.   mlx90614_SDA_0();
  111.   Delay(30);
  112.   mlx90614_SCL_0();
  113.   Delay(30);

  114. }

  115. //----------------------------------------------------------------------------------------------------------------------------------------//
  116. //Name: stop_bit
  117. //----------------------------------------------------------------------------------------------------------------------------------------//
  118. void stop_bit()
  119. {
  120.   _SDA_OUTPUT; //Set SDA as output
  121.   Delay(5);
  122.   //mlx90614_SCL_0();
  123.   mlx90614_SDA_0();
  124.   Delay(30);
  125.   mlx90614_SCL_1();
  126.   Delay(30);
  127.   mlx90614_SDA_1();
  128. }

  129. //----------------------------------------------------------------------------------------------------------------------------------------//
  130. //Name: send_bit
  131. //----------------------------------------------------------------------------------------------------------------------------------------//
  132. void send_bit(unsigned char bit_out)
  133. {
  134.   _SDA_OUTPUT; //Set SDA as output
  135.   Delay(5);
  136.   if(bit_out==0) {mlx90614_SDA_0();}else{mlx90614_SDA_1();}
  137.   Delay(5);
  138.   mlx90614_SCL_1();
  139.   Delay(30);
  140.   mlx90614_SCL_0();
  141.   Delay(30);
  142. }

  143. //----------------------------------------------------------------------------------------------------------------------------------------//
  144. //Name: receive_bit
  145. //----------------------------------------------------------------------------------------------------------------------------------------//
  146. unsigned char receive_bit()
  147. {
  148.   unsigned char bit_in;
  149.   _SDA_INPUT; //Set SDA as input
  150.   Delay(5);
  151.   mlx90614_SCL_1();
  152.   Delay(5);
  153.   if(SDA==1){bit_in=1;}else{bit_in=0;}
  154.   Delay(10);
  155.   mlx90614_SCL_0();
  156.   Delay(30);
  157.   return bit_in;
  158. }

  159. //----------------------------------------------------------------------------------------------------------------------------------------//
  160. //Name: slave_ack
  161. //1 - ACK
  162. //0 -NACK
  163. //----------------------------------------------------------------------------------------------------------------------------------------//
  164. unsigned char slave_ack()
  165. {
  166.   unsigned char ack;
  167.   ack=0;
  168.   _SDA_INPUT; //Set SDA as input
  169.   Delay(5);
  170.   mlx90614_SCL_1();
  171.   Delay(10);
  172.   if(SDA==1){ack=0;}else{ack=1;}
  173.   Delay(10);
  174.   mlx90614_SCL_0();
  175.   Delay(30);
  176.   return ack;
  177. }

  178. //----------------------------------------------------------------------------------------------------------------------------------------//
  179. //Name: TX_byte
  180. //----------------------------------------------------------------------------------------------------------------------------------------//
  181. void TX_byte(unsigned char TX_buffer)
  182. {
  183.   unsigned char Bit_counter;
  184.   unsigned char bit_out;
  185.   for(Bit_counter=8;Bit_counter;Bit_counter--)
  186.   {
  187.     if(TX_buffer&0x80){bit_out=1;}else{bit_out=0;}
  188.     send_bit(bit_out); //Send the current bit on SMBus
  189.     TX_buffer<<=1; //Get next bit to check
  190.   }
  191. }

  192. //----------------------------------------------------------------------------------------------------------------------------------------//
  193. //Name: RX_byte
  194. //Parameters: unsigned char ack_nack (acknowledgment bit)
  195. //0 - Master device sends ACK
  196. //1 - Master device sends NACK
  197. //----------------------------------------------------------------------------------------------------------------------------------------//
  198. unsigned char RX_byte(unsigned char ack_nack)
  199. {
  200.         unsigned char RX_buffer;
  201.         unsigned char Bit_counter;
  202.         for(Bit_counter=8;Bit_counter;Bit_counter--)
  203.         {
  204.          if(receive_bit()==1) //Read a bit from the SDA line
  205.           {
  206.            RX_buffer<<=1; //If the bit is HIGH save 1 in RX_buffer
  207.            RX_buffer|=0x01;
  208.           }
  209.          else //If the bit is LOW save 0 in RX_buffer
  210.           {
  211.            RX_buffer<<=1;
  212.            RX_buffer&=0xfe;
  213.           }
  214.         }
  215.         send_bit(ack_nack); //Sends acknowledgment bit
  216.         return RX_buffer;
  217. }


  218. //----------------------------------------------------------------------------------------------------------------------------------------//
  219. //CALCULATE THE PEC PACKET
  220. //----------------------------------------------------------------------------------------------------------------------------------------//
  221. unsigned char PEC_cal(unsigned char pec[],int n)
  222. {
  223.     unsigned char crc[6];
  224.     unsigned char Bitposition=47;
  225.     unsigned char shift;
  226.     unsigned char i;
  227.     unsigned char j;
  228.     unsigned char temp;
  229.   do{
  230.     crc[5]=0; //Load CRC value 0x000000000107
  231.     crc[4]=0;
  232.     crc[3]=0;
  233.     crc[2]=0;
  234.     crc[1]=0x01;
  235.     crc[0]=0x07;
  236.     Bitposition=47; //Set maximum bit position at 47
  237.     shift=0;        //Find first 1 in the transmitted bytes
  238.     i=5; //Set highest index (package byte index)
  239.     j=0; //Byte bit index, from lowest
  240.     while((pec[i]&(0x80>>j))==0 && (i>0))
  241.     {
  242.      Bitposition--;
  243.      if(j<7){ j++;}
  244.      else {j=0x00;i--;}
  245.     }//the position of highest "1" bit in Bitposition is calculated
  246.     shift=Bitposition-8; //Get shift value for CRC value
  247.         
  248.     while(shift)
  249.     {
  250.       for(i=5;i<0xFF;i--)
  251.       {
  252.        if((crc[i-1]&0x80) && (i>0)) //Check if the MSB of the byte lower is "1"
  253.         { //Yes - current byte + 1
  254.          temp=1; //No - current byte + 0
  255.         } //So that "1" can shift between bytes
  256.        else { temp=0;}
  257.       crc[i]<<=1;
  258.       crc[i]+=temp;
  259.       }
  260.     shift--;
  261.     }
  262.     //Exclusive OR between pec and crc
  263.     for(i=0;i<=5;i++) { pec[i]^=crc[i]; }
  264.     }
  265.     while(Bitposition>8);
  266.     return pec[0];
  267.     }
  268.         
  269. //----------------------------------------------------------------------------------------------------------------------------------------//
  270. //READ DATA FROM RAM/EEPROM
  271. //----------------------------------------------------------------------------------------------------------------------------------------//
  272. unsigned long int MEM_READ(unsigned char slave_addR, unsigned char cmdR)
  273. {
  274.   unsigned char DataL; //
  275.   unsigned char DataH; //Data packets from MLX90614
  276.   unsigned char PEC; //
  277.   unsigned long int Data; //Register value returned from MLX90614
  278.   unsigned char Pecreg; //Calculated PEC byte storage
  279.   unsigned char arr[6]; //Buffer for the sent bytes
  280.   unsigned char ack_nack;
  281.   unsigned char SLA;
  282.   SLA=(slave_addR<<1);
  283. begin:
  284.   start_bit(); //Send start bit
  285.   TX_byte(SLA); //Send slave address, write
  286.   if(slave_ack()==0){stop_bit();goto begin;} //Send command
  287.   TX_byte(cmdR);
  288.   if(slave_ack()==0){stop_bit();goto begin;}//Send Repeated start bit
  289.   start_bit(); //Send slave address, read
  290.   TX_byte(SLA+1);
  291.   if(slave_ack()==0){stop_bit();goto begin;}
  292.   DataL=RX_byte(0); //
  293.   //Read two bytes data
  294.   DataH=RX_byte(0); //
  295.   PEC=RX_byte(ack_nack); //Read PEC from MLX90614
  296.   if(ack_nack==1) //Master sends ack or nack
  297.   //This depends on the pec calculation,
  298.   //if the PEC is not correct, send nack and goto begin
  299.   {stop_bit();goto begin;} //Send stop bit
  300.   stop_bit();
  301.   arr[5]=(SLA);
  302.   arr[4]=cmdR;
  303.   arr[3]=(SLA+1);
  304.   arr[2]=DataL;
  305.   arr[1]=DataH;
  306.   arr[0]=0;
  307.   Pecreg=PEC_cal(arr,6); //Calculate CRC
  308.   if(PEC==Pecreg){ ack_nack=0;}
  309.   else{ ack_nack=1;}
  310.   Data=(DataH*256)+DataL;
  311.   return Data;
  312. }

  313. //---------------------------------------
  314. //Name: CALTEMP           
  315. //Temperature data is T=(Data)*0.02-273.15
  316. //---------------------------------------
  317. void CALTEMP(unsigned long int TEMP)
  318. {
  319.    unsigned long int T;
  320.    unsigned int a,b;
  321.    T=TEMP*2;
  322.    if(T>=27315)
  323.    {
  324.      T=T-27315;
  325.      a=T/100;
  326.      b=T-a*100;
  327.      if(a>=100){A4=a/100;a=a%100;A5=a/10;a=a%10;A6=a;}
  328.      else if(a>=10){A4=0;A5=a/10;a=a%10;A6=a;}
  329.      else {A4=0;A5=0;A6=a;}
  330.      if(b>=10){A7=b/10;b=b%10;A8=b;}
  331.      else{A7=0;A8=b;}
  332.     }
  333.    else
  334.     {
  335.      T=27315-T;
  336.      a=T/100;
  337.      b=T-a*100;
  338.      A4=9;
  339.      if(a>=10){A5=a/10;a=a%10;A6=a;}
  340.      else{A5=0;A6=a;}
  341.      if(b>=10){ A7=b/10;b=b%10;A8=b;}
  342.      else{A7=0;A8=b;}
  343.     }
  344. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶2 踩
回复

使用道具 举报

沙发
ID:474127 发表于 2019-1-30 14:22 | 只看该作者
第一次玩这个,请大神多多指教
回复

使用道具 举报

板凳
ID:1 发表于 2019-1-30 16:09 | 只看该作者
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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