小車采用雙電機(jī)驅(qū)動(dòng),雙路紅外循跡,pwm調(diào)節(jié)占空比以此調(diào)速,輸于入門級(jí)別程序,只涉及i/o口和pwm知識(shí)相關(guān)知識(shí),可以作為入門練手提升自信。
單片機(jī)源程序如下:
- // finish 2020/11/3 GaoYuan
- #include <msp430f5529.h>
- int pwm_l = 20; //給pwm附一個(gè)初始值
- int pwm_r = 20;
- void Keyscan();
- int begin=20;
- //pwm判斷控制
- void detect();
- int main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // 關(guān)閉看門狗
- //初始化
- //motor 正轉(zhuǎn)
- P1DIR |=BIT4+BIT5;//左電機(jī)
- P2DIR |=BIT4+BIT5;//右邊電機(jī)
- P1OUT |=BIT4;
- P1OUT &=~BIT5;
- P2OUT |=BIT4;
- P2OUT &=~BIT5;
- P1DIR |=BIT0;//led
- P4DIR |=BIT7;
- P1DIR |=BIT2+BIT3;//pwm
- P1SEL |= BIT2+BIT3;
- P2DIR &=~(BIT2+BIT3) ; //left,right detection
- P2OUT |= BIT2+BIT3;
- P2REN |=BIT2+BIT3;
- TA0CCR0 = 124; //PWM周期定義
- TA0CTL = TASSEL_1 + MC_1 + TACLR; //參考時(shí)鐘選擇ACLK,增計(jì)數(shù)模式,清除TAR計(jì)數(shù)器
- while(1)
- {
- TA0CCTL1=OUTMOD_7;
- TA0CCTL2=OUTMOD_7;
- detect();
- TA0CCR1=pwm_l;
- TA0CCR2=pwm_r;
- __delay_cycles(30000);
- pwm_r=begin;
- pwm_l=begin;
- }
- }
- void detect()
- {
- {
- if( (P2IN & BIT2) && ( (P2IN & BIT3)==0) )//右偏
- {
- P4OUT |=BIT7;
- __delay_cycles(10000);
- P4OUT &=~BIT7;//led down 4.7
- pwm_l=pwm_l+20;
- }
- } if( ((P2IN & BIT2) ==0) && (P2IN & BIT3) )//左偏
- {
- P1OUT |=BIT0;
- __delay_cycles(10000);
- P1OUT &=~BIT0;//led down 1.0
- pwm_r=pwm_r+20;
- }
- }
復(fù)制代碼
car_control.zip
(78.17 KB, 下載次數(shù): 117)
2020-11-18 20:10 上傳
點(diǎn)擊文件名下載附件
|