我給你來個程序試試
- #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(); //調用小車右轉函數
-
- }
- }
- }
- }
復制代碼 |