MW22-02A/BSP/Driver/timer/Timer.c

167 lines
4.6 KiB
C
Raw Normal View History

2025-05-21 01:55:40 +00:00
#include "gd32f4xx_timer.h"
#include "Timer.h"
#include "gd32f4xx_rcu.h"
#define timer_frequence 10000000 //<2F><>ʱ<EFBFBD><CAB1>Ƶ<EFBFBD><C6B5>1MHZ
/*!
\brief none ˮƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>
\param[in] none
\param[out] none
\retval none
\note LH @2022.07.11
*/
void ptz_hori_timer_start(unsigned int f)
{
unsigned int counter,chcv;
counter = (unsigned int)(timer_frequence / f + 0.5);
chcv = (unsigned int)(counter / 2.0 +0.5);
TIMER_CAR(TIMER3) = counter;
TIMER_CH0CV(TIMER3) = chcv;
timer_enable(TIMER3);
}
/*!
\brief none ˮƽֹͣ
\param[in] none
\param[out] none
\retval none
\note LH @2022.07.11
*/
void ptz_hori_timer_stop()
{
TIMER_CH0CV(TIMER3) = 0;
timer_disable(TIMER3);
}
/*!
\brief none <EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>
\param[in] none
\param[out] none
\retval none
\note LH @2022.07.11
*/
void ptz_vert_timer_start(unsigned int f)
{
unsigned int counter,chcv;
counter = (unsigned int)(timer_frequence / f + 0.5);
chcv = (unsigned int)(counter / 2.0 +0.5);
TIMER_CAR(TIMER2) = counter;
TIMER_CH0CV(TIMER2) = chcv;
timer_enable(TIMER2);
}
/*!
\brief none <EFBFBD><EFBFBD>ֱֹͣ
\param[in] none
\param[out] none
\retval none
\note LH @2022.07.11
*/
void ptz_vert_timer_stop()
{
TIMER_CH0CV(TIMER2) = 0;
timer_disable(TIMER2);
}
/*!
\brief none <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
\param[in] none
\param[out] none
\retval none
\note LH @2022.07.11
*/
static void Step_timer_cfg(void)
{//APB1<42><31><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ƶ<EFBFBD><C6B5>100MHz
timer_parameter_struct timer_initpara;
timer_oc_parameter_struct timer_ocintpara;
rcu_periph_clock_enable(RCU_TIMER2);
rcu_periph_clock_enable(RCU_TIMER3);
// rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
timer_struct_para_init(&timer_initpara);
timer_deinit(TIMER2);
timer_deinit(TIMER3);
// TIMER1 configuration
timer_initpara.prescaler = 9;//<2F><>Ƶϵ<C6B5><CFB5><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>Ƶ<EFBFBD><C6B5>Ϊ1MHZ
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//<2F><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD>
timer_initpara.counterdirection = TIMER_COUNTER_UP;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
timer_initpara.period = 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>ֵ<EFBFBD><D6B5>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>̽<EFBFBD><CCBD><EFBFBD><EFBFBD>޸<EFBFBD><DEB8>Ըı<D4B8><C4B1><EFBFBD><EFBFBD>ڣ<EFBFBD>TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;//ʱ<>ӷ<EFBFBD>Ƶ
timer_initpara.repetitioncounter = 0;//<2F><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ֵ
timer_init(TIMER2,&timer_initpara);
timer_init(TIMER3,&timer_initpara);
// CH0 configuration in PWM mode 0
timer_channel_output_struct_para_init(&timer_ocintpara);
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
timer_channel_output_config(TIMER3,TIMER_CH_0,&timer_ocintpara);
// CH0 configuration in PWM mode 0,duty cycle
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,0);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>͵<EFBFBD>ƽ<EFBFBD><C6BD>TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
// CH0 configuration in PWM mode 0,duty cycle
timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_0,0);
timer_channel_output_mode_config(TIMER3,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER3,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
// auto-reload preload enable
timer_auto_reload_shadow_enable(TIMER2);
timer_enable(TIMER2);
timer_auto_reload_shadow_enable(TIMER3);
timer_enable(TIMER3);
}
/*!
\brief none <EFBFBD><EFBFBD><EFBFBD>Ÿ<EFBFBD><EFBFBD><EFBFBD>
\param[in] none
\param[out] none
\retval none
\note LH @2022.07.11
*/
static void timer_gpio_init(void)
{
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOD);
//H-STEP<45><50>timer3-ch0
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);
//V-STEP<45><50>timer2-ch0
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
//<2F><><EFBFBD>Ÿ<EFBFBD><C5B8><EFBFBD>
gpio_af_set(GPIOD, GPIO_AF_2, GPIO_PIN_12);
gpio_af_set(GPIOC, GPIO_AF_2, GPIO_PIN_6);
}
void step_time_init()
{
Step_timer_cfg();
timer_gpio_init();
}