Timer 2 Nucleo L476RG
I am using the above Timer as both a debounce of 50ms and as a PWM for the on board LED. Basically I hit the user button, debounce for 50ms. While button held PWM to the LED of 1/2 sec on, 1/2 sec off.....Release button and PWM stops.
The timer is running at 4MHz with a prescaler of 4000 and a period of 1000. The pulse is set to 500.
When I push the button the first time all seems to work, I release and all is good. When I push the button the second time and set a breakpoint at the following line I notice two things....
(htim2.Instance->CNT) += 500;1st the counter doesn't seem to update by 500 when I step over this line....
2nd the count value is upwards of 80000 sometimes.....Why is this when the period is 1000.
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
(htim2.Instance->CNT) += 500;
HAL_TIM_Base_Start_IT(&htim2);
htim2.Instance->SR &= ~TIM_SR_UIF; //Start creates a flag glitch! This is needed.
if (GPIO_Pin == BUTTON_Pin)
EXTI->PR1 |= 1 << BUTTON_Pin;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim == &htim2)
{
HAL_TIM_Base_Stop_IT(&htim2);
htim2.Instance->SR &= ~TIM_SR_UIF;
if (HAL_GPIO_ReadPin(BUTTON_GPIO_Port, BUTTON_Pin) == GPIO_PIN_RESET)
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
else
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
}
}