| cbarberis 
 
 
 Joined: 01 Oct 2003
 Posts: 172
 Location: Punta Gorda, Florida USA
 
 
			      
 
 | 
			
				| Driver for LTC2420 Delta Sigma ADC |  
				|  Posted: Mon Jul 06, 2009 6:17 pm |   |  
				| 
 |  
				|  	  | 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 /////////////////////////////////////////
 | 
 |  |