Forums

Timer as counter...

Started by Andrew Berney May 23, 2006
Just a quick question to see if there's anyway of using the inbuilt timer
functionality of a LPC2119 as a counter. Ie to count the number pulses
(rising or falling edge) from an external source over a given time frame.
Basically we have a pot that changes a source input frequency from 36khz to
around 48khz and driving an IRQ for each pulse doesn't strike me as being a
good idea. All I need to realistically know is roughly what frequency it's
being driven at.

Ideas?

Andy

An Engineer's Guide to the LPC2100 Series

---- Original Message ----
From: "Andrew Berney"
To:
Sent: Tuesday, May 23, 2006 5:26 PM
Subject: [lpc2000] Timer as counter...

> Just a quick question to see if there's anyway of using the inbuilt
> timer functionality of a LPC2119 as a counter. Ie to count the number
> pulses (rising or falling edge) from an external source over a given
> time frame. Basically we have a pot that changes a source input
> frequency from 36khz to around 48khz and driving an IRQ for each
> pulse doesn't strike me as being a good idea. All I need to
> realistically know is roughly what frequency it's being driven at.

Being able to count rising and/or falling edges on the CAPx.x pins was added
to LPC213x and newer. The timer hardware of the LPC2119 cannot.

Connecting your signal to an interrupt pin, and incrementing a counter in
the interrupt handler will take a few percent performance.

You may be able to misuse some other peripherals to get a lower interrupt
rate. For example, configure SPI to slave mode, and connect your signal to
SCKx, and you'll get an interrupt every eighth clock.

Or maybe configure a UART to a suitably high baudrate and FIFO with trigger
level 14, and connect your signal to RxD. The UART should detect every
falling edge as the start of a new incoming byte, and at every 14th edge,
you should get a FIFO full interrupt. The baudrate should not be so high
that you get character timeout interrupts.

You can also connect your signal to both a CAPx.0 and a CAPx.1 pin, and
capture CR0 on rising edges and CR1 on falling edges. You can then always
check the half-period time by looking at CR0 and CR1. This requires no
interrupts at all.

Karl Olsen

Hi,

If your processor clock speed is reasonably fast (e.g. 60 Mhz),
you'd see little impact on performance if you configure an FIQ
interrupt to increment a single counter whenever the rising or
falling edge happens.

By pre-loading the banked ragisters before you start (e.g. with the
address of the counter etc.), you'd probably get the code down to
less than about six assembler instructions in total, and you
probably wouldn't even have to save any context. Even with the
standard interrupt latency, you'd almost certainly be less than
about 30 cycles on average (being generous).

At 48KHz interrupt rate and a clock of 60 Mhz, you have 1250 cycles
to play with: 30 of these to handle the interrupt, or 2.4% of the
available CPU.

A much slower timer interrupt could periodically read (and reset)
the counter to calculate the frequency.

Although it's not particularly "clever" (being something of a
sledgehammer) it would work, and has the virtue of simplicity.

We've used 40 Khz interrupts and an interpolating filter to output
an 8KHz sampled data (to reduce noise), and it works very well.

Regards
Brendan
--- In l..., "Andrew Berney" wrote:
> Just a quick question to see if there's anyway of using the
inbuilt timer
> functionality of a LPC2119 as a counter. Ie to count the number
pulses
> (rising or falling edge) from an external source over a given time
frame.
> Basically we have a pot that changes a source input frequency from
36khz to
> around 48khz and driving an IRQ for each pulse doesn't strike me
as being a
> good idea. All I need to realistically know is roughly what
frequency it's
> being driven at.
>
> Ideas?
>
> Andy
>

> > Just a quick question to see if there's anyway of using the inbuilt
> > timer functionality of a LPC2119 as a counter. Ie to count the number
> > pulses (rising or falling edge) from an external source over a given
> > time frame. Basically we have a pot

I too have a similar problem, but I want measure 4 channel low freq (about 1
Khz) signal.
Any idea how it could be implemented using one timer.
Earlier we were using C8051F040, which has five timers.

Best Regards,

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

--- In l..., Mukund Deshmukh
wrote:
> I too have a similar problem, but I want measure 4 channel low
freq (about 1
> Khz) signal.
> Any idea how it could be implemented using one timer.
> Earlier we were using C8051F040, which has five timers.
>

You don't say anything about the signals, e.g. whether they're
analog or digital, or how stable they are in frequency.

If the frequency isn't changing much over a reasonable time frame, a
very simple way to measure the frequency would be to have a single
timer interrupt at a rate somewhat faster than the expected
frequency (in this case, maybe 10 kHz or so). In the interrupt
handler you can sample all channels and work out if each signal has
changed phase since the last sample for that channel. After so many
interrupts (the count depends on how often you need a reading), a
simple sum will give you the frequency of each channel from the
number of phase changes detected.

Obviously if the frequency is changing rapidly, this technique won't
give very acurate results, as it's essentially averaging the
measurement over a period.

