diff options
| -rw-r--r-- | notes.c | 98 |
1 files changed, 41 insertions, 57 deletions
| @@ -39,7 +39,39 @@ unsigned short freqs[] = { | |||
| 39 | FREQ(8000), | 39 | FREQ(8000), |
| 40 | }; | 40 | }; |
| 41 | unsigned char note = 0; | 41 | unsigned char note = 0; |
| 42 | unsigned char buttonpressed = 0; | 42 | |
| 43 | ISR (PCINT0_vect) | ||
| 44 | { | ||
| 45 | // check if any button in the whole port is pressed | ||
| 46 | for (unsigned char i = 0; i <= 5; i++) { | ||
| 47 | if (PINB & (1 << i)) { | ||
| 48 | note = i + 5 + 6; | ||
| 49 | break; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | ISR (PCINT1_vect) | ||
| 55 | { | ||
| 56 | // check if any button in the whole port is pressed | ||
| 57 | for (unsigned char i = 0; i <= 5; i++) { | ||
| 58 | if (PINC & (1 << i)) { | ||
| 59 | note = i + 5; | ||
| 60 | break; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | ISR (PCINT2_vect) | ||
| 66 | { | ||
| 67 | // check if any button in the whole port is pressed | ||
| 68 | for (unsigned char i = 3; i <= 7; i++) { | ||
| 69 | if (PIND & (1 << i)) { | ||
| 70 | note = i; | ||
| 71 | break; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 43 | 75 | ||
| 44 | int | 76 | int |
| 45 | main(void) | 77 | main(void) |
| @@ -50,10 +82,11 @@ main(void) | |||
| 50 | DDRD = (1 << P_TX) | (1 << P_SPKR); | 82 | DDRD = (1 << P_TX) | (1 << P_SPKR); |
| 51 | 83 | ||
| 52 | // Enabling Pin Change Interrupt for All Buttons | 84 | // Enabling Pin Change Interrupt for All Buttons |
| 53 | PCMSK0 = 0xFF; | 85 | PCMSK0 = 0b00111111; |
| 54 | PCMSK1 = 0xFF; | 86 | PCMSK1 = 0b00111111; |
| 55 | PCMSK2 = ~(1 | (1 << 1) | (1 << 2)); | 87 | PCMSK2 = 0b11111000; |
| 56 | PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2); | 88 | // PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2); |
| 89 | PCICR = 1 << PCIE2; | ||
| 57 | 90 | ||
| 58 | TCCR1A = 0x00; // use timer1 CTC mode | 91 | TCCR1A = 0x00; // use timer1 CTC mode |
| 59 | TCCR1B = 1 << WGM12; // already prescaled by 8 in table | 92 | TCCR1B = 1 << WGM12; // already prescaled by 8 in table |
| @@ -62,7 +95,9 @@ main(void) | |||
| 62 | 95 | ||
| 63 | // Sends Square wave to the Speaker When a Button is Pressed | 96 | // Sends Square wave to the Speaker When a Button is Pressed |
| 64 | for (unsigned char n = 0;; n = (n + 1) % 2) { | 97 | for (unsigned char n = 0;; n = (n + 1) % 2) { |
| 65 | if (!buttonpressed && n == 0) continue; | 98 | //if (((PIND & 0b11111000) | (PINC & 0b00111111) | (PINB & 0b00111111)) == 0) |
| 99 | if ((PIND & 0b11111000) == 0) | ||
| 100 | continue; | ||
| 66 | 101 | ||
| 67 | // Prepare Timer/Counter | 102 | // Prepare Timer/Counter |
| 68 | TCNT1 = 1; | 103 | TCNT1 = 1; |
| @@ -77,54 +112,3 @@ main(void) | |||
| 77 | TCCR1B &= ~(1 << CS11); | 112 | TCCR1B &= ~(1 << CS11); |
| 78 | } | 113 | } |
| 79 | } | 114 | } |
| 80 | |||
| 81 | ISR (PCINT0_vect) | ||
| 82 | { | ||
| 83 | // check if any button on any port is pressed | ||
| 84 | if (PINB | PINC | PIND) | ||
| 85 | buttonpressed = 1; | ||
| 86 | else | ||
| 87 | buttonpressed = 0; | ||
| 88 | |||
| 89 | // check if any button in the whole port is pressed | ||
| 90 | for (unsigned char i = 0; i <= 5; i++) { | ||
| 91 | if (PINB & (1 << i)) { | ||
| 92 | note = i; | ||
| 93 | break; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | ISR (PCINT1_vect) | ||
| 99 | { | ||
| 100 | // check if any button on any port is pressed | ||
| 101 | if (PINB | PINC | PIND) | ||
| 102 | buttonpressed = 1; | ||
| 103 | else | ||
| 104 | buttonpressed = 0; | ||
| 105 | |||
| 106 | // check if any button in the whole port is pressed | ||
| 107 | for (unsigned char i = 0; i <= 5; i++) { | ||
| 108 | if (PINC & (1 << i)) { | ||
| 109 | note = i; | ||
| 110 | break; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | ISR (PCINT2_vect) | ||
| 116 | { | ||
| 117 | // check if any button on any port is pressed | ||
| 118 | if (PINB | PINC | PIND) | ||
| 119 | buttonpressed = 1; | ||
| 120 | else | ||
| 121 | buttonpressed = 0; | ||
| 122 | |||
| 123 | // check if any button in the whole port is pressed | ||
| 124 | for (unsigned char i = 3; i <= 7; i++) { | ||
| 125 | if (PIND & (1 << i)) { | ||
| 126 | note = i; | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
