chapter 3: hcs12 hardware and software...

57
H. Huang Transparency No.3-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Chapter 3: HCS12 Hardware and Software Development Tools The HCS12 Microcontroller Han-Way Huang Minnesota State University, Mankato September 2009

Upload: dangdieu

Post on 02-May-2018

239 views

Category:

Documents


3 download

TRANSCRIPT

H. Huang Transparency No.3-1

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Chapter 3: HCS12 Hardware and

Software Development Tools

The HCS12 Microcontroller

Han-Way Huang

Minnesota State University, Mankato

September 2009

H. Huang Transparency No.3-2

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Development Tools

Software Development Tools

Text editor: for entering source code using a PC

Terminal program: allows a PC to communicate with the demo board

Cross assembler: running on a PC to assemble programs written in an assembly language

Cross compiler: running on a PC to compile programs written in a high-level language

Simulator: running on a PC and allows to user to verify the logic of user programs

Source-level debugger: running on a PC and allows the user to verify programs by using

breakpoints, watch list, run-to-cursor, and so options to verify the correctness of programs

Integrated development environment (IDE): a software running on the PC that combines

a text editor, terminal program, cross assembler, cross compiler, and source-debugger and

allows the user to switch from one tool to another without quitting any of them.

H. Huang Transparency No.3-3

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Software Tools Used in this Book

MiniIDE: consisting of a text editor, a cross assembler, a terminal program, and a simple

project manager (a freeware)

asmIDE: consisting of a text editor, a cross assembler, a terminal program, and a simple

project manager (a freeware)

ICC12: consisting of a text editor, a cross C compiler, a terminal program, and a simple

project manager (demo version of ImageCraft product—8 kB size limit)

CodeWarrior: consisting of a text editor, a cross assembler, a cross C compiler, driver

programs for serial monitor and several BDM adaptors, and a source-level debugger

(demo version of Freescale product—32 kB size limit)

EGNU and GNU C compiler: consisting of a text editor, a C compiler, a terminal program,

and a simple project manager (a freeware)

H. Huang Transparency No.3-4

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Hardware Development Tools

BDM-based Debug Adapter

The HCS12 provides a background debug module that allows the user to perform

software debug activities (set break-point, trace program, execute program to a

breakpoint or a certain location, etc) via the serial interface.

In-Circuit Emulator (ICE)

ICE is an expensive hardware that allows the developer to perform program debug

activities before their hardware has been constructed.

Demo Board

A demo board allows the user to test their programs using the target microcontroller

before their final hardware has been constructed.

Digital or ordinal Oscilloscope

A oscilloscope allows the user to observe the waveform generated from the microcontroller

And hence enables the user to verify their program logic.

Only demo boards will be discussed.

H. Huang Transparency No.3-5

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Types of Demo Boards

Demo Boards with the D-Bug12 Monitor

Wytec Dragon12-Plus

Wytec Mini-Dragon

Demo Boards with the Serial Monitor

Wytec Dragon12-Plus

Wytec Mini-Dragon

Demo Boards with the BDM Adaptor

Freescale Project boards with a HCS12DT256 module and a P&E BDM interface

Axiom Manufacturing CMD-12DP512 with the Turbo BDM Light (TBDML) interface

H. Huang Transparency No.3-6

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

The Dragon12-Plus Demo Board

- 24 MHz bus speed (generated from a 4-MHz crystal).

- D-Bug12 or serial monitor

- 16 x 2 LCD kit (4-bit interface)

- Eight LEDs

- Four seven-segment displays

- Keypad connector

- Four buttons for input

- DIP switches for input

- Buzzer for playing siren and songs (wired to the PT5 pin)

- Potentiometer for testing A/D function (wired to PAD7 pin)

- Infrared transceiver

- CAN transceiver (Philips PCA82C250)

- A small breadboard

- BDM IN and BDM OUT connectors

- Two RS232 connector

- LTC1661 10-bit D/A converter chip with SPI interface

