【无标题】

/**************************************************************************
函数功能:motor相关引脚初始化
入口参数:L298n驱动需要的引脚初始化,具体使用哪些引脚根据各自的情况而定
开关引脚PB12~PB15
两路PWM PA6、PA7 TIMER3(0,1)
返回  值:无
**************************************************************************/
void Motor_Init(uint16_t arrs,uint16_t pscs,uint16_t arrp,uint16_t pscp)
{
    /* enable the LEDs GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOB);

    /* configure LED GPIO port */
    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_14);
    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
    /* reset LED GPIO pin */
    gpio_bit_reset(GPIOB, GPIO_PIN_12);
    gpio_bit_reset(GPIOB, GPIO_PIN_13);
    gpio_bit_reset(GPIOB, GPIO_PIN_14);
    gpio_bit_reset(GPIOB, GPIO_PIN_15);

    Servo_Init( arrs, pscs);
    PWM_Init( arrp, pscp);

}
/**************************************************************************
函数功能:控制电舵机PWM初始化TIMER4(0)PA3
入口参数:无
返回  值:无
**************************************************************************/
void Servo_Init(uint16_t arr,uint16_t psc)
{

    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;


    /* enable the LEDs GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);

    /* configure LED GPIO port */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
    

    rcu_periph_clock_enable(RCU_TIMER4);

    timer_deinit(TIMER4);

    /* TIMER4 configuration */
    timer_initpara.prescaler         = arr;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = psc;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER4,&timer_initpara);

    /* CH0/CH1 configuration in PWM mode0 */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER4,TIMER_CH_3,&timer_ocintpara);
    timer_channel_output_pulse_value_config(TIMER4,TIMER_CH_3,SERVO_INIT);
    timer_channel_output_mode_config(TIMER4,TIMER_CH_3,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER4,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER4);
    timer_enable(TIMER4);


}
/**************************************************************************
函数功能:控制电机的两路PWM初始化TIMER3(0,3)
入口参数:无
返回  值:无
**************************************************************************/
void PWM_Init(uint16_t arr,uint16_t psc)
{
    /* TIMER3 configuration: generate PWM signals with different duty cycles:
       TIMER3CLK = SystemCoreClock / 120 = 1MHz */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;


    /* enable the LEDs GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOB);

    /* configure LED GPIO port */
    gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);

    rcu_periph_clock_enable(RCU_TIMER3);

    timer_deinit(TIMER3);

    /* TIMER3 configuration */
    timer_initpara.prescaler         = arr;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = psc;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER3,&timer_initpara);

    /* CH0/CH1 configuration in PWM mode0 */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER3,TIMER_CH_0,&timer_ocintpara);
    timer_channel_output_config(TIMER3,TIMER_CH_1,&timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_0,0);
    timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_1,0);
    timer_channel_output_mode_config(TIMER3,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_mode_config(TIMER3,TIMER_CH_1,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
    timer_channel_output_shadow_config(TIMER3,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER3);
    timer_enable(TIMER3);

}

你可能感兴趣的:(单片机,stm32,嵌入式硬件)