/**************************************************************************** * C SAMPLE #1: SAMPLE1.C * * * * This sample will prompt the user for a voltage between 0 and 10 volts, * * then calculate the actual voltage based on the resulution 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; //just setting up cgets 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) { float volt_value_entered=0.0,volt_value_expected=0.0; unsigned counts_entered,temp,dac_number; unsigned char lo_byte,hi_byte; char ch; // prompt for the DAC number and desired voltage. do { gotoxy (1,2); clreol(); printf("Enter the DAC number you wish to output to (0 - 7 only): "); ch=getche(); dac_number=ch-'0'; //using ascii code to get dac_number } while (((int)dac_number < 0) || ((int)dac_number > 7)); do { gotoxy (1,7); printf("If your card is not set for 0-10V DAC outputs, the value will be proportional."); printf("Enter a voltage between 0.000v to 9.997v: "); clreol(); scanf("%f",&volt_value_entered); } while ((volt_value_entered<0.000) || (volt_value_entered>9.997)); // 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/0.002441); 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); // write the count value out to the DAC outport(Address + (2 * dac_number),counts_entered); } // 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"); Address=AskForBaseAddress(0x300); clrscr(); //clear voltage restrict -- this allows the DAC reference to be non-zero outportb(Address+0x10,0x40); printf( "\n\n\n Board Configuration:\n\n" " -- Base address is %x hex.\n" " -- DACs should be placed in the 10V unipolar mode.\n" " -- if the DACs are in any other mode, the \n" " values will be proportionally incorrect\n" "\n\n\n\nPress any key to continue", Address); 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 */