Polling vs. Interrupts in counting pulses - what is better?

Hi. When counting pulses using a microcontroller (say from an encoder or pulse generator), there are two methods: polling and using interrupts. Two alternative designs can be used (well, at least), one is to have the Microcontroller do it directly, and the other is to use a dedicated chip to do it. The concern is of course that the microcontroller might miss pulses if it is loaded with other tasks.

So which method is prefered? Any suggestions?

Thanks

Reply to
ElderUberGeek
Loading thread data ...

Depends on your pulse rate and processor loading. One thing to look at is the Time Processor Unit (TPU) on Freescale 683xx and PowerPC micros. It is a microcoded coprocessor with the ability to execute either ROM-based or RAM-based microcode. Freescale provides a variety of routines in ROM for various functions. Infineon probably has micros with similar units.

See

formatting link
for some TPU info.

~Dave~

Reply to
dave

Depends on processor speed, loading and data rate, plus the consequences of missing the odd change.

If the change is slow compared with the polling cycle you can do (say, it's never going to be > 1k changes/ second), you can simply poll the inputs in a 1ms rate interrupt.

If you don't mind losing the odd pulse (say a mouse or an encoder used as a volume etc. control), just ignore cycles where there's more than 1 state change, and resynch the process.

Faster than that, it's quite economical to add a PLD to do the encoder for you (example on OpenCores if you can't work it out), depending on the application you might only need a short counter which can be extended in the interrupt.

Paul Burke

Reply to
Paul Burke

Most micros allow you to use a counter directly only interrupting at over/underflow, this is by far the best method. For counting from an encoder there are micros with up/down biphase counters especialy for this application.Useing interrupts is the next best method but you are quite restricted as to how fast you can go. Polling is the very worst option only suited to slow rates and non critical counts.

Reply to
cbarn24050

I assume you really mean counting pulses rather than measuring the time between pulses. For counting pulses, most microcontrollers have counters which can be driven from an input pin in order to count pulses. If you want to measure speed and current position you can read the counter at fixed intervals. If you want to do something after a fixed number of pulses you can usually pre-load the counter with 0-counts required so it overflows at the required count. Most counters can be made to cause an interrupt on overflow.

If you need to do something every pulse that is a whole different ball game.

Ian

Reply to
Ian Bell

Use a microcontroller with an embedded counter circuit. As an example, the PIC18F4550 (nothing special aobut it, I just happen to have the datasheet handy) has 4 timer circuits, and 3 can accept external clock inputs. You can then poll the counter circuits periodically to see the number of pulses detected since the last reading.

Reply to
Richard Henry

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.