/**************************************************************************** * 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. The program also uses the* * calibration values stored in the eeprom in calculating the output * ****************************************************************************/ #include #include #include #include "commdrv.h" unsigned int a = 0,b = 0; 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 */ void ReadCal(unsigned int Address,unsigned int channel) { char MSG[255]="\0"; char temp[255]="\0"; int i = 0; sprintf(temp, "CAL%X?", channel); // read the point configuration writePod(Address, temp); readPod(Address,MSG); sscanf(&MSG[21], "%04X", &a); //values for +/-5V at end of array sscanf(&MSG[26], "%04X", &b); //a=zero offset, b=span offset delay(100); } double Cal(int data) { double value = 0.0; a>>=4; //need to shift values down one nybble for this math b>>=4; value = (((4095.0 - b- a) / 4095.0) * data + a); return value; //returns calibrated counts }//end cal /**************************************************************************** * 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 voltsentered,voltsexpected; unsigned temp,dacnum; char MyStr[255]; char MyStr2[255]; int counts=0; /* prompt for the DAC number and desire voltage. */ do{ gotoxy (1,3); clreol(); printf("Enter the DAC number (0 through 7 only): "); ch=getche(); dacnum=ch-'0'; /* using ascii code to get dac_number*/ }while ((dacnum < 0) || (dacnum > 7)); do{ gotoxy (1,7); clreol(); puts("Card configured for +/-5V range"); printf("Enter a voltage between -5.000V and 4.997V: "); scanf("%f",&voltsentered); }while ((voltsentered<-5.000) || (voltsentered>4.997)); voltsentered = (voltsentered / 0.002441); /* compute the digital output needed for this value and the actual voltage that will be expected, and display the voltage. */ counts=(int) voltsentered; voltsexpected = (float) counts * 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.",voltsexpected); sprintf(MyStr2, "AC%x=0000,00,00,00,0000",dacnum);//setup for +/-5V writePod(Address, MyStr2); readPod(Address, MyStr2); delay(100); ReadCal(Address,dacnum); //get calibration coefficients from card counts+=2047; //can only write positive counts to card counts = Cal(counts); sprintf(MyStr, "A%x=%03x0", dacnum,counts); writePod(Address, MyStr); readPod(Address, MyStr); } /* end write_DAC */ /**************************************************************************** * FUNCTION: main() - local routine * ****************************************************************************/ void main() { int key; unsigned int Address; clrscr(); printf( " C SAMPLE #1: SAMPLE1.C \n\n" " This sample will prompt the user for a voltage between -5V and 4.997V, \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 of the COM card should be set to the address \n" " that will be entered during program execution. \n" " -- All remaining jumper settings are irrelevant \n\n"); //check on other configuration information Address=AskForBaseAddress(0x300); initCommCard(Address, 9600); clrscr(); do { write_DAC(Address); printf("\n\n\nWould you like to output another value (Y or N)? "); key = getch(); clrscr(); }while((key != 'N') && (key != 'n')); clrscr(); } /* end main */