用keil5写一个超声波测距,超声波管脚:sbit Tring=p3^6;sbit Echo=p3^7;具有蜂鸣器,管脚:sbit Feng=p2^0;sbit k2=p2^7;k3=p2^6;k4=p2^5;用oled屏幕显示距离,且没有oled库,管脚:p1^0;p2p1^1;要求:(1)测量距离范围0-3米,液晶显示距离信息(厘米),测量误差小于5%;(2)液晶显示内容及形式可以自定,但必须包括测量距离信息,设置警戒距离;动态更新测量结果,更新时间小于0.5秒; (3)通过按键设定警戒距离,当距离小于1米,蜂鸣器报警;
时间: 2023-06-01 19:02:26 浏览: 157
超声波测距离
#include <reg52.h>
#include <intrins.h>
sbit Tring = P3^6;
sbit Echo = P3^7;
sbit Feng = P2^0;
sbit k2 = P2^7, k3 = P2^6, k4 = P2^5;
sbit SCL = P1^0;
sbit SDA = P1^1;
void delay_us(unsigned int n)
{
while(n--)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void delay_ms(unsigned int n)
{
while(n--)
{
delay_us(1000);
}
}
void IIC_Start()
{
SDA = 1;
delay_us(5);
SCL = 1;
delay_us(5);
SDA = 0;
delay_us(5);
SCL = 0;
delay_us(5);
}
void IIC_Stop()
{
SDA = 0;
delay_us(5);
SCL = 1;
delay_us(5);
SDA = 1;
delay_us(5);
}
void IIC_WriteByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
dat <<= 1;
SDA = CY;
delay_us(5);
SCL = 1;
delay_us(5);
SCL = 0;
delay_us(5);
}
SDA = 1;
delay_us(5);
SCL = 1;
delay_us(5);
SCL = 0;
delay_us(5);
}
void OLED_Init()
{
IIC_Start();
IIC_WriteByte(0x78);
IIC_WriteByte(0x00);
IIC_WriteByte(0xAE);
IIC_WriteByte(0xD5);
IIC_WriteByte(0x80);
IIC_WriteByte(0xA8);
IIC_WriteByte(0x3F);
IIC_WriteByte(0xD3);
IIC_WriteByte(0x00);
IIC_WriteByte(0x40);
IIC_WriteByte(0x8D);
IIC_WriteByte(0x14);
IIC_WriteByte(0x20);
IIC_WriteByte(0x00);
IIC_WriteByte(0xA1);
IIC_WriteByte(0xC8);
IIC_WriteByte(0xDA);
IIC_WriteByte(0x12);
IIC_WriteByte(0x81);
IIC_WriteByte(0xCF);
IIC_WriteByte(0xD9);
IIC_WriteByte(0xF1);
IIC_WriteByte(0xDB);
IIC_WriteByte(0x40);
IIC_WriteByte(0xA4);
IIC_WriteByte(0xA6);
IIC_WriteByte(0xAF);
IIC_Stop();
}
void OLED_Clear()
{
unsigned char i,j;
for(i=0;i<8;i++)
{
IIC_Start();
IIC_WriteByte(0x78);
IIC_WriteByte(0x00);
IIC_WriteByte(0xB0+i);
IIC_WriteByte(0x00);
IIC_WriteByte(0x10);
for(j=0;j<128;j++)
{
IIC_WriteByte(0x00);
}
IIC_Stop();
}
}
void OLED_ShowChar(unsigned char x, unsigned char y, unsigned char chr)
{
unsigned char c = 0,i = 0;
c = chr - 32;
if(x > 127 || y > 7)
{
return;
}
IIC_Start();
IIC_WriteByte(0x78);
IIC_WriteByte(0x00);
IIC_WriteByte(0xB0 + y);
IIC_WriteByte(((x & 0xF0) >> 4) | 0x10);
IIC_WriteByte((x & 0x0F) | 0x00);
for(i=0;i<8;i++)
{
IIC_WriteByte(FONT8X16[c*16+i]);
}
for(i=0;i<8;i++)
{
IIC_WriteByte(FONT8X16[c*16+i+8]);
}
IIC_Stop();
}
void OLED_ShowString(unsigned char x, unsigned char y, unsigned char *chr)
{
unsigned char j=0;
while (chr[j]!='\0')
{
OLED_ShowChar(x,y,chr[j]);
x+=8;
if(x>120)
{
x=0;
y++;
}
j++;
}
}
void main()
{
unsigned int distance = 0;
unsigned char str[20];
unsigned char warning_distance = 100; // 警戒距离,单位:厘米
Feng = 0;
OLED_Init();
OLED_Clear();
while(1)
{
Tring = 1;
delay_us(10);
Tring = 0;
while(!Echo);
TH0 = TL0 = 0;
while(Echo);
distance = (unsigned int)(TH0<<8 | TL0) / 58;
sprintf(str, "Distance: %dcm", distance);
OLED_ShowString(0,0,str);
if(distance < warning_distance)
{
Feng = 1;
}
else
{
Feng = 0;
}
if(k2 == 0)
{
warning_distance = 50;
OLED_Clear();
OLED_ShowString(0,0,"Warning Distance: 50cm");
}
if(k3 == 0)
{
warning_distance = 100;
OLED_Clear();
OLED_ShowString(0,0,"Warning Distance: 100cm");
}
if(k4 == 0)
{
warning_distance = 150;
OLED_Clear();
OLED_ShowString(0,0,"Warning Distance: 150cm");
}
delay_ms(50);
}
}
阅读全文