CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Driver for LTC2420 Delta Sigma ADC

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

Driver for LTC2420 Delta Sigma ADC
PostPosted: Mon Jul 06, 2009 6:17 pm     Reply with quote

Code:
/*********************************************************
*
*  LTC2420.c
*  Driver for LTC2420, SPI 20 bit Delta-Sigma  A/D converter
*  Compiled for CCS compiler V4 and up
*
*  C. Barberis
*  Bartek Technologies
*
*  Includes: None
*
**********************************************************
*
*
**********************************************************
**********************************************************
* Notes:
* Make sure to drive pin #8 with logic level from cpu, this pin controls internal filter
* for line rejection; Vcc = 50Hz and 0V = 60Hz
* Declare the appropiate I/O pin declarations for: FiltSel,ADCSEL,SDI,SDO and SCK before compiling this file
* Make sure to call (once) ExtAdcInit(); prior to reading adc values.
*********************************************************/

 //Function prototypes
 void ExtAdcInit(void);
 unsigned int32 ReadAdcValue(void);

 static short int AdcUnderFlow ;
 static short int AdcOverFlow;

  union { unsigned int32 AdcCode;
          struct {unsigned int8 AdcByte0;
          unsigned int8 AdcByte1;
          unsigned int8 AdcByte2;
          unsigned int8 AdcByte3;}AdcBytes; }AdcValue;

/////////////////////////////////////////////////////////////////////////////////

void ExtAdcInit() // Initialize LTC2420
{
   setup_spi(TRUE);
   setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_16|SPI_SS_DISABLED);   // change spi to low to high sense
   output_high(ADCSEL);
   FiltSel = 0;  // set for 60Hz filtering
   //FiltSel = 1; // set for 50Hz filtering
}

/////////////////////////////////////////////////////////////////////////////////

 unsigned int32 ReadAdcValue()     // Read LTC2420, SPI 20 bit Delta-Sigma ADC
  {
       AdcValue.AdcBytes.AdcByte3 = 0;             // highest byte of the 32 bit int is set to 0
       AdcUnderFlow = FALSE;
       AdcOverFlow = FALSE;
       output_low(ADCSEL);                          // bring ADCSEL line low to start conversion
       while(input(SDI))                            // Test for SDI low ready to start conversion
       restart_wdt();
       delay_us(500);                                // Start conversions after 500 uS delay
       AdcValue.AdcBytes.AdcByte2 = spi_read(0);     // read MSB, bits 23 to 16
       delay_us(10);                         // wait 10 uS before next byte read
       AdcValue.AdcBytes.AdcByte1 = spi_read(0);     // read bits 15 to 8
       delay_us(10);                         // wait 10 uS before next byte read
       AdcValue.AdcBytes.AdcByte0 = spi_read(0);     // read bits 7 to 0
       delay_us(10);                         // wait 10 uS before we raise ADCSEL chip enable
       output_high(ADCSEL);                          // bring ADCSEL line high to after end of conversion
       if( !bit_test(AdcValue.AdcBytes.AdcByte2,5))  // check to see if sign bit shows negative conversion
          AdcUnderFlow = TRUE;                       // if so send underflow error
       if( bit_test(AdcValue.AdcBytes.AdcByte2,4))   // check to see if extended range bit shows overflow condition
          AdcOverFlow = TRUE;                        // if so send overflow error
       if( bit_test(AdcValue.AdcBytes.AdcByte2,5) && !bit_test (AdcValue.AdcBytes.AdcByte2,4) )  // if everything is ok the mask data
             AdcValue.AdcBytes.AdcByte2&=0x0F;

       if(AdcUnderFlow) AdcValue = 0;                // if we detect an underrange contion return a zero value

       return(AdcValue.AdcCode);
                                          }


/////////////////// End of File /////////////////////////////////////////
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group