131 1300 0010
二極管
當(dāng)前位置: 首頁>> 元件技術(shù)>>二極管>>
  • 導(dǎo)航欄目
  • 二極管
  • 整流橋
  • MOS管
  • 其他
  • STM32單片機(jī)實(shí)現(xiàn)直流減速電機(jī)控制的程序設(shè)計(jì)
    STM32單片機(jī)實(shí)現(xiàn)直流減速電機(jī)控制的程序設(shè)計(jì)
  • STM32單片機(jī)實(shí)現(xiàn)直流減速電機(jī)控制的程序設(shè)計(jì)
  •   發(fā)布日期: 2019-10-28  瀏覽次數(shù): 1,124

    在直流減速電機(jī)控制中,最常用的方法就是通過PWM來控制直流電機(jī)的轉(zhuǎn)速。在控制小車走直線的過程中,需要兩者的轉(zhuǎn)速一置(如果要走得很直,還需要在短時(shí)間內(nèi)保證兩者的行程大致相當(dāng),這可以用PID算法來控制)。因此,在檢測(cè)到兩者轉(zhuǎn)速不一樣時(shí),需要?jiǎng)討B(tài)調(diào)整其中一個(gè)或兩個(gè)輪子的PWM的點(diǎn)空比(簡(jiǎn)單點(diǎn)的就以一個(gè)輪為基準(zhǔn),調(diào)整另外一個(gè)輪子即可;如果以一個(gè)固定的標(biāo)準(zhǔn)的話,需要調(diào)整兩個(gè)輪子的PWM占空比)。

    STM32單片機(jī)實(shí)現(xiàn)直流減速電機(jī)控制的程序設(shè)計(jì)
     

     

    1 程序第一步:設(shè)置GPIO,略(輸出PWM的管腳用Mode_AF_PP即可)

    2

    3 程序第二步:設(shè)置定時(shí)器,(保證產(chǎn)生兩路PWM即可,我用的是TIM4)

    4

    5 void TIM4_ConfiguraTIon(void)

    6 {

    7 TIM_TImeBaseInitTypeDef TIM_TimeBaseInitStructure;

    8 TIM_OCInitTypeDef TIM_OCInitStructure;

    9

    10 //時(shí)間基初始化

    11 TIM_TimeBaseInitStructure.TIM_Period=144; //18K/144=125Hz,這個(gè)是電機(jī)PWM的頻率

    12 TIM_TimeBaseInitStructure.TIM_Prescaler=4000; //72000000/4000=18K

    13 TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;

    14 TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;

    15 TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x0000;

    16

    17 TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);

    18

    19 //輸出比較模式設(shè)置,用于4路PWM輸出

    20 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //輸出PWM

    21 TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能正向通道

    22 TIM_OCInitStructure.TIM_OutputNState=TIM_OutputState_Disable; //失能反向通道

    23 TIM_OCInitStructure.TIM_Pulse=PWM_L; //左輪DIR的占空比

    24 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //輸出極性為低電平

    25 TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High;//互補(bǔ)輸出極性為高電平

    26 TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;

    27 TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;

    28

    29 TIM_OC1Init(TIM4,&TIM_OCInitStructure); //PWM_L初始化

    30 TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Disable); //改變點(diǎn)空比后,立即產(chǎn)生效應(yīng)

    31

    32 TIM_OCInitStructure.TIM_Pulse=PWM_R; //左輪PWM的占空比

    33 TIM_OC2Init(TIM4,&TIM_OCInitStructure); //PWM_R初始化

    34 TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Disable); //改變點(diǎn)空比后,立即產(chǎn)生效應(yīng)

    35

    36 //使能定時(shí)器4

    37 TIM_Cmd(TIM4,ENABLE);

    38 TIM_CtrlPWMOutputs(TIM4,ENABLE);

    程序第一步:設(shè)置GPIO,略(輸出PWM的管腳用Mode_AF_PP即可)

    程序第二步:設(shè)置定時(shí)器,(保證產(chǎn)生兩路PWM即可,我用的是TIM4)

    void TIM4_Configuration(void)

    {

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

    TIM_OCInitTypeDef TIM_OCInitStructure;

    //時(shí)間基初始化

    TIM_TimeBaseInitStructure.TIM_Period=144; //18K/144=125Hz,這個(gè)是電機(jī)PWM的頻率

    TIM_TimeBaseInitStructure.TIM_Prescaler=4000; //72000000/4000=18K

    TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;

    TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;

    TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0x0000;

    TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);

    //輸出比較模式設(shè)置,用于4路PWM輸出

    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //輸出PWM

    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能正向通道

    TIM_OCInitStructure.TIM_OutputNState=TIM_OutputState_Disable; //失能反向通道

    TIM_OCInitStructure.TIM_Pulse=PWM_L; //左輪DIR的占空比

    TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //輸出極性為低電平

    TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High;//互補(bǔ)輸出極性為高電平

    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;

    TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;

    TIM_OC1Init(TIM4,&TIM_OCInitStructure); //PWM_L初始化

    TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Disable); //改變點(diǎn)空比后,立即產(chǎn)生效應(yīng)

    TIM_OCInitStructure.TIM_Pulse=PWM_R; //左輪PWM的占空比

    TIM_OC2Init(TIM4,&TIM_OCInitStructure); //PWM_R初始化

    TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Disable); //改變點(diǎn)空比后,立即產(chǎn)生效應(yīng)

    //使能定時(shí)器4

    TIM_Cmd(TIM4,ENABLE);

    TIM_CtrlPWMOutputs(TIM4,ENABLE);

    1 程序第三步:

    2

    3 在SysTick中斷中,讀取兩個(gè)輪子的速度(具體的方法是:每0.1秒讀一次,并以此人作為速度的依據(jù)),并比較,如果以右輪為基準(zhǔn),則調(diào)整左輪的PWM占空比。涉及到關(guān)鍵語句是:TIM_SetCompare1();

    4

    5 u16 COUN1=0;

    6 u16 COUN2=0;

    7

    8 volatile u16 Dist_L=0; //左輪行程脈沖數(shù)

    9 volatile u16 Dist_R=0; //右輪行程脈沖數(shù)

    10 void SysTick_Handler(void)

    11 {

    12

    13   COUN1=TIM1-》CNT; //左輪在0.1秒里脈沖數(shù)

    14   COUN2=TIM2-》CNT; //右輪在0.1秒里脈沖數(shù)

    15   Dist_L=Dist_L+COUN1; //左輪行程脈沖數(shù)

    16   Dist_R=Dist_R+COUN2; //右輪行程脈沖數(shù)

    17   if( (COUN1-COUN2)》2)

    18   {

    19    PWM_L= TIM_GetCapture1(TIM4);

    20    TIM_SetCompare1(TIM4, PWM_L - 4);

    21   }

    22 else if ( (COUN2-COUN1)》2)

    23 {

    24 PWM_L= TIM_GetCapture1(TIM4);

    25 TIM_SetCompare1(TIM4, PWM_L + 4);

    26 }

    27

    28 TIM_SetCounter(TIM1, 0);

    29 TIM_SetCounter(TIM2, 0);

    30

    31 }

    32

    33 關(guān)于TIM_SetCompareX(;)這個(gè)函數(shù),還是有很多用途的,其中另外一個(gè)用途,就是用于產(chǎn)生不同頻率的PWM,具體程序如下:

    34

    35 u16 capture = 0;

    36

    37 extern vu16 CCR1_Val;

    38 extern vu16 CCR2_Val;

    39 extern vu16 CCR3_Val;

    40 extern vu16 CCR4_Val;

    41

    42 void TIM2_IRQHandler(void)

    43 {

    44

    45 /* TIM2_CH1 toggling with frequency = 183.1 Hz */

    46 if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)

    47 {

    48 TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );

    49   capture = TIM_GetCapture1(TIM2);

    50   TIM_SetCompare1(TIM2, capture + CCR1_Val );

    51 }

    52

    53 /* TIM2_CH2 toggling with frequency = 366.2 Hz */

    54 if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)

    55 {

    56 TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

    57   capture = TIM_GetCapture2(TIM2);

    58 TIM_SetCompare2(TIM2, capture + CCR2_Val);

    59 }

    60

    61 /* TIM2_CH3 toggling with frequency = 732.4 Hz */

    62 if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)

    63 {

    64 TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

    65   capture = TIM_GetCapture3(TIM2);

    66 TIM_SetCompare3(TIM2, capture + CCR3_Val);

    67 }

    68

    69 /* TIM2_CH4 toggling with frequency = 1464.8 Hz */

    70 if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)

    71 {

    72 TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

    73   capture = TIM_GetCapture4(TIM2);

    74 TIM_SetCompare4(TIM2, capture + CCR4_Val);

    75 }

    76

    77 }
    來源;21ic


  • ·上一篇:
    ·下一篇:
  • 其他關(guān)聯(lián)資訊
    深圳市日月辰科技有限公司
    地址:深圳市寶安區(qū)松崗鎮(zhèn)潭頭第二工業(yè)城A區(qū)27棟3樓
    電話:0755-2955 6626
    傳真:0755-2978 1585
    手機(jī):131 1300 0010
    郵箱:hu@szryc.com

    深圳市日月辰科技有限公司 版權(quán)所有:Copyright?2010-2023 elibeatofitness.com 電話:13113000010 粵ICP備2021111333號(hào)