This shows you the differences between two versions of the page.
| — | en:software:homelab:library:timer_xmega [2026/02/19 11:30] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== 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// | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | ~~CL~~ | ||
| + | * **//Timer wgm//** \\ Timer 0/1 mode. Possible values and meanings: | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | * // | ||
| + | ~~CL~~ | ||
| + | * **//Timer interrupt names and priorities.// | ||
| + | * // | ||
| + | * //_OFF_gc// - Interrupt disabled | ||
| + | * //_LO_gc// - Low priority interrupt | ||
| + | * //_MED_gc// - Medium priority interrupt | ||
| + | * //_HI_gc// - High priority 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 | ||
| + | * // | ||
| + | * **//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 | ||
| + | * // | ||
| + | * **//void TC0_DisableCCChannels( volatile TC0_t * tc, uint8_t disableMask );//** \\ Disable Timer0 signal generation units | ||
| + | * //tc// - Timer port | ||
| + | * // | ||
| + | * **//void TC0_SetOverflowIntLevel( volatile TC0_t * tc, TC_OVFINTLVL_t intLevel );//** \\ Enable Timer0 overflow interrupt | ||
| + | * //tc// - Timer port | ||
| + | * // | ||
| + | * **//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 | ||
| + | * // | ||
| + | |||
| + | * **//void TC0_Reset( volatile TC0_t * tc );//** Timer0 reset | ||
| + | * //tc// - Timer port | ||
| + | | ||
| + | All functions apply similarly to Timer1. | ||
| + | |||
| + | ===== Macros ===== | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | * //_count// - value to set | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | * //_period// - period value | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | * // | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * **// | ||
| + | * //_tc// - Timer module | ||
| + | |||
| + | ===== Example ===== | ||
| + | |||
| + | The example configures Port E Timer0 to normal counting mode and enables the overflow and compare channel A interrupts. | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // 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(& | ||
| + | TC_SetCompareA(& | ||
| + | |||
| + | // Set Timer E0 clock (F_CPU/ | ||
| + | TC0_ConfigClockSource(& | ||
| + | // Set Timer E0 to normal mode | ||
| + | TC0_ConfigWGM(& | ||
| + | |||
| + | // Enable overflow interrupt with high priority | ||
| + | // Enable compare channel A interrupt with medium priority | ||
| + | TC0_SetOverflowIntLevel(& | ||
| + | TC0_SetCCAIntLevel(& | ||
| + | |||
| + | // 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); | ||
| + | } | ||
| + | </ | ||