diff options
| author | Vineet K <git@vineetk.net> | 2024-04-19 17:42:44 -0400 |
|---|---|---|
| committer | Shokara Kou <kou@13f0.net> | 2024-04-19 17:42:44 -0400 |
| commit | 7ce6f869a058b90e6e2cff2554296f6c3cbcfa63 (patch) | |
| tree | 5617360a5b361158e21b8099618244b7ef28b5e3 | |
| parent | 7eb9db0d821a0b13441ad7d586a159a8ed4c378f (diff) | |
add rudimentary interrupt-less UART
add interrupt support at least for reading the commands that come over
UART
| -rw-r--r-- | notes.c | 33 |
1 files changed, 33 insertions, 0 deletions
| @@ -11,6 +11,9 @@ | |||
| 11 | #define T1_PRESCALAR 8 | 11 | #define T1_PRESCALAR 8 |
| 12 | #define FREQ(f) (16e6 / f / T1_PRESCALAR / 2) | 12 | #define FREQ(f) (16e6 / f / T1_PRESCALAR / 2) |
| 13 | 13 | ||
| 14 | #define HIGH(sn) ((sn >> 8) & 0xff) | ||
| 15 | #define LOW(sn) (sn & 0xff) | ||
| 16 | |||
| 14 | #define P_RX 0 | 17 | #define P_RX 0 |
| 15 | #define P_TX 1 | 18 | #define P_TX 1 |
| 16 | #define P_SPKR 2 | 19 | #define P_SPKR 2 |
| @@ -74,6 +77,29 @@ ISR (PCINT2_vect) | |||
| 74 | } | 77 | } |
| 75 | } | 78 | } |
| 76 | 79 | ||
| 80 | void | ||
| 81 | usart_init(void) | ||
| 82 | { | ||
| 83 | // 115200 bps, RX/TX enabled | ||
| 84 | UBRR0 = 8; | ||
| 85 | UCSR0B = (1 << RXEN0) | (1 << TXEN0); | ||
| 86 | UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); | ||
| 87 | } | ||
| 88 | |||
| 89 | void | ||
| 90 | usart_send(unsigned char c) | ||
| 91 | { | ||
| 92 | while (!(UCSR0A & (1 << UDRE0))); | ||
| 93 | UDR0 = c; | ||
| 94 | } | ||
| 95 | |||
| 96 | unsigned char | ||
| 97 | usart_read(void) | ||
| 98 | { | ||
| 99 | while (!(UCSR0A & (1 << RXC0))); | ||
| 100 | return UDR0; | ||
| 101 | } | ||
| 102 | |||
| 77 | int | 103 | int |
| 78 | main(void) | 104 | main(void) |
| 79 | { | 105 | { |
| @@ -93,7 +119,10 @@ main(void) | |||
| 93 | 119 | ||
| 94 | sei(); | 120 | sei(); |
| 95 | 121 | ||
| 122 | usart_init(); | ||
| 123 | |||
| 96 | // Sends Square wave to the Speaker When a Button is Pressed | 124 | // Sends Square wave to the Speaker When a Button is Pressed |
| 125 | unsigned short cycles = 0; | ||
| 97 | for (unsigned char n = 0;; n = (n + 1) % 2) { | 126 | for (unsigned char n = 0;; n = (n + 1) % 2) { |
| 98 | // check if any buttons are pressed, otherwise don't play a note | 127 | // check if any buttons are pressed, otherwise don't play a note |
| 99 | if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) | 128 | if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) |
| @@ -110,5 +139,9 @@ main(void) | |||
| 110 | PORTD ^= 1 << P_SPKR; | 139 | PORTD ^= 1 << P_SPKR; |
| 111 | TIFR1 |= 1 << OCF1A; | 140 | TIFR1 |= 1 << OCF1A; |
| 112 | TCCR1B &= ~(1 << CS11); | 141 | TCCR1B &= ~(1 << CS11); |
| 142 | cycles = (cycles + 1) % 0xffff; | ||
| 143 | |||
| 144 | usart_send(HIGH(cycles)); | ||
| 145 | usart_send(LOW(cycles)); | ||
| 113 | } | 146 | } |
| 114 | } | 147 | } |
