2011-08-20 01:21 AM
Hi
I wrote a delay routine using timer6. R2 folds the number of milliseconds to delay. It works fine , however only from the second time on. the first time there is no delay , the program just 'flies' through the code without a delay. This is my routine (in assembler) delayms ;delay r2 milliseconds stmfd r13!,{r0,r1,r14} ldr r0,=TIM6_CNT ;start counting from 0000 mov r1,#0x0000 str r1,[r0] ldr r0,=TIM6_PSC ;prescaler = 59904 ; gives 1ms/count ldr r1,=59904 str r1,[r0] ldr r0,=TIM6_ARR ;r2 = # milliseconds str r2,[r0] ldr r0,=TIM6_CR1 ;enable counter,no reload, single puls mode mov r1,#0x000d str r1,[r0] delayl ldr r0,=TIM6_SR ;wait for UIF ldr r1,[r0] cmp r1,#0x0000 beq delayl ldr r0,=TIM6_SR ;clear UIF flag mov r1,#0x0000 str r1,[r0] ldr r0,=TIM6_CR1 ;stop the timer mov r1,#0x0000 str r1,[r0] ldmfd r13!,{r0,r1,pc} Does anyone know what the reason could be for this behaviour ? Any help is welcome ! Ronny Suy2011-08-20 07:36 AM
Without putting it on the bench, I'd be concerned with the status register test and making it more specific, then perhaps look at the loading/reloading of registers.
What frequency are you running the timer at? It I were just spinning in a loop like this, I'd probably just look at using a free running timer, or cycle counter, and waiting for a timeout delta to expire. The core has a 32-bit cycle counter that should permit times of about a minute to be handled, down to tens of nanoseconds.