Using 20MHz ATmega as a pulse generator...

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

I'm using an ATmega168xx @ 20MHz for an embedded ap, and was wondering if it would be suitable to use as a pulse generator?

I have spare pins - my basic requirements are to generate pulse trains with a mark between 20uS & 100mS (1:5000 20uS steps to 1mS, 1mS steps thereafter) and a space between 1mS and 10s (1:10000 1mS steps to 100mS, 10mS steps to 1S and 100mS steps thereafter).

The CPU is not doing a whole bunch - mostly just menus & LCD stuff, so a lot of spare welly there...

What approach would people suggest?

Thanks

Nicko

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

You could use the 16bit timer for this

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

Or just write a bunch of wait loops. Its not elegant, but it would save you the time needed to decode the data sheet realted to teh timer..

--
"Why am I so soft in the middle when the rest of my life is so hard?"
-Paul Simon

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

Quote:

mostly just menus & LCD stuff,

Quote:

Or just write a bunch of wait loops

So what happens to your button scanning in that case?

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

If he wants to do both at the same time, then of course, he would have to use interrupts. As with a timer.

Speed reading has its drawbacks.

--
"Why am I so soft in the middle when the rest of my life is so hard?"
-Paul Simon

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

meslomp wrote:
You could use the 16bit timer for this

I'd thought about this, but the duty cycle varies hugely and over a big time range - I'd obviously like to do all this through PWM (better to use the HW if you can) but with 20uS mark & > 1S space I would have thought that bit twiddling may be the way...

As its not at all vital that the frequency/duty cycle is precise I suspect that using the 16 bit counter then flicking bits may be the best way.

If there is another technique, I'd like to know about it...

Cheers

Nicko

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

Quote:

I'd obviously like to do all this through PWM (better to use the HW if you can) but with 20uS mark & > 1S space I would have thought that bit twiddling may be the way...

Well, I'd do neither/both -- I'd use free-running and compare-match.

The first thing I want to know is the accuracy and precision requirements. If you want to be dead-on on both the pulse width and the pause, such as digital setting for calibration, then it is certainly doable with the fast AVR clock but may require a bit more re-ranging.

If it just has to be "close" like with a dial setting, then we can do things like use the prescaler and not care if 100us is actually 100.432us and it gets pretty simple.

Quote:

As its not at all vital that the frequency/duty cycle is precise I suspect that using the 16 bit counter then flicking bits may be the best way.

Let's see how well it can be done without a lot of re-ranging. 20MHz is a 50ns clock period. The desired is a 20us "tick", or 400 AVR clocks per tick. /1024 is 51.2us per tick; too coarse. /256 is 12.8us per tick; fine enough gut not an "even" number. But let's start with that.

How far can we reach with that tick rate and a 16-bit timer? 12.8us x 65536 is 838.xxx ms--more than enough reach for the desired 100ms pulse width. In fact, it is way more so let's increase our resolution:

/64 gives 3.2us resolution, and a reach of 209.xxx ms -- we have a winner.

Now. let's consider the off time--1 ms. to 10000ms. You know what I'd do? Use a second 8-bit timer ticking at 1ms, and count the ticks to start the first timer. But if you don't have an 8-bit timer free, then you could re-configure timer1 and use that.

If you relax the rules just a bit, the 16-bit timer can reach 3+ seconds at /1024, and then we can do some really clever things with compare match A and B. If you want, I could outline that--it ends up to be about a dozen lines of code in 3 ISRs.

Lee

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

theusch wrote:
Quote:

If you relax the rules just a bit, the 16-bit timer can reach 3+ seconds at /1024, and then we can do some really clever things with compare match A and B. If you want, I could outline that--it ends up to be about a dozen lines of code in 3 ISRs.

Thanks for the detailed response - it's much appreciated.

An outline of your approach would also be very helpful.

Thanks

Nicko

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

Hello,
it is some lines more of code only and you can have an output compare generating 20µs pulses each second. And I mean each second and not a second later. It is easy to do 999.980µs with the precision of you oscillator.
You don't have to switch the prescaler. Just set it to the resolution you want to have. If the 16bit don't reach to the next event set them without turning the output compare pin. Do this at hte next time you will reach your final time.