|
以下是我參照論壇內大佬的代碼寫的單片機代碼,在仿真上出現了以下錯誤
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;
- }
復制代碼 |
-
-
motor,test.zip
2024-4-10 13:36 上傳
點擊文件名下載附件
25.55 KB, 下載次數: 4
仿真
-
-
程序測試.zip
2024-4-11 12:46 上傳
點擊文件名下載附件
106.07 KB, 下載次數: 4
其中的個人嘗試為原文件
|