本帖最后由 ArterySW 于 2021-1-14 20:30 编辑
AT32F421-低频PWM输入捕获
/**********************************************************************/
1.使用AT32F421捕获低至0.5khzPWM波形,可自动计算占空比和PWM频率;
2.此demo使用421的TMR15实现,PA3为PWM输入口;
3.使用时,需将附件解压在AT32BSP的如下路径:Project/AT_START_F421/Examples/TMR
4.查看打印数据:
5.程序源码:
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;
}
}
|