Shop OBEX P1 Docs P2 Docs Learn Events
Are there any Beginners Programs for using the Rotary Encoder with the BS2? — Parallax Forums

Are there any Beginners Programs for using the Rotary Encoder with the BS2?

Rich_W8VKRich_W8VK Posts: 44
edited 2010-02-07 18:47 in BASIC Stamp
freaked.gif·Hello Folks!
······· I'm beginning to think the rotary encoder tied into the BS2 is a Military Secret!
······· First off, i have a rotary encoder that has three terminals.·It appears that with an ohm meter that the center terminal makes up a connection to the left terminal but not the right. When moving the knob a little bit, the center terminal makes a connection from center terminal to the right terminal, and not to the left terminal.
This seems to be true with·all cases when rotating 360 degrees either direction!
······ Has anyone seen any BS2 programs for the beginner that uses the Debug Display to show the Rotation Direction,
and the changes in the states of connection in some form of BCD Code displayed in Debug?
······ I haven't seen any Educational programs to introduce a newbee to this electronics device! Is this a military secret? If anyone has the answer, please feel free to post me an answer! I'm suprised that Parallax hasn't done this some where in there educational stuff! I haven't been able to find anything that is simple enough to duplicate with just the Encoder and BS2 as examples! Thanks for reading this post!


