|
我給你來(lái)個(gè)程序試試
- #include <AT89X52.H> //包含51單片機(jī)頭文件,內(nèi)部有各種寄存器定義
- #define Left_1_led P3_4 //P3_4接四路尋跡模塊接口第一路輸出信號(hào)即中控板上面標(biāo)記為OUT1
- #define Left_2_led P3_5 //P3_5接四路尋跡模塊接口第二路輸出信號(hào)即中控板上面標(biāo)記為OUT2
- #define Right_1_led P3_6 //P3_6接四路尋跡模塊接口第三路輸出信號(hào)即中控板上面標(biāo)記為OUT3
- #define Right_2_led P3_7 //P3_7接四路尋跡模塊接口第四路輸出信號(hào)即中控板上面標(biāo)記為OUT4
- #define Left_moto_pwm P1_1 //PWM信號(hào)端
- #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;} //左邊兩個(gè)電機(jī)向前走
- #define Left_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;} //左邊兩個(gè)電機(jī)向后轉(zhuǎn)
- #define Left_moto_Stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;} //左邊兩個(gè)電機(jī)停轉(zhuǎn)
- #define Right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //右邊兩個(gè)電機(jī)向前走
- #define Right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //右邊兩個(gè)電機(jī)向前走
- #define Right_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //右邊兩個(gè)電機(jī)停轉(zhuǎn)
- unsigned char pwm_val_left =0;//變量定義
- unsigned char push_val_left =0;// 左電機(jī)占空比N/10
- unsigned char pwm_val_right =0;
- unsigned char push_val_right=0;// 右電機(jī)占空比N/10
- bit Right_moto_stop=1;
- bit Left_moto_stop =1;
- unsigned int time=0;
- //前速前進(jìn)
- void run(void)
- {
- push_val_left=7; //速度調(diào)節(jié)變量 0-9。。。9最小,0最大
- push_val_right=7;
- Left_moto_go ; //左電機(jī)往前走
- Right_moto_go ; //右電機(jī)往前走
- }
- //前速后退
- void backrun(void)
- {
- push_val_left=3; //速度調(diào)節(jié)變量 0-9。。。0最小,1最大
- push_val_right=3;
- Left_moto_back ; //左電機(jī)往前走
- Right_moto_back ; //右電機(jī)往前走
- }
- //左轉(zhuǎn)
- void leftrun(void)
- {
-
- Left_moto_back ; //左電機(jī)往前走
- Right_moto_go ; //右電機(jī)往前走
- }
- //右轉(zhuǎn)
- void rightrun(void)
- {
-
- Left_moto_go ; //左電機(jī)往前走
- Right_moto_back ; //右電機(jī)往前走
- }
- /************************************************************************/
- /* PWM調(diào)制電機(jī)轉(zhuǎn)速 */
- /************************************************************************/
- /* 左電機(jī)調(diào)速 */
- /*調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比 */
- 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;
- }
- }
- /******************************************************************/
- /* 右電機(jī)調(diào)速 */
- 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中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
- void timer0()interrupt 1 using 2
- {
- TH0=0XFc; //1Ms定時(shí)
- TL0=0X18;
- time++;
- pwm_val_left++;
- pwm_val_right++;
- pwm_out_left_moto();
- pwm_out_right_moto();
- }
- //主函數(shù)
- void main(void)
- {
- unsigned char i;
- //P1=0X00; //關(guān)電車電機(jī)
- TMOD|=0x01;
- TH0= 0xFc; //1ms定時(shí)
- TL0= 0x18;
- TR0= 1;
- ET0= 1;
- EA = 1; //開(kāi)總中斷
- while(1) //無(wú)限循環(huán)
- {
- //有信號(hào)為0 沒(méi)有信號(hào)為1
- if(Left_1_led==1&&Right_1_led==1)
- run(); //調(diào)用前進(jìn)函數(shù)
- else
- {
- if(Left_1_led==1&&Right_1_led==0) //右邊檢測(cè)到紅外信號(hào)
- {
- leftrun(); //調(diào)用小車左轉(zhuǎn)函數(shù)
- }
-
- if(Right_1_led==1&&Left_1_led==0) //左邊檢測(cè)到紅外信號(hào)
- {
- rightrun(); //調(diào)用小車右轉(zhuǎn)函數(shù)
-
- }
- }
- }
- }
復(fù)制代碼 |
|