study_hardly
2020-01-31 18:17
浏览 329

有大神帮我看看我这程式有什么问题吗(stm32 pwm呼吸灯)?灯只会亮,不会呼吸。

#include

#define Fremap

#ifdef Premap
#define Ch1 GPIO_Pin_4
#define Ch2 GPIO_Pin_5
#define Ch3 GPIO_Pin_0
#define Ch4 GPIO_Pin_1
#else
#ifdef Fremap
#define Ch1 GPIO_Pin_6
#define Ch2 GPIO_Pin_7
#define Ch3 GPIO_Pin_8
#define Ch4 GPIO_Pin_9
#else
#define Ch1 GPIO_Pin_6
#define Ch2 GPIO_Pin_7
#define Ch3 GPIO_Pin_0
#define Ch4 GPIO_Pin_1
#endif
#endif

void GPIO_Tim3PWM(u8 chx)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
switch(chx)
{
case 1:
GPIO_InitStructure.GPIO_Pin=Ch1;
break;

case 2:
GPIO_InitStructure.GPIO_Pin=Ch2;
break;

case 3:
GPIO_InitStructure.GPIO_Pin=Ch3;
break;

case 4:
GPIO_InitStructure.GPIO_Pin=Ch4;
break;

}
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

#ifdef Premap
  GPIO_Init(GPIOB,&GPIO_InitStructure);
#else
  #ifdef Fremap
    GPIO_Init(GPIOC,&GPIO_InitStructure);
#else
  swtich(chx)
  {
     case 1:
          GPIO_Init(GPIOA,&GPIO_InitStructure);
         break; 
     case 2:
          GPIO_Init(GPIOA,&GPIO_InitStructure);
         break;
   case 3:
          GPIO_Init(GPIOB,&GPIO_InitStructure);
         break; 
   case 4:
          GPIO_Init(GPIOB,&GPIO_InitStructure);
         break;     
    }
#endif
#endif

}

void TIM3PinReMap(u8 remap)
{
switch(remap)
{
case 0:
break;
case 1:
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
break;
case 2:
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
break;
}
}

void TIM_Init(TIM_TypeDef*TIMx,u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStrcuture;
TIM_DeInit(TIMx);
TIM_InternalClockConfig(TIMx);

TIM_TimeBaseStrcuture.TIM_Period=arr-1;
TIM_TimeBaseStrcuture.TIM_Prescaler=psc-1;
TIM_TimeBaseStrcuture.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStrcuture.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStrcuture);
TIM_ARRPreloadConfig(TIMx,ENABLE);

}

void TIM_PWMMode(TIM_TypeDef*TIMx,u8 chx,u8 H2L,u16 pulse)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
switch(chx)
{
case 1:
if(H2L)
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
else
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse=pulse-1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIMx,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIMx,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIMx,ENABLE);
TIM_ARRPreloadConfig(TIMx,ENABLE);
TIM_Cmd(TIMx,ENABLE);
break;
case 2:
if(H2L)
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
else
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse=pulse-1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC2Init(TIMx,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIMx,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIMx,ENABLE);
TIM_ARRPreloadConfig(TIMx,ENABLE);
TIM_Cmd(TIMx,ENABLE);
break;
case 3:
if(H2L)
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
else
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse=pulse-1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC3Init(TIMx,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIMx,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIMx,ENABLE);
TIM_ARRPreloadConfig(TIMx,ENABLE);
TIM_Cmd(TIMx,ENABLE);
break;
case 4:
if(H2L)
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
else
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse=pulse-1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC4Init(TIMx,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIMx,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIMx,ENABLE);
TIM_ARRPreloadConfig(TIMx,ENABLE);
TIM_Cmd(TIMx,ENABLE);
break;
default:break;
}
}

void delay_nms(u16 time)
{
    u16 i=0;
    while(time--)
    {
        i=12000;
        while(i--);
    }
}

int main(void)
{
    short int kcnt=2000;

    SystemInit();
    GPIO_Tim3PWM(3);
    TIM3PinReMap(2);
    TIM_Init(TIM3,2000,72);

    GPIO_Tim3PWM(4);
    TIM_PWMMode(TIM3,3,1,kcnt);
    TIM_PWMMode(TIM3,4,0,kcnt);

 while(1)
 {
     for(kcnt=2001;kcnt>0;kcnt=kcnt-200)
     {
         TIM_SetCompare3(TIM3,kcnt);
         TIM_SetCompare4(TIM3,kcnt);
         delay_nms(100);
     }
     for(kcnt=1;kcnt<=2001;kcnt=kcnt+200)
     {
         TIM_SetCompare3(TIM3,kcnt);
         TIM_SetCompare4(TIM3,kcnt);
         delay_nms(100);
     }
 }

}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

相关推荐 更多相似问题