/**************************************************************************** * 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 #include unsigned AskForBaseAddress(unsigned int OldOne) { char msg[3]; unsigned int newone; unsigned int temp = 0; int index = 0; int letter; puts("Please enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); while(temp==0) { newone=0; index=0; while((letter = getchar())!='\n') //Read char by char and store in Msg msg[index++] = letter; sscanf(msg, "%X", &newone); if(newone==0){ //Check to see if address was inputed temp=OldOne; //Assign default if not } else if((newone>=0x100)&&(newone<=0x3f0)){ //Check to see if inputed addres is valid temp=newone; } else { //If Newone invalid ask for new value. printf("\nEnter the Base Address or press ENTER for %x > ", OldOne); for(index=0; index<=3; index++) msg[index]=0; } //Run while until temp = valid address } return(temp); } /* end of AskForBaseAddress */ void IOPermission(unsigned abase) { if(ioperm(abase,16,1)<0) { fprintf(stderr, "NO IO PERMISSION\n"); } } /**************************************************************************** * 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; int ch,clear; /* prompt for the DAC number and desire voltage. */ while((clear=getchar()) != '\n'); system("clear"); do { printf("Enter the DAC number you wish to output to (0 or 1 only): "); ch=getchar(); dac_number=ch-'0'; /* using ascii code to get dac_number */ } while (((int)dac_number < 0) || ((int)dac_number > 1)); do { printf("\nEnter 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; puts("\n\n\nDue to the 12-bit resolution of the DAC, you should expect"); printf("to see a voltage of %4.3f.\n",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 */ outb(lo_byte,Address+0x08+(2 * dac_number)); /* write low byte */ outb(hi_byte,Address+0x08+(2*dac_number+1)); /* write high byte */ } /* end write_DAC */ /**************************************************************************** * * * FUNCTION: main() - local routine * * * ****************************************************************************/ int main() { unsigned int Address; int key,clear; system("clear"); 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); IOPermission(Address); system("clear"); outb(0,Address+0x08); /* set DAC to 0 output */ outb(0,Address+0x09); outb(0,Address+0x0a); /* set DAC to 0 output */ outb(0,Address+0x0b); 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); do { write_DAC(Address); while((clear=getchar()) != '\n'); puts("\nWould you like to output another value (Y or N)"); key=toupper(getchar()); } while(key != 'N'); return 0; } /* end main */