利用GD32F470的定时器实现频率和占空比测试
1)main函数代码如下:
#include "gd32f4xx.h"
#include <stdio.h>
#include "gd32f470i_eval.h"
#include "systick.h"
void TIM_PwmInit(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
/* PWM输出管脚为复用推挽模式 */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,GPIO_PIN_1);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_1);
/* PWM输入管脚为浮空输入模式 */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,GPIO_PIN_6);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_6);
/* TIMER1初始化 */
timer_oc_parameter_struct timer_ocintpara = {0};
timer_parameter_struct timer_initpara = {0};
rcu_periph_clock_enable(RCU_TIMER1);
timer_deinit(TIMER1);
timer_initpara.prescaler = (108 - 1); // 预分频:108MHz / 108 = 1MHz
timer_initpara.alignedmode = TIMER_COUNTER_EDGE; // 边沿对齐计数
timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数
timer_initpara.period = (1000 - 1); // 周期:1MHz / 1000 = 1000Hz 注: 下面设置的占空比必须小于等于period,否则错误;
timer_init(TIMER1, &timer_initpara);
/* 配置所有通道为PWM模式0 */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; // 通道输出极性高,即高电平有效
timer_ocintpara.outputstate = TIMER_CCX_ENABLE; // 使能输出通道
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW; // 输出通道空闲低电平
timer_channel_output_config(TIMER1,TIMER_CH_1, &timer_ocintpara);
/* 配置通道1为25%占空比 */
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, (250 - 1)); // 250 / 1000 = 25%
timer_channel_output_mode_config(TIMER1,TIMER_CH_1, TIMER_OC_MODE_PWM0); // 配置为PWM模式0
timer_channel_output_shadow_config(TIMER1,TIMER_CH_1, TIMER_OC_SHADOW_DISABLE); // 关闭输出影子
timer_auto_reload_shadow_enable(TIMER1); // 使能重装载影子
timer_enable(TIMER1); // 使能定时器1
/* 初始化TIMER2 */
timer_ic_parameter_struct timer_icinitpara = {0};
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
timer_initpara.prescaler = (108 - 1); // 预分频:108MHz / 108 = 1MHz
timer_initpara.alignedmode = TIMER_COUNTER_EDGE; // 边沿对齐计数
timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数模式
timer_initpara.period = (65536 - 1); // 周期最好设置为最高,以免计数器溢出
timer_initpara.clockdivision = TIMER_CKDIV_DIV1; // 输入时钟1分频
timer_init(TIMER2,&timer_initpara);
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING; // 输入极性为上升沿,即上升沿有效
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI; // 输入捕获通道连接至CIx
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1; // 时钟1分频
timer_icinitpara.icfilter = 0x0;
timer_input_pwm_capture_config(TIMER2, TIMER_CH_0, &timer_icinitpara);
timer_input_trigger_source_select(TIMER2, TIMER_SMCFG_TRGSEL_CI0FE0); // 输入触发源为通道0
timer_slave_mode_select(TIMER2, TIMER_SLAVE_MODE_RESTART); // 从模式选择为复位模式
timer_master_slave_mode_config(TIMER2, TIMER_MASTER_SLAVE_MODE_ENABLE); // 使能从模式
timer_auto_reload_shadow_enable(TIMER2); // 使能重装载影子
nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0); // 抢占优先级4位,响应优先级0位
nvic_irq_enable(TIMER2_IRQn, 1, 0); // 使能中断服务,抢占优先级为1
timer_interrupt_flag_clear(TIMER2, TIMER_INT_CH0); // 清除通道0中断标志位
timer_interrupt_enable(TIMER2, TIMER_INT_CH0); // 使能通道0中断
timer_enable(TIMER2); // 使能定时器2
}
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM0, (uint8_t)ch);
while (RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
return ch;
}
extern __IO uint16_t dutycycle;
extern __IO uint16_t frequency;
int main(void)
{
systick_config();
TIM_PwmInit();
gd_eval_com_init(EVAL_COM0);
while(1)
{
printf("dutycycle: %d%%, frequency: %dHz\n", dutycycle, frequency);
delay_1ms(1000);
}
}
2)中断函数如下所示(gd32f4xx_it.c):
uint32_t ic1value = 0, ic2value = 0;
__IO uint16_t dutycycle = 0;
__IO uint16_t frequency = 0;
void TIMER2_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER2, TIMER_INT_CH0))
{
timer_interrupt_flag_clear(TIMER2, TIMER_INT_CH0); // 清除中断标志位
ic1value = timer_channel_capture_value_register_read(TIMER2, TIMER_CH_0) + 1;
if(0 != ic1value)
{
ic2value = timer_channel_capture_value_register_read(TIMER2, TIMER_CH_1) + 1;
dutycycle = (ic2value * 100) / ic1value; // 计算占空比
frequency = (float)1000000 / ic1value; // 计算频率
}
else
{
dutycycle = 0;
frequency = 0;
}
}
}
3)测试方法:PA6与PA1用镊子短接,用示波器连接信号,串口打开,如下图所示:
从上图可知道,频率是927赫兹,(目标代码应该是1000HZ),有一定误差,
上图示波器测试的占空比是25%;
4)打开串口,查看GD32F470测试的结果,如下图所示:
原文地址:https://blog.csdn.net/qq_30143193/article/details/139865385
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!