/**************************************************************************** * 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. * * * * The following setup of the board is expected: * * -- Reference voltage for the DACs should be -10V. * * * ****************************************************************************/ #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) { float volt_value_entered,volt_value_expected; unsigned counts_entered,temp,dac_number; unsigned char lo_byte,hi_byte; char ch; /* prompt for the DAC number and desire voltage. */ do { gotoxy (1,2); clreol(); printf("Enter the DAC number you wish to output to (0 or 1 only): "); ch=getche(); dac_number=ch-'0'; /* using ascii code to get dac_number */ } while (((int)dac_number < 0) || ((int)dac_number > 1)); 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+0x08+(2 * dac_number),lo_byte); /* write low byte */ outportb(Address+0x08+(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"); Address=AskForBaseAddress(0x340); clrscr(); outportb(Address+0x08,0); /* set DAC to 0 output */ outportb(Address+0x09,0); outportb(Address+0x0a,0); /* set DAC to 0 output */ outportb(Address+0x0b,0); printf( "\n\n\n Board Configuration: \n\n" " -- Base address is %x hex. \n" " -- DACs must be placed in the 10V unipolar mode. \n" " -- The 2's complement jumper must be removed. \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 */