Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
a)
Chapter 4:
The C programming
environment for the
8051
Objectives
z Learn about how to develop a micro-controller based (embedded)
system.
z Learn to use a monitor program (PAULMON) for our 8051 system
z Lear to use a “C” compiler to develop application programs for our
8051 system
4.1. Introduction:
In this chapter we introduce methods for developing micro-controller based (or embedded)
systems. Then we will discuss how to setup a C programming environment for the micro-
controller 8051.
Why do we need to have a C programming environment for our robot development work?
Of course, we need a programming language, but can we use assembly language or even
BASIC? Actually I tried both and gave me headache that still aches today. Assembly is
fast to run but very difficult to use, you need to be a half genius to use it efficiently. If
your work is simple it may be an ideal choice, but as your program grows larger, it will
become difficult to trace, debug and to read too. Since we are about to develop
intelligence robots not dump washing machines, the assembly language option is out of
the question. I also used BASIC52 before, which is a public domain monitor program
produced but not supported by Intel (a sort of computer orphan). But it also gave me a
bad experience because it doesn’t support symbolic subroutine calling. That means you
cannot label an instruction by a symbol; you can only call a subprogram by its location
number – structural programming becomes very difficult to write. So I determined to use
C.
1
Mobile Robot chapter 4: C and 8051 (v4.a)
Step1: PC With a
EPROM cross-compiler to
programmer to generate object
write object file code for 8051
into an
EPROM
Step2:
Produced EPROM
8K-byte
e.g.2764 Step3: Place
the EPROM
to the slot by
hand
8051 SBC
EPROM
slot
3. EPROM emulator based approach. This approach is the same as EPROM based
development but use EPROM emulator hardware for rapid downloading and testing.
An EPROM emulator is a RAM sitting between a PC and a SBC. The RAM can be
2
Mobile Robot chapter 4: C and 8051 (v4.a)
Step1: switch
SRAM to PC
Step3:
In the 8051 PC ,
Step3: Switch
SBC, execute Step1: compiles
SRAM to SBC
program in program,
RAM (by generates object
pressing reset if code and saves in
RAM is at SRAM
0000 onwards)
SRAM e.g.62256
Exercise 4. 1 :Show how to use an EPROM emulator to write program and run on our
SBC?
• You first boot up your computer in Windows or something else if you don’t
want to associate yourself with Bill.
• Then you start a compiler, edit your program, and then run your executable code
from the file with a file extension of .exe within the Windows environment.
Exercise 4. 2: Can you build a compiler at your SBC and develop your code solely on the
SBC only just like a PC?
Answer: Basically yes, but since the SBC has little computation power and has no mass
storage (hard disk or floppy disk) for storing programs, it is not easy to have it in reality.
So the following is the solution.
• The trick we used here is. Since your SBC has a serial port and connects to the
host PC, you can utilize your PC to store programs and do the compilation. After
the object code is produced, you can transfer it to the SBC to run it. To use this
scheme you need two things: (1) a program on the SBC to communicate with the
host computer (2) a RAM space to store the object code and use the monitor
program to run this object code from. In the last chapter we discussed that the
address space of 8000H is dedicated for this function. The monitor program we
used is also a public domain program called Paulmon2, which will be discussed
below.
3
Mobile Robot chapter 4: C and 8051 (v4.a)
RS232
8031 SBC Step1: PC:
generate object
Step3: long
jump to the code
Step2: Use
RAM space serial link to
download
RAM at program to
8000H RAM
2. SDCC: A C cross-compiler for the 8051, the word “cross” means you are using an
80x86 processor of your PC to compile your source to become not 80x86 code but
8051 code.
8031 SBC
Step4: use “run”
command of
“Paulmon2” to run
PC windows running “HyperTerminal”
code from 8000H
(SRAM space)
Figure 4. 5 To show relations of the sofware systems used, and the steps of the monitor
development method
1. Start the SBC computer and its serial interface with the PC, which may use terminal
emulator software (i.e. Hyper-terminal in Windows), for the communication. By
4
Mobile Robot chapter 4: C and 8051 (v4.a)
using this serial link the user can use a number of commands from the keyboard of
the PC to instruct the SBC to execute certain functions. Which includes:
• List the code in the SBC memory space onto the screen of the PC. You may
view the code in hex format or dis-assemble format – it has a dis-assembler to
turn the object code in memory to assembly language.
• Jump to the starting address at any program starting locating as the user wishes.
• At the PC, the programmer can write his program using an editor and compile
the C source with a compiler, such as the SDCC. The output of the compiler can
be an object file in the Intel hex format.
• Use the download command of the monitor program to transfer the object code
from the PC to the data/program RAM space (say 8000H), if this space is
common to data and program, the monitor program can place the object code
received to the space as if they are data.
• From the PC hyper terminal, issue a run command to execute the code from the
address location of the testing program.
The advantage of using this monitor program approach is that there is no extra
hardware needed, you only need a SBC, a serial cable and a PC.
So the required public domain tools have been discussed. We will then describe the
installation procedures of the software and give a number of experiments for testing our
system.
If you are using the Windows binary version of the SDCC, skip the following section.
All the links can be found at the course web page under
http://www.cse.cuhk.edu.hk/~ceg3430
Installation of CYGWIN:
5
Mobile Robot chapter 4: C and 8051 (v4.a)
1. Download from:
http://sources.redhat.com/cygwin/
2. Follow the instruction for installation
(save you US$2000!! a bit exaggerating, I confess. But by saying that it should
make you happier, I am sure)
6
Mobile Robot chapter 4: C and 8051 (v4.a)
4. The complete manual for SDCC and the use of its compile options can be found at C:\sdcc\doc
after installation.
int i,a,temp;
xdata unsigned char *p8255_cnt=0xe003;
xdata unsigned char *p8255_p0=0xe000;
main()
{
//INIT 8255
*p8255_cnt=0x89; //set 8255 control word, portA=out
for(i=0;i<10;i++)
{
for(a=0;a<100000;a++)
{
*p8255_p0=0x0f; //set 0x0f as output pattern at portA
}
for(a=0;a<100000;a++)
{
*p8255_p0=0xf0; //set 0x0f as output pattern portA
}
}
}
Now you will have a set of output files :
Test1.asm (assembly program), test1.ihx (executable 8051 Intel hex file format) etc.
7
Mobile Robot chapter 4: C and 8051 (v4.a)
? - Help
M - List programs
R - Run program
D - Download
U - Upload
N - New location
H - Hex dump memory
I - Hex dump internal memory
E - Editing external ram
C - Clear memory
8. Now you are back to Windows-Hyper terminal, select transmit “ASCII” for sending
test1.ihx
9. You will see series of dots “…..”, then get a message of download complete
10. Since the code is actually in 0x8000, so you can read the code or assembly
(disassembled by Paulmon2 I think), try “N” 8000; then “L” for viewing assembly
code , or “H” for viewing hex code.
11. Use “j” 8000 to jump to the start address of the executable code test1.ihx you just
downloaded.
12. The LED of the 8051-sbc should blink and after a few seconds when the program
terminates, Paulmon2 goes back to its starting point again.
13. Done.
8
Mobile Robot chapter 4: C and 8051 (v4.a)
4.14. Conclusion
Various embedded system development techniques have been studied. In particular, the
way of how to setup a low cost 8051 monitor-based C development has been described.
Experiments for students to get familiar with such a system have been designed and
discussed in this chapter
4.15. References
1. Official home of SDCC is at http://sdcc.sourceforge.net/
2. CYGWIN is at http://sources.redhat.com/cygwin/
3. SDCC http://sdcc.sourceforge.net/
9
Mobile Robot chapter 4: C and 8051 (v4.a)
10