【51单片机】【protues仿真】基于51单片机温度检测系统
目录
一、主要功能
二、使用步骤
三、硬件资源
四、软件设计
五、实验现象
一、主要功能
1、LCD1602液晶显示测量温度
2、温度大于上限值或下限值进行声光警报
二、使用步骤
基于51单片机的温度检测系统设计是一种结合传感器技术与实时显示的经典嵌入式方案,通过LCD1602屏幕显示实时获取温度值。
三、硬件资源
1、51单片机核心模块
2、按键模块
3、DS18B20温度传感器
4、蜂鸣器模块
5、LED灯模块
6、LCD1602显示模块
四、软件设计
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define out P0
sbit RS = P2^5;
sbit RW = P2^6;
sbit EN = P2^7;
sbit DQ = P3^7;
sbit Speaker = P1^7;
sbit LED0 = P1^0;
sbit LED1 = P1^1;
uint temp;//温度变量
uchar i,signature;//温度正负判断
uchar count = 0;//定时计数,每5ms 加1,count=200时,读一次温度值
uchar H_Limit =25;//最高温度
char L_Limit =15;//最低温度
uchar code dis1[]={"The Temp is :"};
/* *********
延时函数
********* */
void delay(int x)
{
int i,j;
for(i = 0;i < x;i++)
{
for(j = 0;j < 123;j++)
{
;
}
}
}
void delay5(uchar n)
{
do
{
_nop_();
_nop_();
_nop_();
n--;
}
while(n);
}
/* *************
LCD初始化
************* */
void init()
{
write_cmd(0x38);
write_cmd(0x0c);
write_cmd(0x06);
write_cmd(0x01);
}
/* *************
读LCD忙不忙
************* */
void read_busy()
{
uchar busy;
out = 0xff;
RS = 0;
RW = 1;
do
{
EN = 1;
busy = out;
EN = 0;
}while(busy&0x80);
}
/* ***********
写LCD指令
*********** */
void write_cmd(uchar cmd)
{
read_busy();
RS = 0;
RW = 0;
out = cmd;
EN = 1;
EN = 0;
delay(1);
}
/* ***********
写LCD数据
*********** */
void write_dat(uchar dat)
{
read_busy();
RS = 1;
RW = 0;
out = dat;
EN = 1;
EN = 0;
delay(1);
}
/* *************
DS18B20初始化
************* */
void DS18B20()
{
uchar x=0;
DQ=0;
delay5(120);
DQ=1;
delay5(16);
delay5(80);
}
/* *************
读取1字节数据
************* */
uchar readbyte()
{
uchar date=0;
for(i=8;i>0;i--)
{
DQ=0;
delay5(1);
DQ=1;
date >>=1;
if(DQ)
date|=0x80;
delay5(11);
DQ=1;
}
return (date);
}
/* *************
写一字节数据
************* */
void writebyte(uchar dat)
{
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
delay5(12);
DQ=1;
dat>>=1;
delay5(5);
}
}
/* **************
读取温度的十六进制
************** */
uint retemp()
{
uchar L_8,H_8;
uint temp;
DS18B20();
writebyte(0xcc);//跳过ROM,即跳过多传感器识别
writebyte(0x44);//启动温度转换
DS18B20();
writebyte(0xcc);
writebyte(0xbe);//读取DS18B20寄存器指令
L_8=readbyte();//读取温度值低位
H_8=readbyte();//读取温度值高位
temp=H_8;
temp<<=8;
temp=temp|L_8;
return (temp);
}
void main()
{
init();
retemp();
delay(1000);
TMOD=0x01; //定时器0工作方式0
TH0=(65536-50000)/256; //50ms定时
TL0=(65536-50000)%256;
IE=0x82; //允许T0中断
TR0=1;
while(1)
{
;
}
}
五、实验现象
演示视频: