使用TIM1输出PWM波形,为什么使用TIM3捕获的结果不正确

2019-07-21 02:12发布

                                       
我的程序如下,我的程序如下,
个人思路是TIM1的CH3输出PWM波形,
然后使用TIM3的CH2捕获PWM波形

系统时钟96MHz


//TIM1 PWM部分初始化 
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM1_PWM_Init(u32 arr,u32 psc)
{          
//此部分需手动修改IO口设置
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);          //TIM1时钟使能    
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟 

GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_TIM1); //GPIOF11复用为定时器1

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;           //GPIOA11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure);              //初始化PA11

TIM_TimeBaseStructure.TIM_Prescaler=psc;  //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr;   //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; 

TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//初始化定时器14

//初始化TIM1 Channel4 PWM模式         
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
   // TIM_OCInitStructure.TIM_Pulse =1000;
//根据使用的函数  选择不同的通道
TIM_OC4Init(TIM1, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM1OC1

TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);  //使能TIM14在CCR1上的预装载寄存器

    TIM_ARRPreloadConfig(TIM1,ENABLE);//ARPE使能 

TIM_Cmd(TIM1, ENABLE);  //使能TIM1
    TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能TIM1的PWM输出,TIM1与TIM8有效

}  
//定时器3通道2输入捕获配置
//arr:自动重装值(TIM2,TIM5是32位的!!) (TIM3 和 TIM4 16位
//psc:时钟预分频数
void TIM3_CH2_Cap_Init()
{
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
  
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOB clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  
  /* TIM3 chennel2 configuration : PC.07 */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /* Connect TIM pin to AF2 */
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);

  /* Enable the TIM3 global Interrupt */
  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);


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

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

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);

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

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


/**
  * @brief  This function handles TIM4 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
  RCC_ClocksTypeDef RCC_Clocks;
  RCC_GetClocksFreq(&RCC_Clocks);
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
  {
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
/* Get the Input Capture value */
PWMCCP.Period= TIM_GetCapture2(TIM3);  //周期
if (PWMCCP.Period != 0)
{
   WMCCP.PulseWidth =TIM_GetCapture1(TIM3);  //脉冲宽度
   /* Duty cycle computation */
   WMCCP.DutyCycle = (TIM_GetCapture1(TIM3) * 100) / PWMCCP.Period; //占空比
   /* Frequency computation 
      TIM3 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */
  WMCCP.Frequency = (RCC_Clocks.HCLK_Frequency)/2 / PWMCCP.Period;
}
else
{
   WMCCP.DutyCycle = 0;
   WMCCP.Frequency = 0;
}
  }
}

在主函数里我的PWM的占空是50%,使用示波器看了,PWM波形没问题

但是捕获得到的数据不正常  占空比和周期都不正确
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
4条回答
wang12zhe
1楼-- · 2019-07-21 04:17
 精彩回答 2  元偷偷看……
正点原子
2楼-- · 2019-07-21 07:42
帮顶。。。。
灰太狼小灰兔
3楼-- · 2019-07-21 13:26
回复【3楼】wang12zhe:
---------------------------------
楼主 这个产生PWM的程序没有问题?
银桑
4楼-- · 2019-07-21 13:43
回复【3楼】wang12zhe:
---------------------------------
怎么解决的

一周热门 更多>