1
// TIMER/COUNTER 0
// PIN 6(OC0A) = NON-INVERTED [_BV(COM0A1)]
// PIN 5(OC0B) = INVERTED     [_BV(COM0B1) | _BV(COM0B0)]
// PWM MODE    = FAST PWM     [_BV(WGM01) | _BV(WGM00)]
// PRESCALER   = 1            [_BV(CS00)]
TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(COM0B0) | _BV(WGM01) | _BV(WGM00); 
TCCR0B = _BV(CS00); 

// TIMER/COUNTER 1
// PIN 9(OC1A)  = ?????
// PIN 10(OC1B) = ?????
// PWM MODE     = FAST PWM 8BIT[_BV(WGM12) | _BV(WGM10)]
// PRESCALER    = 1            [_BV(CS10)]
TCCR1A = ????? | _BV(WGM10); 
TCCR1B = _BV(WGM12) | _BV(CS10);

// TIMER/COUNTER 2
// PIN 11(OC2A) = NON-INVERTED [_BV(COM2A1)]
// PIN 3(OC2B)  = NON-INVERTED [_BV(COM2B1)]
// PWM MODE     = FAST PWM     [_BV(WGM21) | _BV(WGM20)]
// PRESCALER    = 1            [_BV(CS20)]
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); 
TCCR2B = _BV(CS20);

Hi. My intention of writing the codes is to clarify my doubts regarding arduino timer.

First of all, I intend to have 6 consistent PWM outputs which is 8 bits and runs at 62.5kHz. The confusion here is FAST PWM and PHASE CORRECT PWM. TIMER 1 and 2 is usually presented as PHASE CORRECT PWM. So does changing the PWM mode of TIMER 1 and 2 to FAST PWM enables the PWM to reach a frequency of 62.5kHz (prescaler set as 1)? Is the code that I have written correct? Do take note of the inverted and non-inverted pin mode.

Secondly, as seen from my code above, Timer/Counter 1 is not properly written (?????). I wish to control pin 9 and 10 so that it can invert or non-invert individually. Timer 0 and 2 allows me to set the pins individually (inverted and non-inverted). However from the atmega328p datasheet, Timer 1 control register is weird. How can I set the register so that I can do what I want to do which is to control the pins individually? Timer0,1,2 Control Register A comparison

I truly apologize for any confusion caused by my sentence structure. I am aware I have a poor way of organizing and presenting my idea. And lastly, thank you for taking your time reading these doubt of mine.

  • Just out of curiosity, why have 6 outputs all at the same frequency? Why not just have one or two? – Nick Gammon Apr 15 '17 at 8:40
  • I am working on a DC/DC converter project. Initially I am using 2 outputs only so only Timer0 is used. But now i need to control 2 extra things, thus a total of 4 outputs to be controlled. Reaching to this point, might as well learn how to use all 6 of them :) – iHateUni Apr 15 '17 at 8:51
  • Where did you get your datasheet from, exactly. I have several datasheets for ATmega328P (for 2013 and 2014), but I certainly do not have the same info as you have. The chapter titles themselves look weird. On mine, I have "TCCR1A – Timer/Counter1 Control Register A" and the bits description is complete. It seems new datasheets edited by microchip have lost quite some information. Try to find youself an older datasheet from Atmel. – jfpoilpret Apr 15 '17 at 9:40
  • And for your information, the bits COM1xx are the same as for COM0xx and COM2xx, in terms of their position in the register. – jfpoilpret Apr 15 '17 at 9:40
  • The datasheet I referred to is atmega328p 2016. I just checked an older version and you are right! It shows me 'COM1A1|COM1A0|COM1B1|COM1B0'. Now down to one question. Will be more than willing to set your answer as answered if you can answer the first question together with the second question – iHateUni Apr 15 '17 at 9:45
0

As mentioned in my comment, it seems latest ATmega328P datasheet from Microchip have lost some information. I use the datasheet from 2014 and it is complete:

enter image description here

Regarding PWM frequency of Timer1 and Timer2, as you spotted yourself, those timers support Phase Correct PWM in addition to Fast PWM.

To reach a 62.5KHz PWM frequency, you have to apply the formulas available in the datahseet.

For Fast PWM

enter image description here

i.e. with 16MHz MCU frequency and prescaler set to 1:

fPWM = 16000000/(1 . (1 + 255)) = 62.5 KHz

which is what you want.

For Phase Correct PWM

enter image description here

i.e. with 16MHz MCU frequency and prescaler set to 1:

fPWM = 16000000/(2 . 1 . 255) = 31.4 KHz

which is the maximum PWM frequency you can get here.

Conclusion

To get 62.5KHz PWM frequency with Timer 1 and Timer2, you shall absolutely use FastPWM, not Phase Correct PWM.

For Timer 1, that means setting WGM12 and WGM10 to 1.

For Timer2, that means setting WGM21 and WGM20 to 1.

Hence your code snippet is correct towards what yu want to achieve.

0

for an arduino, 16Mhz / 62.5Khz = 256 -> prescaler = 1 and single sloped pwm, with its top at 255.

the number of timer modes meeting those conditions are limited.

if you move outside of arduino, a lot more of choices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.