• 0

Analog Discovery 2 - DigitalIn usage with SDK


Hello Digilent world

First, before I write my questions, I would like to provide some information:

- Used device: Analog Discovery 2 (revision 3)

- Programming language is C++

- IDE: Visual Studio Community 2013, version 12

- Operating System: Windows 10

I am trying to control the device with the SDK but I got issues with the Digital-In-Instrument. I attached a .cpp file with my code. My aim is to configure digital pin 0 to take 1000 samples as soon as a rising edge on the same pin occurs. To generate a rising edge on this pin, I connect digital pin 0 and digital pin 1 and configure pin 1 to pulse on enabling.

I checked if the pulse on pin 0 really occurs: This works fine.

Furthermore, I try to read out the logged samples after the triggering and print them on the console. 

Now the description of my problems:

- With the following code I expect the digitalIn Instrument to be armed just one time (setting of acqmodeSingle in row 27). But the print on row 76 prints "DwfStateArmed". I expect it to stay on "DwfStateDone". Do I missunderstand something here?

- I totally don't understand the format of the data that I get with


In the digilent examples I saw it like this:

FDwfDigitalInStatusData(deviceId, Pointer to array with size of "nrOfSamples", nrOfSamples*sizeof(unsigned short))

Why is there a multiplication with the size of an unsigned short?

- Could you please explain me, in which format the logged values are stored in the data-array? Is it a bitfield with the value of each pin or something different?


Thank you very much in advance for the given answers. If somethings not clear, don't hesitate to ask.

Kind regards




Edited by aaronBe

Share this post

Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Hi @aaronBe,

I'm not the resident expert on using WaveForms SDK (that would be @attila to whom I'll defer if they have any corrections to my response) but I will try to answer a couple of your questions.

For the FDwfDigitalInStatusData function, it is multiplying the number of samples by the size of an unsigned short to determine the number of bytes that are to be copied so an exact amount of acquisition data is transferred to the receiving buffer. This is detailed a bit more in the WaveForms SDK Reference Manual on page 61, which can be found (on a Windows computer) C:\Program Files (x86)\Digilent\WaveFormsSDK.

For the format of that data, I believe it will be set in sample sizes of either 8, 16, or 32 bits, with the least significant bit first via the FDwfDigitalInSampleFormatSet function. I'm not certain what the default setting is though, nor do I know if it is a bitfield with the value of each pin or something else, but this function has some more detail on page 63 of the same reference manual.

Thank you,

Share this post

Link to post
Share on other sites
  • 0

Hi @aaronBe

With FDwfDigitalInSampleFormatSet you can specify the sample size: 8 (unsigned char), or 16 (unsigned short), or 32 (unsigned int)

FDwfDigitalInSampleFormatSet(device, 16);
unsigned short* data = new unsigned short[numberOfSamples];
FDwfDigitalInStatusData(device, data, numberOfSamples*sizeof(unsigned short)); // 16 bits, 2 bytes / sample


FDwfDigitalInSampleFormatSet(device, 32);
unsigned int* data = new unsigned int[numberOfSamples];
FDwfDigitalInStatusData(device, data, numberOfSamples*sizeof(unsigned int)); // 32 bits, 4 bytes / sample


The DIO lines are bit fields: DIO0 bit0, DIO1 bit1... of each sample
DIO0 = (data[idx]>>0)&1
DIO1 = (data[idx]>>1)&1


The device automatically re-arms after the data is fetched, this in order to not have to spend precious milliseconds with configure command. 
You don't need the line 75, since you already have fetched the data in line 40.
In line 72 you are using FDwfDigitalInStatusSamplesValid for samplesLeft variable, but you would only need this for recording.

unsigned short* data = new unsigned short[numberOfSamples];
FDwfDigitalOutConfigure(device, 1); 
for(int acquisition = 0, acquisition < 100; acquisition++){
        FDwfDigitalInStatus(device, 1, &digitalInState); // data will be fetched from the device when acquisition is done and the digital-in re-armed
    } while (digitalInState != DwfStateDone);
    FDwfDigitalInStatusData(device, data, numberOfSamples*sizeof(unsigned short)); // get the data
    // process or save the data

I have corrected the above issues in the code, but have not tested if it is actually works: example.cpp

Share this post

Link to post
Share on other sites
  • 0

Hi @attila

I corrected my code as you suggested and it works fine, thank you once again! But I have got another question about the data format:

If I set the data format to 8, no samples would be taken from digital pins 8 to 15? What is stored in the bits on position 16 to 31 with dataformat 32 since I only have 16 DIO lines?

Share this post

Link to post
Share on other sites
  • 0

Hi @aaronBe

The Analog Discovery internally always samples 16bit digital data. The format can be used to specify sample format you need for your application. For 8 it will return samples only with the 8 least significant bits (DIO 0-7) and with 32 having in the least significant position the 16 real bits (DIO 0-15 + 16 dummy bits).
The Digital Discovery makes use of this option (8, 16, 32), to actually store such samples.

Share this post

Link to post
Share on other sites
  • 0

Hi again @attila

Now I moved on to C# and I am facing the same problem again. The function

dwf.FDwfDigitalInStatusData( int, byte[], int );

wants an array of bytes to store the sampled data. How is it possible to set the data format on 16 since the type byte represents an unsigned 8 bit integer?

Edited by aaronBe

Share this post

Link to post
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