« Close

Datasheets and User Guides

App Notes

Software & Driver

 

4.3.5 - Digital I/O

There are eight IOTypes used to write or read digital I/O information:

LJ_ioGET_DIGITAL_BIT         //Also sets direction to input.
LJ_ioGET_DIGITAL_BIT_DIR
LJ_ioGET_DIGITAL_BIT_STATE
LJ_ioGET_DIGITAL_PORT        //Also sets directions to input.  x1 is number of bits.
LJ_ioGET_DIGITAL_PORT_DIR    //x1 is number of bits.
LJ_ioGET_DIGITAL_PORT_STATE  //x1 is number of bits.

LJ_ioPUT_DIGITAL_BIT         //Also sets direction to output.
LJ_ioPUT_DIGITAL_PORT        //Also sets directions to output.  x1 is number of bits.

DIR is short for direction. 0=input and 1=output.

The general bit and port IOTypes automatically control direction, but the _DIR and _STATE ones do not. These can be used to read the current condition of digital I/O without changing the current condition. Note that the _STATE reads are actually doing a read using the input circuitry, not reading the state value last written. When you use LJ_ioGET_DIGITAL_BIT_STATE or LJ_ioGET_DIGITAL_PORT_STATE on a line set to output, it leaves it set as output, but it is doing an actual state read based on the voltage(s) on the pin(s). So if you set a line to output-high, but then something external is driving it low, it might read low.

When a request is done with one of the port IOTypes, the Channel parameter is used to specify the starting bit number, and the x1 parameter is used to specify the number of applicable bits. The bit numbers corresponding to different I/O are:

0-7    FIO0-FIO7
8-15   EIO0-EIO7
16-19  CIO0-CIO3
20-22  MIO0-MIO2

Note that the GetResult function does not have an x1 parameter. That means that if two (or more) port requests are added with the same IOType and Channel, but different x1, the result retrieved by GetResult would be undefined. The GetFirstResult/GetNextResult commands do have the x1 parameter, and thus can handle retrieving responses from multiple port requests with the same IOType and Channel.

Following is example pseudocode for various digital I/O operations:

//Request a read from FIO2.
AddRequest (lngHandle, LJ_ioGET_DIGITAL_BIT, 2, 0, 0, 0);

//Request a read from EIO0-CIO1 (10-bits starting
//from digital channel #8).
AddRequest (lngHandle, LJ_ioGET_DIGITAL_PORT, 8, 0, 10, 0);

//Set FIO3 to output-high.
AddRequest (lngHandle, LJ_ioPUT_DIGITAL_BIT, 3, 1, 0, 0);

//Set CIO2-MIO2 (5-bits starting from digital channel #18)
//to b10100 (=d20).  That is CIO2=0, CIO3=0, MIO0=1,
//MIO1=0, and MIO2=1.
AddRequest (lngHandle, LJ_ioPUT_DIGITAL_PORT, 18, 20, 5, 0);

//Execute the requests.
GoOne (lngHandle);

//Get the FIO2 read.
GetResult (lngHandle, LJ_ioGET_DIGITAL_BIT, 2, &dblValue);

//Get the EIO0-CIO1 read.
GetResult (lngHandle, LJ_ioGET_DIGITAL_PORT, 8, &dblValue);

3 comments

Can I get some more samples on setting ports to input and output then querying the state of the inputs and setting the outputs using the request -> Go -> GetResult method?

What is the output of dblValue look like when it returns from the examples above?

Thanks!

The 4 iotypes used in the pseudocode are probably all you need.  Try using these and then if you get stuck or confused I recommend posting a code snippet showing your problem on our forum.  We have Windows examples available for various programming languages.

The first GetResult gets the result of a bit read from FIO2 and thus will return 0.0 or 1.0.

The second GetResult gets the result of the port read of FIO4-EIO5.  This is 10 bits, so the value you read will be something from 0.0 (all low) to 1023.0 (all high).

In your example:

//Set FIO3 to output-high.
AddRequest (lngHandle, LJ_ioPUT_DIGITAL_BIT, 3, 1, 0, 0);

//Set CIO2-MIO2 (5-bits starting from digital channel #18)
//to b10100 (=d20).  That is CIO2=0, CIO3=0, MIO0=1,
//MIO1=0, and MIO2=1.
AddRequest (lngHandle, LJ_ioPUT_DIGITAL_PORT, 18, 20, 5, 0);

It appears that the x1 parameter (the fifth parameter) is zero when writing to one bit, but five when writing to five bits? Please clearly explain the x1 parameter. Thank you!