|
#include<reg52.h>
#include<intrins.h>
#include"1602.h"
#include"delay.h"
sbit trig = P2^0; // 產生脈沖引腳 tx
sbit echo = P2^1; // 回波引腳 rx
unsigned char flag;
unsigned int s,s1,s2,s3,time;
unsigned char code dofly_code[]={'0','1','2','3','4','5','6','7','8','9'};//轉換成液晶顯示的字符
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
//s=(time*1.56672)/100; //晶振11.0592算出來是CM
s=(time*1.9)/100;//用的時候根據實際反求的
s1=dofly_code[s/100];
s2=dofly_code[(s%100)/10];
s3=dofly_code[(s%100)%10];
//S=(time*1.7)/100; //晶振12M算出來是CM
if((s>=700)||flag==1) //超出測量范圍顯示“
{
flag=0;
LCD_Write_String(1,2," S:--- CM XX ");
delay(500);
LCD_Write_String(1,2," S: CM ");
}
else
{
LCD_Write_String(1,2," S: CM OK ");
LCD_Write_Char(6,2,s1) ;
LCD_Write_Char(7,2,s2) ;
LCD_Write_Char(8,2,s3) ;
// LCD_Write_Char(6,2,s1) ;
}
}
void zd0() interrupt 1 //T0中斷用來計數器溢出,超過測距范圍
{
flag=1; //中斷溢出標志
}
void StartModule() //啟動模塊
{
trig=1; //啟動一次模塊
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
trig=0;
}
void main(void)
{
LCD_Init() ;
LCD_Write_String(1,1," Csheng bo test ");
LCD_Write_String(1,2," S: CM OK ");
while(1)
{
TMOD=0x01; //設T0為方式1,GATE=1;
TH0=0;
TL0=0;
ET0=1; //允許T0中斷
EA=1; //開啟總中斷
while(1)
{
StartModule();
while(!echo); //當Echo為零時為真
TR0=1; //開啟計數
while(echo); //當Echo為1為真
TR0=0; //關閉計數
Conut(); //計算
delay(80); //80MS
}
}
}
這上面是主函數,1602函數和延時函數放在附件里面吧
|
-
-
俠歌無畏.zip
2016-3-22 17:58 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
19.91 KB, 下載次數: 12, 下載積分: 黑幣 -5
評分
-
查看全部評分
|