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/signal.h>
00030 #include <avr/interrupt.h>
00031 
00032 #include "global.h"
00033 #include "extint.h"
00034 
00035 // Global variables
00036 typedef void (*voidFuncPtr)(void);
00037 volatile static voidFuncPtr ExtIntFunc[EXTINT_NUM_INTERRUPTS];
00038 
00039 // functions
00040 
00041 //! initializes extint library
00042 void extintInit(void)
00043 {
00044     u08 intNum;
00045     // detach all user functions from interrupts
00046     for(intNum=0; intNum<EXTINT_NUM_INTERRUPTS; intNum++)
00047         extintDetach(intNum);
00048 
00049 }
00050 
00051 //! Configure external interrupt trigger
00052 // NOTE: this function is not complete!!!
00053 void extintConfigure(u08 interruptNum, u08 configuration)
00054 {
00055     if(interruptNum == EXTINT0)
00056     {
00057         MCUCR &= ~((1<<ISC01) | (1<<ISC00));
00058         MCUCR |= configuration;
00059     }
00060     #ifdef SIG_INTERRUPT1
00061     else if(interruptNum == EXTINT1)
00062     {
00063         MCUCR &= ~((1<<ISC11) | (1<<ISC10));
00064         MCUCR |= configuration<<2;
00065     }
00066     #endif
00067     #ifdef SIG_INTERRUPT2
00068     else if(interruptNum == EXTINT2)
00069     {
00070         if(configuration == EXTINT_EDGE_RISING)
00071             sbi(MCUCSR, ISC2);
00072         else
00073             cbi(MCUCSR, ISC2);
00074     }
00075     #endif
00076     // need to handle a lot more cases
00077     // and differences between processors.
00078     // looking for clean way to do it...
00079 }
00080 
00081 //! Attach a user function to an external interrupt
00082 void extintAttach(u08 interruptNum, void (*userHandler)(void) )
00083 {
00084     // make sure the interrupt number is within bounds
00085     if(interruptNum < EXTINT_NUM_INTERRUPTS)
00086     {
00087         // set the interrupt function to run
00088         // the supplied user's function
00089         ExtIntFunc[interruptNum] = userHandler;
00090     }
00091 }
00092 
00093 //! Detach a user function from an external interrupt
00094 void extintDetach(u08 interruptNum)
00095 {
00096     // make sure the interrupt number is within bounds
00097     if(interruptNum < EXTINT_NUM_INTERRUPTS)
00098     {
00099         // set the interrupt function to run
00100         // the supplied user's function
00101         ExtIntFunc[interruptNum] = 0;
00102     }
00103 }
00104 
00105 //! Interrupt handler for INT0
00106 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT0)
00107 {
00108     // if a user function is defined, execute it
00109     if(ExtIntFunc[EXTINT0])
00110         ExtIntFunc[EXTINT0]();
00111 }
00112 
00113 #ifdef SIG_INTERRUPT1
00114 //! Interrupt handler for INT1
00115 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT1)
00116 {
00117     // if a user function is defined, execute it
00118     if(ExtIntFunc[EXTINT1])
00119         ExtIntFunc[EXTINT1]();
00120 }
00121 #endif
00122 
00123 #ifdef SIG_INTERRUPT2
00124 //! Interrupt handler for INT2
00125 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT2)
00126 {
00127     // if a user function is defined, execute it
00128     if(ExtIntFunc[EXTINT2])
00129         ExtIntFunc[EXTINT2]();
00130 }
00131 #endif
00132 
00133 #ifdef SIG_INTERRUPT3
00134 //! Interrupt handler for INT3
00135 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT3)
00136 {
00137     // if a user function is defined, execute it
00138     if(ExtIntFunc[EXTINT3])
00139         ExtIntFunc[EXTINT3]();
00140 }
00141 #endif
00142 
00143 #ifdef SIG_INTERRUPT4
00144 //! Interrupt handler for INT4
00145 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT4)
00146 {
00147     // if a user function is defined, execute it
00148     if(ExtIntFunc[EXTINT4])
00149         ExtIntFunc[EXTINT4]();
00150 }
00151 #endif
00152 
00153 #ifdef SIG_INTERRUPT5
00154 //! Interrupt handler for INT5
00155 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT5)
00156 {
00157     // if a user function is defined, execute it
00158     if(ExtIntFunc[EXTINT5])
00159         ExtIntFunc[EXTINT5]();
00160 }
00161 #endif
00162 
00163 #ifdef SIG_INTERRUPT6
00164 //! Interrupt handler for INT6
00165 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT6)
00166 {
00167     // if a user function is defined, execute it
00168     if(ExtIntFunc[EXTINT6])
00169         ExtIntFunc[EXTINT6]();
00170 }
00171 #endif
00172 
00173 #ifdef SIG_INTERRUPT7
00174 //! Interrupt handler for INT7
00175 EXTINT_INTERRUPT_HANDLER(SIG_INTERRUPT7)
00176 {
00177     // if a user function is defined, execute it
00178     if(ExtIntFunc[EXTINT7])
00179         ExtIntFunc[EXTINT7]();
00180 }
00181 #endif
00182 

Generated on Tue Sep 20 03:11:42 2005 for Procyon AVRlib by  doxygen 1.4.2