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

# ads7870.c

Go to the documentation of this file.

    00001 /*! \file ads7870.c \brief TI ADS7870 12-bit 8ch A/D Converter Driver Library. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'ads7870.c'
    00005 // Title        : TI ADS7870 12-bit 8ch A/D Converter Driver Library
    00006 // Author       : Pascal Stang - Copyright (C) 2005
    00007 // Created      : 2005.07.19
    00008 // Revised      : 2005.07.21
    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 <lpc2000.h>            // include I/O definitions (port names, pin names, etc)
    00023
    00024 #include "global.h"
    00025 #include "spi.h"
    00026 #include "ads7870.h"
    00027
    00028 // global variables
    00029
    00030 // Functions
    00031 u08 ads7870Init(void)
    00032 {
    00033     // initialize spi interface
    00034     spiInit();
    00035     // switch to f/4 bitrate
    00036 //  cbi(SPCR, SPR0);
    00037 //  cbi(SPCR, SPR1);
    00038     //sbi(SPSR, SPI2X);
    00039
    00040     // setup chip select
    00041     ADS7870_CS_CONFIG;
    00042
    00043     // write serial interface control register
    00044     ads7870WriteReg(ADS7870_SERIFCTRL, 0);
    00045
    00046     // check ID register
    00047     if(ads7870ReadReg(ADS7870_ID) != ADS7870_ID_VALUE)
    00048         return 0;
    00049
    00050     // setup reference and buffer
    00051     ads7870WriteReg(ADS7870_REFOSC, ADS7870_REFOSC_OSCE | ADS7870_REFOSC_REFE | ADS7870_REFOSC_BUFE);
    00052
    00053     // return success
    00054     return 1;
    00055 }
    00056
    00057 s16 ads7870Convert(u08 channel)
    00058 {
    00059     // set single-ended channel bit
    00060     channel |= ADS7870_CH_SINGLE_ENDED;
    00061     // do conversion
    00062     return ads7870ConvertRaw(channel);
    00063 }
    00064
    00065 s16 ads7870ConvertDiff(u08 channel)
    00066 {
    00067     // clear single-ended channel bit
    00068     channel &= ~ADS7870_CH_SINGLE_ENDED;
    00069     // do conversion
    00070     return ads7870ConvertRaw(channel);
    00071 }
    00072
    00073 s16 ads7870ConvertRaw(u08 channel)
    00074 {
    00075     s16 result;
    00076     // assert chip select
    00077     ADS7870_CS_ASSERT;
    00078     // start conversion
    00079     spiTransferByte(ADS7870_CONVERT | channel);
    00080     // wait for completion
    00081     while( ads7870ReadReg(ADS7870_GAINMUX) & ADS7870_GAINMUX_CNVBSY);
    00082     // assert chip select
    00083     ADS7870_CS_ASSERT;
    00084     // read result
    00085     spiTransferByte(ADS7870_REG_READ | ADS7870_REG_16BIT | ADS7870_RESULTHI);
    00086     result  = spiTransferByte(0x00)<<8;
    00087     result |= spiTransferByte(0x00);
    00088     // release chip select
    00089     ADS7870_CS_RELEASE;
    00090     // return result
    00091     return result;
    00092 }
    00093
    00094 u08 ads7870ReadReg(u08 reg)
    00095 {
    00096     u08 data;
    00097     // assert chip select
    00098     ADS7870_CS_ASSERT;
    00099     // issue reg read command
    00100     spiTransferByte(ADS7870_REG_READ | reg);
    00101     // read data
    00102     data = spiTransferByte(0x00);
    00103     // release chip select
    00104     ADS7870_CS_RELEASE;
    00105     // return data
    00106     return data;
    00107 }
    00108
    00109 void ads7870WriteReg(u08 reg, u08 value)
    00110 {
    00111     // assert chip select
    00112     ADS7870_CS_ASSERT;
    00113     // issue reg write command
    00114     spiTransferByte(ADS7870_REG_WRITE | reg);
    00115     // write data
    00116     spiTransferByte(value);
    00117     // release chip select
    00118     ADS7870_CS_RELEASE;
    00119 }
    00120