RE:[bascom] Bascom Avr Quadrature Encoder code
Hi John,
I have posted the following some months ago. Maybe it's what are you looking
for, otherwise don´t hesitate to contact me for more info.
Best Regards.
Fran Dominguez - EC1CFD
----- Original Message -----
From: "Fran Dominguez" <fran@sistelcom.com>
To: <bascom@grote.net>
Sent: Monday, November 12, 2001 1:44 PM
Subject: Re: [bascom] Rotary encoders
> Hi Tim,
> a couple of months ago, I have posted the following text in 8052.com,
maybe
> it will be what are you looking for.......
>
>
****************************************************************************
> ********
> Hi all,
> maybe this thread needs no more comments, but I think this can help
somebody
> that has doubts about what method to use -polling or interrupts- reading
> encoders.
>
> I have been working for many years with ALPS encoders. They are cheap
(about
> $2.5), give typical 2 bit Gray code and 24 detent points, so the angular
> resolution is 360/24= 15 degrees: the possible STABLE positions are:
> 0-15-30-45-60-75-90 and so on, in a stable position both A and B encoder
> outputs are set to logic 1, hence if the user turns the axis from a detent
> position to the next detent position there is no possibility of missing
> codes even when you turn forth and back. The problem arises when the user
> stops turning before reach the next detent position, e.g.. 10 degrees,
then
> an interrupt has been generated but no one will be generated until you
reach
> the next detent position and turn from it, so practically speaking you
have
> not loose pulses, given that you have not completed a full cycle. Well
this
> is not strictly true: suppose you turn from 0 to 10 degrees: an interrupt
> has been generated and check B that is logic 1, then your program assumes
> that you have turn the axis from 0 to 15 degrees, if you continue turning
> from 15 to 30 there is no problem at all, but if you reverse direction
from
> 10 to 0, the new position is 0 degrees but no interrupt has been generated
> because of rising edge on interrupt pin, so your program thinks you are in
> 15 when it really is in 0 degree.
>
> One solution to this problem is polling A and B, as Peter and Erik have
> explained.
>
> Other solution is to use a PIC, they have a change state external
interrupt,
> this means: an interrupt can be generated both in rising and falling edges
> on INT pin. Yeah, I know this is a 8052 forum so I have to give solutions
> and not only complain about some weak points in 8052 architecture.
> Some time ago I posted a message about emulating PIC external interrupts
> with 8052 and Peter post a very good idea on how to do it: using external
> interrupt pin and a counter. The disadvantage of his solution is that you
> need a spare timer. But it really works fine. I named his idea as the
> SOFTWARE SOLUTION.
>
> Ok, now my HARDWARE SOLUTION: you need a XOR gate, apply channel A to one
> input and the same channel A to the other XOR input through a RC filter.
> This RC works as a delay line, so every time A changes its state a pulse
is
> generated in XOR output then join this output to 8051 EXT interrupt pin.
You
> may wonder, but what now? How the missing pulse problem is solved? The
> solution follows:
>
> When an interrupt is triggered you have to test both A and B channels and
> depending on the value you get, you can determine if you have turned the
> axis CW or CCW:
>
> A=0 and B=0 turned Clockwise
> A=1 and B=1 turned Clockwise
> A=0 and B=1 turned CounterClockwise
> A=1 and B=0 turned CounterClocwise
>
> Easy, doesn´t it?
> Maybe it is a bit difficult to understand these A and B values, but if you
> draw a circle with some diametral lines, the sectors you get represents
> alternatively a lever and lack of lever that opens and closes two
> switches -A and B- that placed in the proper place will yield the numbers
> showed previously.
> It sounds complicated but it is easier than it looks. Take into account
that
> English is not my native language and I am not able to express myself as I
> would like, so If someone needs more info about this I can make a draw and
> upload it to somewhere.
>
> I prefer to use interrupt method instead of polling and hope this will be
> useful to those who need interrupt driven Encoder reading, but I think the
> Alfredo´s solution is adequate for human interface and I have been using
it
> for years.
>
> Best Regards.
>
> Fran Dominguez.
>
>
****************************************************************************
> **************
>
> If you want to read the complete thread, the link is:
> http://www.8052.com/forum/read.phtml?id=14599
>
>
> Best regards,
>
> Fran Dominguez
>
>
>
> ----- Original Message -----
> From: "Tim Helps" <thelps@iprimus.com.au>
> To: <bascom@grote.net>
> Sent: Sunday, November 11, 2001 8:10 AM
> Subject: [bascom] Rotary encoders
>
>
> > Hi all,
> > I am new again to the list and was hoping someone can help with some
> BASCOM
> > code for incremental rotary encoders, the ones with 2 output bits. I
need
> > to be able to simply increment or decrement a variable upon turning it.
I
> > beleive this can be done with XOR functions? Does BASCOM support XOR?
> >
> > Many thanks
> >
> > Tim
> >
> >
>