[編譯] 三、在Linux下搭建51單片機的開發燒寫環境(makefile版)

星期二, 10. 七月 2018 01:01上午 - beautifulzzzz php

1、SDCC(Small Device C Compiler)編譯環境搭建

SDCC是一個小型設備的C語言編譯器,該編譯器是標準C語言,能夠編譯Intel MCS51架構的微處理器,也能夠編譯STM8等常見MCU。咱們要在linux上搭建51開發環境,首先要搭建SDCC編譯環境。python

其主頁是:http://sdcc.sourceforge.net/ ,下面是SDCC的首頁簡介:linux

SDCC is a retargettable, optimizing Standard C (ANSI C89, ISO C99, ISO C11) compiler suite that targets the Intel MCS51 based microprocessors (8031, 8032, 8051, 8052, etc.), Maxim (formerly Dallas) DS80C390 variants, Freescale (formerly Motorola) HC08 based (hc08, s08), Zilog Z80 based MCUs (z80, z180, gbz80, Rabbit 2000/3000, Rabbit 3000A, TLCS-90) and STMicroelectronics STM8. Work is in progress on supporting the Microchip PIC16 and PIC18 targets. It can be retargeted for other microprocessors.ios

1.一、下載

有兩個可供下載的:git

注: 對於選擇拿來即用的兄弟,能夠直接跳過1.2節github

1.二、編譯

如何編譯:架構

./config

可能會出現下面幾種錯誤(我也給出瞭解決辦法):electron

  • 錯誤 1 、缺乏bison :工具

    configure: error: Cannot find required program bison.

    解決方法:測試

    sudo apt-get install flex bison
  • 錯誤 2 、找不到adjacency_list.hpp

    configure: error: boost library not found (boost/graph/adjacency_list.hpp).

    解決方法:

    sudo apt-get install libboost-date-time-dev libboost-program-options-dev libboost-system-dev libboost-filesystem-dev libboost-iostreams-dev
  • 錯誤 3 、 沒有PIC1四、PIC16設備

    failed for device/lib/pic14

    解決方法:

    ./configure --disable-pic14-port --disable-pic16-port

1.三、測試

直接下載編譯好的須要將其按照說明放入系統bin等文件中,採用編譯的會自動將其放入系統bin中。而我比較喜歡不讓其和系統的各類東西糾纏在一塊兒,單獨放在工程下的一個叫tool的文件裏,大不了用的時候輸全路徑!

以下,我在example目錄下建一個makefile和a.c文件,將直接編譯好的sdcc文件放在tool下面:

➜  SDCC_WS  tree -L 2
.
├── example
│   ├── a.c
│   └── makefile
├── readme.md
└── tool
    └── sdcc

其中a.c爲51閃燈程序:

#include <8052.h>
 
void delay(void);
 
void main(void){
    while(1){
	 P1 = 0xFF; // Turn ON all LED's connected to Port1
	 delay();
	 P1 = 0x00; // Turn OFF all LED's connected to Port1
	 delay();
    }
}
 
void delay(void){
    int i,j;
    for(i=0;i<0xff;i++)
	 for(j=0;j<0xff;j++);
}

makefile爲:

SDCC_PATH=../tool/sdcc

z_sdcc=${SDCC_PATH}/bin/sdcc
z_packihx=${SDCC_PATH}/bin/packihx


a.hex:a.c
	${z_sdcc} a.c
	${z_packihx} a.ihx > a.hex

clean:
	rm -rf *.asm *.lst *.mem *.rst *.lnk *.rel *.sym *.ihx *.hex *.map *.lk *.bin

2、Hex2Bin+命令行燒寫工具配置使用

2.一、下載工具安裝配置

理論上hex也能燒寫,可是hex比bin要大一些,因此咱們用這裏用bin燒寫。hex轉bin要用到一個Hex2Bin工具,將其下載下來解壓到tool目錄下;stc單片機燒寫工具須要用一個github開源軟件stcflash,也將其下載到tool目錄下:

➜  SDCC_WS  tree -L 2
.
├── example
│   ├── a.c
│   └── makefile
├── readme.md
└── tool
    ├── Hex2bin-2.5
    ├── sdcc
    ├── software
    └── stcflash

將makefile改成:

SDCC_PATH=../tool/sdcc
HEX2BIN_PATH=../tool/Hex2bin-2.5
STCFLASH=../tool/stcflash

z_sdcc=${SDCC_PATH}/bin/sdcc
z_packihx=${SDCC_PATH}/bin/packihx
z_hex2bin=${HEX2BIN_PATH}/hex2bin
z_stcflash=${STCFLASH}/stcflash.py

a.hex:a.c
	${z_sdcc} a.c
	${z_packihx} a.ihx > a.hex
	${z_hex2bin} a.hex

clean:
	rm -rf *.asm *.lst *.mem *.rst *.lnk *.rel *.sym *.ihx *.hex *.map *.lk *.bin

flash:
	python ${z_stcflash} a.bin

2.二、編譯+燒寫

進入example目錄運行make,自動編譯:

輸入make flash,給51單片機從新上電(冷啓動)進行燒寫:

連接

@beautifulzzzz
智能硬件、物聯網,熱愛技術,關注產品
博客:http://blog.beautifulzzzz.com
園友交流羣:414948975
相關文章
相關標籤/搜索