Table of Contents

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

Functions

Timer0 signal generation channel A/B/C/D interrupt configuration

All functions apply similarly to Timer1.

Macros

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);
}