stm32f103 time3配置只记时
时间: 2023-09-27 15:11:09 浏览: 40
STM32F103可以使用定时器3(TIM3)进行计时操作。下面是配置TIM3只进行计时的步骤:
1. 打开TIM3时钟。在RCC寄存器中设置TIM3时钟使能位(bit1)为1,以使能TIM3的时钟。
2. 配置TIM3的计数模式。在TIM3的控制寄存器1(TIM3_CR1)中设置计数模式为向上计数模式(bit4为0)。
3. 配置TIM3的时钟分频因子。在TIM3的控制寄存器1(TIM3_CR1)中设置时钟分频因子,以确定TIM3的计数时钟频率。
4. 配置TIM3的自动重载寄存器(TIM3_ARR)。设置自动重载寄存器的值,以确定计数器计数到该值后会自动重新开始计数。
5. 启动计时器。在TIM3的控制寄存器1(TIM3_CR1)中设置计时器使能位(bit0)为1,以启动TIM3的计时操作。
6. 读取TIM3的计数器值。在需要读取计时器值的时候,读取TIM3的计数器寄存器(TIM3_CNT)的值即可。
注意:在使用TIM3进行计时的过程中,需要保证TIM3的时钟频率不会超过计数器的最大计数值,否则计时结果会出现错误。
下面是一个简单的示例代码,用于配置TIM3只进行计时操作:
```c
#include "stm32f10x.h"
void TIM3_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 打开TIM3时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_Prescaler = 7199; // 时钟分频因子(72000000/7200 = 10000Hz)
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF; // 自动重载值
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分频因子(不分频)
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_Cmd(TIM3, ENABLE); // 启动TIM3计时器
}
uint16_t TIM3_GetCounter(void)
{
return TIM_GetCounter(TIM3); // 读取TIM3计数器值
}
int main(void)
{
TIM3_Init(); // 初始化TIM3
while (1)
{
uint16_t counter = TIM3_GetCounter(); // 读取TIM3计数器值
// TODO: 处理计时器计数值
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)