/***************************************************************************** * SAMPLE3.C : DMA DATA ACQUISITION * * * * This is a demonstration program to be used with the AD12-16 A/D * * board. The program will display sixteen channels using DMA transfers. * * When all transfers are complete the DMA chip generates an interrupt to * * inform the driver that it is done. The timer starts the conversion and at* * the end of conversion the board genertes a DMA request. This demonstrates* * setting the timers(task 17),setting the scan limits(task 1), performing * * DMA transfers after conversions(task 6), checking for completion of back * * ground task(task 8) and converting the readings returned by the board * * into channel and counts(task 9). * * This program must be built using the large model. * * * * The board should be set as follows: * * -- IRQ5 is used for interrupts * * -- Polarity set to bipolar * * -- DMA channel 1 * * -- On board clock set to 1 MHz * * * * LAST MODIFICATION: 2/3/98 * * * *****************************************************************************/ #include #include #include #include #include #include "a16drvc.h" /* These variable MUST be declared as global. They are the ones whose offsets are passed to the A16DRV routine. If they are not global then the driver will not find their segment. */ unsigned pntbuf[100],statcode,task; int datbuf[100],params[5]; 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: a16drv - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************/ unsigned call_driver() { unsigned *temp1,offtask,offparams,offstatcode; int *temp; /* this section extracts the offset of the parameters that we will pass to the assembly driver */ temp1 = &task; offtask = FP_OFF(temp1); /* offset of task number */ temp = params; offparams = FP_OFF(temp); /* offset of param list */ temp1 = &statcode; offstatcode = FP_OFF(temp1); /* offset of status code */ statcode = 0; a16drv(offtask,offparams,offstatcode); /* call the driver */ /* this section checks for an error code */ if (statcode > 0) { 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 to read all 16 A/D channels. * * * * INPUT: None. * * * * CALLS: call_driver - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************/ unsigned setup(Address) { unsigned I,status; /* initialize the board */ params[0] = Address; /* starting board address */ params[1] = 5; /* IRQ5 */ params[2] = 1; /* DMA channel 1 */ task = 0; status = call_driver(); /* call driver */ if (status > 0) return(status); /* if status > 0 then error */ /* set up the timers to divide the 1 MHz clock by 1000 to get 100Hz */ task = 17; params[0] = 100; /* second counter divide by 100 */ params[1] = 10; /* first counter divide by 10 */ status = call_driver(); /* call the driver */ return(status); } /* end setup */ /***************************************************************************** * FUNCTION: get_readings -- local routine * * * * PURPOSE: Reads the 16 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() { int *temp1; unsigned I,status,*temp,offpntbuf,offdatbuf; /* set the scan limits for channel 0 to channel 15 */ task = 1; params[0] = 0; /* lower scan limit */ params[1] = 15; /* upper scan limit */ status = call_driver(); /* call the driver */ if (status > 0) return(status); /* set up to do timer driven DMA. The timer will start conversions at a 100Hz rate. The destination segment could be any unused area of memory. Make sure it is unused! When compete, the DMA chip will] interrupt and the driver will close the process.*/ task = 6; params[0] = 16; /* take 16 samples */ params[1] = 0x7000; /* destination, hope it's unused! */ params[2] = 1; /* use the timer for triggering */ params[3] = 0; /* do one cycle only */ status = call_driver(); if (status > 0) return(status); /* exit if error */ /* this loop waits for the driver to signal that it has completed all conversions. If this were an application, other task could be performed within this loop while waiting for the conversion to be completed. */ do { task = 8; status = call_driver(); } while (params[1] == 1); /* this will be 0 when done */ /* we now can convert the values taken by task 5 into channel and counts and store in seperate arrays. This needs to be done because the board returns a conversion with the count in the upper 12 bits of a 16 bit integer and the channel number in the lower 4 bits. */ /* calculate the offset to the buffers so the fdriver 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); task = 9; params[0] = 16; params[1] = 0x7000; /* task 5 destination segment */ params[2] = 0; /* start with first value */ params[3] = offdatbuf; /* offset of array for count data */ params[4] = offpntbuf; /* offset of array for channel data */ status = call_driver(); /* call the driver */ 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 < 16;I++) { printf(" %5i %5i\n",pntbuf[I],datbuf[I]); } 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 int Address; unsigned status; char ch; clrscr(); printf( " SAMPLE3.C : DMA DATA ACQUISITION \n\n" "This is a demonstration program to be used with the AD12-16 A/D \n" "board. The program will display sixteen channels using DMA transfers. \n" "When all transfers are complete the DMA chip generates an interrupt to \n" "inform the driver that it is done. The timer starts the conversion and at\n" "the end of conversion the board genertes a DMA request. This demonstrates\n" "setting the timers(task 17),setting the scan limits(task 1), performing \n" "DMA transfers after conversions(task 6), checking for completion of back \n" "ground task(task 8) and converting the readings returned by the board \n" "into channel and counts(task 9). \n" " \n"); Address=AskForBaseAddress(0x350); clrscr(); printf( "Board Configuration Requirements: \n\n" " -- Base Address is %X hex \n" " -- IRQ5 is used for interrupts \n" " -- Polarity set to bipolar \n" " -- DMA channel 1 \n" " -- On board clock set to 1 MHz \n",Address); puts(" -- All remaining jumper settings are irrelevant."); printf("\n\n\n\nPress Any Key to Begin, or press E to Exit the program"); ch = getch(); clrscr(); status = setup(Address); /* set up program and the driver */ if (status != 0) return; /* is status > 0 then board error */ while (ch != 'E' && ch != 'e') { /* display current values for the 16 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 */ } clrscr(); } /* end main program */