在Linux上快速搭建8051开发环境(2018-7-2)
0. 写在开始之前
实验操作系统环境: Ubuntu 16.04
编译器: sdcc
SDCC - Small Device C Compiler是一个开源的编译器,可以去官网查看它的详细信息。sdcc提供包括linux, Windows, Mac OS X的版本。
stcflash是一个开源python烧录脚本,可以非常好的支持STC系列的单片机,由于没有其他品牌的单片机所以无法保证所有51内核的单片机都可以使用。
因为这个脚本是python编写的所以windows需要安装python环境,而linux系统一般都自带python。Python版本必须高于2.6。
由于Python没有自带pySerial模块,但是烧录工具需要使用这个模块所以需要手动安装,在Ubuntu可以使用apt安装:
$ sudo apt-get install python-serial
1. 编译器安装
源码编译(可选)
若有兴趣,精力,时间可以选择源码编译安装,本咸鱼在此不多阐述。
下载二进制包
根据具体情况下载,64位linux系统可以下载Linux on AMD64 Sempron(amd64-unknown-linux2.5)
解压到合适的目录(/opt/)
解压到自己满意地方,我比较中意/opt目录:
$ sudo tar -xjvf sdcc-snapshot-amd64-unknown-linux2.5-20180701-10463.tar.bz2 -C /opt/
添加环境变量
如果不添加环境变量,每次编译器的时候都需要输入绝对路径比较麻烦。
修改~/.bashrc来添加环境变量:
$ echo "export PATH=/opt/sdcc/bin:$PATH" >> ~/.bashrc
!PATH中的路径应该是自己解压二进制包的地方。
刷新bash:
$ source ~/.bashrc
检查是否安装成功:
$ sdcc --version
SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8 3.7.1 #10463 (Linux)
published under GNU General Public License (GPL)
如果有以上信息提示,则表明安装完成。
2. 编写测试代码
在/sdcc/share/sdcc/include/mcs51有很多定义好的头文件,我们使用8052.h可以满足一般的需要了。
8052.h
/*-------------------------------------------------------------------------
8052.h: Register Declarations for the Intel 8052 Processor
Copyright (C) 2000, Bela Torok / bela.torok@kssg.ch
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING. If not, write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
As a special exception, if you link this library with other files,
some of which are compiled with SDCC, to produce an executable,
this library does not by itself cause the resulting executable to
be covered by the GNU General Public License. This exception does
not however invalidate any other reasons why the executable file
might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/
#ifndef REG8052_H
#define REG8052_H
#include <8051.h> /* load definitions for the 8051 core */
#ifdef REG8051_H
#undef REG8051_H
#endif
/* define 8052 specific registers only */
/* T2CON */
__sfr __at (0xC8) T2CON ;
/* RCAP2 L & H */
__sfr __at (0xCA) RCAP2L ;
__sfr __at (0xCB) RCAP2H ;
__sfr __at (0xCC) TL2 ;
__sfr __at (0xCD) TH2 ;
/* IE */
__sbit __at (0xAD) ET2 ; /* Enable timer2 interrupt */
/* IP */
__sbit __at (0xBD) PT2 ; /* T2 interrupt priority bit */
/* T2CON bits */
__sbit __at (0xC8) T2CON_0 ;
__sbit __at (0xC9) T2CON_1 ;
__sbit __at (0xCA) T2CON_2 ;
__sbit __at (0xCB) T2CON_3 ;
__sbit __at (0xCC) T2CON_4 ;
__sbit __at (0xCD) T2CON_5 ;
__sbit __at (0xCE) T2CON_6 ;
__sbit __at (0xCF) T2CON_7 ;
__sbit __at (0xC8) CP_RL2 ;
__sbit __at (0xC9) C_T2 ;
__sbit __at (0xCA) TR2 ;
__sbit __at (0xCB) EXEN2 ;
__sbit __at (0xCC) TCLK ;
__sbit __at (0xCD) RCLK ;
__sbit __at (0xCE) EXF2 ;
__sbit __at (0xCF) TF2 ;
#endif
LED程序
我们可以编写自己的LED程序了。
在任意地方创建test目录并进入,然后创建main.c文件:
main.c
#include "8052.h"
#define LED P1_0
void main()
{
LED = 0; // or LED = 1
while (1) {
}
}
sdcc和keil编译器不同,所以定义的语法有些差别,在中断调用方面也有细微的差别,可以从官方文档了解具体信息。
编译
确保在test目录下:
$ sdcc main.c
$ ls
main.asm main.ihx main.lst main.mem main.rst
main.c main.lk main.map main.rel main.sym
编译完成后会生成很多文件,其中main.ihx是我们需要的。
3. 烧写程序
下载python脚本,并安装好依赖,
或者可以直接复制脚本代码。
在test目录下创建stcflash.py:
#!/usr/bin/env python
# stcflash Copyright (C) 2013 laborer (laborer@126.com)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
import time
import logging
import sys
import serial
import os.path
import binascii
import struct
import