MSPM0G3507(十五)——定时器例程讲解4——timx_timer_mode_periodic

以下示例以周期模式配置TimerG并切换LED。周期从500ms开始,每次切换减少50ms,直到周期为100ms,然后重复。设备在等待中断时保持待机模式


#include "ti_msp_dl_config.h"

/* ((32KHz / (32+1)) * 0.5s) = 45 - 1 = 495 due to N+1 ticks */
#define TIMER_500_MILLISECONDS_TICKS (495)                                  //算出定时0.5秒所需要的定时器循环次数
/* ((32KHz / (32+1)) * 0.05s) = 50 */
#define TIMER_50_MILLISECONDS_TICKS (50)                                    //算出定时0.5秒所需要的循环次数

int main(void)
{
    SYSCFG_DL_init();

    NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
    DL_SYSCTL_enableSleepOnExit();

    DL_TimerG_startCounter(TIMER_0_INST);

    while (1) {
        __WFI();
    }
}

void TIMER_0_INST_IRQHandler(void)
{
    static uint32_t count = TIMER_500_MILLISECONDS_TICKS;
    switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST)) {
        case DL_TIMER_IIDX_ZERO:
            /*
             * Counter stopped to avoid a conflict with the timer reading
             * the LOAD value while it's being set
             */
                DL_TimerG_stopCounter(TIMER_0_INST);

            /*
             * Count progressively gets smaller in 0.05 s increments until
             * reset with 0.5s
             */
                if (count > (TIMER_500_MILLISECONDS_TICKS / 5)) {
                    count = count - TIMER_50_MILLISECONDS_TICKS;
                } else {
                    count = TIMER_500_MILLISECONDS_TICKS;
                }

                DL_Timer_setLoadValue(TIMER_0_INST, count);                             //设置定时周期
            /*
             * By default, this should load the new count value and count down
             * from there (CVAE = 0)
             */
                DL_TimerG_startCounter(TIMER_0_INST);

            DL_GPIO_togglePins(GPIO_LEDS_PORT,
                (GPIO_LEDS_USER_LED_1_PIN | GPIO_LEDS_USER_TEST_PIN));
            break;
        default:
            break;
    }
}

用到一个DL_Timer_setLoadValue(TIMER_0_INST, count);  

用来设置定时器定时周期

用法如下

             

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值