环境
硬件
macOS Mojave
版本 10.14.6
普中 HC6800-ES V2.0开发板
软件
串口转 USB 驱动 CH341
51 内核编译器 sdcc
程序下载工具 stcgal
代码编辑器 visual studio code
PlatformIO IDE 插件安装
VSCode插件搜索PlatformIO IDE安装
重启之后,进入插件目录
新建项目路径
安装homebrew
brew 是Mac的包管理管理工具
安装指令
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
安装sdcc
编译内核
安装指令
brew install sdcc
验证是否安装成功:
sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.0 #13081 (Mac OS X x86_64)
published under GNU General Public License (GPL)
如果失败则直接到官方下载
https://sourceforge.net/projects/sdcc/files/
For Mac OS X users:
===================
To install:
* Extract the binary kit to a temporary directory.
This will create a new directory called 'sdcc-4.2.0' in the temporary directory.
cd ~
mkdir tmp
cd tmp
tar xjf path/to/binary/kit/sdcc-4.2.0-x86_64-apple-macosx.tar.bz2
* Change to the sdcc directory and copy all files to /Developer/sdcc
cp -r sdcc /Developer/sdcc
This will install sdcc binaries into: /Developer/sdcc/bin/
header files into: /Developer/sdcc/share/sdcc/include/
non-free header files into: /Developer/sdcc/share/sdcc/non-free/include/
library files into: /Developer/sdcc/share/sdcc/lib/
non-free library files into: /Developer/sdcc/share/sdcc/non-free/lib/
and documentation into: /Developer/sdcc/share/sdcc/doc/
You can test the install by entering:
/Developer/sdcc/bin/sdcc -v
This should return sdcc's version number.
安装CH341驱动
下载地址
https://www.wch.cn/download/CH341SER_MAC_ZIP.html
插上单片机看看驱动是否成功,输入以下指令
ls /dev/tty.wchusbser*
驱动安装成功
/dev/tty.wchusbserial144110
安装 python3和 pip
mac 自带python2,到官网下载python3
brew install python 作者:五街教授 https://www.bilibili.com/read/cv18942590 出处:bilibili
安装烧录程序stcgal
在linux和mac系统下使用得比较多的是stcgal
下载地址:https://github.com/grigorig/stcgal
安装指令:
pip3 install stcgal
验证是否安装成功:
stcgal -v
下载vscode
程序测试
点亮LED灯
#include <8052.h>
#define uint_8 unsigned int
uint_8 len = 8;
void delay_ms(uint_8 ms);
void main()
{
uint_8 i;
while (1)
{
P0 = 0xff;
for (i = 0; i < len; i++)
{
P0 = P0 >> 1;
delay_ms(100);
}
P0 = 0xff;
for (i = 0; i < len; i++)
{
P0 = P0 << 1;
delay_ms(100);
}
}
}
void delay_ms(uint_8 ms)
{
uint_8 i, j;
for (i = ms; i > 0; i--)
for (j = 110; j > 0; j--)
;
}
注意这里
<8051.h> 不是"8051.h",否则出现找不到库的情况
led.c:1:10: error: #include expects "FILENAME" or
led.c:7: error 20: Undefined identifier 'P1_0'
编译程序
cd led.c所在目录,执行
sdcc led.c
led.asm led.c led.ihx led.lk led.lst led.map led.mem led.rel led.rst led.sym
将在当前目录编译出很多目标文件,我们只需要.ihx后缀的文件就可以,其它的文件可以删除。
烧录程序
Target model:
Name: STC89C52RC/LE52R
Magic: F002
烧录指令:
stcgal -p stc89 -p /dev/tty.wchusbserial144110 led.ihx
-p stc89 表示使用的是stc89型号, -p /dev/tty.wchusbserial1410 表示usb串口设备,led.ihx 是刚刚编译好的程序,将要烧录程序。
Waiting for MCU, please cycle power:
输入指令后,如果出现 Waiting for MCU, please cycle power: ,这个时候我们按下单片机的电源键,重启单片机,就可以成功把代码烧录至单片机中了。
点亮效果图
报错
Switching to 19200 baud: checking Serial port error: read timeout
Switching to 19200 baud: Protocol error: incorrect frame start
降低波特率
stcgal -b 1200 -p stc89 -p /dev/tty.wchusbserial144110 led.ihx