/**************************************************************************** * 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 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: 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(unsigned int Address) { char ch; float volt_value_entered,volt_value_expected; unsigned counts_entered,temp,dac_number; unsigned char lo_byte,hi_byte; /* prompt for the DAC number and desire voltage. */ do { gotoxy (1,3); clreol(); printf("Enter the DAC number (0 through 5 only): "); ch=getche(); dac_number=ch-'0'; /* using ascii code to get dac_number*/ } while ((dac_number < 0) || (dac_number > 5)); do { gotoxy (1,7); clreol(); printf("Enter a voltage between 0.000v to 9.997v: "); scanf("%f",&volt_value_entered); } while ((volt_value_entered<0.000) || (volt_value_entered>9.997)); volt_value_entered /= 0.002441; /* 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.002441; printf("\n\n\nDue to the 12-bit resolution of the DAC, you should expect\n"); printf("to see a voltage of %4.3f.",volt_value_expected); /* prepare the control word and write to the DAC */ temp = counts_entered; counts_entered /= 256; /* extract hi byte */ hi_byte = (unsigned char) counts_entered; /* convert to character */ temp %= 256; /* extract low byte */ lo_byte = (unsigned char) temp; /* convert to character */ outportb(Address+(2 * dac_number),lo_byte); /* write low byte */ outportb(Address+(2*dac_number+1),hi_byte); /* write high byte */ } /* end write_DAC */ /**************************************************************************** * FUNCTION: main() - local routine * ****************************************************************************/ void main() { int key_entered; unsigned int Address; 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" " Board Configuration: \n\n" " -- The Base Address should be set to the address which will be \n" " entered during program execution. \n" " -- Reference voltage for the DACs should be -10V. (required) \n" " -- All remaining jumper settings are irrelevant \n\n"); Address=AskForBaseAddress(0x350); puts("\n\n\n\nPress any key to continue"); getch(); do { clrscr(); write_DAC(Address); printf("\n\n\nWould you like to output another value (Y or N)? "); key_entered = getch(); } while((key_entered != 'N') && (key_entered != 'n')); clrscr(); } /* end main */