打印

STM32PWM输入模式

[复制链接]
2020|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xiehaowang|  楼主 | 2013-10-17 09:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void TIM3_PWMINPUT_INIT(u16 arr,u16 psc)
{
  
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef  TIM3_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);     //open TIM3 clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  //open gpioA clock

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;                               //GPIO??
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ  ¼ÆÊýµ½5000Ϊ500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ  10KhzµÄ¼ÆÊýƵÂÊ  
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»

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

TIM3_ICInitStructure.TIM_Channel = TIM_Channel_2;                  
  TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;      
  TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;   
  TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM3_ICInitStructure.TIM_ICFilter = 0x0;

TIM_PWMIConfig(TIM3, &TIM3_ICInitStructure);               
  TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);               
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
  TIM_Cmd(TIM3, ENABLE);                                       
  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);   
}


u16 IC2Value = 0,DutyCycle = 0,Frequency = 0;

void TIM3_IRQHandler(void)
{

  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);                /?TIM???????

  IC2Value = TIM_GetCapture2(TIM3);                         /?IC2???????,??PWM??????
  if (IC2Value != 0)
  {
   
    DutyCycle = TIM_GetCapture1(TIM3);
    Frequency = IC2Value;                                          /?PWM???
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}
大家帮我看下这个代码
这个是STM32的PWM输入
为什么DutyCycle 一直都是0啊
沙发
jjjkkk00| | 2013-10-17 11:32 | 只看该作者
你这是例程上直接拷贝的。我也不知道了。
问下:中断你能进去么?

使用特权

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

本版积分规则

15

主题

30

帖子

0

粉丝