diff options
Diffstat (limited to 'notes.c')
| -rw-r--r-- | notes.c | 30 |
1 files changed, 18 insertions, 12 deletions
| @@ -19,27 +19,25 @@ | |||
| 19 | #define P_SPKR 2 | 19 | #define P_SPKR 2 |
| 20 | 20 | ||
| 21 | unsigned short freqs[] = { | 21 | unsigned short freqs[] = { |
| 22 | // PD3-7 | ||
| 22 | FREQ(100), | 23 | FREQ(100), |
| 23 | FREQ(200), | 24 | FREQ(200), |
| 24 | FREQ(250), | 25 | FREQ(250), |
| 25 | FREQ(300), | 26 | FREQ(300), |
| 26 | FREQ(350), | 27 | FREQ(350), |
| 28 | // PB0-4 | ||
| 27 | FREQ(400), | 29 | FREQ(400), |
| 28 | FREQ(500), | 30 | FREQ(500), |
| 29 | FREQ(600), | 31 | FREQ(600), |
| 30 | FREQ(700), | 32 | FREQ(700), |
| 31 | FREQ(750), | 33 | FREQ(750), |
| 34 | // PC0-4 | ||
| 32 | FREQ(1000), | 35 | FREQ(1000), |
| 33 | FREQ(1250), | 36 | FREQ(1250), |
| 34 | FREQ(1500), | 37 | FREQ(1500), |
| 35 | FREQ(1750), | 38 | FREQ(1750), |
| 36 | FREQ(2000), | 39 | FREQ(2000), |
| 37 | FREQ(3000), | 40 | |
| 38 | FREQ(4000), | ||
| 39 | FREQ(5000), | ||
| 40 | FREQ(6000), | ||
| 41 | FREQ(7000), | ||
| 42 | FREQ(8000), | ||
| 43 | [255] = 0 // used as a 'silent' note | 41 | [255] = 0 // used as a 'silent' note |
| 44 | }; | 42 | }; |
| 45 | unsigned char note = 0; | 43 | unsigned char note = 0; |
| @@ -60,7 +58,7 @@ ISR (PCINT1_vect) | |||
| 60 | // check if any button in the whole port is pressed | 58 | // check if any button in the whole port is pressed |
| 61 | for (unsigned char i = 0; i <= 5; i++) { | 59 | for (unsigned char i = 0; i <= 5; i++) { |
| 62 | if (PINC & (1 << i)) { | 60 | if (PINC & (1 << i)) { |
| 63 | note = i + 5 + 6; | 61 | note = i + 10; |
| 64 | break; | 62 | break; |
| 65 | } | 63 | } |
| 66 | } | 64 | } |
| @@ -123,13 +121,24 @@ main(void) | |||
| 123 | 121 | ||
| 124 | // Sends Square wave to the Speaker When a Button is Pressed | 122 | // Sends Square wave to the Speaker When a Button is Pressed |
| 125 | unsigned short cycles = 0; | 123 | unsigned short cycles = 0; |
| 124 | unsigned char last_note = 0; | ||
| 126 | for (unsigned char n = 0;; n = (n + 1) % 2) { | 125 | for (unsigned char n = 0;; n = (n + 1) % 2) { |
| 127 | // check if any buttons are pressed, otherwise don't play a note | 126 | // check if any buttons are pressed, otherwise don't play a note |
| 128 | if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) | 127 | if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) { |
| 128 | // if a button was released, then send note and cycles played | ||
| 129 | if (cycles > 0) { | ||
| 130 | usart_send(last_note); | ||
| 131 | usart_send(HIGH(cycles)); | ||
| 132 | usart_send(LOW(cycles)); | ||
| 133 | usart_send('\xff'); | ||
| 134 | cycles = 0; | ||
| 135 | } | ||
| 129 | continue; | 136 | continue; |
| 130 | 137 | } | |
| 138 | |||
| 131 | // Prepare Timer/Counter | 139 | // Prepare Timer/Counter |
| 132 | TCNT1 = 1; | 140 | TCNT1 = 1; |
| 141 | last_note = note; | ||
| 133 | OCR1A = freqs[note]; | 142 | OCR1A = freqs[note]; |
| 134 | TCCR1B |= 1 << CS11; | 143 | TCCR1B |= 1 << CS11; |
| 135 | 144 | ||
| @@ -140,8 +149,5 @@ main(void) | |||
| 140 | TIFR1 |= 1 << OCF1A; | 149 | TIFR1 |= 1 << OCF1A; |
| 141 | TCCR1B &= ~(1 << CS11); | 150 | TCCR1B &= ~(1 << CS11); |
| 142 | cycles = (cycles + 1) % 0xffff; | 151 | cycles = (cycles + 1) % 0xffff; |
| 143 | |||
| 144 | usart_send(HIGH(cycles)); | ||
| 145 | usart_send(LOW(cycles)); | ||
| 146 | } | 152 | } |
| 147 | } | 153 | } |
