STM32 4路PWM配置完全函数:
void PWM_Configuration(void)//PWM初始化函数
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器初始化结构
TIM_OCInitTypeDef TIM_OCInitStructure;//通道输出初始化结构
GPIO_InitTypeDef GPIO_InitStructure;//输出通道的GPIO管脚结构
//(1)TIM3初始化,时间基部分
TIM_TimeBaseStructure.TIM_Period =999; //周期0~999
TIM_TimeBaseStructure.TIM_Prescaler = 71; //时钟分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //基本初始化
//TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE); //打开中断,中断需要这行代码
//(2)TIM3通道初始化 ,CCP部分
TIM_OCStructInit(& TIM_OCInitStructure); //默认参数
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //工作状态
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //设定为输出,需要PWM输出才需要这行代码
TIM_OCInitStructure.TIM_Pulse =250 ; //占空长度
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //通道初始化
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //工作状态
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //设定为输出,需要PWM输出才需要这行代码
TIM_OCInitStructure.TIM_Pulse =500 ; //占空长度
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //通道初始化
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //工作状态
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //设定为输出,需要PWM输出才需要这行代码
TIM_OCInitStructure.TIM_Pulse =750 ; //占空长度
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //工作状态
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //设定为输出,需要PWM输出才需要这行代码
TIM_OCInitStructure.TIM_Pulse =900 ; //占空长度
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE); //使能TIM3,到此在TIM中已配置完成,下面开始配置GPIO
//(3)PWM输出通道初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;//选择PC
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //模式为推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化GPIOA寄存器
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE);
}
文章评论(0条评论)
登录后参与讨论