系列说明:Linux下程序开发具有一定的成熟性,包括大部分的MCU,FPGA,DSP甚至PCB制图等都是可行的。本系列将针对51系列的MCU.。
这篇文章环境如下:
OS系统:Ubuntu 12.04
编译器 :sdcc
烧录软件 :avrdude
烧录器件 :usbasp
开发板:开发板的确是有个小的,不过这次特意搭接了一个个人的小面包板版本,为此表示~不要怕开发MCU没有硬件基础,只要去做,其实开发就是这么容易。
最好弄运行的时候只需要电池1.2V*4,用了3个3.6V就可行(我的电池座忘带了,直接连起来啦)~建议最好有个USB接出来的5V电源或者自己有个变压器做的稳压电源,面包板上有一个电源小板子就是如此功能,不过这里没有使用。
注:旁边的小板子是USBASP,也有供电功能~此处就放在那里做个合影~哈~
程序编写:Vim(升级版的记事本,很好用,很推荐)
准备:
软件安装,软件安装建议使用ubuntu的软件中心,比较方便。需要sdcc,avrdude即可了。文本编辑什么都行。可以集成在codeblocks和eclipse里面(51没干过),ubuntu下有个51MCU的IDE,可以下来用‘mcu8051ide’ 。
对于命令行可以如下安装
sudo apt-get install vim
sudo apt-get install avrdude
然后就都安装完毕了。下一步就可以开始了。
源程序:
开始之前需要先写一个main.c的程序
这是一个很简单让一个blink的程序。
#include <8052.h>
typedef unsigned int size_t;
#define LED P0_0
void delay(size_t t)
{
while(t--);
}
void main()
{
while(1)
{
LED = 0;
delay(20000);
LED = 1;
delay(20000);
}
}
编译:
之后就要编译程序把程序变成单片机能使用的*.elf *.bin *.hex之类的
这里大家使用的是SDCC默认编译出现的*.ihx (intel hex)
在当前目录下执行
sdcc -mmcs51 main.c
注:没有-mmcs51也可以。
当面目录下就会生成好多好多文件~记住大家需要的main.ihx~
配置avrdude:
使用avrdude烧写
大家不能直接使用avrdude烧写,这是因为它默认只支持AVR芯片,但是通过一些配置,大家可以使用它来烧写。
对于8051芯片,大家有三种配置相关文件,分别适用于不同的型号。看看你要哪一种,然后复制到/etc/avrdude.conf并保存。
(sudo gedit /etc/avrdude.conf)
For AT89S51
#------------------------------------------------------------
# AT89S51
#------------------------------------------------------------
part
id = "8052";
desc = "AT89S51";
signature= 0x1E 0x51 0x06;
chip_erase_delay = 500000;
pgm_enable = "1 0 1 0 1 1 0 00 1 0 1 0 0 1 1",
"x x x x x x x xx x x x x x x x";
chip_erase = "1 0 1 0 1 1 0 01 0 0 x x x x x",
"x x x x x x x xx x x x x x x x";
timeout = 200;
stabdelay = 100;
cmdexedelay = 25;
synchloops = 32;
bytedelay = 0;
pollindex = 3;
pollvalue = 0x53;
predelay = 1;
postdelay = 1;
pollmethod = 0;
memory "flash"
size= 4096;
paged = no;
min_write_delay = 4000;
max_write_delay = 9000;
readback_p1 = 0xff;
readback_p2 = 0xff;
read= " 0 0 1 00 0 0 0",
" x x x a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" o o o oo o o o";
write = " 0 1 0 00 0 0 0",
" x x x a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" i i i ii i i i";
mode = 0x21;
delay = 12;
;
memory "signature"
size= 3;
read= "0 0 1 0 1 0 0 0 x x x 0 0 0 a1 a0",
"0 0 0 0 0 0 0 0 o o o o o o o o";
;
;
For AT89S52
#------------------------------------------------------------
# AT89S52
#------------------------------------------------------------
part
id = "8052";
desc = "AT89S52";
signature= 0x1E 0x52 0x06;
chip_erase_delay = 500000;
pgm_enable = "1 0 1 0 1 1 0 00 1 0 1 0 0 1 1",
"x x x x x x x xx x x x x x x x";
chip_erase = "1 0 1 0 1 1 0 01 0 0 x x x x x",
"x x x x x x x xx x x x x x x x";
timeout = 200;
stabdelay = 100;
cmdexedelay = 25;
synchloops = 32;
bytedelay = 0;
pollindex = 3;
pollvalue = 0x53;
predelay = 1;
postdelay = 1;
pollmethod = 0;
memory "flash"
size= 8192;
paged = no;
min_write_delay = 4000;
max_write_delay = 9000;
readback_p1 = 0xff;
readback_p2 = 0xff;
read= " 0 0 1 00 0 0 0",
" x x x a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" o o o oo o o o";
write = " 0 1 0 00 0 0 0",
" x x x a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" i i i ii i i i";
mode = 0x21;
delay = 12;
;
memory "signature"
size= 3;
read= "0 0 1 0 1 0 0 0 x x x 0 0 0 a1 a0",
"0 0 0 0 0 0 0 0 o o o o o o o o";
;
;
For AT89S8253
#------------------------------------------------------------
# AT89S8253
#------------------------------------------------------------
part
id = "8253";
desc = "AT89S8253";
chip_erase_delay = 20000;
pgm_enable = "1 0 1 0 1 1 0 00 1 0 1 0 0 1 1",
"x x x x x x x xx x x x x x x x";
chip_erase = "1 0 1 0 1 1 0 01 0 0 x x x x x",
"x x x x x x x xx x x x x x x x";
timeout = 200;
stabdelay = 100;
cmdexedelay = 25;
synchloops = 32;
bytedelay = 0;
pollindex = 3;
pollvalue = 0x53;
predelay = 1;
postdelay = 1;
pollmethod = 0;
memory "flash"
size= 12288;
paged = no;
min_write_delay = 4000;
max_write_delay = 9000;
readback_p1 = 0xff;
readback_p2 = 0xff;
read= " 0 0 1 00 0 0 0",
" x x a13 a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" o o o oo o o o";
write = " 0 1 0 00 0 0 0",
" x x a13 a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" i i i ii i i i";
mode = 0x21;
delay = 12;
;
memory "signature"
size= 2;
readback_p1 = 0x1E;
readback_p2 = 0x73;
read= "0 0 1 0 1 0 0 0 x x x x x x x x",
"x x 1 1 0 0 0 a0 o o o o o o o o";
;
;
特意要说明一下的是,网上有些配置在S51和S52的delay的参数上给的20000,这个delay太短了,在验证代码的时候会出错。因此还是建议用如上的配置。
使用avrdude烧写:
配置完成之后,就可以烧写啦~要用main.hex哦~
monkey@monkey-ThinkPad-E425:~/McuWork/51_SDCC/workpace/Blink$ make program
sudo avrdude -p 8052 -c usbasp -e -U flash:w:'./main.ihx'
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions
Reading | #################### | 100% 0.01s
avrdude: Device signature = 0x1e5206
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "./main.ihx"
avrdude: input file ./main.ihx auto detected as Intel Hex
avrdude: writing flash (140 bytes):
Writing | ####################### | 100% 1.69s
avrdude: 140 bytes of flash written
avrdude: verifying flash memory against ./main.ihx:
avrdude: load data flash data from input file ./main.ihx:
avrdude: input file ./main.ihx auto detected as Intel Hex
avrdude: input file ./main.ihx contains 140 bytes
avrdude: reading on-chip flash data:
Reading | ###################### | 100% 0.57s
avrdude: verifying ...
avrdude: 140 bytes of flash verified
avrdude: safemode: Fuses OK
avrdude done. Thank you.
monkey@monkey-ThinkPad-E425:~/McuWork/51_SDCC/workpace/Blink$
搞定哈~
选项说明(Options):
-p specifies the type of the MCU connected to the programmer.
-c specifies the default programmer
-e causes a chip erase to be executed.
-U memtype:op:filename
The op field specifies what operation to perform:
r read device memory and write to the specified file
w read data from the specified file and write to the device memory
v read data from both the device and the specified file and perform a verify
最后难免会有问题出现,遇到问题就慢慢解决喽~USBASP使用前请用lsusb命令确认识别。USBASP固件在附传~在windows下也有相关的USBASP使用方法在这里就不多说了。
我也做过一个。不过自己做的腐蚀板子出来的相对看很丑啊~~我也出去开板子做了就2个了,一旦大连理工的同学想要的话可以联系下(空板)。(这个USBASP网上买都不到10元~感谢S51系列的在线编程吧,可别买成C51了,那就乖乖用并口或其他烧录吧~至于STC的可以用串口烧录,那就更方便了,不过STC烧录软件没有linux版本的。遗憾了。)
附录:
单源文件简易Makefile
make : 编译
make hex : 生成*.hex
make program : 烧录
#==============================
# FileName: Makefile
# Desc: sdcc signal file
# Author: Galaxy2416(Gin)
#Email: sunxiao.gin@gmail.com
# Compiler: sdcc
# Version: 0.0.1
# LastChange: 2012-05-19 16:30:35
# History:
#================================
CC=sdcc
SOURCE = $(wildcard *.c)
all :
$(CC) -mmcs51 $(SOURCE)
hex:
packihx '$(wildcard *.ihx)' > '$(patsubst %.ihx,%.hex,$(wildcard *.ihx))'
program :
sudo avrdude -p 8052 -c usbasp -e -U flash:w:'./$(wildcard *.ihx)'
.PHONY: clean
clean:
rm -f *.bak
rm -f *.rel
rm -f *.asm
rm -f *.lst
rm -f *.sym
rm -f *.map
rm -f *.lnk
rm -f *.mem
rm -f *.i
rm -f *.d
rm -f *.rst