As the title says, with an STM32 if an interrupt lasts for 1 microsecond should one compensate the CCR by 1 microsecond converted in counter cycles in order to achieve the correct duty cycle?
For example with a period of 100Khz and an overflow interrupt of 1 microsecond should i use a CCR = 50Khz to achieve duty cycle of 50% or should i use a CCR = 50kHz - interrupt delay? Keep in mind that i'm using shadow register to preload the next period/duty cycle combination dynamically.
Oscilloscope readings tells me the second.
Does a flag for automatic compensation exist?
Below some code snippets
void MX_TIM9_Init(void){
TIM_OC_InitTypeDef sConfigOC;
htim9.Instance = TIM9;
htim9.Init.Prescaler = PSC; // Get clock to <freq> Hz
htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
htim9.Init.Period = 250;
htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&htim9);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 125;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&htim9, &sConfigOC, TIM_CHANNEL_1);
__HAL_TIM_ENABLE_IT(&htim9, TIM_IT_UPDATE);
TIM9->CR1 |= TIM_CR1_ARPE;
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm){
GPIO_InitTypeDef GPIO_InitStruct;
if(htim_pwm->Instance==TIM9){
__TIM9_CLK_ENABLE();
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 1, 1);
HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF3_TIM9;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}
}
void TIM1_BRK_TIM9_IRQHandler(void){
if(__HAL_TIM_GET_FLAG(&htim9, TIM_FLAG_UPDATE) != RESET){
if(__HAL_TIM_GET_IT_SOURCE(&htim9, TIM_IT_UPDATE) !=RESET){
__HAL_TIM_CLEAR_IT(&htim9, TIM_IT_UPDATE);
myFunction(void);
}
}
}
void myFunction(){
//perform logics with if
tim9->ARR = r; // r is a value coming from the logics above
tim9->CCR1 = r / 2;
}