GD32F103输入捕获程序,经过多次测试,终于完成了。本程序将TIMER2_CH2通道映射到PB0引脚,捕获PB0引脚低电平脉冲时间宽度。PB0是一个按钮,第1次按下采集一个值保存到TIMER2_CountValue1中,第2次按下采集一个值保存到TIMER2_CountValue2中,然后计算其低电平时间宽度。
网上也有人写了测试案例,但好像能用,但不是很完善。
我喜欢直奔主题,程序如下:
#include "Timer2_InputCapture.h"
#include "stdio.h" //使能printf(),sprintf()
uint16_t TIMER2_CountValueMAX = 65535;//设定最大捕获计数器值为65535
uint32_t TIMER2_CLKFrequence;
uint16_t TIMER2_OverValue = 0;//溢出次数
uint32_t TIMER2_CountValue1=0;
uint32_t TIMER2_CountValue2=0;
uint32_t TIMER2_CountResult=0;
uint8_t TIMER2_CaptchStatus=0;
float MyPeriod=0;
float MyFrequence=0;
void TIMER2_Input_Init(void);
//函数功能:将TIMER2_CH2通道映射到PB0引脚,捕获PB0引脚低电平脉冲时间宽度
//Tout=(65535+1)*(107+1)/108000000=0.065536秒
//TIMER2输入时钟频率:108000000/(107+1)=1000000Hz
void TIMER2_Input_Init(void)
{
timer_parameter_struct TimerParameterStruct; //TIMER0初始化结构体
timer_ic_parameter_struct timer_icinitpara; //TIMER0输入捕获结构体
rcu_periph_clock_enable(RCU_TIMER2); //使能TIMER0时钟
rcu_periph_clock_enable(RCU_GPIOB); //使能GPIOB时钟
rcu_periph_clock_enable(RCU_AF); //使能复用时钟
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
//将TIMER2_CH2通道映射到PB0引脚
timer_deinit(TIMER2);
timer_struct_para_init(&TimerParameterStruct); //将初始化结构体参数变为初始值
TimerParameterStruct.period = TIMER2_CountValueMAX; //定时器装载值,计数值超出后会产生溢出中断
TimerParameterStruct.prescaler = 107; //设置用来作为TIMx时钟频率除数的预分频值(APB2时钟分频值)
//Tout= ((arr+1)(psc+1))/Tclk;
//Tclk:TIM的输入时钟频率(单位为Hz),在这里是108000000Hz
//psc为定时器预分频值,在这里是107
//arr为重装载值,在程序中为65535
//Tout:TIM溢出时间(单位为s),Tout=(65535+1)*(107+1)/108000000=0.065536秒
TimerParameterStruct.clockdivision = TIMER_CKDIV_DIV1; //设置时钟分母值为1
TimerParameterStruct.counterdirection = TIMER_COUNTER_UP; //设置计数方向为"向上计数"
TimerParameterStruct.alignedmode = TIMER_COUNTER_EDGE; //设置为无中央对齐计数模式(边沿对齐模式)
TimerParameterStruct.repetitioncounter = 0; //重复计数,重复溢出多少次才会溢出中断,此处配置为0,不重复
timer_init(TIMER2,&TimerParameterStruct);//根据TimerParameterStruct所指向的参数初始化TIMERx的时间基数单位
timer_channel_input_struct_para_init(&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 = 0; //通道输入捕获滤波
timer_input_capture_config(TIMER2,TIMER_CH_2,&timer_icinitpara);
timer_counter_value_config(TIMER2,0);//设置TIMER2的计数器初始值为0
timer_auto_reload_shadow_enable(TIMER2); //自动重装载使能
/
timer_flag_clear(TIMER2,TIMER_FLAG_UP); //清除"TIMERx更新标志位"
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_UP); //清除"TIMERx更新中断标志位"
timer_interrupt_enable(TIMER2,TIMER_INT_UP); //使能"TIMERx更新"产生中断
timer_internal_clock_config(TIMER2);//设置"内部时钟"作为定时器时钟
TIMER2_CLKFrequence=SystemCoreClock/(TimerParameterStruct.prescaler+1);
//SystemCoreClock=108000000MHz
/
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH2); //清除CH2的中断标志位
timer_interrupt_enable(TIMER2,TIMER_INT_FLAG_CH2); //CH2通道中断使能
nvic_irq_enable(TIMER2_IRQn,1,0); //使能中断线
timer_enable(TIMER2);
}
/*
KEY==0
TIMER2_OverValue=11, TIMER2_CountValue1=19395, TIMER2_CountValue2=30890, TIMER2_CountResult=732380
MyPeriod=0.732380s
MyFrequence=1.365411Hz
KEY==0
*/
//TIMER2中断服务函数
void TIMER2_IRQHandler(void)
{
if( SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_FLAG_UP) )// 读取更新中断标志位
{
if(1 == TIMER2_CaptchStatus) TIMER2_OverValue++;
timer_flag_clear(TIMER2,TIMER_FLAG_UP); //清除"TIMER0更新标志位"
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_UP); //清除"更新中断标志位"
// printf("\r\nTIMER2 Interrupt");
}
if(timer_interrupt_flag_get(TIMER2,TIMER_INT_FLAG_CH2) != RESET)
{
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH2);
if(0 == TIMER2_CaptchStatus)
{
TIMER2_OverValue=0;//清除溢出计数器
TIMER2_CountValue1 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_2);
TIMER2_CaptchStatus = 1;
}
else if(1 == TIMER2_CaptchStatus)
{
TIMER2_CountValue2 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_2);
TIMER2_CountResult = (TIMER2_OverValue*TIMER2_CountValueMAX-TIMER2_CountValue1)+TIMER2_CountValue2;
MyPeriod=(float)TIMER2_CountResult/TIMER2_CLKFrequence;
MyFrequence = (float)TIMER2_CLKFrequence/ TIMER2_CountResult;
printf("\r\nTIMER2_OverValue=%u, TIMER2_CountValue1=%u, TIMER2_CountValue2=%u",TIMER2_OverValue,TIMER2_CountValue1,TIMER2_CountValue2);
printf(", TIMER2_CountResult=%u",TIMER2_CountResult);
printf("\r\nMyPeriod=%fs",MyPeriod);
printf("\r\nMyFrequence=%fHz",MyFrequence);
TIMER2_OverValue = 0;//溢出次数
TIMER2_CountValue1=0;
TIMER2_CountValue2=0;
TIMER2_CountResult=0;
TIMER2_CaptchStatus = 0;
}
}
}
#include "KEY.h"
void KEY_Init(void);
//函数功能:初始化KEY
void KEY_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOB);//使能GPIOB时钟,enable GPIO clock
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_0);//将GPIOB0设置为浮空输入
}
#ifndef __KEY_H
#define __KEY_H
#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#define KEY PBin(0) //读取PB0的输入状态值
#define Read_KEY_Value() gpio_input_bit_get(GPIOB, GPIO_PIN_0) //读取PB0的输入状态值
extern void KEY_Init(void);
#endif
#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t
#include "UART3.h"
#include "stdio.h" //使能printf(),sprintf()
#include "KEY.h"
#include "Timer2_InputCapture.h"
const char CPU_Reset_REG[]="\r\nCPU reset!\r\n";
int main(void)
{
//NVIC_PRIGROUP_PRE4_SUB0:抢占优先级为4bit(取值为0~15),子优先级为0bit(没有响应优先级)
//NVIC_PRIGROUP_PRE3_SUB1:抢占优先级为3bit(取值为0~7),子优先级为1bit(取值为0~1)
//NVIC_PRIGROUP_PRE2_SUB2:抢占优先级为2bit(取值为0~3),子优先级为2bit(取值为0~3)
//NVIC_PRIGROUP_PRE1_SUB3:抢占优先级为1bit(取值为0~1),子优先级为3bit(取值为0~7)
//NVIC_PRIGROUP_PRE0_SUB4:抢占优先级为0bit(没有抢占优先级),子优先级为3bit(取值为0~15)
nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);//设置系统中断优先级"抢占优先级为4bit,子优先级为0bit"
UART3_Init(115200);//初始化UART3
printf("%s",CPU_Reset_REG);//调试串口输出"\r\nCPU reset!\r\n"
INTX_ENABLE();//开启所有中断
KEY_Init();//初始化KEY
TIMER2_Input_Init();//将TIMER2_CH2通道映射到PB0引脚,捕获PB0引脚低电平脉冲时间宽度
while(1)
{
if(KEY==0)
{
printf("\n\rKEY==0");
while(KEY==0);//等待按键释放
}
}
}
#include "UART3.h"
#include "stdio.h" //使能printf(),sprintf()
void UART3_Init(unsigned int bound);
//函数功能:初始化串口3,这个和STM32F103VET6的UART4兼容
void UART3_Init(unsigned int bound)
{
rcu_periph_clock_enable(RCU_GPIOC); //使能GPIOC时钟,enable GPIO clock
rcu_periph_clock_enable(RCU_UART3); //使能UART3时钟,enable USART clock
gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
//将GPIOC10设置为AFIO口(复用IO口),输出上拉
gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
//将GPIOC11设置为浮空输入口
usart_deinit(UART3); //复位UART3,USART configure
usart_baudrate_set(UART3, bound); //设置UART3的波特率
usart_word_length_set(UART3, USART_WL_8BIT); //设置UART3数据传输格式为8位
usart_stop_bit_set(UART3, USART_STB_1BIT); //设置UART3停止位为1位
usart_parity_config(UART3, USART_PM_NONE); //设置UART3无需奇偶校验
usart_hardware_flow_rts_config(UART3, USART_RTS_DISABLE); //设置不使能UART3的RTS引脚功能
usart_hardware_flow_cts_config(UART3, USART_CTS_DISABLE); //设置不使能UART3的CTS引脚功能
usart_receive_config(UART3, USART_RECEIVE_ENABLE); //使能UART3接收
usart_transmit_config(UART3, USART_TRANSMIT_ENABLE); //使能UART3发送
usart_enable(UART3); //使能UART3
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(UART3, (uint8_t) ch);
while( RESET == usart_flag_get(UART3, USART_FLAG_TBE) )
{//等待串口0发送结束
}
return ch;
}
文章来源:https://www.toymoban.com/news/detail-629210.html
文章来源地址https://www.toymoban.com/news/detail-629210.html
到了这里,关于GD32F103输入捕获的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!