Aquaticus

Projects for geeks

Frequency

11 Aug 2009

General rule for choosing frequency for PWM signal: higher is better. Use as high frequency as you can. This rule is not valid only if you need low frequency wave e.g. for blinking LED.

Maximum frequency is often limited by the external components, basically for elements like MOSFETs number of switches in 1 second is limited.

For example, if you use popular L298 to control motor speed, you can use up to 40kHz. For Infineon BTN7960 maximum frequency is 25kHz.

Prescaler

Timers can be clocked directly by the system clock or by the prescaler. Prescaler is internal module of the microcontroller that can divide system clock frequency which is later used to clock the timer.

For ATmega16/32 timer0 and timer1 share the same prescaler module and can divide frequency to: F/1, F/8, F/64, F/256 or F/1024, where F is system clock frequency. Timer2 has its own separate prescaler module that gives frequencies: F/8, F/32, F/64, F/128, F/256 and F/1024.

If prescaler divider is 1, timer is clocked directly by the system clock. If none of prescaler settings is chosen, timer do not run and thus PWM wave is not generated. You must set prescaler in order to use PWM.

Phase correct PWM mode

Frequency in this mode can be calculated by the following equation:

f PWM – resulting frequency of PWM,
f clock – system clock, this is frequency of the crystal oscillator or internal RC oscillator, e.g. 1MHz, 4Mhz, 16MHz,
N – prescaler divider, 1, 8, 64, 256, 1024 for timer0 and timer1 or 1, 8, 32, 64, 128, 256, and f/1024 for timer2,
TOP – maximum OCRx value (controls duty cycle). For 8 bit timers it is always 255, for 16 timer it depends on resolution, 8bit it is 255, for 9bit 511, for 10bits 1023.

Here is the table of PWM frequency for 1MHz, 8MHz and 16Mhz (timer1 prescaler values shown). All frequencies in Hz.

System clock Prescaler PWM freqency 8bit PWM frequency 9bit PWM frequency 10bit
16,000,000 1 31,372.55 15,655.58 7,820.14
16,000,000 8 3,921.57 1,956.95 977.52
16,000,000 64 490.20 244.62 122.19
16,000,000 256 122.55 61.15 30.55
16,000,000 1024 30.64 15.29 7.64
8,000,000 1 15,686.27 7,827.79 3,910.07
8,000,000 8 1,960.78 978.47 488.76
8,000,000 64 245.10 122.31 61.09
8,000,000 256 61.27 30.58 15.27
8,000,000 1024 15.32 7.64 3.82
1,000,000 1 1,960.78 978.47 488.76
1,000,000 8 245.10 122.31 61.09
1,000,000 64 30.64 15.29 7.64
1,000,000 256 7.66 3.82 1.91
1,000,000 1024 1.91 0.96 0.48

As you can see from the table, maximum ever possible PWM wave frequency in this mode for ATmega16/32 is 31.4 kHz (absolute maximum system clock for ATmega16/32 is 16MHz). The other interesting thing is that for higher duty cycle resolution (10bits) maximum frequency is lower than for low 8bit resolution. That means if you need highest frequency possible use 8bit modes.

Fast PWM mode

Frequency in this mode can be calculated by the following equation:

f PWM – resulting frequency of PWM,
f clock – system clock, this is frequency of the crystal oscillator or internal RC oscillator, e.g. 1MHz, 4Mhz, 16MHz,
N – prescaler divider, 1, 8, 64, 256, 1024 for timer0 and timer1 or 1, 8, 32, 64, 128, 256, and f/1024 for timer2,
TOP – maximum OCRx value (controls duty cycle). For 8 bit timers it is always 255, for 16 timer it depends on resolution, 8bit it is 255, for 9bit 511, for 10bits 1023.

Here are the table of PWM frequency for 1MHz, 8MHz and 16MHz (timer1 prescaler values shown).

System clock Prescaler PWM frequency 8bit PWM frequency 9bit PWM frequency 10bit
16,000,000 1 62,500.00 31,250.00 15,625.00
16,000,000 8 7,812.50 3,906.25 1,953.13
16,000,000 64 976.56 488.28 244.14
16,000,000 256 244.14 122.07 61.04
16,000,000 1024 61.04 30.52 15.26
8,000,000 1 31,250.00 15,625.00 7,812.50
8,000,000 8 3,906.25 1,953.13 976.56
8,000,000 64 488.28 244.14 122.07
8,000,000 256 122.07 61.04 30.52
8,000,000 1024 30.52 15.26 7.63
1,000,000 1 3,906.25 1,953.13 976.56
1,000,000 8 488.28 244.14 122.07
1,000,000 64 61.04 30.52 15.26
1,000,000 256 15.26 7.63 3.81
1,000,000 1024 3.81 1.91 0.95

