#include #include #include #include #include #include "kbinp.h" #define uint unsigned int #define CHECKFORBUSY(BASE) (inb(BASE+2) & 0x80) void IOPermission(unsigned abase) { if(ioperm(abase,16,1)<0) { fprintf(stderr, "NO IO PERMISSION\n"); } } void CtrMode(uint addr, char cntr, char mode) { int ctrl; ctrl = (cntr << 6) | 0x30 | (mode << 1); outb(ctrl,addr+3); } void CtrLoad(uint addr ,int c,int val) { outb(val & 0x00FF,addr+c); outb((val>>8) & 0x00FF,addr+c); } uint CtrRead(uint addr , int c) { outb(c<<6,addr+3); return inb(addr+c) + (inb(addr+c) << 8); } unsigned AskForBaseAddress(unsigned int OldOne) { char msg[3]; unsigned int newone; unsigned int temp = 0; int index = 0; int letter; puts("Please enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); while(temp==0) { newone=0; index=0; while((letter = getchar())!='\n') //Read char by char and store in Msg msg[index++] = letter; sscanf(msg, "%X", &newone); if(newone==0){ //Check to see if address was inputed temp=OldOne; //Assign default if not } else if((newone>=0x100)&&(newone<=0x3f0)){ //Check to see if inputed addres is valid temp=newone; } else { //If Newone invalid ask for new value. printf("\nEnter the Base Address or press ENTER for %x > ", OldOne); for(index=0; index<=3; index++) msg[index]=0; } //Run while until temp = valid address } return(temp); } int main(void) { uint BASE=0x340; int data; int chan=0; unsigned long int timeout = 655354L; system("clear"); 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.\n"); IOPermission(BASE); BASE=AskForBaseAddress(BASE); 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); outb(chan,BASE+2);//setup channel and gain (gain code 0, gain of 1) outb(0xE2,BASE+0);//allow counter to start conversions do{ timeout=655354L; while(!CHECKFORBUSY(BASE) && (--timeout));//wait for start of conversion while(CHECKFORBUSY(BASE) && (timeout--)); //wait for end of conversion data=(inb(BASE+6)>>4) & 0x0FFF; //read data from conversion system("tput cup 8 0"); printf("Channel: %i Data Read:%4x\n",chan,data); if (timeout==0) puts(" A/D Timeout Error\n"); chan++; chan%=8; outb(chan,BASE+2);//setup next channel }while(kbhit()==0); return 0; }