基于GD32F303RTC6芯片 配置下降沿捕获PWM低电平时间
时间: 2024-04-29 12:22:56 浏览: 93
1. 配置GPIO口为输入模式,设置下降沿触发中断。
```c
/* 配置GPIO口PC13为下降沿触发中断 */
rcu_periph_clock_enable(RCU_GPIOC);
gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
gpio_interrupt_enable(GPIOC, GPIO_PIN_13, GPIO_INT_FALLING);
```
2. 配置定时器为PWM输出模式,使用CCRx通道捕获下降沿时间,并启用定时器中断。
```c
/* 配置定时器3为PWM输出模式 */
rcu_periph_clock_enable(RCU_TIMER3);
timer_deinit(TIMER3);
timer_output_pwm_mode_config(TIMER3, TIMER_CH_1);
timer_output_pwm_mode_init(TIMER3, TIMER_CH_1, 1000, 50);
timer_primary_output_config(TIMER3, ENABLE);
/* 配置定时器3的CCRx通道捕获下降沿时间 */
timer_ic_parameter_struct timer_icinitpara;
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x0;
timer_input_capture_config(TIMER3, TIMER_CH_1, &timer_icinitpara);
/* 启用定时器3的中断 */
nvic_irq_enable(TIMER3_IRQn, 0, 0);
timer_interrupt_enable(TIMER3, TIMER_INT_CH1);
```
3. 在中断服务函数中读取捕获时间,即为PWM低电平时间。
```c
void TIMER3_IRQHandler()
{
if (timer_interrupt_flag_get(TIMER3, TIMER_INT_CH1)) {
/* 读取捕获时间 */
uint16_t capture_value = timer_channel_capture_value_register_get(TIMER3, TIMER_CH_1);
printf("PWM low level time: %d\n", capture_value);
timer_interrupt_flag_clear(TIMER3, TIMER_INT_CH1);
}
}
```
阅读全文