void Drv_TIM2_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* TIM2 Clock Enable */ RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM2, ENABLE); //Note: TIM2 is a 32-bit up-counter/down-counter /* Configure TIM2 */ TIM_TimeBaseStructure.TIM_Period = 0x000FFFFF; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(TIM2, DISABLE); TIM_UpdateDisableConfig(TIM2, DISABLE); TIM_UpdateRequestConfig(TIM2, TIM_UpdateSource_Regular); /* Clear Update flag */ TIM_ClearITPendingBit(TIM2, TIM_IT_Update); /* Reset cnt */ TIM_SetCounter(TIM2, 0); /* Enable TIM2 */ TIM_Cmd(TIM2, ENABLE); }详细注释
时间: 2024-01-18 09:12:45 浏览: 176
这段代码是用来初始化STM32单片机中的TIM2定时器的。具体注释如下:
```c
void Drv_TIM2_Init(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //声明一个包含TIM2定时器初始化参数的结构体变量
/* TIM2 Clock Enable */
RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM2, ENABLE); //开启TIM2定时器的时钟
/* Configure TIM2 */
TIM_TimeBaseStructure.TIM_Period = 0x000FFFFF; //设置计数器自动重装载值,即计数器计数到这个值后会自动清零
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置预分频系数,这里设为0表示不预分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割系数,这里设为TIM_CKD_DIV1表示不分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //设置计数器的工作模式,这里设为向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据上述参数初始化TIM2定时器
TIM_ARRPreloadConfig(TIM2, DISABLE); //禁止TIM2定时器自动重载值的缓存
TIM_UpdateDisableConfig(TIM2, DISABLE); //使TIM2定时器下一个更新事件的产生不会禁止计数器的计数
TIM_UpdateRequestConfig(TIM2, TIM_UpdateSource_Regular); //设置TIM2定时器下一个更新事件的产生来源为计数器向上计数到自动重装载值
/* Clear Update flag */
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除TIM2定时器的更新标志位
/* Reset cnt */
TIM_SetCounter(TIM2, 0); //将TIM2定时器的计数器清零
/* Enable TIM2 */
TIM_Cmd(TIM2, ENABLE); //使能TIM2定时器
}
```
阅读全文