EmbeddedRelated.com
Forums

Quadrature Encoder

Started by nilesh shirbhate December 25, 2009
hi,
I am trying to interface Quadrature Encoder with LPC 2148.
I am using EINT1 and EINT2 (External Interrupt) for Signal A and Signal B.
I have used two ISRs.
Please guide.
Thanks and Regards
Nilesh


The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. http://in.yahoo.com/

An Engineer's Guide to the LPC2100 Series

nilesh shirbhate schrieb:
> hi,
> I am trying to interface Quadrature Encoder with LPC 2148.
> I am using EINT1 and EINT2 (External Interrupt) for Signal A and Signal B.
> I have used two ISRs.
> Please guide.

Where to ? Next ashram ?

--
42Bastian

--- In l..., nilesh shirbhate wrote:
>
> hi,
> I am trying to interface Quadrature Encoder with LPC 2148.
> I am using EINT1 and EINT2 (External Interrupt) for Signal A and Signal B.
> I have used two ISRs.
> Please guide.
> Thanks and Regards
> Nilesh
>

Assuming your interrupt routines work properly, here's what you need.

If both signals start out low and both interrupts are set to interrupt on the leading edge, then when signal A goes high the ISR generates Distance++ and sets the A edge control for negative.

When B goes high, its interrupt routine generates Distance++ and sets the edge for negative going.

When A goes low, its interrupt routine generates Distance++ and sets the edge for positive.

And so on...

You need to generate a state diagram to see whether to add or subtract at each edge. For example, if A goes positive and generates Distance++ and then A goes negative before B goes positive, the interrupt routine needs to generate Distance--. If B then goes positive, its interrupt routine needs to generate Distance--.

And so on...

Draw the picture. You set the initial interrupt edge depending on the state of the inputs during initialization. You shouldn't just assume both inputs are low and set the interrupts for rising edge.

Richard

--- In l..., "rtstofer" wrote:
>
>
>
> --- In l..., nilesh shirbhate wrote:
> >
> > hi,
> > I am trying to interface Quadrature Encoder with LPC 2148.
> > I am using EINT1 and EINT2 (External Interrupt) for Signal A and Signal B.
> > I have used two ISRs.
> > Please guide.
> > Thanks and Regards
> > Nilesh
> >
>
> Assuming your interrupt routines work properly, here's what you need.
>
> If both signals start out low and both interrupts are set to interrupt on the leading edge, then when signal A goes high the ISR generates Distance++ and sets the A edge control for negative.
>
> When B goes high, its interrupt routine generates Distance++ and sets the edge for negative going.
>
> When A goes low, its interrupt routine generates Distance++ and sets the edge for positive.
>
> And so on...
>
> You need to generate a state diagram to see whether to add or subtract at each edge. For example, if A goes positive and generates Distance++ and then A goes negative before B goes positive, the interrupt routine needs to generate Distance--. If B then goes positive, its interrupt routine needs to generate Distance--.
>
> And so on...
>
> Draw the picture. You set the initial interrupt edge depending on the state of the inputs during initialization. You shouldn't just assume both inputs are low and set the interrupts for rising edge.
>
> Richard
>

If this is a "mechanical" electrical switch encoder, you may want to "debounce" the switch closures and opens as well. Use a fast counter or something like that (timer interrupt ?) to "ignore" switchings that occur faster than say, every few milliseconds or so. Otherwise, you may get LOTS of encoder "hits" when you only wanted one increment or decrement and direction change.

My cents worth on this fine Christmas day here in the pacific northwest.

boB

--- In l..., "bobtransformer" wrote:

> If this is a "mechanical" electrical switch encoder, you may want to "debounce" the switch closures and opens as well. Use a fast counter or something like that (timer interrupt ?) to "ignore" switchings that occur faster than say, every few milliseconds or so. Otherwise, you may get LOTS of encoder "hits" when you only wanted one increment or decrement and direction change.
>
> My cents worth on this fine Christmas day here in the pacific northwest.
>
> boB
>

Even if it is photo-optical, there can still be multiple transitions when an edge is detected. I'm thinking of the alternating black-white spokes, for example.

I have mixed feelings about debouncing this circuit as the whole point of Gray encoding is to eliminate erroneous transitions. In the first place, the uC isn't sensitive to bounce in the millisecond range because the interrupt handler will be very fast and will easily capture these. If it fails to recognize a glitch in the microsecond range, who cares? The glitch made a full transition which would have, in aggregate, had no effect on the total count (Distance++ followed by Distance--, for example). We're looking for a specific edge on the off chance we missed a transition.

If the switch is SPDT then ring-tailed NAND gates can clean up the signal. If it is SPST then a low pass filter can be used but it will limit the maximum frequency but then so would the bounce if it bounces so long that the very long filter is required.

Figure 8 of the User Manual indicates that there is some kind of 'glitch filter' on the input. I didn't see a description of how that works. It probably can't cause much delay because interrupt latency would suffer and it's already delayed a few clocks because of the pipeline.

It's probably worth trying to clean up the signals but it's going to depend on what kind of device is providing the input.

Richard

--- In l..., nilesh shirbhate wrote:
>
> hi,
> I am trying to interface Quadrature Encoder with LPC 2148.
> I am using EINT1 and EINT2 (External Interrupt) for Signal A and Signal B.
> I have used two ISRs.
> Please guide.
> Thanks and Regards
> Nilesh
>

Personally, I would prefer a discrete logic to decode A and B signal to
direction and count signal, unless encoder block is built in micro like PIC.

