/*  AD_INT.C
 *
 *  Analog Input Routines for the SBC1190 with 8 Channel A/D option
 *
 *  This example uses the special Micro/sys system interrupt to
 *  read the onboard A/D convertor.
 *
 *  8/3/98
 */


#include <conio.h>
#include <dos.h>
#include <bios.h>


#define RNGE5VUNIP         0    /*  0  to +5v range  */
#define RNGE5VBIP          1    /* -5  to +5v range  */
#define RNGE10VUNIP        2    /*  0  to +10v range */
#define RNGE10VBIP         3    /* -10 to +10v range */


/*-----------------------------------------------------------------------*     
 *  analogrd(int chan)                                                   *
 *          This function reads analog input given a channel             *
 *          number of 0 to 7                                             *
 *  Return value : Integer { -2047 to +2048 } or { 0 to 4095 }           *
 *-----------------------------------------------------------------------*/
int analogrd(int chan)
{
	union REGS regs86;
	int value;

        regs86.h.ah = 0xDD;     /* special Micro/sys system function */

	regs86.h.al = 0x20;     /* start ADC conversion */

	regs86.h.bh = RNGE10VBIP;  /* 10v bipolar range */

	regs86.h.bl = chan;

	int86(0x1A, &regs86, &regs86);


	for(;;)
	{
                regs86.h.ah = 0xDD;     /* special Micro/sys system function */

		regs86.h.al = 0x21;     /* check ADC conversion */

		int86(0x1A, &regs86, &regs86);

		if (regs86.h.bl)        /* if conversion is done, read value */
			return (regs86.x.ax);
	}
}



void main(void)
{
	int key,n;
	float adcval;


	printf("Reading 8 channels\n\n");

	printf("Ch0    Ch1    Ch2    Ch3    Ch4    Ch5    Ch6    Ch7\n");


	for(;;)
	{
		for(n=0; n<8; n++)
		{
			adcval = (float)analogrd(n)*10.0/2048.0 +.005;
			printf("%-+6.2f ", adcval);
		}
		printf("\r");
	}
}