微處理器 - Lattice GAL22V10 - C/C++ - UART



參考資訊:
https://www.stcmicro.com/datasheet/STC15F204EA-cn.pdf
https://www.stcmicro.com/datasheet/STC15F2K60S2-en.pdf

AUXR


Interrupt


SCON


main.c

#include <mcs51/8051.h>

__sfr __at(0x8E) AUXR;
__sfr __at(0xD6) T2H;
__sfr __at(0xD7) T2L;

#define LED P1_2
#define BAUD 9600
#define FOSC 11059200UL

void send_string(const char *s)
{
    while (*s) {
        SBUF = *s++;
        while (!TI);
        TI = 0;
    }
}

void main(void)
{
    SCON = 0x50;
    T2H = (65536 - (FOSC / 4 / BAUD)) >> 8;
    T2L = (65536 - (FOSC / 4 / BAUD)) & 0xFF;
    AUXR |= 0x10;
    AUXR |= 0x01;
    AUXR |= 0x04;
    ES = 1;
    EA = 1;

    LED = 1;
    send_string("\r\n");
    send_string("Hello, world!\r\n");
    while (1) {
    }
}

void uart1_isr(void) __interrupt(4)
{
    if (RI) {
        RI = 0;
        LED ^= 1;
        send_string("OK\r\n");
    }
}

完成