## Selecting a chip for variable frequency

18 posts / 0 new
Author
Message

Hello, I am a newbie here. I would like to be able to create an arbitrary frequency from about 10 to 50kHz with 50% duty cycle, using an inexpensive chip such as an ATtiny. My questions are
1) Do I need a chip with a 16 bit timer?
2) If I use a 16 bit timer, does that mean I have about 65K possible frequencies?
3) Can the ATtiny chips with 8 bit timers also be used in this manner, but with 256 possible frequencies? My understanding is that only a handful of frequencies are possible because only a small number of prescalers are allowed.
4) Are there any 8 pin ATtinys with 16 bit timers?
Thank you very much for the guidance.

What frequency resolution do you require? Whilst a 16 bit timer is preferrable, there are a number of techniques that can be employed such as fractional n and dds. Fractional N is where you alternate between two divisors to get a fractional division. Dds is where you add a divisor to a variable and output the overflow. This takes more cpu time. Why the tiny? Price or size? If you go for a mega328 , you get more of everything

1) That depends on what resolution you want.
2) You have more than that because the timer has multiple pre-scale values.
3) Again, it depends on the resolution you want.
4) Not that I know of. Why do you need an 8 pin device?

Regards,
Steve A.

The Board helps those that help themselves.

Let me start all over.
Hello, I am trying to use the most inexpensive chip possible and I would like to compare the 8 bit timer with the 16 bit timer in terms of fundamental operation (no fractional n and dds please).
a) Do the 8 bit and 16 bit timers operate in fundamentally similar ways - in other words, by setting a prescalar and a timer count?
b) So then what is the granularity of the frequency setting? Is it one part in 256 versus one count in about 65K?
c) Or, is the 16 bit timer fundamentally different in its ability to use timer count? I was told that with an 8 bit timer you can only adjust frequency by changing the value of the prescalar. Is this correct or a misunderstanding?
I hope these questions are more clear. As I said, I am trying to compare the operation of the 8 bit and 16 bit timers.

Maybe an example helps.
You can see the granularity for 16-bit and 8- bit timer.

F_CPU 8 MHz
mode CTC, frequencies 10-50 kHz.
.

## Attachment(s):

There is no fundamental difference between a 8 bit or 16 bit timer. But on the AVR, 16 bit timers invariably come with an input capture unit.

One thing to think of is that the frequency and the timer value has no linear relation ship, so the frequency difference between each timer value step varies, as you can see in the table posted.

On the ATtiny25 you can set the prescaler to 2, 4, 8, .. 16384 and the input frequency up to 64MHz.
So the CTC value can always be in the range 128 .. 255.
This gives you 1% resolution over the whole range.

On the ATtiny261 you can use the same with a 10 bit timer (0.2% resolution).
But the 64MHz PLL can not be derived from an external crystal.
So I fear, the accuracy was not as 0.2% good.

Peter

Last Edited: Fri. May 25, 2012 - 11:01 AM

Of course there is a linear relationship between frequency and timer. However you will not be able to achieve certain frequencies exactly. e.g. 30kHz.

In practice, the 16-bit timer will get you most frequencies 'good enough'.
Even the 8-bit timer will give you 30kHz -2% accuracy.

David.

Quote:

I am trying to use the most inexpensive chip possible

How many zillion are you making, that a few pennies per unit is significant?

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.

Quote:

4) Are there any 8 pin ATtinys with 16 bit timers?

Quote:

4) Not that I know of. Why do you need an 8 pin device?

Quote:

Let me start all over.
Hello, I am trying to use the most inexpensive chip possible

OK, now let >>me<< start over...

What does "8 pin" have to do with "least expensive"?

What does the datasheet for an ATtiny4 say?

Quote:
â€¢ Peripheral Features
â€“ One 16-bit Timer/Counter with Prescaler and Two PWM Channels

What is the cost for said Tiny4?
Quote:
Price (USD) ... \$0.3659

You still haven't said what resolution you need, and the end application. Besides the timer prescaler, you can use CLKPRR and OSCCAL to "feather" the AVR frequency.

If you are going to dedicate an AVR to this task, why have you dismissed DDS?

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.

Visovian wrote:
Maybe an example helps.
You can see the granularity for 16-bit and 8- bit timer.

Visovian, this is incredibly helpful. Thank you for posting this table. Hopefully Atmel is reading this and will publish this kind of information in the future.

Visovian wrote:
mode CTC, frequencies 10-50 kHz.

Do I understand CTC mode always gives 50% duty cycle?

jayjay1974 wrote:
But on the AVR, 16 bit timers invariably come with an input capture unit.

As a newbie, I'm not sure what the implications if this are. For this discussion, I assume we are talking about generating frequencies in hardware, so that normal processing does not cause "glitches" on the output.

Taking this discussion a step farther, are the 8 bit timers and 16 bit timers the same in how to vary the duty cycle? What is the easiest mode to do this in, and is there a mode where frequency and duty cycle can be set independently? Stated another way, can I use one variable like OCR to set frequency and a second variable to set duty cycle? Or does a change in one always affect the other, requiring a calculation each time?

