|
以下是我参照论坛内大佬的代码写的单片机代码,在仿真上出现了以下错误
1.在没有信号时,其不能完全停止。motor仍显示在转动,但较为缓慢。(void stop)不知道是仿真的问题还是程序的问题。(全部给的反转,速度全部为0)
2.我写的六个运动方式,分别为:停止(stop),直走zhizou)、左大转(dazhuan1)、右大转(dazhuan2)、左微调(weitiao1)、右微调(weitiao2)
现在的问题是除左大转外,其余的运转方式都成功,左大转的左电机管脚出出现闪烁,疑似信号冲突,但我没有找到冲突的点在哪?
3.我尝试过将左大转的正反转函数将0换成1,结果其运行正常,求解
请求各位大佬的帮助,搞了几天,真的看不到冲突的点在哪里.(还有,模拟红外传感器的按钮松开表示有信号1,按下默认为0)
程序写的很乱,但是希望各位大佬能耐心看完,指出问题所在。急需。
在此,感激不尽。祝生活愉快,万事如意。- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- extern void Motor_Left_Q(bit ReverOrCoro, unsigned char DutyCycle); //????????
- extern void Motor_Left_H(bit ReverOrCoro, unsigned char DutyCycle);
- extern void Motor_Right_Q(bit ReverOrCoro, unsigned char DutyCycle); //????????
- extern void Motor_Right_H(bit ReverOrCoro, unsigned char DutyCycle);
- extern void stop(void);
- void Timer0Config(); // ?????0
- void zhizou();
- void dazhuan1();
- void dazhuan2();
- void weitiao1();
- void weitiao2();
- sbit ENA1 = P1^5; //???????
- sbit ENB1= P1^6;
- sbit ENA2 = P2^5; //???????
- sbit ENB2= P2^6;
- sbit led = P2^4;//???
- uchar a;
- uchar Infrared; //??P0????? ?????
- void scan(void);
- sbit inputL1=P0^0; //?1
- sbit inputL2=P0^1; //?2
- sbit inputR1=P0^2; //?2
- sbit inputR2=P0^3; //?1
- void main()
- {
- Timer0Config();
- ENA1 = 1;
- ENB1 = 1;
- ENA2 = 1;
- ENB2 = 1;
- while(1)
- {
- scan();
- }
- }
- void scan()
- {
- if(inputL1==0&&inputR1==0&&inputL2==0&&inputR2==0) //0000
- {
- stop();
- }
- if(inputL1==1&&inputL2==0&&inputR1==0&&inputR2==0) //1000
- {
- dazhuan1();
- }
- if(inputL1==0&&inputL2==1&&inputR1==0&&inputR2==0) //0100
- {
- weitiao1();
- }
- if(inputL1==0&&inputL2==0&&inputR1==1&&inputR2==0) //0010
- {
- weitiao2();
- }
- if(inputL1==0&&inputL2==0&&inputR1==0&&inputR2==1) //0001
- {
- dazhuan2() ;
- }
- else
- {
- zhizou();
- }
- }
- void zhizou()
- {
- Infrared = inputL1==1&&inputL2==1&&inputR1==1&&inputR2==1;
- Motor_Left_Q(1, 100), Motor_Right_Q(1, 100);
- Motor_Left_H(1, 100), Motor_Right_H(1, 100);
-
- }
- void dazhuan1() //左大转/ 1000
- {
- Motor_Left_Q(0, 200); Motor_Right_Q(1, 200);
- Motor_Left_H(0, 200); Motor_Right_H(1, 200);
-
- }
- void dazhuan2() //右大转/ 0001
- {
-
- Motor_Left_Q(1, 200); Motor_Right_Q(0, 200);
- Motor_Left_H(1, 200); Motor_Right_H(0, 200);
- }
- void weitiao1() //左微调/ 0100
- {
-
- Motor_Left_Q(1, 10); Motor_Right_Q(1, 100);
- Motor_Left_H(1, 10); Motor_Right_H(1, 100);
- Infrared = inputL1==0&&inputL2==1&&inputR1==0&&inputR2==0;
-
- }
- void weitiao2()//右微调/ 0010
- {
- Motor_Left_Q(1, 100); Motor_Right_Q(1, 10);
- Motor_Left_H(1, 100); Motor_Right_H(1, 10);//60
- Infrared = inputL1==0&&inputL2==0&&inputR1==1&&inputR2==0;
- }
- void stop()
- {
- Motor_Left_Q(1, 0), Motor_Right_Q(1, 0);
- Motor_Left_H(1, 0), Motor_Right_H(1, 0);
- }
- void Timer0Config()
- {
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TH0 = 0xFF;
- TL0 = 0x7E;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
复制代码 |
|