summaryrefslogtreecommitdiffstats
path: root/PIC/test_uart.c
blob: f453e21da7ee7c8e36fdd99a7b58a7ee52bb20c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
        }
    }
}