2 // Set pin 13 to output
5 // Stop reception of interrupts
6 noInterrupts(); //cli();
8 // Set PB1 to be an output (Pin9 Arduino UNO)
11 // Clear Timer/Counter Control Registers
16 // Set non-inverting mode - Table 15-3 (page 108)
17 TCCR1A |= (1 << COM1A1);
19 // Set Fast-PWM Mode (Mode 14) - Table 15-5 (page 109)
20 TCCR1A |= (1 << WGM11);
21 TCCR1B |= (1 << WGM12);
22 TCCR1B |= (1 << WGM13);
24 // Clear Timer 1 Counter
27 // Set PWM frequency/top value - Output PWM 10kHz
28 ICR1 = 199; // Fclk_io / (Fout * Prescaler) - 1
29 OCR1A = 100; // Output OC1A will be ON for [OCR1A/(ICR1+1)]% of the time -> 100/(199+1) = 50%
31 // Enable compare match interrupt
32 TIMSK1 |= (1 << OCIE1A);
33 TIMSK1 |= (1 << TOIE1);
35 // Set prescaler to 8 and starts PWM
36 TCCR1B |= (1 << CS11);
39 interrupts(); //sei();
46 //Timer1 Compare Match Interrupt turns OFF pin 13 (LED)
47 ISR(TIMER1_COMPA_vect) {
48 digitalWrite(13, LOW);
51 //Timer1 Overflow Interrupt turns ON pin 13 (LED)
52 ISR(TIMER1_OVF_vect) {
53 digitalWrite(13, HIGH);