简体   繁体   English

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

[英]Stm32 Timer Counter With Registers

I try to use timer1 on STM32f103c8t6 (bluepill).我尝试在 STM32f103c8t6(bluepill)上使用 timer1。 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.我在 HAL 库中使用定时器并计算时序计算、周期和预分频器值等。

(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.我的时钟速度是 72Mhz,我将预分频器调整为 1000。我想将周期值设置为 72000,我将有 1 秒计时器。 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

} }

First, be aware that TMR clocks may be different from your system clock.首先,请注意 TMR 时钟可能与您的系统时钟不同。 So, be sure that you adjusted APB2 clock correctly.因此,请确保您正确调整了 APB2 时钟。

Assuming that your APB2 clock is also 72 MHz, for 1 second period, you need to divide it by 72000000 (72e6) somehow.假设您的 APB2 时钟也是 72 MHz,对于 1 秒周期,您需要以某种方式将其除以 72000000 (72e6)。 You need use ARR & PSC registers such that (ARR + 1) * (PSC + 1) = 72e6 .您需要使用 ARR 和 PSC 寄存器,这样(ARR + 1) * (PSC + 1) = 72e6 Keep in mind that these registers are 16-bit, so they can't be greater than 65535.请记住,这些寄存器是 16 位的,因此它们不能大于 65535。

One possible combination is:一种可能的组合是:

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

Please note that I didn't check your code / TMR settings.请注意,我没有检查您的代码/TMR 设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM