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 <avr/io.h>
00023 #include <avr/interrupt.h>
00024 
00025 #include "global.h"
00026 #include "spi.h"
00027 #include "ads7870.h"
00028 
00029 // global variables
00030 
00031 // Functions
00032 u08 ads7870Init(void)
00033 {
00034     // initialize spi interface
00035     spiInit();
00036     // switch to f/4 bitrate
00037     cbi(SPCR, SPR0);
00038     cbi(SPCR, SPR1);
00039     //sbi(SPSR, SPI2X);
00040 
00041     // setup chip select
00042     sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00043     sbi(ADS7870_CS_DDR, ADS7870_CS_PIN);
00044 
00045     // check ID register
00046     if(ads7870ReadReg(ADS7870_ID) != ADS7870_ID_VALUE)
00047         return 0;
00048 
00049     // setup reference and buffer
00050     ads7870WriteReg(ADS7870_REFOSC, ADS7870_REFOSC_OSCE | ADS7870_REFOSC_REFE | ADS7870_REFOSC_BUFE);
00051 
00052     // return success
00053     return 1;
00054 }
00055 
00056 s16 ads7870Convert(u08 channel)
00057 {
00058     // set single-ended channel bit
00059     channel |= ADS7870_CH_SINGLE_ENDED;
00060     // do conversion
00061     return ads7870ConvertRaw(channel);
00062 }
00063 
00064 s16 ads7870ConvertDiff(u08 channel)
00065 {
00066     // clear single-ended channel bit
00067     channel &= ~ADS7870_CH_SINGLE_ENDED;
00068     // do conversion
00069     return ads7870ConvertRaw(channel);
00070 }
00071 
00072 s16 ads7870ConvertRaw(u08 channel)
00073 {
00074     s16 result;
00075     // assert chip select
00076     cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00077     // start conversion
00078     spiTransferByte(ADS7870_CONVERT | channel);
00079     // wait for completion
00080     while( ads7870ReadReg(ADS7870_GAINMUX) & ADS7870_GAINMUX_CNVBSY);
00081     // assert chip select
00082     cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00083     // read result
00084     spiTransferByte(ADS7870_REG_READ | ADS7870_REG_16BIT | ADS7870_RESULTHI);
00085     result  = spiTransferByte(0x00)<<8;
00086     result |= spiTransferByte(0x00);
00087     // release chip select
00088     sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00089     // return result
00090     return result;
00091 }
00092 
00093 u08 ads7870ReadReg(u08 reg)
00094 {
00095     u08 data;
00096     // assert chip select
00097     cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00098     // issue reg read command
00099     spiTransferByte(ADS7870_REG_READ | reg);
00100     // read data
00101     data = spiTransferByte(0x00);
00102     // release chip select
00103     sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00104     // return data
00105     return data;
00106 }
00107 
00108 void ads7870WriteReg(u08 reg, u08 value)
00109 {
00110     // assert chip select
00111     cbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00112     // issue reg write command
00113     spiTransferByte(ADS7870_REG_WRITE | reg);
00114     // write data
00115     spiTransferByte(value);
00116     // release chip select
00117     sbi(ADS7870_CS_PORT, ADS7870_CS_PIN);
00118 }
00119 

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