如何启用ARM CORTEX M3(STM32F103RB)中定时器的计数器?
我正在尝试编写代码来使用定时器(特别是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不应将
TIM2_ARR
寄存器设置为零。该寄存器确定定时器在生成更新或中断事件之前计数器的最大值。因此,如果将其设置为零,计数器将不会开始计数。您应该根据SytemCoreClock
变量(MCU 的时钟频率)配置TIM2_PSC
和TIM2_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 ofTIM2_PSC
andTIM2_ARR
registers based onSytemCoreClock
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.