asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
driver for ADS8317 ADS8320 and related 16Bit AD conversion |
Posted: Sat Jun 05, 2010 7:09 pm |
|
|
Code: |
//
// generic driver for ADS83xx serial 16 bit A/D converters
// inc ADS8317, ADS8320, x18, x21 and many other 8 pin similar parts
// both signed 16bit and unsigned types
// Don Person agw1@nycap.rr.com GentleWind Inc.
//
// define your control pins here - 3 are required
#define ADCstart PIN_E0 //
#define ADCdata PIN_E1 //
#define ADCclock PIN_E2 //
// if using fast_io for E ( suggested - I do ) then set_tris_E(0b11111010)
// NB: this assumes you are using a WEAK resistor pullup on
// the designated ADCdata PIC port pin - even if an external resistor to Vcc
//
// This gets one sample 16 bit unsigned word from serial A/D
//
unsigned int16 ADS83xx(void){ // SERIAL A/D ADS8317,20 and many others
unsigned int16 adcresult=0;
unsigned int8 i;
adcresult=0;
output_HIGH(ADCclock); delay_cycles(4);
// Start a conversion with a lo pulse on ADC_CONVERT
output_low (ADCstart); // LOWER NOT-CS
// NOW CYCLE TILL WE GET THE ACTIVE FIRST BIT ZERO READY
// ADCclock is clock DEFLT LOW
delay_cycles(1); // not needed on master clock speed s below 10 mhz
output_LOW(ADCclock); // clock low
delay_cycles(1); // not needed on master clock speed s below 10 mhz
for ( i = 0 ; i < 8 ; i++ ){
output_HIGH(ADCclock);
IF ( !input(ADCdata)) BREAK;
ELSE output_LOW(ADCclock);
}
output_low(ADCclock); // insure lo clock for a 'break' on HI state
// now read data
for (i=0;i<16;i++){
output_high(ADCclock); // now after rising edge
adcresult |= input(ADCdata); // OR input pin with 16bit int
output_low(ADCclock); // produce a new lo-to-high clock
if (i !=15) adcresult=adcresult<<1; // and shift that bit along toward MSB
}
// leave ready for the next A/D read CYCLE
OUTPUT_HIGH(ADCstart); // adcstart SHUT OFF CONVERTER
return (ADCresult);
}
|
|
|