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:
Timer_A_CaptureModeConfig
Timer_A_CompareModeConfig
Timer_A_ContinuousModeConfig
Timer_A_PWMConfig
Timer_A_UpDownModeConfig
Timer_A_UpModeConfig
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...
TA0CCR0 is set at 0xFFFF
TA0CCR3 is set at 0x1000
Mode is Toggle/SET
So, the timer...
Runs from 0 to 0xffff
When it hits 0x1000 (the OC value), it is toggled (for us, from 1 to 0)
When it hits 0xffff (the counter value), it is set to one
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:
setup the periodic timer itself
setup the OC configuration
setup the GPIO to be wired to the timer
start the timer