Mac开发STC51单片机

Tue 23 September 2025

Mac开发STC51单片机 目录 STC官网 EdSim51模拟器 SDCC官网 SDCC简介 SDCC安装 头文件目录 编译命令 多个.c文件编译 安装烧录工具 使用方法 通讯协议与规定 make 编译烧录 模拟器运行效果 常用命令 选择目标平台 指定芯片型号 设置模型 调整优化级别 生成调试信息 指定输出文件名 选择内存布局 设置堆栈大小 生成汇编文件 指定头文件目录 使用C89标准 指定RAM区域大小 STC官网 https://www.stcmcudata.com/ EdSim51模拟器 http://www.edsim51.com/ 要安装Java环境才能运行 image SDCC官网 https://sdcc.sourceforge.net/ SDCC简介 SDCC(Small Device C Compiler)是一款开源的C语言编译器,专门设计用于嵌入式系统和小型设备的开发。它支持多种架构和芯片,包括一些常见的单片机和微处理器,如8051、Z80、PIC等。

SDCC安装 brew install sdcc sdcc -v 头文件目录 cd /usr/local/share/sdcc/include/mcs51

include

include <8051.h>

include <8052.h>

include

include

include

include

include

编译命令

编译C代码,生成多个文件,包括main.ihx、main.hex

sdcc -mmcs51 --model-small -p89 main.c

-mmcs51: 指定目标平台为MCS-51系列,适用于8051单片机。

--model-small: 选择小型模型,适用于资源受限的设备,如STC89C51。

-p89: 指定目标芯片型号为STC89系列。

生成.hex文件

packihx main.ihx > main.hex 多个.c文件编译

用-c编译.c文件,生成.rel目标文件

sdcc -c tm1638.c

链接.rel目标文件,生成.ihx目标文件

sdcc main.c tm1638.rel

生成.hex文件

packihx main.ihx > main.hex 安装烧录工具 https://github.com/grigorig/stcgal https://github.com/grigorig/stcgal/blob/master/doc/zh_CN/USAGE.md 使用方法 usage: stcgal [-h] [-e] [-a] [-A {dtr,rts}] [-r RESETCMD] [-P {stc89,stc12a,stc12b,stc12,stc15a,stc15,stc8,stc8d,stc8g,usb15,auto}] [-p PORT] [-b BAUD] [-l HANDSHAKE] [-o OPTION] [-t TRIM] [-D] [-V] [code_image] [eeprom_image]

stcgal 1.7 - an STC MCU ISP flash tool (C) 2014-2018 Grigori Goronzy and others https://github.com/grigorig/stcgal

positional arguments: code_image code segment file to flash (BIN/HEX) //代码段文件刷新 eeprom_image eeprom segment file to flash (BIN/HEX) //EEPROM段文件刷新

optional arguments: -h, --help show this help message and exit //显示此帮助消息并退出 -a, --autoreset cycle power automatically by asserting DTR//断言DTR自动重启电源 -A {dtr,rts}, --resetpin {dtr,rts} pin to hold down when using --autoreset (default: DTR) -r RESETCMD, --resetcmd RESETCMD shell command for board power-cycling (instead of DTR //用于板上电重启的shell命令(而不是DTR断言) assertion) -P {stc89,stc12a,stc12b,stc12,stc15a,stc15,stc8,stc8d,stc8g,usb15,auto}, --protocol {stc89,stc12a,stc12b,stc12,stc15a,stc15,stc8,stc8d,stc8g,usb15,auto} protocol version (default: auto) //协议版本(芯片系列)(在默认状态为auto) -p PORT, --port PORT serial port device //串口设备 -b BAUD, --baud BAUD transfer baud rate (default: 115200) //传输波特率(默认值:115200) -l HANDSHAKE, --handshake HANDSHAKE handshake baud rate (default: 2400) //握手波特率(默认值:2400) -o OPTION, --option OPTION set option (can be used multiple times, see//设置选项(可以多次使用,请参阅文档) documentation) -t TRIM, --trim TRIM RC oscillator frequency in kHz (STC15+ series only)//RC振荡器频率(kHz)(仅STC15 +系列) -D, --debug enable debug output //启用调试输出 -V, --version print version info and exit //打印版本信息并退出 通讯协议与规定 auto 自动检测基于UART的协议(默认) stc89 STC89/90 系列 stc89a STC89/90 系列(BSL 7.2.5C) stc12a STC12x052 系列和其他类似系列 stc12b STC12x52 系列, STC12x56 系列和其他类似系列 stc12 多数 STC10/11/12 系列 stc15a STC15x104E 和 STC15x204E(A) 系列 stc15 多数 STC15 系列 stc8 STC8A8K64S4A12 和 STC8F 系列 stc8d 所有 STC8 和 STC32 系列 stc8g STC8G1 和 STC8H1 系列 usb15 支持USB的STC15W4系列 pip3 install stcgal

查看串口

ls /dev/tty.wchusbser*

烧录程序

stcgal -P stc89 -b 9600 -p /dev/tty.wchusbserial14330 main.hex make 编译烧录 main.c

include <8051.h>

void main(void) { for (;;) { P1--; } } Makefile 文件内容

SDCC编译器

CC = sdcc

SDCC链接器

LD = sdcc

编译选项,这里使用了-mmcs51 --model-small来指定目标为8051系列单片机的小模型

CFLAGS = -mmcs51 --model-small -p89

源文件的列表,这里只有一个main.c

SRC = main.c

目标文件的名称

TARGET = main IHX_FILE = $(TARGET).ihx HEX_FILE = $(TARGET).hex

是默认的目标,它依赖于$(HEX_FILE)

all: flash clean

目标用于烧录生成的Hex文件到STC89系列的单片机

flash: $(SRC) $(CC) $(CFLAGS) $(SRC) && packihx $(IHX_FILE) > $(HEX_FILE)

烧录程序

stcgal -p stc89 -P /dev/ttyUSB0 -b 9600 $(IHX_FILE)

stcgal -p stc89 -P /dev/ttyUSB0 -b 9600 $(HEX_FILE)

清理生成的中间文件

clean: rm -f .asm .cdb .rel .rst .map .mem .lk .lst .sym .ihx *.hex make all 模拟器运行效果 load文件.hex,然后run运行 image 常用命令 选择目标平台 -mmcs51: 适用于MCS-51系列单片机,如8051。 -mpic16: 适用于PIC16系列单片机。 指定芯片型号 -p: 指定目标芯片型号,例如 -p89 表示STC89系列单片机。 设置模型 --model-small: 选择小型模型,适用于资源受限的设备。 调整优化级别 -O0: 关闭优化。 -O1: 启用一般优化。 -O2: 启用更高级别的优化。 生成调试信息 --debug: 生成调试信息,方便调试程序。 指定输出文件名 -o : 指定输出文件的名称。 选择内存布局 --xram-loc 0xXXXX: 指定XRAM区的起始地址。 --code-loc 0xXXXX: 指定代码区的起始地址。 设置堆栈大小 --stack-loc 0xXXXX: 指定堆栈区的起始地址。 --stack-size : 指定堆栈的大小。 生成汇编文件 -S: 生成汇编文件而不进行编译。 指定头文件目录 -I: 添加头文件搜索路径。 使用C89标准 --std-c89: 使用C89标准。 指定RAM区域大小 --xram-size : 指定XRAM区的大小

Category: 编程stc51