Jump to content
  • 0

Digital in is recognizing three high samples less than expected


aaronBe

Question

Hello Community

I am using the Analog discovery 2 controlled by a C# Software. At a certain point of the software, I need to check the length of a detected pulse. Before this, I prepare the digital in component of the device.

private const uint BUFFER_SIZE = 4000;
private const uint DIGITAL_IN_DIVIDER = 1000;
private const uint CLOCK_FREQUENCY = 100000000;

public void prepareForPulseDetection( int digitalIoIndex ) {
	checkDigitalIoIndex( digitalIoIndex );
	// sampling frequency = 100 MHz / 1000 = 100 kHz (-> 10 us)
	dwf.FDwfDigitalInDividerSet( device, DIGITAL_IN_DIVIDER ); 
	// just sample DIO pins 0...7
	dwf.FDwfDigitalInSampleFormatSet( device, 8 );
	dwf.FDwfDigitalInBufferSizeSet( device, (int)BUFFER_SIZE );
	dwf.FDwfDigitalInSampleModeSet( device, dwf.DwfDigitalInSampleModeSimple );
	// just one acquisition after trigger
	dwf.FDwfDigitalInAcquisitionModeSet( device, dwf.acqmodeSingle );
	// setting triggersource to digital in detector
	dwf.FDwfDigitalInTriggerSourceSet( device, dwf.trigsrcDetectorDigitalIn );
	// take 50 samples before trigger
	dwf.FDwfDigitalInTriggerPositionSet( device, (uint)BUFFER_SIZE - 50 ); 
	// disable timeout
	dwf.FDwfDigitalInTriggerAutoTimeoutSet( device, 0 );
	// set trigger to rising edge on DIO pin 0
	uint triggeredPin = (uint)0x0001 << digitalIoIndex;
	//                                   high    low     rising        falling
	dwf.FDwfDigitalInTriggerSet( device, 0x0000, 0x0000, triggeredPin, 0x0000 );

	// enable digital in without reconfiguring
	dwf.FDwfDigitalInConfigure( device, 0, 1 ); 
}

private void checkDigitalIoIndex( int digitalIoIndex ) {
	if ( ( digitalIoIndex < 0 ) || ( 7 < digitalIoIndex ) )
		throw new ArgumentException( "Digital IO index must be in array [0...7]" );
}

/// <returns> pulse time in us </returns>
public uint getDetectedPulseLength( int digitalIoIndex ) {
  checkDigitalIoIndex( digitalIoIndex );

  var data = new byte[BUFFER_SIZE];
  dwf.FDwfDigitalInStatusData( device, data, (int)BUFFER_SIZE );

  var numberOfHighSamples = getNumberOfHighSamples( data, digitalIoIndex );
  // 1e6 because of us return
  var usDuration = (uint)( DIGITAL_IN_DIVIDER * 1e6 / CLOCK_FREQUENCY );
  usDuration = (uint)( usDuration * numberOfHighSamples );

  // reset device, else it is not possible to
  // recognize if pulse occured
  dwf.FDwfDigitalInReset( device );
  return usDuration;
}

private int getNumberOfHighSamples( byte[] data, int digitalIoIndex ) {
  if ( allZero( data ) ) 
    return 1; // means pulse was shorter than samplerate
  else {
    int startingIndex = getStartingIndexOfPulse( data, digitalIoIndex );
    int endingIndex = getEndingIndexOfPulse( data, digitalIoIndex );
    int difference = ( endingIndex - startingIndex + 1 );
    return difference + 3; // plus 3 because analog discovery return is inaccurate
  }
}

private bool allZero( byte[] data ) {
  for ( int i = 0 ; i < data.Length ; ++i )
    if ( data[i] != 0 )
    	return false;

  return true;
}

private int getStartingIndexOfPulse( byte[] data, int digitalIoIndex ) {
  int index = 0;
  for ( ; index < data.Length ; ++index )
    if ( checkBit( data[index], (byte)digitalIoIndex ) )
       break;

  return index;
}

private int getEndingIndexOfPulse( byte[] data, int digitalIoIndex ) {
  int index = getStartingIndexOfPulse( data, digitalIoIndex ) + 1;
  for ( ; index < data.Length ; ++index )
     if ( !checkBit( data[index], (byte)digitalIoIndex ) )
       break;

  return --index;
}

private bool checkBit( byte value, byte checkedBit ) {
  byte shiftedValue = (byte)( (int)value >> (int)checkedBit );
  return ( ( shiftedValue & 0x01 ) == 0x01 );
}

The pulses are reliably measured. But the number of high samples is always smaller than expected. Most of the time, I expect the measured pulse three microseconds longer than it is.

Is this a known issue? Or is there a mistake in my code?

I am very sure, that the pulses I am sending to the analog discovery are as long as they should be. I checked it with the analog discovery with the waveforms software and with another osciloscope.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

Hi @attila

I made a mistake in my question. I wrote

Quote

The pulses are reliably measured. But the number of high samples is always smaller than expected. Most of the time, I expect the measured pulse three microseconds longer than it is.

But actually I meant

The pulses are reliably measured. But the number of high samples is always smaller than expected. Most of the time, I expect the measured pulse to be three samples longer than it is.

What means the error equals about 30 microseconds.

Link to comment
Share on other sites

Hi @aaronBe

I would use the WF application to test the digital pulse measurement, you can configure trigger on Pulse/Glitch to detect shorter pulse than expected.
You can use the Scope inputs to verify the signal cleanness, voltage levels, the edge/transition speed...
Make sure to have proper grounding between the circuits and devices.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...