- 24LC16 serial EEPROM with I2C interface

- A DIP switch

- A Temperature sensor

H. Huang Transparency No.3-7

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-8

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

The Mini-Dragon Demo Board

H. Huang Transparency No.3-9

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

The D-Bug12 Monitor

- Supports most HCS12 devices with 128KB and 256 KB flash memory

- Used in many demo boards

- Requires a host terminal program that supports the Xon/Xoff software handshake

for proper operation

- The HyperTerminal bundled with Windows and the terminal program bundled with

asmIDE, miniIDE, ICC12, and EGNU IDE can work with D-Bug12 monitor

- Supports four operating modes: EVB mode, Jump to EEPROM mode, POD mode,

and Serial Bootloader mode

- After reset, the D-Bug12 reads the logic levels on the PAD1 and PAD0 pins to

decide which of the four D-Bug12 modes to enter.

Table 3.1 D-Bug12 operating modes

PAD1 PAD0 Operating mode

0011

0101

D-Bug12; EVBJump to internal EEPROM

D-Bug12; PODSerialBootloader

H. Huang Transparency No.3-10

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

EVB Mode

- The D-Bug12 monitor operates from the flash memory

- The users are restricted to use SRAM (from $1000 to $3BFF) or EEPROM to run

application programs.

- The user runs a terminal program on the PC to communicate with the D-Bug12 monitor

on the demo board.

- EVB operation model is shown in Figure 3.4.

target

system

low-level

interface

routines

D-Bug12

HCS12 demo board

PC running

a terminal

programUser

Figure 3.4 EVB model conceptual model

- When the demo board is powered up and the baud rate is set properly, the message as

shown in Figure 3.5 will appear on the terminal screen.

D-Bug12 4.0.0b24

Copyright 1996 - 2002 Motorola Semiconductor

For Commands type "Help"

>

Figure 3.5 D-Bug12 EVB mode sign on message

H. Huang Transparency No.3-11

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

- The D-Bug12 monitor displays the “>” character to indicate it is ready for operation.

- When a command issued to D-Bug12 is successfully executed, the monitor displays the

execution result and a new > character on a new line.

- If a command is not successfully executed, one can press the reset button to get out.

- An alternative to get out of the unsuccessful command is to press the abort key.

- The abort key is connected to the XIRQ signal.

- The Dragon12-Plus demo board uses the MC9S12DG256 as their MCU.

- The memory maps for these two demo boards are shown in Table 3.2.

Table 3.2 D-Bug12 memory map for HCS12Dx256

Address range Description

$0000-$03FF$0400-$0FFF$1000-$3BFF$3C00-$3FFF$4000-$EE7F$EE80-$EEBF$EEC0-$EEFF$EF00-$EF8B$EF8C-$EFFF$F000-$FFFF

I/O registers on-chip EEPROMon-chip SRAM (available to user)on-chip SRAM (D-Bug12)D-Bug12 codeUser accessable function tableCustomization dataD-Bug12 startup codeSecondary reset/interrupt tableBootloader

H. Huang Transparency No.3-12

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Using the MiniIDE Step 1. Invoke MiniIDE by double clicking the icon of MiniIDE.

Step 2. Communicating with the Demo Board.

- Press the Terminal menu (shown in Figure C.2) and select Show Terminal Window.

- Press the reset button on the demo board and the screen will change to Figure C.3.

H. Huang Transparency No.3-13

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-14

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-15

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Step 3. Setting Options.

- Set Options can be found under the Build menu and Terminal menu.

H. Huang Transparency No.3-16

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-17

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Options in General Category

H. Huang Transparency No.3-18

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Options in the Terminal Category

H. Huang Transparency No.3-19

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Options in the Tools Category

H. Huang Transparency No.3-20

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Options in the Assembler Category

H. Huang Transparency No.3-21

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Step 4. Open a new file for entering an assembler program

- Press the File menu and select New and the screen is changed to Figure C.10.

- Enter the program as shown in Figure C.11.

