diff --git a/notes.c b/notes.c index 7c92b83..946ecd6 100644 --- 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)); } }