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

a2d.c

Go to the documentation of this file.
00001 /*! \file a2d.c \brief Analog-to-Digital converter function library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'a2d.c'
00005 // Title        : Analog-to-digital converter functions
00006 // Author       : Pascal Stang - Copyright (C) 2002
00007 // Created      : 2002-04-08
00008 // Revised      : 2002-09-30
00009 // Version      : 1.1
00010 // Target MCU   : Atmel AVR series
00011 // Editor Tabs  : 4
00012 //
00013 // This code is distributed under the GNU Public License
00014 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00015 //
00016 //*****************************************************************************
00017 
00018 #include <avr/io.h>
00019 #include <avr/signal.h>
00020 #include <avr/interrupt.h>
00021 
00022 #include "global.h"
00023 #include "a2d.h"
00024 
00025 // global variables
00026 
00027 //! Software flag used to indicate when
00028 /// the a2d conversion is complete.
00029 volatile unsigned char a2dCompleteFlag;
00030 
00031 // functions
00032 
00033 // initialize a2d converter
00034 void a2dInit(void)
00035 {
00036     sbi(ADCSR, ADEN);               // enable ADC (turn on ADC power)
00037     cbi(ADCSR, ADFR);               // default to single sample convert mode
00038     a2dSetPrescaler(ADC_PRESCALE);  // set default prescaler
00039     a2dSetReference(ADC_REFERENCE); // set default reference
00040     cbi(ADMUX, ADLAR);              // set to right-adjusted result
00041 
00042     sbi(ADCSR, ADIE);               // enable ADC interrupts
00043 
00044     a2dCompleteFlag = FALSE;        // clear conversion complete flag
00045     sei();                          // turn on interrupts (if not already on)
00046 }
00047 
00048 // turn off a2d converter
00049 void a2dOff(void)
00050 {
00051     cbi(ADCSR, ADIE);               // disable ADC interrupts
00052     cbi(ADCSR, ADEN);               // disable ADC (turn off ADC power)
00053 }
00054 
00055 // configure A2D converter clock division (prescaling)
00056 void a2dSetPrescaler(unsigned char prescale)
00057 {
00058     outb(ADCSR, ((inb(ADCSR) & ~ADC_PRESCALE_MASK) | prescale));
00059 }
00060 
00061 // configure A2D converter voltage reference
00062 void a2dSetReference(unsigned char ref)
00063 {
00064     outb(ADMUX, ((inb(ADMUX) & ~ADC_REFERENCE_MASK) | (ref<<6)));
00065 }
00066 
00067 // sets the a2d input channel
00068 void a2dSetChannel(unsigned char ch)
00069 {
00070     outb(ADMUX, (inb(ADMUX) & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK));    // set channel
00071 }
00072 
00073 // start a conversion on the current a2d input channel
00074 void a2dStartConvert(void)
00075 {
00076     sbi(ADCSR, ADIF);   // clear hardware "conversion complete" flag 
00077     sbi(ADCSR, ADSC);   // start conversion
00078 }
00079 
00080 // return TRUE if conversion is complete
00081 u08 a2dIsComplete(void)
00082 {
00083     return bit_is_set(ADCSR, ADSC);
00084 }
00085 
00086 // Perform a 10-bit conversion
00087 // starts conversion, waits until conversion is done, and returns result
00088 unsigned short a2dConvert10bit(unsigned char ch)
00089 {
00090     a2dCompleteFlag = FALSE;                // clear conversion complete flag
00091     outb(ADMUX, (inb(ADMUX) & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK));    // set channel
00092     sbi(ADCSR, ADIF);                       // clear hardware "conversion complete" flag 
00093     sbi(ADCSR, ADSC);                       // start conversion
00094     //while(!a2dCompleteFlag);              // wait until conversion complete
00095     //while( bit_is_clear(ADCSR, ADIF) );       // wait until conversion complete
00096     while( bit_is_set(ADCSR, ADSC) );       // wait until conversion complete
00097 
00098     // CAUTION: MUST READ ADCL BEFORE ADCH!!!
00099     return (inb(ADCL) | (inb(ADCH)<<8));    // read ADC (full 10 bits);
00100 }
00101 
00102 // Perform a 8-bit conversion.
00103 // starts conversion, waits until conversion is done, and returns result
00104 unsigned char a2dConvert8bit(unsigned char ch)
00105 {
00106     // do 10-bit conversion and return highest 8 bits
00107     return a2dConvert10bit(ch)>>2;          // return ADC MSB byte
00108 }
00109 
00110 //! Interrupt handler for ADC complete interrupt.
00111 SIGNAL(SIG_ADC)
00112 {
00113     // set the a2d conversion flag to indicate "complete"
00114     a2dCompleteFlag = TRUE;
00115 }
00116 

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