November 21, 2024, 06:53:09 AM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Understanding the Use of PINx (and Avoiding Traps)

Started by JonnyMac, October 01, 2011, 11:23:24 AM

Previous topic - Next topic

JonnyMac

October 01, 2011, 11:23:24 AM Last Edit: October 01, 2011, 11:28:42 AM by JonnyMac
In the top of many programs you will find symbols defined like this:

SYMBOL  Sio             = 7                     ' SETUP = UP; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN


Most beginners wonder, "Why does Trigger use 'PIN' and Sio doesn't?"  It has to do with syntax.  In the above definitions, Sio is called a constant as it has a fixed value of 7.  We define pins this way when the are in one position and do not change, and for programming constructs that require a specific value (e.g., pin # for SEROUT and SERIN).

Trigger is actually defined as a bit variable; a variable is a value that can change.  With the above definition we will read the input level (0 or 1) of the trigger pin (P6) for use in a program.  For example, this code is used to 'debounce' a trigger input; debouncing is used to prevent false triggers.

Main:
  timer = 0                                     ' reset debounce timer

Check_Trigger:
  PAUSE 5                                       ' scan delay
  IF Trigger = TrOff THEN Main                  ' check trigger input
    timer = timer + 5                           ' update timer
  IF timer < 100 THEN Check_Trigger             ' check timer


In this use the Prop-1 will look at the value of (input) pin P6 and compare it with a constant value called TrOff.  If those two values match the program jumps to the label, Main.

As with all variables, PINx variables can be read (as we just explored) or written to.  We can also assign PINx variables for outputs, like this:

SYMBOL  Valve           = PIN0

With this definition -- and making the IO pin an output using the DIRS register -- we can write code like this:

  Valve = IsOff

A useful trick with PINx outputs is that one part of our program can set the state of an output pin (like we just did) and another part of the program can read it back.  For example, give the above definitions, we can check to see if the valve is currently turned on or off:

  IF Valve = IsOn THEN Reset

PINx TRAP

And here's an explanation to a trap many Prop-1 programmers -- especially those coming from the Prop-2 -- fall into.  In the above line the program reads the state of the valve pin and then uses it in the comparison (again, it is perfectly legal to read the state of output pins).  When reading a PINx variable we we only ever get 0 or 1. 

A common definition error is this:

SYMBOL  Sio             = PIN7                  ' dangerous definition!!!

Which looks fine; it's actually a valid statement but it doesn't work as intended in a line like this:

SEROUT Sio, OT2400, ("!RC4", %00, "X")

Here's what happens inside the controller when that SEROUT is run, given the definition just above: Sio is defined as a pin variable so the program reads the state of that pin and then uses that value for the transmit pin in the SEROUT statement.  In EFX-TEK projects we use the P7 SETUP jumper in the UP position so this will cause the P7 input value to be "1." The SEROUT, then, is transmitted on P1 instead of P7.

Any person that has programmed a Prop-1 has stumbled into this pothole; now that you understand it, the pothole will be easier to spot and avoid.




Jon McPhalen
EFX-TEK Hollywood Office