Go Down

Topic: Varying the pwm frequency for timer 0 or timer 2? (Read 146624 times) previous topic - next topic


I posted a spreadsheet here that has that information: http://spreadsheets.google.com/pub?key=rtHw_R6eVL140KS9_G8GPkA&gid=0


Oct 14, 2009, 04:54 pm Last Edit: Oct 14, 2009, 07:49 pm by selfonlypath Reason: 1
Very helpful document mem.

Do you know why timer2 on mega board has no OC2C output aka PWM T2C since all 16bit-timers on mega have three registers: OCRnA, OCRnB and OCRnC ?

Do you know when timer5 will be supported by IDE ?


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.


Oct 15, 2009, 07:35 am Last Edit: Oct 15, 2009, 07:45 am by selfonlypath Reason: 1
hello mem,

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

Code: [Select]
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 :-/


hello again mem,

Ok just made some test on timer1 which as you wisely mention on your doc can only have 2 pins outputs (11 and 12).

Well, it is a bit more tricky when reading atmega1280 spec because pin13 is SHARED by timer0 and timer1 so it can either be related to OCOA or OC1C.

If one make sure to only use timer1 and NOT use timer0, then timer1 becomes a full 16bit timer as timer3, timer4 and timer5 with three PW outputs.

Don't know what is best on how to describe this on your doc but with the above restriction, pin13 can be either TOA (as you have it now) and T1C :P


There is more info on atemag1280 datasheet (chapter 19. Ouput Modulator (OCM1COA) giving details how pin 13 on mega board is shared by timer0 and timer1.

As stated before & this can be seen on the schematic, if one fully disable timer0 then timer1 recovers full potential of OC1A, OC1B & OC1C and three pins outputs. If you need to use timer0, then only OC1A & OC1B outputs hence only two pins are controlled by timer1.


Timer0 is used by millis and delay so it can't be disabled without disturbing these core functions.


Oct 17, 2009, 10:32 am Last Edit: Oct 17, 2009, 10:33 am by selfonlypath Reason: 1
OK the idea here is to have full potentiality of timer1 in mega board hence have control of OC1A, OC1B & OC1C. I could be wrong be feel then to preventing timer0 from writing to OC0A which is shared by OC1C (same pin 13 on mega) will leave intact millis() and delays(). For example, this code will block COM0A1 & COMA0 so partially disable timer0:
Code: [Select]
TCCR0A = TCCR0A & 0x3F;


Looks promising, have you tried it and does the PWM work on timer1 without disturbing millis timing?


Oct 17, 2009, 11:58 am Last Edit: Oct 17, 2009, 12:15 pm by selfonlypath Reason: 1
There is no need to try because millis() does not use OC0A which happens to be connected to pin 13. Any project can use pin13 without corrupting millis() having in mind that pin13 happens to be the unique pin with a led inside mega board. My code just disable any output on pin13 coming from OC0A of timer0 so pin13 will be only modulated by OC1C of timer1.


Oct 17, 2009, 12:28 pm Last Edit: Oct 17, 2009, 12:28 pm by mem Reason: 1
There is no need to try because ...

It's been my experience that what one expects to happen based on interpreting a datasheet and what actually happens is not always as expected.  ;)


Oct 17, 2009, 07:42 pm Last Edit: Oct 17, 2009, 07:51 pm by selfonlypath Reason: 1
Ok, just done the test with this code and for me it works fine
Code: [Select]
#include <util/delay_basic.h>

int outputPsuB = 12;  // Timer1-B
int outputPsuC = 13;  // Timer1-C

unsigned long time;

void setup()
// PSU outputs via timer1
 pinMode(outputPsuB, OUTPUT);  // select Pin as ch-B
 pinMode(outputPsuC, OUTPUT);  // select Pin as ch-C

 TCCR1A = B00101001; // Phase and frequency correct PWM change at OCRA
 TCCR1B = B10101;  // System clock / 1024 from prescaler
 OCR1A = 10000; // 0.78125 Hz
 OCR1B = 7500; // 75% PWM
 OCR1C = 500; // 5% PWM
 TCCR0A = TCCR0A & 0x3F; // disable OC0A from timer0

void loop()
 Serial.print("Time: ");
 time = millis();
 //prints time since program started
 // wait a second so as not to send massive amounts of data

The terminal gives these values and I have correct PWM on pin 12 & 13:
Code: [Select]
Time: 4
Time: 1012
Time: 2023
Time: 3034
Time: 4045
Time: 5056
Time: 6067
Time: 7078
Time: 8090
Time: 9101
Time: 10113
Time: 11125
Time: 12137

Now if I remove the timer1 & timer0 specific initialization in setup(), I get same Time code log computed by millis() except there will be no PWM modulating pin 12 & pin 13.

Let me know if this fits what you wanted to test !


On second thought about OC1C output of timer1 on mega, it works fine but there might be a little problem because the shared pin13 is wired with a LED on the board. This will somehow alter or degrade the bandwith of fast PW as well as when using OC0A of timer0 so the problem i snot only with timer1. Worthwhile noting the presence of this led will also degrade the inner Atmega1280 Output Compare Modulator (see section 19)  :-/

It would have been best arduino designer connect the mega board LED to pin not connected to any timer as it is done with duamilanove.



Sorry if this is a dupe, but I can't seem to find this info anywhere else. Here are the PWM pin <-> TCCR register values to change PWM freuquencies on the Arduino Mega. These values were measured for each pin by going through the different TCCRXB values (from X=0 to 5) and measuring the pin output on an oscilloscope.

As above, the code used to set the prescaler in each case was

TCCRXB = TCCRXB & 0b11111000 | <setting>

e.g. to set the PWM freq on pin 5 to 31.25 kHz:
TCCR3B = TCCR3B 0b11111000 | 0x01

Frequencies are truncated at the decimal point. I have to admit, I didn't check every single setting, but just checked the pattern and then filled in the blanks from previous pwm divisor data. However, at least two setting values were checked for each pin, so this data should be reliable, barring a strange deviation from the divisors used in previous arduinos.

PIN     TIMER     Freq. at setting = 0x01   0x02   0x03   0x04  

2       TCCR3B                       31250  3906   488    122
3       TCCR3B                       31250  3906   488    122
4       TCCR0B                       62500  7812   976    244
5       TCCR3B                       31250  3906   488    122
6       TCCR4B                       31250  3906   488    122
7       TCCR4B                       31250  3906   488    122
8       TCCR4B                       31250  3906   488    122
9       TCCR2B                       31250  3906   976    488
10      TCCR2B                       31250  3906   976    488    
11      TCCR1B                       31250  3906   488    122
12      TCCR1B                       31250  3906   488    122
13      TCCR0B                       62500  7812   976    244
45      TCCR5B                       31250  3906   488    122  
46      TCCR5B                       31250  3906   488    122


Hope this is useful.

-- Mark


Gah, I see this information has indeed already been posted! My apologies. Believe it or not, I didn't notice that this thread had two pages so I missed mem's post above. It would have saved me an hour of checking if I had seen it. Hopefully the redundancy means that there is more chance of people finding the answer.

- Mark

Go Up