]> kolegite.com Git - vmks.git/blob
e6bb3efa55096e12e153b616956fd852933a88f1
[vmks.git] /
1 #define BTN_PIN 2
2
3 volatile uint8_t timer_en = 0;
4
5 void button_pressed_ISR(void);
6
7 void setup()
8 {
9 // Stop reception of interrupts
10 noInterrupts(); //cli();
11
12 pinMode(BTN_PIN, INPUT_PULLUP);
13 attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed_ISR, FALLING);
14
15 // Set PB1 to be an output (Pin9 Arduino UNO)
16 DDRB |= (1 << PB1);
17
18 // Clear Timer/Counter Control Registers
19 TCCR1A = 0;
20 TCCR1B = 0;
21 TIMSK1 = 0;
22
23 // Set non-inverting mode - Table 15-3 (page 108)
24 TCCR1A |= (1 << COM1A1);
25
26 // Set Fast-PWM Mode (Mode 14) - Table 15-5 (page 109)
27 TCCR1A |= (1 << WGM11);
28 TCCR1B |= (1 << WGM12);
29 TCCR1B |= (1 << WGM13);
30
31 // Clear Timer 1 Counter
32 TCNT1 = 0;
33
34 // Set PWM frequency/top value - Output PWM 1Hz
35 ICR1 = 15625;
36 OCR1A = 10000;
37
38 // Enable interrupts
39 interrupts();
40 }
41
42 void loop() {
43 // some useless code here
44 }
45
46 void button_pressed_ISR(void)
47 {
48 if (timer_en)
49 {
50 timer_en = 0;
51 // Disable the timer by selecting "none" as the clock source
52 TCCR1B &= ~((1 << CS11) | (1 << CS12) | (1 << CS10));
53 }
54 else
55 {
56 timer_en = 1;
57 // Enable the timer
58 TCCR1B |= (1 << CS12) | (1 << CS10);
59 TCCR1B &= ~(1 << CS11);
60 }
61 }