Timer_A: Programming

The first step is to program the timer itself. There are many modes possible, and many structs for each mode. Available structs are:

The following is an example for configuring a continuous mode Timer_A:

// This timer will go from 0x0 to 0xffff at 3MHz and start back at 0 forever  

Timer_A_ContinuousModeConfig contConfig = {  

    TIMER_A_CLOCKSOURCE_SMCLK, // system clock for the source of the timer clock  

    TIMER_A_CLOCKSOURCE_DIVIDER_1, // prescaler = 1, therefore f = 3 MHz  

    TIMER_A_TAIE_INTERRUPT_DISABLE, // disable interrupts  

    TIMER_A_SKIP_CLEAR // start as is  

};  


Timer_A_configureContinuousMode (TIMER_A0_BASE, &contConfig);  

The period of the above timer in milliseconds  is (0xffff + 1) / 3000000 * 1000 = 21.8 ms. (Note that we are assuming the system clock is 3MHz in this example. You should always make sure you know the actual system clock frequency.)

The second step is to configure either the IC or the OC. For this example, we will configure the output compare mode.  

// Whenever the counter reaches 0x1000, the output pin toggles, 

// when it reaches 0xffff, it sets the output pin (makes it high)  

Timer_A_CompareModeConfig cmpConfig = {  

    TIMER_A_CAPTURECOMPARE_REGISTER_3, // use register 3 for capture mode  

    TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, // no need to interrupt  

    TIMER_A_OUTPUTMODE_TOGGLE_SET, // MCU user manual - page 791  

    0x1000 // the value written to the output capture  

};  

 

Timer_A_initCompare (TIMER_A0_BASE, &cmpConfig);

The time from 0 until the counter hits OC value in milliseconds is (2^12) / 3000000 * 1000 = 1.36 ms

In the above example...

So, the timer...

That results in a waveform that is on or high for 1/16th of the period.

The third step is to configure the GPIO as CI/CO.

// We tell the processor this is not a GPIO anymore. Instead it is used by Timer_A  

GPIO_setAsPeripheralModuleFunctionOutputPin(  

    GPIO_PORT_P2,  

    GPIO_PIN6,  

    GPIO_PRIMARY_MODULE_FUNCTION);

The fourth step is to manually start the timer. The configuration by itself is not enough.  

Timer_A_startCounter (TIMER_A0_BASE, TIMER_A_CONTINUOUS_MODE); 

Once all of the above is done, the output compare function is automatic and the software does not need to do anything.  

The below picture shows the GPIO connected to the Red LED on the booster board using an oscilloscope. We can see the signal has ~22 ms period and the duty cycle is ~1.5ms.

Summary of Timer_A programming in OC mode: