实现智能小车的红外避障、循迹、测速控速和1602显示的功能,
直接上程序了
单片机源程序:
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- void delayXms(uint );
- void delay1ms(void);
- void PWM(void);
- uint i,m,n,z;
- uint h=5, l=5;
- uchar bai1,shi1,ge1,bai2,shi2,ge2,bai3,shi3,ge3;
- long int s,s1,s2,M1,M2,f;
- sbit deng=P3^7;
- sbit fen=P2^7;
- sbit zuo=P2^0;
- sbit jia=P3^7;
- sbit you=P2^1;
- sbit bi=P2^2;
- sbit Zbi=P2^3;
- sbit Ybi=P2^4;
- sbit pwm1 = P1^6;
- sbit pwm2 = P1^7;
- sbit IN1 = P1^2;
- sbit IN2 = P1^3;
- sbit IN3 = P1^4;
- sbit IN4 = P1^5;
- sbit RS=P1^0;
- sbit RW=P1^1;
- sbit E =P2^5;
- uchar dis1[]={"Fr: Hz PWM: %"};
- uchar dis2[]={"Speed: mm/s"};
- void forward()
- {
- IN1 = 0;
- IN2 = 1;
- IN3 = 0;
- IN4 = 1;
- }
- void go()
- {
- pwm1 = 1;
- pwm2 = 1;
- }
- void stop()
- {
- pwm1 = 0;
- pwm2 = 0;
- }
- void PWM(void)
- { go();
- delayXms(h);
- stop();
- delayXms(l);
- }
- void delayXms(uint ms)
- {
- uint k;
- for(k=0;k<ms;k++)
- {delay1ms();}
- }
- void delayms(uint xms)
- {
- uint i,j;
- for(i=xms;i>0;i++)
- for(j=200;j>0;j--);
- }
- void delay1ms(void)
- {
- uint j;
- for(j=0;j<=120;j++)
- {_nop_();}
- }
- uchar lcd_busy()
- {
- bit result;
- RS=0;
- RW=1;
- E=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- result = (bit)(P0&0x80);
- E=0;
- return result;
- }
- void lcd_wcmd(uchar cmd)
- {
- while(lcd_busy());
- RS=0;
- RW=0;
- E=0;
- _nop_();
- _nop_();
- P0=cmd;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- E=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- E=0;
- }
- void lcd_pos(uchar pos)
- {
- lcd_wcmd(pos|0x80);
- }
- void lcd_wdat(uchar dat)
- {
- while(lcd_busy());
- RS=1;
- RW=0;
- E=0;
- P0=dat;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- E=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- E=0;
- }
- void LcdInit() /*LCD1602初始化函数*/
- {
- delayXms(12);
- lcd_wcmd(0x38);
- delayXms(4);
- lcd_wcmd(0x38);
- delayXms(4);
- lcd_wcmd(0x38);
- delayXms(4);
- lcd_wcmd(0x0c);
- delayXms(4);
- lcd_wcmd(0x06);
- delayXms(4);
- lcd_wcmd(0x01);
- delayXms(4);
- }
- /*******************定时器1初使化函数**********************/
- void T1_Init()
- {
- TMOD = 0x10;
- ET1=1;
- EA=1; //打开总中断
- IP=0x04; //设置中断优先级
- TH1 = (65535-50000)/256; //初使加载50毫秒
- TL1 = (65535-50000)%256;
- }
- /*******************外部中断初使化函数***********************/
- void INT_Init()
- {
- IT0 = 1; //设置外部中断0为下降沿触发
- // IT1 = 1; //设置外部中断1为下降沿触发
- EX0 = 1; //开外部中断0
- // EX1 = 1; //开外部中断1
- EA = 1; //开总中断
- }
- /********************外部中断函数**************************/
- void my_INT0(void) interrupt 0
- {
- M1=M1++;
- }
- //void my_INT1(void) interrupt 2
- //{
- // M2=M2++;
- //}
- /******************定时器1中断函数*************************/
- void timer1() interrupt 3
- { TR1=0;
- TH1 = (65535-50000)/256;
- TL1 = (65535-50000)%256;
- n=n+1;
- if(n==20)
- {
- f=M1;
- shi1=(f/10)+48;
- ge1=(f%10)+48;
- z=100*h/(h+l);
- bai2=(z/100)+48;
- shi2=(z%100/10)+48;
- ge2=(z%100%10)+48;
- s1=M1/100.00*3.14*65;
- //s2=M2/100.00*3.14*65;
- s=s1;
- bai3=(s/100)+48;
- shi3=(s%100/10)+48;
- ge3=(s%100%10)+48;
- n=0;
- s1=0;
- // s2=0;
- M1=0;
- // M2=0;
- }
- TR1=1; //启动T1
- }
- void xunji()
- {
- if(zuo==0&&you==0&&bi==1&&Zbi==1&&Ybi==1) //zhizou
- {
- go();
- delayXms(h);
- stop();
- delayXms(l);
- deng=0;
- fen=0;
- }
- if(zuo==1&&you==1&&bi==1&&Zbi==1&&Ybi==1) //tingzhi
- {
- pwm1 = 0;
- pwm2 = 0;
- fen=0;
- deng=0;
- }
- else if(zuo==1&&you==0&&bi==1&&Zbi==1&&Ybi==1) //zuozhuan
- {
- pwm1=1;
- pwm2 = 0;
- deng=0;
- fen=1;
- }
- else if(zuo==0&&you==1&&bi==1&&Zbi==1&&Ybi==1) //youzhuan
- {
- pwm2=1;
- delayXms(1);
- pwm2=0;
- delayXms(0);
- pwm1 = 0;
- deng=0;
- fen=1;
- }
- else if(bi==0&&Zbi==0&&Ybi==0&&zuo==0&&you==0) //zhongbi ting
- {
- pwm1 = 0;
- pwm2 = 0;
- deng=1;
- fen=1;
- }
- else if(Zbi==0&&bi==0)
- {
- pwm2=1;
- delayXms(h);
- pwm2=0;
- delayXms(l);
- pwm1 = 0;
- deng=1;
- fen=0;
- }
- else if(Ybi==0&&bi==0)
- {
- pwm1=1;
- delayXms(h);
- pwm1=0;
- delayXms(l);
- pwm2 = 0;
- deng=1;
- fen=0;
- }
- else if(Ybi==0&&bi==1&&Zbi==1)
- {
- pwm1=1;
- delayXms(h);
- pwm1=0;
- delayXms(l);
- pwm2 = 0;
- deng=1;
- fen=0;
- }
- else if(Zbi==0&&bi==1&&Ybi==1)
- {
- pwm2=1;
- delayXms(h);
- pwm2=0;
- delayXms(l);
- pwm1 = 0;
- deng=1;
- fen=0;
- }
- }
- void main()
- {
- T1_Init();
- INT_Init();
- LcdInit();
- TR1=1;
- lcd_pos(0x00);
- i=0;
- while(dis1[ i] != '\0') //1602液晶显示器第一行初使化显示
- {
- lcd_wdat(dis1[ i]);
- i++;
- };
- i=0;
- lcd_pos(0x41);
- while(dis2 != '\0') //1602液晶显示器第二行初使化显示
- {
- lcd_wdat(dis2);
- i++;
- };
- while(1)
- {
- forward();
- xunji();
- lcd_pos(0x03);
- lcd_wdat(shi1);
- lcd_pos(0x04);
- lcd_wdat(ge1);
- lcd_pos(0x0c);
- lcd_wdat(bai2);
- lcd_pos(0x0d);
- lcd_wdat(shi2);
- lcd_pos(0x0e);
- lcd_wdat(ge2);
- lcd_pos(0x47);
- lcd_wdat(bai3);
- lcd_pos(0x48);
- lcd_wdat(shi3);
- lcd_pos(0x49);
- lcd_wdat(ge3);
- }
- }
复制代码
|