/**************************************************************************** * This sample will prompt the user for a voltage between 0 and 10 volts, * * then calculate the actual voltage based on the resolution of the DAC and* * output the voltage to the desired DAC channel. * ****************************************************************************/ #include #include #include /* Global card base address */ unsigned base; /**************************************************************************** * FUNCTION: write_DAC() - local routine * * PURPOSE: Prompts the user for DAC number and voltage, then calculates * * the actual output voltage based on resolution, displays it * * and writes the value to the DAC. * ****************************************************************************/ void write_DAC(void) { float volt_value_entered,volt_value_expected; unsigned counts_entered,dac_number; /* prompt for the DAC number and desire voltage. */ printf("Enter the DAC number (0 through 15 only): "); scanf("%u",&dac_number); printf("\n\nEnter a voltage between 0.000v to 9.997v: "); scanf("%f",&volt_value_entered); /* convert entered voltage to associated number of DAC counts. Scale factor for 12 bit resolution of DA12-16 is 0.002442. */ volt_value_entered /= 0.002442; /* compute the digital output needed for this value and the actual voltage that will be expected, and display the voltage. */ counts_entered=(int) volt_value_entered; volt_value_expected = (float) counts_entered * 0.002442; printf("\nDue to the 12-bit resolution of the DAC, you should expect\n"); printf("to see a voltage of %4.3f.",volt_value_expected); /* write to the DAC */ outport(base+(2 * dac_number),counts_entered); /* write low byte */ } /* end write_DAC */ /*************************************************************************** * FUNCTION: AskForBaseAddress * * PURPOSE: Prompt user to enter base address for their I/O card. * **************************************************************************/ 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: intro * * PURPOSE: Present user with introductory screen. Get base address. * **************************************************************************/ void intro(void) { clrscr(); printf( " C SAMPLE #1: SAMPLE1.C \n\n" " This sample will prompt the user for a voltage between 0 and 10 volts, \n" " then calculate the actual voltage based on the resolution of the DAC and\n" " output the voltage to the desired DAC channel. \n\n" " The following setup of the board is expected: \n\n" " þ All DAC voltage ranges should be set to 0v-10v unipolar. \n\n"); puts("\n"); puts("Press any key to continue ... "); getch(); clrscr(); base = AskForBaseAddress(0x350); clrscr(); } /* end of intro */ /**************************************************************************** * FUNCTION: main() - local routine * ****************************************************************************/ void main() { int key_entered, Ch; intro(); /* Set all 16 buffers to 0 */ for (Ch = 0; Ch < 16; Ch++) outport(base + (Ch*2), 0x000); inportb(base+10); // set automatic update mode inportb(base+15); // release zero latch do { clrscr(); write_DAC(); printf("\n\nWould you like to output another value (Y or N)? "); key_entered = getch(); } while((key_entered != 'N') && (key_entered != 'n')); /* Set all 16 buffers to 0 */ for (Ch = 0; Ch < 16; Ch++) outport(base + (Ch*2), 0x000); clrscr(); puts("DA12-16 Sample1 complete."); } /* end main */