單片機超聲波測距程序
P10,P11,P12為位選,采用共陽數(shù)碼管顯示。精確到CM級別。tring為發(fā)射控制,本程序定義為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++);
}
//顯示數(shù)據(jù)轉(zhuǎn)換程序
void display(uint temp)
{
uchar ge,shi,bai;
bai=temp/100;
shi=(temp%100)/10;
ge=temp%10;
P0=table[ge]; //送數(shù)字8到段碼端口
wei_ge=0;
delay(5);
關(guān)鍵詞:
單片機超聲波測
評論