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