定时器输入捕获

#include"stdio.h"
#include"gd32f30x.h"
#include"keydiver.h"
#include "systick.h"

 void pwm_init(){
	 rcu_periph_clock_enable(RCU_TIMER1);
	 timer_parameter_struct timercfg;
	  timercfg.prescaler         = 24000U;//5k
    timercfg.alignedmode       = TIMER_COUNTER_EDGE;
    timercfg.counterdirection  = TIMER_COUNTER_UP;
    timercfg.period            = 500000U-1;
    timercfg.clockdivision     = TIMER_CKDIV_DIV1;
    timercfg.repetitioncounter = 0U;
	  timer_init(TIMER1,&timercfg);
	  timer_oc_parameter_struct pwmcfg;
	  pwmcfg.outputstate  = (uint16_t)TIMER_CCX_ENABLE;
    pwmcfg.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    pwmcfg.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
	  timer_channel_output_config(TIMER1,TIMER_CH_1,&pwmcfg);
	  timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
	 //
	  timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,25000U);
	  rcu_periph_clock_enable(RCU_GPIOA);
	  gpio_deinit(RCU_GPIOA);
	  gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
	  timer_enable(TIMER1);
	 
 }
 void pwm_capture(){
	  rcu_periph_clock_enable(RCU_TIMER0);
	  timer_parameter_struct timercfg;
	  timercfg.prescaler         = 12U-1;//5k
    timercfg.alignedmode       = TIMER_COUNTER_EDGE;
    timercfg.counterdirection  = TIMER_COUNTER_UP;
    timercfg.period            = 50000U-1;
    timercfg.clockdivision     = TIMER_CKDIV_DIV1;
    timercfg.repetitioncounter = 0U;
	  timer_init(TIMER0,&timercfg);
	  timer_ic_parameter_struct inputcfg;
	  inputcfg.icpolarity  = TIMER_IC_POLARITY_RISING;
    inputcfg.icselection = TIMER_IC_SELECTION_DIRECTTI;
    inputcfg.icprescaler = TIMER_IC_PSC_DIV1;
    inputcfg.icfilter    = 0U;
	  timer_input_capture_config(TIMER0,TIMER_CH_0,&inputcfg);
	 
	  inputcfg.icpolarity  = TIMER_IC_POLARITY_FALLING;
    inputcfg.icselection = TIMER_IC_SELECTION_INDIRECTTI;
	  timer_input_capture_config(TIMER0,TIMER_CH_1,&inputcfg);
	 
    rcu_periph_clock_enable(RCU_GPIOA);
		gpio_init(GPIOA,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
		timer_interrupt_enable(TIMER0,TIMER_INT_CH0);
		timer_interrupt_enable(TIMER0,TIMER_INT_CH1);
    timer_interrupt_enable(TIMER0,TIMER_INT_UP);
		timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_CH0);
		timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_CH1);
		timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_UP);
		nvic_irq_enable(TIMER0_UP_IRQn,1,2);
		nvic_irq_enable(TIMER0_Channel_IRQn,2,2);
		timer_counter_value_config(TIMER0,0);
		
	  timer_enable(TIMER0);
 }
 volatile unsigned long overcnt=0;
 volatile unsigned long intervalcnt=0;
 volatile unsigned long ratialcnt=0;
 volatile unsigned long intervalcnt0=0;
 volatile unsigned long ratialcnt0=0;
 volatile unsigned long state=0;
 
 void TIMER0_channel_IRQHandler(){
	 if(timer_interrupt_flag_get(TIMER0,TIMER_INT_FLAG_CH0)){
		 timer_disable(TIMER0);
		 
		 timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_CH0);
		 
		
			 timer_counter_value_config(TIMER0,0);
			 intervalcnt=overcnt+timer_channel_capture_value_register_read(TIMER0,TIMER_CH_0);
			 intervalcnt0=intervalcnt;
			 overcnt=0;
		
		   timer_counter_value_config(TIMER0,0);
			 timer_enable(TIMER0);
		 
	 }
	 if(timer_interrupt_flag_get(TIMER0,TIMER_INT_FLAG_CH1)){
		 timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_CH1);
		 ratialcnt=overcnt+timer_channel_capture_value_register_read(TIMER0,TIMER_CH_0);
	 }
		 
 }
 
 void TIMER0_UP_IRQHandler(){
	 
	 if(timer_interrupt_flag_get(TIMER0,TIMER_INT_FLAG_UP)){
		 timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_UP);
		 overcnt+=50000U;
	 }
 }
	 void TIMER0_Channel_IRQHandler(){
		 if(timer_interrupt_flag_get(TIMER0,TIMER_INT_FLAG_UP)){
			 timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_UP);}
		
 }
int main()
{
	pwm_init();
	pwm_capture();
		
	}
	

首先配置pwm,然后配置捕获,本质是定时器捕获上升沿和下降沿,两个下降沿的差为周期,gpio的误差会比较大,所以不适用,每200分之1秒计数一次,将通道改为直接模式,捕获上升沿,改为非直接模式,捕获下降沿,这样一个引脚可以同时捕获上升沿和下降沿,开启两个通道的中断,和更新中断,使能之前先清除中断,配置中断函数,编写更新中断和通道捕获中断,在中断中得到上升沿和下降沿的时刻,如果超过10s就进入更新中断,然后计算时再加上更新的数值

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值