RISC-V GD32VF103(五)定时器中断

#include "gd32vf103.h"
#include "systick.h"
#include 


void LED_Init(void)
{
    rcu_periph_clock_enable(RCU_GPIOC);//使能GPIOC时钟
    gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);//PC13--推挽输出--速度50MHZ
    gpio_bit_write(GPIOC, GPIO_PIN_13, 1);//PC13输出高电平,LED灭
} 
void Reversal_leve(void)
{
    gpio_bit_write(GPIOC, GPIO_PIN_13, (1-gpio_output_bit_get(GPIOC, GPIO_PIN_13)));//反转电平
}


void TIMER1_Init(void)
{
    eclic_global_interrupt_enable();//使能全局中断
    eclic_set_nlbits(ECLIC_GROUP_LEVEL3_PRIO1);//分组
    eclic_irq_enable(TIMER1_IRQn, 1 ,0);//使能TIMER1中断请求,设置优先级

    rcu_periph_clock_enable(RCU_TIMER1);//使能TIMER1时钟
    timer_deinit(TIMER1);//重置TIMER1
    timer_parameter_struct timer1;//定义结构体
    timer1.prescaler = 10799;//预装载值
    timer1.alignedmode = TIMER_COUNTER_EDGE;//边沿对齐模式
    timer1.counterdirection = TIMER_COUNTER_UP;//向上计数
    timer1.period = 9999;//周期
    timer1.clockdivision = TIMER_CKDIV_DIV1;//时钟分频
    timer1.repetitioncounter = 0;//重复值,一般赋值为0
    timer_init(TIMER1, &timer1);//初始化TIMER01 
    timer_interrupt_enable(TIMER1, TIMER_INT_UP);//启用TIMER1中断
    timer_enable(TIMER1);//使能TIMER1
}


int main(void)
{
    LED_Init();
    TIMER1_Init();
    while(1)
    {

    }
}


void TIMER1_IRQHandler(void)
 {
    if(SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP))//判断是否产生中断
    {
        timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);//清除中断标志位
        Reversal_leve();
    }
}

 

GD32VF03的晶振为108Mhz

定时器溢出时间计算方法:T=(周期+1)*(预装载值+1))/晶振频率(注意单位换算)

实验效果:LED灯间隔1秒亮和灭

实验代码,未进行优化和封装。

你可能感兴趣的:(RISC-V,GD32VF103,单片机爱好者)