STC8G1K08A 最小 UART hello world 示例

另请参见:

本最小示例基于 sdcc 编译器与 FwLib_STC8 库。它将 UART 初始化为 115200 波特率,并持续打印 Hello, world from UART1! 加换行符。

main.c
#include "fw_hal.h"

/* 将 UART1 配置为 115200 波特率的普通 8N1 调试控制台。
 * 使用 Timer1 作为波特率发生器,因为本仓库中其他小型示例
 * 已经采用了这一默认配置。
 */
static void UART_Init(void)
{
    UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
}

void main(void)
{
    /* 从 fw_conf.h 加载时钟微调值,并在由 SYSCLK 推导任何外设时序之前
     * 将 MCU 切换到所配置的工作频率。
     */
    SYS_SetClock();

    /* 初始化 UART1,以便下方循环能够输出人类可读的内容。 */
    UART_Init();

    while(1)
    {
        /* 通过 UART1 发送一条以 NUL 结尾的字符串。
         * 库会逐字节发送,直到遇到 '\0' 为止。
         */
        UART1_TxString("Hello, world from UART1!\r\n");

        /* 大约等待一秒,使终端输出保持可读,
         * 并便于在串口适配器上核对消息节拍。
         */
        SYS_Delay(1000);
    }
}

如何烧录

参见我们的文章 如何使用 stcgal 烧录 STC8G1K08A

如何查看串口输出

使用以下命令查看输出:

monitor.sh
picocom -b 115200 /dev/ttyUSB0

内存占用

memory_usage.txt
===== Memory usage summary for uart_hello_world =====
Internal RAM layout:
      0 1 2 3 4 5 6 7 8 9 A B C D E F
0x00:|0|0|0|0|0|0|0|0|Q|Q|Q|Q|S|S|S|S|
0x10:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x20:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x30:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x40:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x50:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x60:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x70:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x80:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0x90:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0xa0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0xb0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0xc0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0xd0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0xe0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0xf0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
0-3:Reg Banks, T:Bit regs, a-z:Data, B:Bits, Q:Overlay, I:iData, S:Stack, A:Absolute

Stack starts at: 0x0c (sp set to 0x0b) with 244 bytes available.
No spare internal RAM space left.

Other memory:
   Name             Start    End      Size     Max
   ---------------- -------- -------- -------- --------
   PAGED EXT. RAM                         0      256
   EXTERNAL RAM     0x0001   0x0057      87     1024
   ROM/EPROM/FLASH  0x0000   0x0856    2135     8192
===============================================

Check out similar posts by category: STC8, Embedded, C/C++