Rich_W8VK
«1

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-18 12:48
    · Without more·details of your encoder, it sounds like the center pin goes to +5 volts and as the shaft turns, the left and right pins alternate. That is the general arrangement for a quadrature encoder. Its worth finding out a bit more as the design of your encoder could just as easily be meant to have the center pin at·0 volts.

    · Nonetheless, the concept you want to Google is "quadrature encoder".

    · I've attached a very simple encoder program to get you going. Note that in practical terms, the Stamp cannot keep up with more than 10 or so pulses per second from the encoder.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-11-18 16:42
    I wrrote a tutorial with BS2 examples at this URL:
    http://www.emesys.com/BS2fsm.htm#twobit

    I might let you in on a few of the "secrets"!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-19 23:08
    ·Hi Rich

    ·· Here's a circuit which should work with the encoder.

    ·· Edit: As PJ points out later, the inputs should be p4,p5 to work with the posted version of the program.

    ·Cheers

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com


    Post Edited (stamptrol) : 11/22/2009 1:23:02 PM GMT
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-20 03:21
    Okay Tom,
    Let me ask you this flat outright! In your previous posting of the file, (Quadhicount.bs2), Have you tried this code out on a unit that is put together as per the circuit you have just posted? In other words do you have a working unit at this time?

    I personally don't think you do! I think this code is "untested" by you! And to me, the code doesn't come close to working!

    Furthermore, I took the time to write a program for the diode on P0, diode on P8, diode on P9, diode on P10, diode on P11, diode on P12, diode on P13, diode on P14, and diode on P15, to first be all turned on one at a time in order, and then all to be turned off one at a time in order! And that all works! Then I looped the code to continuely cyle all diodes on and off! And this works too!

    I also mounted diodes on each input line to show the present state of change when the encoder has been rotated! That all works into the input lines of P6 and P7. So I think i've proved that my circuitry works!

    And i believe your code doesn't work at all! And that you do not have a unit together that your running this code with!
    Am I correct Tom? A Yes or No Please!
    Thank you!

    Rich_W8VK
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-11-20 04:00
    Rich_W8VK

    Can you·just·Post·your working code and a working circuit·

    The code that you posted did not work·and why you think it did not work and·this is what I did which work for what I want to do

    That was all that was need and NO MORE

    I can tell you something that NOT ALL CODE's AND CIRCUIT's·ARE TESTED· before they are posted· this is what you have to do your self
    this forum is here to HELP you to give you idea.gif·on how to get··your code and circuit· to work

    This says it all··and in you first post·
    Are there any Beginners Programs for using the Rotary Encoder with the BS2?
    ··
    I can tell you something if I was helping you I would stop helping you·because of this

    Please Tone it DOWN·......>>>>>>

    ·Okay Tom,
    Let me ask you this flat outright! In your previous posting of the file, (Quadhicount.bs2), Have you tried this code out on a unit that is put together as per the circuit you have just posted? In other words do you have a working unit at this time?

    I personally don't think you do! I think this code is "untested" by you! And to me, the code doesn't come close to working!

    Furthermore, I took the time to write a program for the diode on P0, diode on P8, diode on P9, diode on P10, diode on P11, diode on P12, diode on P13, diode on P14, and diode on P15, to first be all turned on one at a time in order, and then all to be turned off one at a time in order! And that all works! Then I looped the code to continuely cyle all diodes on and off! And this works too!

    I also mounted diodes on each input line to show the present state of change when the encoder has been rotated! That all works into the input lines of P6 and P7. So I think i've proved that my circuitry works!

    And i believe your code doesn't work at all! And that you do not have a unit together that your running this code with!
    Am I correct Tom? A Yes or No Please!
    Thank you!············································································ <<<<<...............
    for all of us smile.gif· Thank You

    One more thing did you go to Tracy Allen web page there is a lot of info on a lot of thing

    Also do you have a web link to the encoder that you are using

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 11/20/2009 4:43:01 AM GMT
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-20 07:00
    Sam,
    This is how to look at what you've answered me with!
    I am the "Moderator" on this "Topic"

    This is an "Educational Topic for myself and many others!

    I aim to see this get to a working sample to "Provide Education" about an electronics device many of us know nothing about!

    What you have "Blessed" me with so far is:
    1. Establish a Truth Table if possible for your Rotary Encoder
    ( you suggested using LED's to aid in this task! Excellent Sam

    Now I'm needing some ideas on the next point (Step)
    2. Lets get some simple PBasic Code that will get the BS2 to reconize this Truth Table by Displaying some information
    on the screen in the DEBUG Terminal if posible!

    (We need some contributers that have a BS2 Free to work on coding, and a Rotary Encoder that matches my
    Truth Table Below!)

    I have the Following Truth Table From my Rotary Encoder! I hope you can have the same!
    (And this is consistantly cycling over and over again!)

    Ch A Ch B
    0 0
    0 1
    1 1
    1 0

    Any ideas are sure welcome! Thanks to all, especially Sam, for getting us this far, so far!

    I am also going to try and make some more sense of the material provided by Tracy Allen!

    Thank you all for following this!

    Rich_W8VK
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-20 07:37
    Hello you all again!
    I not only would like to see the input from the Rotary Encoder come up on the Debug Screen, It would be nice if this data would move a Counter Count! (That would be the BS2 reconizing the Device! That would be progress)

    Or does some one have another idea?

    I am using P6 for Ch A, P7 for Ch B, and am thinking it might take a Button on P0 to Change Counts Up/Down!

    Oh! that was Tom Sisk that presented the great idea and help in making a Truth Table with the use of LED's on the inputs!
    Thanks so Much Tom! I appoligize if you were affended on the code and working sample but i think you can now see the
    direction and hot persuit this Topic is headed! And appoligizes to you to Sam!

    Feel free to post your thoughts in here Folks! Thanks!

    Rich_W8VK
  • stephenwagnerstephenwagner Posts: 147
    edited 2009-11-20 19:07
    Rich,

    I hope this helps.

    Parallax / Resources / Nuts & Volts / Nuts & Volts Volume 1 / Rotary Encoders Provide Friendly Spin and Grin Interface

    http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol1/col/nv8.pdf

    Stephen Wagner
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-20 22:17
    Ok Stephen Wagner!
    Thanks for bringing another sample of code into the project!

    I have tried using pieces of this to no avail myself!
    But this is another good source for code! Thank you!

    I am still looking for ideas from poeple to help in coding to bring information input from Encoder Ch A and Ch B to the
    DEBUG Terminal Screen! Like adding a counter and having the count change in the Debug Terminal Screen on a line!

    This lets the Biginner start at a lower basic level with lower requirements and no additional Cost!

    The LCD Part, can be farther down the road as another Option!

    Thank you very much Stephen Wagner!

    Rich_W8VK
  • FranklinFranklin Posts: 4,747
    edited 2009-11-20 23:27
    Somebody said...
    I have tried using pieces of this to no avail myself!
    Just what is it you need? Several people have tried to help and most of your responses are that that doesn't work for you. What code do you have? Why does that code not meet your needs and How do you have your parts connected? That would help us help you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-11-20 23:38
    stamptrol,

    In your program you're utilising INB and you're masking with %0011.· In your comments and drawing you've indicated P7 & P6.· Shouldn't those be P5 and P4 then?


    W8VK,

    Instead of HIGH 0 and LOW 0 (turning on/off an LED) then it's a simple matter of using DEBUG "CW", CR and DEBUG "CCW", CR

    You can print the value of the variable "counter" in addition to the CW/CCW.
  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-21 00:36
    ·PJ, You may be right.

    ··Here is a video clip from the clients' application. Green LEDS are outputs, p15 at top. Been running since 2001.

    · http://www.youtube.com/watch?v=85CtYsow84M

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-21 00:51
    Hello All!
    ···· As I said, I'm not much of a code writter, But this is what i started using of Tracy Allens XOR Code! No Errors yet! but doesn't print anything to the Debug Screen either! I don't think it's much of anything·or much Help! Thanks!

    Feel free to add and change and re-post the new complete code!

    Rich_W8VK


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' This is Rotary Encoder Sample 1 White Case Covering, Big Pot
    '
    ' ----[noparse][[/noparse] Description ]
    '
    'This is a Quadrature Encoder with Inputs Ch A and Ch B inputting to a
    'counter that is displaying a number in the DUBUG Terminal! The up/down
    'is updated with rotation of the Encoder Knob.
    'This is XOR Logic originally by Tracy Allen.
    '
    ' ----[noparse][[/noparse] Varibles ]
    '
    ··········································· ' encoder, signals on P8 and P9 (two Lsb's of nibC)
    ··········································· ' using XOR logic
    old······ VAR······ Nib·················' previous state, only two bits
    new······VAR······ Nib················' current state
    ob······· VAR······ old.BIT0·········' bits to test
    nb······· VAR······ new.BIT1
    cnt······ VAR······ Word·············' for demonstration
    '
    ' ----[noparse][[/noparse] Initialization ]
    '
    DIRC = 0······························· ' make port C inputs
    old = INC & 3························· ' read initial state of inputs
    decode:
    ·· new = INC & 3····················· ' read state of encoder, two bits
    ·· IF new = old THEN decode····· ' loop until change to new state
    ·· cnt = ob ^ nb * 2-1 + cnt····· ' calculate motion
    ·· old = new··························· ' new becomes old
    · DEBUG HOME, SDEC5 cnt
    GOTO decode
    'The calculation of the Count
    'adds 1 when ob ^ nb = 1 and
    'subtracts 1 when ob ^ nb = 0.
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-21 04:15
    tongue.gif·Hello Folks!
    ······· I was able to go back in some of the other programs I wrote and adapt one that works!


    ' ======[noparse][[/noparse] Title ]==========================================================
    '
    '·· File....... Encod_Count_Test_1.BS2/Quadrature Encoder and Debug Display
    '·· Purpose.... Display a Count on DEBUG Terminal Screen per Encoder Movement!
    '·· Author..... Rich Carstensen
    '·· E-mail..... rcarstensen@woh.rr.com
    '·· Started.... 04 May 2008
    '·· Updated.... 20 November 2009
    '
    '·· {$STAMP BS2}
    '·· {$PBASIC 2.5}
    '
    ' =========================================================================
    '
    '
    [noparse][[/noparse] Program Description ]
    '
    ' BS2 MicroProcessor Scans P6 and P7 inputs for a button closure! This then
    ' changes a current counter count and Displays the new count number to the
    ' DEBUG Terminal Screen
    '
    '
    [noparse][[/noparse] Program Operation ]
    'Three of the four positions are used on the encoder! Holding in one
    'position to left of center counts down. Hold on center and count stops!
    'Hold on position to right of center counts up!
    '
    [noparse][[/noparse] I/O Definitions ]
    '
    UpBtn··········· ·PIN···· 6······················ ' select robot mode
    DownBtn········ PIN···· 7······················ ' start/stop robot
    '
    '
    [noparse][[/noparse] Constants ]
    '
    Pressed·········· CON···· 0···················· ' button states
    NotPressed····· CON···· 1
    '
    '
    [noparse][[/noparse] Variables ]
    '
    btns··········· VAR···· Nib···················· ·· ' button holder
    btn1··········· VAR···· btns.BIT0·············· ' debounced button value
    btn2··········· VAR···· btns.BIT1·············· ' deboucned button value
    idx··········· ·· VAR···· Byte··············· ···· ' digit index
    counter········ VAR···· Byte··················· ' current digit to display
    '
    '
    [noparse][[/noparse] EEPROM Data ]
    '
    '
    [noparse][[/noparse] Initialization ]
    '
    idx = 00
    '
    ·DEBUG CLS, CR,
    ······· "Encoder & Display Test! By: Rich Carstensen, W8VK", CR,
    ······· "counter· 0", CR,
    ······· "Down (P6).......... ", CR,
    ······· "Up·· (P7).......... "
    '
    PAUSE 500
    '
    '
    [noparse][[/noparse] Program Code ]
    '
    Main:
    · DEBUG CLS,
    ······· "Encoder & Display Test! By: Rich Carstensen, W8VK", CR,
    ······· "counter· 0", CR,
    ······· "Down (P6).......... ", CR,
    ······· "Up·· (P7).......... "
    '
    · DO
    ··· GOSUB Get_Buttons································· ·' scan buttons
    ··· GOSUB Show_Buttons······························· ' show states
    ··· IF (btns > %00) AND (btns < %11) THEN······ ' one or the other pressed?
    ····· counter = (counter + btn1)······················ ' increment if Button1 = +1
    ····· counter = (counter - btn2)·······················' decrement if Button2 = -1
    ····· PAUSE 250············································ ' 1/16 sec between changes
    ····· DEBUG CRSRXY, 0, 1, DEC ?counter··········· ' update DEBUG screen
    ··· ENDIF
    · LOOP······················································· ' do forever
    '
    '
    [noparse][[/noparse] Subroutines ]
    '
    Get_Buttons:············································· ·' Scan and debounce both buttons
    · btns = %0011·········································· ·' assume both pressed
    · FOR idx = 00 TO 2
    ··· btns.BIT0 = btns.BIT0 & UpBtn················· ·' scan mode button
    ··· btns.BIT1 = btns.BIT1 & DownBtn············· ·' scan start/stop button
    ··· PAUSE 5················································ ' debounce delay
    · NEXT
    · RETURN
    '
    ' Show current button states
    '
    Show_Buttons:
    · FOR idx = 0 TO 1
    ··· DEBUG CRSRXY, 20, 2 + idx····················· · ' move cursor
    ··· IF (btns.LOWBIT(idx) = Pressed) THEN····· ·· ' check and display status
    ····· DEBUG "Pressed", CLREOL
    ··· ELSE
    ····· DEBUG "Not Pressed"
    ··· ENDIF
    · NEXT
    · RETURN
    '

    Thanks!
    Rich_W8VK
  • FranklinFranklin Posts: 4,747
    edited 2009-11-21 04:18
    Somebody said...
    ·No Errors yet! but doesn't print anything to the Debug Screen either!
    Nothing? No numbers? Could you post how you have this all hooked up and how it is powered? Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-11-21 17:52
    Rich,

    It appears from your most recent post that your encoder is attached to pins p6 and p7, but in the example code adapted from my web site two posts back, it is assumed to be on p8 and p9. Which is it? You have to be sure that the PBASIC matches up with the hardware! If the encoder inputs really are on p6 and p7, you have to change 4 program lines,
    ob VAR old.BIT2 ' bits to test, instead of BIT0 and BIT1
    nb VAR new.BIT3
    old = inC & 12 ' instead of inC & 3
    new = inC & 12

    I am puzzled by your reference to "buttons" in the most recent post. UpBtn and DownBtn appear to be your names for the encoder inputs. It is okay to call them that, but I just want you to be sure that you recognize the "secret", that an encoder is quite different in purpose from a button. Your most recently posted program does not have encoder logic. It treats those inputs in fact as buttons and (although I have not tried it except Gedanken) the count will increment or decrement so long as the inputs are in opposite states, but that misses the intended purpose of an encoder.

    edit: I should have said, for p6 and p7...
    old = inB & 12 ' instead of inC & 3
    new = inB & 12

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 11/22/2009 9:52:24 PM GMT
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-22 07:51
    smile.gif·Hello again Folks!
    ······· This is a Final Draft to the Encod_Count_Test.bs2 and Renamed to Contact_Test_1.bs2
    ······· I corrected alot of the terms in this and other things if felt that needed it!
    ·Thanks!

    Rich_W8VK

    ' ======[noparse][[/noparse] Title ]==========================================================
    '
    '·· {$STAMP BS2}
    '·· {$PBASIC 2.5}
    '
    '·· File....... Contact_Test_1.BS2/Quadrature Encoder and Debug Display
    '·· Purpose.... Display a Count on DEBUG Terminal Screen per Encoder
    '·············· Movement!
    '·· Author..... Rich Carstensen
    '·· E-mail..... rcarstensen@woh.rr.com
    '·· Started.... 04 May 2008 Version 0.0
    '·· Updated.... 22 November 2009 Version 1.0
    '
    ' =========================================================================
    '
    '
    [noparse][[/noparse] Program Description ]
    '
    ' BS2 MicroProcessor Scans P6 and P7 inputs for a contact closure! This then
    ' changes a current counter count and Displays the new count number to the
    ' DEBUG Terminal Screen
    '
    '
    [noparse][[/noparse] Program Operation ]
    'Three of the four positions are used on the encoder! Holding in one
    'position to left of center counts down. Hold on center and count stops!
    'Hold on position to right of center counts up!
    '
    [noparse][[/noparse] I/O Definitions ]
    '
    Upcnt·········· PIN···· 6······················ ' select Up mode········· *
    Downcnt········ PIN···· 7······················ ' select Down Mode······· *
    '
    '
    [noparse][[/noparse] Constants ]
    '
    Pressed········ CON···· 0······················ ' Contact states
    NotPressed····· CON···· 1
    '
    '
    [noparse][[/noparse] Variables ]
    '
    cnts··········· VAR···· Nib················· ' Contact holder············ *
    cnt1··········· VAR···· cnts.BIT0··········· ' debounced Contact value··· *
    cnt2··········· VAR···· cnts.BIT1··········· ' deboucned Contact value··· *
    idx············ VAR···· Byte················ ' digit index
    counter········ VAR···· Byte················ ' current digit to display
    '
    '
    [noparse][[/noparse] EEPROM Data ]
    '
    '
    [noparse][[/noparse] Initialization ]
    '
    idx = 00
    '
    ·DEBUG CLS, CR,
    ······· "Encoder & Display Test! By: Rich Carstensen, W8VK", CR,
    ······· "counter· 0", CR,
    ······· "Down (P6).......... ", CR,
    ······· "Up·· (P7).......... "
    '
    PAUSE 500
    '
    '
    [noparse][[/noparse] Program Code ]
    '
    Main:
    · DEBUG CLS,
    ······· "Encoder & Display Test! By: Rich Carstensen, W8VK", CR,
    ······· "counter· 0", CR,
    ······· "Down (P6).......... ", CR,
    ······· "Up·· (P7).......... "
    '
    · DO
    ··· GOSUB Get_contacts······················ ' scan Contacts············· *
    ··· GOSUB Show_contacts····················· ' show Contact states······· *
    ··· IF (cnts > %00) AND (cnts < %11) THEN··· ' one or the other pressed?· *
    ····· counter = (counter + cnt1)············ ' increment if Contact1 = +1 *
    ····· counter = (counter - cnt2)············ ' decrement if Contact2 = -1 *
    ····· PAUSE 250····························· ' 1/16 sec between changes
    ····· DEBUG CRSRXY, 0, 1, DEC ?counter······ ' update DEBUG screen
    ··· ENDIF
    · LOOP······································ ' do forever
    '
    '
    [noparse][[/noparse] Subroutines ]
    '
    Get_contacts:·························· ' Scan and debounce both Contacts *
    · cnts = %0011························· ' assume both Contacts pressed··· *
    · FOR idx = 00 TO 2
    ··· cnts.BIT0 = cnts.BIT0 & Upcnt······ ' scan mode Up Contact··········· *
    ··· cnts.BIT1 = cnts.BIT1 & Downcnt···· ' scan mode Down Contact········· *
    ··· PAUSE 5···························· ' debounce delay
    · NEXT
    · RETURN
    '
    ······································· ' Show current Contact states···· *
    '
    Show_contacts:························· '································ *
    · FOR idx = 0 TO 1
    ··· DEBUG CRSRXY, 20, 2 + idx·············· ' move cursor
    ··· IF (cnts.LOWBIT(idx) = Pressed) THEN··· ' check and display status··· *
    ····· DEBUG "Pressed", CLREOL
    ··· ELSE
    ····· DEBUG "Not Pressed"
    ··· ENDIF
    · NEXT
    · RETURN
    '
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-22 08:52
    Hello Folks!
    ···· The following is a Revision to the TATestPgm1.bs2 to the new file TATestPgm3.bs2. This will be my final post to this and you Folks can add and modify it at will and then post the completed updated file as a new attachment!

    ···· I did see the you Tube Video, which is amazing!

    ···· My hopes with this file are that someone might incorporate some kind of Debug Display in the program!

    ···· And as a Final project with all that has been submitted to this Basic Stamp Topic, I was hoping someone would get the Scott Edwards Old time Radio Display up and working to the Debug Terminal Window!

    ··· Thanks for all the help! I think this is my Exit Point!

    ·· Rich_W8VK


    ' TATestPgm3.BS2
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' This is Rotary Encoder Sample 1 White Case Covering, Big Pot
    ' The * indicates Much Help in Editing the Program Line
    '
    ' ----[noparse][[/noparse] Description ]
    '
    'This is a Quadrature Encoder with Inputs Ch A and Ch B inputting to a
    'counter that is displaying a number in the DUBUG Terminal! The up/down
    'is updated with rotation of the Encoder Knob.
    'This is XOR Logic originally by Tracy Allen, and some present Editing!
    '
    ' ----[noparse][[/noparse] Varibles ]
    '
    ········································ ' encoder, signals on P6 and P7,
    ········································ '·(two Lsb's of nibC)
    ········································ ' using XOR logic
    old······ VAR······ Nib············· ' previous state, only two bits
    new······VAR······ Nib············ ' current state
    ob······· VAR······ old.BIT2······' bits to test··············· ······················ *
    nb······· VAR······ new.BIT3···· '·· ·················································· *
    cnt······ VAR······ Word··········' for demonstration
    '
    ' ----[noparse][[/noparse] Initialization ]
    '
    DIRC = 0··························· ' make port C inputs
    old = INC & 12···················· ' read initial state of inputs········· ······· ·*
    decode:
    ·· new = INC & 12················' read state of encoder, two bits··· ······· *
    ·· IF new = old THEN decode········ ' LOOp UNTIl change To new state
    ·· cnt = ob ^ nb * 2-1 + cnt······· ' calculate motion
    ·· old = new······················· ' new becomes old
    · DEBUG HOME, SDEC5 cnt
    GOTO decode
    'The calculation of the Count
    'adds 1 when ob ^ nb = 1 and
    'subtracts 1 when ob ^ nb = 0.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-11-22 15:59
    ·Rich_W8VK

    Thank You for sharing your code with us

    I going to give your code a try

    I have a Project that I want do with a switch like that

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-11-22 18:55
    As a side-note (after the fact) there is at least one project on these forum that uses a rotary encoder and has published code. Please see the following link for details.

    http://forums.parallax.com/showthread.php?p=637023

    A few others, such as the Binary / Digital Clock had been modified for this purpose as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    50 72 6F 6A 65 63 74 20 53 69 74 65
    ·
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-11-22 21:48
    Hi Rich,

    Did you try the encoder program and find that it worked up to your expectations? I ask, because in giving cheap advice, I forgot to change the input port from inC to inB. Pins p6 and p7 are the two msb on port inB. So that is why I wonder how the program as listed using inC could possibly have worked!? It should have the following lines:

    ' encoder, signals on P6 and P7,
                                             ' (two msb's of nibB)   <-----------!!!!!!!!!!
    old = inB & 12                     ' read initial state of inputs             *
    new = inB & 12                     ' read state of inputs                *
    



    A professor I had always said not to worry about mistakes in presentations, because they alert the presenter, at least in retrospect, if anyone is/was awake, understanding and skeptical. Of course, the logical alternative explanation is that the presenter is the one asleep!

    It is possible to write the code so that the pins are not hard-coded, however, that will slow down the operation and the Stamp needs all the speed it can get. When you have it working, test to see how fast you can turn the encoder before it gives errors. Not too fast, depending on the angular resolution of the encoder.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 11/22/2009 9:54:45 PM GMT
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-22 22:37
    smile.gif·Hello Tracy!
    ········ Yes I just did the correction to the code and man what a BIG Difference!
    I now have a count occurring on the Debug Screen! I couldn't get it to error out yet! I'm probably not spinning it fast enough yet! I did kind of notice a slow down
    it seemed like to the sreen though!

    Well Done Tracy Allen! Thanks! The Updated Code is thus posted!

    Any body want to take a shot at the old time radio tuning Display of Scott Edwards? I think it would also make a Great example for us learners!

    Thank you so much everybody!

    Rich_W8VK


    ' TATestPgm4.BS2
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' This is Rotary Encoder Sample 1 White Case Covering, Big Pot
    ' The * indicates Much Help in Editing the Program Line
    '
    ' ----[noparse][[/noparse] Description ]
    '
    'This is a Quadrature Encoder with Inputs Ch A and Ch B inputting to a
    'counter that is displaying a number in the DUBUG Terminal! The up/down
    'is updated with rotation of the Encoder Knob.
    'This is XOR Logic originally by Tracy Allen, and some present Editing!
    '
    ' ----[noparse][[/noparse] Varibles ]
    '
    ··································· ' encoder, signals on P6 and P7,
    ·························· ' (two Msb's of nibB) <
    !!!!!!!!!! *
    ··································· ' using XOR logic
    old······ VAR······ Nib············ ' previous state, only two bits
    new······ VAR······ Nib············ ' current state
    ob······· VAR······ old.BIT2······· ' bits to test····················· *
    nb······· VAR······ new.BIT3······· '·································· *
    cnt······ VAR······ Word··········· ' for demonstration
    '
    ' ----[noparse][[/noparse] Initialization ]
    '
    DIRC = 0··························· ' make port C inputs
    old = INB & 12····················· ' read initial state of inputs····· *
    decode:
    ·· new = INB & 12·················· ' read state of inputs············· *
    ·· IF new = old THEN decode········ ' LOOp UNTIl change To new state
    ·· cnt = ob ^ nb * 2-1 + cnt······· ' calculate motion
    ·· old = new······················· ' new becomes old
    · DEBUG HOME, SDEC5 cnt
    GOTO decode
    'The calculation of the Count
    'adds 1 when ob ^ nb = 1 and
    'subtracts 1 when ob ^ nb = 0.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-11-23 01:34
    Tracy Allan or any one else (Please explain What this part is)

    old = INB (& 12)·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-11-23 02:06
    old = INB & %1100

    That's a bitmask to isolate bit7 and bit6 and therefore·ignore bit5 and bit4.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-11-23 02:14
    PJ Allen

    Thank You

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-11-26 00:58
    Rich,
    I came in after the cartoons and definetly do not want to preach to the choir.
    May I recomend using·DEBUG freely in your future programs to verify your code?
    DEBUG is not only to see the final results of your code and it can be commented out when necessary.
    Can you supply some more info about your "old time radio" project?
    As noticed by others, BS2 is rather speed limited and "fast tunning dial" with display is difficult to implement.
    I do have a code for direct frequency synthesis circuit and finding it "cumbersome" to tune.
    AA7EJ Vaclav
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-11-26 03:07
    The dial·details are·in the Nuts & Volts (nv8) article that stephenwagner hotlinked several Replys ago.
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-27 07:15
    Hello AA7EJ vaclav_sal,
    ···· Here is the Old Time Radio Display Code as per the original program!
    I've marked four lines with the SEROUT that will have to be worked into some kind of DEBUG routine!
    ···· Anyone that wants to take a shot at it, feel free to change the code, then relist the whole bs2 file for people to try!
    ···· Thanks!

    Rich_W8VK


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Program: Rotary.BAS (Read a rotary encoder and scroll a display)
    ' This program demonstrates the use of a digital rotary encoder
    ' as a unique user-interface control. Turning the knob scrolls a
    ' virtual tuning dial displayed on an LCD. To keep the code and
    ' hardware as simple as possible, the LCD is equipped with an
    ' LCD Serial Backpack, which interprets data and instructions sent
    ' to it serially.
    SYMBOL old = B0 ' Previous state of encoder bits.
    SYMBOL new = B1 ' Current state of encoder bits.
    SYMBOL directn = BIT0 ' Direction of encoder travel; 1=CW.
    SYMBOL index1 = B2 ' For/Next counter variable.
    SYMBOL index2 = B3 ' For/Next counter variable.
    SYMBOL I = 254 ' Instruction-toggle for LCD.
    SYMBOL LCD_cls = 1 ' Clear-screen instruction for LCD.
    SYMBOL left = 24 ' Scroll-left instruction for LCD.
    SYMBOL right = 28 ' Scroll-right instruction for LCD.
    ' The program starts by printing a scale on the LCD screen.
    ' The LCD's RAM can hold up to 80 characters and scroll them
    ' circularly across the display. The code below prints...
    ' 0........10........20........30........40........50..
    ' ...up to 70. Only the first 24 characters are initially
    ' visible on the display, but turning the encoder knob scrolls
    ' them into view, like an old-fashioned radio tuning dial.
    pause 1000 ' Let LCD initialize.
    serout 0,n2400,(I,LCD_cls,I) ' Clear LCD screen.
    for index1 = 0 TO 70 STEP 10 ' Scale: 0-70 (uses 80-char LCD RAM).
    SEROUT 0,n2400,(#index1) ' Print number on the screen.················ 'Change
    for index2 = 1 TO 8
    SEROUT 0,n2400,(".") ' Print "........" between numbers.·············· 'Change
    next
    next
    ' Before entering the main loop, the program stores the beginning
    ' state of the encoder bits into the variable 'new.' It ANDs the
    ' pins with %11000000 in order to strip off all bits except for
    ' 6 and 7. (ANDing a bit with 0 always produces 0; ANDing with 1
    ' copies the state of the bit.)
    let new = pins & %11000000 ' Mask off all but bits 6 & 7.
    start:
    let old = new & %11000000 ' Mask bits and copy new into old.
    again:
    let new = pins & %11000000 ' Copy encoder bits to new.
    IF new = old THEN again ' If no change, try again.
    let directn = BIT6 ^ BIT15 ' XOR right bit of new w/ left bit of old.
    IF directn = 1 THEN CW ' If result=1, encoder turned clockwise.
    SEROUT 0,n2400,(I,left,I) ' If result=0, counterclock (scroll left).· 'Change!
    goto start ' Do it again.
    CW:
    SEROUT 0,n2400,(I,right,I) ' Clockwise (scroll right).··············· 'Change!
    goto start ' Do it again.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-11-27 18:24
    Here's a first stab at converting that BS1 program to PBASIC 2.5 for the Stamp 2. It compiles okay, but I haven't tested it on the hardware.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Rich_W8VKRich_W8VK Posts: 44
    edited 2009-11-28 14:04
    Hello Folks!
    ····· This is a Draft of the Old Time Radio Dial Graciously Submitted By: Tracy Allen. Tracy has taken the original BS1 Code and Transformed it into BS2 Code! I have tried it out and I see data across an LED hooked to P0 Line, but I don't have a Serial LCD and can't see it doing it's thing! Again Thanks goes to Tracy Allen! The new file is shown and attached! Hopefully a DEBUG Program will transform! Thanks!

    Rich_W8VK

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Program: Rotary.BAS (Read a rotary encoder and scroll a display)
    ' Note converted for PBASIC 2.5 for BASIC Stamp II
    ' This program demonstrates the use of a digital rotary encoder
    ' as a unique user-interface control. Turning the knob scrolls a
    ' virtual tuning dial displayed on an LCD. To keep the code and
    ' hardware as simple as possible, the LCD is equipped with an
    ' LCD Serial Backpack, which interprets data and instructions sent
    ' to it serially.
    old VAR Byte ' Previous state of encoder bits.
    new VAR Byte ' Current state of encoder bits.
    directn VAR Byte ' Direction of encoder travel; 1=CW.
    index1 VAR Byte ' For/Next counter variable.
    index2 VAR Byte ' For/Next counter variable.
    I CON 254 ' Instruction-toggle for LCD.
    LCD_cls CON 1 ' Clear-screen instruction for LCD.
    left CON 24 ' Scroll-left instruction for LCD.
    right CON 28 ' Scroll-right instruction for LCD.
    n2400 CON $4186·· ' 2400 Baud inverted for LCD
    ' The program starts by printing a scale on the LCD screen.
    ' The LCD's RAM can hold up to 80 characters and scroll them
    ' circularly across the display. The code below prints...
    ' 0........10........20........30........40........50..
    ' ...up to 70. Only the first 24 characters are initially
    ' visible on the display, but turning the encoder knob scrolls
    ' them into view, like an old-fashioned radio tuning dial.
    init_LCD:
    · PAUSE 1000 ' Let LCD initialize.
    · SEROUT 0,n2400,[noparse][[/noparse]I,LCD_cls,I] ' Clear LCD screen.·························· 'Change
    · FOR index1 = 0 TO 70 STEP 10 ' Scale: 0-70 (uses 80-char LCD RAM).
    ····· SEROUT 0,n2400,[noparse][[/noparse]DEC index1] ' Print number on the screen.············· 'Change
    ··· FOR index2 = 1 TO 8
    ····· SEROUT 0,n2400,[noparse][[/noparse]"."] ' Print "........" between numbers.·············· 'Change
    ··· NEXT
    · NEXT
    ' Before entering the main loop, the program stores the beginning
    ' state of the encoder bits into the variable 'new.' It ANDs the
    ' pins with %11000000 in order to strip off all bits except for
    ' 6 and 7. (ANDing a bit with 0 always produces 0; ANDing with 1
    ' copies the state of the bit.)
    new = INL & %11000000 ' Mask off all but bits 6 & 7.
    main:
    · DO
    ··· old = new & %11000000 ' Mask bits and copy new into old.
    ··· DO
    ····· new = INL & %11000000 ' Copy encoder bits to new.
    ··· LOOP WHILE new = old ' If no change, try again.
    ··· directn = old.BIT6 ^ new.BIT7 ' XOR right bit of new w/ left bit of old.
    ··· IF directn THEN' If result=1, encoder turned clockwise.
    ····· SEROUT 0,n2400,[noparse][[/noparse]I,right,I] ' Clockwise (scroll right).··············· 'Change!
    ··· ELSE
    ····· SEROUT 0,n2400,[noparse][[/noparse]I,left,I] ' If result=0, counterclock (scroll left).· 'Change!
    ··· ENDIF
    · LOOP ' Do it again.

    ·
Sign In or Register to comment.