- #include<reg52.h>
- #include<pwmkl.h>
- unsigned int count,i,j;
- unsigned int n;
- void delay(int xms)
- {
- for(i=1000;i--;i>0)
- for(j=xms;j--;j>0);
- }
- void xunji()
- {
- if((leftxled==0)&&(rightxled==0)/*&&(leftxled1==0)&&(rightxled1==0)*/) //向前走
- {
- n=0;
- }
- else if((leftxled==1)&&(rightxled==0)/*&&(leftxled1==0)&&(rightxled1==0)*/) //左1
- {
- n=3;
- }
- /* else if((leftxled==0)&&(rightxled==1)&&(leftxled1==0)&&(rightxled1==0)) //左2
- {
- n=2;
- }*/
- else if((leftxled==0)&&(rightxled==1)/*&&(leftxled1==0)&&(rightxled1==0)*/)//右1
- {
- n=1;
- }
- /*else if((leftxled==0)&&(rightxled==0)&&(leftxled1==0)&&(rightxled1==1))//右2
- {
- n=4;
- }*/
- else if((leftxled==1)&&(rightxled==1)/*&&(leftxled1==1)&&(rightxled1==1)*/)//停
- {
- n=5;
- }
- switch(n)
- {
- case(0):run();;break;
- case(1):leftrun();break;
- case(2):rightrun();break;
- case(3):rightrun();break;
- case(4):rightrun();break;
- case(5):stop();break;
- default:break;
- }
- }
- void main()
- {
- TMOD=0x01;
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- /*TH1=0xff;
- TL1=0xa3; */
- EA=1;
- /*ET1=1;
- TR1=1;*/
- ET0=1;
- TR0=1;
- /*IT0=1;
- EX0=1;*/
- while(1)
- {
- xunji();
- }
- }
- /*void Time1_Int() interrupt 3//
- {
- TH1=0xff;
- TL1=0xa3;
- count=(count+1);//0.5ms次数加1
- count=count%40;//次数始终保持为40即保持周期为20ms
- }*/
- /*中断*/
- void Int0() interrupt 0
- {
- if(leftxled==0&&rightxled==1)//靠左边1检查到白,右边检查到黑
- {
-
- leftrun();
- delay(100);
- stop();
- }
- if(leftxled1==0&&rightxled1==1)//靠左边2检查到白,右边检查到黑
- {
-
- leftrun();
- delay(100);
- stop();
- }
- if(leftxled==1&&rightxled==0) //靠右边1检查到白,左边检查到黑
- {
-
- rightrun();
- delay(100);
- stop();
- }
- if(leftxled1==1&&rightxled1==0) //靠右边1检查到白,左边检查到黑
- {
-
- rightrun();
- delay(100);
- stop();
- }
- }
- /****************************************pwm 头文件***************************/
- #ifndef _LED_H_
- #define _LED_H_
- sbit IN1=P1^0; // 右1电机
- sbit IN2=P1^1; // 右1电机
- sbit IN3=P1^2; // 左1电机
- sbit IN4=P1^3; // 左1电机
- //循迹传感器定义 左P3^6,右P3^5。
- #define leftxled P3_6 //靠左边1循迹小灯
- #define rightxled P3_5 //右1
- #define leftxled1 P3_4 //靠左边2循迹小灯
- #define rightxled1 P3_7
- /*避障传感器定义 左P3^7,右P3^4。
- #define leftbled P3^7 //右边循迹小灯
- #define rightbled P3^4 */
- //定义pwm始能端
- #define leftpwm P1_4 //左边pwm 始能端
- #define rightpwm P1_5 //右边pwm 始能端
- //电机转向
- #define leftgo {IN1=1,IN2=0;} //左边电机前进
- #define leftstop {IN1=0,IN2=0;}// 左 停
- #define rightgo {IN3=1,IN4=0;} //右边电机前进
- #define rightstop {IN3=0,IN4=0;}// 右 停
- #define leftback {IN1=0,IN2=1;} //左边电机后退
- #define rightback {IN3=0,IN4=1;} //右边电机后退
- //定义pwm、占空比
- unsigned char pwmleft =0;//变量定义
- unsigned char pwmright =0;
- unsigned char pushleft =0;//占空比N/10
- unsigned char pushright =0;
- //定义固定变量
- bit k=1;
- bit kk=1;
- unsigned int time=0;
- //前进函数
- void run()
- {
- pushleft=4;
- leftgo;
- pushright=4;
- rightgo;
- }
- //houtui
- void back()
- {
- leftback;
- rightback;
- }
- void stop()
- {
- leftstop;
- rightstop;
- }
- //左转函数
- void leftrun()
- {
- pushleft=15;
- leftgo;
- pushright=12;
- rightback;
- }
- //右转函数
- void rightrun()
- {
- pushleft=12;
- leftback;
- pushright=15;
- rightgo;
- }
- /******************pwm 调速************************************/
- //左电机进行调速
- void leftpwml()
- {
- if(k)
- {
- if(pwmleft<=pushleft)
- {
- leftpwm=1;
- }
- else
- {
- leftpwm=0;
- }
- if(pwmleft>=20)
- {
- pwmleft=0;
- }
- }
- else
- {
- leftpwm=1;
- }
- }
- //右电机调速
- void rightpwmr()
- {
- if(kk)
- {
- if(pwmright<=pushright)
- {
- rightpwm=1;
- }
- else
- {
- rightpwm=0;
- }
- if(pwmright>=20)
- {
- pwmright=0;
- }
- }
- else
- {
- rightpwm=1;
- }
- }
- //定时器0产生中断信号
- void time0() interrupt 1
- {
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- time++;
- pwmleft++;
- pwmright++;
- leftpwml();
- rightpwmr();
- }
- #endif
复制代码
|