Shop OBEX P1 Docs P2 Docs Learn Events
encoder3 — Parallax Forums

encoder3

BernardBernard Posts: 3
edited 2004-12-30 00:22 in BASIC Stamp
Dear Jon

This is the code you sent me and I use to test encoder.
I change nothing execpt input pins ( good rate for now because nothing else in the progamme).

What I try to do is to set a value in a programme read by the encoder.
·(Ie:As a feed back for a position of a motor, or use for setup and adress·in case of serial interrogation for another BasicStamp. Like have 2 swicths up/down)


·Bernard
' =========================================================================
'
'·· File....... Encoder.BS2
'·· Purpose....
'·· Author..... Jon Williams -- Parallax, Inc.
'·· E-mail..... jwilliams@parallax.com
'·· Started....
'·· Updated.... 13 DEC 2004
'
'·· {$STAMP BS2p}
'·· {$PBASIC 2.5}
'
' =========================================================================

'
[noparse][[/noparse] Program Description ]

'
[noparse][[/noparse] Revision History ]

'
[noparse][[/noparse] I/O Definitions ]
EncPort········ VAR···· INA 'APLS encoder with pull up IN0(A),IN1(B)

'
[noparse][[/noparse] Constants ]
Span··········· CON··· 60000···················· ' set value 0 - 99
NumTasks······· CON···· 3

'
[noparse][[/noparse] Variables ]
encNew········· VAR···· Nib···················· ' current scan
encOld········· VAR···· Nib···················· ' old scan
value·········· VAR···· Word
task··········· VAR···· Nib

'
[noparse][[/noparse] EEPROM Data ]

'
[noparse][[/noparse] Initialization ]
Reset:
· encNew = EncPort· & %0011· ' read current inputs
· encOld = encNew
· value=3000··· '3000 midlle value to test up and down
'
[noparse][[/noparse] Program Code ]
Main:
· DO
··· GOSUB Check_Encoder
··· ON task GOSUB Task_0, Task_1, Task_2
· LOOP
· END

'
[noparse][[/noparse] Subroutines ]
Check_Encoder:
· encNew = EncPort & %0011··············· ' read current inputs
········································· ' test for change
· IF ((encNew ^ encOld) <> 0) THEN········ ' test direction
· IF (encOld.BIT0 ^ encNew.BIT1) THEN
· value = value +(Span - 1) // Span····· ' increment value
· ELSE
· value = value + 1 // Span············· ' decrement value
· ENDIF
· encOld = encNew
· ENDIF
· RETURN

Task_0:
· DEBUG HOME, DEC5 value················ ' print value
· task = task + 1 // NumTasks··········· ' update task pointer
· RETURN

Task_1:
· ' do something
· task = task + 1 // NumTasks
· RETURN

Task_2:
· ' do something
· task = task + 1 // NumTasks
· RETURN

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-30 00:22
    Keep in mind that the BASIC Stamp can only handle a single task, so you'll have to use a flow-control pin for serial connection to an external device. If you're looking to build a coprocessor for other projects, you might want to look into the SX and SX/B. This link shows how to read an encoder in an Interrupt Service Routine so that you can get really good performance. Likewise, you could add serial routines in the interrupt so that you don't miss any communications (otherwise you need to disable the interrupt when using SERIN or SEROUT):

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

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.