Sunteți pe pagina 1din 11

Laboratory Short Course

Introduction to CodeWarrior™ –
Simulating the Microcontroller in
Assembly Language

www.freescale.com/universityprograms

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc.
All other product or service names are the property of their respective owners
© Freescale Semiconductor, Inc. 2006.
Document Number: LABS12CINTRO03S /REV 1
Reading this Document

Answers provided to the Instructor assume that the reader is using Freescale HCS12C Family Student Learning Kit,
and CodeWarrior development software.

This short course has been created using an adapted version of the Process Oriented Guided Inquiry Learning (POGIL)
method. For more information visit www.pogil.org

Freescale Semiconductor LABS12CINTRO03S, Rev 1 1


Overview
A laboratory experience is vital for us to be able to start programming the microcontroller and to be able to really understand how it
works and to apply it in embedded system designs. This module shows the student how to enter an assembly language program in
the CodeWarrior® integrated development environment and then to simulate its execution using the True-Time simulator/debugger.

Learning Objectives
In this module we introduce you to the CodeWarrior® programming environment. We will see how to start the system and enter a
program to be run with the debugger. In the next module we will give you a program and you will start to learn to use the HCS12
instruction set by modifying if to change how it operates.

Success Criteria
At the end of this module you will be able to assemble a program and to run and debug it with the CodeWarrior simulator/debugger.

Prerequisites
You must know about the HCS12 instruction set and the memory addressing modes available. You do not have to know the
instruction set in detail. The following lab modules will help you learn that.

More Resources and Further Information


Cady, Fredrick M., Software and Hardware Engineering: Assembly and C Programming for the Freescale HCS12
Microcontroller, 2nd edition. (New York: Oxford University Press, Inc., 2008). Chapter 5 An Assembler Program, Chapter 6 The
Linker, Chapter 7 The HCS12 Instruction Set.

Smart Linker, Metrowerks Corporation, Austin, TX, 2003.

Debug (DBG) Module V1 Block User Guide, S12DBGV1/D, Freescale Semiconductor, 2003.

MC9S12C128 Data Sheet: Covers MC9S12C Family and MC9S12GC Family, Freescale Semiconductor, Austin, Texas, 2005.

Montañez, E. and Ruggles, S., MCUSLK_CSM12C32 - Getting Started with the Microcontroller Student Learning Kit
(MCUSLK), Freescale Semiconductor, Austin, Texas, 2005.

Sibigtroth, J. M., CPU12RM/AD rev. 3 – CPU12 Reference Manual, Freescale Semiconductor, Austin, Texas, 2002.

Williams, J., AN2548SW1 – CodeWarrior Project Software files for AN2548/D, Freescale Semiconductor, Austin, Texas, 2003.

Debugger HCS12 Onchip DBG Module User Interface, Metrowerks, 2003.

Using the CodeWarrior® Simulator


1. Launch the CodeWarrior IDE.
a. From the Windows desktop, click Start > Programs > Freescale CodeWarrior > CW for HC12 Vx.x > CodeWarrior IDE.

2. Create New Project.


a. From the IDE main menu bar, select File > New. New window appears.

b. Highlight HC(S)12 New Project Wizard.

c. Enter a Project Name that makes sense to you (like lab_x).

d. Enter a Location for the project. This should be on a drive or in a directory where you want to keep your HCS12 projects.

e. Click OK.

3. In the New Project Wizard

Page 2: Select MC9S12C32 (or your derivative) as the derivative you want to use and click Next.

Freescale Semiconductor LABS12CINTRO03S, Rev 1 2


Page 3: Check the language support you want (Assembly) and click Next.

Page 4: Check Relocatable Assembly.

Page 5: Check Full Chip Simulation and P&E Multilink/Cyclone Pro and click Finish.

In the Project Manager Window

4. Switch to the Full Chip Simulation for debugging by clicking the pull down arrow and selecting Full Chip Simulation. (This
does not use the hardware for debugging. If you want to actually use the HCS12 hardware, choose P&E Multilink Cyclone
Pro.)

