timer.c
#include "timer.h"
#include "gd32f10x.h"
#include "usart0.h"
#include "systick.h"
#include "led.h"
extern unsigned int seconds;
void timer5_init(void)
{
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER5);
timer_deinit(TIMER5);
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 11999; // 主频率120M
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER5, &timer_initpara);
timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER5, TIMER_INT_UP);
timer_auto_reload_shadow_enable(TIMER5);
timer_enable(TIMER5);
nvic_irq_enable(TIMER5_IRQn, 2, 3); // 配置中断优先级
}
void TIMER5_IRQHandler(void)
{
if(timer_interrupt_flag_get(TIMER5, TIMER_INT_FLAG_UP))
{
timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
if(seconds==100)
{
seconds=0;
led1=!led1;
}
seconds++;
}
}
void timer6_init(void)
{
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER6);
timer_deinit(TIMER6);
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 11999; // 主频率120M
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER6, &timer_initpara);
timer_interrupt_flag_clear(TIMER6, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER6, TIMER_INT_UP);
timer_enable(TIMER6);
nvic_irq_enable(TIMER6_IRQn, 0, 1);
}
void TIMER6_IRQHandler(void)
{
if(timer_interrupt_flag_get(TIMER6, TIMER_INT_FLAG_UP))
{
timer_interrupt_flag_clear(TIMER6, TIMER_INT_FLAG_UP);
if(seconds==100)
{
seconds=0;
led2=!led2;
}
seconds++;
}
}
main.c
#include "main.h"
unsigned int seconds;
int main(void)
{
rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV1);//AHB主频是1分频
systick_config();//系统主频108MHZ,采用外部晶振,由两个宏决定(__SYSTEM_CLOCK_108M_PLL_HXTAL与HXTAL_VALUE)
LED_Init();
USART0_init(9600U); //串口0 初始化函数
timer5_init();
timer6_init();
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
while(1)
{
}
} |