超聲波數碼管顯示
0.png (281.4 KB, 下載次數: 61)
下載附件
2017-8-3 17:37 上傳
0.png (51.21 KB, 下載次數: 59)
下載附件
2017-8-3 17:37 上傳
單片機脈寬測量源程序如下:
- /***********************************************************************************************************/
- //hc-sr04 超聲波測距模塊 DEMO 程序
- //晶振:11。0592
- //程序 QQ: 517535000
- //接線:模塊TRIG接 P0.2 ECH0 接P0.1
- //數碼管:共陽數碼管P1接數據口,P2.5 P2.4 P2.3接選通數碼管
- /***********************************************************************************************************/
- #include <AT89x51.H> //器件配置文件
- #include <intrins.h>
- #define RX P0_1
- #define TX P0_2
- unsigned int time=0;
- unsigned int timer=0;
- unsigned char posit=0;
- unsigned long S=0;
- bit flag =0;
- unsigned char const discode[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
- unsigned char const positon[3]={ 0xdf,0xef,0xf7};
- unsigned char disbuff[4] ={ 0,0,0,0,};
- /********************************************************/
- void Display(void) //掃描數碼管
- {
- if(posit==0)
- {P1=(discode[disbuff[posit]])&0x7f;}
- else
- {P1=discode[disbuff[posit]];}
- P2=positon[posit];
- if(++posit>=3)
- posit=0;
- }
- /********************************************************/
- void Conut(void)
- {
- time=TH0*256+TL0;
- TH0=0;
- TL0=0;
-
- S=(time*1.7)/100; //算出來是CM
- if((S>=700)||flag==1) //超出測量范圍顯示“-”
- {
- flag=0;
- disbuff[0]=10; //“-”
- disbuff[1]=10; //“-”
- disbuff[2]=10; //“-”
- }
- else
- {
- disbuff[0]=S%1000/100;
- disbuff[1]=S%1000%100/10;
- disbuff[2]=S%1000%10 %10;
- }
- }
- /********************************************************/
- void zd0() interrupt 1 //T0中斷用來計數器溢出,超過測距范圍
- {
- flag=1; //中斷溢出標志
- }
- /********************************************************/
- void zd3() interrupt 3 //T1中斷用來掃描數碼管和計800MS啟動模塊
- {
- TH1=0xf8;
- TL1=0x30;
- Display();
- timer++;
- if(timer>=400)
- {
- timer=0;
- TX=1; //800MS 啟動一次模塊
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- TX=0;
- }
- }
- /*********************************************************/
- void main( void )
- {
- TMOD=0x11; //設T0為方式1,GATE=1;
- TH0=0;
- TL0=0;
- TH1=0xf8; //2MS定時
- TL1=0x30;
- ET0=1; //允許T0中斷
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
超聲波測距數碼管顯示.rar
(74.8 KB, 下載次數: 34)
2017-8-3 10:35 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|