Quote:

Do I understand CTC mode always gives 50% duty cycle?

Yes.
Quote:

Taking this discussion a step farther, are the 8 bit timers and 16 bit timers the same in how to vary the duty cycle?

Yes apart from granularity. For each "step" in duty cycle width on an 8bit timer it is divided into 256 sub-steps on a 16 bit timer.
Quote:

d is there a mode where frequency and duty cycle can be set independently?

Most AVR 16 bit timers have (PWM) modes where the OCRA register sets TOP (the frequency) and OCRB sets the duty ratio.

I don't think you've identified a specific AVR you plan to use yet but taking the mega164/324/644/1284 as an example the 16 entry WGM table shows TOP set by a register (so frequency variable) in modes 4, 8, 9, 10, 11, 12, 14, 15

Quote:
As a newbie, I'm not sure what the implications if this are.
For creating an output it has no relevance.
Quote:
Do I understand CTC mode always gives 50% duty cycle?
Yes.
Quote:
Taking this discussion a step farther, are the 8 bit timers and 16 bit timers the same in how to vary the duty cycle?
For timers that have PWM modes, basically yes (but some timers have more modes than others).
Quote:
is there a mode where frequency and duty cycle can be set independently?
On some timers, yes (this is most, if not all, 16 bit timers).
Quote:
Or does a change in one always affect the other, requiring a calculation each time?
If you change the frequency (by changing TOP), then you will need to adjust the OCRx value proportionally to get the same duty cycle.

Regards,
Steve A.

The Board helps those that help themselves.

Life is a lot easier if you describe what you really want to do.

You can set up hardware timers to produce a frequency on an OCnx pin. This will use no cpu power at all. You set it up and the signal is produced forever.

With CTC mode, you get a 50% duty cycle and a wide range of frequencies.
With PWM modes, you get a range of duty cycles and a reasonable range of frequencies.

You can calculate a CTC mode signal by:

`OCR1A = F_CPU / divN / 2 / frequency - 1;`

If you get an integer, you have a perfect frequency.
In practice, a non integer implies a small percentage error. e.g. OCR1A = 123.4 suggests 0.324% error. (0.4 / 123.4)

Regarding 8-bit or 16-bit timer, you just do the sums. If you can get your desired frequency with 8-bits and divN prescaler, the job's done.

Note that you can do lots with a Tiny4. If you want accuracy you need a crystal somewhere. Otherwise 0.001% means nothing.

David.

clawson wrote:

Most AVR 16 bit timers have (PWM) modes where the OCRA register sets TOP (the frequency) and OCRB sets the duty ratio.

Koshchi wrote:
If you change the frequency (by changing TOP), then you will need to adjust the OCRx value proportionally to get the same duty cycle.

Thanks guys, it helps to understand this relationship.

To david.prentice, thanks for the info on calculations. Judging from the table, the granularity for both timers is better than the accuracy of the internal RC clock, which is fine for me. I have several applications and am just trying to understand how the timers work.

To theusch, 8 pin is a preference, not a requirement. Thanks for pointing out the ATtiny4. I have downloaded some of the datasheets, but not all of them. A 40-80 cent device is fine for most of my consumer-type applications.

jwriter,

I have found that newbies to the micro world have a few common misconceptions about how counter-timers generate square waves. It is important for you to be sure about what the counters CANNOT do and how the DON'T work, so you don't waste time and get frustrated trying to make them do soemthing they cannot do.

#1 - There is no way to directly set the frequency of the output square wave. This has to be calculated from the clock source frequency (typically that of the external crystal clocking the micro) and the way you ultimatley use the counters & prescaler.

#2 - There is no way to directly set the duty cycle of the output square wave either. Again, this depends on the counter's clock source and how you choose to use the counters.

#3 - There is no simple way to get even or smooth Hertz steps of output frequency using the counter scheme found in virtually all microcontrollers.

#4 - The number of unique frequencies and duty cycles which can be generated from a given clock source is surprizingly limited. That is, you can make the same freq and duty cycle in a number of differnt ways, and there are certain frequencies and duty cycles which are impossible to make for a given clock source.

To answer one of your questions directly. The 8-bit and 16-bit counters in the AtMega processors are pretty much the same operationally (except for the obvious difference of # of bits, which therefore dictates the range and resolution of possible outputs). The main difference is that the 16 bit counters have more operational modes than the 8 bit counters. But for simple square wave generation, the extra 16 modes do not offer any significant advantages. In general though, I'd always take the 16 bit counter if I have the choice, because I can make it do everything the 8-bit can do, and more.

To add to that list... another mistake that people often seem to make is to think that a timer can do only one single thing at a time.

With a bit of cleverness it can actually multiple things. You can have one single timer do PWM, provide a master system tick, measure a frequency or dutycycle and generate a pulse train.

Mostly thanks to the output compare units ;)