November 21, 2024, 04:34:57 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.


Question on DIRS

Started by aquawilly54, October 22, 2015, 05:08:05 PM

Previous topic - Next topic

aquawilly54

Folks,
  I need some learning here.  With all of my programs working now, I must admit that I cut and paste bits and pieces from past programs because I know they worked, plus major help from Jonny Mac.  That said, I don't usually have a clue as to why they work, so here's a question.  What is DIRS?

I've been looking for "definitions" for a lot of these commands / variables to get a grasp of what they mean and why they go in to a program.  I think it means Directive Symbols:  Yes?  My understanding is that it's a variable for I/O's being that 0's are input and 1's are output.  I am confused because of my program.  It works, but in my head it shouldn't because I have one action and one sound.  Looking at the program it shows the following:

  DIRS = %00111111                              ' P0..P5 are outputs

I'm assuming (I know, that's dangerous) that the eight digits are 0 thru 7.  I don't have any idea as to what the % is for.

I'm using only P0 for the output to a solenoid, and P7 for serial control to the AP-16+.  If 1's are output, why does it work? 
Here's the program for reference.  Remember; it is working. 

Thanks much
Steve 

' =========================================================================
'
'   File......      Witch jumper
'   Purpose...      Jump up with sound (witch's cackle)
'   Author....      Steve
'   E-mail....
'   Started...      10/20/2015
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' PIR activates controls.  Controls activate air solenoid, then plays audio
' SFX01.WAV.  Back to Reset and wait 30 seconds for next trigger.


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Sio             = 7                     ' SETUP = UP; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  Cylinder        = 0                     ' activate air cylinder


' -----[ Constants ]-------------------------------------------------------

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  TrOn            = 1                     ' active-high trigger
SYMBOL  TrOff           = 0

SYMBOL  IsOn            = 1                     ' active-high I/O
SYMBOL  IsOff           = 0

SYMBOL  Baud            = OT2400


' -----[ Variables ]-------------------------------------------------------

SYMBOL  timer           = B2                    ' for debounce loop


' -----[ Initialization ]--------------------------------------------------

Power_Up:
  PAUSE 3000                                    ' let AP-16+ initialize

Reset:
  PINS = %00000000                              ' all off
  DIRS = %00111111                              ' P0..P5 are outputs

  PAUSE 30000                                   ' PIR warm-up/delay


' -----[ Program Code ]----------------------------------------------------

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

  HIGH cylinder                                 ' activate cylinder
  PAUSE 10000                                   ' pause for 10 seconds

  ' play audio file

  SEROUT Sio, Baud, ("!AP16", %00, "PS", 1, 1)

  GOTO Reset


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------

JackMan

DIRS designates if a PIN is an input or an output. Your program works because you only have 1 output listed in I/O Definitions which is 0. For example, if you had a PIN that was intended to be an output but under DIRS it was defined with a "0", it would not function. I'm sure Jon probably has a better explaination but this is basically the scoop.

aquawilly54

Thanks JackMan.  I think I figured it out while at dinner, so let me know if I'm close.

The placement of the 1's and 0's in the DIRS as in this example;        DIRS = %00111111               ' P0..P5       must not be relative. 

The fact that I'm using P0 as an output does not mean the first digit in the DIRS must be a 1...correct?  Therefore, as long as I have six 1's in the DIRS I have six outputs, and this arrangement will work in most programing cases.

Let me know if this is correct thinking please. 

I have another question going in PROP 1 shortly.

Thank you
Steve


JackMan

The position of 1's and 0's is relevant. The digit at far right is PIN 0, far left is PIN7.

JonnyMac

The bits go from left-to-right in this order: P7 P6 P5 P4 P3 P2 P1 P0

...so it would be helpful to comment code like this:

Reset:
  PINS = %00000000                              ' all off
  DIRS = %00111111                              ' P5..P0 are outputs
Jon McPhalen
EFX-TEK Hollywood Office

aquawilly54

Ah!  That explains why this ' P5..P0 are outputs  has always been backwards.  Now I got it. 

What about the % sign in front?  what is it for?

JonnyMac

The BASIC Stamp editor understands three number systems

1) Decimal (same as we use)
2) Binary -- indicated with %
3) Hexadecimal -- indicated by $

That means this number: %1011 has a value of 13, not one thousand eleven.

Learning binary is very important to your success with embedded programming.
Jon McPhalen
EFX-TEK Hollywood Office