正如标题所说,如果中断持续 1 微秒,STM32 是否应该将 CCR 补偿 1 微秒以转换为计数器周期以实现正确的占空比?
例如,周期为 100Khz,溢出中断为 1 微秒,我应该使用 CCR = 50Khz 来实现 50% 的占空比,还是应该使用 CCR = 50kHz - 中断延迟?请记住,我正在使用影子寄存器动态预加载下一个周期/占空比组合。
示波器读数告诉我第二个。
是否存在自动补偿标志?
下面是一些代码片段
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;
}