Counter at 20 Mhz

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Everybody,

I have to measure pulse about 20 Mhz. On average, I have about 200 000 pulses in 1 second, but these pulses are not sent regularly. In reality, the pulses are sent per packet, and therefore has a frequency higher than 200 kHz (I think about 20 Mhz, may be more).

I developped a solution with an Arduino Nano (ATmega328), and its 16-bit counter. But I have a problem, the meter can not count correctly. In fact, I have about 4 times less pulses than expected.

I saw that the Arduino Leonardo has a high-speed counter (ATmega32U4 64MHz).

- Do you think this counter will be good enough for my problem?
- I'm trying to understand the datasheet of ATmega32U4 (10 bits), and the counter seems harder to use than the ATmega328 (16 bits).

I use Arduino for rapid prototyping, but after I develop my own card.

Can you help me with my questions?

Thank you a lot

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I developped a solution with an Arduino Nano (ATmega328)

It would be a good trick to make a 16MHz CPU resolve a 20MHz signal.

Anyway looking at the 32U4 it would appear that 64MHz timer 4 is specifically for 3 phase PWM output generation with dead-time (that is 3 phase motor control). I don't see anything there suggesting it has input capture facility and if you try to do it in software then you are back to being limited by the CPU speed and the fact that several opcode cycles will be required.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you a lot!

What would be the best solution for my problem?

Kind regards

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

What would be the best solution for my problem?


Well, "best" is always subjective. What accuracy and resolution is needed? What is, in fact, the max frequency? How wide are the pulses?

If full resolution isn't needed, you could run the signal into a divide-by-n counter chip. Then run that output into the AVR's Tn pin.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or, if you need full resolution, run the signal to an external counter that the microcontroller just gates then reads out.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or use a CPU that is up to the job.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Or use a CPU that is up to the job.


Hmmm--I'm not an Xmega person, yet. I cannot find in the "family" or "device" manuals the max frequency of timer-as-counter input. I do see that timers can be cascaded to get a 32-bit count, which would work OK for this.

In particular, can the Hi-Res Extension be used to clock the timer from the fast peripheral clock to count at 20MHz when the Xmega's main clock is, say, slower.

[edit] AVR1311 states (implies) that Hi-Res is only for output, and counts 4 for each system clock.

And,

Quote:
Atmel AVR1617: Frequency Measurement with
Atmel AVR XMEGA Family Devices
...
• Measure frequencies up to 16MHz using a 32MHz clkCPU
...

All that said, wouldn't a $0.25 /8 external prescaler let a normal AVR do the job?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Hmmm--I'm not an Xmega person, yet.

Me neither and I never intend to be. I was simply thinking that if you can get a 70-150MHz Cortex for $2 then why beat yourself up trying to analyze a 20MHz signal with a 16-20MHz (or even 32MHz) micro if you can get one going many times that speed for the same money that surely could easily handle the job. The fact that it probably has five or six 32bit timers into the bargain is just icing on the cake.

And, yes, you can probably add some logic onto an AVR to get to a solution but is really as cost effective?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm curious about what is 20MHz in the real world--that is useful measuring. I can't think of much, short of e.g. monitoring traffic to/from an SD card hooked to a fast micro (can't think of the practical benefit for that), or USB monitor, or ???

I've got it--a simple tachometer for a crotch-rocket.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, I have a doubt about this speed of counter4. This work only to control a 3 phase PWM output or I can use this to counter time? I didn't undestand how I can get 64 MHz if my clock is 16 MHz...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some AVR parts have a PLL (phase locked loop). This is used to multiply the oscillator frequency. In many (most) of the ARM cortex m0/3/4 parts, these have a pll for the processor clock. With these you put a 8 to 12 MHz crystal and the frequency is multiplied internally to the chip.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank You! But what happen if I use timer 4 of ATmega32u4 to generate a interrupt in a frequency greater than 16 MHz? I think that it's impossible to the cpu to process, so the feature of this timer is mainly to generate a pwm of higher resolution?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, the feature is for higher resolution pwm.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bouskap wrote:

Thank you a lot!

What would be the best solution for my problem?

 

Most Micros sample their external Counter clock inputs, and thus limit to SysCLK/2, SysCLK/4  or lower

 

The simplest solution is an external prescaler, as a simple Divider - use the lowest divide you can still count to,

to keep the best system precision. Choose one with a 50% output duty cycle.

common examples would be a tap on HC4060/HC4020/HC4040/HC6323

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's a really good tip!Very thanks Who-me!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suppose you can use an external counter IC or an external shift register.  An external shift register clocked faster than the AVR could 'present' the AVR 8 bits at a time.  Even better with a counter than a shift register: If you have an external 8-bit counter, and you don't mind using up a whole 8-bit port, you can count pulses way higher than the AVR's clock.

 

Back to the shift register: Does anyone know the maximum clock rate of SCLK when using SPI as a slave device?  I can kind of imagine that the shift register that the SPI uses is entirely driven by outside SCLK without any coupling to the CPU clock frequency.  So, if the AVR is an SPI slave, can it have data crammed into it faster than the AVR's clock?  For instance, on a 16 mhz chip, can you receive at 64 mhz and thus the AVR receives 1 byte of SPI data at a rate faster than once every 8 clocks?  If that is the case, if you have a 64mhz PWM channel, the AVR can poke it's own SS line and sample an external signal.  Maybe not reliable at 64 mhz, but maybe you can just do 20 Mhz or do.  If every 6 clocks the SPI interface gives you 8 bits of data, you can just barely do 20 mhz sample on a 16 mhz MCU.

Last Edited: Mon. Mar 16, 2015 - 10:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Probably about half the cpu clock rate. There'll be a spec in the datasheet methinks. Regardless of how fast you can clock it, you still need to feed the data in and pull the data out.