stm32f103 tim1 pwm
时间: 2023-09-30 22:01:44 浏览: 140
stm32f103
好的,下面是关于 STM32F103 的 TIM1 PWM 的基本步骤:
1. 配置 TIM1 时钟和预分频器:
```
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; //预分频器
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; //时基
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
```
2. 配置 TIM1 PWM 模式:
```
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500 - 1; //占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
```
3. 启动 TIM1 计数器:
```
TIM_Cmd(TIM1, ENABLE);
```
这样就完成了 STM32F103 的 TIM1 PWM 配置,其中占空比可以根据需要进行调节。
阅读全文