PDA

View Full Version : quad encoders



cpayne
- 13th March 2007, 17:49
Hi. I have gotten a quad endcoder working using a similar approach to this thread http://www.picbasic.co.uk/forum/showthread.php?t=778

The issue I am having is I am not clever enough to elimnate the while end statement. The issue is if the encoder is not moving I do not want it to wait. I need to be doing other "quick" tasks then continue looking for a change. I also need to add another quad encoder, so it can not be waiting on one while missing all the counts on the other. The encoders are not moving fast and are only 128ppm so speed is not a huge issue and I do not have access to the timers, nor do I have enough for two quad encoders.

here is my code thus far...

start:
if P1 = 1 then 'wait for button push
HIGH LX1 : low LX2 'led green
else
low LX1 : high LX2 'led red
countsx = 1000 'junk number for now
countsy = 1000 'junk number for now
debug 10,13,"Start...",10,13 'debug statements to be removed to speed up later
'will put a command to drive a motor here
repeat
gosub sub_count
until (countsX >= (1000 + 128 * 10)) or Estop = 0 'want to count Y axis too on another encoder
'will stop motor here, recount during drift, adjust motor, etc.
endif
debug "X: ", dec countsx, " ", 13

goto start

sub_count:
'right now just looking at x axis encoder. Need to look at y axis at same time
OldPosx = (PORTB & %00011000)>>3 ' get only PORTB<3:4> bits
NewPosx = OldPosx
while (NewPosx==0) or (NewPosx==3)
'program hangs here if not moving.
'tried an if then, but the oldposx keeps getting reset so it does not work
'cant figure out a clever way to remove this loop without missing or counting too much
NewPosx = (PORTB & %00011000)>>3 ' get only PORTB<3:4> bits
wend
Newposx = newposx ^ oldposx
select case newposx
case 1
countsx = countsx + 1
case 2
countsx=countsx - 1
end select
return