您的位置 首页 电路

根据C8051单片机超声波测距电平触发编写

includec8051f020h>includeintrinsh>defineucharunsignedchardefineuintunsignedintvoidSYSCLK_Init(

#include

#include #define uchar unsigned char#define uint unsigned intvoid SYSCLK_Init (void);void PORT_Init (void); void  StartModule() ;void display(unsigned char bai,unsigned char shi,unsigned char ge);void delay(unsigned int z);void timerinit();void delay(unsigned int z);void Timer_Count(void);unsigned int  time=0;uchar i;uchar j;unsigned long S=0;unsigned char qian,bai,shi,ge;bit flag =0;sbit lcdrs=P2^5;sbit lcden=P2^7;sbit lcdwr=P2^6;sbit  RX = P0^1;sbit  TX = P0^0;uchar abc[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};   //1234567;uchar result[3];void delay(void){int a=10000;while(a!=0){a--;}}void write_com(uchar com){lcdrs=0;P3=com;delay();lcden=1;delay();lcden=0;}void write_date(uchar dat){lcdrs=1;P3=dat;delay();lcden=1;delay();lcden=0;}void init(){lcdwr=0;lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);}void main(){unsigned int valA;WDTCN = 0xde;WDTCN = 0xad; timerinit();                      SYSCLK_Init ();                                                                 PORT_Init ();                        init();lcdrs=0;lcdwr=0;write_com(0x40);while(1){    RX=1;StartModule();for(valA=100;valA>0;valA--){if(RX==1){Timer_Count();}}}}void SYSCLK_Init (void){OSCICN = 0x05;                                                                                  }void PORT_Init (void){XBR0    = 0x04;                     // Enable UART0XBR1    = 0x00;XBR2    = 0x40;                     // Enable crossbar and weak pull-upsP0MDOUT |= 0xff;                    // enable TX0 as a push-pull outputP2MDOUT  = 0xe0;                    // P2口设为推挽方法P3MDOUT  = 0xff;                    // P2口设为推挽方法      }void Conut(void){time=TH0*256+TL0;TH0=0;TL0=0;S=(uint)(time*1.7*1.6*1.5)/10;     //算出来是MMif((S>=7000)||flag==1) //超出丈量规模显现“-”{ flag=0;display(0x80,0x80,0x80);}else{bai=S/100;shi=S%100/10;ge=S%10;display(bai,shi,ge);}}void zd0() interrupt 1    //T0中止用来计数器溢出,超越测距规模{flag=1;        //中止溢出标志RX=0;}void  StartModule()            //发动模块{TX=1;                        //发动一次模块_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();/*   _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();*/TX=0;}void Timer_Count(void){TR0=1;       //敞开计数while(RX);   //当RX为1计数并等候TR0=0;    //封闭计数Conut();   //核算}void timerinit(){TMOD=0x01;     //设T0为方法1,GATE=1;TH0=0;TL0=0;         ET0=1;             //答应T0中止EA=1;      //敞开总中止   }/*********************************************************/void display(unsigned char bai,unsigned char shi,unsigned char ge){ uchar i;result[0]=abc[bai];result[1]=abc[shi];result[2]=abc[ge];write_com(0x40);for(i=0;i<3;i++){write_com(0x80+i+1);write_date(result[i])  ;}}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/dianlu/255700.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部