Question.9
Amy has generated a PWM signal to control the LED brightness. What will be the PWM signal frequency after executing the following code?
Given: CPU clock frequency is 16MHz
Code
#define LED_PIN 3
void setup() {
pinMode(LED_PIN, OUTPUT);
// Configure Timer2 for Fast PWM, Non-Inverting Mode with 1024 Prescaler, clear OC2B on compare match
TCCR2A = (1 << WGM21) | (1 << WGM20) | (1 << COM2B1);
TCCR2B = (1 << WGM22) | (1 << CS22) | (1 << CS21) | (1 << CS20);
OCR2A = 77; // Set the TOP value for the timer (frequency control)
}
void loop() {
// Gradually increasing PWM
for (int duty = 0; duty <= 77; duty++) {
OCR2B = duty; // Set the current duty cycle
delay(10);
}
// Gradually decreasing PWM
for (int duty = 77; duty >= 0; duty--) {
OCR2B = duty; // Set the current duty cycle
delay(10);
}
}