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/signal.h>
00024 #include <avr/interrupt.h>
00025 
00026 #include "global.h"
00027 #include "i2c.h"
00028 #include "ads7828.h"
00029 
00030 // global variables
00031 u08 Ads7282RefMode;
00032 
00033 // Functions
00034 u08 ads7828Init(u08 i2cAddr)
00035 {
00036     u08 channel = 0x80;
00037 
00038     // setup default A/D voltage reference
00039     ads7828SetReference(0);
00040 
00041     // issue a convserion to test chip presence
00042     // return TRUE if chip detected
00043     // return FALSE if chip does not respond
00044     return (i2cMasterSendNI(i2cAddr, 1, &channel) == I2C_OK);
00045 }
00046 
00047 u16 ads7828Convert(u08 i2cAddr, u08 channel)
00048 {
00049     // re-order channel bits for
00050     // logical single-ended channel selection
00051     // channel bit0 -> C2
00052     // channel bit1 -> C0
00053     // channel bit2 -> C1
00054     channel = (((channel>>1) | (channel&0x01)<<2)<<4) | ADS7828_CMD_SD;
00055     // do conversion
00056     return ads7828ConvertRaw(i2cAddr, channel);
00057 }
00058 
00059 u16 ads7828ConvertDiff(u08 i2cAddr, u08 channel)
00060 {
00061     // clear single-ended channel bit
00062     channel = (channel&0x07)<<4;
00063     // do conversion
00064     return ads7828ConvertRaw(i2cAddr, channel);
00065 }
00066 
00067 u16 ads7828ConvertRaw(u08 i2cAddr, u08 channel)
00068 {
00069     u08 buffer[2];
00070     // combine raw channel and reference bits
00071     channel &= 0xF0;
00072     channel |= Ads7282RefMode;
00073     // start conversion on requested channel
00074     i2cMasterSendNI(i2cAddr, 1, &channel);
00075     // retrieve conversion result
00076     i2cMasterReceiveNI(i2cAddr, 2, buffer);
00077     // pack bytes and return result
00078     return ((buffer[0]<<8) | buffer[1]);
00079 }
00080 
00081 void ads7828SetReference(u08 ref)
00082 {
00083     if(ref)
00084     {
00085         // use internal reference
00086         Ads7282RefMode = ADS7828_CMD_PDMODE2;
00087     }
00088     else
00089     {
00090         // use external reference
00091         Ads7282RefMode = ADS7828_CMD_PDMODE0;
00092     }
00093 }

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