/*	Subttl	Read Pin Voltage*/
/******************************************************************************/
/* Read universal device pin voltage.  This performs a successive	      */
/* approximation upon the pin.	The threshold DAC is initially set to the     */
/* mid-point value and then the pin is read from the specified pin.  The pin  */
/* number is used as a reference into the read_pin_table to derive the mask   */
/* and device address and the pin is read and ANDed with its mask. If this    */
/* value is a one, one half of the last guess is added to the guess other-    */
/* wise, one half of the last guess is subtracted from the last guess.	The   */
/* new last guess is used as the new value for the threshold DAC and is       */
/* repeated until the last guess offset is zero.  This value of the approx-   */
/* imation is return.							      */
/*									      */
/* Enter:   pin 		= pin number to read			      */
/*									      */
/* Call:    read_pin_voltage (pin)					      */
/*									      */
/* Return:  approximate voltage of the specified pin			      */
/******************************************************************************/

read_pin_voltage (pin)
unsigned int pin;
{
    unsigned int approx;
    unsigned int guess;

    approx = 0; 						/* initial approximation			*/

    for (guess = 0x80; guess > 0; guess >>= 1)			/* addend and control				*/
    {
	udi_write_mem (THRESH_HOLD_DAC, approx + guess);	/* set comp drive				*/

	if (udi_read_bit (pin)) 				/* was it too low?				*/
	{
	    approx = approx + guess;				/* yes - restore AND add			*/
	}
    }

    return (approx * 10);					/* return pinvalue				*/
}
