encoder3
Bernard
Posts: 3
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
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
http://forums.parallax.com/showthread.php?p=519708
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office