新手求助。。
刚开始使用STM32,使用的是STM32F103的板子,现在希望利用TIM1的互补输出功能输出两路互补的SPWM波,用以合成50Hz正弦波波形,控制开关管实现DC逆变。
现在的问题是:SPWM波能够输出了,是20kHz,利用SysTick_Config(3600)配置SysTick,在SysTick中断处理的函数里修改两路输出的CCR值,脉宽也确实在变化,是50Hz,但是在这些波形中基本上都存在着1us宽的一个脉冲,频率是20kHz,具体见后面的图。
第一张图中较窄的那个脉冲就是所说的“未知”脉冲,周期性出现,频率20kHz;
测量这些脉冲的脉宽,是1us;
上面是TIM1的CH1的输出,下面是CH1N,配置了死区时间。这里奇怪的就是CH1中没有这个脉冲但CH1N有,这是CH1输出低电平的情况;
另外一种情况是上面这样:CH1和CH1N都出现了这个脉冲,只是反相,宽度一致,为1us;
还是上上一张图的情况。
计数器采用的是中心对准的模式,CenterAligned1,但这个脉冲出现的位置与SPWM波的中心位置无关,出现位置是随机的。。
代码如下:
void SysTick_Handler(void)
{
if(count<200)
{
CCR1_Val=CCR1_DATA[count];
CCR2_Val=CCR2_DATA[count];
}
if(count>=200&&count<400)
{
CCR1_Val=CCR2_DATA[count-200];
CCR2_Val=CCR1_DATA[count-200];
}
count++;
if(count>=399) //正弦波形取400点,对应占空比
count=0;
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM1_OCInitStructure.TIM_Pulse =CCR1_Val;
TIM_OC1Init(TIM1, &TIM1_OCInitStructure);
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM1_OCInitStructure.TIM_Pulse =CCR2_Val;
TIM_OC2Init(TIM1, &TIM1_OCInitStructure);
}
以上是SysTick中断的,下面是配置定时器的;
=============================================
void time_ini(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //TIM1 时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* TIM1复用功能开启*/
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1 , ENABLE);
/* Time Base configuration */
TIM1_TimeBaseStructure.TIM_Prescaler = 2;
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
TIM1_TimeBaseStructure.TIM_Period =599;
TIM1_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2 ;
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM1_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OC1Init(TIM1, &TIM1_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM1_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM1, &TIM1_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_BDTRStructInit(&TIM1_BDTRInitStructure);
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
|