Warm Regards,
Mukund Deshmukh,
Beta Computronics Pvt Ltd,
10/1 IT Park, Parsodi,
Nagpur -440022.

Mukund Deshmukh wrote:
> --- In l..., nilesh shirbhate wrote:
>> hi,
>> I am trying to interface Quadrature Encoder with LPC 2148.
>> I am using EINT1 and EINT2 (External Interrupt) for Signal A and Signal B.
>> I have used two ISRs.
>> Please guide.
>> Thanks and Regards
>> Nilesh
>> Personally, I would prefer a discrete logic to decode A and B signal to
> direction and count signal, unless encoder block is built in micro like PIC.
>
> Warm Regards,
> Mukund Deshmukh,

Hi,

If you are looking for an external decoder we have been using for
years an LSI-CSI part called the LSLF7084. It decodes the A and B
channels and produces a Clock and Direction output. It also has a RC
input so you can eliminate noise on the inputs.

http://usdigital.com/products/interfaces/ics/lfls7084-s/

If you want a programmable SPI version with 32 bit counter you can
look at their LSLF7366 part. It is a great part but the SPI clock can
only run up to 4MHz.

http://usdigital.com/products/interfaces/ics/lfls7366r-s/

regards,
Charles
--- In l..., "Charles R. Grenz" wrote:

> Hi,
>
> If you are looking for an external decoder we have been using for
> years an LSI-CSI part called the LSLF7084. It decodes the A and B
> channels and produces a Clock and Direction output. It also has a RC
> input so you can eliminate noise on the inputs.
>
> http://usdigital.com/products/interfaces/ics/lfls7084-s/
>
> If you want a programmable SPI version with 32 bit counter you can
> look at their LSLF7366 part. It is a great part but the SPI clock can
> only run up to 4MHz.
>
> http://usdigital.com/products/interfaces/ics/lfls7366r-s/
>
> regards,
> Charles
>

Either the 7083 or 7084 would be a great candidate. The uC still has to deal with count interrupts but that might be preferred if some kind of PID was being implemented.

The 7366 part could be quite useful in situations where PID was not a consideration. It offloads all of the work in exchange for having to implement SPI which might be required for some other module anyway.

I have implemented the quadrature encoder logic on an Atmel ATmega128. Even though the processor is only running at 14.7 MHz, the interrupts can occur well into the 50 kHz range (I measured this once but I am only guessing at the results). In any event, it was fast enough for any motor or wheel that I might consider.

I have been thinking about implementing the encoder on an LPC2148 as a module for a later project.

Richard

rtstofer wrote:
>
> --- In l..., "Charles R. Grenz" wrote:
>
>> Hi,
>>
>> If you are looking for an external decoder we have been using for
>> years an LSI-CSI part called the LSLF7084. It decodes the A and B
>> channels and produces a Clock and Direction output. It also has a RC
>> input so you can eliminate noise on the inputs.
>>
>> http://usdigital.com/products/interfaces/ics/lfls7084-s/
>>
>> If you want a programmable SPI version with 32 bit counter you can
>> look at their LSLF7366 part. It is a great part but the SPI clock can
>> only run up to 4MHz.
>>
>> http://usdigital.com/products/interfaces/ics/lfls7366r-s/
>>
>> regards,
>> Charles
>> Either the 7083 or 7084 would be a great candidate. The uC still has to deal with count interrupts but that might be preferred if some kind of PID was being implemented.
>
> The 7366 part could be quite useful in situations where PID was not a consideration. It offloads all of the work in exchange for having to implement SPI which might be required for some other module anyway.
>
> I have implemented the quadrature encoder logic on an Atmel ATmega128. Even though the processor is only running at 14.7 MHz, the interrupts can occur well into the 50 kHz range (I measured this once but I am only guessing at the results). In any event, it was fast enough for any motor or wheel that I might consider.
>
> I have been thinking about implementing the encoder on an LPC2148 as a module for a later project.
>
> Richard
>

Hi Richard,

We currently use the 7366 with a PID loop Td of 4KHz and we control
motor speeds from 25rpm to 18,000rpm. The 4MHz SPI maximum is not a
problem with this control and using the LPC2148 or even the LPC2103
running at 60MHz we can service the timer isr with encoder read in
26us. More then enough time left over for other duties. This is for an
BLDC controller we currently sell.

The 7084/7083 we have used also with the LPC2138 (59.xxxMHz), 2148 and
2103 running at 60MHz and with full optimization on (IAR 5.4.1) it
takes 2us to run the encoder isr (signed long integer with direction
check) and another 2us to perform a floating point scaling of the
encoder value.

regards,
Charles
--- In l..., "Charles R. Grenz" wrote:

> Hi Richard,
>
> We currently use the 7366 with a PID loop Td of 4KHz and we control
> motor speeds from 25rpm to 18,000rpm. The 4MHz SPI maximum is not a
> problem with this control and using the LPC2148 or even the LPC2103
> running at 60MHz we can service the timer isr with encoder read in
> 26us. More then enough time left over for other duties. This is for an
> BLDC controller we currently sell.

For the 18k RPM, how many PPR are you using?

I think my interest lies with measuring distance from the wheels of a small robot. The RPM won't be very high and neither will the PPR (maybe 32 PPR) so handling this directly with the '2148 seems quite feasible.

But I'm going to keep those chips in mind. In fact, I may order a few after the first of the year. They're pretty inexpensive.

Richard