|
- #include<reg52.h> //包含頭文件,一般情況不需要改動,頭文件包含特殊功能寄存器的定義
- #include<stdio.h>
- #include<intrins.h>
- #include "delay.h"
- #include "1602.h"
- #include "mlx90614.h"
- sbit buzzer=P1^0; //引腳定義
- sbit key1=P2^0;
- sbit key2=P2^1;
- unsigned long time_20ms; //定時計數(shù)
- char dis0[16]; //液晶數(shù)組顯示暫存
- char dis1[16];
- unsigned int setNum = 37; //設(shè)置值
- //---------------------------------------
- //Name: CALTEMP
- //Temperature data is T=(Data)*0.02-273.15
- //---------------------------------------
- bit rekey =0;//防止按鍵重復(fù)按下
- bit disFlag =0;//更新顯示標(biāo)志
- unsigned int Tem; //溫度
- float objTemp,envTemp; //物體溫度和環(huán)境溫度
- void Init_Timer0(void); //函數(shù)聲明
- void main (void)
- {
- Init_Timer0(); //定時器0初始化
-
- LCD_Init(); //初始化液晶
- DelayMs(20); //延時有助于穩(wěn)定
- LCD_Clear();
- while (1) //主循環(huán)
- {
- if(disFlag==1) //顯示標(biāo)志
- {
- Tem=ReadObjectTemp(); //讀取實物溫度
- objTemp=(float)(Tem)*0.02-273.15;
-
- Tem=ReadEnvironTemp(); //讀取環(huán)境溫度
- envTemp=(float)(Tem)*0.02-273.15;
- sprintf(dis0,"E: %4.1f'C ",envTemp);//打印溫度值
- LCD_Write_String(0,0,dis0);//顯示
- sprintf(dis1,"O:%5.1f'C %3d ",objTemp,setNum);//打印溫度設(shè)置值
- LCD_Write_String(0,1,dis1);//顯示
- if(objTemp>setNum)
- {buzzer =0; } //溫度高蜂鳴器鳴叫
- else
- {buzzer=1;} //溫度低 停止
- disFlag = 0;
- }
- if((key1 == 0)||(key2 == 0)) //有按鍵按下
- {
- if(rekey == 0)
- {
- DelayMs(10);
- if(key1 == 0) //按鍵1按下
- {
- rekey =1; //防止重復(fù)按下
- if(setNum<200) setNum++; //++
- }
- else if((key2 == 0))//按鍵2按下
- {
- rekey =1; //防止重復(fù)按下
- if(setNum>0) setNum--; //--
- }
- }
- }
- else
- {
- rekey = 0; //防止重復(fù)按下
- }
- }
- }
- void Init_Timer0(void)
- {
- TMOD |= 0x01; //使用模式1,16位定時器,使用"|"符號可以在使用多個定時器時不受影響
- TH0=(65536-20000)/256; //重新賦值 20ms
- TL0=(65536-20000)%256;
- EA=1; //總中斷打開
- ET0=1; //定時器中斷打開
- TR0=1; //定時器開關(guān)打開
- }
- void Timer0_isr(void) interrupt 1
- {
- TH0=(65536-20000)/256; //重新賦值 20ms
- TL0=(65536-20000)%256;
- time_20ms++;
- if(time_20ms%20==0) //定時更新顯示
- {disFlag = 1;}
- }
復(fù)制代碼- #include "mlx90614.h"
- #define Nack_counter 10
- //************數(shù)據(jù)定義****************
- unsigned char bit_out=1;
- unsigned char bit_in=1;
- unsigned char DataH,DataL,Pecreg;
- 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(unsigned char dat_byte)
- {
- char i,n,dat;
- n=Nack_counter;
- 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--;}
- else
- return;
- }
- else
- return;
- start_bit();
- tx_byte(dat_byte); //函數(shù)自身回調(diào)
- }
- ////---------發(fā)送一個字節(jié)---------
- //void tx_byte(unsigned char 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é)--------
- unsigned char rx_byte(void)
- {
- unsigned char 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_();
- }
- //------------------------------
- unsigned int ReadObjectTemp(void)
- {
- start_bit();
- tx_byte(0x00); //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);
- }
- unsigned int ReadEnvironTemp(void)
- {
- start_bit();
- tx_byte(0x00); //Send SlaveAddress
- tx_byte(0x06); //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);
- }
- //---------------------------------------
- //Name: CALTEMP
- //Temperature data is T=(Data)*0.02-273.15
- //Tem=ReadObjectTemp();
- //objTemp=(float)(Tem)*0.02-273.15;
- //sprintf(tab_up,"O:%5.2f %05d ",objTemp,Tem);//打印溫度值
- //LCD_Write_String(0,0,tab_up);//顯示第一行
- //
- //Tem=ReadEnvironTemp();
- //envTemp=(float)(Tem)*0.02-273.15;
- //sprintf(tab_dw,"E:%5.2f %05d ",envTemp,Tem);//打印溫度值
- //LCD_Write_String(0,1,tab_dw);//顯示第一行
- //
- //DelayMs(200);
復(fù)制代碼
1.png (51.1 KB, 下載次數(shù): 59)
下載附件
2021-10-27 17:15 上傳
代碼下載:
精準(zhǔn)測溫MLX90614.zip
(79.45 KB, 下載次數(shù): 47)
2021-10-27 16:55 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|