Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

extint.c

Go to the documentation of this file.
00001 /*! \file extint.c \brief External-Interrupt function library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'extint.c'
00005 // Title        : External-Interrupt function library
00006 // Author       : Pascal Stang - Copyright (C) 2002-2004
00007 // Created      : 5/10/2002
00008 // Revised      : 11/16/2004
00009 // Version      : 1.0
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // Notes:   This library provides convenient standardized configuration and
00014 //          access to external interrupts.  The library is designed to make
00015 //          it possible to write code that uses external interrupts without
00016 //          digging into the processor datasheets to find register names and
00017 //          bit-defines.  The library also strives to allow code which uses
00018 //          external interrupts to more easily cross-compile between different
00019 //          microcontrollers.
00020 //
00021 //          NOTE: Using this library has certain advantages, but also adds
00022 //          overhead and latency to interrupt servicing.  If the smallest
00023 //          code size or fastest possible latency is needed, do NOT use this
00024 //          library; link your interrupts directly.
00025 //
00026 //*****************************************************************************
00027 
00028 #include <avr/io.h>
00029 #include <avr/interrupt.h>
00030 
00031 #include "global.h"
00032 #include "extint.h"
00033 
00034 // Global variables
00035 typedef void (*voidFuncPtr)(void);
00036 volatile static voidFuncPtr ExtIntFunc[EXTINT_NUM_INTERRUPTS];
00037 
00038 // functions
00039 
00040 //! initializes extint library
00041 void extintInit(void)
00042 {
00043     u08 intNum;
00044     // detach all user functions from interrupts
00045     for(intNum=0; intNum<EXTINT_NUM_INTERRUPTS; intNum++)
00046         extintDetach(intNum);
00047 
00048 }
00049 
00050 //! Configure external interrupt trigger
00051 // NOTE: this function is not complete!!!
00052 void extintConfigure(u08 interruptNum, u08 configuration)
00053 {
00054     if(interruptNum == EXTINT0)
00055     {
00056         MCUCR &= ~((1<<ISC01) | (1<<ISC00));
00057         MCUCR |= configuration;
00058     }
00059     #ifdef SIG_INTERRUPT1
00060     else if(interruptNum == EXTINT1)
00061     {
00062         MCUCR &= ~((1<<ISC11) | (1<<ISC10));
00063         MCUCR |= configuration<<2;
00064     }
00065     #endif
00066     #ifdef SIG_INTERRUPT2
00067     else if(interruptNum == EXTINT2)
00068     {
00069         if(configuration == EXTINT_EDGE_RISING)
00070             sbi(MCUCSR, ISC2);
00071         else
00072             cbi(MCUCSR, ISC2);
00073     }
00074     #endif
00075     // need to handle a lot more cases
00076     // and differences between processors.
00077     // looking for clean way to do it...
00078 }
00079 
00080 //! Attach a user function to an external interrupt
00081 void extintAttach(u08 interruptNum, void (*userHandler)(void) )
00082 {
00083     // make sure the interrupt number is within bounds
00084     if(interruptNum < EXTINT_NUM_INTERRUPTS)
00085     {
00086         // set the interrupt function to run
00087         // the supplied user's function
00088         ExtIntFunc[interruptNum] = userHandler;
00089     }
00090 }
00091 
00092 //! Detach a user function from an external interrupt
00093 void extintDetach(u08 interruptNum)
00094 {
00095     // make sure the interrupt number is within bounds
00096     if(interruptNum < EXTINT_NUM_INTERRUPTS)
00097     {
00098         // set the interrupt function to run
00099         // the supplied user's function
00100         ExtIntFunc[interruptNum] = 0;
00101     }
00102 }
00103 
00104 //! Interrupt handler for INT0
00105 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT0)
00106 {
00107     // if a user function is defined, execute it
00108     if(ExtIntFunc[EXTINT0])
00109         ExtIntFunc[EXTINT0]();
00110 }
00111 
00112 #ifdef SIG_INTERRUPT1
00113 //! Interrupt handler for INT1
00114 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT1)
00115 {
00116     // if a user function is defined, execute it
00117     if(ExtIntFunc[EXTINT1])
00118         ExtIntFunc[EXTINT1]();
00119 }
00120 #endif
00121 
00122 #ifdef SIG_INTERRUPT2
00123 //! Interrupt handler for INT2
00124 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT2)
00125 {
00126     // if a user function is defined, execute it
00127     if(ExtIntFunc[EXTINT2])
00128         ExtIntFunc[EXTINT2]();
00129 }
00130 #endif
00131 
00132 #ifdef SIG_INTERRUPT3
00133 //! Interrupt handler for INT3
00134 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT3)
00135 {
00136     // if a user function is defined, execute it
00137     if(ExtIntFunc[EXTINT3])
00138         ExtIntFunc[EXTINT3]();
00139 }
00140 #endif
00141 
00142 #ifdef SIG_INTERRUPT4
00143 //! Interrupt handler for INT4
00144 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT4)
00145 {
00146     // if a user function is defined, execute it
00147     if(ExtIntFunc[EXTINT4])
00148         ExtIntFunc[EXTINT4]();
00149 }
00150 #endif
00151 
00152 #ifdef SIG_INTERRUPT5
00153 //! Interrupt handler for INT5
00154 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT5)
00155 {
00156     // if a user function is defined, execute it
00157     if(ExtIntFunc[EXTINT5])
00158         ExtIntFunc[EXTINT5]();
00159 }
00160 #endif
00161 
00162 #ifdef SIG_INTERRUPT6
00163 //! Interrupt handler for INT6
00164 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT6)
00165 {
00166     // if a user function is defined, execute it
00167     if(ExtIntFunc[EXTINT6])
00168         ExtIntFunc[EXTINT6]();
00169 }
00170 #endif
00171 
00172 #ifdef SIG_INTERRUPT7
00173 //! Interrupt handler for INT7
00174 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT7)
00175 {
00176     // if a user function is defined, execute it
00177     if(ExtIntFunc[EXTINT7])
00178         ExtIntFunc[EXTINT7]();
00179 }
00180 #endif
00181 

Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by  doxygen 1.4.2