summaryrefslogtreecommitdiffstats
path: root/PIC/test_uart.c
blob: 2656371791b7fb593faf6e52f7c9493b6e7108c8 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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 != 116);
}

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("Hello\r\n");

    while (1) {
        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();
    }
}