- Save the file after the whole program is entered. Press the File menu and select Save

as shown in Figure C.12

H. Huang Transparency No.3-22

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-23

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-24

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Step 5. Assemble the program.

- Press the Build menu and select Build siren.asm as shown in Figure C.13.

- After a successful assembly, the status window will display the corresponding

message as shown in Figure C.14.

H. Huang Transparency No.3-25

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-26

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Step 6. Download the program onto the demo board.

- Type the load command followed by the enter key in the terminal window.

- Press the Terminal menu and select Download File (shown in Figure C.15).

- A dialog box as shown in Figure C.16 appears.

- Enter the name of the file to be downloaded and click on Open.

- After a successful download, the screen is changed to Figure C.17.

H. Huang Transparency No.3-27

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-28

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

H. Huang Transparency No.3-29

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Step 7. Running and debugging the program.

- Type g 2000 followed by enter key.

- This program will generate a two-tone siren

Using the D-Bug12 Commands

BF <StartAddress> <EndAddress> [<Data>]

- Fill a block of memory locations with the value of <Data>.

- To fill the memory locations from $1000 to $1FFF with 0, enter the following command:

>bf 1000 1FFF 0

MD <StartAddress> [< EndAddress >]

- Display memory contents from < StartAddress > to < EndAddress >.

- 16 bytes are displayed on each line.

- The <StartAddress> is rounded down to the next lower multiple of 16.

- The <EndAddress> is rounded up to the next higher multiple of 16.

- Only one line is displayed if the EndAddress is not specified.

H. Huang Transparency No.3-30

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>md 1000

1000 AA 85 06 0C - D7 98 9A 61 - DF BE BC E9 - 03 AE D0 3D .......a.......=

>md 1005 1020

1000 AA 85 06 0C - D7 98 9A 61 - DF BE BC E9 - 03 AE D0 3D .......a.......=

1010 75 DA DF 39 - 3F 34 BD A9 - 2A CA FA DB - AC DA 18 97 u..9?4..*.......

