]> kolegite.com Git - vmks.git/blob
a592e20cfdf94e0c390870ccde8c878c22091e52
[vmks.git] /
1 void setup() {
2 // Set pin 13 to output
3 pinMode(13, OUTPUT);
4
5 // Stop reception of interrupts
6 noInterrupts(); //cli();
7
8 // Set PB1 to be an output (Pin9 Arduino UNO)
9 DDRB |= (1 << PB1);
10
11 // Clear Timer/Counter Control Registers
12 TCCR1A = 0;
13 TCCR1B = 0;
14 TIMSK1 = 0;
15
16 // Set non-inverting mode - Table 15-3 (page 108)
17 TCCR1A |= (1 << COM1A1);
18
19 // Set Fast-PWM Mode (Mode 14) - Table 15-5 (page 109)
20 TCCR1A |= (1 << WGM11);
21 TCCR1B |= (1 << WGM12);
22 TCCR1B |= (1 << WGM13);
23
24 // Clear Timer 1 Counter
25 TCNT1 = 0;
26
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%
30
31 // Enable compare match interrupt
32 TIMSK1 |= (1 << OCIE1A);
33 TIMSK1 |= (1 << TOIE1);
34
35 // Set prescaler to 8 and starts PWM
36 TCCR1B |= (1 << CS11);
37
38 // Enables interrupts
39 interrupts(); //sei();
40 }
41
42 void loop() {
43 // Empty
44 }
45
46 //Timer1 Compare Match Interrupt turns OFF pin 13 (LED)
47 ISR(TIMER1_COMPA_vect) {
48 digitalWrite(13, LOW);
49 }
50
51 //Timer1 Overflow Interrupt turns ON pin 13 (LED)
52 ISR(TIMER1_OVF_vect) {
53 digitalWrite(13, HIGH);
54 }