summaryrefslogtreecommitdiff
path: root/notes.c
diff options
context:
space:
mode:
authorVineet K <git@vineetk.net>2024-04-20 00:01:55 -0400
committerkar <kar@13f0.net>2024-04-20 00:01:55 -0400
commitedd35cae3b841281d5f1754cd5eb7cabfa7c8b16 (patch)
tree1ccc81b07b6c78017637de832a57010212c9ab75 /notes.c
parent7ce6f869a058b90e6e2cff2554296f6c3cbcfa63 (diff)
update UART code to send note and #cycles played
Besides moving the UART into an interrupt, we also need to read from UART to set the mode of playing from buttons or from serial notes
Diffstat (limited to 'notes.c')
-rw-r--r--notes.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/notes.c b/notes.c
index 946ecd6..c677c9a 100644
--- a/notes.c
+++ b/notes.c
@@ -19,27 +19,25 @@
19#define P_SPKR 2 19#define P_SPKR 2
20 20
21unsigned short freqs[] = { 21unsigned 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};
45unsigned char note = 0; 43unsigned 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}