Timer1 won't generate PWM signal on Pin9

Hello,
I am using Leonardo so I base on ATmega32U4 datasheet.

On the datasheet is information when I set Timer1 on FastPWM mode with WGM13:0 = 14 or 15 and COM1A1:0 = 1 output pin OC1A (pin 9) should toggle on compare match.
In theory everything is very easy and should works but it doesn't.

and my code:

void setup()
{
  pinMode(9, OUTPUT);
  TCCR1A = 0;        // Reset entire TCCR1A to 0
  TCCR1B = 0;       // Reset entire TCCR1B to 0
  TCNT1 = 0x0;      // set Timer1 start value
  TCCR1A |= B01000010;  // COM1A1:0 = 1 (enable D9) and WGM11:0 = 2
  TCCR1B |= B00011111;  // WGM13:2 = 3 and clock external rising edge
  ICR1 = 30;              // set TOP value
  OCR1A = 10;             // set OCR1A value
}

void loop()
{
  //nothing
}

Pin9 should toggle but is constantly low.
When I change mode to 15 everything works but I need TOP different than OCR1A value because I plan use also OCR1B with higher value.

What am I missing :question:

Do you understand that in this mode you need an external clock, for example from another timer? Do you use it?

Try getting the code to work with an internal clock selection, then implement the external clock.

This works on an Uno, with COM1A1:0 = 2

The wording of Table 14-3 Compare Output Mode, Fast PWM is confusing.


void setup()
{
  pinMode(9, OUTPUT);
  TCCR1A = 0;        // Reset entire TCCR1A to 0
  TCCR1B = 0;       // Reset entire TCCR1B to 0
  TCNT1 = 0x0;      // set Timer1 start value
  TCCR1A |= B10000010;  // COM1A1:0 = 2 (enable D9) and WGM11:0 = 2
  TCCR1B |= B00011011;  // WGM13:2 = 3 and clk/64
  ICR1 = 30;              // set TOP value
  OCR1A = 10;             // set OCR1A value
}

void loop()
{
  //nothing
}

because I plan use also OCR1B with higher value

For what?

1 Like

This may be a long shot, but you might try setting ICR1 and OCR1A before TCCR1A & B.

void setup()
{
  pinMode(9, OUTPUT);
  TCCR1A = 0;        // Reset entire TCCR1A to 0
  TCCR1B = 0;       // Reset entire TCCR1B to 0
  TCNT1 = 0x0;      // set Timer1 start value
  ICR1 = 30;              // set TOP value
  OCR1A = 10;             // set OCR1A value
  TCCR1A |= B10000010;  // COM1A1:0 = 2 (enable D9) and WGM11:0 = 2
  TCCR1B |= B00011011;  // WGM13:2 = 3 and clk/64
}

Yeah
Since setting the prescaler starts the timer - it is generally a good idea to set TCCR1B as last thing, after the timer is fully set up.

Makes no difference on the Uno R3. Both work as expected. However, the OP's original code (modified for internal clock) still has no output on D9 with that change. It is not clear to me why.

void setup()
{
  pinMode(9, OUTPUT);
  TCCR1A = 0;        // Reset entire TCCR1A to 0
  TCCR1B = 0;       // Reset entire TCCR1B to 0
  TCNT1 = 0x0;      // set Timer1 start value
  ICR1 = 30;              // set TOP value
  OCR1A = 10;             // set OCR1A 
  TCCR1A = B01000010;  // COM1A1:0 = 1 (enable D9) and WGM11:0 = 2
  TCCR1B = B00011011;  // WGM13:2 = 3 and clk/64
}

void loop(){}

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