编写一段stm32f103c6t6配置TIM1的通道1和通道4输出PWM的完整代码
时间: 2023-03-06 13:03:39 浏览: 129
我们可以使用以下代码来配置TIM1的通道1和通道4输出PWM://设置TIM1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//设置TIM1的时钟分频因子
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; //不分频
TIM_TimeBaseStructure.TIM_Period= 0xFFFF; //设置计数器自动重装的值
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);//设置TIM1 OC1通道
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能TIM1输出
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //设置反转状态
TIM_OCInitStructure.TIM_Pulse = 0; //设置脉冲宽度
TIM_OC1Init(TIM1, &TIM_OCInitStructure);//设置TIM1 OC4通道
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能TIM1输出
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //设置反转状态
TIM_OCInitStructure.TIM_Pulse = 0; //设置脉冲宽度
TIM_OC4Init(TIM1, &TIM_OCInitStructure);//使能TIM1
TIM_Cmd(TIM1, ENABLE);
阅读全文