CH32V208 TIM5 到底32位还是16位?

如标题,用TIM2 CH2 输出PWM 正常,替换为 TIM5 则无法工作。还请给予帮助。

void TIM5_PWMOut_Init(u16 arr, u16 psc, u16 ccp) //PA1管脚PWM输出控制发光管电流

{

    GPIO_InitTypeDef GPIO_InitStructure={0};

    TIM_OCInitTypeDef TIM_OCInitStructure={0};

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};


    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );

    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM5, ENABLE );


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init( GPIOA, &GPIO_InitStructure );


    TIM_TimeBaseInitStructure.TIM_Period = arr;//定时器周期

    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;//预分频

    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分频

    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数模式

    TIM_TimeBaseInit( TIM5, &TIM_TimeBaseInitStructure);


    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = ccp;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC2Init( TIM5, &TIM_OCInitStructure );


    TIM_CtrlPWMOutputs(TIM5, ENABLE );

    TIM_OC2PreloadConfig( TIM5, TIM_OCPreload_Disable );

    TIM_ARRPreloadConfig( TIM5, ENABLE );

    TIM_Cmd( TIM5, ENABLE );

}


已找到问题。首先确认TIM5是32位的。

1、TIM_TimeBaseInit()函数中 修改


if((TIMx == TIM5)){

? ? ?TIMx->ATRLR_R32 = (uint32_t)TIM_TimeBaseInitStruct->TIM_Period;

?}else{

? ? TIMx->ATRLR = TIM_TimeBaseInitStruct->TIM_Period;

???}

2、TIM_OC2Init()函数中修改

if((TIMx == TIM5)){

? ? ? TIMx->CH2CVR_R32 = (uint32_t)TIM_OCInitStruct->TIM_Pulse;

???}else{

? ? ?TIMx->CH2CVR = TIM_OCInitStruct->TIM_Pulse;

???}?

可正常输出PWM了。


只有登录才能回复,可以选择微信账号登录