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

ads7828.c

Go to the documentation of this file.
00001 /*! \file ads7828.c \brief TI ADS7828 12-bit 8ch A/D Converter Driver Library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'ads7828.c'
00005 // Title        : TI ADS7828 12-bit 8ch A/D Converter Driver Library
00006 // Author       : Pascal Stang - Copyright (C) 2004
00007 // Created      : 2004.02.10
00008 // Revised      : 2004.02.19
00009 // Version      : 0.1
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // NOTE: This code is currently below version 1.0, and therefore is considered
00014 // to be lacking in some functionality or documentation, or may not be fully
00015 // tested.  Nonetheless, you can expect most functions to work.
00016 //
00017 // This code is distributed under the GNU Public License
00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00019 //
00020 //*****************************************************************************
00021 
00022 #include <avr/io.h>
00023 #include <avr/interrupt.h>
00024 
00025 #include "global.h"
00026 #include "i2c.h"
00027 #include "ads7828.h"
00028 
00029 // global variables
00030 u08 Ads7282RefMode;
00031 
00032 // Functions
00033 u08 ads7828Init(u08 i2cAddr)
00034 {
00035     u08 channel = 0x80;
00036 
00037     // setup default A/D voltage reference
00038     ads7828SetReference(0);
00039 
00040     // issue a convserion to test chip presence
00041     // return TRUE if chip detected
00042     // return FALSE if chip does not respond
00043     return (i2cMasterSendNI(i2cAddr, 1, &channel) == I2C_OK);
00044 }
00045 
00046 u16 ads7828Convert(u08 i2cAddr, u08 channel)
00047 {
00048     // re-order channel bits for
00049     // logical single-ended channel selection
00050     // channel bit0 -> C2
00051     // channel bit1 -> C0
00052     // channel bit2 -> C1
00053     channel = (((channel>>1) | (channel&0x01)<<2)<<4) | ADS7828_CMD_SD;
00054     // do conversion
00055     return ads7828ConvertRaw(i2cAddr, channel);
00056 }
00057 
00058 u16 ads7828ConvertDiff(u08 i2cAddr, u08 channel)
00059 {
00060     // clear single-ended channel bit
00061     channel = (channel&0x07)<<4;
00062     // do conversion
00063     return ads7828ConvertRaw(i2cAddr, channel);
00064 }
00065 
00066 u16 ads7828ConvertRaw(u08 i2cAddr, u08 channel)
00067 {
00068     u08 buffer[2];
00069     // combine raw channel and reference bits
00070     channel &= 0xF0;
00071     channel |= Ads7282RefMode;
00072     // start conversion on requested channel
00073     i2cMasterSendNI(i2cAddr, 1, &channel);
00074     // retrieve conversion result
00075     i2cMasterReceiveNI(i2cAddr, 2, buffer);
00076     // pack bytes and return result
00077     return ((buffer[0]<<8) | buffer[1]);
00078 }
00079 
00080 void ads7828SetReference(u08 ref)
00081 {
00082     if(ref)
00083     {
00084         // use internal reference
00085         Ads7282RefMode = ADS7828_CMD_PDMODE2;
00086     }
00087     else
00088     {
00089         // use external reference
00090         Ads7282RefMode = ADS7828_CMD_PDMODE0;
00091     }
00092 }

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