对于编译器我喜欢用命令行模式的,因为可以写成makefile编译。基本上我所用的软件开发工具都做成这种模式,除了有些特别的开发工具比如labview。其它的像asm, C,C++等都是用编译器+makefile+notepad2。以前写windows程序这是这么玩的。
在网上找了下支持PIC 8bit单片机的免费编译器,
下载sdcc和gputils。
这两个软件都可以在sourceforge.net上下载。
首先就要研究下各种开发工具命令行模式下的参数。
像gpasm的参数:
# gpasm options: # -a FMT, --hex-format FMT Select hex file format. [inhx32] # -c, --object Output relocatable object. # -C, --old-coff Use old Microchip COFF format. # -d, --debug Output debug messages. # -D SYM=VAL, --define SYM=VAL Define SYM with value VAL. # -e [ON|OFF], --expand [ON|OFF] Macro expansion. # -g, --debug-info Use debug directives for COFF. # -h, --help Show this usage message. # -i, --ignore-case Case insensitive. # -I DIR, --include DIR Specify include directory. # -l, --list-chips List supported processors. # -L, --force-list Ignore nolist directives. # -m, --dump Memory dump. # --mpasm-compatible MPASM compatibility mode # -M, --deps Output dependency file. # -o FILE, --output FILE Alternate name of output file. # -p PROC, --processor PROC Select processor. # -P FILE, --preprocess FILE Write preprocessed asm file to FILE # -q, --quiet Quiet. # -r RADIX, --radix RADIX Select radix. [hex] # -u, --absolute Use absolute pathes. # -v, --version Show version. # -w [0|1|2], --warning [0|1|2] Set message level. [0] # -y, --extended Enable 18xx extended mode.
gplink参数:
# gplink options: # -a FMT, --hex-format FMT Select hex file format. # -c, --object Output executable object file. # -d, --debug Output debug messages. # -f VALUE, --fill VALUE Fill unused program memory with value. # -h, --help Show this usage message. # -I DIR, --include DIR Specify include directory. # -l, --no-list Disable list file output. # -m, --map Output a map file. # -o FILE, --output FILE Alternate name of output file. # -O OPT, --optimize OPT Optimization level [1]. # -q, --quiet Quiet. # -r, --use-shared Use shared memory if necessary. # -s FILE, --script FILE Linker script. # -t SIZE, --stack SIZE Create a stack section. # -u, --macro symbol[=value] Add macro value for script. # -v, --version Show version. # -w, --processor-mismatch Disable "processor mismatch" warning. #
以下是编写的makefile文件:
##########################################################################
# 配置MCU类型。必须同实际的硬件一致。
CPU = 16f877a
ARCH = pic14 # 配置外部晶振的频率。
XTAL = 12000000
##########################################################################
# 启用调式模式。
DEBUG = TRUE
# 输出文件名。
ifeq (, $(PROJECT))
PROJECT = $(ARCH)-$(CPU)
endif
TARGET = $(PROJECT)
FIRMWARE = $(PROJECT).hex
# 编译器选择。
CCNAME = sdcc-x86-3.4.0
GPNAME = gputils-x86-1.2.0
##########################################################################
# 源代码所在目录。
ROOT = .
CCROOT = \home\cc # 导出一些环境变量。
export SDCC_HOME := $(CCROOT)\$(CCNAME)
export SDCC_INCLUDE := $(SDCC_HOME)\include
export SDCC_LIB := $(SDCC_HOME)\lib
export GPUTILSDIR := $(CCROOT)\$(GPNAME)
export GPUTILS_HEADER_PATH := $(GPUTILSDIR)\header
export GPUTILS_LKR_PATH := $(GPUTILSDIR)\lkr
export GPUTILS_LIB_PATH := $(GPUTILSDIR)\lib
##########################################################################
# 获取当前目录下所有.c文件。
# 获取${src}中每个文件名的基本名称然后追加.o在每个文件名之后。
SCAN = *.c
SCAN += *.cc
SCAN += *.cpp
SCAN += *.cxx
SCAN += *.S
ifeq (mcs51, $(ARCH))
OXX = .rel
else
ifeq (pic14, $(ARCH))
OXX = .o
else
$(error CPU "$(ARCH)" dosn't support.)
endif
endif
OBJS := $(addsuffix $(OXX), $(basename $(wildcard $(SCAN))))
##########################################################################
##########################################################################
# 库搜索路径。
CCBIN = $(SDCC_HOME)\bin
GPBIN = $(GPUTILSDIR)\bin
CCINC = -I"$(ROOT)"
CCINC += -I"$(SDCC_INCLUDE)"
ifeq (mcs51, $(ARCH))
CCINC += -I"$(SDCC_INCLUDE)\mcs51"
CCINC += -I"$(SDCC_INCLUDE)\asm\mcs51"
endif
ifeq (pic14, $(ARCH))
CCINC += -I"$(SDCC_INCLUDE)\pic14"
CCINC += -I"$(SDCC_INCLUDE)\asm\pic14"
CCINC += -I"$(SDCC_HOME)\non-free\include\pic14"
endif
ifeq (pic14, $(ARCH))
ASINC = -I"$(GPUTILSDIR)\header"
endif
ifeq (mcs51, $(ARCH))
CCLIB = -L"$(ROOT)"
CCLIB += -L"$(SDCC_LIB)\small"
endif
ifeq (pic14, $(ARCH))
CCLIB = -I"$(ROOT)"
CCLIB += -I"$(SDCC_LIB)\pic14"
CCLIB += -I"$(SDCC_HOME)\non-free\lib\pic14"
endif
##########################################################################
# 编译器路径。
CC = "$(CCBIN)\sdcc"
CP = "$(CCBIN)\sdcpp"
LIB = "$(CCBIN)\sdcclib"
HEX = "$(CCBIN)\packihx"
SIZE = size
ifeq (mcs51, $(ARCH))
AS = "$(CCBIN)\sdas8051"
LD = "$(CCBIN)\sdcc"
SIM = "$(CCBIN)\s51"
endif
ifeq ("$(ARCH)", "pic14")
# 汇编器执行文件。
AS = "$(GPBIN)\gpasm"
# 连接器执行文件。
LD = "$(GPBIN)\gplink"
# 模拟器执行文件。
SIM = gpsim
endif
OCFLAGS = -o
OLFLAGS = -o
CCFLAGS = --std-sdcc99
CCFLAGS += --fverbose-asm
CCFLAGS += -D_XTAL=$(XTAL)
CCFLAGS += -D_$(ARCH)_
CCFLAGS += -D_$(CPU)_
CCFLAGS += -p$(CPU)
CCFLAGS += -m$(ARCH)
CCFLAGS += --vc
ifeq (pic14, $(ARCH))
ASFLAGS = -c
ASFLAGS += -i
ASFLAGS += -w0
# 仅编译。
CCFLAGS += -S
# 使用non-free库。
CCFLAGS += --use-non-free
CCFLAGS += --nojtbound
# CCFLAGS += --nolabelopt
# 输出格式。
LDFLAGS = -ainhx32
LDFLAGS += -f255
ifeq (TRUE, $(DEBUG))
ASFLAGS+=
# CCFLAGS+= --debug
CCFLAGS+= --Werror
CCFLAGS+= --debug-xtra
LDFLAGS+= -m
else
ASFLAGS+=
CCFLAGS+= --peep-asm
CCFLAGS+= --opt-code-speed
LDFLAGS+= -O3
endif
LDFLAGS += -s"$(GPUTILS_LKR_PATH)\$(CPU)_g.lkr"
SMFLAGS = -s $(TARGET).cod
endif
all: clean build
sim: clean build
# 当编译完成后开始运行模拟器。
$(SIM) $(SMFLAGS)
%.rel: %.c
$(CC) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@ $<
%.rel: %.cc
$(CC) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@ $<
%.rel: %.cpp
$(CP) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@ $<
%.rel : %.cxx
$(CP) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@ $<
%.rel: %.asm
$(AS) $(ASFLAGS) $(ASINC) $(OCFLAGS)$@ $<
%.rel: %.S
$(AS) $(ASFLAGS) $(ASINC) $(OCFLAGS)$@ $<
%.o: %.c
$(CC) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@.as $<
$(AS) $(ASFLAGS) $(ASINC) $(OLFLAGS)$@ $@.as
%.o: %.cc
$(CC) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@.as $<
$(AS) $(ASFLAGS) $(ASINC) $(OLFLAGS)$@ $@.as
%.o: %.cpp
$(CC) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@.as $<
$(AS) $(ASFLAGS) $(ASINC) $(OLFLAGS)$@ $@.as
%.o : %.cxx
$(CC) $(CCFLAGS) $(CCINC) $(OCFLAGS)$@.as $<
$(AS) $(ASFLAGS) $(ASINC) $(OLFLAGS)$@ $@.as
%.o: %.asm
$(AS) $(ASFLAGS) $(ASINC) $(OLFLAGS)$@ $<
%.o: %.S
$(AS) $(ASFLAGS) $(ASINC) $(OLFLAGS)$@ $<
build: $(OBJS)
$(LD) $(LDFLAGS) $(CCLIB) $(OLFLAGS)$(FIRMWARE) $(OBJS) $(ADDLIB)
@gecho
$(SIZE) $(FIRMWARE)
@gecho