Written By: SweePoh, Quek (Kevin)
Testech Electronics Pte Ltd. Blk 118, #05-118, Aljunied Ave 2, Singapore 380118
Tel: (+65)-67492162 Fax: (+65)-67494246. Email: email@example.com Web: www.testech-elect.com
IAR EW8051 V7.20C and above, supports many variant of the 8051, JTAG Debug
such as Silicon Lab USB JTAG Probe, and interface to Real Time In Circuit
Emulator such as ME52HU REal Time ICE. In addition, for practicing Engineers,
School and Students pursuing a course in 8051 microcontroller hardware
interfacing and programming, a complete 8051 Laboratory training board witn Lab
session and source code base on the IAR EW8051 is now available. This Lab51
board can be interface to the PC directly for debug using the IAR CSPY Debugger
CSPY ROM: using serial Port connecting to the Lab51 Board, for code download and HLL Debugging.
ME52HU: Real Time In Circuit Emulator that plug into the target CPU socket and non intrusive Real Time Debugging and Hardware Trace support.
This quickstart tutorial get you started quickly on creating a new project for 8052/8032 for Assembly Language, by creating a complete new project
1. IAR EW8051 V7.20D (this already include a Software Simulator with Peripheral Simulator for running on PC only)
2. Lab51 Experiement Board (if you wants to run the code on the target board)
3. ME52HU Real Time In Circuit Emulator, DLL Interface to IAR CSPY Debugger. (if you want to debug the code on the target board)
For debugging using CSPY Simulator, you can directly simulate your code on the PC.
For debugging using ME52HU Real Time In Circuit Emulator: Remove the 80C52/80C52 Microcontroller from the LAB51 CPU socket, and insert the ME52 Probe card into the socket. Then connect the ME52 Probe card to the Emulator Base, Connec the the Emulator base to the PC Parallel Port for debugging. Set the Host PC to EPP Mode (for most clone PC, boot the PC, and press delete key when the POST message shows, then set the PC LPT mode to EPP mode).
For debugging using CSPY ROM Monitor. This requires a different board that has the target board implement the Von Neuman hardware memory map scheme that map both the code and xdata space to the target external SRAM, contact us for this options.
Basic Steps of Embedded Development Using IAR EW8051
For beginner to Embedded Microcontroller Development (as oppose to PC Desktop
Application Development), the embedded development requires at least 3 setps
using the IAR Components belows:
IDE: IAR Embedded WorkBench IDE consist of a Project Management (that setup and maintain your project, target processor compile/assembler options, Linker Command File, Simulator or Debug Options and a integrated Editor with Syntax coloring. The Editor are built for embedded C/C++/ASM environment with brace matching, Source Code Browser. A WorkBench is to be created, and this workbench is a container for multiple projects that user created for each embedded projects.
C/C++ Compiler/Assembler: You can write your code in combination of Assembler, C, C++. IAR EW8051 support MISRA C (a standard of coding for automotive). You can create code for single chip, or External Flash, or external RAM code execution
Linker/Locator: IAR EW8051 uses XLINK linker/Locator. You specify your link options (that is how segment (code/data/xdata/pdata/no_init) are physically located in target physical memory space.
Simulator: The EW8051 edition has built in cycle accurate Software Simulator with Peripheral Simulator, you can simulate your code on the PC, very useful to test algorithm and doing code benchmarking. Macro are supported to add peripheral simulations.
Debugger: The EW8051 support several chip vendor JTAG (Hardware Debug Probe that allows directly debug on certain enhanced 8051 microcontroller with JTAG Debug Core on chip). In addition, ManLey ME51H Real Time In circuit Emulator supports the IAR CSPY Debugger directly (that is you can debug using IAR CSPY Debugger environment). Using CSPY Debugger allows you to stays within the same environment, and allows you code browser, and edit while you debug.
UML: This is optional component known as Visual State. It is a 4th generation tools, that allows you to design your system software arhitecture using GUI environment and Statechart, you can create statechart, simulate the statechart, as well as download and do target debugging within this GUI environment. Using UML embedded statechart isolate the system sofware architecture from the low level coding. Of course you need to implement the actual hardware driver that will be called by the UML hierachy IO blocks.
RTOS: You can code optionally using a third party or your own roll out Real Time RTOS, Built in Kernel Aware feature supports CMX, UCOS-II etc. With IAR built in RTOS Aware, you can see your RTOS Thread, Semaphore, Queues, Events etc Kernel Object during debugging in the Kernel Information Windows.
Creating a New Workspace
Create a new directory in your C or D drive under a directory name LAB51, for
The directory structure are:
D:\LAB51 : Parent Directory
D:\LAB51\ASM: All ASM Project Source Codes
D:\LAB51\C: All C Project Source Code
The IAR Workspace can contains multiple projects, for example you can have a project with variant for Debug using CSPY Simulator, Debug using CSPY ROM Debugger and Debug using Real Time ICE directly from CSPY Debugger. You can also can have a project variant for release mode, that has high optimisation and generate Hex code for output that you can program to your Microcontroller using IC Programmer, or ISP Programmer.
In the EW8051, click on File -> New -> WorkSpace. Save this workspace later under the name of LAB51ASM Workspace. Later we shall create multiple project under this workspace. We will save all asssembly project and workspace in D:\LAB51\ASM and all C/C++ Project in D:\LAB51\C
Create a New Assembly Project
Select Project -> Create New Project. Select the empty ASM Project. Name the project as H1 (an extension ewp will be added automatically),
Noted that a empty assembly shell assembly file asm.s51 will be created for you.
Looking at the above empty shell asm project below is a brief explanation.
Each Assembly File module can have a name. This module should have a NAME <filename> and END <filename< that mark th start and end of assembly.
The public keyword makes the 'main' label available to outside program and linker.
ORG <address in hex>
The ORG directive specify a hardware address, that is an absolute hardware address for the linker to start assembly the code starting at the given address.
The DS16 diective request the assembly to allocate a storage address (notd that the 8051 uses 16 bit address D0 - D15) for this label.
The EW8051 has a relocatable linker XLINK. This is a very powerful linker that assigned physical address to your assembly language code. In typically application other than interrupt vector address (when interrupt occurs it must goes to fetch instruction at a specific address), or hardware interface address (for example special function address such as PORT0, PORT1, TIMER REGISTERS etc that has been assigned fixed hardware address) you would have to use a absolute prefine hardware address to access these registers or external hardware. However for user code and data, it is best to have the linker assign the address. The linker uses a linker command file that specify where each segment final located address shall be, during locate phase.
Thefore the above directive RSEG code indicate the assembly language code following will reside in the code segement, and its actual hardare final address shall be assigned during linker/locate time.
Writing your first assembly language Program
In this tutorial we shall write a assembly language program for the 8052 microcontroller. This is a slightly enhanced microcontroller from the 8051 in that it has additional 128 bytes of RAM (making it total of 256Bytes Internal RAM)and a additional timer/counter. Actually we have found 8052/32 easier to purchase than the vanilar 8051/31. Many companies makes a competitive 8052/32 chip including Philips, Atmel, Intel, Winbond and there are many chips on the market that will directly runs code from the 8052/32.
First we like to include a couple of file to ease our assembly language, so that we can directly refer to the special function registers such as P1, P2, EA etc instead of trying to hardcode the register location in Hex. At the start of the code, type the followings:-
The '$' is a assembly include directory to request the assembly to include the header file that define all the registers on the 8052 which a symbolic name. the bitdef.h is a include file that define the bit addressable location such as EA, TR0 bit etc, and allow us to code easily the SETB (set bit) and CLR (clear bit or clear byte) instructions.
Code the following assembly code, and save the file as H1.S51 (Hardware Experiement 1). You have to take note that the label (such as main:) MUST start at the leftmost column (that is colum 0), and directive such as ORG must start greater than column 0. In assembler the ';' character is use to write comment. Assembly will igore any character after the ';' character
;*** Hardware Experiement #1 using Lab51 and IAR EW8051, With ManLey ME52HU
Real Time ICE
;*** modified for IAR EW8051 by Kevin Quek, Testech Electronics Pte Ltd
;*** Email: firstname.lastname@example.org URL: www.testech-elect.com
MOV SP, #60H ; Setup up the Stack Pointer
MOV A, #0FEH;
MOV P1, A;
MOV P2, A;
MOV P3, A;
JB ACC.0, OUTLOOP;
;*** 1 msec Delay Subroutine
MOV R7, #00H ; one loop takes 256x4 = 1000usec
DJNZ R7, TIME_1MS0;
;** 50msec delay subroutine, internal loop is 256 x (2 + 1 + 1) = 1msec
;** entire delay is 50 x 1 = 50msec
MOV R6, #32H;
DJNZ R6, T1MS50;
;** 100msec delay subroutine
MOV R6, #64H;
DJNZ R6, T1MS100;
;** Buzzer sound for 50msec
;** uses DPTR, A in this subroutine
MOV A, #0FEH ; Buzzer sound duration is 30msec
MOV DPTR, #0002H ;
MOVX @DPTR, A;
MOV A, #0FFH;
MOVX @DPTR, A;
Save the file name as H1.S51 in D:\Lab51\ASM directory.
By now you should have a workspace name LAB51ASM and a project name H1.EWP
and a assembly source code named H1.S51. When you create a new project, two
different output can be selected by you,
DEBUG : This is typically use for debugging with simulator and Emulator, that is the symbols are generated in the output.
Release: This is use for generating a Hex code for the release version. The Hex Code is use to program your chip using a IC Programmer or EPROM.
Now that we have a workable assembly language program to test we shall continue to setup the entire project for assembler + Linking/Locating, Simulating and Target Debugging.
Setting the EW8051 Project Options
Right click on Workspace Pane Project -> H1 - Debug (you must select the debug output). Select the Options Sub Menu
The below is the option menu for this project.
In the derivative, select any 8952 microcontroller derivatives.
Select the Core Deriviates as Plain.IAR C/C++ Compiler supports the standard 8051 derivate (plain), a very special small micros Philips LPC series (Tiny) that has really little code space onchip, or the extended architecture (such as Dallas DS80C390) that has 16MB Linear Memory address scheme. Since we are using standard 8xC51/52, or even the 8xC51Rx2 (those enahnce 6 clock machine cycle derivate) select the Plain Option.
Since we are actually writing in assembly it really does not matter for us
those compiler options as we are using assembler directly. Set the rest as
belowto be consistant for later C projects..
Data Model = Small
Calling Conventation = IDATA Overlay or DATA Overlay depends
Location for Constant and Strings = Code Memory
Click on the Assembler option menu.
Select the Assembler Options. IAR assembler can treat symbols as case sensitives or insensitive. You can turn off this option if someelse write the code symbols without case sensitives.
We wanted to generate debug Information so that we can debug with emulator or CSPY Debugger. In the Assembler -> Output Tab, select Generate Debug Information.
Click on the Linker Option.
We wanted to use IAR CSPY to debug on the code on the target using MANLEY ME52 Emulator and IAR CSPY Simulator. Set to the following options. If you are using third party emulator, you can use IAR UBROF output or generate Hex File (no debug information) or AOMF (many emulator support this Intel 8051 debug format).
In the Linker Listing Tab, select the followings. We like to view the output of the linker map file to see the final physical address of all the code and data/xdata segment, absolute code segement etc. Therefore please turn on the option Generate Linker Listing and Module Summary.
Click the Linker -> Config Tab to select the Linker configuration file. For this tutorial the default linker config file in the IAR EW8051 install directory works just fine and we can use it, or we can modify the lnk51ew.xcl text file to modify the location. We leave this for a separate exercise later.
Click the Debugger Menu.
First we like to try out the CSPY simulator. Select the Simulator. If your assembler source code did not have a 'main' label at reset vector, do not click the "run To" address. Since our sample project above we have a main label the CSPY Simulator can upon reset immedately run the code and then break at the 'main' label. Note that your assembler label is 'main' but the assembler + linker will add two underscore character in front of any labels. So be sure to change the Run To label to __main so that when CSPY Debugger runs, it will break at the main label. Or else you can this off, and the debugger will stop at reset vector address 0x0000
In the Plugins tab, select the folllowings. The IAR CSPY Debugger support code profiling, code coverage and Hardware Trace and stack windows. Stack and Trace are very good feature for IAR debugger that save you many hours of debugging your code.
Select the Simulator Option and turn off the Stack Guard. This option is useful for C project with stack segment define and guard the stack pointer from pointing outside the stack segment (stack overflow, stack underflow).
We are now ready to assemble the code and debug. Click on OK to close the option menu to return to workbench.
BUILDING THE CODE
In the Top Menu -> project -> Select Make. The code will be assembled and link. The result of the assembler+Link process is displayed in the bottom Message windows. If you have any assembler error, click on the error line and it will immeately point the editor windows to the source line that cause the error. Fix the code error and remake again.
Now click on the debug icon or select Project -> Debug (CTRL+D) to start the simulator.
The following show the main CSPY Debug screen as it break at the main: label.
Click on the step into icon the debugger will step one assembly instruction and break at the next assembly instructions.
Step until the line LCALL TIME_100MS. Here you may choose to step into the
subroutine or simply STEP OVER that is simply run the line as one instruction
call and immediately return.
Click on the Step Over to run over the subroutine
The Debugger will step over the line "LCALL TIME_100MS" and then break at the next source assembly line "MOV P1, A;"
If you did not see the register windows. in the WorkBench Top Menu, select View -> Register Window. In the Register windows, select the register that you can view while debugging. In this example it is useful to see the IO Port register so that you can see the Port P0, P1, P2, P3 that changes to toggle external LEDs.
Now Step Over each subroutine call and observe the Port1 Register value changes as each LED is being turn on and off as the program runs.
To sit back and relax while debugging, you can select the Debug Menu -> Auto Step.
Select the Step Over source Level so that we do not step into the subroutine. select the delay to 1000msec between each auto step.
Here you can see the CSPY Debugging single stepping automatically. Now let it autostep and sit back relax and watch the debugger.
When it is satisfactory, click on the Stop icon
Please feel free to explore the various debugger options, open the watch windows, local variable windows etc. When you are through with debugging, click on the stop debugging icon to return to Workspace.
TARGET DEBUGGING WITH MANLEY REAL TIME ICE ON LAB51 EXPERIMENT BOARD
Now we will like to debug the code on the LAB51 Experiment Board. This tutorial
correspond to the LAB51 Hardware Experiment #3.
You should have the IAR EW V7.20D that has the MANLEY Driver or else please contact us for upgrade.
You must use the 8pin connector and connect the PORT1 Header to the J19 LED header (Brown color to P1.0 -> J19-L8) in order for the PORT1 pin to drive the LEDs
Open the Project Option Menu. Slect the Debugger Option and select third party driver.
Now select the Third Party Driver Sub Menu and select 'ME52HU.dll' from the IAR EW8051 Install Directory\8051\drivers\manley directory.
Once you select OK, a new Menu item will appear on IAR Embedded WorkBench. You can see that the Emulator Option is now integrated into IAR Environment. Connect the ME52HU Real Time ICE via USB to your PC and install the driver. The Manley emulator should be connected to the PC for any direct operation using the emulator including option settings.
Connect the DIP40 target Tip Board into the LAB51 DIP40 CPU socket. Then turn on the Power to the Emulator, follows by the Power to the target.
First select the Communication Setting.
Select the USB communication port since we are using USB to connect to the emulator.
Now in the Emulator setting, select the CPU as 8xC52
In the clock option, select 12MHZ for this experiement, and the crystal is map to emulator internal clock generator.
In the Emulator Mode tab select a standard single chip 8x51 mode, should you use 80C32, you should select the other option where ort0, Port2 are use as Data/Address Bus.
In the Misc tab select the followings for the LAB51 Board
In the code map, since this is single chip 8xC51/52 hence all code are map to emulator ROM memory. for some customer that wants to debug their code in external EPROM/FLASH, using the 8xC31/32 or the 8xC51/52 in expanded memory mode, customer may map all the code space to target.
In the Data Map tab, since we are using single chip with no external RAM for this assembly project, there is no xdata memory to be map. Otherwise if you have select the 8x31/32 or 8xC51/52 with external SRAM (port0, port2 uses as data/address pin) you can selectively map the xdata to either the emulator xdata space or target xdata space. If you have external IO devices in external xdata space, you MUST map this range to target for extenral IO devices.
For target with both external SRAM and External XDATA memory memory IO peripherals, we simply will map ALL to target.
Now are ready to debug on the target.
The code will be downloaded to the ME52H Real TIme ICE emulator. The CSPY Debugger for MANLEY ME52H Real Time ICE will appear. Note the the CSPY Debugger windows is exactly the same as simulator!!!!
Now you can step into, step over, autostep as stated before earlier in this tutorial usign the CSPY Simulator. in this way, you have a unify debug environment for both IAR CSPY Simulator and IAR Emulator Debugger using MANLEY ME52H.
Now click on the GO icon to run the code on the target at full speed. Note the running LED on the LAB51 Target Board.
This completed our first EW8051 Assembly Language Tutorial.
Return to IAR EW8051 Appnotes
Back to IAR Home Page Back to Testech Home Page Compiler RTOS Emulator/Debugging