Main Page   Data Structures   File List   Data Fields   Globals  

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 //! interrupt handler for ADC complete interrupt
00032 SIGNAL(SIG_ADC)
00033 {
00034     // set the a2d conversion flag to indicate "complete"
00035     a2dCompleteFlag = TRUE;
00036 }
00037 
00038 // functions
00039 
00040 //! initialize a2d converter
00041 void a2dInit(void)
00042 {
00043     sbi(ADCSR, ADEN);               // enable ADC (turn on ADC power)
00044     cbi(ADCSR, ADFR);               // default to single sample convert mode
00045     a2dSetPrescaler(ADC_PRESCALE);  // set default prescaler
00046     a2dSetReference(ADC_REFERENCE); // set default reference
00047     cbi(ADMUX, ADLAR);              // set to right-adjusted result
00048 
00049    sbi(ADCSR, ADIE);                // enable ADC interrupts
00050 
00051     a2dCompleteFlag = FALSE;        // clear conversion complete flag
00052     sei();                          // turn on interrupts (if not already on)
00053 }
00054 
00055 //! turn off a2d converter
00056 void a2dOff(void)
00057 {
00058     cbi(ADCSR, ADIE);               // disable ADC interrupts
00059     cbi(ADCSR, ADEN);               // disable ADC (turn off ADC power)
00060 }
00061 
00062 //! configure A2D converter clock division (prescaling)
00063 void a2dSetPrescaler(unsigned char prescale)
00064 {
00065     outp( ((inp(ADCSR) & ~ADC_PRESCALE_MASK) | prescale), ADCSR);
00066 }
00067 
00068 //! configure A2D converter voltage reference
00069 void a2dSetReference(unsigned char ref)
00070 {
00071     outp( ((inp(ADMUX) & ~ADC_REFERENCE_MASK) | (ref<<6)), ADMUX);
00072 }
00073 
00074 //! sets the a2d input channel
00075 void a2dSetChannel(unsigned char ch)
00076 {
00077     outp((inp(ADMUX) & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK), ADMUX);    // set channel
00078 }
00079 
00080 //! Perform a 10-bit conversion
00081 // starts conversion, waits until conversion is done, and returns result
00082 unsigned short a2dConvert10bit(unsigned char ch)
00083 {
00084     a2dCompleteFlag = FALSE;                // clear conversion complete flag
00085     outp((inp(ADMUX) & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK), ADMUX);    // set channel
00086     sbi(ADCSR, ADIF);                       // clear hardware "conversion complete" flag 
00087     sbi(ADCSR, ADSC);                       // start conversion
00088     //while(!a2dCompleteFlag);              // wait until conversion complete
00089     //while( bit_is_clear(ADCSR, ADIF) );       // wait until conversion complete
00090     while( bit_is_set(ADCSR, ADSC) );       // wait until conversion complete
00091 
00092     // CAUTION: MUST READ ADCL BEFORE ADCH!!!
00093     return (inp(ADCL) | (inp(ADCH)<<8));    // read ADC (full 10 bits);
00094 }
00095 
00096 //! Perform a 8-bit conversion.
00097 // starts conversion, waits until conversion is done, and returns result
00098 unsigned char a2dConvert8bit(unsigned char ch)
00099 {
00100     // do 10-bit conversion and return highest 8 bits
00101     return a2dConvert10bit(ch)>>2;          // return ADC MSB byte
00102 }
00103 

Generated on Tue Feb 4 20:18:36 2003 for Procyon AVRlib by doxygen1.3-rc2