5. Create listing files by opening the Simulator Settings panel by clicking the icon shown above.

a. Click on + next to Target in the Target Settings.

b. Highlight Assembler for HC12 and in that panel click on Options.

(i) Check Generate a listing file.

(ii) Check Object File Format. Choose ELF/DWARF 2.0 Object File Format.

(iii) Click OK.

c. Click OK.

6. Open the Sources folder (click on the + if it is not open).

7. Double click on main.asm and enter the following code after the comment
; Your program code goes here.
; Your program code goes here
; Initialize I/O
main_loop:
; DO
nop ; A "no operation" operation
ldx #ConstData ; Initialize X with an address
ldab #$02 ; Initialize a counter with data
; Load and store data in a loop
loop:
ldaa 0,x ; Get the data
staa VarData ; Store it in RAM
ldaa 2,x ; Get another byte
staa VarData+1 ; Store it
inx ; Increment the pointer
decb ; Decrement the counter
bne loop ; Loop until B = 0
nop
; Forever
bra main_loop

Freescale Semiconductor LABS12CINTRO03S, Rev 1 3


;**************************************************************
MyConst:SECTION
; Place constant data here
ConstData: DC.B $0a,$0b,$11,10
;**************************************************************
MyData: SECTION
; Place variable data here
VarData: DS.B 2 ; Two bytes of storage
8. Save it to whatever name you want: File > Save As. (Make sure you give it an .asm filename, or you can just use the main.asm
file if you want.)

9. Check to make sure debugging information is going to be generated.


a. In the Project Manager Files window, check to see that there is a • in the rightmost column under the bug icon.

10. Assemble the file to check for errors:


a. Click on Project > Compile.

b. Correct any errors.

11. Have a look at your list file.


a. File > Open and position to the bin folder in your project and open the .lst file.

Explore 1.

1. How many bytes are in the program and constant data?


2. You cannot tell from this listing in what memory locations the VarData variable bytes will be located. Why not?

3. What op code (in hexadecimal) is generated for a nop instruction?

4. Do any of the labels in the program generate any code bytes?

Stimulate 1.
1. Why does the instruction ldx ConstData assemble to CExx xx?

2. What are the four bytes (in hexadecimal) assembled as constant data bytes by the assembler directive DC.B?

3. In the ConstData why does DC.B produce different data bytes for $10 and 10?

In future lab exercises you may wish to add other source files to your project. Here is how you do that.

12. Add other source files to the relocatable files in the project:

Freescale Semiconductor LABS12CINTRO03S, Rev 1 4


a. Click on the Add New Text File icon:

b. Edit the file.


c. File > Save As (use .asm as the extension for assembler files).

d. Add it to the project: Project > Add *.asm to the project. In Add Files check both Full Chip Simulation and P&E Multilink
Cyclone Pro.

e. Assemble/Compile it: Project > Compile.

13. View the linker parameter file.


a. Click the + next to Prm and double click the Full_Chip_Simulation_linker.prm file to open it in the editor.

Explore 2.

The linker parameter file allows us to locate the various parts of the program in the proper sort of memory. For example, all code
and constant data must be in ROM and variable data and the stack in RAM.

1. Look at the SEGMENTS portion.

a. What memory locations are used for RAM?

b. Where memory locations are used for ROM?

2. Look at the PLACEMENT portion. A variety of default placement options are included. We are interested in where the
DEFAULT_ROM (code and constant data SECTIONS in our assembly program) and the DEFAULT_RAM (variable data
SECTION) will be located.

a. In what memory locations will the code and constants be located?

b. In what memory locations will the variable data be located?

Stimulate 2.

1. What change would you make to the linker parameter file if the target hardware had RAM in memory locations $1000 - $1FFF
(0x1000 – 0x1FFF)?

2. What change would you make to the linker parameter file if you wanted to locate the code and constants in memory starting at
$4000 (0x4000)?

14. Make the project.


a. Project > Make or click on the icon

b. Correct any linking errors.

15. Inspect the Linker Map file.


