-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskM1T1p.cpp
More file actions
98 lines (82 loc) · 2.34 KB
/
TaskM1T1p.cpp
File metadata and controls
98 lines (82 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <avr/interrupt.h>
#include <Arduino.h>
// Pins
constexpr uint8_t LED_A = 6;
constexpr uint8_t LED_B = 7;
constexpr uint8_t BTN1 = 8; // PCINT0
constexpr uint8_t BTN2 = 9; // PCINT1
constexpr uint8_t BTN3 = 10; // PCINT2
// Debounce
constexpr uint16_t DEBOUNCE_MS = 30;
unsigned long lastPressMs[3] = {0,0,0};
// ISR-shared
volatile uint8_t last_pb = 0; // PB0..PB2 snapshot
volatile uint8_t pendingPressMask = 0; // bit0=BTN1, bit1=BTN2, bit2=BTN3
volatile bool tick = false;
void setup() {
pinMode(LED_A, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(BTN1, INPUT_PULLUP);
pinMode(BTN2, INPUT_PULLUP);
pinMode(BTN3, INPUT_PULLUP);
Serial.begin(9600);
Serial.println(F("PCINT D8..D10, Timer1 1Hz"));
// PCINT for PORTB (D8..D13)
last_pb = PINB & 0b00000111;
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0) | (1 << PCINT1) | (1 << PCINT2);
// Timer1 CTC 1 Hz
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 15624; // 16MHz/1024 - 1
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A);
interrupts();
}
void loop() {
noInterrupts();
uint8_t mask = pendingPressMask;
pendingPressMask = 0;
interrupts();
if (mask) {
const unsigned long now = millis();
if (mask & 0b001) {
if (now - lastPressMs[0] > DEBOUNCE_MS) {
lastPressMs[0] = now;
Serial.println(F("BTN1: LED_B ON"));
digitalWrite(LED_B, HIGH);
}
}
if (mask & 0b010) {
if (now - lastPressMs[1] > DEBOUNCE_MS) {
lastPressMs[1] = now;
Serial.println(F("BTN2: LED_B OFF"));
digitalWrite(LED_B, LOW);
}
}
if (mask & 0b100) {
if (now - lastPressMs[2] > DEBOUNCE_MS) {
lastPressMs[2] = now;
Serial.println(F("BTN3: TOGGLE A+B"));
digitalWrite(LED_A, !digitalRead(LED_A));
digitalWrite(LED_B, !digitalRead(LED_B));
}
}
}
}
ISR(PCINT0_vect) {
uint8_t pb = PINB & 0b00000111;
uint8_t changed = pb ^ last_pb;
if ((changed & (1 << 0)) && !(pb & (1 << 0))) pendingPressMask |= 0b001;
if ((changed & (1 << 1)) && !(pb & (1 << 1))) pendingPressMask |= 0b010;
if ((changed & (1 << 2)) && !(pb & (1 << 2))) pendingPressMask |= 0b100;
last_pb = pb;
}
ISR(TIMER1_COMPA_vect) {
tick = !tick;
digitalWrite(LED_A, tick);
Serial.println(F("tick"));
}