《基于单片机的温度报警系统+proteus仿真》由会员分享,可在线阅读,更多相关《基于单片机的温度报警系统+proteus仿真(11页珍藏版)》请在金锄头文库上搜索。
1、#include#include#define uchar unsigned char#define uint unsigned int#define delayNOP() _nop_();_nop_();_nop_();_nop_();sbit HI_LED=P23; /高温,低温白净闪烁 LEDsbit LO_LED=P26;sbit DQ=P33; /DS18B20数据线sbit BEEP=P37; /报警sbit RS=P20; sbit RW=P21; sbit EN=P22; sbit K1=P17; /正常显示温度,越界时报警sbit K2=P14; /显示报警温度sbit K3
2、=P11; /查看 ROM CODEuchar code RomCodeStr=- ROM CODE -;uchar RomCode8=0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00; /64位ROM CODEuchar code Temp_Disp_Title =Current Temp: ;uchar Current_Temp_Display_Buffer=TEMPE: ;uchar code Temperature_Char8=0x0c,0x12,0x12,0x0c,0x00,0x00,0x00,0x00; /温度示字符uchar code Alarm_T
3、emp=ALARM TEMP Hi Lo;uchar Alarm_HI_LO_STR=Hi: Lo: ;uchar temp_data2=0x00,0x00;uchar temp_alarm2=0x00,0x00;uchar display5=0x00,0x00,0x00,0x00,0x00,; /温度值uchar display13=0x00,0x00,0x00; /温度报警值uchar code df_Table=0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9; /温度小数位对照表/./报警温度上下限(取值范围:-128+127)(DS18B20温度范围为:-55+125)
4、/数组中前一位为高温值,后一位为低温值/此处类型注意设为char,否则不能进行有符号数的比较char Alarm_Temp_HL2=70,-20;/.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 DelayXus(int
5、 x) uchar i; while(x-)for(i=0;i200;i+);/. /忙检测bit LCD_Busy_Check()bit LCD_Status;RS=0;/寄存器选择RW=1;/读状态寄存器EN=1;/开始读DelayXus(1);LCD_Status=(bit)(P0&0x80) ;EN=0;return LCD_Status;/写LCD指令void Write_LCD_Command(uchar cmd) while(LCD_Busy_Check()&0x80)=0x80); /忙等待 RS=0; /写选择命令寄存器 RW=0; /写 EN=0; P0=cmd; EN=1
6、; DelayXus(1); EN=0;/向LCD写数据void Write_LCD_Data(uchar dat) while(LCD_Busy_Check()&0x80)=0x80); /忙等待 RS=1;RW=0;EN=0;P0=dat;EN=1; DelayXus(1); EN=0;/设置液晶显示位置void Set_LCD_POS(uchar pos) Write_LCD_Command(pos |0x80);/LCD初始化void LCD_Initialise() Write_LCD_Command(0x38);DelayXus(1); Write_LCD_Command(0x01
7、); /清屏DelayXus(1); Write_LCD_Command(0x06); /字符进入模式:屏幕不动,字符后移DelayXus(1); Write_LCD_Command(0x0C); /显示开,关光标DelayXus(1); /././自定义字符写CGRAM/.void Write_NEW_LCD_Char() uchar i; Write_LCD_Command(0x40); /写CGRAM for(i=0;i8;i+) Write_LCD_Data(Temperature_Chari); /写入温度符号/./延时/.void Delay(uint num) while(-nu
8、m);/./初始化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=1;DQ=1;_nop_(); _nop_(); if(DQ) dat |=0x80;Delay(30);DQ=1; return dat;/./写一字节/.v
9、oid WriteOneByte(uchar dat) uchar i; for(i=0;i=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_Value0=ReadOneByte(); /温度低8位 Temp_Value1=ReadOneByte(); /温度高8位 Alarm_Temp_HL0=ReadOneByte(); /报警温度TH Alarm_Temp_HL1=ReadOneByte(); /报警温度TL DS18B20_IS_OK=1; /./设置DS18B20温度报警值/.void Set_Alarm_Temp_Value() Init_DS18B20(); WriteOneByte(0xCC);