基于C8051单片机超声波测距电平触发编写
时间:11-29
来源:互联网
点击:
#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]) ;}}
C8051单片机超声波测距电平触 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)