add rudimentary interrupt-less UART

add interrupt support at least for reading the commands that come over
UART
This commit is contained in:
Vineet K 2024-04-19 17:42:44 -04:00 committed by Shokara Kou
parent 7eb9db0d82
commit 7ce6f869a0

33
notes.c
View File

@ -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));
}
}