MAC環(huán)境51單片機(jī)開(kāi)發(fā)

環(huán)境

硬件
macOS Mojave
版本 10.14.6

普中 HC6800-ES V2.0開(kāi)發(fā)板
軟件
串口轉(zhuǎn) USB 驅(qū)動(dòng) CH341

51 內(nèi)核編譯器 sdcc

程序下載工具 stcgal

代碼編輯器 visual studio code

PlatformIO IDE 插件安裝

image.png

VSCode插件搜索PlatformIO IDE安裝

重啟之后,進(jìn)入插件目錄

新建項(xiàng)目路徑


image.png
新建項(xiàng)目
填寫(xiě)信息

安裝homebrew

brew 是Mac的包管理管理工具
安裝指令

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

安裝sdcc

編譯內(nèi)核
安裝指令

brew install sdcc

驗(yàn)證是否安裝成功:

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ū)動(dòng)

下載地址

https://www.wch.cn/download/CH341SER_MAC_ZIP.html

插上單片機(jī)看看驅(qū)動(dòng)是否成功,輸入以下指令

ls /dev/tty.wchusbser*

驅(qū)動(dòng)安裝成功

/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

驗(yàn)證是否安裝成功:

stcgal -v

下載vscode

程序測(cè)試

點(diǎn)亮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)找不到庫(kù)的情況

led.c:1:10: error: #include expects "FILENAME" or <FILENAME>
led.c:7: error 20: Undefined identifier 'P1_0'
image.png

編譯程序

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型號(hào), -p /dev/tty.wchusbserial1410 表示usb串口設(shè)備,led.ihx 是剛剛編譯好的程序,將要燒錄程序。

Waiting for MCU, please cycle power: 

輸入指令后,如果出現(xiàn) Waiting for MCU, please cycle power: ,這個(gè)時(shí)候我們按下單片機(jī)的電源鍵,重啟單片機(jī),就可以成功把代碼燒錄至單片機(jī)中了。

點(diǎn)亮效果圖

報(bào)錯(cuò)

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容