/**************************************************************************** * 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 #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 desired voltage. */ printf("Enter the DAC number (0 or 1 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 PCI-A12-16(A) 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) + 0xC, 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 *TypeString) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; printf("\nPlease enter the %sBase Address for your card (in hex)\n", TypeString); printf("or press ENTER for 0x%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(0xE000, ""); clrscr(); } /* end of intro */ /**************************************************************************** * FUNCTION: main() - local routine * ****************************************************************************/ void main() { int key_entered, Ch; intro(); 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')); clrscr(); puts("PCI-A12-16(A) Sample1 complete."); } /* end main */