#include #include #include #include #include #include #include #define uint unsigned int #define INT 5 int IRQGenerated = 0; uint BASE=0x340; 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 interrupt far (*oldirq)(); void interrupt ADC_interrupt() { IRQGenerated = 1; inp(BASE+1); outp(0x20,0x20); // reset PIC } void main(void) { int data; int chan=0; clrscr(); puts("Sample 7\n"); puts("This sample will read data from all channels on the A/D board using\n" "timer/counter #2 to time and start conversions. End of conversion\n" "will be detected by IRQ generation."); puts("Card MUST be set at IRQ5."); puts("All other jumper settings are irrelevant."); BASE=AskForBaseAddress(BASE); outportb(BASE + 0x18, 1); oldirq = getvect(INT+8); // save old ISR setvect(INT+8, ADC_interrupt); // set new ISR outp( 0x21, inportb( 0x21) & ~(1 << INT)); // enable PIC outp(0x20, 0x20); // clear pic 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,0xEA);//allow counter to start conversions inportb(BASE+1);//clear existing interrupts while(!kbhit()){ while ((!IRQGenerated) && (!kbhit())); IRQGenerated = 0; data=(inport(BASE+6)>>4) & 0x0FFF; //read data from conversion gotoxy(10,12+chan); printf("Channel: %i Data Read:%4x",chan,data); chan++; chan%=8; outportb(BASE+2,chan);//setup next channel } getch(); gotoxy(1,20); outportb(BASE+0,0x00);//turn off IRQs and counters outp(0x21, (inp(0x21) | (1 << INT))); // disable interrupt; setvect(INT+8, oldirq); // restore old ISR }