diff options
| author | Vaino Kauppila <vaino@vke.fi> | 2026-04-19 03:02:33 +0300 |
|---|---|---|
| committer | Vaino Kauppila <vaino@vke.fi> | 2026-04-19 03:02:33 +0300 |
| commit | 129a1c975dbd0c042576010b1f6eb40d19fc5733 (patch) | |
| tree | 63a6507f068b112b3b275f3f32369cce532f65a5 /PIC/test_uart.c | |
| parent | 8bd8dd6ce07fb8e247f651b3c61c94bb1f00895a (diff) | |
| download | vhdl_pic-129a1c975dbd0c042576010b1f6eb40d19fc5733.tar.gz vhdl_pic-129a1c975dbd0c042576010b1f6eb40d19fc5733.zip | |
fix operand order of subtraction. add rollover counter UART test.
Diffstat (limited to 'PIC/test_uart.c')
| -rw-r--r-- | PIC/test_uart.c | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/PIC/test_uart.c b/PIC/test_uart.c index f453e21..2656371 100644 --- a/PIC/test_uart.c +++ b/PIC/test_uart.c @@ -2,22 +2,32 @@ 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 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 != 116); +} + static void uart_send(uint8_t byte) { - while (UART_STATUS & 0x01); // wait until tx_busy = 0 + while (UART_STATUS & 0x01); UART_TX = byte; } -static void uart_puts(const char *s) { +// Keep this ONLY for literal strings (ROM) +static void uart_puts_rom(const char *s) { while (*s) { uart_send((uint8_t)*s); s++; @@ -25,12 +35,33 @@ static void uart_puts(const char *s) { } void main(void) { - uart_puts("Hello from PIC!\r\n"); + counter = 0; + uart_puts_rom("Hello\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); + uint8_t v = counter; + uint8_t started = 0; + + 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'); + + uart_puts_rom("\r\n"); + + counter++; + delay(); } } |
