基于单片机的温度报警系统proteus仿真_第1页
基于单片机的温度报警系统proteus仿真_第2页
基于单片机的温度报警系统proteus仿真_第3页
基于单片机的温度报警系统proteus仿真_第4页
基于单片机的温度报警系统proteus仿真_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include<reg51.h>#include<intrins.h>#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; /正常显示温度,

2、越界时报警sbit k2=p14; /显示报警温度sbit k3=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=

3、0x0c,0x12,0x12,0x0c,0x00,0x00,0x00,0x00; /温度示字符uchar code alarm_temp="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,

4、1,1,2,3,3,4,4,5,6,6,7,8,8,9,9; /温度小数位对照表/./报警温度上下限(取值范围:-128+127)(ds18b20温度范围为:-55+125)/数组中前一位为高温值,后一位为低温值/此处类型注意设为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; /高

5、低温报警标志bit ds18b20_is_ok=1; /传感器正常标志uint time0_count=0; /定时器延时累加/./延时/.void delayxus(int x) uchar i; while(x-)for(i=0;i<200;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(

6、uchar cmd) while(lcd_busy_check()&0x80)=0x80); /忙等待 rs=0; /写选择命令寄存器 rw=0; /写 en=0; p0=cmd; en=1; 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_comm

7、and(pos |0x80);/lcd初始化void lcd_initialise() write_lcd_command(0x38);delayxus(1); write_lcd_command(0x01); /清屏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);

8、 /写cgram for(i=0;i<8;i+) write_lcd_data(temperature_chari); /写入温度符号/./延时/.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=

9、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) /ds18b

10、20故障 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 ds

11、18b20_is_ok=1; /./设置ds18b20温度报警值/.void set_alarm_temp_value() init_ds18b20(); writeonebyte(0xcc); /跳过序列号 writeonebyte(0x4e); /将设定的温度报警值写入ds18b20 writeonebyte(alarm_temp_hl0); /写th writeonebyte(alarm_temp_hl1); /写tl writeonebyte(0x7f); /12位精度 init_ds18b20(); writeonebyte(0xcc); /跳过序列号 writeonebyte(0x

12、48); /温度报警值存入ds18b20/./在lcd上显示当前温度/.void display_temperature() uchar i; uchar t=150; /延时值 uchar ng=0; /负数标识 char signed_current_temp;/如果为负数则取反加1,并设置负数标识if(temp_value1&0xf8)=0xf8) temp_value1=temp_value1; temp_value0=temp_value0+1; if(temp_value0=0x00) temp_value1+; ng=1; /设负数标识/查表得到温度小数部分display

13、_digit0=df_tabletemp_value0&0x0f;/获取温度整数部分(无符号)currentt=(temp_value0&0xf0)>>4)|(temp_value1&0x07)<<4);/有符号的当前温度值,注意此处定义为char, 其值可为-128+127signed_current_temp=ng?-currentt:currentt;/高低温报警标志设置(与定义为char类型的alarm_temp_hl比较,这样可区分正负比较)hi_alarm=signed_current_temp>=alarm_temp_hl0?

14、1:0;lo_alarm=signed_current_temp<=alarm_temp_hl1?1:0;/将整数部分分解为三位待显示数字display_digit3=currentt/100;display_digit2=currentt%100/10;display_digit1=currentt%10;/刷新lcd显示缓冲current_temp_display_buffer11=display_digit0+'0'current_temp_display_buffer10='.'current_temp_display_buffer9=displa

15、y_digit1+'0'current_temp_display_buffer8=display_digit2+'0'current_temp_display_buffer7=display_digit3+'0'/高位为0时不显示if(display_digit3=0) current_temp_display_buffer7=' '/高位为0且次高位为0时,次高位不显示if(display_digit2=0&&display_digit3=0)current_temp_display_buffer8='

