GD32F450VET6定时器脉冲宽度捕获代码
时间: 2023-11-12 07:54:56 浏览: 112
对于GD32F450VET6芯片的定时器脉冲宽度捕获,以下是一个简单的示例代码:
```c
#include "gd32f4xx.h"
#include <stdio.h>
void timer_capture_init(void)
{
/* 使能定时器时钟 */
rcu_periph_clock_enable(RCU_TIMER1);
/* 配置TIM1通用定时器中的通道1引脚(PA8)为复用功能 */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_8);
/* 配置定时器TIM1 */
timer_prescaler_config(TIMER1, 10800 - 1);
timer_ic_parameter_config(TIMER1, TIMER_CH_1, TIMER_IC_POLARITY_RISING, TIMER_IC_SELECTION_DIRECTTI, TIMER_IC_PSC_DIV1, 0x0);
timer_channel_input_capture_config(TIMER1, TIMER_CH_1, TIMER_IC_POLARITY_FALLING);
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 0xFFFF);
timer_channel_mode_config(TIMER1, TIMER_CH_1, TIMER_CH_MODE_CAPTURE);
timer_channel_enable(TIMER1, TIMER_CH_1);
/* 使能定时器 */
timer_enable(TIMER1);
}
int main(void)
{
/* 初始化定时器 */
timer_capture_init();
while (1) {
/* 获取捕获值 */
uint16_t capture_value = timer_channel_capture_value_register_read(TIMER1, TIMER_CH_1);
/* 打印捕获值 */
printf("Capture Value: %d\n", capture_value);
}
}
```
阅读全文