?以TIMER7為例,對定時器函數進行封裝
注意事項:GD32中TIMER7是高級定時器,相關詳細請參考上一篇文章。
?main.c
//main.c#include "gd32f4xx.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "Usart0.h"
#include "Timer.h"float duty0 = 0;
float duty1 = 0;
float duty2 = 0;void Usart0_on_recv(uint8_t* data, uint32_t len) {printf("recv: %s\r\n", data);if(data[0] == 0x00) {// T2 ch0 +Timer7_ch0_update(duty0++);} else if(data[0] == 0x01) {// T2 ch0 -Timer7_ch0_update(duty0--);} else if(data[0] == 0x02) {// T2 ch1 +Timer7_ch1_update(duty1++);} else if(data[0] == 0x03) {// T2 ch1 -Timer7_ch1_update(duty1--);} else if(data[0] == 0x04) {// T2 ch2 +Timer7_ch2_update(duty2++);} else if(data[0] == 0x05) {// T2 ch2 -Timer7_ch2_update(duty2--);}
}int main(void)
{systick_config();Usart0_init();Timer_init();while(1) {}
}
關心的內容
該代碼中,data[0] == 0x00
?等等是用來檢查通過 USART0 (串口0) 接收到的數據的第一個字節是否為?0x00
。這個?data
?數組是通過?Usart0_on_recv
?函數的參數傳入的,而這個函數看起來是一個串口接收回調函數。?
Timer7.c?
//Timer7.c#include "Timer7.h"
#if USE_TIMER_7void Timer7_init() {// T7 ch0 ch1 ch2 ch3// PC6 PC7 PC8//哪個定時器uint32_t timer_rcu = RCU_TIMER7;uint32_t timer = TIMER7;// uint32_t timer_rcu_psc = RCU_TIMER_PSC_MUL4;// 倍頻//定時器周期和分頻uint32_t timer_param_psc = TIMER7_PSC;uint32_t timer_param_period = TIMER7_PERIOD;/******************** GPIO *********************************/// // PC6 PC7 PC8
//////////// ch0
#if TIMER7_CH0_ENABLE// P和N
#if TIMER7_CH0_P_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH0_P_PORT_RCU);gpio_mode_set(TIMER7_CH0_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH0_P_PIN);gpio_output_options_set(TIMER7_CH0_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH0_P_PIN);gpio_af_set(TIMER7_CH0_P_PORT, TIMER7_CH0_P_AF, TIMER7_CH0_P_PIN);
#endif
#if TIMER7_CH0_N_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH0_N_PORT_RCU);gpio_mode_set(TIMER7_CH0_N_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH0_N_PIN);gpio_output_options_set(TIMER7_CH0_N_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH0_N_PIN);gpio_af_set(TIMER7_CH0_N_PORT, TIMER7_CH0_N_AF, TIMER7_CH0_N_PIN);
#endif
#endif//////////// ch1
#if TIMER7_CH1_ENABLE// P和N
#if TIMER7_CH1_P_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH1_P_PORT_RCU);gpio_mode_set(TIMER7_CH1_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH1_P_PIN);gpio_output_options_set(TIMER7_CH1_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH1_P_PIN);gpio_af_set(TIMER7_CH1_P_PORT, TIMER7_CH1_P_AF, TIMER7_CH1_P_PIN);
#endif
#if TIMER7_CH1_N_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH1_N_PORT_RCU);gpio_mode_set(TIMER7_CH1_N_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH1_N_PIN);gpio_output_options_set(TIMER7_CH1_N_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH1_N_PIN);gpio_af_set(TIMER7_CH1_N_PORT, TIMER7_CH1_N_AF, TIMER7_CH1_N_PIN);
#endif
#endif
//////////// ch2
#if TIMER7_CH2_ENABLE// P和N
#if TIMER7_CH2_P_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH2_P_PORT_RCU);gpio_mode_set(TIMER7_CH2_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH2_P_PIN);gpio_output_options_set(TIMER7_CH2_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH2_P_PIN);gpio_af_set(TIMER7_CH2_P_PORT, TIMER7_CH2_P_AF, TIMER7_CH2_P_PIN);
#endif
#if TIMER7_CH2_N_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH2_N_PORT_RCU);gpio_mode_set(TIMER7_CH2_N_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH2_N_PIN);gpio_output_options_set(TIMER7_CH2_N_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH2_N_PIN);gpio_af_set(TIMER7_CH2_N_PORT, TIMER7_CH2_N_AF, TIMER7_CH2_N_PIN);
#endif
#endif
//////////// ch3
#if TIMER7_CH3_ENABLE// P和N
#if TIMER7_CH3_P_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH3_P_PORT_RCU);gpio_mode_set(TIMER7_CH3_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH3_P_PIN);gpio_output_options_set(TIMER7_CH3_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH3_P_PIN);gpio_af_set(TIMER7_CH3_P_PORT, TIMER7_CH3_P_AF, TIMER7_CH3_P_PIN);
#endif
#if TIMER7_CH3_N_ENABLE// gpio初始化 ch0rcu_periph_clock_enable(TIMER7_CH3_N_PORT_RCU);gpio_mode_set(TIMER7_CH3_N_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER7_CH3_N_PIN);gpio_output_options_set(TIMER7_CH3_N_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER7_CH3_N_PIN);gpio_af_set(TIMER7_CH3_N_PORT, TIMER7_CH3_N_AF, TIMER7_CH3_N_PIN);
#endif
#endif/************************ timer ************************/// 初始化定時器相關配置rcu_periph_clock_enable(timer_rcu);timer_deinit(timer);timer_parameter_struct tps;timer_struct_para_init(&tps);//配置默認值// 根據實際需求去修改值tps.prescaler = timer_param_psc;tps.period = timer_param_period; // 周期計數(從0開始計數)timer_init(timer, &tps);/*************** timer CH 配置 **********************/timer_oc_parameter_struct tops;// 定時器通道配置 CH0
//////////////// ch0
#if TIMER7_CH0_ENABLEtimer_channel_output_struct_para_init(&tops);// N和P
#if TIMER7_CH0_P_ENABLEtops.outputstate = TIMER_CCX_ENABLE;tops.ocpolarity = TIMER_OC_POLARITY_HIGH;
#endif
#if TIMER7_CH0_N_ENABLEtops.outputnstate = TIMER_CCXN_ENABLE;tops.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
#endiftimer_channel_output_config(timer, TIMER_CH_0 , &tops);timer_channel_output_mode_config(timer, TIMER_CH_0, TIMER_OC_MODE_PWM0);timer_channel_output_pulse_value_config(timer, TIMER_CH_0, 0);//pwm值
#endif
//////////////////////////////////////////
#if TIMER7_CH1_ENABLE// 定時器通道配置 CH1timer_channel_output_struct_para_init(&tops);// N和P
#if TIMER7_CH1_P_ENABLEtops.outputstate = TIMER_CCX_ENABLE;
#endif
#if TIMER7_CH1_N_ENABLEtops.outputnstate = TIMER_CCXN_ENABLE;
#endiftimer_channel_output_config(timer, TIMER_CH_1 , &tops);timer_channel_output_mode_config(timer, TIMER_CH_1, TIMER_OC_MODE_PWM0);timer_channel_output_pulse_value_config(timer, TIMER_CH_1, 0);//pwm值
#endif
#if TIMER7_CH2_ENABLE// 定時器通道配置 CH2timer_channel_output_struct_para_init(&tops);// N和P
#if TIMER7_CH2_P_ENABLEtops.outputstate = TIMER_CCX_ENABLE;
#endif
#if TIMER7_CH2_N_ENABLEtops.outputnstate = TIMER_CCXN_ENABLE;
#endiftimer_channel_output_config(timer, TIMER_CH_2 , &tops);timer_channel_output_mode_config(timer, TIMER_CH_2, TIMER_OC_MODE_PWM0);timer_channel_output_pulse_value_config(timer, TIMER_CH_2, 0);//pwm值
#endif
#if TIMER7_CH3_ENABLEtimer_channel_output_struct_para_init(&tops);
#if TIMER7_CH3_P_ENABLEtops.outputstate = TIMER_CCX_ENABLE;
#endif
#if TIMER7_CH3_N_ENABLEtops.outputnstate = TIMER_CCXN_ENABLE;
#endiftimer_channel_output_config(timer, TIMER_CH_3 , &tops);timer_channel_output_mode_config(timer, TIMER_CH_3, TIMER_OC_MODE_PWM0);timer_channel_output_pulse_value_config(timer, TIMER_CH_3, 0);//pwm值
#endif//只針對高級定時器,break,保護電路,打開break電路timer_break_parameter_struct tbps;timer_break_struct_para_init(&tbps);tbps.breakpolarity = TIMER_BREAK_POLARITY_HIGH;tbps.outputautostate = TIMER_OUTAUTO_ENABLE;tbps.breakstate = TIMER_BREAK_ENABLE;timer_break_config(timer, &tbps);timer_break_enable(timer);timer_enable(timer);
}#if TIMER7_CH0_ENABLE
void Timer7_ch0_update(float duty){uint32_t timer = TIMER7;if(duty > 100) duty = 100;if(duty < 0) duty = 0;uint32_t count = TIMER7_PERIOD + 1;uint32_t pulse = count * duty / 100 - 1;//計數值timer_channel_output_pulse_value_config(timer, TIMER_CH_0, pulse);//pwm值
}
#endif#if TIMER7_CH1_ENABLE
void Timer7_ch1_update(float duty){uint32_t timer = TIMER7;if(duty > 100) duty = 100;if(duty < 0) duty = 0;uint32_t count = TIMER7_PERIOD + 1;uint32_t pulse = count * duty / 100 - 1;//計數值timer_channel_output_pulse_value_config(timer, TIMER_CH_1, pulse);//pwm值
}
#endif#if TIMER7_CH2_ENABLE
void Timer7_ch2_update(float duty){uint32_t timer = TIMER7;if(duty > 100) duty = 100;if(duty < 0) duty = 0;uint32_t count = TIMER7_PERIOD + 1;uint32_t pulse = count * duty / 100 - 1;//計數值timer_channel_output_pulse_value_config(timer, TIMER_CH_2, pulse);//pwm值
}
#endif#if TIMER7_CH3_ENABLE
void Timer7_ch3_update(float duty){uint32_t timer = TIMER7;if(duty > 100) duty = 100;if(duty < 0) duty = 0;uint32_t count = TIMER7_PERIOD + 1;uint32_t pulse = count * duty / 100 - 1;//計數值timer_channel_output_pulse_value_config(timer, TIMER_CH_3, pulse);//pwm值
}
#endif#endif
關心的內容
?TIMER7高級定時器,四通道
這里的引腳分為P和N類型,不同引腳要配置不同的輸出狀態
所以封裝過程中需要分類
P、N配置
?Timer7.h
//Timer7.h#ifndef __TIMER_7_H__
#define __TIMER_7_H__#include "Timer_config.h"#if USE_TIMER_7// 1. 基本timer,有中斷函數
// 2. timer, 控制多個通道的duty變化
// 3. timer, 控制周期頻率動態變化
// 4. ARM中有很多timervoid Timer7_init();#if TIMER7_CH0_ENABLE
void Timer7_ch0_update(float duty);
#endif#if TIMER7_CH1_ENABLE
void Timer7_ch1_update(float duty);
#endif#if TIMER7_CH2_ENABLE
void Timer7_ch2_update(float duty);
#endif#if TIMER7_CH3_ENABLE
void Timer7_ch3_update(float duty);
#endif#endif
#endif
?Timer_config.h
//Timer_config.h#ifndef __TIMER_CONFIG_H__
#define __TIMER_CONFIG_H__#include "gd32f4xx.h"
#include "systick.h"#define USE_TIMER_0 0
#define USE_TIMER_1 0
#define USE_TIMER_2 0
#define USE_TIMER_3 0
#define USE_TIMER_4 0
#define USE_TIMER_5 0
#define USE_TIMER_6 0
#define USE_TIMER_7 1
#define USE_TIMER_8 0
#define USE_TIMER_9 0
#define USE_TIMER_10 0
#define USE_TIMER_11 0
#define USE_TIMER_12 0
#define USE_TIMER_13 0/*************** Timer 0 *************/
/*************** Timer 1 *************/
/*************** Timer 2 *************/
#if USE_TIMER_2
// 0: output 1: base timer 2: input
#define TIMER2_FUNC 0#if TIMER2_FUNC == 0
#define TIMER2_CH0_ENABLE 1
#define TIMER2_CH1_ENABLE 1
#define TIMER2_CH2_ENABLE 1
#define TIMER2_CH3_ENABLE 0
#endif/**
* TIMER2_PSC (10 - 1)
* TIMER2_PERIOD (SystemCoreClock / 10000 - 1)
* 表示10秒執行10000次
*/
#define TIMER2_PSC (1 - 1)
#define TIMER2_PERIOD (SystemCoreClock / 10000 - 1)#if TIMER2_CH0_ENABLE
// PA6(AF2),PB4(AF2),PC6(AF2)
#define TIMER2_CH0_PORT_RCU RCU_GPIOC
#define TIMER2_CH0_PORT GPIOC
#define TIMER2_CH0_PIN GPIO_PIN_6
#define TIMER2_CH0_AF GPIO_AF_2
#endif#if TIMER2_CH1_ENABLE
//PA7,PB5,PC7
#define TIMER2_CH1_PORT_RCU RCU_GPIOC
#define TIMER2_CH1_PORT GPIOC
#define TIMER2_CH1_PIN GPIO_PIN_7
#define TIMER2_CH1_AF GPIO_AF_2
#endif#if TIMER2_CH2_ENABLE
//PB0,PC8
#define TIMER2_CH2_PORT_RCU RCU_GPIOC
#define TIMER2_CH2_PORT GPIOC
#define TIMER2_CH2_PIN GPIO_PIN_8
#define TIMER2_CH2_AF GPIO_AF_2
#endif#if TIMER2_CH3_ENABLE
//PB1,PC9
#define TIMER2_CH3_PORT_RCU RCU_GPIOC
#define TIMER2_CH3_PORT GPIOC
#define TIMER2_CH3_PIN GPIO_PIN_6
#define TIMER2_CH3_AF GPIO_AF_2
#endif
#endif/***************************************/
/*************** Timer 7 **************/
/**************************************/
#if USE_TIMER_7
// 0: output 1: base timer 2: input
#define TIMER7_FUNC 0#if TIMER7_FUNC == 0
#define TIMER7_CH0_ENABLE 1
#define TIMER7_CH1_ENABLE 1
#define TIMER7_CH2_ENABLE 1
#define TIMER7_CH3_ENABLE 0
#endif#if TIMER7_CH0_ENABLE
#define TIMER7_CH0_P_ENABLE 1
#define TIMER7_CH0_N_ENABLE 1
#endif#if TIMER7_CH1_ENABLE
#define TIMER7_CH1_P_ENABLE 0
#define TIMER7_CH1_N_ENABLE 1
#endif#if TIMER7_CH2_ENABLE
#define TIMER7_CH2_P_ENABLE 0
#define TIMER7_CH2_N_ENABLE 1
#endif#if TIMER7_CH3_ENABLE
#define TIMER7_CH3_P_ENABLE 0
#define TIMER7_CH3_N_ENABLE 1
#endif/**
* TIMER2_PSC (10 - 1)
* TIMER2_PERIOD (SystemCoreClock / 10000 - 1)
* 表示10秒執行10000次
*/
#define TIMER7_PSC (1 - 1)
#define TIMER7_PERIOD (SystemCoreClock / 10000 - 1)//////////////// ch0
#if TIMER7_CH0_P_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH0_P_PORT_RCU RCU_GPIOC
#define TIMER7_CH0_P_PORT GPIOC
#define TIMER7_CH0_P_PIN GPIO_PIN_6
#define TIMER7_CH0_P_AF GPIO_AF_3
#endif#if TIMER7_CH0_N_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH0_N_PORT_RCU RCU_GPIOA
#define TIMER7_CH0_N_PORT GPIOA
#define TIMER7_CH0_N_PIN GPIO_PIN_5
#define TIMER7_CH0_N_AF GPIO_AF_3
#endif
////////////////////////////////////////////////////////// ch1
#if TIMER7_CH1_P_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH1_P_PORT_RCU RCU_GPIOC
#define TIMER7_CH1_P_PORT GPIOC
#define TIMER7_CH1_P_PIN GPIO_PIN_6
#define TIMER7_CH1_P_AF GPIO_AF_3
#endif#if TIMER7_CH1_N_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH1_N_PORT_RCU RCU_GPIOA
#define TIMER7_CH1_N_PORT GPIOA
#define TIMER7_CH1_N_PIN GPIO_PIN_7
#define TIMER7_CH1_N_AF GPIO_AF_3
#endif
////////////////////////////////////////////////////////// ch2
#if TIMER7_CH2_P_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH2_P_PORT_RCU RCU_GPIOC
#define TIMER7_CH2_P_PORT GPIOC
#define TIMER7_CH2_P_PIN GPIO_PIN_6
#define TIMER7_CH2_P_AF GPIO_AF_3
#endif#if TIMER7_CH2_N_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH2_N_PORT_RCU RCU_GPIOA
#define TIMER7_CH2_N_PORT GPIOA
#define TIMER7_CH2_N_PIN GPIO_PIN_7
#define TIMER7_CH2_N_AF GPIO_AF_3
#endif
////////////////////////////////////////////////////////// ch3
#if TIMER7_CH3_P_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH3_P_PORT_RCU RCU_GPIOC
#define TIMER7_CH3_P_PORT GPIOC
#define TIMER7_CH3_P_PIN GPIO_PIN_6
#define TIMER7_CH3_P_AF GPIO_AF_3
#endif#if TIMER7_CH3_N_ENABLE
// PA7(AF3),PB4(AF2),PC6(AF2)
#define TIMER7_CH3_N_PORT_RCU RCU_GPIOA
#define TIMER7_CH3_N_PORT GPIOA
#define TIMER7_CH3_N_PIN GPIO_PIN_7
#define TIMER7_CH3_N_AF GPIO_AF_3
#endif
//////////////////////////////////////////#endif
/*********************************************************/#endif
以上是以高級定時器TIMER7和通用定時器TIMER2為例封裝配置,可參考此流程封裝配置GD32F4其余剩下的12個定時器。