Nano Timer2 OCR2A & B could not work separately

This is my first time using the Forum.
I have been trying for 2 days to get both works simultaneously but with independent compare match value (Timer2 OCR2A & B) and control its own output accordingly . Each OCR2A and OCR2B has different match compare value. Also each output pin (11 ,3) (OCR2A & B) would be active ( PWM) output for a duration and then OFF for a different duration. I just could not make this works. :tired_face: :disappointed:

Please help me.

The following is my sketch.

#include <Wire.h>

const int ocr2top = 255; //Mode 1 default top xFF.
float i = 0;
float j = 0;

//----------------------------------------------
void setup()
{
// OC2A arduino pin11 (PB3) OC2B arduino pin3 (PD3)

Serial.begin(9600);

// mode1 1 phasecorrection
TCCR2A = (1 << COM2A1)+(1 << COM2B1) + (0 << WGM21) + (1 << WGM20);
// WGM20 set -> Mode 1, phasecorrect xFF top
// COM2A1=1 and COM2B0 = 0 means clear OC2A n B compare match.

TCCR2B = (1 << CS22) + (1 << CS21) + (0 << CS20) + (0 << WGM22); // prescaler

//pinMode(3, OUTPUT); //OCR2B
//pinMode(11, OUTPUT); //OCR2A

delay ( 100);
}
//==================== LOOP ===========================
void loop()
{
// -------------------OCR2A--------------
if (i < 400)
{
i = i + 1;
}
else {
i = 0;
}
if (( i > 0 ) && (i < 4))
{
fun_OCR2A(140);
}
if ((i > 210) && (i < 214))
{
fun_FWDoff();
}
//--------------OCR2B------------
if (j < 600)
{
j = j + 1;
}
else {
j = 0;
}

if (( j > 0 ) && (j < 9 ))
{
fun_OCR2B(204);
}
else if ((j > 300) && (j < 308))
{
fun_REVoff();
}
}
//========= functions ==========================

//--------------------------------------------------------
void fun_FWDoff()
{
OCR2A = 0 ;
digitalWrite (11, LOW);
Serial.print(" OCR2A off= ");
Serial.println(OCR2A);
}
//------------------------------------

void fun_OCR2A(int val)
{
pinMode(11, OUTPUT); //OCR2A
OCR2A = val;
Serial.print(" OCR2A = ");
Serial.println(OCR2A);
}
//--------------------------------------------------------

void fun_REVoff()
{
OCR2B = 0 ;
digitalWrite (3, LOW);
Serial.print(" OCR2B off= ");
Serial.println(OCR2B);
}

//------------------------------------

void fun_OCR2B(int val)
{
pinMode(3, OUTPUT);
OCR2B = val;
Serial.print(" OCR2B = ");
Serial.println(OCR2B);
}

Please edit your post to add code tags (select code and use the "</>" editor button).

Please explain what you expect to happen, and what happens instead.

"could not make this works" tells us nothing.

It is difficult to tell from your Q, but is the symptom that after some stuff, the PWM part stops working?

If so, it could be in code like this:

Calling digitalWrite() on a timer pin disables the PWM control of the pin and it would need reconfiguring to work again.

Here's the source code of digitalWrite() where it checks to turn off PWM:
ArduinoCore-avr/cores/arduino/wiring_digital.c at 2cebe625833afcdb76cc941ccf90e5f5fefc27a9 ยท arduino/ArduinoCore-avr ยท GitHub

1 Like

You are using TC2 in Mode-1 to generate Phase Correct PWM signal.
1. Excute a simple program to see PWM signal at DPin-11 (OC2A) and DPin-3 (OC2B) simultaneously.

void setup()
{
    Serial.begin(9600);
    TCCR2A = 0x00;  //always reset 
    TCCR2B = 0x00; //always reset
    TCCR2A = (1<<COM2A1)|((1<<COM2B1) | (0<<WGM21)|(1<<WGM20); //Mode-1
    TCCR2B = (1<<CS22)|(1<<CS21)|(0<<CS20)|(0<<WGM22); //Mode-1 and prescale 256
    pinMode(11, OUTPUT);  //Ch-A
    pinMode(3, OUTPUT);     //Ch-B
    //-------------------------------
    OCR2A = 127;    //50% duty cycle
    OCR2B =  64;    //about 25% duty cycle
}

void loop()
{

}

2. Upload the above sketch and check that you have about 123 Hz (= 16MHz/(256*510) PWM signals at both DPin-11 and Dpin-3 with different duty cycles.

3. If you want to control the duty cycle of Ch-A manually, then connect a 5K pot with A0-pin of UNO and include the following codes in the loop() function. Slowly turn the pot and check that the brightness of a LED connected at DPin-11 changes (modulates).

OCR2A = map(analogRead(A0), 0, 1023, 0, 250);
delay (2000); //test interval

THANKS FOR ADVISE. I will repose and will separate the multiple issues with single issue/question. That will be better. The issues could be multiple.

thanks for your advise. I had tested with a sketch similar to your and both ports works with pwm ( continuous) and I scoped both and confirmed.

I will repose my question differently so my questions/issues will be address desperately.

Please see my other post only focus on one issue only for the time being.

Nano- TIMER2 OCR2A pwm in burst mode cannot control burst duration

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.