Thus spake Bradley, Larry (Larry.Bradley@NRC.CA): > Has anyone written code for the MAXIM 186 8-channel serial A/D converter > (or any of the MAX serial A/Ds? If no-one has, I'll have to, and will Here is some C code for driving the MAX186 from an XA micro - it should at least give you a starting point. Make sure you do the timing calculations to ensure all the parameters are met (should not be a problem with a PIC). This code uses internal clock mode - using external clock mode can result in faster throughput, if done properly. static far unsigned char ADIN @ 0xF8000; /* input port */ #define SSTRB 0x01 /* strobe is bit 1 */ #define DATAIN 0x02 /* input data bit */ static far unsigned char ADOUT @ 0xE0000; /* output port (leds too */ #define SSCLK 0x02 /* output clock bit */ #define DATAOUT 0x01 /* output data bit */ #define ADCS T1 /* T1 selects the ADC */ /* Fuction: write_word * Descrip: Write one byte to the ADC. The MSB of val must be set. * Arguments: * Returns: */ static void write_word(unsigned char val) { unsigned char i, j; ADOUT = SSCLK; /* set clock high */ T1 = 0; /* select chip */ i = 8; /* the timing of this loop is such that the 1.5uS sample and hold time is met in the last 3 bits, even at 30MHz processor clock */ do { j = (val >> 7) & 0x1; /* get a bit */ ADOUT = j; /* drive clock low */ val <<= 1; ADOUT = SSCLK | j; /* clock high */ } while(--i); ADOUT = 0; /* set clock low */ ADCS = 1; /* deselect chip */ } /* Fuction: read_word * Descrip: Read two bytes from the ADC, deselect CS. It assumes * write_adc has been called to start the conversion. * Arguments: * Returns: */ static unsigned int read_word(void) { unsigned char i; unsigned int val; ADCS = 0; /* select the chip */ for(val = 0xFFFF ; --val ; ) if(ADIN & SSTRB) /* wait for completion */ break; if(!(ADIN & SSTRB)) /* timeout? Return impossible value */ return 0xFFFF; val = 0; i = 12; /* we only need 12 bits */ do { ADOUT = SSCLK; /* clock high */ val <<= 1; ADOUT = 0; /* clock low */ if(ADIN & DATAIN) val |= 1; } while(--i); ADCS = 1; /* deselect chip */ return val; } /* Fuction: adc * Descrip: Perform a conversion on the specfied channel * Arguments: * Returns: */ unsigned int adc(unsigned char chan) { write_word((chan << 4) | 0x8E); return read_word(); } -- Clyde Smith-Stubbs | HI-TECH Software, | Voice: +61 7 3354 2411 clyde@htsoft.com | P.O. Box 103, Alderley, | Fax: +61 7 3354 2422 http://www.htsoft.com | QLD, 4051, AUSTRALIA. | --------------------------------------------------------------------------- Download a FREE beta version of our new ANSI C compiler for the PIC microcontroller! Point your WWW browser at http://www.htsoft.com/