通过1602显示屏显示以ds18b20温度传感器测试的温度和hr04超声波模块测试的距离的51单片机程序
时间: 2023-10-31 07:05:22 浏览: 33
以下是基于ds18b20和hr04的温度和距离测量的51单片机程序,并将结果显示在1602液晶屏上:
```c
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
sbit LCD_D4=P2^4;
sbit LCD_D5=P2^5;
sbit LCD_D6=P2^6;
sbit LCD_D7=P2^7;
uchar code table[]="Temp: 00 C Dist: 000 cm"; //初始化显示内容
void Delay5us() //5us延时函数
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void Delay1ms(uint x) //毫秒级延时函数
{
uint i,j;
for(i=0;i<x;i++)
{
for(j=0;j<114;j++);
}
}
void InitLCD() //LCD初始化函数
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
Delay1ms(15);
LCD_EN=1;
LCD_D7=1;
LCD_D6=1;
LCD_D5=0;
LCD_D4=0;
LCD_EN=0;
Delay5us();
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_D7=0;
LCD_D6=0;
LCD_D5=1;
LCD_D4=0;
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_D7=0;
LCD_D6=0;
LCD_D5=0;
LCD_D4=0;
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_D7=0;
LCD_D6=0;
LCD_D5=0;
LCD_D4=1;
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_D7=0;
LCD_D6=0;
LCD_D5=0;
LCD_D4=0;
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_D7=0;
LCD_D6=0;
LCD_D5=0;
LCD_D4=1;
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_D7=1;
LCD_D6=1;
LCD_D5=1;
LCD_D4=1;
LCD_EN=1;
LCD_EN=0;
Delay5us();
}
void WriteCom(uchar com) //写命令函数
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_D7=(com>>7)&0x01;
LCD_D6=(com>>6)&0x01;
LCD_D5=(com>>5)&0x01;
LCD_D4=(com>>4)&0x01;
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_D7=(com>>3)&0x01;
LCD_D6=(com>>2)&0x01;
LCD_D5=(com>>1)&0x01;
LCD_D4=com&0x01;
LCD_EN=1;
LCD_EN=0;
Delay5us();
}
void WriteData(uchar dat) //写数据函数
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
LCD_D7=(dat>>7)&0x01;
LCD_D6=(dat>>6)&0x01;
LCD_D5=(dat>>5)&0x01;
LCD_D4=(dat>>4)&0x01;
LCD_EN=1;
LCD_EN=0;
Delay5us();
LCD_D7=(dat>>3)&0x01;
LCD_D6=(dat>>2)&0x01;
LCD_D5=(dat>>1)&0x01;
LCD_D4=dat&0x01;
LCD_EN=1;
LCD_EN=0;
Delay5us();
}
void WriteString(uchar x, uchar y, uchar *s) //在指定位置写入字符串函数
{
uchar i;
if(x>0) x--;
if(y>0) y--;
i=x;
i|=0x80;
i|=y<<6;
WriteCom(i);
while(*s)
{
WriteData(*s);
s++;
}
}
void WriteTemp(uchar temp) //写入温度函数
{
uchar temp1,temp2;
temp1=temp/10+'0';
temp2=temp%10+'0';
table[6]=temp1;
table[7]=temp2;
WriteString(0,0,table);
}
void WriteDist(int dist) //写入距离函数
{
uchar dist1,dist2,dist3;
dist1=dist/100+'0';
dist2=dist%100/10+'0';
dist3=dist%10+'0';
table[14]=dist1;
table[15]=dist2;
table[16]=dist3;
WriteString(0,0,table);
}
uchar Init_DS18B20() //DS18B20初始化函数
{
uchar i;
DSPORT=0;
i++;
DSPORT=1;
i++;
if(DSPORT==0)
{
i++;
return 0;
}
else
{
i++;
return 1;
}
}
void Write_DS18B20(uchar dat) //DS18B20写函数
{
uchar i;
for(i=0;i<8;i++)
{
DSPORT=0;
DSPORT=dat&0x01;
_nop_();
_nop_();
_nop_();
DSPORT=1;
dat>>=1;
}
}
uchar Read_DS18B20() //DS18B20读函数
{
uchar i,dat;
for(i=0;i<8;i++)
{
DSPORT=0;
DSPORT=1;
_nop_();
_nop_();
_nop_();
dat|=(DSPORT<<i);
}
return dat;
}
void Conversion_DS18B20() //DS18B20转换函数
{
Init_DS18B20();
Delay1ms(1);
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
}
uchar ReadTemp_DS18B20() //DS18B20温度读取函数
{
uchar temp;
Init_DS18B20();
Delay1ms(1);
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
temp=Read_DS18B20();
temp=Read_DS18B20();
return temp;
}
void Init_HC_SR04() //HC-SR04初始化函数
{
TR1=0;
TH1=0;
TL1=0;
TR0=0;
TH0=0;
TL0=0;
IT0=1;
EX0=1;
IT1=1;
EX1=1;
}
void Trigger_HC_SR04() //HC-SR04触发函数
{
TH0=0;
TL0=0;
TR0=1;
P3_5=1;
Delay1ms(10);
P3_5=0;
TR0=0;
}
void main()
{
uchar temp;
int dist;
InitLCD();
WriteString(0,0,table);
Init_DS18B20();
Init_HC_SR04();
while(1)
{
Conversion_DS18B20();
temp=ReadTemp_DS18B20();
WriteTemp(temp);
Delay1ms(1000);
Trigger_HC_SR04();
Delay1ms(20);
}
}
void Interrupt_0() interrupt 0 //超声波模块中断函数
{
TH1=TL1=0;
TR1=1;
}
void Interrupt_1() interrupt 2 //超声波模块中断函数
{
TR1=0;
dist=TH1*256+TL1;
dist=dist/58;
WriteDist(dist);
}
```
需要注意的是,上述代码中的DS18B20和HC-SR04的具体接线和引脚配置应根据实际情况进行调整。同时,代码中的部分参数和函数也需要根据实际情况进行修改。