我想初始化TIM1的PWM时做错了。我需要在PA8上收到信号,但是我的代码(对于TIM2-TIM5正常工作)不适用于TIM1。下面的设置是否有误?
首先,我想这是因为我没有调用TIM_OCStructInit(),但是添加之后,我在PA8上仍然处于低电平并且没有信号。它配置为AF_PP,并且我没有重新映射TIM1。
... {.tim = TIM1, .id = RCC_APB2Periph_TIM1,
.channels = {
{.gpio = GPIOA, .gpio_pin = GPIO_Pin_8, },
{.gpio = GPIOA, .gpio_pin = GPIO_Pin_9, },
{.gpio = GPIOA, .gpio_pin = GPIO_Pin_10, },
{.gpio = GPIOA, .gpio_pin = GPIO_Pin_11, }
}
} ...
.....
RCC_APB1PeriphClockCmd(_timers[tim_id].id, ENABLE);
TIM_TimeBaseInitTypeDef timerInitStructure;
TIM_OCInitTypeDef outputChannelInit;
TIM_TimeBaseStructInit(&timerInitStructure);
timerInitStructure.TIM_Prescaler = F_CPU/1000000UL; // set 1us resolution
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = period;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIMx, &timerInitStructure);
TIM_OCStructInit(&outputChannelInit);
outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
outputChannelInit.TIM_Pulse = def_width;
outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
switch((chan & 0x3)){
case 0:
TIM_OC1Init(TIMx, &outputChannelInit);
TIM_OC1PreloadConfig(TIMx, TIM_OCPreload_Enable);
break;
case 1:
TIM_OC2Init(TIMx, &outputChannelInit);
TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);
break;
case 2:
TIM_OC3Init(TIMx, &outputChannelInit);
TIM_OC3PreloadConfig(TIMx, TIM_OCPreload_Enable);
break;
case 3:
TIM_OC4Init(TIMx, &outputChannelInit);
TIM_OC4PreloadConfig(TIMx, TIM_OCPreload_Enable);
break;
};
//TIM_ARRPreloadConfig(TIMx, ENABLE);
TIM_CtrlPWMOutputs(TIMx, ENABLE);
TIM_Cmd(TIMx, ENABLE);
GPIO_InitTypeDef gpioStructure;
gpioStructure.GPIO_Pin = _timers[tim_id].channels[chan & 0x3].gpio_pin;
gpioStructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(_timers[tim_id].channels[chan & 0x3].gpio, &gpioStructure);
最佳答案
得到它了!当我应该对TIM1使用RCC_APB2PeriphClockCmd时,我对所有计时器都使用RCC_APB1PeriphClockCmd。现在一切正常!