用51单片机写一个温室大棚的程序设计
时间: 2024-05-13 10:14:43 浏览: 6
以下是一个简单的温室大棚程序设计,基于51单片机和DS18B20温度传感器:
```c
#include <reg51.h>
#include <intrins.h>
#define DQ P2_0 // DS18B20数据线
unsigned char code TempTable[] = { // 温度补偿表
0x00, 0x19, 0x33, 0x4C, 0x66, 0x7F, 0x99, 0xB2, 0xCC, 0xE5, 0xFF
};
unsigned char TempValue; // 温度值
unsigned char TempComp; // 温度补偿值
unsigned char TempSet; // 温度设定值
unsigned char TempHigh; // 温度上限,超过此值则启动散热器
unsigned char TempLow; // 温度下限,低于此值则启动加热器
void Delay10us(unsigned char t) { // 延时函数
while(t--) {
_nop_();
_nop_();
}
}
void InitDS18B20() { // 初始化DS18B20
DQ = 1;
Delay10us(2);
DQ = 0;
Delay10us(80);
DQ = 1;
Delay10us(2);
}
unsigned char ReadByte() { // 读取一个字节
unsigned char i, dat = 0;
for(i = 0; i < 8; i++) {
DQ = 0;
Delay10us(2);
DQ = 1;
Delay10us(2);
dat >>= 1;
if(DQ) {
dat |= 0x80;
}
Delay10us(6);
}
return dat;
}
void WriteByte(unsigned char dat) { // 写入一个字节
unsigned char i;
for(i = 0; i < 8; i++) {
DQ = 0;
Delay10us(2);
DQ = dat & 0x01;
Delay10us(60);
DQ = 1;
dat >>= 1;
}
}
void GetTemperature() { // 获取温度值
unsigned char TL, TH;
InitDS18B20();
WriteByte(0xCC);
WriteByte(0x44);
Delay10us(120);
InitDS18B20();
WriteByte(0xCC);
WriteByte(0xBE);
TL = ReadByte();
TH = ReadByte();
TempValue = TL >> 1;
TempValue |= (TH & 0x07) << 7;
TempComp = TempTable[(TH & 0xF0) >> 4];
}
void DisplayTemperature() { // 显示温度值
unsigned char i, j, k;
unsigned char t = TempValue + TempComp;
unsigned char digit[3];
digit[0] = t / 100;
digit[1] = (t % 100) / 10;
digit[2] = t % 10;
for(i = 0; i < 3; i++) {
if(digit[i] != 0 || (digit[i] == 0 && i == 2)) {
for(j = 0; j < i; j++) {
P0 = 0;
for(k = 0; k < 8; k++) {
P1 = 0;
Delay10us(1);
P1 = 1;
}
}
P0 = digit[i];
for(k = 0; k < 8; k++) {
P1 = 0;
Delay10us(1);
P1 = 1;
}
}
}
}
void main() {
TempSet = 30; // 设定温度为30度
TempHigh = 32; // 温度上限为32度
TempLow = 28; // 温度下限为28度
while(1) {
GetTemperature();
DisplayTemperature();
if(TempValue + TempComp > TempHigh) { // 温度过高,启动散热器
P3_0 = 1; // P3_0为散热器控制端口
} else if(TempValue + TempComp < TempLow) { // 温度过低,启动加热器
P3_0 = 0;
}
Delay10us(50);
}
}
```
该程序通过DS18B20温度传感器获取温度值,并显示在数码管上。若温度超过设定的上限,则启动散热器;若温度低于设定的下限,则启动加热器。具体的控制方法可以根据实际需要进行修改。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)