STM32F407 using advanced timer TIM1 in output compare
I have been unsuccessful in using TIM1 in Output Compare mode. I was trying to use the code that works for the general purpose timers but I cannot get it to work with the advanced timer TIM1. I read online that it is because it was an advanced timer and I had to set the TIMx_BDTR.MOE bit. I have tried this but am still not seeing the signal on my oscilloscope although I can see it for the general purpose timers expected.
void TIMER1_Init()
{
TIM_OC_InitTypeDef tim1OC_init;
GPIO_InitTypeDef gpio_init;
// Enable the TIM1 clock
__HAL_RCC_TIM1_CLK_ENABLE();
// Enable the GPIO clock for TIM1 channel pin (e.g., PA8 for TIM1_CH1)
__HAL_RCC_GPIOA_CLK_ENABLE();
// Configure GPIO pin for TIM1_CH1
gpio_init.Pin = GPIO_PIN_8;
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Pull = GPIO_NOPULL;
gpio_init.Speed = GPIO_SPEED_FREQ_LOW;
gpio_init.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA, &gpio_init);
// Enable the Main Output (MOE) for advanced timers
TIM1->BDTR |= TIM_BDTR_MOE;
// Initialize TIM1 base
htimer1.Instance = TIM1;
htimer1.Init.Prescaler = 1;
htimer1.Init.Period = 0xFFFF;
if (HAL_TIM_OC_Init(&htimer1) != HAL_OK)
{
Error_handler();
}
// Configure Output Compare mode for TIM1_CH1
tim1OC_init.OCMode = TIM_OCMODE_TOGGLE;
tim1OC_init.OCPolarity = TIM_OCPOLARITY_HIGH;
tim1OC_init.Pulse = pulse[4];
if (HAL_TIM_OC_ConfigChannel(&htimer1, &tim1OC_init, TIM_CHANNEL_1) != HAL_OK)
{
Error_handler();
}
}
Note: I am using function HAL_TIM_OC_Start_IT(&htimer1, TIM_CHANNEL_1) != HAL_OK
