这几天闲来无事弄磁悬浮,顺便学习下PWM的控制,为以后的SPWM学习铺路。碰见如下问题TIM1_CH1 的幅值 是3.3V 而我镜像配置的TIM1_CH2只有0.3V
我看见贴吧好像有人碰到这个问题,但是没有正确答案,希望能够正确的解决,帮助到有需求的人。
PWM2
PWM1
前面是TIM1_CH2的波形,后面是TIM1_CH1的波形
代码如下
static void TIM1_PWM_2load_Init(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_TIM1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.Speed = GPIO_Speed_50MHZ;
GPIO_Iint(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TImeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE)
}
void PWM_Init(void)
{
TIM1_PWM_2load_Init(899,0);
}
void PWM_Breath(void)
{
static uint8_t flag=0;
static uint16_t pwmval =800;
Delay_ms(10);
if(0 == flag)
{
Delay_ms(20);
pwmval --;
}
else
{
pwmval +=20;
}
if(pwmval>879)
{
flag = 1;
}
if(pwmval<749)
{
flag = 0;
}
TIM_SetCompare1(TIM1,pwmval);
TIM_SetCompare2(TIM2,899-pwmval);
}
int main ( void)
{
Peripheral_Configuration();
PWM_Init();
while(1)
{
PWM_Breath();
}
return 0;
}
我将定时器全部映射,输出电平是正常的,说明程序没有问题,难道是管脚真的坏了,但是将管脚配置位GPIO_Mode_Out_PP 输出正常, GPIO_Mode_AF_PP只能输出0.3V,推挽模式应该是一样的,我在去查查推挽方式
一周热门 更多>