PDA

View Full Version : Counting pulses


Jawahar
04-14-04, 03:19 PM
Is it possible to use pins other than timer pins for counting pulses?

I have a IsoPod controller.

Thanks.

RMDumse
04-14-04, 03:32 PM
The timer pins are the best for counting pulses, because they have hardware allowing them to count. No other pins have hardware counting.

It would certainly be possible to count on the port lines using software, if the pulses were slow enough, and the software ran a high enough periodicity.

Another approach would be to use interrupts, and make the port lines cause an interrupt when they changed state. As long as the lines were not bouncy, they could be made into very good counters to a rather high frequency.

The quadrature decoders could count as well, but since they share lines with the timer lines, there's not much point in exploring that.

That's all the possibilities that come to mind. Hope that helps.

Jawahar
04-14-04, 03:50 PM
The pulses come in at around 300 per second. Would I be able to use port-lines/software like you said?

Thanks.

RMDumse
04-14-04, 04:06 PM
If you set your software to run in the machine-chain and the machine chain ran 1000 times a second (for instance) it should be very easy to count pulses in software.

I can think of a bunch of ways to approach this in software, but this looks like a perfect little statemachine example. Let me see...

VARIABLE COUNT-PAO

MACHINE WATCH-PAO
ON-MACHINE WATCH-PAO
APPEND-STATE WHILE-HI-PA0
APPEND-STATE WHILE-LO-PA0

IN-STATE
WHILE-HI-PA0
CONDITION
PA0 OFF?
CAUSES
COUNT-PA0 1+!
THEN-STATE
WHILE-LO-PA0
TO-HAPPEN

IN-STATE
WHILE-LO-PA0
CONDITION
PA0 ON?
CAUSES
COUNT-PA0 1+!
THEN-STATE
WHILE-HI-PA0
TO-HAPPEN

: INIT
0 COUNT-PA0 !
EVERY 5000 CYCLES SCHEDULE-RUNS WATCH-PA0
;


That's not tested code, but it ought to give you the basic idea. You will get a count each time PA0 changes state. To do more counters, just copy the machine for each line, and change the particulars to the appropriate port line. Add a machine chain listing all the machines, and there you go.

Jawahar
04-14-04, 04:15 PM
I'll give it a shot using port lines. Let me see what happens.

Thanks for your answers!:)

Jawahar
04-16-04, 04:40 PM
Yeah...PA0, PA1, ... port lines do work fine as a pulse counter with the code you have given.

Thanks.