我给你来个程序试试
- #include <AT89X52.H> //包含51单片机头文件,内部有各种寄存器定义
- #define Left_1_led P3_4 //P3_4接四路寻迹模块接口第一路输出信号即中控板上面标记为OUT1
- #define Left_2_led P3_5 //P3_5接四路寻迹模块接口第二路输出信号即中控板上面标记为OUT2
- #define Right_1_led P3_6 //P3_6接四路寻迹模块接口第三路输出信号即中控板上面标记为OUT3
- #define Right_2_led P3_7 //P3_7接四路寻迹模块接口第四路输出信号即中控板上面标记为OUT4
- #define Left_moto_pwm P1_1 //PWM信号端
- #define Left_moto_pwm1 P1_3
- #define Right_moto_pwm P1_5
- #define Right_moto_pwm1 P1_7
- #define Left_moto_go {P1_0=1,P1_1=0,P1_2=1,P1_3=0;} //左边两个电机向前走
- #define Left_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;} //左边两个电机向后转
- #define Left_moto_Stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;} //左边两个电机停转
- #define Right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //右边两个电机向前走
- #define Right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //右边两个电机向前走
- #define Right_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //右边两个电机停转
- unsigned char pwm_val_left =0;//变量定义
- unsigned char push_val_left =0;// 左电机占空比N/10
- unsigned char pwm_val_right =0;
- unsigned char push_val_right=0;// 右电机占空比N/10
- bit Right_moto_stop=1;
- bit Left_moto_stop =1;
- unsigned int time=0;
- //前速前进
- void run(void)
- {
- push_val_left=7; //速度调节变量 0-9。。。9最小,0最大
- push_val_right=7;
- Left_moto_go ; //左电机往前走
- Right_moto_go ; //右电机往前走
- }
- //前速后退
- void backrun(void)
- {
- push_val_left=3; //速度调节变量 0-9。。。0最小,1最大
- push_val_right=3;
- Left_moto_back ; //左电机往前走
- Right_moto_back ; //右电机往前走
- }
- //左转
- void leftrun(void)
- {
-
- Left_moto_back ; //左电机往前走
- Right_moto_go ; //右电机往前走
- }
- //右转
- void rightrun(void)
- {
-
- Left_moto_go ; //左电机往前走
- Right_moto_back ; //右电机往前走
- }
- /************************************************************************/
- /* PWM调制电机转速 */
- /************************************************************************/
- /* 左电机调速 */
- /*调节push_val_left的值改变电机转速,占空比 */
- void pwm_out_left_moto(void)
- {
- if(Left_moto_stop)
- {
- if(pwm_val_left<=push_val_left)
- {
- Left_moto_pwm=1;
- Left_moto_pwm1=1;
- }
- else
- {
- Left_moto_pwm=0;
- Left_moto_pwm1=0;
- }
- if(pwm_val_left>=10)
- pwm_val_left=0;
- }
- else
- {
- Left_moto_pwm=0;
- Left_moto_pwm1=0;
- }
- }
- /******************************************************************/
- /* 右电机调速 */
- void pwm_out_right_moto(void)
- {
- if(Right_moto_stop)
- {
- if(pwm_val_right<=push_val_right)
- {
- Right_moto_pwm=1;
- Right_moto_pwm1=1;
- }
- else
- {
- Right_moto_pwm=0;
- Right_moto_pwm1=0;
- }
- if(pwm_val_right>=10)
- pwm_val_right=0;
- }
- else
- {
- Right_moto_pwm=0;
- Right_moto_pwm1=0;
- }
- }
- /***************************************************/
- ///*TIMER0中断服务子函数产生PWM信号*/
- void timer0()interrupt 1 using 2
- {
- TH0=0XFc; //1Ms定时
- TL0=0X18;
- time++;
- pwm_val_left++;
- pwm_val_right++;
- pwm_out_left_moto();
- pwm_out_right_moto();
- }
- //主函数
- void main(void)
- {
- unsigned char i;
- //P1=0X00; //关电车电机
- TMOD|=0x01;
- TH0= 0xFc; //1ms定时
- TL0= 0x18;
- TR0= 1;
- ET0= 1;
- EA = 1; //开总中断
- while(1) //无限循环
- {
- //有信号为0 没有信号为1
- if(Left_1_led==1&&Right_1_led==1)
- run(); //调用前进函数
- else
- {
- if(Left_1_led==1&&Right_1_led==0) //右边检测到红外信号
- {
- leftrun(); //调用小车左转函数
- }
-
- if(Right_1_led==1&&Left_1_led==0) //左边检测到红外信号
- {
- rightrun(); //调用小车右转函数
-
- }
- }
- }
- }
复制代码 |