There are many ways of measuring or detecting signal's frequency: if
you give more details of exactly what you're trying to do, other
techniques might be more appropriate.

Brendan.

--- In l..., Mukund Deshmukh wrote:
>
> > > Just a quick question to see if there's anyway of using the inbuilt
> > > timer functionality of a LPC2119 as a counter. Ie to count the
number
> > > pulses (rising or falling edge) from an external source over a given
> > > time frame. Basically we have a pot
>
> I too have a similar problem, but I want measure 4 channel low freq
(about 1
> Khz) signal.
> Any idea how it could be implemented using one timer.
> Earlier we were using C8051F040, which has five timers.
>
> Best Regards,
>
> Mukund Deshmukh.
> Beta Computronics Pvt Ltd
> 10/1, IT Park, Parsodi,
> Nagpur-440022
> Cell - 9422113746
>

Hi,

if you do not need the CAN, using the LPC2136 instead of the LPC2129
or the 2134 instead of the 2119 could resolve your problem.
Nevertheless, the proposals from Karl apply here as well and as
Brendan pointed out CPU load will be very low.
I guess the missing counter inputs were recognized as something to
improve and therefor implemented in newer derivatives.

Another (little crazy) option would be to use a LPC2101 which uses the
same tools and is very code compatible to the LPC21x9, let it do
everything around counting and timers and have it communicate with the
LPC21x9 via SPI or UART once it has the job done.

Bob





> You don't say anything about the signals, e.g. whether they're
> analog or digital, or how stable they are in frequency.
>

I am sorry the message was hurriedly posted.
I am using 4 proximity switches with toothed wheel to determine the speed of
4 shafts. The shaft are driven by 4 different source and not linked.
The output from each proximity can vary from 0 to kHz.

Earlier we were using C8051F040 (CAN not used) with 5 timers. We had
allotted a timer/counter channel to each proximity and we could get speed
accurately.

Now same design is being shifted to LPC2136.
Any idea how this can be achieved with one timer, and yes I would like to
avoid interrupt.
Brendan's idea looks simple, but....?

Best Regards,

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





Mukund,

I would imagine the technique I proposed (sampling each channel at a
rate higher than the maximum expected frequency) would work well in
the setup you describe.

As regrads accuracy, it's the same as using a counter, as all I'm
really suggesting is to use software rather than hardware to do the
counting. The rate at which you read the counters is up to you in
each case.

In terms of performance, I'd be surprised if you used up more than 2
or 3% of the CPU to do the actual counting (assuming 10s of MHz as a
clock rate for the processor). The counting can be done in a single
timer interrupt, without interfering with anything else going on.

It mightn't be that pretty, but there's a lot to be said for
simplicity....

Brendan

--- In l..., Mukund Deshmukh
wrote:
>
> > You don't say anything about the signals, e.g. whether they're
> > analog or digital, or how stable they are in frequency.
> >
>
> I am sorry the message was hurriedly posted.
> I am using 4 proximity switches with toothed wheel to determine
the speed of
> 4 shafts. The shaft are driven by 4 different source and not
linked.
> The output from each proximity can vary from 0 to kHz.
>
> Earlier we were using C8051F040 (CAN not used) with 5 timers. We
had
> allotted a timer/counter channel to each proximity and we could
get speed
> accurately.
>
> Now same design is being shifted to LPC2136.
> Any idea how this can be achieved with one timer, and yes I would
like to
> avoid interrupt.
> Brendan's idea looks simple, but....?
>
> Best Regards,
>
> Mukund Deshmukh.
> Beta Computronics Pvt Ltd
> 10/1, IT Park, Parsodi,
> Nagpur-440022
> Cell - 9422113746
>





>In terms of performance, I'd be surprised if you used up more than 2
>or 3% of the CPU to do the actual counting (assuming 10s of MHz as a
>clock rate for the processor). The counting can be done in a single
>timer interrupt, without interfering with anything else going on.

Well, my plan was to use the timer at 0.9 mil sec (max freq being 1Khz), and
average out the reading at 100 mil sec, so that I don't over flow the 8 bit
counter.
I will also count rising and falling edges so that accuracy would be still
better.
I can not increase 100 mil sec reading period, as the value is used in
another control loop
for controlling speed of variable freq. drive.

In short the application is 4 channel variable ratio electronic gear box.
Best Regards,

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





--- In l..., Mukund Deshmukh
wrote:
> Well, my plan was to use the timer at 0.9 mil sec (max freq being
1Khz), and
> average out the reading at 100 mil sec, so that I don't over flow
the 8 bit
> counter.

Why not use 32-bit counters?

If you use a 5 kHz timer, you can easily count both phase changes,
with plenty of safety margin for a maximum 1 kHz signal.

Every 500 interrupts (i.e. 100 ms) the frequency of each channel is
then:

F = (c / 2 * 10) or F = c * 5

where F is frequency in Hz, c is count of phase changes in the last
100 ms period.

Brendan