summaryrefslogtreecommitdiffstats
path: root/PIC/test_uart.c
diff options
context:
space:
mode:
Diffstat (limited to 'PIC/test_uart.c')
-rw-r--r--PIC/test_uart.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/PIC/test_uart.c b/PIC/test_uart.c
new file mode 100644
index 0000000..f453e21
--- /dev/null
+++ b/PIC/test_uart.c
@@ -0,0 +1,36 @@
+#include <pic16f84a.h>
+
+typedef unsigned char uint8_t;
+
+// UART memory-mapped registers (hooked up in dpram.vhd).
+// Placed at 0x70-0x72: unimplemented in the real PIC16F84A so no
+// conflict with the SFR definitions in pic16f84a.h.
+// 0x70 UART_TX write: transmit a byte
+// 0x71 UART_RX read: receive a byte (pops the RX FIFO)
+// 0x72 UART_STATUS read: bit0=tx_busy, bit1=rx_empty, bit2=rx_full
+__sfr __at(0x70) UART_TX;
+__sfr __at(0x71) UART_RX;
+__sfr __at(0x72) UART_STATUS;
+
+static void uart_send(uint8_t byte) {
+ while (UART_STATUS & 0x01); // wait until tx_busy = 0
+ UART_TX = byte;
+}
+
+static void uart_puts(const char *s) {
+ while (*s) {
+ uart_send((uint8_t)*s);
+ s++;
+ }
+}
+
+void main(void) {
+ uart_puts("Hello from PIC!\r\n");
+
+ // Echo loop: read every incoming byte and send it back
+ while (1) {
+ if (!(UART_STATUS & 0x02)) { // rx_empty = 0 → data available
+ uart_send(UART_RX);
+ }
+ }
+}