long eDAC(HANDLE Handle, ue9CalibrationInfo *CalibrationInfo, long Channel, double Voltage, long Binary, long Reserved1, long Reserved2) { uint8 IOType, channel; uint16 bytesVoltage; if( isCalibrationInfoValid(CalibrationInfo) == 0 ) { printf("eDAC error: calibration information is required"); return -1; } if( getDacBinVoltCalibrated(CalibrationInfo, (uint8)Channel, Voltage, &bytesVoltage) < 0 ) return -1; return ehSingleIO(Handle, 5, (uint8)Channel, (uint8)( bytesVoltage & (0x00FF) ), (uint8)(( bytesVoltage /256 ) + 192), 0, &IOType, &channel, NULL, NULL, NULL); }
//Sends 1000 Feedback low-level commands to read digital IO and analog inputs. //On the first send, the following are set: DAC0 to 2.5 volts, DAC1 to 3.5 //volts, and digital IOs to inputs. int allIO(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[34], recBuff[64], settlingTime; uint8 numChannels; //Number of AIN channels, 0-16. uint16 checksumTotal, bytesVoltage, ainMask; int sendChars, recChars, i, j; int initialize; //boolean to init. DAC and digital IO settings int numIterations, valueDIPort; long time, ainResolution; double valueAIN[16]; numIterations = 1000; initialize = 1; time = 0; numChannels = 8; ainResolution = 12; for( i = 0; i < 16; i++ ) valueAIN[i] = 9999.0; settlingTime = 0; ainMask = pow(2.0, numChannels) - 1; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x0E); //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 255; //FIOMask : setting the mask of all FIOs sendBuff[7] = 0; //FIODir : setting all FIO directions to input sendBuff[8] = 0; //FIOState : all FIO directions are input, so // state writes do not apply sendBuff[9] = 255; //EIOMask : setting the mask of all EIOs sendBuff[10] = 0; //EIODir : setting all EIO directions to input sendBuff[11] = 0; //EIOState : all EIO directions are input, so // state writes do not apply sendBuff[12] = 15; //CIOMask : setting the mask of all CIOs sendBuff[13] = 0; //CIODirState : setting all CIO directions to input, // state writes do not apply sendBuff[14] = 7; //MIOMask : setting the mask of all MIOs sendBuff[15] = 0; //MIODirState : setting all MIO directions to input, // state writes do not apply //Getting binary DAC0 value of 2.5 volts if( getDacBinVoltCalibrated(caliInfo, 0, 2.500, &bytesVoltage) < 0 ) return -1; //Setting the voltage of DAC0 to 2.5 sendBuff[16] = (uint8)(bytesVoltage & 255); //low bits of DAC0 sendBuff[17] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC0 //(bit 7 : Enable, // bit 6: Update) //Getting binary DAC1 value of 3.5 volts if( getDacBinVoltCalibrated(caliInfo, 1, 3.500, &bytesVoltage) < 0 ) return -1; //Setting the voltage of DAC1 to 3.5 volts sendBuff[18] = (uint8)(bytesVoltage & 255); //low bits of DAC1 sendBuff[19] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC1 //(bit 7 : Enable, // bit 6: Update) //AINMask - reading the number of AINs specified by numChannels sendBuff[20] = ainMask & 255; //AINMask (low byte) sendBuff[21] = ainMask/256; //AINMask (high byte) sendBuff[22] = 14; //AIN14ChannelNumber : setting to channel 14 sendBuff[23] = 15; //AIN15ChannelNumber : setting to channel 15 sendBuff[24] = ainResolution; //Resolution : Resolution specified by // ainResolution sendBuff[25] = settlingTime; //SettlingTime //Setting all BipGains (Gain = 1, Bipolar = 1) for( i = 26; i < 34; i++ ) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 34); time = getTickCount(); for( i = 0; i < numIterations; i++ ) { //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, 34); if( sendChars < 34 ) { if( sendChars == 0 ) printf("Feedback error (Iteration %d) : write failed\n", i); else printf("Feedback error (Iteration %d) : did not write all of the buffer\n", i); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 64); if( recChars < 64 ) { if( recChars == 0 ) printf("Feedback error (Iteration %d) : read failed\n", i); else printf("Feedback error (Iteration %d) : did not read all of the buffer\n", i); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(MSB)\n", i); return -1; } if( (uint8)(checksumTotal & 255) != recBuff[4] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(LSB)\n", i); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum8\n", i); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback error (Iteration %d) : read buffer has wrong command bytes\n", i); return -1; } for( j = 0; j < numChannels && j < 16; j++ ) getAinVoltCalibrated(caliInfo, 0x00, (uint8)ainResolution, recBuff[12 + j*2] + recBuff[13 + j*2]*256, &valueAIN[j]); valueDIPort = recBuff[7] + recBuff[9]*256 + (recBuff[10] & 15)*65536 + (recBuff[11] & 7)*1048576; if( initialize == 1 ) { //Unsetting digital IO bit masks since we only want to read states now sendBuff[6] = 0; //FIOMask sendBuff[9] = 0; //EIOMask sendBuff[12] = 0; //CIOMask sendBuff[14] = 0; //MIOMask //Turning off Update bit of DACs sendBuff[17] = sendBuff[17] - 64; //high bits of DAC0 sendBuff[19] = sendBuff[19] - 64; //high bits of DAC1 extendedChecksum(sendBuff, 34); initialize = 0; } } time = getTickCount() - time; printf("Milleseconds per iteration = %.3f\n", (double)time / (double)numIterations); printf("\nDigital Input (FIO0-7, EIO0-7, CIO0-3, MIO0-2) = %d\n",valueDIPort); printf("\nAIN readings from last iteration:\n"); for( j = 0; j < numChannels; j++ ) printf("%.3f\n", valueAIN[j]); return 0; }
long getDacBinVoltUncalibrated(int dacNumber, double analogVolt, uint8 *bytesVolt) { U3_CALIBRATION_INFO_DEFAULT.hardwareVersion = 1.20; U3_CALIBRATION_INFO_DEFAULT.highVoltage = 0; return getDacBinVoltCalibrated(&U3_CALIBRATION_INFO_DEFAULT, dacNumber, analogVolt, bytesVolt); }
long getDacBinVoltUncalibrated(int dacNumber, double analogVolt, uint16 *bytesVolt) { return getDacBinVoltCalibrated(&UE9_CALIBRATION_INFO_DEFAULT, dacNumber, analogVolt, bytesVolt); }
//Sends a Feedback low-level command to set DAC0, DAC1, read FIO0-FIO3 and //AI0-AI3. int feedback_example(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[34], recBuff[64], ainResolution, gainBip; uint16 checksumTotal, bytesVoltage; uint32 tempDir, tempState; int sendChars, recChars, i; double voltage; ainResolution = 12; //ainResolution = 18; //high-res mode for UE9 Pro only gainBip = 0; //(Gain = 1, Bipolar = 0) sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x0E); //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number //All these bytes are set to zero since we are not changing the FIO, EIO, CIO //and MIO directions and states for( i = 6; i <= 15; i++ ) sendBuff[i] = (uint8)(0x00); if( getDacBinVoltCalibrated(caliInfo, 0, 2.500, &bytesVoltage) < 0 ) return -1; //setting the voltage of DAC0 sendBuff[16] = (uint8)( bytesVoltage & (0x00FF) ); //low bits of DAC0 sendBuff[17] = (uint8)( bytesVoltage / 256 ) + 192; //high bits of DAC0 //(bit 7 : Enable, // bit 6: Update) if( getDacBinVoltCalibrated(caliInfo, 1, 3.500, &bytesVoltage) < 0 ) return -1; //setting the voltage of DAC1 sendBuff[18] = (uint8)( bytesVoltage & (0x00FF) ); //low bits of DAC1 sendBuff[19] = (uint8)( bytesVoltage / 256 ) + 192; //high bits of DAC1 //(bit 7 : Enable, // bit 6: Update) sendBuff[20] = (uint8)(0x0f); //AINMask - reading AIN0-AIN3, not AIN4-AIN7 sendBuff[21] = (uint8)(0x00); //AINMask - not reading AIN8-AIN15 sendBuff[22] = (uint8)(0x00); //AIN14ChannelNumber - not using sendBuff[23] = (uint8)(0x00); //AIN15ChannelNumber - not using sendBuff[24] = ainResolution; //Resolution = 12 //Setting BipGains for( i = 25; i < 34; i++ ) sendBuff[i] = gainBip; extendedChecksum(sendBuff, 34); //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, 34); if( sendChars < 34 ) { if( sendChars == 0 ) printf("Error : write failed\n"); else printf("Error : did not write all of the buffer\n"); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 64); if( recChars < 64 ) { if( recChars == 0 ) printf("Error : read failed\n"); else printf("Error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : read buffer has bad checksum16(LSB)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00) ) { printf("Error : read buffer has wrong command bytes \n"); return -1; } printf("Set DAC0 to 2.500 volts and DAC1 to 3.500 volts.\n\n"); printf("Flexible digital I/O directions and states (FIO0 - FIO3):\n"); for( i = 0; i < 4; i++ ) { tempDir = ( (uint32)(recBuff[6] / pow(2, i)) & (0x01) ); tempState = ( (uint32)(recBuff[7] / pow(2, i)) & (0x01) ); printf(" FI%d: %u and %u\n", i, tempDir, tempState); } printf("\nAnalog Inputs (AI0 - AI3):\n"); for( i = 0; i < 4; i++ ) { bytesVoltage = recBuff[12 + 2*i] + recBuff[13 + 2*i]*256; //getting analog voltage if( getAinVoltCalibrated(caliInfo, gainBip, ainResolution, bytesVoltage, &voltage) < 0 ) return -1; printf(" AI%d: %.4f V\n", i, voltage); } printf("\n"); return 0; }