0
votes

I try to use timer1 on STM32f103c8t6 (bluepill). I was read the datasheet and internet response/questions. However I can't understand "how can I adjust period value with registers". I was using timers with HAL library and calculate timing calculations, period and prescaler values etc.

(period)*(prescaler) / (clock Speed) = second

This is my already know formula.

My Clock Speed is 72Mhz and I was adjust Prescaler to 1000. I want to Period value to 72000 and I will have 1 second timer. But I don't know how can ı adjust period value, where is it.

 void TIM1_Config(){
 RCC-> APB2ENR  |= 0x00000400;      //TIM1 CLK Enable
 TIM1-> CR1     |= 0x0083;          //Auto Reload,Update Request Source, Counter Enable
 TIM1-> DIER    |= 0x0003;          //CC1 Interrupt Enable, Update Interrupt Enable
 TIM1-> ARR      = 0x0064;          //100 is set as Auto Reload Value
 TIM1-> PSC     |= 0x03E8;          //1000 is set as Prescaler Value
 TIM1-> (period value I need it)???? // it will set 72000

}

1
Magic numbers == troubles. Use supplied human readable definitions. There is nothing like "period value" in the stm32 timers - 0___________
I agree. Also, you don't need to use hex literals. If you need to set a register to 1000, just write 1000, not 0x3E8. - Tagli

1 Answers

0
votes

First, be aware that TMR clocks may be different from your system clock. So, be sure that you adjusted APB2 clock correctly.

Assuming that your APB2 clock is also 72 MHz, for 1 second period, you need to divide it by 72000000 (72e6) somehow. You need use ARR & PSC registers such that (ARR + 1) * (PSC + 1) = 72e6. Keep in mind that these registers are 16-bit, so they can't be greater than 65535.

One possible combination is:

TIM1->PSC = 1124;
TIM1->ARR = 63999;

Please note that I didn't check your code / TMR settings.