Quick explanation - PWM on ATmega328P

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, I have successfully used TC2 - timer/counter2 as a pwm generator with the following settings:

DDRB |= (1 << DDB3); // pin 11 on arduino (OC2A)

TCCR2A |= (1 << COM2A1) | (1 << WGM20); // phase correct, clear on compare match
TCCR2B |= (1 << CS22);  // 64 prescaler

I use OCR2A to change period etc.

 

 

So I thought the TC0 - timer/counter0 could be used in the same manner, swapping the digits of course:

DDRD |= (1 << DDD6); // pin 6 on arduino (OC0A)
TCCR0A |= (1 << COM0A1)| (1 << WGM00);
TCCR0B |= (1 << CS02);

However, it doesn't seem to work. I use OCR0A in the same way to change the period.

 

What am I missing? Just because they have similar names/enumerations doesn't mean they will work the same but, please give me some clue (:

 

Cheers.

sol i sinne - brun inne

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tickstart wrote:
What am I missing? Just because they have similar names/enumerations doesn't mean they will work the same but, please give me some clue

What does the datasheet say about the Modes Of Operation for the two timers?  Does Mode 1 have the same characteristics on both timers?  What about the COM and CS bits?

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.

Last Edited: Sat. Nov 5, 2016 - 04:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, thanks for the quick reply!

Hm, from what I can gather they should be identical..?

 

For the COMs

 

Mhyehh... Don't know what I'm missing..

sol i sinne - brun inne

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't use |= 

 

I believe Arduino may already configure timer 0 so you are adding bits not overriding its previous settings . 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't use it as a general rule or just in this case? I will see if it makes a difference, thanks!

sol i sinne - brun inne

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When first configuring a peripheral always use = not |=

 

If you later want to add some additional bits then you can use |=

 

The reason may be exactly what's happening here, if I'm right and you are inheriting bits the Arduino code has already set. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried it but it didn't make a difference :( But sound advice nontheless

sol i sinne - brun inne

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

try prescaler 1
and send us your code

I don't need a Handkerchief; i have no more tear to shed anymore - Solid Snake

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry but.. A problem I encountered yesterday, the datasheet specified a prescale register bit for the PWMs called CA21 or 22 or 11 etc. However, there is apparently no such thing when compiling. The "CS22" and "CS02" do not exist in the datasheet but in AVR studio 4 I'm using. Weird. I don't know what it means for the code. But it works in one case so hey, why change it.

sol i sinne - brun inne

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tickstart wrote:
the datasheet specified a prescale register bit for the PWMs called CA21 or 22 or 11 etc.

???  Which datasheet is this?  Quote or snapshot this piece, with enough context to find it.

 

In one sentence above you talk of CS.  In the next you talk of CA.  Which is it?  Tell the datasheet version that has no CSxx in it.

 

 

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.atmel.com/Images/Atme...

 

 

Same with TC2. TC1 has all the CS-bits I'm used to.

sol i sinne - brun inne

Last Edited: Sun. Nov 6, 2016 - 02:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LOL -- Atmel goes from b&w "plain straightforward datasheets, to the new and improved color layout.  And sees fit to introduce typos.  (And you couldn't figure out to use CS?  Strange.)  No wonder they couldn't stay in business.

 

It looked OK in 2009, but you kids always need to have the new stuff.

 

Does the Datasheet Revision History in your version have a note "Changed CS to CA just to trip up certain users"?

 

 

 

 

 

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

-- All of the older "Mega88" data sheet versions, through M, have CS.

-- All of the Mega88P versions, through D, have CS.

-- The PB datasheets have CS.

 

So now we are on to the PA...

 

 

Now it is getting weird:

ATmega48A/PA/88A/PA/168A/PA/328/P [DATASHEET] Atmel-8271J-AVR- ATmega-Datasheet_11/2015

 

...has CS.  So you have a datasheet, >>only<< for '328P, that is different than the omnibus version which has CS?

 

 

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is indeed weird--starting with why there is a separate datasheet in the first place.

 

I downloaded your version.

Tickstart wrote:
The "CS22" and "CS02" do not exist in the datasheet

Let's look at that a bit.  What is immediately above the table you pasted?

And above that?

And in the register summary?

 

All those are is a simple number anyway.

 

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow--that is a "brand-new" datasheet made for this one model of the family.  Yet they introduced what you found?

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There they are yeah, didn't look hard enough.. I thought, "hm, well, the 'A' is indeed very clode to the 'S' on the keyboard..." :D So I just assumed that's what they meant.

sol i sinne - brun inne