Pages: [1]   Go Down
Author Topic: Can't set to Fast PWM TOP=OCRA mode  (Read 999 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Trying to have Fast PWM mode when TOP == ORCA.


Works fine with OCA toggle(measuring correct frequency), but doesn't work in inverting or non-inverting mode.


In non-inverting mode I just got steady 1 on output and inverting mode gives logical 0. I was expecting to see the frequency F = 16*10^6 / (1024 * 127) = 123Hz.



Code:
  cli();

  // Fast PWM Mode
  TCCR2A = 0;
  TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A0) | _BV(COM2A1); // Inverting mode fails
//TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A1); // Non-Inverting mode fails
//TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A0); // Toggle mode OK

  // Prescaler set to 1024 (p.162)
  TCCR2B = 0;
  TCCR2B |= _BV(CS20) | _BV(CS21) | _BV(CS22) | _BV(WGM22);

  // Enable interrupt on counter match
  TIMSK2 |= _BV(OCIE2A);

  OCR2A = 127;

  sei();

MCU=Atmega328.
« Last Edit: November 21, 2012, 02:54:18 am by acid » Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...doesn't work in non-inverting mode.

  TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A0) | _BV(COM2A1); // Inverting mode fails

Which mode?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Both, inverting and non inverting mode does not work. It was typo in comments.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Apparently I was too terse... Which mode do you want to get working?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fast PWM when TOP=OCRA. I am trying to get desired frequency on OCA pin.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Fast PWM when TOP=OCRA. I am trying to get desired frequency on OCA pin.

You stated that was working in Compare Output Mode = Toggle so I guess we're done.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but I need inverting mode and I want to understand why those modes are not working according to documentation.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Excellent.  The goal is get inverting output mode working on timer 2 at a frequency of 123 Hz on an ATmega328P processor.  Off to the datasheet...
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Are you expecting an inverted output on the OC2A pin?

If yes then you will have to adjust your expectations.  In Waveform Generation Mode = 7 (Fast PWM, TOP = OCRA) you essentially give up the A channel because the OCRA register is used to specify TOP.  The B channel (OC2B pin) will work the way you expect.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for explanation, but something is confusing for me...

1. Why COM2A1:0 = 1 (Toggle OC2A Mode) is giving me frequency? In this mode WGM22:0 = 7 and OCR2A also busy with holding TOP value.
2. Documentation says:
Quote
In non-inverting Compare Output mode, the Output Compare (OC2x) is cleared on the compare match
between TCNT2 and OCR2x, and set at BOTTOM. In inverting Compare Output mode, the out-put is set on compare match and cleared at BOTTOM.
This means both, OCR2A and OCR2B can hold TOP values.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1. Why COM2A1:0 = 1 (Toggle OC2A Mode) is giving me frequency? In this mode WGM22:0 = 7 and OCR2A also busy with holding TOP value.

The counter increments from zero (BOTTOM) to the value in the OCR2A register (TOP).  When the counter reaches the value in the OCR2A register (TOP), the counter is reset to zero (BOTTOM).

When the Compare Output Mode is Toggle, the output pin is toggled when the counter value equals the value in the OCR2A register (which also happens to be TOP).

The counter reaches OCR2A (TOP), the pin is toggled, the counter value is reset.

Quote
2. Documentation says:
Quote
In non-inverting Compare Output mode, the Output Compare (OC2x) is cleared on the compare match
between TCNT2 and OCR2x, and set at BOTTOM. In inverting Compare Output mode, the out-put is set on compare match and cleared at BOTTOM.
This means both, OCR2A and OCR2B can hold TOP values.

No.  "TOP" is the label used for the value at which the counter is reset.  It has nothing to do with the output.  Only the OCR2A register is used for the TOP value.

OCR2A and OCR2B registers are used for the value at which the "output action" is performed.  This has nothing to do with when the counter is reset.
Logged

Pages: [1]   Go Up
Jump to: