Ubuntu下STC89C52RC开发环境的建立
What it is, how to set up a development environment on Linux (Ubuntu), a sample software and some basic explanations.
[TODO: put photos, format, add links to doc etc.]
As I was still in China, and electronic stuffs being far cheaper there than in Europe for some days I’ve bought with a friend a HOT-51 board with a STC 89C52RC MCU on it (8052 family)
First I know really few things about MCU, embedded programming etc. so please notice it to me if I’m misunderstanding some notions.
The pack we’ve bought contain the following:
the HOT-51 board (8 digits leds, 8 led, a 8×8 matrix of leds, a 4×4 matrix of button, a stuff to play sound)
a stuff to measure the temperature,
a 2×16 characters screen,
2 motors
a remote
an usb to RS232 cable an usb cable (for alimentation)
the MCU on it is a STC89C52 as said before.
The main goal for us is to have fun while learning the basic of embedded programming, in order to, if we enjoy it, buy after more complex board to start doing concrete software.
setting up a dev environment on Linux
We need 3 things
- a text editor (let’s say vim ;-))
- a compiler (will be sdcc)
- an ISP (it will be gSTCISP)
how to get them
For the compiler it’s pretty simple
apt-get install sdcc
and you’re done
For gSTCISP well you first need to grab the source here
http://sourceforge.net/projects/gstcisp/files/
you will need, in addition to the gtk-dev packages, the libvte
apt-get install libvte-dev
then you can uncompress the archive, run the ./configure, after you will need to modify the Makefile in the src directory (I know it’s bad and it should be possible to do it in the makefile.am but I don’t want to search, if someone want to propose the “good way” in comments feel free).
in the Makefile replace the line 72 (which start by CFLAGS) by
CFLAGS = -g -O2 -I/usr/include/vte-0.0/
otherwise the make will complain with a :
main.c:25:21: fatal error: vte/vte.h: No such file or directory
once you’ve finished the make, you can run it with
./gSTCISP
and voila, now you’re ready to code 🙂
a sample software
the document being in Chinese, it’s a bit hard to understand, so here is a sample software that count to 99 with a delay of 1 second using the hardware interruption
#include "8052.h" typedef unsigned int uint; typedef unsigned char uchar; uchar timeTemp = 0; uchar time[8] = {0,0,0,0,0,0,0,0}; void add_one_sec() { // 7 is the top right digit // and 0 the top left one // TODO by inverting the LED position array // should be possible to make it more natural // (0 for the top right one) ++time[7]; time[7] %= 10; // if we were at 000X9 + 1 // the we add one to X if (time[7] == 0) { ++time[6]; time[6] %= 10;// when we reach 00 again if (time[6] == 0) { // we stop the timer - TR0 = 0; } } } // initialize timer 0 void time0_init () { // activate 16 bit timer mode for timer 0 TMOD = 0x01;// enable all interruption // and enable the interrupt 1 (timer 0) IE = 0x82; // 1000 0010 // set the 16 bits register to a delay of 0.05 sec TH0 = 0x3c; TL0 = 0xb0; TR0 = 1; //start timer 0 } // will be called each time the interruption 1 // is raised void time0_interrupt() interrupt1 { // reset the counter to interrupt in 0,05s // with the 11.0592 Xstal TH0 = 0x3c; TL0 =0xb0; // every 4 interrupts we add one to the LEDs digits timeTemp++; // 20 * 0.05s = 1s if(timeTemp == 20) { add_one_sec(); timeTemp = 0; } } int main() { uchar i = 0; // array representing the numbers // from 0 to 9 with the leds // each bits position is linked to one // of the leds used to form the digit // by playing with it you should be able // to make letters etc. uchar code numbers[10] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,0x07, 0x7f, 0x6f }; // LED position uchar code LED_W[8] = {0,1,2,3,4,5,6,7}; time0_init(); while(1) { // we refresh the value of each LEDs // in sequential order, it should be enough // fast to give the impression of being simultanate P0 = numbers[time[i]] ; P2 = LED_W[i]; i = (i + 1)%8; } }
the comments should be enough to understand how it works (if you’ve some basic knowledges in embedded programming)
compile it and run it on the board
compile
to compile it and turn it into a .bin here is the command
sdcc main.c && makebin -p < main.ihx > toto.bin
you can replace main.c by the name of your .c file (you will then to change main.ihx too)
run
now you need to load it on the MCU to do this, run gSTCISP (as root), connect your board to your computer using the RS232 to usb cable, also plug the USB-USB cable for power supply (if you don’t have an external one)
choose /dev/ttyUSB0 (or 1) , and 4800 bauds
select your .bin file and then nearly at the same time, click on “download” and press the power on button of your board (well I admit it’s ), if it says “We are trying to connect to your MCU …” , just stop the download, power off your board and repeat (do some black magic tricks etc.)
and here you are, now you’re able to hack this sample and load anyway .bin you will create/found.
More to come…
更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2
玩玩单片机
在淘宝上买了块天祥电子的51单片机玩玩,折腾了好久,终于在linux上搭建好了开发平台。我发现网上介绍linux中编写51单片机的流程很凌乱,所以下面做一下汇总。
我用的是Ubuntu,首先用apt-get下载sdcc,这是一个编译器,安装很顺利。下面写一段简单的代码。如下,抱歉排版原因没有缩进:
#include <mcs51/8051.h>
void delay02s(void){
unsigned char i,j,k;
for(i=20;i>0;i–)
for(j=20;j>0;j–)
for(k=248;k>0;k–);
}void main(void){
while(1){
P1_0=0;
delay02s();
P1_0=1;
delay02s();
}
}
注意这里的#include <mcs51/8051.h>。和Keil不同。接着编译命令很简单:
sdcc light.c
编译成功后会生成”light.ihx”。然后,找到一个叫”hex2bin”的软件,地址在戳。注意不是“hextobin”,那是另一个很搓的软件。让我走了很多弯路。
“hex2bin”要从源代码编译安装,还算简单。安装完后运行:
hex2bin light.ihx
它会生成light.bin。到这一步编译都完成了,就差把bin文件刷入单片机了。
找到一个叫“gSTCISP”的软件。这个软件编译安装的时候比较麻烦。需要手工改Makefile。
把单片机的电源关掉,把串口接上电脑,然后以管理员身份运行gSTCISP,如图:
选择好bin文件。然后点击DownLoad,然后打开单片机电源。bin文件就会自动刷到单片机里去,并开始运行。
注意:
1、修改 “ in the Makefile replace the line 72 (which start by CFLAGS) by
CFLAGS = -g -O2 -I/usr/include/vte-0.0/ ” 时,有两个文件需要修改: gSTC-ISP/src/Makefile 和gSTC-ISP/Makefile
2、gSTC-ISP_v1.0.tar.gz的安装
大多以tar.gz 和tar.bz2打包软件,大多是通过 ./configure ;make ;make install 来安装的
(三)开发步骤:
1、使用编辑器写 main.c 文件,注意这里的#include <mcs51/8051.h>,还有将 P1^1 改为 P1_1 ,和Keil不同
2、通过 sdcc main.c 编译命令后会生成 main.ihx文件
3、通过 makebin -p < main.ihx > main.bin 命令后生成.bin文件,因为gSTCISP下载要求的格式是bin
4、使用 sudo gSTCISP 命令下载程序,注意以管理员身份运行gSTCISP。