Dear ARM-Community,
I need help on my timer implementation. I want to let the LED flash with 1Hz on my STM32F0-Nucleo with an 48MHz internal clock.
My C code is working perfectly:
... #define LD3 (0x03) int main() { //setup LED RCC->AHBENR |= RCC_AHBENR_GPIOBEN; GPIOB->MODER &= ~(0x3 << (LD3*2)); GPIOB->MODER |= (0x1 << (LD3*2)); GPIOB->OTYPER &= ~(1 << LD3); RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; TIM2->CR1 &= ~(TIM_CR1_CEN); TIM2->PSC = 47999; TIM2->ARR = 499; TIM2->CR1 |= TIM_CR1_CEN; TIM2->EGR |= TIM_EGR_UG; while(1) { if (TIM2->SR & TIM_SR_UIF) { GPIOB->ODR ^= (1UL << LD3); TIM2->SR &= ~(TIM_SR_UIF); } } }
Here is my assembler code, which does not work 100%:
... @enable TIM2 ldr r0, =APB1ENR movs r1, #0x1 str r1, [r0] @reset TIM2 clk ldr r0, =TIM2_CR1 orrs r1, r1 str r1, [r0] @set output cmp mode ldr r0, =TIM2_CCMR1 movs r1, #0x7 lsls r1, r1, #0x4 str r1, [r0] @set prescaler ldr r0, =TIM2_PSC eors r1, r1 ldr r1, =#47999 @ldr r1, =#0xbb7f str r1, [r0] @set auto reload value ldr r0, =TIM2_ARR eors r1, r1 ldr r1, =#1000 @500ms - 1ms = 0x1f3 str r1, [r0] @set TIM2 clk ldr r0, =TIM2_CR1 movs r1, #0x1 str r1, [r0] @setup vectors ldr r0, =TIM2_SR ldr r1, =GPIOB_ODR movs r2, #0x1 movs r3, #0x8 @trigger update event ldr r6, =TIM2_EGR movs r7, #0x1 str r7, [r0] @while loop w0: ldr r4, [r0] tst r4, r2 beq w0 ldr r5, [r1] eors r5, r5, r3 str r5, [r1] @reset SR eors r5, r5 str r5, [r0] b w0
I tested the LED already with a simple loop, that worked. All the vectors are also set correctly! But now, even though I set the right values, the delay is not working correctly.
I guess it has strangely to do something with the ARR, because I can change the f with it.
I have no clue what is wrong here. Can somebody help me?
Thank you!
Why is this posted in "Infrastructure Solutions" ?
Carl Benz said:My C code is working perfectly
So step through your 'C' code, and step through your assembler - compare & contrast ...