commit e16ce4456373d02beab8a507c13f0d91598dc887
parent 3808c19de4ced0a834341564f3b24cd21de59311
Author: Vineet K <git@vineetk.net>
Date: Fri, 19 Apr 2024 16:04:29 -0400
fix button interrupt and handling
Now notes play on the speaker when the buttons are held.
Diffstat:
| M | notes.c | | | 98 | +++++++++++++++++++++++++++++++++---------------------------------------------- |
1 file changed, 41 insertions(+), 57 deletions(-)
diff --git a/notes.c b/notes.c
@@ -39,7 +39,39 @@ unsigned short freqs[] = {
FREQ(8000),
};
unsigned char note = 0;
-unsigned char buttonpressed = 0;
+
+ISR (PCINT0_vect)
+{
+ // check if any button in the whole port is pressed
+ for (unsigned char i = 0; i <= 5; i++) {
+ if (PINB & (1 << i)) {
+ note = i + 5 + 6;
+ break;
+ }
+ }
+}
+
+ISR (PCINT1_vect)
+{
+ // check if any button in the whole port is pressed
+ for (unsigned char i = 0; i <= 5; i++) {
+ if (PINC & (1 << i)) {
+ note = i + 5;
+ break;
+ }
+ }
+}
+
+ISR (PCINT2_vect)
+{
+ // check if any button in the whole port is pressed
+ for (unsigned char i = 3; i <= 7; i++) {
+ if (PIND & (1 << i)) {
+ note = i;
+ break;
+ }
+ }
+}
int
main(void)
@@ -50,10 +82,11 @@ main(void)
DDRD = (1 << P_TX) | (1 << P_SPKR);
// Enabling Pin Change Interrupt for All Buttons
- PCMSK0 = 0xFF;
- PCMSK1 = 0xFF;
- PCMSK2 = ~(1 | (1 << 1) | (1 << 2));
- PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2);
+ PCMSK0 = 0b00111111;
+ PCMSK1 = 0b00111111;
+ PCMSK2 = 0b11111000;
+// PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2);
+ PCICR = 1 << PCIE2;
TCCR1A = 0x00; // use timer1 CTC mode
TCCR1B = 1 << WGM12; // already prescaled by 8 in table
@@ -62,7 +95,9 @@ main(void)
// Sends Square wave to the Speaker When a Button is Pressed
for (unsigned char n = 0;; n = (n + 1) % 2) {
- if (!buttonpressed && n == 0) continue;
+ //if (((PIND & 0b11111000) | (PINC & 0b00111111) | (PINB & 0b00111111)) == 0)
+ if ((PIND & 0b11111000) == 0)
+ continue;
// Prepare Timer/Counter
TCNT1 = 1;
@@ -77,54 +112,3 @@ main(void)
TCCR1B &= ~(1 << CS11);
}
}
-
-ISR (PCINT0_vect)
-{
- // check if any button on any port is pressed
- if (PINB | PINC | PIND)
- buttonpressed = 1;
- else
- buttonpressed = 0;
-
- // check if any button in the whole port is pressed
- for (unsigned char i = 0; i <= 5; i++) {
- if (PINB & (1 << i)) {
- note = i;
- break;
- }
- }
-}
-
-ISR (PCINT1_vect)
-{
- // check if any button on any port is pressed
- if (PINB | PINC | PIND)
- buttonpressed = 1;
- else
- buttonpressed = 0;
-
- // check if any button in the whole port is pressed
- for (unsigned char i = 0; i <= 5; i++) {
- if (PINC & (1 << i)) {
- note = i;
- break;
- }
- }
-}
-
-ISR (PCINT2_vect)
-{
- // check if any button on any port is pressed
- if (PINB | PINC | PIND)
- buttonpressed = 1;
- else
- buttonpressed = 0;
-
- // check if any button in the whole port is pressed
- for (unsigned char i = 3; i <= 7; i++) {
- if (PIND & (1 << i)) {
- note = i;
- break;
- }
- }
-}