完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include #include #include ***it OPEN=P2^7; ***it SPEAK=P0^7; uchar a,b; char tempL,tempH; int t; uchart1; char temp1,temp2; bit FLAG2=0;//报警开关,1打开,0关闭 uchar Fnum=0; uchar code dis6[]={"OFF"}; uchar code dis7[]={"ON"}; /*********************报警子程序**********************/ void Alarm(uchar t) { uchar i;j; for(i=0;i<50;i++) { SPEAK=~SPEAK; for(j=0;j SPEAK=1; } /********************主程序*****************************/ void main() { uchar slunm=0; bit FLAG1=0; temp1=45; temp2=60; tempL=temp1;//将设置的值存入 tempH=temp2; SPEAK=0; LCD_init();//液晶的初始化 LCD_init1(0); LCD_putchar(1,13,dis6); DS18b20_init();//启动传输 delay(2200); while(1) { if(SET==0) { delay_ms(5); if(SET==0) { slnum++; while(!SET);//释放确认 FLAG1=1;//设置标志位,进入设置值得状态 if(slnum==1) { LCD_INIT(); LCD_putchar(1,3,dis3); LCD_init1(temp1); LCD_pos(1,8);//光标定位 LCD_write_char(0,0x0f);//光标开始闪烁 } if(slnum==2) { LCD_putchar(1,3,dis4); LCD_write_number(1,5,temp2); LCD_pos(1,8);//光标定位 LCD_write_char(0,0x0f);//光标开始闪烁 } if(slnum==3) { slnum=0; LCD_putchar(1,3,dis5); LCD_write_char(0,0x0c);//光标停止闪烁 FLAG1=0; if(FLAG2)LCD_putchar(1,13,dis6); else LCD_putchar(1,13,dis7); tempL=temp1;//将设置的值存入 tempH=temp2; } } } if(slnum!=0) { if(ADD==0) { delay_ms(5); if(ADD==0)//增加键确认 { while(!ADD);//确认释放 if(slnum==1) { temp1++ LCD_write_number(1,5,temp1); LCD_pos(1,8);//显示位置重新回到调节处 } if(slnum==2) { temp2++ LCD_write_number(1,5,temp2); LCD_pos(1,8);//显示位置重新回到调节处 } } } if(SUB==0) { delay_ms(5); if(SUB==0)//减少键确认 { while(!SUB);//确认释放 if(slnum==1) { temp1--; LCD_write_number(1,5,temp1); LCD_pos(1,8);//显示位置重新回到调节处 } if(slnum==2) { temp2--; LCD_write_number(1,5,temp2); LCD_pos(1,8);//显示位置重新回到调节处 } } } } if(OPEN==0)//关闭报警 { delay_ms(5); if(OPEN==0) { while(!OPEN);//释放确认 Fnum++; if(Fnum==1){FLAG2=0;LCD_putchar(1,13,dis7);} if(Fnum==2){FLAG2=1;Fnum=0;LCD_putchar(1,13,dis6);} } } /************************DS18b20程序部分****************/ delay(120); while(init_DS18b20()); DS18b20_Write(0xcc);//跳过读序号的操作 DS18b20_Write(0xBE);//读取温度寄存器等 a=DS18b20_Read();//读取温度值低位 b=DS18b20_Read();//读取温度值高位 if(b>127)//若为负数 { a=~a;//将真值部分取出来 b=~b; a=a>>4;//舍弃小数位。精度为0.0625,则低4位为小数位0.0625*16=1 t1=b<<4;//舍弃符号位 t1=t1|a; t1=t1+1; t=-t1; } else { a>>=4; b<<=4; t1=a|b; t=t1; } if(!FLAG1) LCD_write_number(1,5,t); //if(!((LED==1)&(FLAG2==1)&(t<=tempL)|(t<=tempH))) if(FLAG2==0) P3=0xff; if((FLAG2==0)&&((t {if(t Alarm(9); Alarm(12); } DS18b20_init();//启动转换 } } /**********************LCD1602显示程序****************/ #ifndef _L1602_H_ #define _L1602_H_ #define uchar unsigned char #define uint unsigned int ***it EN=P2^0; ***it RS=P2^1; ***it SET=P2^2; ***it ADD=P2^3; ***it SUB=P2^4; #define LCD_PORT P1 uchar code dis1[]={"Now temperature:"}; uchar code dis2[]={"C"}; uchar code dis3[]={"L"}; uchar code dis4[]={"H"}; uchar code dis5[]={""}; void delay_us(uint t) { t=t/2-4; while(--t); } void delay_ms(uchar t) { unsigned char i=0; while(t--)for(i=0;i<120;i++); } void LCD_writedata(uint data2) { P1=data2; delay_ms(5); EN=1; delay_ms(5); EN=0; } void LCD_write_char(uchar command,uint data1) { if(command==0) { RS=0;//发送命令 } else { RS=1;//发送数据 } LCD_writedata(data1); delay_us(100); } void LCD_pos(uchar x,uchar y) { uchar pos; if(x==0) {x=0x80;} else {x=0xc0;} pos=x+y; LCD_write_char(0,pos); } void LCD_putchar(uchar x,uchar y,uchar *str) { LCD_pos(x,y); while(*str=' |