(基于单片机的智能温度监测系统的代码)
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^3;//DS18B20数据线
sbit BEEP=P3^7;//报警器
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
sbit K1=P1^7;
uchar code Temp_Disp_Title[]={
" Current Temp : "};
uchar Current_Temp_Display_Buffer[]={
"TEMP: "};
uchar code Alarm_Temp[]={
"ALARM TEMP Hi Lo"};
uchar Alarm_HI_LO_STR[]={
"Hi: Lo: "};
uchar code df_Table[]={
0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};//温度小数位对照表
char Alarm_Temp_HL[2]={
100,0};
uchar CurrentT=0;//当前读取的温度整数部分
uchar Temp_Value[]={
0x00,0x00};//从DS18B20读取的温度值
uchar Display_Digit[]={
0,0,0,0};//待显示的各温度数位
bit HI_Alarm=0,LO_Alarm=0;//高温低温报警标志
bit DS18B20_IS_OK=1;//传感器正常标志
uint Time0_Count=0;//定时器延时累加
//延时
void DelayMS(uint x){
uchar i;
while(x--)for(i=0;i<120;i++);
}
//读LCD状态
uchar Read_LCD_State(){
uchar state;
LCD_RS=0;LCD_RW=1;LCD_EN=1;DelayMS(1);state=P0;LCD_EN=0;DelayMS(1);
return state;
}
//忙等待
void LCD_Busy_Wait(){
while((Read_LCD_State()&0x80)==0x80);
DelayMS(5);
}
//写LCD指令
void Write_LCD_Command(uchar cmd){
LCD_Busy_Wait();
LCD_RS=0;LCD_RW=0;LCD_EN=0;P0=cmd;LCD_EN=1;DelayMS(1);LCD_EN=0;
}
//向LCD写数据
void Write_LCD_Data(uchar dat){
LCD_Busy_Wait();
LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;LCD_EN=1;DelayMS(1);LCD_EN=0;
}
//延时
void DelayXus(int x){
uchar i;
while(x--)for(i=0;i<200;i++);
}
//延时
void Delay(uint num){
while(--num);
}
//初始化DS18B20
uchar Init_DS18B20(){
uchar status;
DQ=1;Delay(8);
DQ=0;Delay(90);
DQ=1;Delay(8);
status=DQ;
Delay(100);
DQ=1;
return status;//初始化成功返回0
}
//读一字节
uchar ReadOneByte(){
uchar i,dat=0;
DQ=1;_nop_();
for(i=0;i<8;i++){
DQ=0;dat>>=1;DQ=1;_nop_();_nop_();
if(DQ)dat|=0x80;Delay(30);DQ=1;
}
return dat;
}
//写一个字节
void WriteOneByte(uchar dat){
uchar i;
for(i=0;i<8;i++){
DQ=0;DQ=dat&0x01;Delay(5);DQ=1;dat>>=1;
}
}
//读取温度值
void Read_Temperature(){
if(Init_DS18B20()==1)//DS18B20故障
DS18B20_IS_OK=0;
else{
WriteOneByte(0xcc);//跳过序列号
WriteOneByte(0x44);//启动温度转换
Init_DS18B20();
WriteOneByte(0xcc);//跳过序列号
WriteOneByte(0xbe);//读取温度寄存器
Temp_Value[0]=ReadOneByte();//读取低8位
Temp_Value[1]=ReadOneByte();//温度高8位
Alarm_Temp_HL[0]=ReadOneByte();//报警TH
Alarm_Temp_HL[1]=ReadOneByte();//报警TL
DS18B20_IS_OK=1;
}
}
//设置DS18B20温度报警值
void Set_Alarm_Temp_Value(){
Init_DS18B20();
WriteOneByte(0xcc);//跳过序列号
WriteOneByte(0x4e);//将设定的温度报警值写入DS18B20
WriteOneByte(Alarm_Temp_HL[