以下示例以周期模式配置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); ?
用來設置定時器定時周期
用法如下
? ? ? ? ? ? ?