commit edd35cae3b841281d5f1754cd5eb7cabfa7c8b16
parent 7ce6f869a058b90e6e2cff2554296f6c3cbcfa63
Author: Vineet K <git@vineetk.net>
Date: Sat, 20 Apr 2024 00:01:55 -0400
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:
2 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/Makefile b/Makefile
@@ -21,3 +21,9 @@ lcd:
flash_lcd: notes
avrdude -p atmega328p -c arduino -P ${PORT} -b 115200 -U flash:w:lcd.hex:i
+
+serial_setup:
+ stty -F ${PORT} raw speed 115200 cs8 -cstopb -parenb
+
+serial_open:
+ od -x --endian=big -w4 ${PORT}
diff --git a/notes.c b/notes.c
@@ -19,27 +19,25 @@
#define P_SPKR 2
unsigned short freqs[] = {
+ // PD3-7
FREQ(100),
FREQ(200),
FREQ(250),
FREQ(300),
FREQ(350),
+ // PB0-4
FREQ(400),
FREQ(500),
FREQ(600),
FREQ(700),
FREQ(750),
+ // PC0-4
FREQ(1000),
FREQ(1250),
FREQ(1500),
FREQ(1750),
FREQ(2000),
- FREQ(3000),
- FREQ(4000),
- FREQ(5000),
- FREQ(6000),
- FREQ(7000),
- FREQ(8000),
+
[255] = 0 // used as a 'silent' note
};
unsigned char note = 0;
@@ -60,7 +58,7 @@ 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 + 6;
+ note = i + 10;
break;
}
}
@@ -123,13 +121,24 @@ main(void)
// Sends Square wave to the Speaker When a Button is Pressed
unsigned short cycles = 0;
+ unsigned char last_note = 0;
for (unsigned char n = 0;; n = (n + 1) % 2) {
// check if any buttons are pressed, otherwise don't play a note
- if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0)
+ if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) {
+ // if a button was released, then send note and cycles played
+ if (cycles > 0) {
+ usart_send(last_note);
+ usart_send(HIGH(cycles));
+ usart_send(LOW(cycles));
+ usart_send('\xff');
+ cycles = 0;
+ }
continue;
-
+ }
+
// Prepare Timer/Counter
TCNT1 = 1;
+ last_note = note;
OCR1A = freqs[note];
TCCR1B |= 1 << CS11;
@@ -140,8 +149,5 @@ main(void)
TIFR1 |= 1 << OCF1A;
TCCR1B &= ~(1 << CS11);
cycles = (cycles + 1) % 0xffff;
-
- usart_send(HIGH(cycles));
- usart_send(LOW(cycles));
}
}