PWM syntax question

Greetings, I am new to setting up PWM without analogwrite and need some help. I have been reading the help and found:

Compare Match Output A Mode bits (COMnA): these enable/disable/invert output A
Compare Match Output B Mode bits (COMnB): these enable/disable/invert output B

Setting the COM2A bits and COM2B bits to 10 provides non-inverted PWM for outputs A and B.

So is this code inverted?

pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(CS22);
  OCR2A = 180;
  OCR2B = 50;

How would I set one or the other? What does each of the above commands do in practice?

Please use code tags.

Read this before posting a programming question


http:/www.gammon.com.au/timers

zippo01:
Greetings, I am new to setting up PWM without analogwrite and need some help. I have been reading the help and found:

Compare Match Output A Mode bits (COMnA): these enable/disable/invert output A
Compare Match Output B Mode bits (COMnB): these enable/disable/invert output B

Setting the COM2A bits and COM2B bits to 10 provides non-inverted PWM for outputs A and B.

So is this code inverted?

pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS22);
OCR2A = 180;
OCR2B = 50;

How would I set one or the other? What does each of the above commands do in practice?

That code sets 4 bits in the 2A register. Go to the datasheet and look up what they do.

I normally just use hex or binary constants since you still need to go to the datasheet to workout what
bits do what and that long line of ORed constants is clumsy

MarkT:
That code sets 4 bits in the 2A register.

I would say the code sets all 8 bits. It sets 4 bits to one and 4 bits to zero.

Whandall:
I would say the code sets all 8 bits. It sets 4 bits to one and 4 bits to zero.

Perhaps I agree. "bit setting" would more accurately reflect an operation using "|=".

Ok so I have been doing a lot of reading, just checking to make sure I understand.

TCCR2A is one of the timers available.

// I found this in the Atmel datasheet
COMnA1 COMnA0
COMnB1 COMnB0
COMnC1 COMnC0

1 0 Clear OCnA/OCnB/OCnC on compare match, set OCnA/OCnB/OCnC at BOTTOM (non-inverting mode)
1 1 Set OCnA/OCnB/OCnC on compare match, clear OCnA/OCnB/OCnC at BOTTOM (inverting mode)

So _BV(COM2A1) | _BV(COM2B1) sets this PWM to inverting. _BV(COM2A1) only would be non-inverting

_BV(WGM21) | _BV(WGM20); are what set fast PWM mode?

TCCR2B = _BV(CS22); sets the clock?

What assoiated pinMode(3, OUTPUT); pinMode(11, OUTPUT); When I look at the data sheet for various pins I only see (OC3B/INT4)PE4 I would use OC3B?

Thanks for all the help. I'm really trying to learn this, and I don't have anyone local to me that can help!

See reply #1. Code tags, buddy.

Cheat sheet for timer 2:

Download attachment for larger version.

Sorry will do from now on.