Ubuntu下STC89C52RC开发环境的建立

UbuntuSTC89C52RC开发环境的建立

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 8x8 matrix of leds, a 4x4 matrix of button, a stuff to play sound)
a stuff to measure the temperature,
a 2x16 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

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.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/18908.html