如何启用ARM CORTEX M3(STM32F103RB)中定时器的计数器?

发布于 2025-01-18 16:35:48 字数 2054 浏览 3 评论 0原文

我正在尝试编写代码来使用定时器(特别是 TIM2)切换端口(PORTC)的引脚(引脚 15)。我必须使用预分频寄存器来划分时钟频率,以便获得切换引脚所需的延迟。 因此,我在阅读了 STM32F103RB 参考手册 3 小时后编写了一些代码。我检查了我的程序出了什么问题,并注意到即使我启用了计数器启用引脚,计数器寄存器也没有更新。 谁能告诉我哪里出错了?我已附上我的代码以供参考。

#include "stm32f10x.h"

void Q2() {
    int step = 0;
    RCC->APB2ENR |= (1<<4);  //To enable the PORTC
    RCC->APB1ENR |= (1<<0);  // Enable Timer 2
    GPIOC->CRH &= (0<<30);   // setting CNF
    GPIOC->CRH |= (1<<28);   // Setting mode to get general purpose Output push pull
    
    TIM2->PSC = 0xD2F0;    // Prescalar value set such that the 0.5ms is over till overflow
    TIM2->CR1 |= 1<<7;  // Auto reload preload enable is buffered
    TIM2->ARR = 0x0000;  // Auto reload value is 0
    TIM2->CR1 |= 1<<0;  //Counter enabled

    while (1)
    {
        TIM2->PSC = 0xD2F0;    // Prescalar value set such that the 0.5ms is over till overflow 
        TIM2->CR1 |= 1<<7;  // Auto reload preload enable is buffered
        TIM2->ARR = 0x0000;  // Auto reload value is 0
        TIM2->CR1 |= 1<<0;  //Counter enabled
        TIM2->SR &= 0<<0;
        while(step!=2) {
            if((TIM2->SR && 0x0001) == 0x0001) {

                step++; 
            }
        }
        step = 0;
        GPIOC->BSRR |= (1<<15);  // set pin 15 of port C
        TIM2->PSC = 0xD2F0;    // Prescalar value set such that the 0.5ms is over till overflow 
        TIM2->CR1 |= 1<<7;  // Auto reload preload enable is buffered
        TIM2->ARR = 0x0000;  // Auto reload value is 0
        TIM2->CR1 |= 1<<0;  //Counter enabled   
        TIM2->SR &= 0<<0;
        while(step!=2) {
            if((TIM2->SR && 0x0001) == 0x0001) {

                step++; 
            }
        }
        step = 0;
        
        GPIOC->BSRR |= (1<<31);  // reset pin 15 of Port C
    }
        
    }
    int main ()
    {
        SystemInit ();
        Q2();
    }

谢谢

I am trying to write code for toggling a pin(Pin 15) of a port (PORTC) using timers(specifically TIM2). I have to use the prescale registers to divide the clock frequency such that I obtain the required delay for toggling the pin.
So I wrote some code after 3 hrs of going through Reference Manual of the STM32F103RB. I checked what is wrong with my program and noticed that the Counter register is not updating even though I enabled the counter enable pin.
Could anyone tell where I am going wrong? I have attached my code for reference.

#include "stm32f10x.h"

void Q2() {
    int step = 0;
    RCC->APB2ENR |= (1<<4);  //To enable the PORTC
    RCC->APB1ENR |= (1<<0);  // Enable Timer 2
    GPIOC->CRH &= (0<<30);   // setting CNF
    GPIOC->CRH |= (1<<28);   // Setting mode to get general purpose Output push pull
    
    TIM2->PSC = 0xD2F0;    // Prescalar value set such that the 0.5ms is over till overflow
    TIM2->CR1 |= 1<<7;  // Auto reload preload enable is buffered
    TIM2->ARR = 0x0000;  // Auto reload value is 0
    TIM2->CR1 |= 1<<0;  //Counter enabled

    while (1)
    {
        TIM2->PSC = 0xD2F0;    // Prescalar value set such that the 0.5ms is over till overflow 
        TIM2->CR1 |= 1<<7;  // Auto reload preload enable is buffered
        TIM2->ARR = 0x0000;  // Auto reload value is 0
        TIM2->CR1 |= 1<<0;  //Counter enabled
        TIM2->SR &= 0<<0;
        while(step!=2) {
            if((TIM2->SR && 0x0001) == 0x0001) {

                step++; 
            }
        }
        step = 0;
        GPIOC->BSRR |= (1<<15);  // set pin 15 of port C
        TIM2->PSC = 0xD2F0;    // Prescalar value set such that the 0.5ms is over till overflow 
        TIM2->CR1 |= 1<<7;  // Auto reload preload enable is buffered
        TIM2->ARR = 0x0000;  // Auto reload value is 0
        TIM2->CR1 |= 1<<0;  //Counter enabled   
        TIM2->SR &= 0<<0;
        while(step!=2) {
            if((TIM2->SR && 0x0001) == 0x0001) {

                step++; 
            }
        }
        step = 0;
        
        GPIOC->BSRR |= (1<<31);  // reset pin 15 of Port C
    }
        
    }
    int main ()
    {
        SystemInit ();
        Q2();
    }

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2025-01-25 16:35:48

首先,您不应将 TIM2_ARR 寄存器设置为零。该寄存器确定定时器在生成更新或中断事件之前计数器的最大值。因此,如果将其设置为零,计数器将不会开始计数。您应该根据 SytemCoreClock 变量(MCU 的时钟频率)配置 TIM2_PSCTIM2_ARR 寄存器的值,以获得所需的切换延迟GPIO 引脚。

而且您不需要每次切换 GPIO 引脚后都重新配置计时器。如果你想重置定时器并从零开始计数TIM2->CNT &= 0x0就足够了。

First of all, you should not set the TIM2_ARR register to zero. This register determines the maximum value of the counter before generating update or interrupt event by the timer. So if you set it to zero the counter won't start counting. You should configure the value of TIM2_PSC and TIM2_ARR registers based on SytemCoreClock Variable (the clock frequency of your MCU) to get the desired delay for toggling the GPIO pin.

And also you don't need to re-configure the timer every time after toggling the GPIO pin. If you want to reset the timer and start counting from zero TIM2->CNT &= 0x0 would be enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文