找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3187|回復: 3
收起左側

MLX90614+單片機制作的簡易體外溫度測量

[復制鏈接]
ID:789174 發(fā)表于 2020-11-14 21:38 | 顯示全部樓層 |閱讀模式
IMG_20201013_132819.jpg

單片機源程序如下:
  1. #include"reg52.h"
  2. #include"intrins.h"
  3. #define  uint  unsigned int
  4. #define  uchar unsigned char
  5. #define  Nack_counter  10
  6. uchar num;
  7. signed char alarm=30; //默認為30攝氏度報警
  8. signed char tempalarm=0;
  9. unsigned char szl[3];
  10. //************** 端口定義**************
  11. //LCD 控制線接口
  12. uchar flag1;
  13. sbit  RS=P0^7;
  14. sbit  RW=P0^6;
  15. sbit  LCDE=P0^5;
  16. //按鍵接口
  17. sbit key1=P0^0;
  18. sbit key2=P0^1;
  19. sbit key3=P0^2;
  20. //蜂鳴器報警接口
  21. sbit buzzer= P1^5;          
  22. //mlx90614 端口定義
  23. sbit  SCL=P1^6;// 時鐘線
  24. sbit  SDA=P1^7;// 數(shù)據(jù)線
  25. //************ 數(shù)據(jù)定義****************
  26. bdata uchar flag;//可位尋址數(shù)據(jù)
  27. sbit bit_out=flag^7;
  28. sbit bit_in=flag^0;
  29. uchar DataH,DataL,Pecreg,t,num;
  30. //************ 函數(shù)聲明*****************************************
  31. void   start_bit();                 //MLX90614 發(fā)起始位子程序
  32. void   stop_bit();                 //MLX90614發(fā)結束位子程序
  33. uchar  rx_byte(void);              //MLX90614 接收字節(jié)子程序
  34. void   send_bit(void);             //MLX90614發(fā)送位子程序
  35. void   tx_byte(uchar dat_byte);     //MLX90614 接收字節(jié)子程序
  36. void   receive_bit(void);           //MLX90614接收位子程序
  37. void   delay(uint N);              //延時程序
  38. uint   memread(void);             // 讀溫度數(shù)據(jù)
  39. void   init1602(void);            //LCD 初始化子程序
  40. void   chk_busy_flg(void);         //LCD 判斷忙子程序
  41. void   dis_cmd_wrt(uchar cmd);     //LCD 寫命令子程序
  42. void   dis_dat_wrt(uchar dat);       //LCD 寫數(shù)據(jù)子程序
  43. void   display(uint Tem);           // 顯示子程序
  44. void   baojing(void);                                  //報警子程序

  45. //*************主函數(shù)*******************************************
  46. void main()
  47. {
  48. uint Tem,k;  //函數(shù)部分
  49. SCL=1;
  50. SDA=1;
  51. _nop_(); _nop_();_nop_();_nop_();
  52. SCL=0;
  53. delay(1000);
  54. SCL=1;
  55. init1602();
  56. while(1)
  57. {            if(!key1)
  58.   {
  59.      delay(4000);
  60.          if(!key1&&alarm<125)
  61.          {        delay(4000);
  62.             alarm++;
  63.                 delay(4000);
  64.      }
  65.   }
  66.    if(!key3)
  67.    {  
  68.      delay(4000);
  69.      if(!key3&&alarm>0)
  70.          {  
  71.             delay(4000);
  72.           alarm--;
  73.           delay(4000);
  74.          }
  75.    }
  76.          dis_cmd_wrt(0x80);   
  77.      dis_dat_wrt('T');
  78.          dis_dat_wrt(':');
  79.          dis_cmd_wrt(0x80+0x40);   
  80.      dis_dat_wrt('E');
  81.          dis_dat_wrt(':');
  82.      szl[0]=alarm/100+0x30;
  83.          szl[1]=alarm%100/10+0x30;
  84.          szl[2]=alarm%100%10+0x30;
  85.              dis_cmd_wrt(0x80+0x43);
  86.                  for(k=0;k<3;k++)
  87.                  {
  88.                   dis_dat_wrt(szl[k]);
  89.                   delay(10);
  90.                  }
  91.          dis_cmd_wrt(0x80+0x48);   
  92.      dis_dat_wrt(0xdf);
  93.          dis_dat_wrt('C');
  94.      dis_cmd_wrt(0x88);   
  95.      dis_dat_wrt(0xdf);
  96.          dis_dat_wrt('C');
  97.    Tem=memread();
  98.    display(Tem);
  99.    delay(20);
  100.    tempalarm=num;
  101.    baojing();  
  102. }
  103. }
  104. //*************報警控制*******************************************
  105. void baojing(void)
  106. {  
  107.   if(tempalarm>=alarm)//報警判斷
  108.   {
  109.         for(t=0;t<4;t++)
  110.         {       
  111.         buzzer=0;//打開報警蜂鳴器
  112.         delay(2000);
  113.         buzzer=1;
  114.         delay(2000);
  115.         }
  116.   }   
  117. }
  118. void delay_nms(unsigned int n)
  119. {
  120.         unsigned int j=0;
  121.         unsigned char i=0;
  122.         for(j=0;j<n;j++)
  123.         {
  124.                 for(i=0;i<120;i++)
  125.                 {
  126.                         _nop_();
  127.                 }
  128.         }
  129. }

  130. //*********輸入轉換并顯示*********
  131. void display(uint Tem)
  132. {
  133. uint T,a,b;
  134. T=Tem*2;
  135. if(!key2)
  136. {
  137.     delay(20);
  138.         if(!key2)
  139.          {  
  140.            delay(20);
  141.            T=(0.1923*(T*0.01-273.15)+29.9250)*100+27315;  //人體體溫補償
  142.           
  143.           }
  144.   }
  145.   dis_cmd_wrt(0x83);  //初始顯示位置
  146. if(T>=27315)
  147. {
  148.    T=T-27315;
  149.    a=T/100;
  150.    num=a;
  151.    b=T-a*100;
  152. //---------------------------
  153.    if(a>=100)
  154.    {
  155.     dis_dat_wrt(0x30+a/100);
  156.     a=a%100;
  157.     dis_dat_wrt(0x30+a/10);
  158.     a=a%10;
  159.     dis_dat_wrt(0x30+a);
  160.    }
  161.    else if(a>=10)
  162.    {
  163.     dis_dat_wrt(0x30+a/10);
  164.     a=a%10;
  165.     dis_dat_wrt(0x30+a);
  166.    }
  167.    else
  168.    {
  169.     dis_dat_wrt(0x30+a);
  170.    }
  171.    dis_dat_wrt(0x2e);// 顯示點
  172.    //---------------------------
  173.   if(b>=10)
  174.   {
  175.     dis_dat_wrt(0x30+b/10);
  176.     b=b%10;  
  177.   }
  178.   else
  179.   {
  180.     dis_dat_wrt(0x30);  
  181.   }


  182. }
  183. //==========
  184. else
  185. {
  186.   T=27315-T;
  187.   a=T/100;
  188.   num=a;
  189.   b=T-a*100;
  190.   dis_dat_wrt(0x2d);
  191. //--------------------------
  192.   if(a>=10)
  193.   {
  194.     dis_dat_wrt(0x30+a/10);
  195.     a=a%10;
  196.     dis_dat_wrt(0x30+a);
  197.   }
  198.   else
  199.   {
  200.     dis_dat_wrt(0x30+a);
  201.   }
  202.     dis_dat_wrt(0x2e);//顯示點
  203. //--------------------------
  204.   if(b>=10)
  205.   {
  206.     dis_dat_wrt(0x30+b/10);
  207.     b=b%10;
  208.     dis_dat_wrt(0x30+b);
  209.   }
  210.   else
  211.   {
  212.     dis_dat_wrt(0x30);
  213.     dis_dat_wrt(0x30+b);
  214.   }
  215.   
  216. }
  217. }

  218. //************************************
  219. void   start_bit(void)
  220. {
  221.    SDA=1;
  222.    _nop_();_nop_();_nop_();_nop_();_nop_();
  223.    SCL=1;
  224.    _nop_();_nop_();_nop_();_nop_();_nop_();
  225.    SDA=0;
  226.    _nop_();_nop_();_nop_();_nop_();_nop_();
  227.    SCL=0;
  228.    _nop_();_nop_();_nop_();_nop_();_nop_();

  229. }
  230. //------------------------------
  231. void   stop_bit(void)
  232. {

  233.    SCL=0;
  234.    _nop_();_nop_();_nop_();_nop_();_nop_();
  235.    SDA=0;
  236.    _nop_();_nop_();_nop_();_nop_();_nop_();
  237.    SCL=1;
  238.    _nop_();_nop_();_nop_();_nop_();_nop_();
  239.    SDA=1;
  240. }
  241. //--------- 發(fā)送一個字節(jié)---------
  242. void  tx_byte(uchar dat_byte)
  243. {
  244.    char i,n,dat;
  245.    n=Nack_counter;
  246. TX_again:
  247.    dat=dat_byte;
  248.    for(i=0;i<8;i++)
  249.    {
  250.      if(dat&0x80)
  251.       bit_out=1;
  252.      else
  253.       bit_out=0;
  254.      send_bit();
  255.      dat=dat<<1;
  256.    }
  257.    
  258.       receive_bit();
  259.    if(bit_in==1)
  260.    {
  261.     stop_bit();
  262.     if(n!=0)
  263.     {n--;goto Repeat;}
  264.     else
  265.      goto exit;
  266.    }
  267.    else
  268.     goto exit;
  269. Repeat:
  270.     start_bit();
  271.     goto TX_again;
  272. exit: ;
  273. }
  274. //-----------發(fā)送一個位---------
  275. void  send_bit(void)
  276. {
  277.   if(bit_out==0)
  278.   
  279.   SDA=0;
  280.   else
  281.   SDA=1;
  282.   _nop_();
  283.   SCL=1;
  284.   _nop_();_nop_();_nop_();_nop_();
  285.   _nop_();_nop_();_nop_();_nop_();
  286.   SCL=0;
  287.   _nop_();_nop_();_nop_();_nop_();
  288.   _nop_();_nop_();_nop_();_nop_();
  289. }
  290. //---------- 接收一個字節(jié)--------
  291. uchar rx_byte(void)
  292. {
  293.   uchar i,dat;
  294.   dat=0;
  295.   for(i=0;i<8;i++)
  296.   {
  297.     dat=dat<<1;
  298.     receive_bit();
  299.     if(bit_in==1)
  300.     dat=dat+1;
  301.   }
  302.   send_bit();
  303.   return dat;
  304. }

  305. //---------- 接收一個位----------
  306. void receive_bit(void)
  307. {
  308.   SDA=1;bit_in=1;
  309.   SCL=1;
  310.   _nop_();_nop_();_nop_();_nop_();
  311.   _nop_();_nop_();_nop_();_nop_();
  312.   bit_in=SDA;
  313.   _nop_();
  314.   SCL=0;
  315.   _nop_();_nop_();_nop_();_nop_();
  316.   _nop_();_nop_();_nop_();_nop_();
  317. }
  318. //------------ 延時--------------
  319. void   delay(uint N)
  320. {
  321.   uint i;
  322.   for(i=0;i<N;i++)  
  323.      _nop_();
  324. }
  325. //------------------------------
  326. uint memread(void)
  327. {
  328.   start_bit();
  329.   tx_byte(0xB4);  //Send SlaveAddress ==============================
  330.   tx_byte(0x07);  //Send Command
  331.   //------------
  332.   start_bit();
  333.   tx_byte(0x01);
  334.   bit_out=0;
  335.   DataL=rx_byte();
  336.   bit_out=0;
  337.   DataH=rx_byte();
  338.   bit_out=1;
  339.   Pecreg=rx_byte();
  340.   stop_bit();
  341.   return(DataH*256+DataL);
  342. }
  343. //******************LCD 顯示子函數(shù)***********************
  344. void init1602(void)        // 初始化LCD
  345. {
  346.    dis_cmd_wrt(0x01);
  347.    dis_cmd_wrt(0x0c);
  348.    dis_cmd_wrt(0x06);
  349.    dis_cmd_wrt(0x38);
  350.    
  351. ……………………

  352. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有程序51hei提供下載:
溫度測量.zip (12.65 KB, 下載次數(shù): 64)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

ID:869708 發(fā)表于 2021-1-1 22:36 | 顯示全部樓層
我用51單片機做調試,晶振是用11.0592M,發(fā)現(xiàn)這個程序使用不了,返回值都是255
回復

使用道具 舉報

ID:959370 發(fā)表于 2021-8-11 16:19 | 顯示全部樓層
可以分享一下原理圖嗎
回復

使用道具 舉報

ID:953538 發(fā)表于 2021-8-12 08:26 | 顯示全部樓層
把原理圖補上就好了,配置齊全了。
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表