May 17, 2024, 11:50:37 PM

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.


Pin number versus Bit Register

Started by Caretaker.CCI, October 12, 2008, 10:17:26 AM

Previous topic - Next topic

Caretaker.CCI

OK... there was a (short) discussion here recently about the difference between

SYMBOL  Sio = 7

Versus

SYMBOL  Sio = PIN7

"the pin number, not the pin I/O bit register"

It was at the end of a thread (as I recall) and I thought I had copied it to study your response for later but now I can't find it. Can you (or anyone) direct me to that thread?

Greg

JonnyMac

Some commands, like HIGH and LOW, expect a numeric value the indicates the pin to affect -- 0 to 7.  So, to "blip" and output we might do something like this:

  HIGH 0
  PAUSE 25
  LOW 0


This is of course completely legal and works.  That said, in my opinion it is not user friendly.  At the very least we should name pins; so we might do something like this:

SYMBOL  AudioStart      = 0

Now the code above looks like this:

  HIGH AudioStart
  PAUSE 25
  LOW AudioStart


This is better, but from a professional engineering stand-point it is still incomplete.  Why?  Because circuits can be active-high or active-low.  I tend to favor active-high programming with our customers so you'll find these definitions in most of my programs.

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0


Now, before we use the IsOn and IsOff constants we have to change the way we're accessing the pin.  At the top we just used the pin number (0 to 7) with HIGH and LOW.  If we look under the hood what we find is that HIGH and LOW actually perform two actions.  For example, HIGH 0 actually translates to:

  PIN0 = 1
  DIR0 = 1


So you see, HIGH 0 writes a "1" to the pin's output register and then makes the pin an output by writing a "1" to the pin's direction register.  We can make our programs a little more efficient by making the pin an output at the beginning of the program and then writing to its register as required (1 for on, 0 for off).

Before we do that, though, we need to change the pin definition from a constant value (0) to a register that we can write to (PIN0):

SYMBOL  AudioStart      = PIN0

Now our code segment becomes a little more friendly:

  AudioStart = IsOn
  PAUSE 25
  AudioStart = IsOff


I like this style of code as it's self-documenting -- there is little need for additional comments when you write programs like this.

This style does not lend itself to all PBASIC commands.  If you're controlling servos, for example, then you need to define the pin as a constant value (0 to 7), not a register value (PIN0 to PIN7).  Why?  Well, let's say that you want to do a PULSOUT on P5 and you accidentally code it like this:

  PULSOUT PIN5, 150

This code works, but doesn't do what you want.  You see, when a PINx register name is used like this (or is on the right side of an = sign) the interpreter assumes that you want to read the pin register then substitute the value read for the pin number for PULSOUT.  That means that the line above will do a PULSOUT on P0 if P5 reads low (most likely because the ULN acts as a pull-down), or P1 if P5 reads high.

Yep, this stuff can get a little tricky and I'm working on finding good ways to explain it -- will be a big part of my Prop-1 book when I can figure it out.  And, FWIW, I was part of the team that updated the BS2's language from 2.0 to 2.5; we took steps with the PIN definition so that code would [nearly] always be interpreted correctly.
Jon McPhalen
EFX-TEK Hollywood Office