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 clockSelection
    Timer 0/1 prescaler selection type. Possible values and meanings:
    • TC_CLKSEL_OFF_gc - Prescaler not used.
    • TC_CLKSEL_DIV1_gc - Prescale factor 1.
    • TC_CLKSEL_DIV2_gc - Prescale factor 2.
    • TC_CLKSEL_DIV4_gc - Prescale factor 4.
    • TC_CLKSEL_DIV8_gc - Prescale factor 8.
    • TC_CLKSEL_DIV64_gc - Prescale factor 64.
    • TC_CLKSEL_DIV256_gc - Prescale factor 256.
    • TC_CLKSEL_DIV1024_gc - Prescale factor 1024.
  • Timer wgm
    Timer 0/1 mode. Possible values and meanings:
    • TC_WGMODE_NORMAL_gc - Normal mode
    • TC_WGMODE_FRQ_gc - Frequency generator mode
    • TC_WGMODE_SINGLESLOPE_gc - PWM mode, count up only
    • TC_WGMODE_DSTOP_gc - PWM mode, up/down count, registers updated at TOP
    • TC_WGMODE_DSBOTH_gc - PWM mode, up/down count, registers updated at TOP and BOTTOM
    • TC_WGMODE_DSBOTTOM_gc - PWM mode, up/down count, registers updated at BOTTOM
  • 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.
    • tc - Timer port
    • clockSelection - Clock setting
  • 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
    • tc - Timer port
    • enableMask - Channels to enable (multiple can be enabled with bitwise OR)
  • void TC0_DisableCCChannels( volatile TC0_t * tc, uint8_t disableMask );
    Disable Timer0 signal generation units
    • tc - Timer port
    • disableMask - Channels to disable (multiple can be disabled with bitwise OR)
  • void TC0_SetOverflowIntLevel( volatile TC0_t * tc, TC_OVFINTLVL_t intLevel );
    Enable Timer0 overflow interrupt
    • tc - Timer port
    • intLevel - Overflow interrupt priority
  • 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

  • tc - Timer port
  • intLevel - Compare interrupt priority
  • void TC0_Reset( volatile TC0_t * tc ); Timer0 reset
    • tc - Timer port

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 - Timer module
    • _compareValue - value to set
  • TC_GetOverflowFlag( _tc ) Check overflow flag
    • _tc - Timer module
  • TC_ClearOverflowFlag( _tc ) Clear overflow flag
    • _tc - Timer module
  • TC_GetCCAFlag( _tc )
  • TC_GetCCBFlag( _tc )
  • TC_GetCCCFlag( _tc )
  • TC_GetCCDFlag( _tc ) Check signal generator flag
    • _tc - Timer module
  • TC_ClearCCAFlag( _tc )
  • TC_ClearCCBFlag( _tc )
  • TC_ClearCCCFlag( _tc )
  • TC_ClearCCDFlag( _tc ) Clear signal generator flag
    • _tc - Timer module
  • TC_GetCaptureA( _tc )
  • TC_GetCaptureB( _tc )
  • TC_GetCaptureC( _tc )
  • TC_GetCaptureD( _tc ) Check event counter
    • _tc - Timer module

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);
}
en/software/homelab/library/timer_xmega.txt · Last modified: by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0