a. Click the + on Linker Map and open Full_Chip_Simulation.map.

Explore 3.
STARTUP SECTION

Freescale Semiconductor LABS12CINTRO03S, Rev 1 5


1. What is the starting address of the program?
SECTION-ALLOCATION SECTION

2. What number base is used to tell you the size of the various sections?

3. How many bytes of ROM are used by this program?

4. How many bytes of RAM are used by this program?


OBJECT-ALLOCATION SECTION
5. What is the memory address of the label main_loop?

6. What is the memory address of the label loop?

7. In what memory locations would you find MyData?

16. Run the program without the PBMCUSLK using the CodeWarrior True-Time Simulator.
a. Make sure Full_Chip_Simulation is shown in the Project Manager pull down window as shown in step 4 above.

b. Launch the True-Time Simulator by Project > Debug, F5, or clicking on the icon

If a loader warning appears click OK.

c. If Simulator is not the debug target, click on Component > Set Target and chose Simulator Target Interface and click
OK.

d. If your program has not been loaded, or you want to reload it, click Simulator > Load and load bin\simulator.abs.

e. You can use all the features of the simulator on the code.

Explore 4.

After the simulator has been launched, have a look at the display:
1. The Source window shows your source program.

2. The Assembly window shows the program as it looks in memory. Right Click in the Assembly window and check Display
Code to make your display show the code in the memory locations $C000 - $C01A. Right Click again and check Display
Symbolic to show symbolic instead of decimal addresses.

3. The Registers window shows the current status/contents of all registers.

4. The Data window will show the current value of constant and variable data in your program. Click on the +ConstData to show
the four constant data bytes. Right Click on ConstData and select FORMAT ALL Hex. Click on the +VarData to show the two
variable bytes.
5. The Procedure window shows what procedure you are currently in and the Command window allows you to enter
simulator/debugger commands.
6. The Memory window allows the display of memory contents. Right Click in the Memory window and select Address . . . and
in the Display Address window enter C000.

a. What is displayed here?

7. Try resizing any of the windows.

Run the Program in the Simulator


Explore 5.
1. In the Source window position the cursor pointer over the nop instruction and Right Click and select Set Breakpoint.

Freescale Semiconductor LABS12CINTRO03S, Rev 1 6


2. Click the Green Start/Continue Arrow or click Run > Start/Continue. The program should run to your breakpoint and stop.
(Note: A breakpoint stops the program before it executes that statement.)
3. Now, step through your program one assembly language statement at a time by clicking on the single-step button or pressing
F11 until you reach the nop instruction
at the beginning of the program again.
(We will see what the LDS instruction is
all about later.)

4. At each step inspect the Register window to see what is happening to the registers. Try to predict what will happen at each
step BEFORE you click on the step button.

Stimulate 3.

1. Under what circumstances does it make sense to run and debug your programs in the simulator?

2. When would you want to run and debug your programs using the hardware debugger?

Skill Exercise 1

1. Double click on each of the register boxes in the Register window and enter a value of zero for each register. You won't be
able to change the CCR.
2. Press the Reset Target button (or type control-R) to return the program to the initial reset state.

3. Single-step through the program and complete a table showing the result of each instruction. The CCR (Condition Code
Register) bits NZVC are shown bold when the bit is set.

Instruction D A B IX IY PC SP NZVC
Reset state 0000 00 00 0000 0000 C000 FFFE 0000
lds
#__SEG_END_SSTACK
nop
ldx #ConstData
ldab #$02
ldaa 0,x
staa VarData
ldaa 2,x
staa VarData+1
inx
decb
bne loop
ldaa 0,x
staa VarData
ldaa 2,x
staa VarData+1
inx
decb
bne loop

Freescale Semiconductor LABS12CINTRO03S, Rev 1 7


nop
bra main_loop

Communication
1. As you step through your program explain to your lab instructor what you see on the screen. In particular, explain what is
happening to each register and to the condition code register as you step through your program. What questions do you have?

