Xmega Application Note


AC_example_window_and_interrupt.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00059 #include "avr_compiler.h"
00060 #include "ac_driver.h"
00061 
00062 /* The Analog comparator used in the example.*/
00063 #define AC ACA
00064 
00065 /* Global bool that indicates if event is above window is used or not. */
00066 volatile bool above;
00067 
00068 
00069 int main(void)
00070 {
00071         /* Enable window mode with event on "midpoint above window". */
00072         AC_EnableWindowMode(&AC, AC_WINTMODE_ABOVE_gc, AC_WINTLVL_LO_gc);
00073         above = true;
00074 
00075         /* Set up MUXes to sense pin 1 and 2, with pin 0 as midpoint. */
00076         AC_ConfigMUX(&AC, ANALOG_COMPARATOR0, AC_MUXPOS_PIN0_gc, AC_MUXNEG_PIN1_gc);
00077         AC_ConfigMUX(&AC, ANALOG_COMPARATOR1, AC_MUXPOS_PIN0_gc, AC_MUXNEG_PIN3_gc);
00078 
00079 
00080         /* Enable both comparators in low power mode. */
00081         AC_Enable(&AC, ANALOG_COMPARATOR0, false);
00082         AC_Enable(&AC, ANALOG_COMPARATOR1, false);
00083 
00084 
00085         /* Enable output on PORTD. */
00086         PORTD.DIRSET = 0xFF;
00087         PORTD.OUT = 0xFF;
00088 
00089         /* Enable PMIC interrupt level low. */
00090         PMIC.CTRL |= PMIC_LOLVLEX_bm;
00091 
00092         /* Enable global interrupts. */
00093         sei();
00094 
00095         do {
00096                 /* Wait for interrupts. To check the if interrupts occur, put a breakpoint in
00097                  * the interrupt handler.*/
00098                 if( AC_GetWindowState(&AC) == AC_WSTATE_ABOVE_gc){
00099                         PORTD.OUT = 0xFB;
00100                 }else if( AC_GetWindowState(&AC) == AC_WSTATE_INSIDE_gc){
00101                         PORTD.OUT = 0xFD;
00102                 }else if( AC_GetWindowState(&AC) == AC_WSTATE_BELOW_gc){
00103                         PORTD.OUT = 0xFE;
00104                 }
00105 
00106         } while(true);
00107 }
00108 
00109 
00113 ISR(ACA_ACW_vect)
00114 {
00115         if(above){
00116                 /* Enable window mode with event on "midpoint below window". */
00117                 AC_EnableWindowMode(&AC, AC_WINTMODE_BELOW_gc, AC_WINTLVL_LO_gc);
00118                 above = false;
00119         }else{
00120                 /* Enable window mode with event on "midpoint above window". */
00121                 AC_EnableWindowMode(&AC, AC_WINTMODE_ABOVE_gc, AC_WINTLVL_LO_gc);
00122                 above = true;
00123   }
00124 }
@DOC_TITLE@
Generated on Tue Apr 22 15:13:25 2008 for AVR1302 Using the XMEGA Analog Comparator by doxygen 1.5.5