Question.8
What will be the duty cycle of the PWM signal generated on pin 9 and pin 10 of Arduino UNO,
after executing the following code?
Note: Arduino clock frequency is 16MHz.
Code:
void setup() {
pinMode(9, OUTPUT); // OC1A (PWM output)
pinMode(10, OUTPUT); // OC1B (PWM OUTPUT)
// Configure Timer1 for Fast PWM, Non-Inverting, ICR1 as TOP
TCCR1A = (1 << WGM11);
TCCR1B = (1 << WGM12) | (1 << WGM13);
// Non-Inverting PWM on OC1A (Pin 9)and OC1B (Pin 11).
TCCR1A |= (1 << COM1A1) | (1 << COM1B1);
// Set Prescaler = 1 (No prescaling, 16MHz clock)
TCCR1B |= (1 << CS10);
// Set TOP value (ICR1) to control PWM frequency
ICR1 = 15999; // PWM Frequency = 16MHz / (1 * (15999 + 1)) ≈ 1kHz
OCR1A = 3999; // pin 9
OCR1B = 1999; // pin 10
}
void loop() {
}