Welcome to MSPM0G3519 Development Board repository
6. Timer
Timer
Timer1. Learning ObjectivesTimerTimer Basic Parameters2. Hardware Setup3. Experiment Steps1. Open SYSCONFIG Configuration Tool2. Timer Parameter Configuration3. Write Program5. Compile4. Program Analysis5. Experimental Phenomena
1. Learning Objectives
Learn the basic use of the timer on the MSPM0G3519 motherboard.
Implement timed LED status toggle.
Timer
The timer is a very important peripheral in microcontrollers, used for timing and generating timed interrupts. The core principle of a timer is to record time through an internal counter. The counter increments by one for each machine cycle. When the counter reaches a preset value, it triggers an interrupt. The program can execute certain operations in the interrupt service routine, such as toggling an LED, reading sensor data, controlling a motor, etc.
Timer Basic Parameters
Prescaler
The prescaler can divide the timer clock (TIMCLK) frequency by any value between 1 and 256 (1 to 256 based on the timer prescaler being 8-bit). TIMG can select BUSCLK, MFCLK, or LFCLK as the clock source. It can divide the clock by up to 8, then passes through an 8-bit prescaler to become the timer's counting clock.

Up or Down Counting Mode
Up counting means the counter continuously counts up from 0 to the auto-reload value. Once the counter counts to the auto-reload value, it resets to 0 and continues counting up, generating an overflow event. Down counting means the counter continuously counts down from the auto-reload value to 0. Once the counter counts to 0, it resets to the auto-reload value and continues counting down, generating an underflow event.
Update Event
An update event is triggered when the counter starts a new counting cycle due to overflow or underflow. Update events can trigger DMA requests to timely update the timer's operating parameters at the start of the next counting cycle, which is especially suitable for real-time control.
2. Hardware Setup
The MSPM0G series has 7 timers in total, which can be divided into 2 types: general-purpose timers (TIMG) and advanced control timers (TIMA). Different types of timers have different numbers of features. Generally, advanced timers have the most features, and general-purpose timers have fewer.

3. Experiment Steps
If a project needs to execute or repeatedly execute specific tasks at regular intervals, timer configuration is particularly important. The following will introduce how to configure the timer to trigger an interrupt every 1 second and implement LED status toggle in the interrupt service function, thereby achieving the effect of LED on for 1 second and off for 1 second.
1. Open SYSCONFIG Configuration Tool
Create a new empty project empty in CCS.


Find and open the empty.syscfg file in the left workspace of CCS.

In SYSCONFIG, select MCU peripherals on the left, find the TIMER option and click to enter. Click ADD in TIMER to add a timer peripheral.

2. Timer Parameter Configuration
In this case, Timer Clock Source selects BUSCLK-32MHz. Timer Clock Divider configures timer division, selecting divide by 8. Timer Clock Prescaler configures 100 prescaler, resulting in a timer frequency of 40KHz. Desired Timer Period sets the timer period to 1S. Timer Mode sets to periodic down-counting mode. Enable Interrupts enables the 0 overflow interrupt. LED GPIO configuration follows the LED light course.

Save the configuration in the .syscfg file using the shortcut key Ctrl+S.
3. Write Program
In the empty.c file, write the following code
x#include "ti_msp_dl_config.h"
int main(void){ SYSCFG_DL_init(); // Clear the timer interrupt flag NVIC_ClearPendingIRQ(TIMER_0_INST_INT_IRQN); // Enable timer interrupt NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
while (1) {
}}
// The timer interrupt service function has been configured to a period of 1 secondvoid TIMER_0_INST_IRQHandler(void){ // If a timer interrupt occurs switch( DL_TimerG_getPendingInterrupt(TIMER_0_INST) ) { case DL_TIMER_IIDX_ZERO:// If it is 0 overflow interrupt // Flip the LED status DL_GPIO_togglePins(LED1_PORT, LED1_PIN_2_PIN); break;
default:// Other timer interrupts break; }}5. Compile

Compilation was successful. You can now download the program to the development board.
4. Program Analysis
empty.c

This is a timer interrupt service routine used to handle timer overflow interrupts and execute corresponding operations. Whenever the timer overflows, it toggles the status of an LED.
5. Experimental Phenomena
After the program is downloaded, you can see the LED light flashing according to the 1-second period configuration.