Troubles modulating a pulse generated by CTC

Hello;
I have some troubles with my code: it produces Random delays on my pulse...
Any comments are welcome, cauz' I have absolutely no idea about what's going on...
I tried a lot of different way to time the modulation but I can't have a correct modulation.

It seems than my way to manage Timer2 is pretty ugly.
It seems than my way to time modulation is correct.

Thanks for helping!
Cyriaque

const byte LED_IR = 11;  // Timer 2 "A" output: OC2A

unsigned long MyTime = 0;
int onandoff=0;

void setup() {

  pinMode (LED_IR, OUTPUT);
  
  // set up Timer 2
  TCCR2A = _BV (COM2A0) | _BV(WGM21);  // CTC, toggle OC2A on Compare Match
  TCCR2B = _BV (CS20);   // No prescaler
  OCR2A =  209;          // compare A register value (210 * clock speed)
                                //  = 13.125 nS , so frequency is 1 / (2 * 13.125) = 38095

  Serial.begin(9600);   
}  // end of setup

void loop() 
{ 
      TCCR2A = _BV (COM2A1)  | _BV(WGM21); //FIGER
      onandoff = 1; 
      Modulate(850);
      for (int i=0;i<25;i++)Go(275);
      delay(2000);
}
      

void Modulate(int temps) { 
      if ( (unsigned long) micros() > (MyTime + temps) )
      {    
        MyTime = (unsigned long) micros();
        if ( onandoff )
        {
        TCCR2A = _BV (COM2A0) | _BV(WGM21);  // CTC, toggle OC2A on Compare Match
        onandoff = 0;  
        }
        else
        {
        TCCR2A = _BV (COM2A1)  | _BV(WGM21); //FIGER
        onandoff = 1;   
        }
      }
}

I have some code here which modulates 38 KHz signals:

Plus I believe the IR Remote library does that as well.

Your advices are very helpfull, and the link you gave me is helping me much.

Thank you :slight_smile:

Moreover;
I can't use the fast PWM mode with the Arduino Pin 11 , it work with the Arduino Pin 3.
Hopefully, I think I can do it with Pin 3 but I do not understand why t doesn't work; following those 3 steps from the
"working on Pin 3 code":
Change the OUTPUT Pin setting: pinMode (11, OUTPUT);
Change the TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2A1);
Change my wire from Pin3 to Pin11.

I do not mind there is anything else to do, would you have any other information in mind?

Thank you much for evrything you did for me anyway!!

Post the whole sketch so I can be sure we are talking about the same thing.

Well, trying to figure out what's going on I found this in the doc:

Table 18-6. Compare Output Mode, Fast PWM Mode(1)
COM2B1 COM2B0 Description
0 0 Normal port operation, OC2B disconnected.
0 1 [b][i][u]Reserved[/u][/i][/b]
1 0
Clear OC2B on Compare Match, set OC2B at BOTTOM,
(non-inverting mode).
1 1
Set OC2B on Compare Match, clear OC2B at BOTTOM,
(inverting mode).

So I guess this is my answer!!!

I was trying to toggle but it is not allowed (reserved)

You quoted PWM mode. You are talking about CTC which is not PWM mode.

The PWM modes toggle on and off.

CTC is not a PWM mode per se, but it can generate a 50% duty cycle square wave on a match to OCR2A. The frequency of the signal will be 1/2 the prescaled clock rate since it is toggling the output pin each time a CTC match occurs. This only works on the OC2A pin apparently. The datasheet implies that it will work on either/both pins in the tables, but the description of CTC mode for Timer2 seems to indicate that it can only generate a waveform on OC2A (Pin11).

EDIT: This is odd, since it seems to be the opposite of the OP's symptoms. Looks like the OP had his bits wrong when trying pin 11. I haven't tested yet, but it looks like either pin 11 or pin 3 should be usable.

Cyriaque:
Moreover;
I can't use the fast PWM mode with the Arduino Pin 11 , it work with the Arduino Pin 3.
Hopefully, I think I can do it with Pin 3 but I do not understand why t doesn't work; following those 3 steps from the
"working on Pin 3 code":
Change the OUTPUT Pin setting: pinMode (11, OUTPUT);
Change the TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2A1);
Change my wire from Pin3 to Pin11.

I do not mind there is anything else to do, would you have any other information in mind?

Thank you much for evrything you did for me anyway!!

COM2A1 should not be set, COM2A0 should to enable the toggle. You seem to have it set so that it will clear the pin (make it low) on a match. Maybe this is the problem?
You should have this instead I think:

TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2A0);

That's true, there is a mistake into the code i did post, sorry about it!
Anyway; here is the point:
this code produce a signal on pin 11 but not on pin 3;
see the Table 18-6. in the 328p doc, wich restrict the use of OC2B

#define CntValue 16000000 / 560 / 128
void setup()
{
  pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  TCCR2B = 0;                // Stop timer2
  TCNT2 = 0;                 // Clear timer2 counter
  TCCR2B = _BV(WGM22);       // Fast PWM mode, TOP = OCR2A; Prescaler = No Clock
  TCCR2A = _BV(WGM21) | _BV(WGM20) | _BV(COM2A0) | _BV(COM2B0);
  OCR2A = CntValue;          // Set timer2 TOP value
  TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS20);       // Enable timer2, divide-by-128 prescale
}

void loop() 
{ 
// listen the hp
}

I did test this code right now, arduino uno.

This is for PWM mode.

In Compare Output Mode,
there is an odd thing :
WGM22 = 0: Normal Port Operation, OC2A Disconnected.
WGM22 = 1: Toggle OC2A on Compare Match.
You cannot activate 0C2A if WGM22 isn't set, but you can set OC2B to be toggling in both conditions.

This forbide a few uses for both OC2B and OC2A.

Table 18-6 is for fast PWM mode, not CTC mode. Tables 18-2 and 18-5 are for CTC mode (actually non-PWM). Everything I posted is assuming that you want to use CTC mode as your subject line suggests.

Well, my conclusion is simple, Next time I'll read evrything :stuck_out_tongue:
Thanks!