protues仿真51单片机1602液晶显示器显示DS18B20与HD-SR04,温度与距离符合一定条件时驱动蜂鸣器或者步进电机
时间: 2023-09-13 13:04:25 浏览: 76
首先,你需要在Proteus中添加51单片机、DS18B20和HD-SR04以及1602液晶显示屏等元器件。然后,你需要编写51单片机的程序,实现以下功能:
1. 初始化1602液晶显示屏和DS18B20和HD-SR04。
2. 循环读取DS18B20的温度值和HD-SR04的距离值,并将它们显示在1602液晶显示屏上。
3. 如果温度值和距离值符合一定的条件,就让蜂鸣器发出声音或者驱动步进电机运动。
下面是一个示例程序,可以参考一下:
```c
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit trig = P3^5;
sbit echo = P3^2;
sbit beep = P1^5;
uchar code table[] = "Temperature: ";
uchar code table1[] = "Distance: ";
uchar code table2[] = " cm";
uchar code table3[] = "Too Hot or too close!";
uchar code table4[] = "Normal";
uint distance;
uchar temp = 0;
uchar flag = 0;
void delay(uint i)
{
while(i--);
}
void write_com(uchar com)
{
P2 = com;
P0 = 0x00;
P0 = 0x04;
delay(5);
P0 = 0x00;
}
void write_data(uchar dat)
{
P2 = dat;
P0 = 0x01;
P0 = 0x05;
delay(5);
P0 = 0x01;
}
void init()
{
write_com(0x38);
write_com(0x0e);
write_com(0x06);
write_com(0x01);
}
void delayms(uint ms)
{
uint i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 120; j++);
}
void ds_delay(uint i)
{
while(i--);
}
uchar ds18b20_init()
{
uchar i;
DSPORT = 0;
ds_delay(80);
DSPORT = 1;
ds_delay(2);
i = DSPORT;
ds_delay(28);
i = DSPORT;
return i;
}
uchar ds18b20_read_byte()
{
uchar i, j, dat = 0;
for(i = 0; i < 8; i++)
{
DSPORT = 0;
ds_delay(2);
DSPORT = 1;
ds_delay(2);
j = DSPORT;
ds_delay(6);
dat = (j << 7) | (dat >> 1);
}
return dat;
}
void ds18b20_write_byte(uchar dat)
{
uchar i, j;
for(i = 0; i < 8; i++)
{
j = dat & 0x01;
DSPORT = 0;
ds_delay(2);
DSPORT = j;
ds_delay(60);
DSPORT = 1;
dat >>= 1;
}
}
int ds18b20_read_temp()
{
uchar TL, TH;
int temp;
ds18b20_init();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0x44);
ds_delay(100);
ds18b20_init();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0xbe);
TL = ds18b20_read_byte();
TH = ds18b20_read_byte();
temp = TH;
temp <<= 8;
temp |= TL;
return temp;
}
void get_distance()
{
TR0 = 1;
trig = 1;
delayms(10);
trig = 0;
while(!echo);
TR0 = 1;
while(echo)
{
TH0 = 0;
TL0 = 0;
}
distance = (TH0 << 8) | TL0;
distance /= 58;
}
void main()
{
TMOD = 0x01;
TH0 = 0;
TL0 = 0;
TR0 = 0;
init();
while(1)
{
temp = ds18b20_read_temp() >> 4;
write_com(0x80);
for(uchar i = 0; i < 12; i++)
write_data(table[i]);
write_com(0x86);
write_data(temp / 10 + '0');
write_data(temp % 10 + '0');
write_data('C');
get_distance();
write_com(0xc0);
for(uchar i = 0; i < 10; i++)
write_data(table1[i]);
write_com(0xc9);
write_data(distance / 10 + '0');
write_data(distance % 10 + '0');
write_com(0xcd);
if(temp > 25 || distance < 10)
{
if(flag == 0)
{
flag = 1;
write_data('!');
write_com(0x8d);
for(uchar i = 0; i < 18; i++)
write_data(table3[i]);
beep = 1;
delayms(500);
beep = 0;
}
}
else
{
if(flag == 1)
{
flag = 0;
write_data(' ');
write_com(0x8d);
for(uchar i = 0; i < 6; i++)
write_data(table4[i]);
}
}
delayms(500);
}
}
```
在程序中,我们通过DS18B20读取温度值,通过HD-SR04读取距离值,并将它们显示在1602液晶显示屏上。如果温度值大于25℃或者距离小于10cm,就让蜂鸣器发出声音或者驱动步进电机运动。
最后,你需要将程序下载到51单片机上,并在Proteus中运行仿真,就可以看到效果了。
阅读全文