STM32
直播中

江根磊

7年用户 1050经验值
私信 关注
[问答]

为什么STM32F103ZET定时器TIM1无法输出6路PWM呢

为什么STM32F103ZET定时器tiM1无法输出6路PWM呢?怎样去解决这个问题呢?

回帖(1)

王桂兰

2021-12-14 11:26:06
使用默认引脚输出6路PWM,#define FullRemap_TIM1则重映射 PIN脚;
遗留问题:调用TIM_CCxNCmd(TIM1, TIM_Channel_X, TIM_CCx_Enable);导致互补PWM无输出。
TIM_CCxCmd(TIM1, TIM_Channel_X, TIM_CCx);确可以正常开关PWM输出;
遇到问题:PA8、10、PB13、14、15均有输出,PA9无输出;
问题解决:虽然没初始化UART1模块,但打开RCC_APB2Periph_USART1时钟导致;

void Timer1_Init(void)
{
    TIM_OCInitTypeDef TIM_OCInitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
     
        NVIC_InitTypeDef NVIC_InitStructure;
     
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

#ifdef FullRemap_TIM1
    GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
#endif
       
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
#ifdef FullRemap_TIM1
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9
    |GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
#endif
    TIM_TimeBaseStructure.TIM_Period    = 5999;//ARR 10K
    TIM_TimeBaseStructure.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Prescaler = 99;//??????1us
    TIM_TimeBaseStructure.TIM_ClockDivision     = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
     
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState  = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 1000;//CCR
    TIM_OCInitStructure.TIM_OCPolarity   = TIM_OCPolarity_Low;
    TIM_OCInitStructure.TIM_OCNPolarity  = TIM_OCNPolarity_Low; //关断后PWM默认输出状态
    TIM_OCInitStructure.TIM_OCIdleState  = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);

        TIM_OCInitStructure.TIM_Pulse = 2000;//CCR
        TIM_OC2Init(TIM1, &TIM_OCInitStructure);

    TIM_OCInitStructure.TIM_Pulse = 3000;//CCR
        TIM_OC3Init(TIM1, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
        TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
        TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);

    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
    TIM_BDTRInitStructure.TIM_DeadTime = 0;
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
        TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

        NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

    TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);
    TIM_ARRPreloadConfig(TIM1, ENABLE);
    TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
        TIM_CtrlPWMOutputs(TIM1, ENABLE);
    TIM_Cmd(TIM1, ENABLE);

        //TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable);
        //TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable);
        //TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable);
        //TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable);
        //TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable);
        //TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable);
}
void TIM1_UP_IRQHandler(void)                                                        //
{       
        if(TIM_GetITStatus(TIM1,TIM_IT_Update)!=RESET)
        {
                TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
        }
}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分