cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f469 PWN not output NState ?

ac.2
Associate II

My project needs to output PWM NState, My code is as follows, but no PWM signal output has been detected by the oscilloscope measurement. How can I solve this problem?

#define MOTOR_DE_RCC_PERIPH             RCC_AHB1Periph_GPIOC
#define MOTOR_DE_PORT                   GPIOC
#define MOTOR_DE_PIN                    GPIO_Pin_6
 
#define MOTOR_PWM_RCC_PERIPH            RCC_AHB1Periph_GPIOC
#define MOTOR_PWM_TIMER_PERIPH          RCC_APB2Periph_TIM8    
#define MOTOR_PWM_PORT                  GPIOC
#define MOTOR_PWM_PIN                   GPIO_Pin_7
 
#define MOTOR_PWM_AF                    GPIO_AF_TIM8
#define MOTOR_PWM_TIM                   TIM8
#define MOTOR_PWM_PIN_SOURCE            GPIO_PinSource7
 
#define PWM_FREQ                        100             //      100 Hz
#define PWM_INITIAL_PULSE               1000    
 
void bsp_motor_output_nstate  (void)
{
    TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;
    TIM_OCInitTypeDef           TIM_OCInitStructure;
    GPIO_InitTypeDef            GPIO_InitStructure;
    uint16_t                    usPeriod;
    uint16_t                    usPrescaler;
    uint32_t                    uiTIMxCLK;
 
   // enable GPIO clock
    if(MOTOR_PWM_RCC_PERIPH == MOTOR_DE_RCC_PERIPH)
    {
        RCC_AHB1PeriphClockCmd(MOTOR_DE_RCC_PERIPH, ENABLE);
    }
    else
    {
        RCC_AHB1PeriphClockCmd(MOTOR_DE_RCC_PERIPH, ENABLE);
        RCC_AHB1PeriphClockCmd(MOTOR_PWM_RCC_PERIPH, ENABLE);
    }
    
    //configure GPIO 
    GPIO_InitStructure.GPIO_Pin   = MOTOR_DE_PIN;		
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;	
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL ;	
    GPIO_Init(MOTOR_DE_PORT, &GPIO_InitStructure);
    
    GPIO_WriteBit(MOTOR_DE_PORT, MOTOR_DE_PIN, Bit_RESET);  
    
            /* confiure motor PWM timer */
    // enable tiemr clk
    RCC_APB2PeriphClockCmd(MOTOR_PWM_TIMER_PERIPH, ENABLE);
    
    //configure GPIO
    GPIO_InitStructure.GPIO_Pin   = MOTOR_PWM_PIN;	
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
    GPIO_Init(MOTOR_PWM_PORT, &GPIO_InitStructure);
 
    // link to AF function 
    GPIO_PinAFConfig(MOTOR_PWM_PORT, MOTOR_PWM_PIN_SOURCE, MOTOR_PWM_AF);
    
    // configure PWM  Freq
    uiTIMxCLK = SystemCoreClock;
    
    if(PWM_FREQ < 100)
    {
        usPrescaler = 10000 - 1;
        usPeriod =  (uiTIMxCLK / 10000) / PWM_FREQ  - 1;	
    }
    else if(PWM_FREQ < 3000)
    {
        usPrescaler = 100 - 1;					
        usPeriod    = (uiTIMxCLK / 100) / PWM_FREQ - 1;	
    }
    else
    {
        usPrescaler = 0;					
        usPeriod = uiTIMxCLK / PWM_FREQ - 1;	
    }
    
    // Time base configuration 
    TIM_TimeBaseStructure.TIM_Period            = usPeriod;
    TIM_TimeBaseStructure.TIM_Prescaler         = usPrescaler;
    TIM_TimeBaseStructure.TIM_ClockDivision     = 0;
    TIM_TimeBaseStructure.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;		// TIM1 and TIM8 must setting	
 
    TIM_TimeBaseInit(MOTOR_PWM_TIM, &TIM_TimeBaseStructure);
 
    // PWM1 Mode configuration: Channel2 
    TIM_OCInitStructure.TIM_OCMode      = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
    TIM_OCInitStructure.TIM_Pulse       = (PWM_INITIAL_PULSE * usPeriod) / 10000;
    TIM_OCInitStructure.TIM_OCPolarity  = TIM_OCPolarity_High;
	
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;	// only for TIM1 and TIM8. 	
    TIM_OCInitStructure.TIM_OCNPolarity  = TIM_OCNPolarity_High;	// only for TIM1 and TIM8. 		
    TIM_OCInitStructure.TIM_OCIdleState  = TIM_OCIdleState_Reset;	// only for TIM1 and TIM8. 
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;	// only for TIM1 and TIM8. 
    
    TIM_OC2Init         (MOTOR_PWM_TIM, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(MOTOR_PWM_TIM, TIM_OCPreload_Enable);
    
    TIM_ARRPreloadConfig(MOTOR_PWM_TIM, ENABLE);
 
    // TIMx enable counter 
    TIM_Cmd            (MOTOR_PWM_TIM, ENABLE);
    TIM_CtrlPWMOutputs (MOTOR_PWM_TIM, ENABLE);
}

0 REPLIES 0