1020 4D 5B 48 BA - B2 F7 B6 1B - 92 99 E5 E4 - A5 E9 01 9F M[H.............

>

MDW <StartAddress> [<EndAddress>]

>mdw 1000

1000 AA85 060C - D798 9A61 - DFBE BCE9 - 03AE D03D .......a.......=

>mdw 1000 1020

1000 AA85 060C - D798 9A61 - DFBE BCE9 - 03AE D03D .......a.......=

1010 75DA DF39 - 3F34 BDA9 - 2ACA FADB - ACDA 1897 u..9?4..*.......

1020 4D5B 48BA - B2F7 B61B - 9299 E5E4 - A5E9 019F M[H.............

>

H. Huang Transparency No.3-31

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

MM <Address> [<Data>]

- Used to examine and modify the contents of memory locations one byte at a time.

- If the 8-bit data parameter is present on the command line, the byte at memory location

<Address> is replaced with <Data> and the command is terminated.

- If no data is provided, then D-Bug12 enters the interactive memory modify mode.

- In the interactive mode, each byte is displayed on a separate line following the address

of data.

- Single-character sub-commands are used for the modification and verification of

memory contents in interactive mode.

- The available sub-commands are as follows:

[<Data>] <CR> Optionally update current location and display the next location.

[<Data>] </> or <=> Optionally update current location and redisplay the same location.

[<Data>] <^> or <-> Optionally update current location and display the previous location.

[<Data>] <.> Optionally update current location and exit Memory Modify.

H. Huang Transparency No.3-32

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Example of MM Command

>mm 1000

1000 00

1001 00 FF

1002 00 ^

1001 FF

1002 00

1003 00 55 /

1003 55 .

>

MMW <Address> [<Data>]

- Allows the contents of memory to be examined and/or modified as 16-bit hex data.

- If the 16-bit data is present on the command line, the word at memory location <Address> is replaced with <Data> and the command is terminated.

- If no data is provided, then D-Bug12 enters the interactive memory modify mode.

- MMW supports the same set of sub-commands as does the MM command.

H. Huang Transparency No.3-33

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>mmw 1100

1100 00F0

1102 AA55 0008

1104 0000 ^

1102 0008 aabb

1104 0000

1106 0000 .

>

Move <StartAddress> <EndAddress> <DestAddress>

- The number of bytes moved is one more than <EndAddress> - <StartAddress>

>move 1000 10ff 1100

>

RD – register display

>rd

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1521 3C00 2014 0000 6E:14 1001 0100

xx:1521 9C42 CPD $0042

>

H. Huang Transparency No.3-34

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

RM – register modification

This command is used to examine or modify the contents of CPU registers.

>rm

PC=0000 1500

SP=0A00

IX=0000 0100

IY=0000

A=00

B=00 ff

CCR=90 d1

PC=1500 .

>

<RegisterName> <RegisterValue>

- Allow one to change the value of any CPU register.

- Each bit of the CCR register can be changed by specifying its name.

H. Huang Transparency No.3-35

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>pc 2000

PC SP X Y D = A:B CCR = SXHI NZVC

2000 0A00 0100 0000 00:FF 1101 0001

>x 800

PC SP X Y D = A:B CCR = SXHI NZVC

2000 0A00 0800 0000 00:FF 1101 0001

>c 0

PC SP X Y D = A:B CCR = SXHI NZVC

2000 0A00 0800 0000 00:FF 1101 0000

>z 1

PC SP X Y D = A:B CCR = SXHI NZVC

2000 0A00 0800 0000 00:FF 1101 0100

>d 2010

PC SP X Y D = A:B CCR = SXHI NZVC

2000 0A00 0800 0000 20:10 1101 0100

>

H. Huang Transparency No.3-36

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

CCR bit name Description Legal Values

S

H

N

Z

V

C

IM

XM

STOP enable

Half carry

Negative flag

Zero flag

Two's complement over flg

Carry flag

IRQ interrupt mask

XIRQ interrupt mask

0 or 1

0 or 1

0 or 1

0 or 1

0 or 1

0 or 1

0 or 1

0 or 1

Table 3.4 Condition code register bits

ASM <Address>

- Invokes the one-line assembler/disassembler.

- Allows memory contents to be viewed and altered using assembly language mnemonics.

- When displaying instructions, each instruction is displayed in its mnemonic form.

- The assembly/disassembly process can be terminated by a period.

- The one-line assembler displays the current instruction and allows the user to enter new

instruction.

- User can skip the current instruction by pressing the Enter key.

H. Huang Transparency No.3-37

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

The following example displays instruction starting from $2000:

>asm 2000

2000 FC0800 LDD $0800 >

2003 CD0900 LDY #$0900 >

2006 CE000A LDX #$000A >

2009 1810 IDIV >

200B CB30 ADDB #$30 >

200D 6B44 STAB 4,Y >

200F B7C5 XGDX >

2011 CE000A LDX #$000A >.

>

The following example enters three instructions (in bold face) starting from $1500: >asm 1500

1500 FC0800 LDD $0800

1503 F30802 ADDD $0802

1506 7C0900 STD $0900

1509 E78C TST 12,SP >.

>

H. Huang Transparency No.3-38

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

BR [<Address> …] Setting or examine breakpoints

- A breakpoint halts the program execution when the CPU reaches the breakpoint address.

- When a breakpoint is encountered, the D-Bug12 monitor displays the contents of CPU

registers and the instruction at the breakpoint (not executed yet).

- Breakpoints are set by typing the breakpoint command followed by one or more

breakpoint addresses.

- Entering the breakpoint command without any breakpoint addresses will display all the

currently set breakpoints.

- A maximum of two user breakpoints may be set at one time.

>br 1020 1040 1050 ; set three breakpoints

Breakpoints: 1020 1040

Breakpoint Table Full

>

H. Huang Transparency No.3-39

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

NOBR [<Address> <Address>]

- Delete one or more previously defined breakpoints

- All breakpoints will be deleted if no addresses are specified. >br 2000 2010 2020 2040 2090 ; set four breakpoints

Breakpoints: 2000 2010

Breakpoint Table Full

>nobr 2000 ; delete one breakpoints

Breakpoints: 2010

>

G [<Address>]

- Begin execution of user code at the specified address.

- If no address is specified, CPU starts execution of the instruction at the current

PC address. >g 1500

User Bkpt Encountered

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 150C 3C00 7B48 0000 03:E8 1001 0001

xx:150C 911E CMPA $001E

>

H. Huang Transparency No.3-40

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

GT <Address>

- Execute instruction until the given address and stop.

- User usually needs to specify where the program execution should start before

issuing this command.

>pc 1500

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1500 3C00 1000 1002 00:00 1001 0101

xx:1500 CF1500 LDS #$1500

>gt 1540

Temporary Breakpoint Encountered

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1510 1500 1000 1002 1E:00 1001 0000

xx:1510 3B PSHD

>

H. Huang Transparency No.3-41

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

T [<count>]

- Used to execute one or multiple instructions starting from the current PC address.

- As each program instruction is executed, the CPU register contents and the next

instruction to be executed are displayed.

- Only one instruction will be executed when no count is specified.

>pc 1500

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1500 1500 1000 1002 1E:00 1001 0000

xx:1500 CF1500 LDS #$1500

>t

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1503 1500 1000 1002 1E:00 1001 0000

xx:1503 CE1000 LDX #$1000

>t 2

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1506 1500 1000 1002 1E:00 1001 0000

xx:1506 34 PSHX

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1507 14FE 1000 1002 1E:00 1001 0000

xx:1507 861E LDAA #$1E

>

H. Huang Transparency No.3-42

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

CALL [<Address>]

- Used to execute a subroutine and returns to the D-Bug12 monitor program.

- All CPU registers contain the values at the time the final RTS instruction was

executed, with the exception of the program counter.

- The program counter contains the starting address of the subroutine when returning

from the subroutine.

>call 1600

Subroutine Call Returned

pp PC SP X Y D = A:B CCR = SXHI NZVC

38 1600 0A00 0032 0900 00:31 1001 0000

xx:1600 FC1000 LDD $1000

>

H. Huang Transparency No.3-43

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

The Pod Mode

- This mode is intended to run the demo board as a BDM host to control a target board.

- The arrangement is shown in Figure 3.20.

UserTerminal

(or PC)

your demo board

D-Bug12

Low-level

interface

routine

Background debug

command

Target

system

HCS12

microcontroller

Figure 3.19 D-Bug12's POD mode conceptual model

H. Huang Transparency No.3-44

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

The Jump-to-EEPROM Mode

- Execute a small program from the on-chip EEPROM whenever the EVB is powered up

or reset.

- This mode provides a way to execute a program in a standalone manner

without having to erase and program the on-chip flash memory using the bootloader.

The Bootloader Mode

- The Bootloader resides from $F000 to $FFFF.

- Bootloader can be used to erase and reprogram the remainder of on-chip flash memory

or erase the on-chip EEPROM.

- Bootloader utilizes the SCI port for communication.

- The only required host program is a terminal program that can communicate at 9600 to

115,200 baud and supports XON/XOFF handshaking.

- The bootloader mode prompt is as follows:

HCS912DP256 Bootloader

a) Erase Flashb) Program Flashc) Set Baud Rated) Erase EEPROM?

