单片机超声波测距程序
P10,P11,P12为位选,采用共阳数码管显示。精确到CM级别。tring为发射控制,本程序定义为P20引脚。 echo接外部中断0.
#include
#include
sbit wei_ge=P1^0;
sbit wei_shi=P1^1;
sbit wei_bai=P1^2;
sbit a=P2^3;
#define uchar unsigned char
#define uint unsigned int
int time;
int succeed_flag;
uchar timeL;
uchar timeH;
sbit Trig=P2^0;
sbit Echo=P3^2;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay_20us()
{
uchar a ;
for(a=0;a<100;a++);
}
//显示数据转换程序
void display(uint temp)
{
uchar ge,shi,bai;
bai=temp/100;
shi=(temp%100)/10;
ge=temp%10;
P0=table[ge]; //送数字8到段码端口
wei_ge=0;
delay(5);
关键词:
单片机超声波测
评论