Timers: timer32 Programming

To use the Timer32 modules, we will use DriverLib functions to interface with them. The MSP432 has two Timer32 modules, and each one can be configured independently of the other. To start, let's look at the initialization function.

extern void Timer32_initModule(uint32_t timer, uint32_t preScaler,

        uint32_t resolution, uint32_t mode);

The timer parameter selects which of the two Timer32 modules you would like to initialize: TIMER32_0_BASE or TIMER32_1_BASE. The preScaler parameter, as the name implies, decides which of three prescaler values to use: TIMER32_PRESCALER_1, TIMER32_PRESCALER_16, or TIMER32_PRESCALER_256. The resolution parameter determines the resolution of the timer, whether it be the default 16-bit value (TIMER32_16BIT) or a 32-bit value (TIMER32_32BIT). For our purposes, you will generally want to use the 32-bit resolution option. The mode parameter determines what happens when the timer rolls over. In TIMER32_FREE_RUN_MODE, the default, the timer's counter is loaded to the maximum load value (2^32 -1 or 2^16 -1), whereas in TIMER32_PERIODIC_MODE, the timer's counter is loaded to the specified load-value, N. An example initialization looks like as follows:

Timer32_initModule (TIMER32_0_BASE,  

        TIMER32_PRESCALER_1,  

        TIMER32_32BIT,  

        TIMER32_PERIODIC_MODE); 

In the periodic mode, we will need some way to specify the load value for each timer. For that, we have the following function:

extern void Timer32_setCount(uint32_t timer, uint32_t count);

As with the previous function, the timer parameter selects the Timer32 module whose count (N) value you would like to set. The count parameter is the value you would like to load for N. This value must be a 32-bit unsigned integer; in other words, the maximum amount that you can load the counter with is 2^32 - 1 = 4,294,967,295.

Now that the timer is set up, the next step is to start the timer.

extern void Timer32_startTimer(uint32_t timer, bool oneShot);

The timer parameter is the same as before. The oneShot parameter determines whether this timer will count in one-shot timer or not. If the flag is true, the timer will be a one-shot timer and will not reload upon reaching zero. If the flag is false, the timer will be periodic and will reload upon reaching zero.

Finally, if you want to check the value that the timer's counter is currently at, the following function can satisfy that:

extern uint32_t Timer32_getValue(uint32_t timer);

Where, once again, timer selects which Timer32 module you want.