piclist 2008\07\28\170720a >
Thread: Using the Encoder Sample
www.piclist.com/techref/ubicom/lib/io/index.htm?key=sensor+encoder
BY : _BPM_


In SX Microcontrollers, SX/B Compiler and SX-Key Tool, _BPM_ wrote:

I tried to turn this code into code that can read SIX encoders.  
I ran into variable space limits.
Is there any way to process that many encoders with a single sx?    I am I running into territory that would be eaiser if I went with a PROPELLER?


[Quoting: "Michael Chadwick"]I haven't tested this (no hardware eh?) but it compiles.

This saves a couple bytes, at the cost of using a second temp in the interrupt. Since you have both encoders on the same port, there is no reason you can't save both channels in one oldbyte and save space.  You can also do the shifting and xoring of both channels at the same time, since you only look at one bit of the results to determine direction.
This also lends itself to extension to 4 channels on one 8 bit port, by adding the appropriate bit tests for additional channels.  Or make the thing a loop with a bit mask to test the change and direction bits and increment or decrement the positions in an array.


DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000,4_480_000
ID  "ENCODER"
EncPort         VAR     RA                      ' encoder port
TRIS_Enc        VAR     TRIS_A
MaxVal          CON     255
 
'encCheck VAR Byte
EncodersOld     VAR     byte                  
EncodersNew     VAR     byte                  
Encoder_A_Count VAR     byte  
Encoder_B_Count VAR     byte  
tmpB1           VAR     byte
tmpB2           VAR     Byte
tmpB3           VAR     Byte

' =========================================================================
INTERRUPT
' =========================================================================
' Runs every 64 uS @ 4 MHz (no prescaler)
ISR_Start:
EncodersNew = EncPort
TmpB1 = EncodersOld XOR EncodersNew ' compare old with new
                                    ' to work out which channels have changed
TmpB2 = TmpB1 << 1   ' make single bit that shows change on A or B phase
TmpB1 = TmpB1 OR TmpB2
EncodersOld = EncodersOld << 1  ' make direction bits by comparing old A with new B
EncodersOld = EncodersOld XOR EncodersNew ' xor old A with new B
IF TmpB1.3 = 1 THEN
    IF EncodersOld.3 = 1 THEN
  INC Encoder_A_Count                          
    ELSE
  DEC Encoder_A_Count                          
     ENDIF
 ENDIF
' =========================================================================
IF tmpB1.1 = 1 THEN                  
     IF EncodersOld.1 = 1 THEN
    INC Encoder_B_Count                          
     ELSE
  DEC Encoder_B_Count                          
     ENDIF
  ENDIF
EncodersOld=EncodersNew
'ISR_Exit:
 RETURNINT                                    
' =========================================================================
 PROGRAM Start
' =========================================================================

Start:
                             

 EncodersNew = EncPort
 EncodersOld = EncodersNew
 Encoder_A_Count = 0
 Encoder_B_Count = 0
 
 OPTION = $88                                  ' interrupt, no prescaler
Main:
 DO

ShiftOut RC.7,RC.6,1,Encoder_A_Count
Pulsout RC.0,1
 
ShiftOut RC.7,RC.6,1,Encoder_B_Count
Pulsout RC.0,1  
 
 
 
 LOOP

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=147541#m281889
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2008 (http://www.dotNetBB.com)
<bf2802d0b5474493a19a3748a4d65891@ZEUS>

See also: www.piclist.com/techref/ubicom/lib/io/index.htm?key=sensor+encoder
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Using the Encoder Sample

month overview.

new search...