/*-------------------------------------------------------------------- * C LANGUAGE SAMPLE 0 * * Sample program for the A/D Converter/Counter-Timer * demonstrating register level data aquistion. * * Last Modification : 2 December 1994. *------------------------------------------------------------------*/ #include #include #include #include #include unsigned base; unsigned AskForBaseAddress(unsigned int OldOne) { char msg[81]; unsigned int NewOne=0; window(5,5,75,19); textcolor(WHITE); textbackground(BLUE); clrscr(); window(10,7,70,17); cprintf("\n\n\nPlease enter the Base Address for your card (in hex)\n\r" "or press ENTER for %X.\n\r" ">",OldOne); msg[0]=5; cgets(msg); sscanf(msg+2,"%x",&NewOne); textcolor(LIGHTGRAY); textbackground(BLACK); window(1,1,80,24); clrscr(); return ((NewOne>=0x100)&&(NewOne<=0x3f0)) ? NewOne : OldOne; } /* end of AskForBaseAddress */ void intro(void) { clrscr(); puts("Sample 0"); puts("\n"); puts("This sample reads the eight analog inputs and "); puts("displays them for you."); puts("\n"); puts("Board Configuration Requirements:"); puts("\n"); puts("RANGE: 5 Volt Range"); puts("POLARITY: Bipolar"); puts("\n"); puts("\n"); puts("\n"); puts("Please press any key to set base address."); getch(); clrscr(); base = AskForBaseAddress(0x300); clrscr(); printf("Address entered was %X hexidecimal.\n", base); printf("Is this the correct address for your configuration? (Y/n)"); while(!kbhit()); if (toupper(getch()) == 'N') { printf("\n\nPlease restart program and enter correct address.\n"); exit(1); } } /* end of Intro */ void main(void) { unsigned char channel; int counts; float volts; int i,j; char c = '?'; intro(); while (c != 'E') { clrscr(); printf(" Channel Counts Volts\n"); printf(" ------- ------ -----\n"); /*THIS IS THE DATA ACQUISITION SECTION =====================================*/ for (channel = 0; channel <= 7; channel++) { outportb(base+2, channel); /* Set channel */ for (i = 0; i < 1000; i++) /* Wait for settle count */ ; outportb(base+1,0); /* Start A/D conversion */ j = 0; /* Wait for EOC or timeout */ while (((inportb(base+2) & 0x80) >= 0x80) || (j < 1000)) j++; counts = inport(base) >> 4; /* Read count data */ /* Scaling 5v range on 12 bit device = 0.00244v/count */ volts = ((float)counts-2048.0)*0.00244; /* Display results */ printf("%14d%12d%12.3f\n",channel,counts,volts); } /*=======================================================================*/ printf("\n Press 'E' to exit program\n"); printf(" Press any other key to update readings"); while (!kbhit()) ; c = toupper(getch()); } /* end of while not 'E' */ } /* end of main */