summaryrefslogtreecommitdiffstats
path: root/fw/test_uart.c
diff options
context:
space:
mode:
authorVaino Kauppila <vaino@vke.fi>2026-04-19 12:32:58 +0300
committerVaino Kauppila <vaino@vke.fi>2026-04-19 12:32:58 +0300
commiteb8643ddbef43547eb7bfe58789a3908926c7258 (patch)
treefadfba88a1c71b266b8c67df166a3a6ead6b30d1 /fw/test_uart.c
parent129a1c975dbd0c042576010b1f6eb40d19fc5733 (diff)
downloadvhdl_pic-eb8643ddbef43547eb7bfe58789a3908926c7258.tar.gz
vhdl_pic-eb8643ddbef43547eb7bfe58789a3908926c7258.zip
refactor dir structure, add fib.c
Diffstat (limited to 'fw/test_uart.c')
-rw-r--r--fw/test_uart.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/fw/test_uart.c b/fw/test_uart.c
new file mode 100644
index 0000000..4e18dc5
--- /dev/null
+++ b/fw/test_uart.c
@@ -0,0 +1,69 @@
+#include <pic16f84a.h>
+
+typedef unsigned char uint8_t;
+
+__sfr __at(0x70) UART_TX;
+__sfr __at(0x71) UART_RX;
+__sfr __at(0x72) UART_STATUS;
+
+static volatile uint8_t _d0, _d1, _d2;
+static uint8_t counter;
+
+static void delay(void) {
+ _d0 = 0;
+ do {
+ _d1 = 0;
+ do {
+ _d2 = 0;
+ do { _d2++; } while (_d2 != 0);
+ } while (++_d1 != 30);
+ _d0++;
+ } while (_d0 != 10);
+}
+
+static void uart_send(uint8_t byte) {
+ while (UART_STATUS & 0x01);
+ UART_TX = byte;
+}
+
+// Keep this ONLY for literal strings (ROM)
+static void uart_puts_rom(const char *s) {
+ while (*s) {
+ uart_send((uint8_t)*s);
+ s++;
+ }
+}
+
+void main(void) {
+ counter = 0;
+ uart_puts_rom("counter\r\n");
+
+ while (1) {
+ uint8_t v = counter;
+ uint8_t started = 0;
+
+
+ uart_puts_rom("\r");
+ uart_puts_rom("\x1B[K");
+
+ uart_send('C');
+ uart_send(':');
+
+ // Hundreds
+ if (v >= 200) { uart_send('2'); v -= 200; started = 1; }
+ else if (v >= 100) { uart_send('1'); v -= 100; started = 1; }
+
+ // Tens
+ {
+ uint8_t tens = '0';
+ while (v >= 10) { v -= 10; tens++; }
+ if (started || tens != '0') { uart_send(tens); }
+ }
+
+ // Units (always)
+ uart_send(v + '0');
+
+ counter++;
+ delay();
+ }
+}