Maximum ever possible PWM wave frequency in all modes for ATmega16/32 is 62.5kHz (Fast PWM is the fastest mode). Comparing to Phase Correct mode, frequency is 2 times higher.

Phase and Frequency Correct PWM mode

The most interesting thing in this mode is you can change frequency of the signal to almost any value you need. In that case we can not say about “PWM wave frequency” but rather maximum and minimum possible PWM frequency.

Maximum frequency can be calculated using the same equation as for phase correct mode.

f PWM – resulting frequency of PWM,
f clock – system clock, this is frequency of the crystal oscillator or internal RC oscillator, e.g. 1MHz, 4Mhz, 16MHz,
N – prescaler divider, 1, 8, 64, 256, 1024 for timer0 and timer1 or 1, 8, 32, 64, 128, 256, and 1024 for timer2,
TOP – maximum ICRx or OCRx value. For 8 bit timers it is always 255, for 16 timer it depends on resolution, 8bit it is 255, for 9bit 511, for 10bits 1023.

Frequency is changed by writing a value to ICRx register. You can also use mode when OCx do the same thing, but using ICRx is more handy.
The frequency can be controlled from F/(2*N*1) to F/(2*N*TOP), that gives us for 10bit resolution:

System clock Prescaler Max frequency Min frequency
16000000 1 4,000,000.00 7,820.14
16000000 8 500,000.00 977.52
16000000 64 62,500.00 122.19
16000000 256 15,625.00 30.55
16000000 1024 3,906.25 7.64
8000000 1 2,000,000.00 3,910.07
8000000 8 250,000.00 488.76
8000000 64 31,250.00 61.09
8000000 256 7,812.50 15.27
8000000 1024 1,953.13 3.82
1000000 1 250,000.00 488.76
1000000 8 31,250.00 61.09
1000000 64 3,906.25 7.64
1000000 256 976.56 1.91
1000000 1024 244.14 0.48

For 8 bit and 9 bit resolution maximum frequency is the same but minimum frequency is higher, so the frequency range is tighter. For example, for 16MHz prescaler 1 minimum frequency is 31372Hz, while for 10 bit resolution it is 7820Hz.

Human factor

Most people can hear sounds of frequency up to 20kHz. When PWM signal controls a device (e.g. H-bridge) using frequency less than 20kHz, humans may hear annoying sound thanks to low quality capacitors or coils. Recall all those broken fluorescent lamps that generate low frequency noise.

It is better (when possible) to use frequency greater than 20kHz, people would not hear anything even if capacitor became sound generator.

If the source of light blinks more then 50 times a second, human eye can not detect the change. This effect is utilized in computer monitors and TVs. If you want to control brightness of the light source you must use frequency higher than 50Hz.

Crystal resonator

You can guess what all those funny, hard to remember crystals frequencies are for, e.g. 8.192 MHz. One group of crystals give 0% error for serial communication. e.g. 14.7456 MHz. The other group of values which is more suitable for PWM, gives you nice looking integer values when divided by prescaler numbers. Below you have a table with the popular values.

System clock Prescaler 8 Prescaler 32 Prescaler 64 Prescaler 128 Prescaler 256 Prescaler 1024
1,024,000 128,000 32,000 16,000 8,000 4,000 1,000
3,072,000 384,000 96,000 48,000 24,000 12,000 3,000
4,096,000 512,000 128,000 64,000 32,000 16,000 4,000
6,400,000 800,000 200,000 100,000 50,000 25,000 6,250
8,192,000 1,024,000 256,000 128,000 64,000 32,000 8,000
10,240,000 1,280,000 320,000 160,000 80,000 40,000 10,000

For example, 8.192 MHz crystal with 32 prescaler in Fast PWM mode where wave frequency for 8 bit gives you:

Using crystal with integer frequency value never gives you PWM wave with integer frequency. In most cases it does not matter whether frequency is 1 kHz or 1.1 kHz, so you can use any crystal resonator, but if you need something like 1kHz, 2kHz you have to use one of those strange crystals.