在Linux下沒有像keli那樣好用的IDE來開發51單片機,開發環境只能自己搭建了。
第一步:安裝交叉編譯工具
a) 安裝SDCC
sudo apt-get install sdcc
b)測試SDCC是否可用,這是個網上找的簡單的流水燈代碼 test.c, 用來測試
1 #include "8051.h" 2 3 #define uint unsigned int 4 #define uchar unsigned char 5 uchar tab[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; 6 7 void Delay(uint xms){ 8 uint i,j; 9 for(i=xms;i>0;i--) 10 for(j=110;j>0;j--); 11 } 12 13 void main(){ 14 uchar i; 15 while(1){ 16 for(i=0;i<8;i++){ 17 P1 = tab[i]; 18 Delay(100); 19 } 20 } 21 }
編譯它:
sdcc test.c
會生成這么多的文件(我們只需要其中的 test.ihx):
test.lk test.map test.rel test.sym test.asm test.ihx test.lst test.mem test.rst
packihx file.ihx >file.hex 轉換為hex文件
接着下載hex2bin文件,網址(http://sourceforge.net/projects/hex2bin/files/latest/download)。
hex2bin sourcefile.hex。之后就會生成sourcefile.bin文件。
注意:為了方便以后調用hex2bin,可以將路徑加入到 .bashrc文件在~/.bashrc最后一行加上Hex2bin 所在的文件夾位置:
PATH=$PATH:/home/jikexianfeng/51-demo/Hex2bin-2.5
可以寫個makefile文件,編譯方便些
這是我寫的makefile:
1 test.hex : test.c 2 sdcc test.c 3 packihx test.ihx > test.hex 4 hex2bin test.hex 5 clean: 6 rm -rf *.asm *.lst *.mem *.rst *.lnk *.rel *.sym *.ihx *.hex *.map
第二步:安裝燒寫工具
a)下載stcflash: http://github.com/laborer/stcflash ,這是個用python寫的向單片機燒寫bin文件的軟件
b)安裝環境:sudo apt-get install python-serial
c)燒寫 : sudo python ./stcflash.py test.bin
解決 ImportError: No module named 'serial' 問題
Traceback (most recent call last): File "/home/jikexianfeng/51-demo/.stcflash/stcflash.py", line 22, in <module> import serial ImportError: No module named serial
解決辦法(安裝 serial module,這里區分python2和 python3):
sudo apt install python-pip //python2 sudo apt install python3-pip //python3

安裝python3的 pip3的時候,如果時國內的軟件源可能安裝不上,(當時用中國科學技術大學的軟件源,python3-pip下載有問題),換成ubuntu官網成功下載。
安裝pyserial:
pip install pyserial //python2 pip3 install pyserial //python3

可以從pyserial下載這里去獲取源碼進行熟悉和學習。
