/*-------------------------------------------------------------------- * C LANGUAGE SAMPLE 0 : * * Sample program for the Analog and Digital I/O card * demonstrating register level data aquistion from the * * Last Modification : 18 November 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 sixteen analog inputs and "); puts("displays them for you."); puts("\n"); puts("\n"); puts("Board Configuration Requirements:"); puts("\n"); puts("RANGE: 5 Volt Range"); puts("POLARITY: Bipolar"); 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, i; float volts; char c = '?'; intro(); outportb(base+2, 0xF0); /* Set scan from Ch0 to Ch15 */ while (c != 'E') { clrscr(); printf(" Channel Counts Volts\n"); printf(" ------- ------ -----\n"); do { outportb(base, 0); /* Start A/D conversion */ i = 0; /* Wait for EOC or timeout */ while ( ((inportb(base+8) & 0x80) >= 0x80) || (i < 1000) ) i++; if (i >= 1000) /* if timeout, display error & quit */ { channel = inportb(base) & 0x0F; fprintf(stderr,"\nError: Timeout on channel %d\n\n", channel); exit(1); } channel = inportb(base) & 0x0F; /* Get channel number */ counts = (unsigned)(inport(base) >> 4); /* Get channel counts */ /* Scaling 5v range on 12 bit device = 0.00244v/count */ volts = (counts-2048)*0.00244; printf("%14d%12d%12.3f\n",channel,counts,volts); /* Show data */ } while ((inportb(base) & 0x0F) != 0x0F); /* while not Ch15 */ printf("\n Press 'E' to exit program\n"); while (!kbhit()) ; c = toupper(getch()); } /* end of while not 'E' */ } /* end of main */