Searching for Algo to compute prescaler&OCR1

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

I guess this questions has been asked zillion of times, sorry for this , but I can't find the right keyword for this info.
Does anyone know an algorithm(or code) to automatically compute the best prescaler and timer value for a CTC timer (8 or 16bits).
For instance, I'd like to set a timer at 1500microsec, what would be the best prescale, and what OCR1 value (this is just an example).

I am writing a c++ class to encapsulate timers, and would like to hide this prescaler stuff from the interface.
Thanks

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

Put "avrcalc" into the search engine and see what transpires (also kavrcalc)

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

I have nothing more to say than the prescaler must be big enough so the count is small enough to fit into the timer registers. It can't be that difficult.

Remember that different AVRs have different timers which may have different prescaler options. You also need the current clock frequency.

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

I made a search on "avrcalc", and found binaries for windows and linux. The linux version does not work on mine, and I get the error:

Warning: Unknown map line : [ ffdf8000-ffe0d000 rwxp 7ffffffea000 00:00 0                              [stack] ]Inconsistency detected by ld.so: dl-open.c: 623: _dl_open: Assertion `_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT' failed!

The windows version works more or less, but the thing is: there is no source code in both.

I did not get good result also with kavrcalc.

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

Quote:

The windows version works more or less, but the thing is: there is no source code in both.

I did not get good result also with kavrcalc.


Quote:

automatically compute the best prescaler and timer value

Well, I guess AVRcalc isn't fully "automatic". And I don't know if there is such a thing as the "best" value--in one app a different prescaler might be more appropriate than in another.

But I have use AVRcalc for years, and it works for me [given the adjustment below].

You tell your AVR frequency, and you tell the desired period, and it tells you how close it can come.

If it is out-of-range, then pick a different prescaler and try again.

I only do it once every few apps anyway. I can easily explore the "reach". What problems are you having?

[re "adjustment": for CTC you need to subtract one from the AVRcalc number as it counts both 0 and TOP.]

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

I finally found kavrcalc here:
[url]
http://www.b9.com//elect/avr/kav...
[/url]
It works fine on windows.
I wanted to make something more generic in a c++ class which does not need human hand to set a new value for a timer.
But it looks unrealistic for now, so I'll change my mind.
Thanks.

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

The problem is that there are so many timer modes and situations that one size does not fit all. CTC is one of the more straightforward but even that has the -1 adjustment needed. And if you use CTC for square-wave frequency, the TOP value will be quite different than for PWM with the same output, and yet again different for an up-down counting mode.

In practice I have a few apps where I calculate on the fly, and indeed range-shift with different prescalers. In nearly all cases it is set-it-and-forget-it.

Once there is understanding about how it all works using pencil and paper, then you have an idea about what makes sense. At that point the tools are quite helpful and faster than re-doing the calcs.

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.