16、'/负数符号显示恰当位置if (ng) if(current_temp_display_buffer8=' ') current_temp_display_buffer8='_' else if(current_temp_display_buffer7=' ') current_temp_display_buffer7='_' else current_temp_display_buffer6='_'/在第一行显示标题set_lcd_pos(0x00);for(i=0;i<16;i+) write_l

17、cd_data( temp_disp_titlei);/在第二行显示当前温度set_lcd_pos(0x40);for(i=0;i<16;i+) write_lcd_data(current_temp_display_bufferi);/显示温度符号set_lcd_pos(0x4d);write_lcd_data(0x00);set_lcd_pos(0x4e);write_lcd_data('c');/./定时器中断,控制报警声音/.void t0_int() interrupt 1 th0=-1000/256; tl0=-1000%256; beep=!beep; if

18、(+time0_count=400) time0_count=0; if(hi_alarm) hi_led=hi_led; else hi_led=0; if(lo_alarm) lo_led=lo_led; else lo_led=0; tr0=0;/./rom code转换与显示/.void display_rom_code() uchar i,t; set_lcd_pos(0x40); for(i=0;i<8;i+) t=(romcodei&0xf0)>>4); if(t>9) t+=0x37;else t+='0' write_lcd_d

19、ata(t); /高位数显示 t=romcodei&0x0f; if(t>9) t+=0x37;else t+='0' write_lcd_data(t); /低位数显示 /./读64位序列码/.void read_rom_code() uchar i; init_ds18b20(); writeonebyte(0x33); /读序列码 for (i=0;i<8;i+) romcodei=readonebyte();/./显示rom code/.void display_romcode() uchar i; set_lcd_pos(0x00); for(i=

20、0;i<16;i+) /显示标题 write_lcd_data(romcodestri); read_rom_code(); /读64位序列码 display_rom_code(); /显示64位rom code/./显示报警温度/.void disp_alarm_temperature() uchar i,ng;/显示alarm_temp_hl数组中的报警温度值/由于alarm_temp_hl类型为char,故可以直接进行正负比较/高温报警值. ng=0; if(alarm_temp_hl0<0) /如果为负数则取反加1 alarm_temp_hl0=alarm_temp_hl0

21、+1; ng=1;/分解高温各数位到待显示串中alarm_hi_lo_str4=alarm_temp_hl0/100+'0'alarm_hi_lo_str5=alarm_temp_hl0/10%10+'0'alarm_hi_lo_str6=alarm_temp_hl0%10+'0'/屏蔽高位不显示的0if(alarm_hi_lo_str4='0') alarm_hi_lo_str4=' 'if(alarm_hi_lo_str4=' '&& alarm_hi_lo_str5='

22、0') alarm_hi_lo_str5=' '/"-"符号显示if (ng) if (alarm_hi_lo_str5=' ') alarm_hi_lo_str5='-' else if(alarm_hi_lo_str4=' ') alarm_hi_lo_str4='-' else alarm_hi_lo_str3='-'/低温报警值ng=0;if(alarm_temp_hl1<0) /如果为负数则取反加1 alarm_temp_hl1=alarm_temp_hl

23、1+1; ng=1;/分解低温各数位到待显示串中alarm_hi_lo_str12=alarm_temp_hl1/100+'0'alarm_hi_lo_str13=alarm_temp_hl0/10%10+'0'alarm_hi_lo_str14=alarm_temp_hl0%10+'0'/屏蔽高位不显示的0if(alarm_hi_lo_str12='0') alarm_hi_lo_str12=' 'if(alarm_hi_lo_str12=' '&& alarm_hi_lo_str13='0') alarm_hi_lo_str13=' '/"-"符号显示if (ng) if (alarm_hi_lo_str13=' ') alarm_hi_lo_str13='-' else if(alarm_hi_lo_str12=' ') alarm_hi_lo_str12='-' else alarm_hi_lo_str11='-'/显示高低温报警温度值set_lcd_pos(0x00); /显示

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论