STM32F407调用库函数,是不是不能用同一个定时器输出两路周期相同,占空比相同,相位相差180°的PWM.下面是我的程序,我的比较直不同,但是只能输出相同的两路PWM,请问各位大佬这什么情况。
#include"pwm.h"
#include"stm32f4xx.h"
void PWM_Init(u16 arr,u16 psc){
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//NVIC_InitTypeDef NVIC_InitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ʹÄÜ
AʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //ʹÄÜTIM3ʱÖÓ
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_TIM5);//PA2¸´ÓÃΪTIM5
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM5);//PA3¸´ÓÃΪTIM5
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//PA2£¬3,
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_PinSource2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=arr; //ԤװÔØÖµ
TIM_TimeBaseStructure.TIM_Prescaler=psc; //·ÖƵϵÊý
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_CenterAligned3; //ÖÐÑë¶ÔÆë¼ÆÊý
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCNPolarity=TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse=470;
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2; //Êä³öģʽ£º
WM1ģʽ£ºCNT<CCʱ£¬ÓÐЧ
TIM_OCInitStruct.TIM_OCNPolarity=TIM_OCPolarity_Low; //¼«ÐÔ
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable; //Êä³öʹÄÜ
TIM_OCInitStruct.TIM_Pulse=1210; //±È½ÏµÄÖµCC
TIM_OC2Init(TIM5,&TIM_OCInitStruct);
TIM_OC3Init(TIM5,&TIM_OCInitStruct);
TIM_OC2PreloadConfig(TIM5,TIM_OCPreload_Enable);//ʹÄÜCCR2ÉϵÄԤװÔØ
TIM_OC3PreloadConfig(TIM5,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM5,ENABLE);//arrµÄԤװÔØʹÄÜ
TIM_Cmd(TIM5,ENABLE); //ʹÄܶ¨Ê±Æ÷5
}
那就用另一个通道,把 CCER 的 CCxP(极性)设为1, 就是180度, 又是可以设不同的占空比了
一周热门 更多>