Jump to content
  • 0

Analog Discovery 3 - Issues Reading data from Digital Input C#


HasNoCreek

Question

Hello all,

I am attempting to read back the Digital IO pins on the AD3, for tracking the ticks of a rotary encoder, but even with no pins coming in high, im getting non 0 values for my pins(when reading from SDK only, Waveforms app does not show any pins high)

 

I assume that I am doing something wrong on the read and have attached my test below.

Dwf.DigitalInReset(_deviceManager.GetDevice()!.Id.Value);

int leadingPinIndex = 0;
int trailingPinIndex = 1;
var id = _deviceManager.GetDevice()!.Id;
var hzSys = Dwf.DigitalInInternalClockInfo(id.Value);

// sample rate to 1MHz
var divider = (uint)(hzSys / 1e6);
Dwf.DigitalInDividerSet(id.Value, divider);


// 16bit WORD format
Dwf.DigitalInSampleFormatSet(id.Value, 16);


Dwf.DigitalInAcquisitionModeSet(id.Value, ACQMODE.Single);

// get the maximum buffer size
var cSamples = Dwf.DigitalInBufferSizeInfo(id.Value);
// default buffer size is set to maximum
Dwf.DigitalInBufferSizeSet(id.Value, cSamples);


// set trigger position to the middle 
Dwf.DigitalInTriggerPositionSet(id.Value, (uint)(cSamples / 2));


// trigger on any pin transition
Dwf.DigitalInTriggerSourceSet(id.Value, DigitalTriggerSource.DetectorDigitalIn.MapToWaveFormsSdkType());

//_digitalInService.DigitalInTriggerAutoTimeoutSet(id, 10.0);
//var triggerBits = SetBits(0, 1);
uint triggerBits = 0xFFFF;
Dwf.DigitalInTriggerSet(id.Value, triggerBits, triggerBits, triggerBits, triggerBits);
Dwf.DigitalInTriggerAutoTimeoutSet(id.Value, 5);


// start
Dwf.DigitalInConfigure(id.Value, false, true);

bool previousLeading = false;
bool previousTrailing = false;
int count = 0;
while (count < 5)
{
    STATE sts;
    do
    {
        sts = Dwf.DigitalInStatus(id.Value, false);
    } while (sts != STATE.Done);

    var rgwSamples = Dwf.DigitalInStatusData(id.Value, cSamples);

    for (int i = 0; i < rgwSamples.Length; i += 2)
    {
        // Ensure there's a pair of bytes to process
        if (i + 1 < rgwSamples.Length)
        {
            var byte1 = rgwSamples[i]; // Even byte - pins [0-7]
            var byte2 = rgwSamples[i + 1]; // Odd byte - pins [8-15]

            var byte1BoolArray = ByteToBoolArray(byte1);
            var byte2BoolArray = ByteToBoolArray(byte2);

            bool leadingPin = leadingPinIndex < 8 ? byte1BoolArray[leadingPinIndex] : byte2BoolArray[leadingPinIndex - 8];
            bool trailingPin = trailingPinIndex < 8 ? byte1BoolArray[trailingPinIndex] : byte2BoolArray[trailingPinIndex - 8];

            if (leadingPin != previousLeading || trailingPin != previousTrailing)
            {
                //This will yield return in actual application
                //yield return (leadingPin, trailingPin);
                previousLeading = leadingPin;
                previousTrailing = trailingPin;
            }
        }
    }
    count++;
}

If I provide a True to the DigitalInStatus call, rgwSamples is [3,0,3,0,3,0,3,....]

If I provide a False to the DigitalInStatus call, rgwSamples is [80,1,145,144,229,1,0,0....] and then the series repeats.

My expectation would be that if I have no high digital pins, my entire rgwSamples would be 0s.

Since I have a 16 bit format and 16 pins, its my assumption that the even elements in rgwSamples would be for pins 0-7 and the odd elements would be for pins 1-15. Is that a misunderstanding?

 

Thanks in advice for any advice/help!

 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Hi @HasNoCreek

See the examples and manual:

image.png

FDwfDigitalInTriggerSet(hdwf, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF) is not a valid trigger, DIN15:0 to be high and low at the same time and also to be either edge, rising or falling.

FDwfDigitalInStatus with false (0) instructs to not fetch capture data from the device.

image.png

Link to comment
Share on other sites

  • 0
Posted (edited)

Hey there @attila,

Appreciate the response. With the change you suggested, I am still receiving the [3,0,3,0....] repeating data after the trigger timeout.

 

image.thumb.png.43ac4e11999b511f4f0737795af09c21.png

 

I also ran a test with the triggering removed entirely, and received the same result. Any idea what I am missing there?

 

I have attached a simplified version of my test without triggering or data processing:

Dwf.DigitalInReset(_deviceManager.GetDevice()!.Id.Value);

var id = _deviceManager.GetDevice()!.Id;


var hzSys = Dwf.DigitalInInternalClockInfo(id.Value);

// sample rate to 1MHz
var divider = (uint)(hzSys / 100e6);
Dwf.DigitalInDividerSet(id.Value, divider);

// 16bit WORD format
Dwf.DigitalInSampleFormatSet(id.Value, 16);

Dwf.DigitalInAcquisitionModeSet(id.Value, ACQMODE.Single);

// get the maximum buffer size
var cSamples = 1000;
// default buffer size is set to maximum
Dwf.DigitalInBufferSizeSet(id.Value, cSamples);

// start
Dwf.DigitalInConfigure(id.Value, false, true);

int count = 0;
while (count < 5)
{
    STATE sts;
    do
    {
        sts = Dwf.DigitalInStatus(id.Value, true);
    } while (sts != STATE.Done);

    var rgwSamples = Dwf.DigitalInStatusData(id.Value, cSamples);
    count++;
    //Do something with data
}
    
            

Thanks man

Edited by HasNoCreek
Link to comment
Share on other sites

  • 0

Ahhh, I have found the culprit. My pins 0 and 1 are high, but I can only see that with the master enable on and the positive supply cranked up to 4V, which is happening behind the scenes in my application. Enabling that in waveform shows me correctly that those pins are high!

 

Thanks for the help @attila!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...