Figure 3.20 Serial bootloader prompt

H. Huang Transparency No.3-45

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Tips for Assembly Program Debugging

Syntax Errors

- Misspelling of instruction mnemonics.

- Starting instruction mnemonic at column 1. The mnemonic is treated as a label whereas

the operands are treated as mnemonic.

- Missing operands

- Will be highlighted by the assembler and are easy to fix.

Logic Errors

1. Using extended (or direct) mode instead of immediate mode

- A program with this type of addressing mode error is on the next page.

H. Huang Transparency No.3-46

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

N equ 20 ; array count

org $1000

array dc.b 2,4,6,8,10,12,14,16,18,20

dc.b 22,24,26,28,30,32,34,36,38,40

sum ds.w 1

org $1500

ldx array ; place the starting address of array in X

movw 0,sum ; initialize sum to 0

ldy N ; initialize loop count to N

loop ldab 1,x+ ; place one number in B and move array pointer

sex B,D ; sign-extend the 8-bit number to 16-bit

addd sum ; add to sum

std sum ; update the sum

dbne y,loop ; add all numbers to sum yet?

swi ; return to monitor

end

- Assemble and download this program onto the demo board. >load

....

done

>

H. Huang Transparency No.3-47

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

- Use the asm command to make sure that the program is downloaded correctly.

