这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » 【G32R501 Micro-EVB开发板】+04调试定时器功能

共2条 1/1 1 跳转至

【G32R501 Micro-EVB开发板】+04调试定时器功能

高工
2026-08-20 16:44:49     打赏
一:定时器的基本知识

Cortex-M52集成3个定时器,分为TMR0/1/2,如果TMR2不用于实时操作系统(RTOS)中,那么三个定时器都可以用于用户应用程序中,否则,只有TMR0和TMR1可以用于应用程序中。

二:基本特性:

(1)定时器中断信号

     TMRINTO、TMRINT1、TMRINT2

(2)可设置32位定时器周期并存储当前计数值

(3)16位定时器可对时钟信号预分频并存储预分频计数值

三:软件的代码编写流程:

1:使能定时器时钟。

2:初始化定时器,配置ARR,PSC。

3:开启定时器中断,配置NVIC。

4:使能定时器。

5: 编写中断服务函数。

四:软件代码:

4.1 :定时器初始化底层函数

void configCPUTimer(uint32_t cpuTimer, float freq, float period)
{
    uint32_t temp;
    // Initialize timer period:
    //
    temp = (uint32_t)((freq / 1000000) * period);
    TMR_setPeriod(cpuTimer, temp);
    //
    // Set pre-scale counter to divide by 1 (SYSCLKOUT):
    //
    TMR_setPreScaler(cpuTimer, 0);
    //
    // Initializes timer control register. The timer is stopped, reloaded,
    // free run enabled, and interrupt enabled.
    //
    TMR_stopTimer(cpuTimer);
    TMR_reloadTimerCounter(cpuTimer);
    TMR_setEmulationMode(cpuTimer, TMR_EMULATIONMODE_RUNFREE);
    TMR_enableInterrupt(cpuTimer);
    //
    // Resets interrupt counters for the three cpuTimers
    //
    if (cpuTimer == TMR0_BASE)
    {
        cpuTimer0IntCount = 0;
    }
    else if(cpuTimer == TMR1_BASE)
    {
        cpuTimer1IntCount = 0;
    }
    else if(cpuTimer == TMR2_BASE)
    {
        cpuTimer2IntCount = 0;
    }
}

4.2 配置定时器的中断频率,10ms进一次中断

    Interrupt_register(INT_TIMER0, &INT_TIMER0_IRQHandler);
    initCPUTimers();
    configCPUTimer(TMR0_BASE, DEVICE_SYSCLK_FREQ, 10000);
    Interrupt_setPriorityGroup(INTERRUPT_PRIGROUP_PREEMPT_7_6_SUB_5_0);
    Interrupt_setPriority(INT_TIMER0,0,0);
    TMR_enableInterrupt(TMR0_BASE);
    Interrupt_enable(INT_TIMER0);
    TMR_startTimer(TMR0_BASE);
    //
    // Enable Global Interrupt (INTM) and realtime interrupt (DBGM)
    //
    EINT;
    ERTM;

4.3 定时器中断处理函数:

void INT_TIMER0_IRQHandler(void)
{
    cpuTimer0IntCount++;
    if(cpuTimer0IntCount%100 ==0)
    {
      GPIO_togglePin(LED1_IO);        
    }
}





关键词: G32R501     定时器    

专家
2026-08-21 10:40:56     打赏
2楼

Cortex-M52 把Systick去掉了吗 ?!


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]