AnsweredAssumed Answered

STM32F051 TIM2 counting way too fast?

Question asked by Philipp Krause on Feb 20, 2018
Latest reply on Feb 20, 2018 by waclawek.jan

I'm trying to use TIM2 on the STM32F0-Discovery board (I am new to the STM32, but have used the STM8 before).

 

I setup TIM2 and USART1, then in a loop I send the value of TIM2_CNT via the USART1, so I can see how TIM2 counts. Since the USART1 stuff is working at the intended 9600 baud, I am confident that the basic clock setup is there and the STM32F0 is running at 8 Mhz.

 

However, I see TIM2_CNT count at about 50 Mhz, no matter the value of TIM2_PSC.

 

Here is my initialization code:

 

#include <stdint.h>

 

#define TIM2_BASE    0x40000000
#define TIM2_CR1    (*(volatile uint16_t *)(TIM2_BASE + 0x00))
#define TIM2_CNT    (*(volatile uint32_t *)(TIM2_BASE + 0x24))
#define TIM2_PSC    (*(volatile uint16_t *)(TIM2_BASE + 0x28))
#define RCC_BASE    0x40021000
#define RCC_APB2ENR    (*(volatile uint32_t *)(RCC_BASE + 0x18))
#define RCC_APB1ENR    (*(volatile uint32_t *)(RCC_BASE + 0x1c))
#define GPIOA_BASE    0x48000000
#define GPIOA_MODER    (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
#define GPIOA_AFRH    (*(volatile uint32_t *)(GPIOA_BASE + 0x24))
#define USART1_BASE    0x40013800
#define USART1_CR1    (*(volatile uint32_t *)(USART1_BASE + 0x00))
#define USART1_BRR    (*(volatile uint32_t *)(USART1_BASE + 0x0c))
#define USART1_ISR    (*(volatile uint32_t *)(USART1_BASE + 0x1c))
#define USART1_TDR    (*(volatile uint32_t *)(USART1_BASE + 0x28))

 

void init(void)
{
    RCC_AHBENR |= (1ul << 17); // Enable I/O A
    RCC_APB2ENR |= (1ul << 14); // Enable USART1
    RCC_APB1ENR |= (1ul << 0); // Enable Timer 2

 

    // Use PA9, PA10 for USART instead of GPIO.
    GPIOA_MODER |= (0x2ul << 18) | (0x2ul << 20);
    GPIOA_AFRH |= (0x1ul << 4) | (0x1ul << 8);

 

    // 9600 baud, 1 start bit, 8 data bits, 1 stop bit.
    USART1_BRR = 8000000 / 9600;
    USART1_CR1 = 0xd;

 

    // Set timer to 1000 ticks per second?
    TIM2_PSC = 8000;
    TIM2_CR1 = (1ul << 0);
}

 

 

Philipp

Outcomes