>asm 1500

xx:1500 FE1000 LDX $1000 >

xx:1503 180400001014 MOVW $0000,$1014 >

xx:1509 DD14 LDY $0014 >

xx:150B E630 LDAB 1,X+ >

xx:150D B714 SEX B,D >

xx:150F F31014 ADDD $1014 >

xx:1512 7C1014 STD $1014 >

xx:1515 0436F3 DBNE Y,$150B >

xx:1518 3F SWI >.

- Make sure that program data is downloaded correctly. Use the md command:

>md 1000 1010

1000 02 04 06 08 - 0A 0C 0E 10 - 12 14 16 18 - 1A 1C 1E 20 ...............

1010 22 24 26 28 - 00 00 B9 A9 - 2A CA FA DB - AC DA 18 97 "$&(....*.......

>

H. Huang Transparency No.3-48

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Run the Program

>g 1500

User Bkpt Encountered

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1519 3C00 0213 0000 FF:07 1001 1000

xx:1519 88F4 EORA #$F4

>

Examine the execution result – incorrect!!

>md 1010

1010 22 24 26 28 - FF 07 B9 A9 - 2A CA FA DB - AC DA 18 97

>

- The program is short.

- Errors can be found by tracing.

- Set PC to the start of the program (at $1500)

>pc 1500

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1500 3C00 0213 0000 FF:07 1001 1000

xx:1500 FE1000 LDX $1000

>

H. Huang Transparency No.3-49

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Trace One Instruction at a time

>t 1

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1503 3C00 0204 0000 FF:07 1001 0000

xx:1503 180400001014 MOVW $0000,$1014

>

- The executed instruction is “ldx $1000” which should place the start address of

the array in X.

- The instruction trace result shows that X receives $0104 not $1000.

- This is due to addressing mode error.

- Change the instruction to ldx #$1000 and rerun the program.

- Reload the program and trace the program.

- Trace two instruction this time.

H. Huang Transparency No.3-50

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>t 2

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1503 3C00 1000 0000 FF:F0 1001 0000

xx:1503 180400001014 MOVW $0000,$1014

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1509 3C00 1000 0000 FF:F0 1001 0000

xx:1509 DD14 LDY $0014

>md 1010 ; examine sum at $1014~$1015.

1010 22 24 26 28 - FF 00 B9 A9 - 2A CA FA DB - AC DA 18 97

>

- We expect the variable sum (at $1014 and $1015) to receive $0000. But it didn’t.

- The error is again caused by incorrect use of the addressing mode.

- The movm 0,sum instruction copies the contents of memory location 0 to sum.

- Change the second instruction to movw #0,sum. Rerun the program and examine

the memory contents. It is still incorrect !!

H. Huang Transparency No.3-51

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>load

*

>g 1500

User Bkpt Encountered

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1519 3C00 100F 0000 00:F0 1001 0000

xx:1519 88F4 EORA #$F4

>md 1010

1010 22 24 26 28 - 00 F0 B9 A9 - 2A CA FA DB - AC DA 18 97

>

- Trace the program up to the third instruction:

