ReadPin() return value

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

ReadPin() return value

Post by spamiam »

I am FINALLY, after a LONG hiatus getting back to a project. It is some software to interface to an IEEE-488 (GPIB) bus. As usual, It is MUCH MUCH easier to do development in ZBasic rather than C/Arduino.

In the documentation of PinRead() it says
a non-zero return value indicates a logic high
Is this return value always a "1" or is it some other non-zero value from time to time? Under what circumstances might a high value be represented by something other than a "1"?

-Tony
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: ReadPin() return value

Post by dkinzer »

spamiam wrote:Under what circumstances might a high value be represented by something other than a "1"?
The return value is produced by ANDing a mask value having but one bit asserted with the value obtained by reading the PIN register containing the specified pin. Consequently, the non-zero return value will equal the mask value.

This may be more clear by examining the code produced for this simple program:

Code: Select all

Dim b as Byte

Sub Main()
    b = PinRead(C.4)
End Sub
Generated code:

Code: Select all

static uint8_t mt2_zv_b;

void
zf_Main(void)
{
    mt2_zv_b = (PINC & 0x10);
}
If you need the value to be either zero or 1, use this:

Code: Select all

b = IIF&#40;PinRead&#40;C.4&#41;<>0,1,0&#41;
- Don Kinzer
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Post by spamiam »

Thanks, Don.

It is not a problem. It is easy to use "<> 0" instead of "= 1". I do not know the assembly language options, but it may be faster to compare to zero than to a non-zero number where the non-zero argument number may need to be loaded into a register for comparison.

I will adjust my code accordingly.

In some cases the "<>0 comparison will work well. But other times, I will compare against a variable which will be High or Low, and the IIF() statement is does the job nicely.
Post Reply