南京工程学院单片机课程设计《基于DS18B20的测温报警器》.docx
《南京工程学院单片机课程设计《基于DS18B20的测温报警器》.docx》由会员分享,可在线阅读,更多相关《南京工程学院单片机课程设计《基于DS18B20的测温报警器》.docx(27页珍藏版)》请在冰豆网上搜索。
南京工程学院单片机课程设计《基于DS18B20的测温报警器》
图251单片机内部结构
图351单片机双列直插封装方式的引脚
I/O口说明:
图4晶振电路
上电复位按键复位
图551单片机的两种复位电路
DS18B20介绍
2.引脚封装
图6DS18B20引脚封装及定义图
3.硬件连接
图7DS18B20典型电路
4.工作原理
图864位光刻ROM各位定义
图9高速暂存器RAM
以上这些指令分别涉及DS18B20的ROM和RAM
图10DS18B20内部温度的存储格式
图11串口通信电路
图12按键电路图13报警电路
图14显示电路
74HC595简介:
图1574hc595管脚图
图16整体电路图
图17PCB布线图
图18课设实际使用PCB布线图
DS18B20工作程序:
/*******************************************************************/
/*程序名:
18B20.H*/
/*功能:
对ds18b20的控制,时期能够正确检测当前环境实时温度并转换处理温度*/
/*包括延时函数和温度显示数据的转换函数*/
/*******************************************************************/
#defineucharunsignedchar
#defineuintunsignedint
sbitds=P3^4;//引脚定义
uinttemp;
floatf_temp;
uinttemperature[4]={0};
voiddelay(uintcount)//延时函数
{
unsignedinti;
while(count)
{
i=200;
while(i>0)i--;
count--;
}
}
voiddsreset(void)//发送复位和初始化
{
unsignedinti;
ds=0;
i=103;
while(i>0)i--;//延时
ds=1;
i=4;
while(i>0)i--;
}
bittempreadbit(void)//读取数据的一位
{
uinti;
bitdat;
ds=0;i++;
ds=1;i++;i++;//延时
dat=ds;
i=8;while(i>0)i--;//延时
return(dat);
}
uchartempread(void)//读一个字节
{
uchari,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tempreadbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
voidtempwritebyte(uchardat)//写一个字节
{
uinti;
ucharj;
bittestb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
ds=0;//写0
i++;i++;
ds=1;
i=8;while(i>0)i--;
}
else
{
ds=0;//写0
i=8;while(i>0)i--;
ds=1;
i++;i++;
}
}
}
voidtempchange(void)//ds1820开始转换
{
dsreset();//复位
delay
(1);//延时
tempwritebyte(0xcc);//跳过序列号命令
tempwritebyte(0x44);//发转换命令44H,
}
uintget_temp(void)//读取温度
{
uchara,b;
dsreset();//复位
delay
(1);//延时
tempwritebyte(0xcc);//跳过序列号命令
tempwritebyte(0xbe);//发送读取命令
a=tempread();//读取低位温度
b=tempread();//读取高位温度
temp=b;
temp<<=8;
temp=temp|a;
f_temp=temp*0.0625;
temp=f_temp*10+0.5;
f_temp=f_temp*10+0.05;
returntemp;//返回测量值
}
voidtranslate(uintt)//显示数据转换函数
{
temperature[0]=t/100;
temperature[1]=t%100/10;
temperature[2]=t%100%10;
temperature[3]=1000;
}
主函数功能程序:
/*******************************************************************/
/*程序名:
mainhanshu.c*/
/*功能:
实时测量温度,超过上下限报警,报警温度可手动调整。
K1是用来进行*/上下限调节模式的,当按一下K1进入上限调节模式,再按一下进入下限调节模式。
在正常模式下,按一下K2进入查看上限温度模式,显示1s左右自动退出;按一下K3进入查看下限温度模式,显示1s左右自动退出;在调节上下限温度模式下,K2是实现加1功能,K1是实现减1功能。
/*******************************************************************/
#include//头文件
#include<18b20.h>
#include"intrins.h"
#defineucharunsignedchar//宏定义
#defineuintunsignedint
sbitST=P1^4;//74hc595引脚位定义
sbitCP=P1^2;
sbitDIN=P1^3;
sbitkey1=P1^5;//按键定义
sbitkey2=P1^6;
sbitkey3=P1^7;
sbitbeep=P3^2;//蜂鸣器定义
unsignedchartable[]={0x82,0x9f,0xa4,0x85,0x99,0xc1,0xc0,0x8b,0x80,0x81,0x02,0x1f,0x24,0x05,
0x19,0x41,0x40,0x0b,0x00,0x01};
//带小数点与否的0~9段码表
uintwendushangxian=36;//初始温度上下限设定
uintwenduxiaxian=0;
uintk1,k2;
voidshuchu()//74hc595输出函数
{
ST=0;
_nop_();
_nop_();
ST=1;
_nop_();
}
voidshuru(unsignedchara)//74hc595输入函数
{
unsignedchari;
for(i=8;i>0;i--)
{
CP=0;
a<<=1;
_nop_();
DIN=CY;
CP=1;
_nop_();
}
}
voiddisplay()//显示函数
{
tempchange();
get_temp();
translate(temp);
shuru(table[temperature[2]]);
shuru(table[temperature[1]+10]);
shuru(table[temperature[0]]);
shuru(0xff);
shuchu();
}
voidkeyscan()//按键扫描函数
{
if(key1==0)
{
delay(20);
if(key1==0)
{
while(!
key1);//松手检测
beep=0;//按键声
delay(10);
beep=1;
while
(1)
{
if(key2==0)
{
delay(20);
if(key2==0)
{
beep=0;
delay(10);
beep=1;
shuru(table[wendushangxian%10]);
shuru(table[wendushangxian/10]);
shuru(0xff);
shuru(0xff);
shuchu();
while(!
key2);//松手检测
while
(1)
{
if(key2==0)//上限值设置+
{
delay(20);
if(key2==0)
{
wendushangxian++;
beep=0;
delay(10);
beep=1;
if((wendushangxian==-1)||(wendushangxian==-2)||(wendushangxian==-3))
wendushangxian=0;
if(wendushangxian==100)
wendushangxian=99;
if(wendushangxian<=wenduxiaxian)
wendushangxian++;
beep=0;
delay(10);
beep=1;
shuru(table[wendushangxian%10]);
shuru(table[wendushangxian/10]);
shuru(0xff);
shuru(0xff);
shuchu();
}
while(!
key2);
}
if(key3==0)//上限值设置-
{
delay(20);
if(key3==0)
{
wendushangxian--;
beep=0;
delay(10);
beep=1;
if(wendushangxian==100)
wendushangxian=99;
if(wendushangxian<=wenduxiaxian)
wendushangxian++;
beep=0;
delay(10);
beep=1;
shuru(table[wendushangxian%10]);
shuru(table[wendushangxian/10]);
shuru(0xff);
shuru(0xff);
shuchu();
}
while(!
key3);//松手检测
}
if(key1==0)
{
delay(20);
if(key1==0)
delay(20);
break;//确认
}
}
}
}
if(key3==0)
{
delay(20);
if(key3==0)
{
while
(1)
{
if(key2==0)//下限值设置+
{
delay(20);
if(key2==0)
{
wenduxiaxian++;
beep=0;
delay(10);
beep=1;
if((wenduxiaxian==-1)||(wenduxiaxian==-2)||(wenduxiaxian==-3))
wenduxiaxian=0;
if(wenduxiaxian>=wendushangxian)
wenduxiaxian--;
beep=0;
delay(10);
beep=1;
shuru(table[wenduxiaxian%10]);
shuru(table[wenduxiaxian/10]);
shuru(0xff);
shuru(0xff);
shuchu();
}while(!
key2);
}
if(key3==0)//下限值设置-
{
delay(20);
if(key3==0)
{
wenduxiaxian--;
beep=0;
delay(10);
beep=1;
if((wenduxiaxian==-1)||(wenduxiaxian==-2)||(wenduxiaxian==-3))
wenduxiaxian=0;
if(wenduxiaxian>=wendushangxian)
wenduxiaxian--;
beep=0;
delay(10);
beep=1;
shuru(table[wenduxiaxian%10]);
shuru(table[wenduxiaxian/10]);
shuru(0xff);
shuru(0xff);
shuchu();
}while(!
key3);
}
if(key1==0)
{
delay(20);
if(key1==0)
beep=0;
delay(10);
while(!
key1)
beep=1;
break;
}
}
}
}
if(key1==0)
{
delay(20);
if(key1==0)
beep=0;
delay(10);
while(!
key1)
beep=1;
break;
}
}
}
}}
warn1(uintk1)//上限报警函数
{
if((temperature[0]*10+temperature[1])>=wendushangxian)
{
beep=0;
delay(k1);
beep=1;
delay(k1);
}
else
beep=1;
}
warn2(uintk2)//下限报警函数
{if((temperature[0]*10+temperature[1])<=wenduxiaxian)
{
beep=0;
delay(k2);
beep=1;
delay(k2);
}
else
beep=1;
}
voidguan_baojing()//关报警函数
{
if(key3==0)
{
delay(20);
if(key3==0)
beep=1;
}
}
voidmain()//主函数
{
k2=200;
beep=1;
while
(1)
{k1+=5;
k2-=5;
if(k1==200)
k1=0;
if(k2==0)
k2=200;
keyscan();
warn1(k1);
warn2(k2);
guan_baojing();
display();
}
}
数字温度测试系统元件清单
序号
名称
规格
封装
数量
代号
1
插座
双排针座
IDC10
1
JP5
2
数码管(共阳)
红色0.5英寸
SMG0.5
4
DG1
DG2
DG3
DG4
3
排电阻
8*5.1K
SIP9
1
R18
4
电阻
1K
AXIAL0.4
4
R1
R3
R4
R5
5
二极管
IN4001
DIODE0.4
4
D1
D2
D3
D4
6
蜂鸣器
5V
ECAP1
1
FM
7
插头座
座弯针_头孔
DB9
1
JP2
8
电容
47U
ECAP
1
C1
9
电容
30
CAP
2
C2
C3
10
电容
22U
ECAP
4
C4
C5
C6
C7
11
插头座
4脚*5mm
J5X4
1
JP1
12
按键
12*12
KEY2
1
S4
13
晶振
11.0592MHz
XTAL
1
T1
14
温度传感器
DS18B20
SIP3
1
T3
15
三极管
8550
SIP3
1
T4
16
集成电路
ICL232
DIP-16
1
U1
17
集成电路
AT89S52
DIP-40
1
U2
18
集成电路
74HC595
DIP16
4
U16
U17
U18
U19
19
集成插座
16P
DIP16
1
U1
U16
U17
U18
U19
20
集成插座
40P
DIP40
1
U2