16.7 Wait for the Conversion to Finish

The A/D converter chip takes up to 5us to complete one A/D conversion. Most processors and software can operate fast enough so that if you try to read the A/D converter immediately after starting the conversion, the read will occur before the A/D conversion completes and return invalid data. Therefore, the data acquisition circuit provides a status bit ADBUSY to indicate whether the A/D circuit is busy or idle. This bit can be read back from the status register at Base+3, bit 7. When the A/D converter is busy (performing an A/D conversion), ADBUSY=1 and the program must wait. When the A/D converter is idle (conversion is done and data is available), ADBUSY=0 and the program may read the data.

while (inp(base+3) & 0x80); // wait for ADBUSY to go low, base+3 bit 7

The above example could hang your program if there is a hardware fault and the bit is stuck at 1. A better solution is to use a loop with a timeout, as shown below.

int checkstatus() // returns 0 if ok, -1 if error

{

int i;

for (i = 0; i < 20000; i++)

if (!(inp(base+3) & 0x80)) return(0); // conversion completed

return(-1); // conversion did not complete

}

Last updated