3

我正在使用 stm32f101cb 微控制器实现一个项目。我知道这个控件里面有 3 个计时器。TIM2 和 TIM4 已设置为编码器模式以捕获 2 个旋转编码器。

我现在只剩下唯一的定时器 TIM3,我想问我是否可以将 TIM3 设置为 PWM 模式并同时进行正常的定时器中断?我在参考手册或编程手册中找不到它。

谢谢!

4

1 回答 1

2

这应该是可能的。我正在为 STM32F091RC 做这件事我已经开发了一个应用程序,我用 PWM 控制 LED:s 并使用相同的定时器外设生成定时器更新中断(计数器复位)。

这是我的一些配置代码:

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
/* LED_TIM clock enable */
RCC_APB1PeriphClockCmd(TIM_LED_RCC, ENABLE);

/* LED_PORT Configuration: Channel 1, 2, 3 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(LED_PORT, &GPIO_InitStructure);

GPIO_PinAFConfig(LED_PORT, LED_PIN_SOURCE, LED_TIM_AF);

/* Enable the TIM global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = LED_TIM_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = LED_TIM_PSC;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;    // Max 0xFFFF
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

/* Channel 1 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = LED_INIT_PULSE;
TIM_OC1Init(LED_TIM, &TIM_OCInitStructure);  // Init Channel 1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);

/* TIM IT enable */
TIM_ITConfig(LED_TIM, TIM_IT_Update, ENABLE);  // Enable interrupts for LED_TIM

/* LED_TIM counter enable */
TIM_Cmd(LED_TIM, ENABLE);

/* LED_TIM Main Output Enable */
TIM_CtrlPWMOutputs(LED_TIM, ENABLE);

您的设备应该非常相似。如果一切正常,您应该在 TIM3_IRQHandler ISR 中获得中断

void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(LED_TIM, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(LED_TIM, TIM_IT_Update);

    // Do ISR stuff here!

  }
}
于 2015-08-13T11:10:10.187 回答