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
This commit is contained in:
Vineet K 2024-04-20 00:01:55 -04:00 committed by kar
parent 7ce6f869a0
commit edd35cae3b
2 changed files with 24 additions and 12 deletions

View File

@ -21,3 +21,9 @@ lcd:
flash_lcd: notes flash_lcd: notes
avrdude -p atmega328p -c arduino -P ${PORT} -b 115200 -U flash:w:lcd.hex:i 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}

30
notes.c
View File

@ -19,27 +19,25 @@
#define P_SPKR 2 #define P_SPKR 2
unsigned short freqs[] = { unsigned short freqs[] = {
// PD3-7
FREQ(100), FREQ(100),
FREQ(200), FREQ(200),
FREQ(250), FREQ(250),
FREQ(300), FREQ(300),
FREQ(350), FREQ(350),
// PB0-4
FREQ(400), FREQ(400),
FREQ(500), FREQ(500),
FREQ(600), FREQ(600),
FREQ(700), FREQ(700),
FREQ(750), FREQ(750),
// PC0-4
FREQ(1000), FREQ(1000),
FREQ(1250), FREQ(1250),
FREQ(1500), FREQ(1500),
FREQ(1750), FREQ(1750),
FREQ(2000), FREQ(2000),
FREQ(3000),
FREQ(4000),
FREQ(5000),
FREQ(6000),
FREQ(7000),
FREQ(8000),
[255] = 0 // used as a 'silent' note [255] = 0 // used as a 'silent' note
}; };
unsigned char note = 0; unsigned char note = 0;
@ -60,7 +58,7 @@ ISR (PCINT1_vect)
// check if any button in the whole port is pressed // check if any button in the whole port is pressed
for (unsigned char i = 0; i <= 5; i++) { for (unsigned char i = 0; i <= 5; i++) {
if (PINC & (1 << i)) { if (PINC & (1 << i)) {
note = i + 5 + 6; note = i + 10;
break; break;
} }
} }
@ -123,13 +121,24 @@ main(void)
// Sends Square wave to the Speaker When a Button is Pressed // Sends Square wave to the Speaker When a Button is Pressed
unsigned short cycles = 0; unsigned short cycles = 0;
unsigned char last_note = 0;
for (unsigned char n = 0;; n = (n + 1) % 2) { for (unsigned char n = 0;; n = (n + 1) % 2) {
// check if any buttons are pressed, otherwise don't play a note // 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; continue;
}
// Prepare Timer/Counter // Prepare Timer/Counter
TCNT1 = 1; TCNT1 = 1;
last_note = note;
OCR1A = freqs[note]; OCR1A = freqs[note];
TCCR1B |= 1 << CS11; TCCR1B |= 1 << CS11;
@ -140,8 +149,5 @@ main(void)
TIFR1 |= 1 << OCF1A; TIFR1 |= 1 << OCF1A;
TCCR1B &= ~(1 << CS11); TCCR1B &= ~(1 << CS11);
cycles = (cycles + 1) % 0xffff; cycles = (cycles + 1) % 0xffff;
usart_send(HIGH(cycles));
usart_send(LOW(cycles));
} }
} }