XMEGA Timers
This timer library covers a large part of the ATXmega128A1U timer functionality. Since AVR timers differ quite a lot between chips, it is not possible to write universal functions for them. The described ATXmega128A1U functions are largely just primitive register read/write functions, but they are still more readable than raw registers.
Data types
Timer interrupt names and priorities.
All interrupts on XMega series microcontrollers are prioritized.
TC_OVFINTLVL - Overflow interrupt
_OFF_gc - Interrupt disabled
_LO_gc - Low priority interrupt
_MED_gc - Medium priority interrupt
_HI_gc - High priority interrupt
TC_CCAINTLVL - Compare register interrupt
TC_CCBINTLVL - Compare register interrupt
TC_CCCINTLVL - Compare register interrupt
TC_CCDINTLVL - Compare register interrupt
_OFF_gc - Interrupt disabled
_LO_gc - Low priority interrupt
_MED_gc - Medium priority interrupt
_HI_gc - High priority interrupt
Functions
void TC0_ConfigClockSource( volatile TC0_t * tc, TC_CLKSEL_t clockSelection );
Configure Timer0 clock source.
void TC0_ConfigWGM( volatile TC0_t * tc, TC_WGMODE_t wgm );
Configure Timer0 mode.
tc - Timer port
wgm - Mode setting
void TC0_EnableCCChannels( volatile TC0_t * tc, uint8_t enableMask );
Enable Timer0 signal generation units
void TC0_DisableCCChannels( volatile TC0_t * tc, uint8_t disableMask );
Disable Timer0 signal generation units
void TC0_SetOverflowIntLevel( volatile TC0_t * tc, TC_OVFINTLVL_t intLevel );
Enable Timer0 overflow interrupt
void TC0_SetCCAIntLevel( volatile TC0_t * tc, TC_CCAINTLVL_t intLevel );
void TC0_SetCCBIntLevel( volatile TC0_t * tc, TC_CCBINTLVL_t intLevel );
void TC0_SetCCCIntLevel( volatile TC0_t * tc, TC_CCCINTLVL_t intLevel );
void TC0_SetCCDIntLevel( volatile TC0_t * tc, TC_CCDINTLVL_t intLevel );
Timer0 signal generation channel A/B/C/D interrupt configuration
All functions apply similarly to Timer1.
Macros
TC_SetCount( _tc, _count ) Allows changing the timer value manually
_tc - Timer module
_count - value to set
TC_SetPeriod( _tc, _period ) Sets the timer period
_tc - Timer module
_period - period value
TC_SetCompareA( _tc, _compareValue )
TC_SetCompareB( _tc, _compareValue )
TC_SetCompareC( _tc, _compareValue )
TC_SetCompareD( _tc, _compareValue ) Set the compare value for a channel
TC_GetOverflowFlag( _tc ) Check overflow flag
TC_ClearOverflowFlag( _tc ) Clear overflow flag
TC_GetCCAFlag( _tc )
TC_GetCCBFlag( _tc )
TC_GetCCCFlag( _tc )
TC_GetCCDFlag( _tc ) Check signal generator flag
TC_ClearCCAFlag( _tc )
TC_ClearCCBFlag( _tc )
TC_ClearCCCFlag( _tc )
TC_ClearCCDFlag( _tc ) Clear signal generator flag
TC_GetCaptureA( _tc )
TC_GetCaptureB( _tc )
TC_GetCaptureC( _tc )
TC_GetCaptureD( _tc ) Check event counter
Example
The example configures Port E Timer0 to normal counting mode and enables the overflow and compare channel A interrupts.
#include <homelab/xmega/clksys_driver.h>
#include <homelab/pin.h>
#include <homelab/xmega/TC_driver.h>
#include <avr/interrupt.h>
// Overflow interrupt
ISR(TCE0_OVF_vect)
{
led_on(led_green);
}
// Compare channel A interrupt
ISR(TCE0_CCA_vect)
{
led_off(led_green);
}
int main(void)
{
// Configure green LED as output
pin_setup_output(led_green);
led_off(led_green);
// Set Timer E0 period
// Set Timer E0 duty cycle length
TC_SetPeriod(&TCE0, 20000);
TC_SetCompareA(&TCE0, 15000);
// Set Timer E0 clock (F_CPU/1024)
TC0_ConfigClockSource(&TCE0, TC_CLKSEL_DIV1024_gc);
// Set Timer E0 to normal mode
TC0_ConfigWGM(&TCE0, TC_WGMODE_NORMAL_gc);
// Enable overflow interrupt with high priority
// Enable compare channel A interrupt with medium priority
TC0_SetOverflowIntLevel(&TCE0,TC_OVFINTLVL_HI_gc);
TC0_SetCCAIntLevel(&TCE0, TC_CCAINTLVL_MED_gc);
// Enable medium and high priority interrupts
// Enable global interrupts
PMIC.CTRL |= PMIC_MEDLVLEN_bm|PMIC_HILVLEN_bm;
sei();
// Empty loop, program runs on interrupts
while(1);
}