keil5运行hc-sr04超声波测距用lcd1602显示,用声光报警器报警代码
时间: 2023-11-05 14:11:00 浏览: 236
我可以为您提供以下参考代码:
```c
#include <reg52.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
sbit ECHO = P3^2;
sbit TRIG = P3^3;
sbit buzz = P2^3;
void delay_us(uint us)
{
while(us--)
{
_nop_();
}
}
void delay_ms(uint ms)
{
while(ms--)
{
delay_us(1000);
}
}
void LcdWriteCom(uchar com)
{
P0 = com;
P2 &= ~0x01;
_nop_();
P2 |= 0x01;
delay_us(100);
P2 &= ~0x01;
}
void LcdWriteData(uchar dat)
{
P0 = dat;
P2 |= 0x04;
_nop_();
P2 |= 0x01;
delay_us(100);
P2 &= ~0x01;
}
void LcdInit()
{
LcdWriteCom(0x38);
delay_ms(5);
LcdWriteCom(0x38);
delay_ms(5);
LcdWriteCom(0x38);
delay_ms(5);
LcdWriteCom(0x08);
delay_ms(5);
LcdWriteCom(0x01);
delay_ms(5);
LcdWriteCom(0x06);
delay_ms(5);
LcdWriteCom(0x0c);
delay_ms(5);
}
void LcdShowChar(uchar X, uchar Y, uchar DData)
{
if(Y==0) LcdWriteCom(0x80+X);
else if(Y==1) LcdWriteCom(0xc0+X);
else if(Y==2) LcdWriteCom(0x94+X);
else if(Y==3) LcdWriteCom(0xd4+X);
LcdWriteData(DData);
}
void LcdShowStr(uchar X, uchar Y, uchar *DData)
{
while(*DData!='\0')
{
LcdShowChar(X, Y, *DData);
X++;
DData++;
}
}
void beep(int freq, int time)
{
int i, j;
while(time--)
{
buzz = 1;
for(i=0;i<freq;i++)
{
for(j=0;j<10;j++);
}
buzz = 0;
for(i=0;i<freq;i++)
{
for(j=0;j<10;j++);
}
}
}
uint GetEchoLength()
{
uint length = 0;
TRIG = 1;
delay_us(10);
TRIG = 0;
while(!(ECHO));
while(ECHO)
{
delay_us(1);
length++;
}
return length;
}
int main()
{
uint distance;
float dist;
uchar s[16];
LcdInit();
LcdShowStr(0, 0, (uchar *)"Distance:");
LcdShowStr(0, 1, (uchar *)"");
LcdShowStr(12, 1, (uchar *)"cm");
delay_ms(500);
while(1)
{
distance = GetEchoLength();
dist = (float)distance * 0.015; // 单位换算,计算距离
LcdShowStr(0, 1, (uchar *)" ");
sprintf(s, "%.2f", dist); // 转换为字符串并显示
LcdShowStr(0, 1, s);
if(dist < 10.0) beep(1000, 200); // 距离小于10cm,报警
delay_ms(500);
}
return 0;
}
```
这是使用Keil5开发的,可以测量HC-SR04超声波传感器的距离,并将距离值显示在LCD1602上,在距离小于10cm时还会触发声光报警器。希望这能帮到你!
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)