Xmega Application Note


event_system_example.c File Reference


Detailed Description

XMEGA Event system example source code.

This file contains example code that demonstrates the event system. It shows how to set up the Event system to perform some of the examples given in the application note document AVR1001.

Application note:
AVR1001: Getting Started With the XMEGA Event System
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
1569
Date
2008-04-22 13:03:43 +0200 (ti, 22 apr 2008)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file event_system_example.c.

#include "avr_compiler.h"
#include "event_system_driver.h"

Include dependency graph for event_system_example.c:

Go to the source code of this file.

Functions

void Example1 (void)
 Example 1 from the application note.
void Example2 (void)
 Example 2 from the application note.
void Example3 (void)
 Example 3 from the application note.
void Example4 (void)
 Example 4 from the application note.
int main (void)
 Main example function.


Function Documentation

void Example1 ( void   ) 

Example 1 from the application note.

This function implements example 1, "Input Capture" from the "Examples" section of the application note AVR1001.

The example shows how to set up Timer/Counter TCC0 for input capture, triggered by a change on the input of the I/O port pin PD0. The corresponding interrupt flag is set when a capture has occured and the capture time is put in the CCA register.

Definition at line 88 of file event_system_example.c.

References EVSYS_SetEventSource().

Referenced by main().

00089 {
00090         /* Configure PD0 as input, sense on both edges. */
00091         PORTD.PIN0CTRL |= PORT_ISC_BOTHEDGES_gc;
00092         PORTD.DIRCLR = 0x01;
00093 
00094         /* Select PD0 as event channel 0 multiplexer input. */
00095         EVSYS_SetEventSource( 0, EVSYS_CHMUX_PORTD_PIN0_gc );
00096 
00097         /*  Select event channel 0 as event source for TCC0 and input
00098          *  capture as event action.
00099          */
00100         TCC0.CTRLD = (uint8_t) TC_EVSEL_CH0_gc | TC_EVACT_CAPT_gc;
00101 
00102         /* Enable TCC0 "Compare or Capture" Channel A. */
00103         TCC0.CTRLB |= TC0_CCAEN_bm;
00104 
00105         /* Configure TCC0 with the desired frequency and period. */
00106         TCC0.PER = 0xFFFF;
00107         TCC0.CTRLA = TC_CLKSEL_DIV1_gc;
00108 
00109         while (1) {
00110                 if ( TCC0.INTFLAGS & TC0_CCAIF_bm ) {
00111                         /*  Clear interrupt flag when new value is captured.
00112                          *  The last capture value is now available
00113                          *  in the CCA register.
00114                          */
00115                         TCC0.INTFLAGS |= TC0_CCAIF_bm;
00116                 }
00117         }
00118 }

Here is the call graph for this function:

void Example2 ( void   ) 

Example 2 from the application note.

This function implements example 2, "Sweep of 4 ADC Channels on Timer/Counter Overflow" section of the application note AVR1001.

The example shows how to set up the sweep of 4 ADC channels on a Timer/Counter TCC0 overflow event. More information of set up of ADC can be found in the application note AVR1300.

Definition at line 130 of file event_system_example.c.

References EVSYS_SetEventSource().

00131 {
00132         /* Select TC overflow as event channel 0 multiplexer input. */
00133         EVSYS_SetEventSource( 0, EVSYS_CHMUX_TCC0_OVF_gc );
00134 
00135         /*  Configure ADC A event channel and configure which channels to sweep
00136          *  and enable channel sweep.
00137          */
00138         ADCA.EVCTRL = (uint8_t) ADC_SWEEP_0123_gc |
00139                       ADC_EVSEL_0123_gc |
00140                       ADC_EVACT_SWEEP_gc;
00141 
00142         /* Configure the input of the ADC cannels and single ended mode. */
00143         ADCA.CH0.MUXCTRL = (uint8_t) ADC_CH_MUXPOS_PIN4_gc | ADC_CH_MUXNEG_PIN0_gc;
00144         ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
00145         ADCA.CH1.MUXCTRL = (uint8_t) ADC_CH_MUXPOS_PIN5_gc | ADC_CH_MUXNEG_PIN0_gc;
00146         ADCA.CH1.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
00147         ADCA.CH2.MUXCTRL = (uint8_t) ADC_CH_MUXPOS_PIN6_gc | ADC_CH_MUXNEG_PIN0_gc;
00148         ADCA.CH2.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
00149         ADCA.CH3.MUXCTRL = (uint8_t) ADC_CH_MUXPOS_PIN7_gc | ADC_CH_MUXNEG_PIN0_gc;
00150         ADCA.CH3.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
00151 
00152 
00153         /*  Configure prescaler, resolution, singed mode and set voltage
00154          *  reference to internal Vcc - 0.6. Finally enable ADC.
00155          */
00156         ADCA.PRESCALER = ( ADCA.PRESCALER & ~ADC_PRESCALER_gm ) |
00157                          ADC_PRESCALER_DIV8_gc;
00158         ADCA.CTRLB = ( ADCA.CTRLB & ~ADC_RESOLUTION_gm ) |
00159                      ADC_RESOLUTION_12BIT_gc;
00160         ADCA.CTRLB = ( ADCA.CTRLB & ~( ADC_CONMODE_bm | ADC_FREERUN_bm ) );
00161         ADCA.REFCTRL = ( ADCA.REFCTRL & ~ADC_REFSEL_gm ) |
00162                        ADC_REFSEL_VCC_gc;
00163         ADCA.CTRLA |= ADC_ENABLE_bm;
00164 
00165         /* Configure TCC0 with the desired frequency and period. */
00166         TCC0.PER = 0xFFFF;
00167         TCC0.CTRLA = TC_CLKSEL_DIV1_gc;
00168 
00169         while (1) {
00170                 /* Wait while ADC sweeps on every TCC0 Overflow. */
00171         }
00172 }

