Shop OBEX P1 Docs P2 Docs Learn Events
How to use 3 pin rotary encoder with basic stamp? — Parallax Forums

How to use 3 pin rotary encoder with basic stamp?

CuriousOneCuriousOne Posts: 931
edited 2013-06-06 14:02 in BASIC Stamp
Hello.

I have a simple rotary encoder, which has only 3 pins, unlike the 4 pin one, described in NV article.

Is there anywhere explanation how this type of encoder can be used with basic stamp?

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-11-29 09:21
    Incremental Rotary Encoders such as Parallax part #27805 work easily with the BASIC Stamp Modules (and even better with the Propeller chip). I have attached two small programs which demostrate how to read them. Bear in mind the DEBUG statement which allows you to see the values is the main limiting factor on the speed at which the encoder will work.
  • CuriousOneCuriousOne Posts: 931
    edited 2012-11-30 02:53
    Thanks!

    I have a couple of questions if you don't mind:

    Wiring diagram. Currently I've wired my encoder as a two active-low switches, middle contact connected to GND. Is that correct?

    Some commands:

    OW PIN 8 ' 1-Wire buss pin

    I think above may be omitted and not needed, right?

    DIRS = %1111111111111100 ' I/O Pins 0 - 1 Inputs

    Since I want to use pins #4 and #5, this line should look like this:

    DIRS = %1111111111100111

    correct?

    newBits = INA & %0011

    What is purpose of INA command?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-11-30 09:40
    You can wire the COM pin to either Vdd or Vss (GND). When it is wired to ground then the A/B outputs should be pulled high via 10K resistors and also connected to P0 and P1. INA reads P0-P3 at the same time whereas the & %0011 masks off the two bits we don't want. Setting DIRS like that you have to be careful. Your DIRS comand sets P0 and P1 to outputs. If you want to use different pins you should use ones that are aligned with a nibble on the I/O port so that you can read both at once.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-11-30 09:46
    Almost forgot to mention...If you want to use P4 and P5 you can change the INA to INB and everything should work the same. As for DIRS it shouldn't be necessary to set the entire direction register at once unless you need to preset some outputs at the start of the code.
  • CuriousOneCuriousOne Posts: 931
    edited 2013-06-05 09:28
    Can you please help on this?

    newBits = INA & %0011

    What is purpose of INA command?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-06-05 09:31
    As per the BASIC Stamp Manual, INA reads 4 bits in from P0-P3. It's a way of reading multiple pins at the same time. Pins can be read 1, 4, 8 or 16 at a time. We only need 2, but the closest is 4 bits, so the command reads all 4 then masks off the 2 bits we don't want by ANDing the inputs with the mask shown.
  • CuriousOneCuriousOne Posts: 931
    edited 2013-06-05 23:10
    Is there a way to replace that statement with something different for better text readability?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-06-06 07:36
    Yes, but the replacement would not work the same. The issue is that you want to read both I/O pins at the same time and what Chris showed is the only way to do that. If you read first one I/O pin, then the other, the first I/O pin might change in the time between the two reads and you'd get an incorrect result.

    newBits = (IN1 << 1) | IN0

    is not at all equivalent to

    newBits = INA & %0011

    even though it may look like it ought to be
  • SapphireSapphire Posts: 496
    edited 2013-06-06 14:02
    Just like any other variable, INA can have an alias. So if you don't like INA, you could redefine it as something else.
    encoderBits   VAR   INA        ' alias for INA
    
    newBits = encoderBits & 3      ' read encoder bits
    
Sign In or Register to comment.