H. Huang Transparency No.3-52

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>pc 1500

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1500 3C00 100F 0000 00:F0 1001 0000

xx:1500 CE1000 LDX #$1000 ; 1st instruction

>t 3

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1503 3C00 1000 0000 00:F0 1001 0000

xx:1503 180300001014 MOVW #$0000,$1014 ; 2nd instruction

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1509 3C00 1000 0000 00:F0 1001 0000

xx:1509 DD14 LDY $0014 ; 3rd instruction

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 150B 3C00 1000 000F 00:F0 1001 0000

xx:150B E630 LDAB 1,X+

>

- The program intends to load 20 into Y with the third instruction and expect Y

to be set to 20. But Y did not get 20. It receives F instead.

- This is due to the incorrect use of the addressing mode.

- Change the instruction to ldy #20 and rerun the program.

H. Huang Transparency No.3-53

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>g 1500

User Bkpt Encountered

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 151A 3C00 1014 0000 01:A4 1001 0000

xx:151A F421BD ANDB $21BD

>md 1010

1010 22 24 26 28 - 01 A4 B9 A9 - 2A CA FA DB - AC DA 18 97

>

After this correction, sum receives the correct value $1A4 (420).

H. Huang Transparency No.3-54

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

Mismatch of Operand Size

Example Program – Finding the sum of elements of an array

N equ 20 ; array count

org $1000

array dc.b 2,4,6,8,10,12,14,16,18,20

dc.b 22,24,26,28,30,32,34,36,38,40

sum ds.w 1

org $1500

ldx #array ; place the starting address of array in X

movw #0,sum ; initialize sum to 0

ldy #N ; initialize loop count to N

loop ldd 1,x+ ; place one number in D and move array pointer

addd sum ; add to sum

std sum ; update the sum

dbne y,loop ; add all numbers to sum yet?

swi ; return to monitor

end

H. Huang Transparency No.3-55

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

The value of sum is incorrect after running the program:

>md 1010

1010 22 24 26 28 - A6 1F B9 A9 - 2A CA FA DB - AC DA 18 97

>

This program can be debugged by tracing:

>pc 1500

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1500 3C00 1014 0000 A6:1F 1001 1000

xx:1500 CE1000 LDX #$1000

>t

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1503 3C00 1000 0000 A6:1F 1001 0000

xx:1503 180300001014 MOVW #$0000,$1014

>t

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 1509 3C00 1000 0000 A6:1F 1001 0000

xx:1509 CD0014 LDY #$0014

>t

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 150C 3C00 1000 0014 A6:1F 1001 0000

xx:150C EC30 LDD 1,X+

H. Huang Transparency No.3-56

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

>t

PP PC SP X Y D = A:B CCR = SXHI NZVC

38 150E 3C00 1001 0014 02:04 1001 0000

xx:150E F31014 ADDD $1014

>

The 4th instruction should place the value 2 in D rather than $0204. This is due to

the incorrect use of the instruction of ldd 1,x+. This instruction should be replaced

by the following two instructions:

ldab 1,x+ clra Other logic errors

1. Inappropriate Use of Index Addressing Mode

- Indexed addressing mode is often used to step through array elements.

- After accessing each element, the index register must be incremented or decremented.

- Program execution can’t be correct if index register is incremented or decremented

incorrectly.

- This error can be found after performing computation in the first one or two elements

by program tracing.

H. Huang Transparency No.3-57

The HCS12/MC9S12 Microcontroller

Copyright © 2010 Delmar Cengage Learning

2. Stack frame errors

This error will be discussed in Chapter 4.

3. Incorrect algorithm

- This type of error can be detected by tracing the program.

- The user needs to read the program carefully in order to find out the errors.

Using CodeWarrior

Has a built-in simulator.

Supports debugging using the serial monitor resident on the demo board.

Has drivers with several BDM-based debug adapters:

1. P&E Multilink/CyclonePro BDM adaptor

2. TBDML adaptor

3. Abatron BDI adaptor

4. Softec’s inDART debugger