文档库 最新最全的文档下载
当前位置:文档库 › 温度报警器程序

温度报警器程序

硬件设计:

电源电压VCC为5V,可以焊一只USB座到板上,然后用相应的USB线连接至电脑,通过电脑USB口供电;数码管选用四位共阳红色数码管;DS18B20也可以根据需要用屏蔽电缆引出来。K1为进入/退出设置键;K2为增加键;K3为减少键。电路原理图点击放大。



Hex文件供下载:DS18B20.txt(右键目标另存为,需要将扩展名txt改为hex)

软件设计:

有两个文件,DS18B20.c和DS18B20.h,将这两个文件添加到工程里即可。

DS18B20.c:

/******************************************************************
程序名称:DS18B20温度测量、报警系统
简要说明:DS18B20温度计,温度测量范围0~99.9摄氏度
可设置上限报警温度、下限报警温度
即高于上限值或者低于下限值时蜂鸣器报警
默认上限报警温度为38℃、默认下限报警温度为5℃
报警值可设置范围:最低上限报警值等于当前下限报警值
最高下限报警值等于当前上限报警值
将下限报警值调为0时为关闭下限报警功能
编 写:https://www.wendangku.net/doc/f73698116.html,
最后更新:09/04/16晚于寝室
******************************************************************/
#include
#include "DS18B20.h"
#define uint unsigned int
#define uchar unsigned char //宏定义
#define SET P3_1 //定义调整键
#define DEC P3_2 //定义减少键
#define ADD P3_3 //定义增加键
#define BEEP P3_7 //定义蜂鸣器
bit shanshuo_st; //闪烁间隔标志
bit beep_st; //蜂鸣器间隔标志
sbit DIAN = P2^7; //小数点
uchar x=0; //计数器
signed char m; //温度值全局变量
uchar n; //温度值全局变量
uchar set_st=0; //状态标志
signed char shangxian=38; //上限报警温度,默认值为38
signed char xiaxian=5; //下限报警温度,默认值为38
uchar code LEDData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff};

/*****延时子程序*****/
void Delay(uint num)
{
while( --num );
}

/*****初始化定时器0*****/
void InitTimer(void)
{
TMOD=0x1;
TH0=0x3c;
TL0=0xb0; //50ms(晶振12M)
}

/*****定时器0中断服务程序*****/
void timer0(void) interrupt 1
{
TH0=0x3c;
TL0=0xb0;
x++;
}

/*****外部中断0服务程序*****/
void int0(void) interrupt 0
{

EX0=0; //关外部中断0
if(DEC==0&&set_st==1)
{
shangxian--;
if(shangxian}
else if(DEC==0&&set_st==2)
{
xiaxian--;
if(xiaxian<0)xiaxian=0;
}
}

/*****外部中断1服务程序*****/
void int1(void) interrupt 2
{
EX1=0; //关外部中断1
if(ADD==0&&set_st==1)
{
shangxian++;
if(shangxian>99)shangxian=99;
}
else if(ADD==0&&set_st==2)
{
xiaxian++;
if(xiaxian>shangxian)xiaxian=shangxian;
}
}

/*****读取温度*****/
void check_wendu(void)
{
uint a,b,

c;
c=ReadTemperature()-5; //获取温度值并减去DS18B20的温漂误差
a=c/100; //计算得到十位数字
b=c/10-a*10; //计算得到个位数字
m=c/10; //计算得到整数位
n=c-a*100-b*10; //计算得到小数位
if(m<0){m=0;n=0;} //设置温度显示上限
if(m>99){m=99;n=9;} //设置温度显示上限
}

/*****显示开机初始化等待画面*****/
Disp_init()
{
P2 = 0xbf; //显示-
P1 = 0xf7;
Delay(200);
P1 = 0xfb;
Delay(200);
P1 = 0xfd;
Delay(200);
P1 = 0xfe;
Delay(200);

P1 = 0xff; //关闭显示
}

/*****显示温度子程序*****/
Disp_Temperature() //显示温度
{
P2 =0xc6; //显示C
P1 = 0xf7;
Delay(300);

P2 =LEDData[n]; //显示个位
P1 = 0xfb;
Delay(300);

P2 =LEDData[m%10]; //显示十位
DIAN = 0; //显示小数点
P1 = 0xfd;
Delay(300);

P2 =LEDData[m/10]; //显示百位
P1 = 0xfe;
Delay(300);

P1 = 0xff; //关闭显示
}

/*****显示报警温度子程序*****/
Disp_alarm(uchar baojing)
{
P2 =0xc6; //显示C
P1 = 0xf7;
Delay(200);

P2 =LEDData[baojing%10]; //显示十位
P1 = 0xfb;
Delay(200);

P2 =LEDData[baojing/10]; //显示百位
P1 = 0xfd;
Delay(200);

if(set_st==1)P2 =0x89;
else if(set_st==2)P2 =0xc7; //上限H、下限L标示
P1 = 0xfe;
Delay(200);

P1 = 0xff; //关闭显示
}

/*****报警子程序*****/
void Alarm()
{
if(x>=10){beep_st=~beep_st;x=0;}
if((m>=shangxian&&beep_st==1)||(melse BEEP=1;
}

/*****主函数*****/
void main(void)
{
uint z;
InitTimer(); //初始化定时器
EA=1; //全局中断开关
TR0=1;
ET0=1; //开启定时器0
IT0=1;
IT1=1;
check_wendu();
check_wendu();
for(z=0;z<300;z++)
{
Disp_init();
}
while(1)
{
if(SET==0)
{
Delay(2000);
do{}while(SET==0);
set_st++;x=0;shanshuo_st=1;
if(set_st>2)set_st=0;
}
if(set_st==0)
{
EX0=0; //关闭外部中断0
EX1=0; //关闭外部中断1
check_wendu();
Disp_Temperature();
Alarm(); //报警检测
}
else if(set_st==1)
{
BEEP=1; //关闭蜂鸣器
EX0=1; //开启外部中断0
EX1=1; //开启外部中断1
if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
if(shanshuo_st) {Disp_alarm(shangxian);}
}
else if(set_st==2)
{
BEEP=1; //关闭蜂鸣器
EX0=1; //开启外部中断0
EX1=1; //开启外部中断1
if(x>=10){shanshuo_st=~shanshuo_st;x=0;}
if(shanshuo_st) {Disp_alarm(xiaxian);}
}
}
}

/*****END*****/

DS18B20.h:

#include
#define DQ P3_6 //定义DS18B20总线I/O

/*****延时子程序*****/
void Delay_DS18B20(int num)
{
while(num--) ;
}

/*****初始化DS18B20*****/
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
Delay_DS18B20(8); //稍

做延时
DQ = 0; //单片机将DQ拉低
Delay_DS18B20(80); //精确延时,大于480us
DQ = 1; //拉高总线
Delay_DS18B20(14);
x = DQ; //稍做延时后,如果x=0则初始化成功,x=1则初始化失败
Delay_DS18B20(20);
}

/*****读一个字节*****/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay_DS18B20(4);
}
return(dat);
}

/*****写一个字节*****/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay_DS18B20(5);
DQ = 1;
dat>>=1;
}
}

/*****读取温度*****/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x44); //启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5; //放大10倍输出并四舍五入
return(t);
}

/*****END*****/

相关文档