OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4291|回复: 0

AT32F421-低频PWM输入捕获

[复制链接]

44

主题

49

帖子

0

精华

初级会员

Rank: 2

积分
175
金钱
175
注册时间
2017-5-6
在线时间
10 小时
发表于 2021-1-14 20:20:41 | 显示全部楼层 |阅读模式
AT32F421-低频PWM输入捕获
/**********************************************************************/
1.使用AT32F421捕获低至0.5khzPWM波形,可自动计算占空比和PWM频率;
2.使用时,需将附件解压在AT32BSP的如下路径:Project/AT_START_F421/Examples/TMR
3.查看打印数据:
捕获.JPG
4.程序源码:
main函数:
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_at32f413_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_at32f4xx.c file
     */

  /* System Clocks Configuration */
  RCC_Configuration();

  /* NVIC configuration */
  NVIC_Configuration();

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

  /* Board.c init */
  UART_Print_Init(115200);

  /* TMR15 configuration: PWM Input mode ------------------------
     The external signal is connected to TMR15 CH2 pin (PA.01),
     The Rising edge is used as active edge,
     The TMR15 CCR2 is used to compute the frequency value
     The TMR15 CCR1 is used to compute the duty cycle value
  ------------------------------------------------------------ */
/* TMRe base configuration */
  TMR_TimeBaseStructInit(&TMR_TMReBaseStructure);
  TMR_TMReBaseStructure.TMR_Period = 65535;
  TMR_TMReBaseStructure.TMR_DIV = 3;
  TMR_TMReBaseStructure.TMR_ClockDivision = 0;
  TMR_TMReBaseStructure.TMR_CounterMode = TMR_CounterDIR_Up;

  TMR_TimeBaseInit(TMR15, &TMR_TMReBaseStructure);

  TMR_ICStructInit(&TMR_ICInitStructure);
  TMR_ICInitStructure.TMR_Channel = TMR_Channel_2;
  TMR_ICInitStructure.TMR_ICPolarity = TMR_ICPolarity_Rising;
  TMR_ICInitStructure.TMR_ICSelection = TMR_ICSelection_DirectTI;
  TMR_ICInitStructure.TMR_ICDIV = TMR_ICDIV_DIV1;
  TMR_ICInitStructure.TMR_ICFilter = 0x0;

  TMR_PWMIConfig(TMR15, &TMR_ICInitStructure);

  /* Select the TMR15 Input Trigger: TI2FP2 */
  TMR_SelectInputTrigger(TMR15, TMR_TRGSEL_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TMR_SelectSlaveMode(TMR15, TMR_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TMR_SelectMasterSlaveMode(TMR15, TMR_MasterSlaveMode_Enable);

  /* TMR enable counter */
  TMR_Cmd(TMR15, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TMR_INTConfig(TMR15, TMR_INT_CC2, ENABLE);

  while (1)
  {
    printf("Frequency=%dHZ,Dutycycle=%d%%\r\n",Frequency,DutyCycle);
  }
}

中断处理:
void TMR15_GLOBAL_IRQHandler(void)
{
  /* Clear TMR3 Capture compare interrupt pending bit */
  TMR_ClearITPendingBit(TMR15, TMR_INT_CC2);

  /* Get the Input Capture value */
  IC2Value = TMR_GetCapture2(TMR15);

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

    /* Frequency computation */
    Frequency = SystemCoreClock / (IC2Value*4);
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}

pwm_input.rar

1.64 MB, 下载次数: 5

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-14 09:57

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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