neji_ee
neji_ee

Reputation: 1

STM32 timer can't capture PWM input

my board:STM32 H743 after configure the advanced timer to capture pwm input,i can get the pwm frequency. but if i use hal_init() in the

main function,can't get, why? i add led in the interupt , now led not on, means ,can get into call back function.show my codes:

main:

    HAL_Init();
    SystemClock_Config();
    TIMx_Configuration(); 
    TIM8_Configuration();
configuration part:

tim2 just let led toggle 1s in callback function

    void TIMx_Configuration(void)
    {
        //TIMx_GPIO_Config();
        HAL_NVIC_SetPriority(GENERAL_TIM_IRQn, 0, 1);
        HAL_NVIC_EnableIRQ(GENERAL_TIM_IRQn);
        GENERAL_TIM_CLK_ENABLE();
         
        TIM_TimeBaseStructure.Instance = GENERAL_TIM;
    
        TIM_TimeBaseStructure.Init.Period = 10000 - 1;
    
        TIM_TimeBaseStructure.Init.Prescaler =  240000 - 1;
    
        HAL_TIM_Base_Init(&TIM_TimeBaseStructure);
    
        HAL_TIM_Base_Start_IT(&TIM_TimeBaseStructure);
    }
    void TIM8_Configuration()
    {
        //set priority 
        HAL_NVIC_SetPriority(MOTORIC_TIM_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(MOTORIC_TIM_IRQn);
        TIM_IC_InitTypeDef      TIM_ICInitStructure;
        TIM_SlaveConfigTypeDef  TIM_SlaveConfigStructure; 
        MOTORIC_TIM_CLK_ENABLE(); 
    
        motoric_htimx_bldcm.Instance = MOTORIC_TIM;
        motoric_htimx_bldcm.Init.Period = 100;
    
      // TIMxCLK = HCLK=240MHz 
      // timer frequency=TIMxCLK/(TIM_Prescaler+1)=1MHz
        motoric_htimx_bldcm.Init.Prescaler = MOTOR_ICPWM_PRESCALER_COUNT - 1;   
    
        motoric_htimx_bldcm.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
        motoric_htimx_bldcm.Init.CounterMode=TIM_COUNTERMODE_UP;
    
 
  //motoric_htimx_bldcm.Init.RepetitionCounter=0;
        HAL_TIM_IC_Init(&motoric_htimx_bldcm);
  • configure timer ic mode*
        TIM_ICInitStructure.ICPolarity = TIM_ICPOLARITY_RISING;
        TIM_ICInitStructure.ICSelection = TIM_ICSELECTION_DIRECTTI;
        TIM_ICInitStructure.ICPrescaler = TIM_ICPSC_DIV1;
        TIM_ICInitStructure.ICFilter = 0x0;
        HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_1);
        HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_2);
        HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_3);
        HAL_TIM_IC_ConfigChannel(&motoric_htimx_bldcm,&TIM_ICInitStructure,TIM_CHANNEL_4);
        
        TIM_SlaveConfigStructure.SlaveMode = TIM_SLAVEMODE_RESET;
        TIM_SlaveConfigStructure.InputTrigger = TIM_TS_TI1FP1;
        HAL_TIM_SlaveConfigSynchronization(&motoric_htimx_bldcm,&TIM_SlaveConfigStructure);

//four channel capture every channel use ic it mode,input frequency is about 1.5khz

        HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_1);
        HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_2);
        HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_3);
        HAL_TIM_IC_Start_IT(&motoric_htimx_bldcm,TIM_CHANNEL_4);
    }

just led,show the interrupt is ok

    void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
    {
        if(htim->Instance == GENERAL_TIM)
        {
            LED1_TOGGLE;
        }
    }
    void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
    {
      

//get capture value

      if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
      {
          LED1_TOGGLE;
          IC1Value = HAL_TIM_ReadCapturedValue(&motoric_htimx_bldcm,TIM_CHANNEL_1);
          IC2Value = HAL_TIM_ReadCapturedValue(&motoric_htimx_bldcm,TIM_CHANNEL_2); 
          if (IC1Value != 0)
          {

//to count frequency

            DutyCycle = (float)((IC2Value+1) * 100) / (IC1Value+1);
            Frequency = 240000000/24000/(float)(IC1Value+1);
          }
          else
          {

if not get ,zero

            DutyCycle = 0;
            Frequency = 0;
          }
    
      }
    }

Upvotes: 0

Views: 1062

Answers (1)

neji_ee
neji_ee

Reputation: 1

finally, this question is solved. it's not about hal_init(),the reason is that:my pwm capture configuration is configured to slave mode,need to change to master mode .like this:

TIM_MasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
TIM_MasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&motoric_htimx_bldcm, &TIM_MasterConfig);

Upvotes: 0

Related Questions