Timers 0 and 2 on the 328 (duemilanove)

I’m trying to get to grips with some of the detail of timers but have a problem.

Here’s a (working) example using Timer2 that generates a 10uS positive pulse at 10kHz on OC2B.

//Generate a 10uS +ve going pulse at 10kHz

#define OC2B 3 //we'll use Timer 2 / pin 3

void setup()
{
  pinMode (OC2B, OUTPUT);
  
  //clear the counter
  TCNT2 = 0;
  
  //we'll use Clear OC2B on Compare Match with Mode7 and a prescaler of 8
  TCCR2A = (1 << COM2B1) | (1 << WGM21) | (1 << WGM20);
  TCCR2B = (1 << CS21) | (1 << WGM22);
  
  OCR2A = 199; //100uS
  OCR2B = 19; //10uS
}

void loop()
{
}

So I “thought” that this would also work fine transposed to the other pin available for timer2 i.e. OC2A by making the following changes. i.e. use pin 11 instead of pin 3 and use the COM2A1 bit instead of the COM2B1 bit in TCCR2A.

//Generate a 10uS +ve going pulse at 10kHz

#define OC2A 11 //we'll use Timer 2 / pin 11

void setup()
{
  pinMode (OC2A, OUTPUT);
  
  //clear the counter
  TCNT2 = 0;
  
  //we'll use Clear OC2A on Compare Match with Mode7 and a prescaler of 8
  TCCR2A = (1 << COM2A1) | (1 << WGM21) | (1 << WGM20);
  TCCR2B = (1 << CS21) | (1 << WGM22);
  
  OCR2A = 199; //100uS
  OCR2B = 19; //10uS
}

void loop()
{
}

But it does NOT produce any waveform on pin 11.

I’ve also tried transposing this to Timer0 and it works fine on pin OC0B but not on OC0A.

What am I doing wrong - OR - is my assumption just wrong that both pins on a given timer are created equal.

Thanks

  OCR2A = 199; //100uS
  OCR2B = 19; //10uS

When you tried to enable a pulse from OCR2A did you remember to use the mode that uses ICR2 for TOP? You can't use OCR2A for both TOP and the output compare.

I don't think Timer 2 has any such mode. Otherwise I agree with johnwasser that you can't do it like that ... I don't think.

[quote author=Nick Gammon date=1424635576 link=msg=2106812]I don't think Timer 2 has any such mode. Otherwise I agree with johnwasser that you can't do it like that ... I don't think.

http://www.gammon.com.au/timers[/quote]
You are right, as usual. Timer2 can't both count off 100 uS intervals (TOP=OCR2A=199) and time 10 uS pulses (OCR2A=19). The only other choice for TOP is 255.
One work-around I can think of is to enable the OCR2A interrupt. Then when the interrupt happens at 19 clocks you can set Timer2 forward to 75. Then you will hit TOP (255) after 180 more clocks. 19+180=199=100uS

Thanks both

@johnwasser - your first comment put me on track and after a datasheet re-read it made it clear why my second set of code doesn't/can't work.

Just to be clear about your second comment in response to Nick.

johnwasser:
You are right, as usual. Timer2 can't both count off 100 uS intervals (TOP=OCR2A=199) and time 10 uS pulses (OCR2A=19). The only other choice for TOP is 255.

I assume you mean ONLY with respect to OC2A because of course it CAN do exactly this on OC2B as I've shown in my first set of code. I'm sure this is what you meant but just thought I'd clarify.

Anyway - thanks again.

yaafm:
I assume you mean ONLY with respect to OC2A because of course it CAN do exactly this on OC2B as I've shown in my first set of code. I'm sure this is what you meant but just thought I'd clarify.

That assumption is correct. You can use OCR2B for output compare even if OCR2A is used for TOP.

Thanks