Correction to the code for single-chip ADC


On further experimenting with the chip, it was found that the following code gave the correct conversion:

FUNCTION ADC (chan%)
DIM i AS INTEGER, bits AS INTEGER

'CONST CLKbar = 1  - a reminder of the definitions
'CONST DINbar = 2
'CONST CS     = 4
'CONST VDDbar = 8

'CONST CLKlo =  CLKbar + DINbar
'CONST CLKhi =  DINbar

bits = 0

OUT pout, CLKlo            'VDDbar=0, CS=true

OUT pout, CLKlo - DINbar   'Start bit
OUT pout, CLKhi - DINbar   'clock it
OUT pout, CLKlo - DINbar

OUT pout, CLKlo - DINbar   'First bit 1 for single ended
OUT pout, CLKhi - DINbar   'clock it
OUT pout, CLKlo - DINbar

OUT pout, CLKlo            'don't care - so output zero
OUT pout, CLKhi
OUT pout, CLKlo
IF (chan% AND 2) THEN  'High bit of channel number
   OUT pout, CLKlo - DINbar
   OUT pout, CLKhi - DINbar
   OUT pout, CLKlo - DINbar
ELSE
    OUT pout, CLKlo
    OUT pout, CLKhi
    OUT pout, CLKlo
END IF

IF (chan% AND 1) THEN  'Low bit of channel number
    OUT pout, CLKlo - DINbar
    OUT pout, CLKhi - DINbar
    OUT pout, CLKlo - DINbar
ELSE
    OUT pout, CLKlo
    OUT pout, CLKhi
    OUT pout, CLKlo
END IF

    OUT pout, CLKlo
    OUT pout, CLKhi   'The chip samples input now
 
    OUT pout, CLKlo   'null bit starts now
    OUT pout, CLKhi

FOR i = 0 TO 12
    OUT pout, CLKlo   'next bit ready now, MSB first
    bits = 2 * bits + (INP(pinp) AND &H10) \ &H10
    OUT pout, CLKhi
NEXT

ADC = bits / 4096! - 1

OUT pout, SHDN + CLKbar + DINbar   'deselect, CSbar high

END FUNCTION