STM32F103C8T6定时器2,定时器3,定时器4初始化
时间: 2023-10-02 18:14:17 浏览: 124
以下是STM32F103C8T6的定时器2、定时器3、定时器4初始化的示例代码:
定时器2初始化:
```
#include "stm32f10x.h"
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;
TIM_TimeBaseInitStruct.TIM_Period = 10000 - 1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_Cmd(TIM2, ENABLE);
}
```
定时器3初始化:
```
#include "stm32f10x.h"
void TIM3_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;
TIM_TimeBaseInitStruct.TIM_Period = 10000 - 1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_Cmd(TIM3, ENABLE);
}
```
定时器4初始化:
```
#include "stm32f10x.h"
void TIM4_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;
TIM_TimeBaseInitStruct.TIM_Period = 10000 - 1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct);
TIM_Cmd(TIM4, ENABLE);
}
```
以上代码中,使用了TIM_TimeBaseInitTypeDef结构体来配置定时器的基本参数,包括计数模式、分频系数、预分频系数和周期。在初始化完成之后,通过TIM_Cmd()函数启动定时器。
阅读全文