#include #include #include #include #include #include #include #define uint unsigned int #define CHECKFORBUSY(BASE) (inportb(BASE+0) & 0x80) void CtrMode(uint addr, char cntr, char mode) { int ctrl; ctrl = (cntr << 6) | 0x30 | (mode << 1); outportb(addr+3,ctrl); } void CtrLoad(uint addr ,int c,int val) { outportb(addr+c,val & 0x00FF); outportb(addr+c,(val>>8) & 0x00FF); } uint CtrRead(uint addr , int c) { outportb(addr+3,c<<6); return inportb (addr+c) + (inportb(addr+c) << 8); } unsigned AskForBaseAddress(unsigned int OldOne) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("Please enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 5; msg[1] = 0; cgets(msg); sscanf(msg + 2, "%x", &NewOne); Success = 1; Dummy = NewOne; if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } while(!Success); return (Dummy); } /* end of AskForBaseAddress */ void main(void) { uint BASE=0x300; int data; int chan=0; unsigned long int timeout = 655354L; clrscr(); puts("Sample 5\n"); puts("This sample will read data from all channels on the A/D card using\n" "timer/counter #2 to time and start conversions."); BASE=AskForBaseAddress(BASE); outportb(BASE + 0x18, 1); CtrMode(BASE+12,0,2);//all counters in mode 2 CtrMode(BASE+12,1,2);//all counters in mode 2 CtrMode(BASE+12,2,2);//all counters in mode 2 //counter zero without count, it won't increment, cause we don't need it. CtrLoad(BASE+12,1,0x00FF); CtrLoad(BASE+12,2,0x00FF); outportb(BASE+2,chan);//setup channel and gain (gain code 0, gain of 1) outportb(BASE+0,0xE2);//allow counter to start conversions while(!kbhit()){ timeout=655354L; while(CHECKFORBUSY(BASE) && (--timeout));//wait for start of conversion while(!CHECKFORBUSY(BASE) && (timeout--)); //wait for end of conversion data=inport(BASE+2) & 0x0FFF; //read data from conversion gotoxy(10,10+chan); printf("Channel: %i Data Read:%4x",chan,data); if (timeout==0) puts(" A/D Timeout Error");else clreol(); chan++; chan%=8; outportb(BASE+2,chan);//setup next channel } gotoxy(1,20); }