環(huán)境
硬件
macOS Mojave
版本 10.14.6
普中 HC6800-ES V2.0開發(fā)板
軟件
串口轉(zhuǎn) USB 驅(qū)動 CH341
51 內(nèi)核編譯器 sdcc
程序下載工具 stcgal
代碼編輯器 visual studio code
PlatformIO IDE 插件安裝
VSCode插件搜索PlatformIO IDE安裝
重啟之后咱揍,進(jìn)入插件目錄
新建項目路徑
安裝homebrew
brew 是Mac的包管理管理工具
安裝指令
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
安裝sdcc
編譯內(nèi)核
安裝指令
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驅(qū)動
下載地址
https://www.wch.cn/download/CH341SER_MAC_ZIP.html
插上單片機(jī)看看驅(qū)動是否成功贼邓,輸入以下指令
ls /dev/tty.wchusbser*
驅(qū)動安裝成功
/dev/tty.wchusbserial144110
安裝 python3和 pip
mac 自帶python2荔仁,到官網(wǎng)下載python3
brew install python 作者:五街教授 https://www.bilibili.com/read/cv18942590 出處:bilibili
安裝燒錄程序stcgal
在linux和mac系統(tǒng)下使用得比較多的是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",否則出現(xiàn)找不到庫的情況
led.c:1:10: error: #include expects "FILENAME" or <FILENAME>
led.c:7: error 20: Undefined identifier 'P1_0'
編譯程序
cd led.c所在目錄挤聘,執(zhí)行
sdcc led.c
led.asm led.c led.ihx led.lk led.lst led.map led.mem led.rel led.rst led.sym
將在當(dāng)前目錄編譯出很多目標(biāo)文件弧岳,我們只需要.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串口設(shè)備只壳,led.ihx 是剛剛編譯好的程序俏拱,將要燒錄程序。
Waiting for MCU, please cycle power:
輸入指令后吼句,如果出現(xiàn) Waiting for MCU, please cycle power: 彰触,這個時候我們按下單片機(jī)的電源鍵,重啟單片機(jī)命辖,就可以成功把代碼燒錄至單片機(jī)中了况毅。
點亮效果圖
報錯
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