Shop OBEX P1 Docs P2 Docs Learn Events
help with rotary encoder, code — Parallax Forums

help with rotary encoder, code

I have an incremental encoder and I try it using the basic stamp.
The tests went well but I have doubts regarding a line of code.

' =========================================================================
' {$STAMP BS2}
' {$PBASIC 2.5}

'
[ I/O Definitions ]

' A Output Connects To P0
' B Output Connects To P1


'
[ Variables ]

oldBits VAR Nib ' Previous State Of I/O Pins 0 - 3
newBits VAR Nib ' Current State Of I/O Pins 0 - 3
direction VAR Bit ' Direction Encoder Is Turned
counter VAR Word ' Counter


'
[ Initialization ]

OUTS = %0000000000000000 ' Set All Output Pins Low
DIRS = %1111111111111100 ' I/O Pins 0 - 1 Inputs

newBits = INA & %0011

'
[ Program Code ]

Main:
DO
' newBits = INA & %0011 ' Get State Of I/O Pins 0, 1
' IF newBits <> oldBits THEN ' Have Bits Changed?
' direction = newBits.BIT0 ^ oldBits.BIT1
counter = counter - 1 + (2 * direction)
DEBUG HOME, DEC5 counter ' Show New Counter On DEBUG Screen
oldBits = newBits ' Update oldBits
' ENDIF
LOOP
'STOP


What does INA mean?

Why changing it to INB or INC nothing happens and the program is still normal.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2017-06-01 19:48
    INA is a pseudo-variable that returns the state of the lower 4 I/O pins, packed as 4 binary bits, P0 being the least-significant. If the pin is high, its bit will return as a 1; otherwise, 0.

    -Phil
  • INA means Input pins 3..0. INB means 7..4. And so on. The program would still compile, but would be using the wrong inputs.

  • Thank you so much guys,
Sign In or Register to comment.