Use TIMER2 timer to generate 1ms timing interrupt to drive onboard LED4 to flash, and also drive PA6 pin to generate the same interrupt and use logic analyzer sampling to verify.
1. Experimental resources
1. GD32L233C-START;
2. Keil V5.36.0.0;
3. Logic analyzer + Logic 2.3.45
2. Knowledge Preparation
The timer resources of GD32L233C are divided into general L0 level (TIMER1, TIMER2); general L1 level (TIMER8, TIMER11); basic level (TIMER5, TIMER6), all of which are 16-bit counters. Among them, the two timers of the general L0 level have more functions, and the two timers of the basic level have fewer functions, which can be selected according to specific applications.
This experiment uses the general L0 level TIMER2 timer. Although it is a general-level timer, its functions are still very powerful and there is no problem for most applications.
The use of timers cannot avoid counting clocks. In the experiment, the counting clock of TIMER2 uses the internal clock source "CK_TIMER". Let's take a look at the source of "CK_TIMER":
From the clock tree above, we can see that "CK_TIMER" comes from the three optional clocks: internal 16MHz, internal 16MHz and external 4~32MHz clocks. After being processed by "CK_PLL", the "CK_SYS" system clock is generated, and after being divided by "AHB", a "CK_AHB" up to 64MHz can be generated. This clock is distributed to various on-chip peripherals by the system. This also includes the "CK_TIMER" after being pre-divided by "TIMER".
3. Experimental Content
1. Hardware connection
The interrupt of TIMER2 is used to change the level of GPIO pins. PA6 and PA7 are selected in the experiment. PA6 is used to connect to the logic analyzer; PA7 is used to drive the onboard LED1 to observe the working status. In addition, PA6 and PA7 are also the two output channels of TIMER2 (TIMER2_CH0, TIMER2_CH1), which makes it easy to conduct related experiments on this basis.
2. Program implementation
The main purpose is to use TIMER2 to count the "CK_TIMER" clock and generate a 1ms interrupt.
void TIMER_Config(void)
{
GPIO_Config();
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
/*
定时器时钟计数频率 = 64MHz/(prescaler+1) = 64MHz/(63+1)=1MHz
定时时间 = 定时器时钟计数频率/(period+1) = 1HMz/(999+1) = 1KHz = 0.001s = 1ms
*/
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 63; //时钟预分频系数
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//边缘对齐
timer_initpara.counterdirection = TIMER_COUNTER_UP; //向上计数
timer_initpara.period = 999; //重载计数值
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER2, &timer_initpara);
timer_auto_reload_shadow_enable(TIMER2);//使能TIMER2重装载
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);//清除TIMER2向上计数中断标志位
timer_interrupt_enable(TIMER2, TIMER_INT_UP);//使能TIMER2向上计数中断
nvic_irq_enable(TIMER2_IRQn, 0);//配置TIMER2中断
timer_enable(TIMER2);//使能TIMER2
}
After the counting clock is divided by 64 in the program, the actual counting clock is: timer clock counting frequency = 64MHz/(prescaler+1) = 64MHz/(63+1)=1MHz; the reload counter is configured to "999", the timing time = timer clock counting frequency/(period+1) = 1HMz/(999+1) = 1KHz = 0.001s = 1ms.
In the TIMER2 interrupt program, adjust the LED flip time to 100ms to facilitate observation of the working status of LED1:
void TIMER2_IRQHandler(void)
{
static uint8_t num=0;
if(SET == timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP))
{
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);//清除向上计数中断标志位
num++;
if(num==100)
{
num=0;
gpio_bit_toggle(LED1_PORT, LED1_PIN);//工作指示
gpio_bit_toggle(GPIOA, GPIO_PIN_6);//逻辑分析仪测试探针
}
}
}
3. Experimental results
4. Attachments
TIMER.rar
(603.71 KB, downloads: 12)
|