Question
STM32 microsecond delay function with polling method
Hi,
I am using stm32f0. I try to microsecond delay function. But, I observe that TIMx->CNT not increasing.
I probably mistake on configutration function. But I could not solved. In 29 line, UF flag not to be set
Here is my code;
void ledconfig(){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE) ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void configure_us_timer(uint32_t clock_freq, uint32_t us_delay) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
uint32_t prescaler;
uint32_t reload;
prescaler = clock_freq / 1000000 - 1;
reload = (us_delay * clock_freq) / (1000000 * (prescaler + 1)) - 1;
TIM1->CR1 &= ~TIM_CR1_CEN;
TIM1->PSC = prescaler;
TIM1->ARR = reload;
TIM1->CR1 |= TIM_CR1_URS;
TIM1->CR1 |= TIM_CR1_CEN;
}
void delay_us(uint32_t us_delay) {
// Configure timer for desired delay
configure_us_timer(SystemCoreClock, us_delay);
// Wait for update flag to be set and then cleared (indicates one timer period elapsed)
while ((TIM1->SR & TIM_SR_UIF) == 0);
while ((TIM1->SR & TIM_SR_UIF) != 0);
// Disable timer (optional)
TIM1->CR1 &= ~TIM_CR1_CEN;
}
int main(void)
{
ledconfig();
GPIO_SetBits(GPIOC,GPIO_Pin_9) ;
delay_us(1000000); // to abserve that I configure it 1 second
GPIO_ResetBits(GPIOC,GPIO_Pin_9) ;
while (1)
{
}
}
Thanks for helping
