社区导航

 

搜索
查看: 3175|回复: 2

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

[复制链接]

29

TA的帖子

0

TA的资源

一粒金砂(中级)

Rank: 2

发表于 2012-8-5 16:22 | 显示全部楼层 |阅读模式
各位前辈,我现在在用一个传感器,其中两个引脚分别输出两个PWM信号,我准备用STM32F4discover的板子在Freertos下面进行数据读取,我看了官方的例程,我用的笨办法就是用两个Timer分别捕获一路的PWM信号。但是我现在的问题是代码在网站上我找到了,也明白具体是怎么工作的了,但是现在就是不懂要是在Freertos下面怎么进行修改,比如怎么设置task。这里我想请有经验的前辈给我指点一下,看了几天实在是不知道怎么下手了,现在很急了,非常感谢各位。
此帖出自stm32/stm8论坛

回复

使用道具 举报

29

TA的帖子

0

TA的资源

一粒金砂(中级)

Rank: 2

 楼主| 发表于 2012-8-5 16:33 | 显示全部楼层

这是找的相关代码

/* 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;
  }
}

回复

使用道具 举报

29

TA的帖子

0

TA的资源

一粒金砂(中级)

Rank: 2

 楼主| 发表于 2012-8-6 01:35 | 显示全部楼层
诚求高手指点一下啊

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

关闭

站长推荐上一条 /5 下一条

  • 论坛活动 E手掌握

    扫码关注
    EEWORLD 官方微信

  • EE福利  唾手可得

    扫码关注
    EE福利 唾手可得

Archiver|手机版|小黑屋|电子工程世界 ( 京ICP证 060456 )

GMT+8, 2020-2-19 09:04 , Processed in 0.149491 second(s), 17 queries , Gzip On, MemCache On.

快速回复 返回顶部 返回列表