一般功能:
比如高级定时器0,最大是108M,108M/65536/65536=39.76823……(最大定时时间)
如果你觉得时间不够,还可以使用定时器级联功能,三个定时器级联,就可以定时34万亿年。
高级/通用/基本
GD32F103只有三个定时器,一个高级Timer0,两个通用Timer1、2
计数模式
向上,向下,中央对齐。
其中中央对齐可以用在SVPWM控制算法中,产生三角载波。
影子寄存器/缓冲寄存器
预分频寄存器与自动重载寄存器都是有影子寄存器的,只有在下一个周期你更改后的值才会生效。
问题1
定时器-基本定时功能
#include "gd32f10x.h" // Device header
void Timer_Init(void)
{
/* TIMER2 configuration */
timer_parameter_struct Timer_initpara;
rcu_periph_clock_enable(RCU_TIMER2); //使能RCU_TIMER2时钟
timer_deinit(TIMER1);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&Timer_initpara);
Timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
Timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
Timer_initpara.counterdirection = TIMER_COUNTER_UP;
Timer_initpara.period = 10000 - 1;
Timer_initpara.prescaler = 10800 - 1;
Timer_initpara.repetitioncounter = 0;
timer_init(TIMER2, &Timer_initpara);
//手动把更新中断标志位清除一下,避免刚初始化完成就进入中断
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
/* TIMER2 channel control update interrupt enable */
timer_interrupt_enable(TIMER2, TIMER_INT_UP);
timer_enable(TIMER2);
/* enable and set key EXTI interrupt to the specified priority */
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(TIMER2_IRQn, 1U, 1U);
}
//void TIMER2_IRQHandler(void)
//{
// if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP) == SET)
// {
//
// timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
// }
//}
#include "gd32f10x.h"
#include "gd32f10x_libopt.h"
#include "systick.h"
#include "OLED.h"
#include "Timer.h"
uint16_t Num;
int main(void)
{
OLED_Init();
Timer_Init();
OLED_ShowString(1, 1, "Num:");
while (1)
{
OLED_ShowNum(1, 5, Num, 5);
OLED_ShowNum(2, 5, timer_counter_read(TIMER2), 5);
OLED_ShowNum(3, 5, timer_prescaler_read(TIMER2), 5);
}
}
void TIMER2_IRQHandler(void)
{
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP) == SET)
{
Num++;
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
}
}
定时器-外部时钟功能
外部时钟,配置输入捕获功能IC
void Timer_Init(void) { / TIMER1 configuration / timer_parameter_struct Timer_initpara; timer_ic_parameter_struct timer_icinitpara;
rcu_periph_clock_enable(RCU_TIMER1); //使能RCU_TIMER1时钟
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
timer_deinit(TIMER1);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&Timer_initpara);
Timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
Timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
Timer_initpara.counterdirection = TIMER_COUNTER_UP;
Timer_initpara.period = 10 - 1;
Timer_initpara.prescaler = 1 - 1;
Timer_initpara.repetitioncounter = 0;
timer_init(TIMER1, &Timer_initpara);
/* TIMER1 CH0 input capture configuration */
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x00;
timer_input_capture_config(TIMER1, TIMER_CH_0, &timer_icinitpara);
/* slave mode selection : TIMER1 */
/* TIMER1 input trigger : external trigger connected to CI0 */
timer_input_trigger_source_select(TIMER1,TIMER_SMCFG_TRGSEL_CI0FE0);
timer_slave_mode_select(TIMER1,TIMER_SLAVE_MODE_EXTERNAL0);
//手动把更新中断标志位清除一下,避免刚初始化完成就进入中断
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
/* TIMER2 channel control update interrupt enable */
timer_interrupt_enable(TIMER1, TIMER_INT_UP);
timer_enable(TIMER1);
/* enable and set key EXTI interrupt to the specified priority */
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(TIMER1_IRQn, 1U, 1U);
}
//void TIMER2_IRQHandler(void) //{
// if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP) == SET)
// {
//
// timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
// }
//}
<a name="MNfPo"></a>
# 输出比较功能-PWM
输出比较可以通过比较CNT与CCR寄存器值的关系,来对输出电平进行置1、置0或翻转的操作,用于输出一定频率和占空比的PWM波形。<br />每个高级定时器和通用定时器都拥有4个输出比较通道。<br />高级定时器的前3个通道额外拥有死区生成和互补输出的功能。<br />死区时间的插入,确保了通道互补的两路信号不会同时有效。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/29409159/1666853764785-dc39752b-fda6-4d2f-aab3-5bae49fdad65.png#clientId=uc25cbe92-31ee-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=524&id=u09561e74&margin=%5Bobject%20Object%5D&name=image.png&originHeight=524&originWidth=1172&originalType=binary&ratio=1&rotation=0&showTitle=false&size=39721&status=done&style=none&taskId=u7443f5fa-7a69-4485-8e32-ad3b8e082d6&title=&width=1172)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/29409159/1666853818753-771a6119-b683-4133-b664-38fb1b28eedf.png#clientId=uc25cbe92-31ee-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=326&id=uf82c0407&margin=%5Bobject%20Object%5D&name=image.png&originHeight=326&originWidth=576&originalType=binary&ratio=1&rotation=0&showTitle=false&size=6066&status=done&style=none&taskId=u93f9b0aa-bc12-4b94-9b98-deb13396ff4&title=&width=576)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/29409159/1666853829179-efd9dd71-38da-4307-8b8a-f46bd9f9a223.png#clientId=uc25cbe92-31ee-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=146&id=uacaf90ed&margin=%5Bobject%20Object%5D&name=image.png&originHeight=146&originWidth=1161&originalType=binary&ratio=1&rotation=0&showTitle=false&size=19464&status=done&style=none&taskId=u85fbd9d2-5706-419b-a15d-499aea0f88b&title=&width=1161)
<a name="nbIIe"></a>
## 呼吸灯
```c
#include "gd32f10x.h" // Device header
void PWM_Init(void)
{
//输出比较与定时器初始化结构体
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
//时钟 TIMER1 GPIOA
rcu_periph_clock_enable(RCU_TIMER1);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
//PA1初始化 复用推挽输出
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
timer_deinit(TIMER1);
/* TIMER1 参数配置 */
timer_initpara.prescaler = 10800 - 1; //PSC
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 100 - 1; //ARR
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER1,&timer_initpara);
//输出比较参数配置
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;//高极性,不反转,REF直接输出
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;//输出使能
timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
/* CH1 configuration in PWM mode1,duty cycle 0% */
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,0);//CCR 占空比
timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);//PWM模式0 模式1
timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER1);
/* auto-reload preload enable */
timer_enable(TIMER1);
}
void PWM_SetCompare1(uint16_t Compare)
{
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,Compare);
}
#include "gd32f10x.h"
#include "gd32f10x_libopt.h"
#include "systick.h"
#include "OLED.h"
#include "Timer.h"
#include "PWM.h"
uint8_t i;
int main(void)
{
OLED_Init();
PWM_Init();
OLED_ShowString(1, 1, "Duty: %");
while (1)
{
for (i = 0; i <= 100; i++)
{
PWM_SetCompare1(i);
Delay_ms(10);
OLED_ShowNum(1, 6, i, 3);
}
for (i = 0; i <= 100; i++)
{
PWM_SetCompare1(100 - i);
Delay_ms(10);
OLED_ShowNum(1, 6, i, 3);
}
}
}
输出比较-互补、死区时间、刹车
1.高级定时器只有 TIMER0 四通道定时器
通过配置互补通道PB13 PA8
配置刹车PB12
#include "gd32f10x.h" // Device header
void PWM_Init(void)
{
//输出比较与定时器初始化结构体 刹车结构体
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
timer_break_parameter_struct timer_breakpara;
//时钟 TIMER0 GPIOA GPIOB
rcu_periph_clock_enable(RCU_TIMER0);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_AF);
//PA1初始化 复用推挽输出
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
//PB13 互补通道初始化 复用推挽输出
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
/* configure PB12(TIMER0 BKIN) as alternate function */
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
timer_deinit(TIMER0);
/* TIMER0 参数配置 */
timer_initpara.prescaler = 108 - 1; //PSC
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 600 - 1; //ARR
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0,&timer_initpara);
//输出比较参数配置
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;//输出使能
timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;//互补输出使能
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;//高极性,不反转,REF直接输出
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH;
//通道选择
timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
/* CH0 configuration in PWM mode1,duty cycle 0% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,0);//CCR 占空比
timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);//PWM模式0 模式1
timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
/* automatic output enable, break, dead time and lock configuration*/
timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ;
timer_breakpara.deadtime = 164;
timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
timer_break_config(TIMER0,&timer_breakpara);
/* auto-reload preload enable */
// timer_auto_reload_shadow_enable(TIMER0);
/* TIMER0 primary output function enable */
timer_primary_output_config(TIMER0,ENABLE);
/* TIMER0 channel control update interrupt enable */
timer_interrupt_enable(TIMER0,TIMER_INT_CMT);
/* TIMER0 break interrupt disable */
timer_interrupt_disable(TIMER0,TIMER_INT_BRK);
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(TIMER0_TRG_CMT_IRQn, 0, 1);
/* TIMER0 counter enable */
timer_enable(TIMER0);
}
void PWM_SetCompare1(uint16_t Compare)
{
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,Compare);
}
互补输出:
死区时间:
刹车功能