commit 7ce6f869a058b90e6e2cff2554296f6c3cbcfa63
parent 7eb9db0d821a0b13441ad7d586a159a8ed4c378f
Author: Vineet K <git@vineetk.net>
Date: Fri, 19 Apr 2024 17:42:44 -0400
add rudimentary interrupt-less UART
add interrupt support at least for reading the commands that come over
UART
Diffstat:
| M | notes.c | | | 33 | +++++++++++++++++++++++++++++++++ |
1 file changed, 33 insertions(+), 0 deletions(-)
diff --git a/notes.c b/notes.c
@@ -11,6 +11,9 @@
#define T1_PRESCALAR 8
#define FREQ(f) (16e6 / f / T1_PRESCALAR / 2)
+#define HIGH(sn) ((sn >> 8) & 0xff)
+#define LOW(sn) (sn & 0xff)
+
#define P_RX 0
#define P_TX 1
#define P_SPKR 2
@@ -74,6 +77,29 @@ ISR (PCINT2_vect)
}
}
+void
+usart_init(void)
+{
+ // 115200 bps, RX/TX enabled
+ UBRR0 = 8;
+ UCSR0B = (1 << RXEN0) | (1 << TXEN0);
+ UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
+}
+
+void
+usart_send(unsigned char c)
+{
+ while (!(UCSR0A & (1 << UDRE0)));
+ UDR0 = c;
+}
+
+unsigned char
+usart_read(void)
+{
+ while (!(UCSR0A & (1 << RXC0)));
+ return UDR0;
+}
+
int
main(void)
{
@@ -93,7 +119,10 @@ main(void)
sei();
+ usart_init();
+
// Sends Square wave to the Speaker When a Button is Pressed
+ unsigned short cycles = 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)
@@ -110,5 +139,9 @@ main(void)
PORTD ^= 1 << P_SPKR;
TIFR1 |= 1 << OCF1A;
TCCR1B &= ~(1 << CS11);
+ cycles = (cycles + 1) % 0xffff;
+
+ usart_send(HIGH(cycles));
+ usart_send(LOW(cycles));
}
}