Here is the call graph for this function:

void Example3 ( void   ) 

Example 3 from the application note.

This function implements example 3, "32-bit Timer/Counter with 32-bit Input Capture" from the "Examples" section of the application note AVR1001.

The example shows how to set up Timer/Counter TCC0 and TCC1 for 32 bit input capture, triggered by a logic change on PD0. The corresponding interrupt flag is set when a capture has occured and the capture time is put in the CCA register.

Definition at line 185 of file event_system_example.c.

References EVSYS_SetEventSource().

00186 {
00187         /* Configure PD0 as input, sense on both edges. */
00188         PORTD.PIN0CTRL = PORT_ISC_BOTHEDGES_gc;
00189         PORTD.DIRCLR = 0x01;
00190 
00191         /* Select TCC0 overflow as event channel 0 multiplexer input.
00192          * (Overflow propagation)
00193          */
00194         EVSYS_SetEventSource( 0, EVSYS_CHMUX_TCC0_OVF_gc );
00195 
00196         /* Select PD0 as event channel 1 multiplexer input. (Input capture) */
00197         EVSYS_SetEventSource( 1, EVSYS_CHMUX_PORTD_PIN0_gc );
00198 
00199         /* Select event channel 0 as clock source for TCC1. */
00200         TCC1.CTRLA = TC_CLKSEL_EVCH0_gc;
00201 
00202         /* Configure TCC0 for input capture. */
00203         TCC0.CTRLD = (uint8_t) TC_EVSEL_CH1_gc | TC_EVACT_CAPT_gc;
00204 
00205         /* Configure TCC1 for input capture with event delay. */
00206         TCC1.CTRLD = (uint8_t) TC_EVSEL_CH1_gc | TC0_EVDLY_bm | TC_EVACT_CAPT_gc;
00207 
00208         /* Enable Compare or Capture Channel A for both timers. */
00209         TCC0.CTRLB = TC0_CCAEN_bm;
00210         TCC1.CTRLB = TC1_CCAEN_bm;
00211 
00212         /* Select system clock as clock source for TCC0. */
00213         TCC0.CTRLA = TC_CLKSEL_DIV1_gc;
00214 
00215         while (1) {
00216                 if ( TCC0.INTFLAGS & TC0_CCAIF_bm ) {
00217                         /*  Clear interrupt flag when new value is captured.
00218                          *  The last capture value is now available
00219                          *  in the CCA register.
00220                          */
00221                         TCC0.INTFLAGS |= TC0_CCAIF_bm;
00222                         TCC1.INTFLAGS |= TC1_CCAIF_bm;
00223                 }
00224         }
00225 }

Here is the call graph for this function:

void Example4 ( void   ) 

Example 4 from the application note.

This function implements example 4, "Event counting" from the "Examples" section of the application note AVR1001.

The example shows how to set up Timer/Counter TCC0 to count the number of input capture, triggered by a logic change on PD0.

Definition at line 235 of file event_system_example.c.

References EVSYS_SetEventChannelFilter(), and EVSYS_SetEventSource().

00236 {
00237         /* Configure PD0 as input, sense on rising edge. */
00238         PORTD.PIN0CTRL = PORT_ISC_RISING_gc;
00239         PORTD.DIRCLR = 0x01;
00240 
00241         /* Configure port C to show result. */
00242         PORTC.DIRSET = 0xFF;
00243 
00244         /* Select PD0 as event channel 0 multiplexer input. */
00245         EVSYS_SetEventSource( 0, EVSYS_CHMUX_PORTD_PIN0_gc );
00246 
00247         /*  Set the digital filter on event channel 0 to maximum filter
00248          *  samples to filter out button bounces.
00249          */
00250         EVSYS_SetEventChannelFilter( 0, EVSYS_DIGFILT_8SAMPLES_gc );
00251 
00252         /* Configure TCC0 with period and event channel 0 as clock source. */
00253         TCC0.PER = 0xFFFF;
00254         TCC0.CTRLA = TC_CLKSEL_EVCH0_gc;
00255 
00256 
00257         while (1) {
00258                 /* Show the inverted value on the active low LEDs. */
00259                 PORTC.OUT = ~TCC0.CNT;
00260         }
00261 }

Here is the call graph for this function:

int main ( void   ) 

Main example function.

This is the main function. Uncomment one of the function calls in main to test the different examples.

Definition at line 64 of file event_system_example.c.

References Example1().

00065 {
00066 
00067         Example1();
00068         /*Example2();*/
00069         /*Example3();*/
00070         /*Example4();*/
00071 
00072         do {
00073 
00074         }while (1);
00075 }

Here is the call graph for this function:

@DOC_TITLE@
Generated on Tue Apr 22 14:20:09 2008 for AVR1001 Getting Started With the XMEGA Event System by doxygen 1.5.5