|
单片机做的温度报警:
电路图如下
- /*
- 功能:温度采集显示和可调时钟显示,时钟用中断法实现
- s1:选择要调节的时、分、秒位
- s2:时、分、秒加一
- s3:时、分、秒减一
- */
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar num,s1num,count;
- uchar shi,fen,miao;
- sbit rs=P2^0;
- sbit e=P2^1;
- sbit s1=P3^0;
- sbit s2=P3^1;
- sbit s3=P3^2; //按键定义
- sbit DQ =P2^2; //定义通信端口
- sbit beep=P1^0;
- bit flag;
- uchar code table1[]="Time: 00:00:00";
- uchar code table2[]="Temperature: 'C";//1602初始化字符
- void alarm(uchar num);
- void delay(uint z)//延时子函数
- {
- uint x,y;
- for(x=z; x>0; x--)
- for(y=110;y>0;y--);
- }
- #include<1602.h>
- #include<18b20.h>
- #include<clock_set.h>
- void main()
- {
- uchar temp;
- init_1602();
- while(1)
- { delay(500); //去除0秒和1秒之间显示间隔跟正常1秒间隔相差过大
- if(miao==0) //每分钟进行温度采集一次
- {
- TR0=0; //进入温度采集后,关闭定时器
- flag=1; //flag=1,标志进入温度采集模式
- write_com(0x80+0x40+13);
- write_dat(0x30+1); //消除不显示数字1
- temp=ReadTemperature(); //读取DS18B20的温度值
- alarm(temp);
- write_1602(12,temp); //向1602中写入温度值
- TR0=1; //温度采集完成后,打开定时器
- delay(100);//等待miao不等零,跳出循环
- }
- flag=0; //flag=0,标志在时钟显示模式下
- keyscan(); //调用键盘扫描子程序,用以调整时间
- }
- }
- void timer0() interrupt 1//定时器1的中断服务程序,并向1602中写时分秒显示时间
- {
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- count++;
- if (count==18)
- {
- count=0;
- miao++;
- if(miao==60)
- {
- miao=0;
- fen++;
- if(fen==60)
- {
- fen=0;
- shi++;
- if(shi==24)
- {
- shi=0;
- }
- if(flag==0) //flag=0,标志在时钟显示模式下,然后才显示时间
- write_1602(6,shi);
- }
- if(flag==0)
- write_1602(9,fen);
- }
- if(flag==0)
- write_1602(12,miao);
- }
- }
- void alarm(uchar num) //报警
- {
- if(40>=num)
- beep=1; //在温度允许范围,不报警
- else
- beep=~beep;
- return(0);
- }
复制代码
|
评分
-
查看全部评分
|