我想用stm32f030定时器输出比较模式实现PWM波,一帧PWM波形的周期是固定的,里面包含8个通道的信号(低电平)输出,我的思路是:计数器1微秒加一次 ARR是固定的 然后就是通过改变CCR1来 达到这种效果 当然高电平是固定的为400微秒 每次CNT 与CCR 比较匹配后 都会翻转电平 并且产生中断来改变CCR的值 然后再这样
可是输出的波形 与所想的完全是两码事情 请大神们与我一起来看看这怎么回事
下面是代码:
uint32_t CONVERTER_CCR = 400;
uint32_t CONVERTER_ARR = 700;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//³õʼ»¯timer3×÷Ϊ¶¨Ê±Æ÷ʹÓÃ
void TIM3_Init_OC1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6,GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF ;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3 ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 48-1;
TIM_TimeBaseStructure.TIM_Period = 22000-1;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
//TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = CONVERTER_CCR ;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ITConfig(TIM3,TIM_IT_CC1|TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
All_counter = 0 ;
Channel_counter = 0 ;
CONVERTER_CCR = 400 ;
TIM_SetCompare1(TIM3,CONVERTER_CCR);
TIM_ClearITPendingBit (TIM3,TIM_IT_Update);
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC1)==SET)
{
All_counter++;
//if(Converter_channelbankFlag==0)
// {
Converter_Channel1_8();
// }
//if(Converter_channelbankFlag==1)
// {
// Converter_Channel9_16();
//}
TIM_SetCompare1(TIM3,CONVERTER_CCR);
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
}
}
void Converter_Channel1_8()
{
if(All_counter>=15)
{ }
else if(All_counter%2==1)
{
Channel_counter++ ;
//CONVERTER_CCR += Receiver_to_Servo_data.CH[Channel_counter] ;
CONVERTER_CCR += 1000 ;
}
else CONVERTER_CCR += 400 ;
}
|