新人求助stm32在FreeRtos下的PWM读取

2019-03-23 19:41发布

各位前辈,我现在在用一个传感器,其中两个引脚分别输出两个PWM信号,我准备用STM32F4discover的板子在Freertos下面进行数据读取,我看了官方的例程,我用的笨办法就是用两个Timer分别捕获一路的PWM信号。但是我现在的问题是代码在网站上我找到了,也明白具体是怎么工作的了,但是现在就是不懂要是在Freertos下面怎么进行修改,比如怎么设置task。这里我想请有经验的前辈给我指点一下,看了几天实在是不知道怎么下手了,现在很急了,非常感谢各位。 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
2条回答
haohongyu
2019-03-23 21:41
/ /* TIM2 channel 2 pin (PA.01) configuration */
        //PWM capture PA1
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* TIM3 channel 2 pin (PA.07) configuration */
        //PWM capture PA7
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
      
void TIM_Configuration(void)
{
    TIM_ICInitTypeDef  TIM_ICInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

        //TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        //TIM_OCInitTypeDef  TIM_OCInitStructure;

        TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
        TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
        TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
        TIM_ICInitStructure.TIM_ICFilter = 0x0;
        
        TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);  
        TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);        /* Select the TIM2 Input Trigger: TI2FP2 */  
        TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
        TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
        
        TIM_Cmd(TIM2, ENABLE); /* TIM enable counter */  
        TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */
        
        /* Enable the TIM4 global Interrupt */
         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
         NVIC_Init(&NVIC_InitStructure);
        
        TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
        TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
        TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
        TIM_ICInitStructure.TIM_ICFilter = 0x0;
        
        TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);  
        TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);        /* Select the TIM2 Input Trigger: TI2FP2 */  
        TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); /* Select the slave Mode: Reset Mode */
        TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); /* Enable the Master/Slave Mode */
        
        TIM_Cmd(TIM3, ENABLE); /* TIM enable counter */  
        TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); /* Enable the CC2 Interrupt Request */         

         NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
         NVIC_Init(&NVIC_InitStructure);

}
  
void TIM2_IRQ(void)
{
  PlusCounter ++;
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
  
  IC2Value = TIM_GetCapture2(TIM2);  /* Get the Input Capture value */

  if (IC2Value != 0)
  {   
    DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value; /* Duty cycle computation */   
    Frequency = 72000000 / IC2Value;  /* Frequency computation */
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}

void TIM3_IRQ(void)
{
  PlusCounter2 ++;
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); /* Clear TIM2 Capture compare interrupt pending bit */
  
  IC2Value2 = TIM_GetCapture2(TIM3);  /* Get the Input Capture value */

  if (IC2Value2 != 0)
  {   
    DutyCycle2 = (TIM_GetCapture1(TIM3) * 100) / IC2Value2; /* Duty cycle computation */   
    Frequency2 = 72000000 / IC2Value2;  /* Frequency computation */
  }
  else
  {
    DutyCycle2 = 0;
    Frequency2 = 0;
  }
}

一周热门 更多>