單片機源程序如下:
- #include"reg52.h"
- #include"intrins.h"
- #define uint unsigned int
- #define uchar unsigned char
- #define Nack_counter 10
- uchar num;
- signed char alarm=30; //默認為30攝氏度報警
- signed char tempalarm=0;
- unsigned char szl[3];
- //************** 端口定義**************
- //LCD 控制線接口
- uchar flag1;
- sbit RS=P0^7;
- sbit RW=P0^6;
- sbit LCDE=P0^5;
- //按鍵接口
- sbit key1=P0^0;
- sbit key2=P0^1;
- sbit key3=P0^2;
- //蜂鳴器報警接口
- sbit buzzer= P1^5;
- //mlx90614 端口定義
- sbit SCL=P1^6;// 時鐘線
- sbit SDA=P1^7;// 數(shù)據(jù)線
- //************ 數(shù)據(jù)定義****************
- bdata uchar flag;//可位尋址數(shù)據(jù)
- sbit bit_out=flag^7;
- sbit bit_in=flag^0;
- uchar DataH,DataL,Pecreg,t,num;
- //************ 函數(shù)聲明*****************************************
- void start_bit(); //MLX90614 發(fā)起始位子程序
- void stop_bit(); //MLX90614發(fā)結束位子程序
- uchar rx_byte(void); //MLX90614 接收字節(jié)子程序
- void send_bit(void); //MLX90614發(fā)送位子程序
- void tx_byte(uchar dat_byte); //MLX90614 接收字節(jié)子程序
- void receive_bit(void); //MLX90614接收位子程序
- void delay(uint N); //延時程序
- uint memread(void); // 讀溫度數(shù)據(jù)
- void init1602(void); //LCD 初始化子程序
- void chk_busy_flg(void); //LCD 判斷忙子程序
- void dis_cmd_wrt(uchar cmd); //LCD 寫命令子程序
- void dis_dat_wrt(uchar dat); //LCD 寫數(shù)據(jù)子程序
- void display(uint Tem); // 顯示子程序
- void baojing(void); //報警子程序
- //*************主函數(shù)*******************************************
- void main()
- {
- uint Tem,k; //函數(shù)部分
- SCL=1;
- SDA=1;
- _nop_(); _nop_();_nop_();_nop_();
- SCL=0;
- delay(1000);
- SCL=1;
- init1602();
- while(1)
- { if(!key1)
- {
- delay(4000);
- if(!key1&&alarm<125)
- { delay(4000);
- alarm++;
- delay(4000);
- }
- }
- if(!key3)
- {
- delay(4000);
- if(!key3&&alarm>0)
- {
- delay(4000);
- alarm--;
- delay(4000);
- }
- }
- dis_cmd_wrt(0x80);
- dis_dat_wrt('T');
- dis_dat_wrt(':');
- dis_cmd_wrt(0x80+0x40);
- dis_dat_wrt('E');
- dis_dat_wrt(':');
- szl[0]=alarm/100+0x30;
- szl[1]=alarm%100/10+0x30;
- szl[2]=alarm%100%10+0x30;
- dis_cmd_wrt(0x80+0x43);
- for(k=0;k<3;k++)
- {
- dis_dat_wrt(szl[k]);
- delay(10);
- }
- dis_cmd_wrt(0x80+0x48);
- dis_dat_wrt(0xdf);
- dis_dat_wrt('C');
- dis_cmd_wrt(0x88);
- dis_dat_wrt(0xdf);
- dis_dat_wrt('C');
- Tem=memread();
- display(Tem);
- delay(20);
- tempalarm=num;
- baojing();
- }
- }
- //*************報警控制*******************************************
- void baojing(void)
- {
- if(tempalarm>=alarm)//報警判斷
- {
- for(t=0;t<4;t++)
- {
- buzzer=0;//打開報警蜂鳴器
- delay(2000);
- buzzer=1;
- delay(2000);
- }
- }
- }
- void delay_nms(unsigned int n)
- {
- unsigned int j=0;
- unsigned char i=0;
- for(j=0;j<n;j++)
- {
- for(i=0;i<120;i++)
- {
- _nop_();
- }
- }
- }
- //*********輸入轉換并顯示*********
- void display(uint Tem)
- {
- uint T,a,b;
- T=Tem*2;
- if(!key2)
- {
- delay(20);
- if(!key2)
- {
- delay(20);
- T=(0.1923*(T*0.01-273.15)+29.9250)*100+27315; //人體體溫補償
-
- }
- }
- dis_cmd_wrt(0x83); //初始顯示位置
- if(T>=27315)
- {
- T=T-27315;
- a=T/100;
- num=a;
- b=T-a*100;
- //---------------------------
- if(a>=100)
- {
- dis_dat_wrt(0x30+a/100);
- a=a%100;
- dis_dat_wrt(0x30+a/10);
- a=a%10;
- dis_dat_wrt(0x30+a);
- }
- else if(a>=10)
- {
- dis_dat_wrt(0x30+a/10);
- a=a%10;
- dis_dat_wrt(0x30+a);
- }
- else
- {
- dis_dat_wrt(0x30+a);
- }
- dis_dat_wrt(0x2e);// 顯示點
- //---------------------------
- if(b>=10)
- {
- dis_dat_wrt(0x30+b/10);
- b=b%10;
- }
- else
- {
- dis_dat_wrt(0x30);
- }
- }
- //==========
- else
- {
- T=27315-T;
- a=T/100;
- num=a;
- b=T-a*100;
- dis_dat_wrt(0x2d);
- //--------------------------
- if(a>=10)
- {
- dis_dat_wrt(0x30+a/10);
- a=a%10;
- dis_dat_wrt(0x30+a);
- }
- else
- {
- dis_dat_wrt(0x30+a);
- }
- dis_dat_wrt(0x2e);//顯示點
- //--------------------------
- if(b>=10)
- {
- dis_dat_wrt(0x30+b/10);
- b=b%10;
- dis_dat_wrt(0x30+b);
- }
- else
- {
- dis_dat_wrt(0x30);
- dis_dat_wrt(0x30+b);
- }
-
- }
- }
- //************************************
- void start_bit(void)
- {
- SDA=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
-
- }
- //------------------------------
- void stop_bit(void)
- {
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=1;
- }
- //--------- 發(fā)送一個字節(jié)---------
- void tx_byte(uchar dat_byte)
- {
- char i,n,dat;
- n=Nack_counter;
- TX_again:
- dat=dat_byte;
- for(i=0;i<8;i++)
- {
- if(dat&0x80)
- bit_out=1;
- else
- bit_out=0;
- send_bit();
- dat=dat<<1;
- }
-
- receive_bit();
- if(bit_in==1)
- {
- stop_bit();
- if(n!=0)
- {n--;goto Repeat;}
- else
- goto exit;
- }
- else
- goto exit;
- Repeat:
- start_bit();
- goto TX_again;
- exit: ;
- }
- //-----------發(fā)送一個位---------
- void send_bit(void)
- {
- if(bit_out==0)
-
- SDA=0;
- else
- SDA=1;
- _nop_();
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- //---------- 接收一個字節(jié)--------
- uchar rx_byte(void)
- {
- uchar i,dat;
- dat=0;
- for(i=0;i<8;i++)
- {
- dat=dat<<1;
- receive_bit();
- if(bit_in==1)
- dat=dat+1;
- }
- send_bit();
- return dat;
- }
- //---------- 接收一個位----------
- void receive_bit(void)
- {
- SDA=1;bit_in=1;
- SCL=1;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- bit_in=SDA;
- _nop_();
- SCL=0;
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- //------------ 延時--------------
- void delay(uint N)
- {
- uint i;
- for(i=0;i<N;i++)
- _nop_();
- }
- //------------------------------
- uint memread(void)
- {
- start_bit();
- tx_byte(0xB4); //Send SlaveAddress ==============================
- tx_byte(0x07); //Send Command
- //------------
- start_bit();
- tx_byte(0x01);
- bit_out=0;
- DataL=rx_byte();
- bit_out=0;
- DataH=rx_byte();
- bit_out=1;
- Pecreg=rx_byte();
- stop_bit();
- return(DataH*256+DataL);
- }
- //******************LCD 顯示子函數(shù)***********************
- void init1602(void) // 初始化LCD
- {
- dis_cmd_wrt(0x01);
- dis_cmd_wrt(0x0c);
- dis_cmd_wrt(0x06);
- dis_cmd_wrt(0x38);
-
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有程序51hei提供下載:
溫度測量.zip
(12.65 KB, 下載次數(shù): 64)
2020-11-14 21:38 上傳
點擊文件名下載附件
|