先来段代码
TIM1_PWM_Init(376,0);//不分频
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=arr-1;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_CenterAligned3; //设置成加减计数方式
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
//TIM_OCInitStructure.TIM_Pulse=500;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
//TIM_OCInitStructure.TIM_Pulse=500;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
//此段为定时器自带的互补PWM,因电路设计需要并未采用
// TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
// TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
// TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
// TIM_BDTRInitStructure.TIM_DeadTime = 0x90;
// TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
// TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
// TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
// TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
//TIM_OCInitStructure.TIM_Pulse=500;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC3Init(TIM1,&TIM_OCInitStructure);
// TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
// TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
// TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
// TIM_BDTRInitStructure.TIM_DeadTime = 0x90;
// TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
// TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
// TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
// TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
//TIM_OCInitStructure.TIM_Pulse=500;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;
TIM_OC4Init(TIM1,&TIM_OCInitStructure);
// TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
// TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
// TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
// TIM_BDTRInitStructure.TIM_DeadTime = 0x90;
// TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
// TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
// TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
// TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
//使能TIM1在CCR上的预装载寄存器
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_SetAutoreload(TIM1,arr-1);
//此段为强制拉低PWM引脚,然而并未起作用,若有此需要可用TIM_SetCompare设置成arr-1或0
// TIM_ForcedOC1Config(TIM1, TIM_ForcedAction_InActive);
// TIM_ForcedOC2Config(TIM1, TIM_ForcedAction_InActive);
//TIM_ForcedOC3Config(TIM1, TIM_ForcedAction_InActive);
//TIM_ForcedOC4Config(TIM1, TIM_ForcedAction_InActive);
TIM_SetCompare1(TIM1,300);
TIM_SetCompare2(TIM1,300);
TIM_SetCompare3(TIM1,arr-1-300);
TIM_SetCompare4(TIM1,arr-1-300);
}
各引脚输出波形如图
PA8和PA9为互补的波形,PA8和PA10相差180°,PA9和PA11相差180°。