单片机通过A/D转换器件(或数字温度传感器)对环境温度进行定时采集,并进行数据处理和分析,将采样处理后的数据显示在数码管或液晶显示器上,当环境温度高于设定温度时,启动风扇进行降温,通过蜂鸣器进行报警提示,测量的温度数据可通过串行通信口将数据发送到计算机端,系统能够利用按键(或遥控器)修改和保存温度设定值。以51单片机为例写出代码
时间: 2024-03-06 12:46:52 浏览: 103
以下是基于51单片机的温度监测和控制系统的代码,代码中使用了DS18B20数字温度传感器进行温度采集:
```c
#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define DQ P3_7 // DS18B20数据线
sbit beep = P1^0; // 蜂鸣器
sbit fan = P1^1; // 风扇
uchar temp, TH, TL; // 温度变量
uchar flag, key, set_temp; // 标志位、按键状态、设定温度
// 延时函数
void delay(uint t)
{
uint i, j;
for(i=0; i<t; i++)
for(j=0; j<120; j++);
}
// 发送一个字节
void write_byte(uchar dat)
{
uchar i;
for(i=0; i<8; i++)
{
DQ = 0;
_nop_();
_nop_();
DQ = dat & 0x01;
_nop_();
_nop_();
DQ = 1;
dat >>= 1;
}
}
// 读取一个字节
uchar read_byte()
{
uchar i, dat = 0;
for(i=0; i<8; i++)
{
DQ = 0;
_nop_();
_nop_();
dat >>= 1;
if(DQ) dat |= 0x80;
_nop_();
_nop_();
DQ = 1;
}
return dat;
}
// 初始化DS18B20
void init_ds18b20()
{
DQ = 1;
delay(2);
DQ = 0;
delay(80);
DQ = 1;
delay(2);
}
// 读取温度
uchar read_temp()
{
uchar i;
init_ds18b20();
write_byte(0xCC); // 跳过ROM
write_byte(0x44); // 开始温度转换
delay(100);
init_ds18b20();
write_byte(0xCC); // 跳过ROM
write_byte(0xBE); // 读取温度
TL = read_byte();
TH = read_byte();
if(TH & 0x80) // 温度为负数
{
TL = ~TL;
TH = ~TH;
temp = TL;
temp |= TH << 8;
temp = temp * (-1) + 1;
}
else // 温度为正数
{
temp = TL;
temp |= TH << 8;
}
return temp;
}
// 数码管显示函数
void display(uchar dat)
{
uchar i, j, k, l;
uchar code table[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
uchar code table2[] = {
0xfe, 0xfd, 0xfb, 0xf7
};
for(i=0; i<4; i++)
{
P2 = 0xff;
P0 = table[dat % 10];
P2 = table2[i];
delay(5);
P2 = 0xff;
if(i == 0)
P0 = table[dat / 10];
else if(i == 2)
P0 = 0x40; // 小数点
else
P0 = 0xff;
P2 = table2[i];
delay(5);
}
}
// 蜂鸣器报警
void alarm()
{
uchar i;
for(i=0; i<100; i++)
{
beep = !beep;
delay(10);
}
}
// 主函数
void main()
{
P1 = 0xff;
P2 = 0xff;
P0 = 0xff;
while(1)
{
temp = read_temp(); // 读取温度
display(temp); // 显示温度
if(temp > set_temp) // 温度高于设定温度
{
fan = 1; // 启动风扇
alarm(); // 报警提示
}
else
fan = 0; // 关闭风扇
if(flag)
{
if(key == 0x01) // 按下加键
set_temp++;
else if(key == 0x02) // 按下减键
set_temp--;
flag = 0;
}
}
}
// 中断函数
void isr() interrupt 0
{
delay(10);
key = P1;
if(key != 0xff)
flag = 1;
}
```
代码中使用中断方式检测按键状态,当按键状态改变时,设置标志位flag为1,由主函数进行处理。同时,代码中使用了数码管和蜂鸣器进行温度显示和报警提示,使用风扇进行降温控制。该系统还支持通过串行通信口将数据发送到计算机端,可以根据需求添加相应的串口通信代码。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)