Question
Delay function with timer
Posted on February 22, 2013 at 09:12
Hi,
I am trying to make a simple delay function using a timer but i get weird results .Here is some of the code void TIMERS_init(){ TIM3->PSC = 23999; // f timer = fclk / 24000 => 1kHz TIM3->ARR = 0xFFFF; TIM3->CR1 = TIM_CR1_CEN; DBGMCU->CR = DBGMCU_CR_DBG_TIM3_STOP;}void Delay_ms(uint16_t ms){ TIM3->CNT = 0; while (TIM3->CNT < ms){}}while(1){
GPIOC->ODR ^= GPIO_ODR_ODR8; Delay_ms(500); //delay 0.5 sec GPIOC->ODR ^= GPIO_ODR_ODR8; Delay_ms(500); //delay 0.5 sec}The while in main is to test that the blue LED blinks at 2 Hz .I am using keil 4 . In debug if i run step by step works fine , but in continuously run the LED is half bright (probably is on and off at a high frequency) . I can't understand why because the prescalar remains set .