Hi there, I am very new in programming micro.I am using stm32f100 discovery board and made a small code to turn the stm board led on for 5 second once hit the user push button.then the led goes off.The partial code below I have to do the work.The program runs my led on for 5 sec only the first time I hit the user button.But next time I hit user it gives me 4 sec, and keep changing everytime to 3s,4s,,like random.I could not figure out what I am doing wrong.Could anyone please help me on this.Thanks in advance.
TIM3->PSC = 23999; // Set prescaler to 24 000 (PSC + 1) TIM3->ARR = 5000; // Auto reload value 1000 while(1) { if(GPIOA->IDR & (1 << 0)) //if user ush button is set { TIM3->CR1 = TIM_CR1_CEN;//enable timer3 TIM3->SR &= ~TIM_SR_UIF;// clear uif bit while (1) { GPIOC->BSRR = (1 << 8); //until UIF flag is not set led 4(pc8) in stm board blinks if(TIM3->SR & TIM_SR_UIF) // if UIF flag is set { GPIOC->BRR = (1 << 8); //clear the pc8 break; } } } }
Just one quick suggestion. Please set you tab selection to create spaces instead of tab characters and re-post your code. We might be able to read you code a bit better.
Rgds, Bradford
I don't work with your processor and haven't the time to retrieve a datasheet for it. But are you making sure that the actual counter register of that timer is reset every time you start the timer. If the timer is free-running, your delay will depend on where it is in the count sequence when you press your button.
Enabling a timer isn't normally resetting the timer, making it a difference when you enable the timer the first time, and when you enable the timer following times.
Thanks a lot. You are right,the clock was still running.I have reset the clock and it works perfect now. That's great!
@Bradford,sorry about that.Next time I will use tab instead the space bar.Thanks for the suggestion.
No, Bradford didn't talk about use of tab key contra space key. Just that if you use the tab key to indent, you should configure the editor to convert indents into a sequence of spaces. Because source code that contains the tab character will expand differently depending on where you post it, or which editor you open the source code in.