392 lines
13 KiB
C
392 lines
13 KiB
C
#include "gd32f4xx_timer.h"
|
||
#include "Timer.h"
|
||
#include "gd32f4xx_rcu.h"
|
||
#include "ptz_type_select.h"
|
||
#include "ptz_struct.h"
|
||
#include <includes.h>
|
||
#include "gd32f4xx_it.h"
|
||
|
||
#ifdef TMC2160
|
||
|
||
#define timer_frequence 2000000 //定时器频率1MHZ
|
||
|
||
|
||
/*!
|
||
\brief none 水平输出频率
|
||
\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 垂直输出频率
|
||
\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 垂直停止
|
||
\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 步进电机定时器初始化
|
||
\param[in] none
|
||
\param[out] none
|
||
\retval none
|
||
\note LH @2022.07.11
|
||
*/
|
||
static void Step_timer_cfg(void)
|
||
{//APB1总线时钟频率50MHz
|
||
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 = 49;//分频系数,确定频率为1MHZ
|
||
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//计数模式,边缘对齐
|
||
timer_initpara.counterdirection = TIMER_COUNTER_UP;//计数方向
|
||
timer_initpara.period = 999;//计数重装载值,确定周期,,该值可在任意时刻进行修改以改变周期,TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
|
||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;//时钟分频
|
||
timer_initpara.repetitioncounter = 0;//计数重复值
|
||
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);//输出无效低电平,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 引脚复用
|
||
\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,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,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);
|
||
|
||
//引脚复用
|
||
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();
|
||
|
||
}
|
||
|
||
|
||
#endif
|
||
|
||
|
||
|
||
#ifdef Full_bridge
|
||
|
||
void PWM_timer4_cfg();
|
||
|
||
/*!
|
||
\brief none 定时器引脚复用
|
||
\param[in] none
|
||
\param[out] none
|
||
\retval none
|
||
\note LH @2022.07.20
|
||
*/
|
||
static void PWM_gpio_init(void)
|
||
{
|
||
rcu_periph_clock_enable(RCU_GPIOC);
|
||
// rcu_periph_clock_enable(RCU_GPIOD);
|
||
//水平PC6-TIM2_CH0,水平PC7-TIM2_CH1,水平PC8-TIM2_CH2
|
||
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);
|
||
|
||
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
|
||
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
|
||
|
||
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
|
||
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
|
||
|
||
//引脚复用
|
||
gpio_af_set(GPIOC, GPIO_AF_2, GPIO_PIN_6);
|
||
gpio_af_set(GPIOC, GPIO_AF_2, GPIO_PIN_7);
|
||
gpio_af_set(GPIOC, GPIO_AF_2, GPIO_PIN_8);
|
||
|
||
//垂直PD12-TIM3_CH0,垂直PD13-TIM3_CH1,垂直PD14-TIM3_CH2
|
||
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);
|
||
|
||
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
|
||
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
|
||
|
||
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_14);
|
||
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_14);
|
||
|
||
//引脚复用
|
||
gpio_af_set(GPIOD, GPIO_AF_2, GPIO_PIN_12);
|
||
gpio_af_set(GPIOD, GPIO_AF_2, GPIO_PIN_13);
|
||
gpio_af_set(GPIOD, GPIO_AF_2, GPIO_PIN_14);
|
||
}
|
||
|
||
|
||
/*!
|
||
\brief PWM输出初始化
|
||
\param[in] none
|
||
\param[out] none
|
||
\retval none
|
||
\note LH @2022.07.20
|
||
*/
|
||
static void PWM_timer_cfg(void)
|
||
{//APB1总线时钟频率50MHz,APB2总线时钟频率100MHz----timer0,7
|
||
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 = 2;//分频系数,确定频率为25MHZ
|
||
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//计数模式,边缘对齐
|
||
timer_initpara.counterdirection = TIMER_COUNTER_UP;//计数方向
|
||
timer_initpara.period = 1999;//计数重装载值,确定周期,,该值可在任意时刻进行修改以改变周期,TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
|
||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;//时钟分频
|
||
timer_initpara.repetitioncounter = 0;//计数重复值
|
||
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(TIMER2,TIMER_CH_1,&timer_ocintpara);
|
||
timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);
|
||
|
||
timer_channel_output_config(TIMER3,TIMER_CH_0,&timer_ocintpara);
|
||
timer_channel_output_config(TIMER3,TIMER_CH_1,&timer_ocintpara);
|
||
timer_channel_output_config(TIMER3,TIMER_CH_2,&timer_ocintpara);
|
||
|
||
// CH0 configuration in PWM mode 0,duty cycle
|
||
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,0);//输出无效低电平,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(TIMER2,TIMER_CH_1,0);//输出无效低电平,TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
|
||
timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_PWM0);
|
||
timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
|
||
|
||
// CH0 configuration in PWM mode 0,duty cycle
|
||
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,0);//输出无效低电平,TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
|
||
timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
|
||
timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
|
||
|
||
// CH0 configuration in PWM mode 0,duty cycle
|
||
timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_0,0);//输出无效低电平,TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
|
||
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);
|
||
|
||
// CH0 configuration in PWM mode 0,duty cycle
|
||
timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_1,0);//输出无效低电平,TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
|
||
timer_channel_output_mode_config(TIMER3,TIMER_CH_1,TIMER_OC_MODE_PWM0);
|
||
timer_channel_output_shadow_config(TIMER3,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
|
||
|
||
// CH0 configuration in PWM mode 0,duty cycle
|
||
timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_2,0);//输出无效低电平,TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
|
||
timer_channel_output_mode_config(TIMER3,TIMER_CH_2,TIMER_OC_MODE_PWM0);
|
||
timer_channel_output_shadow_config(TIMER3,TIMER_CH_2,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);
|
||
|
||
// timer_interrupt_enable(TIMER2,TIMER_INT_UP);//定时器更新中断使能
|
||
// nvic_irq_enable(TIMER2_IRQn, 3U, 0U);
|
||
|
||
// timer_interrupt_enable(TIMER3,TIMER_INT_UP);//定时器更新中断使能
|
||
// nvic_irq_enable(TIMER3_IRQn, 3U, 1U);
|
||
}
|
||
|
||
/*!
|
||
\brief 定时器输出初始化,,初始化于速度模块
|
||
\param[in] none
|
||
\param[out] none
|
||
\retval none
|
||
\note LH @2022.07.20
|
||
*/
|
||
void bldc_pwm_init(void)
|
||
{
|
||
PWM_gpio_init();
|
||
|
||
PWM_timer_cfg();
|
||
|
||
OSTimeDlyHMSM(0u, 0u, 0u, 1u);
|
||
|
||
// //初始化定时器后,先不使能,仅当电机启动后使能,,防止CPU负荷过高
|
||
// timer_disable(TIMER3);
|
||
// timer_disable(TIMER2);
|
||
// timer_interrupt_disable(TIMER3,TIMER_INT_UP);//定时器更新中断使能
|
||
// timer_interrupt_disable(TIMER2,TIMER_INT_UP);//定时器更新中断使能
|
||
PWM_timer4_cfg();
|
||
}
|
||
|
||
|
||
|
||
//测试,计算电机速度
|
||
/*!
|
||
\brief 定时器中断里电机换向———水平和垂直
|
||
\param[in] none
|
||
\param[out] none
|
||
\retval none
|
||
\note LH @2022.07.20
|
||
*/
|
||
void PWM_timer4_cfg()
|
||
{//APB1总线时钟频率50MHz,APB2总线时钟频率100MHz----timer0,7
|
||
timer_parameter_struct timer_initpara;
|
||
// timer_oc_parameter_struct timer_ocintpara;
|
||
|
||
rcu_periph_clock_enable(RCU_TIMER4);
|
||
timer_struct_para_init(&timer_initpara);
|
||
timer_deinit(TIMER4);
|
||
|
||
// TIMER1 configuration
|
||
timer_initpara.prescaler = 9;//分频系数,确定频率为25MHZ
|
||
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//计数模式,边缘对齐
|
||
timer_initpara.counterdirection = TIMER_COUNTER_UP;//计数方向
|
||
timer_initpara.period = 999;//计数重装载值,确定周期,,该值可在任意时刻进行修改以改变周期,TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
|
||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;//时钟分频
|
||
timer_initpara.repetitioncounter = 0;//计数重复值
|
||
timer_init(TIMER4,&timer_initpara);
|
||
|
||
timer_auto_reload_shadow_enable(TIMER4);
|
||
timer_enable(TIMER4);
|
||
|
||
|
||
timer_interrupt_enable(TIMER4,TIMER_INT_UP);//定时器更新中断使能
|
||
nvic_irq_enable(TIMER4_IRQn, 2U, 1U);
|
||
// BSP_IntVectSet(66,TIMER4_IRQHandler);
|
||
// BSP_IntEn(66);
|
||
|
||
}
|
||
|
||
|
||
//int hall_count,hall_count_last;//=0,counter_diff;
|
||
//float motor_speed;//电机转速
|
||
//
|
||
//void hall_irq_process()
|
||
//{
|
||
// if(abs(hall_count_last-g_ptz.vert_l6235d.l6235d_hall_h1_count) >= 1)
|
||
// {
|
||
// motor_speed = (144.0 / hall_count * 8000.0) / 6.0;//单位转每分
|
||
//
|
||
//// g_ptz.vert_motor_speed_hall_actual = motor_speed;
|
||
// hall_count_last=g_ptz.vert_l6235d.l6235d_hall_h1_count;
|
||
// hall_count=0;
|
||
//}}
|
||
|
||
|
||
#endif
|
||
|