基于51单片机来实现一个倒车雷达系统,可通过按键来调整雷达预警的阈值。也可以调节报警范围,代码也比较简单,可靠性强,可转换为实物,也可用于设计,仿真Proteus如图。
仿真效果图
- #include<reg51.h>
- #include<intrins.h>
- sbit LED0=P2^0;
- sbit LED1=P2^1;
- sbit speak=P1^7;
- sbit key_up=P1^0;
- sbit key_dn=P1^1;
- sbit TRIG=P3^7;
- sbit ECHO=P3^2;
- sbit LCD_E=P2^5;
- sbit LCD_RW=P2^6;
- sbit LCD_RS=P2^7;
- unsigned int Set=40;//距离值,单位cm
- unsigned int Now;
- unsigned int count_T0=0,count_1ms,miao=0;
- unsigned int High_Time;
- unsigned char flag;
- unsigned char code word1[]="Set= cm";//0--15
- unsigned char code word2[]="Now= cm";//0--15
- unsigned char code word3[]="0123456789";
- void lcd_write_com(unsigned char com);
- void lcd_write_data(unsigned char dat);
- void lcdInit();
- void display_LCD(unsigned char hang,unsigned char lie,unsigned dat);
- void Timer0_Init();
- void delayms(unsigned char t);
- void scan_key();
- unsigned int WAVE();
- void display_string();
- void Timer_delay(unsigned int BS);
- void Speak();
- void main()
- {
- unsigned char bai,shi,ge;
- lcdInit();
- display_string();//显示开始的字母
- while(1)
- {
- scan_key();
- if((Set/100%10)!=0)
- {
- display_LCD(0,6,word3[Set/100%10]);
- delayms(1);
- }
- display_LCD(0,7,word3[Set/10%10]);
- delayms(1);
- display_LCD(0,8,word3[Set%10]);
- delayms(1);
-
- High_Time=WAVE();//超声波
- //Now=(int)(High_Time*1.72)/100;
- Now=(int)(High_Time*0.0175);
- Speak();
-
- bai=Now/100%10;
- shi=Now/10%10;
- ge=Now%10;
-
- if(bai!=0)
- {
- display_LCD(1,6,word3[bai]);
- delayms(1);
- }
- display_LCD(1,7,word3[shi]);
- delayms(1);
- display_LCD(1,8,word3[ge]);
- delayms(1);
- }
- }
- /*——————————蜂鸣器发声————————*/
- void Speak()
- {
- if(Now<Set)
- {
- speak=0;
- Timer_delay(1);
- }
- else if(Now<(Set+0))
- {
- speak=0;
- Timer_delay(5);
- }
- else
- {
- speak=1;
- }
- }
- //*————————————定时器T1延时————————————*/
- void Timer_delay(unsigned int BS)//T1延时0.5ms
- {
- unsigned int k;
- for(k=0;k<BS;k++)
- {
- TH1=(65536-100)/256;
- TL1=(65536-100)%256;
- while(TF1==0);
- TF1=0;
- }
- }
- //*——————显示开始的字母————————————*/
- void display_string()
- {
- unsigned m,n;
- for(m=0;m<16;m++)
- {
- display_LCD(0,m,word1[m]);
- delayms(1);
- }
- for(n=0;n<16;n++)
- {
- display_LCD(1,n,word2[n]);
- delayms(1);
- }
- }
- /*——————————————超声波计算时间————————————*/
- unsigned int WAVE()
- {
- unsigned int result;
- unsigned char p;
- TRIG=0;
- _nop_();//1微秒
- TRIG=1;
- for(p=0;p<10;p++);//大于10us
- TRIG=0;
- while(ECHO==0);//等高电平来
- Timer0_Init();//开始计时记高电平时间即超声波发射--返回时间
- while(ECHO)
- {
- if((TH0>0x8c)|| (TH0==0x8c&&TL0>0xa0))break;
- }
- TR0=0;
- //18us=18000ms=4650H ;100us--18ms有效 超过36ms无效 36ms=8cA0
- if((TH0<0x46)|| (TH0==0x46&&TL0<=0x50))
- {
- result=(TH0<<8)+TL0;
- return result;
- }
- else return 0;
- }
- /*——————————————LCD初始化————————————*/
- void lcdInit()
- {
- lcd_write_com(0x38);//字符为5*7点阵
- lcd_write_com(0x0c); //显示开 光标关 闪烁关
- lcd_write_com(0x06);//写入数据后光标右移一位 显示屏不动
- lcd_write_com(0x01);//清屏
- lcd_write_com(0x80);//设置数据指针起点
- }
- /**———————————LCD写命令——————————————*/
- void lcd_write_com(unsigned char com)
- {
- LCD_E=0;
- LCD_RS=0;//命令
- LCD_RW=0;//写入
- P0=com;
- delayms(1);
- LCD_E=1;//写入时序
- delayms(1);
- LCD_E=0;
- }
- /*————————LCD写数据————————————————————*/
- void lcd_write_data(unsigned char dat)
- {
- LCD_E=0;
- LCD_RW=0;//写入
- LCD_RS=1;//数据
- P0=dat;
- delayms(1);
- LCD_E=1;
- delayms(1);
- LCD_E=0;
- }
- //*———————————液晶屏显示—————————————————*/
- void display_LCD(unsigned char hang,unsigned char lie,unsigned value)
- {
- if(hang==0)
- {
- lcd_write_com(0x80+lie);
- }
- if(hang==1)
- {
- lcd_write_com(0xc0+lie);
- }
- lcd_write_data(value);
- }
- /*————————定时器初始化————————*///T1延时,T0中断
- void Timer0_Init()
- {
- //count_T0=0;
- //miao=0;
- TMOD=0x11;
- TH0=0;
- TL0=0;
- //ET0=1;
- TR0=1;TR1=1;
- //EA=1;
- }
- void Timer0() interrupt 1
- {
- TH0=0;
- TL0=0;
- }
- //*———————————按键扫描—————————————————*/
- void scan_key()
- {
- P1=0xff;
- if(P1!=0xff)delayms(10);
- //key_up=0;
- if(P1!=0xff)
- {
- switch(P1&0xff)
- {
- case 0xfe :Set=Set+1;LED0=0;break;//key_up p1.0
- case 0xfd :Set=Set-1;break;//key_dn p1.1
- }
- while(P1!=0xff);
- }
- }
- //*——————————————————————————————————————————*/
- void delayms(unsigned char t)
- {
- unsigned i,j;
- for(i=0;i<t;i++)
- for(j=0;j<120;j++);
- }
复制代码
|