Hello everyone!
I'd like to generate a PWM signal with a duty cycle of 30%. When the following code was flashed to an UNO board, pin 11 (PB3) remained high and never did the good old oscillation. I'm sure I screw something up, I just can't seem to figure out what.
void setup()
{
Serial.begin(19200);
/* MY SETTINGS:
╔════════╦═══════╦═══════╦═══╦═══╦═══════╦══════╦══════╦══════╗
║ ║ FOC2A ║ FOC2B ║ - ║ - ║ WGM22 ║ CS22 ║ CS21 ║ CS20 ║
╠════════╬═══════╬═══════╬═══╬═══╬═══════╬══════╬══════╬══════╣
║ TCCR2B ║ 0 ║ 0 ║ - ║ - ║ 1 ║ 1 ║ 0 ║ 1 ║
╚════════╩═══════╩═══════╩═══╩═══╩═══════╩══════╩══════╩══════╝
╔════════╦════════╦════════╦════════╦════════╦═══╦═══╦═══════╦═══════╗
║ ║ COM2A1 ║ COM2A0 ║ COM2B1 ║ COM2B0 ║ - ║ - ║ WGM21 ║ WGM20 ║
╠════════╬════════╬════════╬════════╬════════╬═══╬═══╬═══════╬═══════╣
║ TCCR2A ║ 1 ║ 0 ║ 0 ║ 0 ║ - ║ - ║ 1 ║ 1 ║
╚════════╩════════╩════════╩════════╩════════╩═══╩═══╩═══════╩═══════╝
Timer Mode: 7 (WGM2:0 = 7)
Duty cycle: 30%
TOP: 102
*/
pinMode(LED_BUILTIN, OUTPUT);
// set OC2A as OUTPUT
DDRB |= (1 << PB3);; // OC2A pin - pin No.3
// clear settings
TCCR2A = 0;
TCCR2B = 0;
Serial.print("TCCR2A: ");
Serial.println(TCCR2A, BIN);
Serial.print("TCCR2B: ");
Serial.println(TCCR2B, BIN);
// set TOP to 102 -> generate 25KHz
OCR2A = 102;
// 30% duty cycle
OCR2B = 34;
// set Timer mode 7: goes from BOTTOM to TOP
// WARNING: WGM22 is part of TCCR2B
TCCR2A |= (1 << WGM21) | (1 << WGM20);
// clear OC2A on compare match, set OC2A at BOTTOM
// non-inverting FAST PWM mode
TCCR2A |= (1 << COM2A1);
Serial.print("TCCR2A: ");
Serial.println(TCCR2A, BIN); // prints 10000011
// set prescaler to 0 -> running at 16MHz - start timer
TCCR2B = (1 << CS22) | (0 << CS21) | (1 << CS20);
// WGM2:0 define mode 7
TCCR2B |= (1 << WGM22);
Serial.print("TCCR2B: ");
Serial.println(TCCR2B, BIN); //prints 00001101
}
So the question is: what am I missing to generate the PWM signal? Thank you in advance!