Reflection on Learning
1. For the program entered and stepped through, list any instructions that you are not sure how they work. Compare your list with
your lab partner's and if you understand something that he or she does not, explain it so that they understand. If there are still
instructions you are not sure of, ask your laboratory instructor to explain them to you.

Freescale Semiconductor LABS12CINTRO03S, Rev 1 8


Revision History

Revision Comments Author


0 Initial Release Fred Cady
1 Conversion to CodeWarrior 4.6 Fred Cady

Freescale Semiconductor LABS12CINTRO03S, Rev 1 9


How to Reach Us:
Home Page:
www.freescale.com Information in this document is provided solely to enable system and software
implementers to use Freescale Semiconductor products. There are no express or
implied copyright license granted hereunder to design or fabricate any integrated
Web Support: circuits or integrated circuits based on the information in this document.
http://www.freescale.com/support Freescale Semiconductor reserves the right to make changes without further notice to
any products herein. Freescale Semiconductor makes no warranty, representation or
USA/Europe or Locations Not Listed: guarantee regarding the suitability of its products for any particular purpose, nor does
Freescale Semiconductor, Inc. Freescale Semiconductor assume any liability arising out of the application or use of
Technical Information Center, EL516 any product or circuit, and specifically disclaims any and all liability, including without
2100 East Elliot Road limitation consequential or incidental damages. “Typical” parameters which may be
Tempe, Arizona 85284 provided in Freescale Semiconductor data sheets and/or specifications can and do
+1-800-521-6274 or +1-480-768-2130 vary in different applications and actual performance may vary over time. All operating
www.freescale.com/support parameters, including “Typicals” must be validated for each customer application by
customer’s technical experts. Freescale Semiconductor does not convey any license
under its patent rights nor the rights of others. Freescale Semiconductor products are
Europe, Middle East, and Africa: not designed, intended, or authorized for use as components in systems intended for
Freescale Halbleiter Deutschland GmbH surgical implant into the body, or other applications intended to support or sustain life,
Technical Information Center or for any other application in which the failure of the Freescale Semiconductor
Schatzbogen 7 product could create a situation where personal injury or death may occur. Should
81829 Muenchen, Germany Buyer purchase or use Freescale Semiconductor products for any such unintended or
+44 1296 380 456 (English) unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor
+46 8 52200080 (English) and its officers, employees, subsidiaries, affiliates, and distributors harmless against
+49 89 92103 559 (German) all claims, costs, damages, and expenses, and reasonable attorney fees arising out
+33 1 69 35 48 48 (French) of, directly or indirectly, any claim of personal injury or death associated with such
www.freescale.com/support unintended or unauthorized use, even if such claim alleges that Freescale
Semiconductor was negligent regarding the design or manufacture of the part.

Japan:
Freescale Semiconductor Japan Ltd.
Headquarters
ARCO Tower 15F
1-8-1, Shimo-Meguro, Meguro-ku,
Tokyo 153-0064
Japan
0120 191014 or +81 3 5437 9125
support.japan@freescale.com

Asia/Pacific:
Freescale Semiconductor Hong Kong Ltd.
Technical Information Center
2 Dai King Street
Tai Po Industrial Estate
Tai Po, N.T., Hong Kong
+800 2666 8080
support.asia@freescale.com

For Literature Requests Only:


Freescale Semiconductor Literature Distribution
Center
P.O. Box 5405
Denver, Colorado 80217
1-800-441-2447 or 303-675-2140
Fax: 303-675-2150
LDCForFreescaleSemiconductor@hibbertgroup.com

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other
product or service names are the property of their respective owners. ARM is the registered
trademark of ARM Limited. ARM9, ARM11, and ARML210™ are the trademarks of ARM Limited.
Java and all other Java-based marks are trademarks or registered trademarks of Sun
Microsystems, Inc. in the U.S. and other countries. The “PowerPC” name is a trademark of IBM
Corp. and used under license.
© Freescale Semiconductor, Inc. 2006.
Document Number: LABS12CINTRO03S /REV 1

S-ar putea să vă placă și