Like I said earlier, read the state of channel B at the rising edge of channel A and you have the direction.
Like I said earlier, read the state of channel B at the rising edge of channel A and you have the direction.
The two signals are 90 degrees out of phase in relation to each other. This is what it looks like:
When the encoder is rotating "forward" (ie you're going left to right in the above picture) channel B is low at the rising edge of channel A. When the encoder is rotating "backwards" (ie you're going right to left in the above picture) channel B is high at the rising edge of channel A. So, you need to detect when channel A goes from low to high and then read channel B.The above code obviosuly presents a risk of hanging if the velocity happens to be 0 or very low but you get the idea.Code:WHILE ChannelA_Input = 1 : WEND ' Wait for channel A to go low if it happens to be high WHILE ChannelA_Input = 0 : WEND ' Now wait for the rising edge of channel A Direction = ChannelB_Input ' If channel B is high we're moving in one direction, if it's low we're moving in the other
/Henrik.
I can find direction with this code:
A tied to portb.4
B tied to portb.5
Direction = portb.5 ^ portb.4
lcdout $FE,1,DEC(Direction)
but, the reading is NOT consistent , if I turn clockwise , Direction is not always 1 ( well , most of the time it is 1 ) and vice versa
any tips anyone ?
When doing it like that you don't "where" in the cycle you are reading the inputs.
Since you're basically first reading one input then the other it's possible for "the other" input to change state from what it actually was when you read the first input.
Try something likeThis will insure that both bits are sampled at exactly the same time.Code:Temp = PortB Direction = Temp.5 ^ Temp.4
/Henrik.
Sorry to be so dumb, but I don't see how XOR'ing A and B produces a direction signal. When I try it on paper I get a new signal at double the frequency??
Bookmarks