Hi,
I'm trying to use the Diecimila Arduino board to pulse solenoid valves, at a frequency of 125Hz. I can get pins 9 and 10 to work fine using the following:
TCCR1A=0x00;
TCCR1B=0x12;
ICR1=0x1F40;
However I have no idea how to change the pwm frequency at pins 3, 5, 6 and 11. I understand these don't use timer 1, but timer0 and timer 2 instead, and that these don't have the ICR1 input capture unit, which I believe the above code was using to vary the pwm frequency.
So is it possible to change the pwm frequncy of the other timers? And if so how?
Thanks in advance, and any help will be much appreciated!
I brought up this subject as a feature request a while back. A few suggestions were made on how to do this. Not sure if it has what you need. But it may help:
Thanks for the reply - I had actually already found that thread, and like you found the document on controlling timers far beyond my programming ability!
If someone has achieved this (low frequency pwm on a tleast 4 channels) I'd be very grateful if they could let me know how!
It was wrong, I fixed it all. The last two sections are divided by two for the Arduino. I also left in the decimals because they might factor into someone's results.
I was figuring out how to smoothly dim/fade some neon sign segments and they needed something close to 100Hz, so I had calculated most of the data and checked it with an oscilloscope already.
The PWM range increased from 0-255 to 0-1023 in the cases I tested. Is this true for all values?
delay() and millis() are no longer behaving as expected after changing the TCCR0B settings. Do you know of any other side effects of changing these timers?
Pins 5 and 6:
Setting Divisor Frequency
0x03 64 976.5625
TCCR0B = TCCR0B & 0b11111000 | ;
Pins 11 and 3:
Setting Divisor Frequency
0x04 64 488.28125
TCCR2B = TCCR2B & 0b11111000 | ;
Does timer2 really run half the frequency of timer0 with the same prescaler? I coudn't find anything obvious to account for such a discrepancy in the AVR datasheet, although it matches observations. What am I missing?
westfw, start with the first section on page 136 of the atmega168 datasheet. I am not able to fully make sense of it all, but it looks like timers 0 and 1 source from different clock inputs (one 8bit vs one 16bit), so even with the shared prescaler logic and settings, they have different frequency outputs; timer 2 is more independent. I wonder what the design considerations were, but I think they were going for maximum variation between the three, while sharing a lot of logic.
macegr: I'm afraid that the frequencies in your post for pins 3, 9, 10 and 11 are a little of. The correct formula is f = clock/(510 N), where N is the divisor (prescaler) and clock = 16,000,000. That gives f = 490.196 Hz for the default setting. But close enough. See the ATmega168 datasheet.
westfw: The reason is that Timer0 runs in the FastPWM mode to feed the millis() function. That gives a frequency f = clock / (256 N), N = 64 by default. In FastPWM, the counter is always incrementing until it reaches 256 when it overflows to 0. The frequency f = 976.563 that is close enough to generate milliseconds.
Timers 1 and 2 run in the Phase correct PWM, which is apperently better for motors. Then the counter increments until it reaches the top 255 and then starts decrementing until it reaches 0. And then again incrementing... Thus for each cycle it has to go through 255 + 255 = 510 steps, hence the frequency clock/ (510 N), N = 64. That gives you ~~ 490Hz.
If you change TCCR0B, it affects millis() and delay(). They will count time faster or slower than normal if you change the TCCR0B settings. Below is the adjustment factor to maintain consistent behavior of these functions:
Default: delay(1000) or 1000 millis() ~ 1 second
0x01: delay(64000) or 64000 millis() ~ 1 second
0x02: delay(8000) or 8000 millis() ~ 1 second
0x03: is the default
0x04: delay(250) or 250 millis() ~ 1 second
0x05: delay(62) or 62 millis() ~ 1 second
(Or 63 if you need to round up. The number is actually 62.5)
Also, the default settings for the other timers are:
TCCR1B: 0x03
TCCR2B: 0x04
There may be other side effects from changing TCCR0B. For example my project would not properly run with TCCR0B set to 0x02 or 0x01. But it worked fine at 0x03 and higher. YMMV
Hi, selfonlypath, I am not aware of how the Arduino designers chose which resources to expose.
But looking at the source code, I think PWM A and B on Timer5 may actually work. Try doing an analogWrite on pins 45 and 46. Let me know if it works and I will update the spreadsheet.
Just done the test with mega board and version 15 running on my Macintosh.
I confirm timer5 works perfect, here is my code where I generate 799,92Hz freq, PWM of 75% on OC5B and PWM of 1% on OC5C with clock system, phase & freq correct PWM
int outputPsuB = 45; // Timer5-B
int outputPsuC = 44; // Timer5-C
void setup()
{
// PSU outputs via timer5
pinMode(outputPsuB, OUTPUT); // select Pin as ch-B
pinMode(outputPsuC, OUTPUT); // select Pin as ch-C
TCCR5A = B00101001; // Phase and frequency correct PWM change at OCRA
TCCR5B = B10001; // System clock
OCR5A = 10000; // 799,92Hz
OCR5B = 7500; // 75%
OCR5C = 100; // 1%
}
i've tried pre-scaling to diminish frequency and works perfect.
For me it looks 16-bits timer3, timer4 & timer5 are fully compatible with version 15 on Macintosh.
I'll look later on timer1 issue you've raised on your doc :-/