/***************************************************************************** * C LANGUAGE SAMPLE 1: SAMPLE1.C * * * * This is a demonstration program to be used with the AD12-8 A/D * * board. The program will display eight channels of data polled from the * * board using the A12DRVC.OBJ driver module supplied with the board. * * This program must be built using the large model. * * * * LAST MODIFICATION: 2/3/98 * * * *****************************************************************************/ #include #include #include #include #include "a12drvc.h" #include #include #include #include /* These variable MUST be declared as global. They are the ones whose offsets are passed to the A12DRV routine. If they are not global then the driver will not find their segment. */ /* Note that the 200 array size,below, is for convenience only, as the arrays will only contain eight points apiece. */ unsigned pntbuf[200],statcode,task; int datbuf[200],params[5]; unsigned get_readings(void); unsigned call_driver(void); unsigned AskForBaseAddress(unsigned int OldOne) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("\nPlease 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 */ /***************************************************************************** * FUNCTION: call_driver -- local routine * * * * PURPOSE: Performs the call to the driver package. * * * * INPUT: None. * * * * CALLS: a12drv - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************/ unsigned call_driver() { a12drv(FP_OFF(&task),FP_OFF(params),FP_OFF(&statcode)); /* this section checks for an error code */ if (statcode > 0) { puts("\n"); printf("A status error code of %i was detected.\n",statcode); printf("Program terminated."); return(statcode); } else return(0); } /* end call_driver */ /***************************************************************************** * FUNCTION: setup -- local routine * * * * PURPOSE: Sets up the driver package, and sets the scaling for each of * * the 8 A/D channels. * * * * INPUT: None. * * * * CALLS: * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************/ unsigned setup() { unsigned int Address; unsigned I,status; clrscr(); puts(" SAMPLE 1.C : AD12-8 \n"); puts("This sample reads the eight analog inputs from the AD12-8 and "); puts("displays them for you.\n"); Address=AskForBaseAddress(0x350); clrscr(); puts("\n\n\nBoard Configuration:"); puts("\n"); printf(" -- BASE ADDRESS: %x hex\n", Address); puts(" -- RANGE: 5 Volt Range (required)"); puts(" -- POLARITY: Bipolar (required)"); puts(" -- All remaining jumper settings are irrelevant"); puts("\n"); puts("\n"); puts("\n"); puts("Please press any key to continue."); getch(); params[0] = 1; /* manual initialization */ params[1] = Address; /* starting board address */ params[2] = 3; /* IRQ3 */ params[3] = 5; /* 5V range */ params[4] = 1; /* bipolar mode */ task = 0; status = call_driver(); /* call routine to call ext module */ if (status > 0) return(status); /* if status > 0 then error */ task = 11; /* Set up a small delay for fast computers */ params[0]=5; params[1]=10; status=call_driver(); if (status > 0) return(status); /* now reset the task list index */ task = 11; params[0] = 1; status = call_driver(); /* call routine to call ext module */ if (status > 0) return(status); /* if status > 0 then error */ /* set up the driver to scale counts to volts */ for (I = 0;I <= 7;I++) { task = 10; params[0] = 3; params[1] = I * 16; params[2] = -5000; /* -5000 to 5000 is 10 volt bipolar range */ params[3] = 5000; /* which for this program is +-5V */ status = call_driver(); /* call routine to call ext module */ if (status > 0) return(status); /* if status > 0 then error */ } return(0); } /* end setup */ /***************************************************************************** * FUNCTION: get_readings -- local routine * * * * PURPOSE: Reads the 8 A/D channels and displays them on the screen. * * * * INPUT: None. * * * * CALLS: call_driver - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * *****************************************************************************/ unsigned get_readings() { unsigned channel,I,status; int *temp1; double data; unsigned *temp,offpntbuf,offdatbuf; /* now reset the task list index */ task = 11; params[0] = 1; status = call_driver(); /* call routine to call ext module */ if (status > 0) return(status); /* if status > 0 then error */ task = 8; /* calculate the offset to the buffers so the driver can return the data. datbuf will contain the data read from the channels and pnt buffer will contain the channel and gain info. */ temp1 = datbuf; offdatbuf = FP_OFF(temp1); temp = pntbuf; offpntbuf = FP_OFF(temp); params[0] = offdatbuf; /* offset to data buffer */ params[1] = offpntbuf; /* offset to point bufffer */ params[2] = 8; /* make 8 readings from point list */ params[3] = 0; /* number of samples returned here */ status = call_driver(); /* call routine to call ext module */ if (status > 0) return(status); /* if status > 0 then error */ /* now if OK list the data to the screen */ clrscr(); printf("\n CHANNEL VALUE\n"); printf(" ------- ------\n"); for (I = 0;I <= 7;I++) { channel = (pntbuf[I] & 0xff00) / 256; /* upper bits contain ch number */ data = datbuf[I] * 0.001; /* scale millivolts to volts */ printf(" %i",channel); gotoxy(17,I+4); printf("%6.3f\n",data); } return(0); } /* end get_readings */ /***************************************************************************** * FUNCTION: main -- local routine * * * * PURPOSE: Controls program flow, detects when user is ready to exit. * * * * INPUT: None. * * * * CALLS: setup - set up program and drivers. * * get_readings - read the A/D channels and display. * * * * OUTPUT: None. * *****************************************************************************/ void main() { unsigned status; char ch; status = setup(); /* set up program and the driver */ if (status != 0) return; /* is status > 0 then board error */ do { /* display current values for the 8 channels */ status = get_readings(); if (status != 0) return; /* if status > 0 then error */ /* check for program exit */ printf("\nPress 'E' to exit the program. Press any other key to rescan the data..."); while (!kbhit()); /* wait for key press */ ch = getch(); /* read the char */ } while (ch != 'E' && ch != 'e'); } /* end main program */