打印

拜托香主能不能给个STM32的pwm捕获的例程?

[复制链接]
5135|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
AVALJ|  楼主 | 2009-10-29 12:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
拜托香主能不能给个STM32的pwm捕获的例程?我整了很久了就是测不出来
沙发
香水城| | 2009-10-29 12:19 | 只看该作者
STM32固件库中有PWM输入模式的例子。

使用特权

评论回复
板凳
AVALJ|  楼主 | 2009-10-29 12:55 | 只看该作者
不好意思,没有找到:'(

使用特权

评论回复
地板
香水城| | 2009-10-29 13:27 | 只看该作者
没有找到固件库?还是没有找到例子?

使用特权

评论回复
5
AVALJ|  楼主 | 2009-10-29 13:29 | 只看该作者
没找到例子

使用特权

评论回复
6
AVALJ|  楼主 | 2009-10-29 13:35 | 只看该作者
下面是我的程序,我要测量输入PWM的频率是50Hz,你看看有什么问题吗?
void tim2_config(void)
{
   TIM_DeInit(TIM2);
   
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
   TIM_BaseInitStructure.TIM_Period = 65535;       //ARR  
   TIM_BaseInitStructure.TIM_Prescaler = 199;      //PSC
   TIM_BaseInitStructure.TIM_ClockDivision = 0;   
   TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInit(TIM2, &TIM_BaseInitStructure);

   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);                       //复位模式为从模式

   TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);          //使能主从模式

   TIM_Cmd(TIM2, ENABLE);                           //使能TIM2计数器

   TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);          //使能CC2中断请求


}

void TIM2_IRQHandler(void)
{
  /* Clear TIM2 Capture compare interrupt pending bit */
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

  /* Get the Input Capture value */
  IC2Value = TIM_GetCapture2(TIM2);
}

/*******************************************************************************
* Function Name   : NVIC_Configuration
* Description        : Configures NVIC and Vector Table base location.
* Input                    : None
* Output                 : None
* Return                 : None
*******************************************************************************/
void NVIC_Configuration(void)
{
  // NVIC_InitTypeDef NVIC_InitStructure;
  
        #ifdef   VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
        NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else   /* VECT_TAB_FLASH   */
/* Set the Vector Table base location at 0x08000000 */
        NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
        #endif
  
   /* Configure the NVIC Preemption Priority Bits */  
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  
   /* Enable the USART1 Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
}

使用特权

评论回复
7
lxyppc| | 2009-10-29 14:50 | 只看该作者
int main(void)
{
#ifdef DEBUG
  debug();
#endif

  /* System Clocks Configuration */
  RCC_Configuration();

  /* NVIC configuration */
  NVIC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* TIM2 configuration: PWM Input mode ------------------------
     The external signal is connected to TIM2 CH2 pin (PA.01),
     The Rising edge is used as active edge,
     The TIM2 CCR2 is used to compute the frequency value
     The TIM2 CCR1 is used to compute the duty cycle value
  ------------------------------------------------------------ */


  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);

  /* Select the TIM2 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM2, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);

  while (1);
}

这个是官方例子上的,没有进行Base的初始化

下面是中断处理函数
void TIM2_IRQHandler(void)
{
  /* Clear TIM2 Capture compare interrupt pending bit */
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

  /* Get the Input Capture value */
  IC2Value = TIM_GetCapture2(TIM2);

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

    /* Frequency computation */
    Frequency = 72000000 / IC2Value;
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}

使用特权

评论回复
8
AVALJ|  楼主 | 2009-10-29 15:10 | 只看该作者
谢谢各位的回答
我的程序跟你的没什么区别,不知道为什么测的一直都是0
不知楼上的可不可以把官方的例子打包发一下?

使用特权

评论回复
9
henry_wu001| | 2009-10-29 15:22 | 只看该作者

使用特权

评论回复
10
LIU_XF| | 2009-10-30 08:06 | 只看该作者
楼主真是的,开口就要程序,STM32例程网上多着呢,自已就不勤快找一下,其实找资料的过程中也能有很多意外的收获的。

什么事情都找香主,香主都成大家保姆了,我想香主回答这样的问题会觉得很没意思。

使用特权

评论回复
11
AVALJ|  楼主 | 2009-10-30 11:07 | 只看该作者
谢谢各位的提醒,
很抱歉我刚开始学习STM32不知道官网上有例程
我的问题好像解决了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

11

主题

31

帖子

0

粉丝