带寄存器的 Stm32 定时器计数器

Stm32 Timer Counter With Registers

我尝试在 STM32f103c8t6 (bluepill) 上使用 timer1。我阅读了数据表和互联网 response/questions。但是我无法理解"how can I adjust period value with registers"。我在 HAL 库中使用定时器并计算时序计算、周期和预分频器值等。

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

这是我已经知道的公式。

我的时钟速度是 72Mhz,我将预分频器调整为 1000。我想将周期值设置为 72000,我将有 1 秒计时器。但是我不知道怎么调整period值,它在哪呢

 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

}

首先,请注意 TMR 时钟可能与您的系统时钟不同。所以,请确保您正确调整了 APB2 时钟。

假设您的 APB2 时钟也是 72 MHz,对于 1 秒周期,您需要以某种方式将其除以 72000000 (72e6)。您需要使用 ARR 和 PSC 寄存器,以便 (ARR + 1) * (PSC + 1) = 72e6。请记住,这些寄存器是 16 位的,因此它们不能大于 65535。

一种可能的组合是:

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

请注意,我没有检查您的代码/TMR 设置。