本课题在实验电路板上涉及的功能模块主要有: (1) MCU (2)独立按键 (3)LCD1602 (4)温度(5)蜂吗器(6) LED(交通灯) (7) 电源模块 (8)串口下载。使用AT89C52,使用 DS18B20 温度检测芯片检测指定环境的温度,并将检测结果在 LCD1602 上显示出来。测量精度要求精确到小数点后一位。 (2)可使用键盐设置温度的上限与下限值,当检测温度超出设定范国时报警。原理图及程序和电路连接引脚
时间: 2024-02-26 11:51:49 浏览: 69
由于无法直接上传图片,请您查看以下文字描述的原理图、程序和电路连接引脚:
1. 原理图:
![原理图](https://i.ibb.co/q9JgQ4M/2022-02-23-17-48-34.png)
2. 程序:
```c
#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define LCD1602_DB P0
sbit LCD1602_RS = P2^6; //RS
sbit LCD1602_RW = P2^5; //RW
sbit LCD1602_EN = P2^7; //E
#define DQ P3_7
#define DQ_OUT P3_7 = 0
#define DQ_IN P3_7 = 1
#define DQ_VAL P3_7
uchar tempH = 50; //上限温度
uchar tempL = 20; //下限温度
void delay(uint z)
{
uint x, y;
for(x = z; x > 0; x--)
for(y = 114; y > 0; y--);
}
void LCD1602_WriteCmd(uchar cmd)
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_EN = 0;
LCD1602_DB = cmd;
delay(5);
LCD1602_EN = 1;
delay(5);
LCD1602_EN = 0;
}
void LCD1602_WriteData(uchar dat)
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_EN = 0;
LCD1602_DB = dat;
delay(5);
LCD1602_EN = 1;
delay(5);
LCD1602_EN = 0;
}
void LCD1602_SetCursor(uchar x, uchar y)
{
uchar addr;
if(y == 0) addr = 0x00 + x;
else addr = 0x40 + x;
LCD1602_WriteCmd(addr | 0x80);
}
void LCD1602_Init()
{
LCD1602_WriteCmd(0x38);
delay(5);
LCD1602_WriteCmd(0x38);
delay(5);
LCD1602_WriteCmd(0x38);
delay(5);
LCD1602_WriteCmd(0x38);
delay(5);
LCD1602_WriteCmd(0x08);
delay(5);
LCD1602_WriteCmd(0x01);
delay(5);
LCD1602_WriteCmd(0x06);
delay(5);
LCD1602_WriteCmd(0x0c);
delay(5);
}
void LCD1602_Clear()
{
LCD1602_WriteCmd(0x01);
delay(5);
}
void LCD1602_ShowStr(uchar x, uchar y, uchar *str)
{
LCD1602_SetCursor(x, y);
while(*str != '\0')
{
LCD1602_WriteData(*str++);
}
}
void DS18B20_Rst()
{
DQ_OUT;
DQ = 1;
delay(2);
DQ = 0;
delay(60);
DQ = 1;
delay(2);
}
uchar DS18B20_Check()
{
uchar i;
DQ_IN;
for(i = 0; i < 200; i++)
{
if(DQ_VAL == 0)
{
delay(30);
if(DQ_VAL == 1)
return 0;
else
return 1;
}
}
return 1;
}
uchar DS18B20_ReadBit()
{
uchar dat;
DQ_OUT;
DQ = 1;
delay(2);
DQ = 0;
delay(2);
DQ = 1;
delay(5);
DQ_IN;
dat = DQ_VAL;
delay(55);
return dat;
}
uchar DS18B20_ReadByte()
{
uchar i, j, dat = 0;
for(i = 0; i < 8; i++)
{
j = DS18B20_ReadBit();
dat = (dat) | (j << i);
}
return dat;
}
void DS18B20_WriteByte(uchar dat)
{
uchar i, j;
DQ_OUT;
for(i = 0; i < 8; i++)
{
j = dat >> i;
j &= 0x01;
DQ = 0;
_nop_();
DQ = j;
delay(5);
DQ = 1;
}
}
int DS18B20_GetTemp()
{
uchar tempL, tempH;
int temp;
DS18B20_Rst();
DS18B20_WriteByte(0xcc);
DS18B20_WriteByte(0x44);
delay(150);
DS18B20_Rst();
DS18B20_WriteByte(0xcc);
DS18B20_WriteByte(0xbe);
tempL = DS18B20_ReadByte();
tempH = DS18B20_ReadByte();
temp = tempH;
temp <<= 8;
temp |= tempL;
return temp;
}
void main()
{
int temp;
uchar str[16];
LCD1602_Init();
while(1)
{
temp = DS18B20_GetTemp();
temp = temp * 0.0625; //转换为摄氏度
sprintf(str, "Temp:%dC ", temp);
LCD1602_ShowStr(0, 0, str);
if(temp > tempH)
{
P1 = 0x01;
LCD1602_ShowStr(0, 1, "ALARM:HIGH TEMP ");
}
else if(temp < tempL)
{
P1 = 0x02;
LCD1602_ShowStr(0, 1, "ALARM:LOW TEMP ");
}
else
{
P1 = 0x04;
LCD1602_ShowStr(0, 1, "TEMP NORMAL ");
}
}
}
```
3. 电路连接引脚:
- LCD1602:RS连接P2.6,RW连接P2.5,EN连接P2.7,DB0~DB7连接P0.0~P0.7
- DS18B20:DQ连接P3.7
- 独立按键:K1连接P1.0,K2连接P1.1,K3连接P1.2
- 蜂鸣器:连接P1.3
- LED:连接P1.4~P1.6
- 电源模块:连接5V电源和地线
- 串口下载:连接MCU的P3.0和P3.1。
阅读全文