23.10.2013 Views

M68HC05 Family — Understanding Small Microcontrollers

M68HC05 Family — Understanding Small Microcontrollers

M68HC05 Family — Understanding Small Microcontrollers

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong>TB/D<br />

Rev. 2.0<br />

HC<br />

5<br />

<strong>M68HC05</strong> <strong>Family</strong><br />

<strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong><br />

For More Information On This Product,<br />

Go to: www.freescale.com


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

For More Information On This Product,<br />

Go to: www.freescale.com


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Acknowledgment<br />

JamesM.Sibigtroth,aprincipalmemberofthetechnicalstaff<br />

at Motorola, is author of this text book. He is based in Austin,<br />

Texas.<br />

The author wishes to acknowledge Gordon Doughman for<br />

contributing the chapter entitled On-Chip Peripheral<br />

Systems.GordonisafieldapplicationsengineerforMotorola<br />

in Dayton, Ohio.<br />

Motorola reserves the right to make changes without further notice to<br />

any products herein to improve reliability, function or design. Motorola<br />

does not assume any liability arising out of the application or use of any<br />

product or circuit described herein; neither does it convey any license<br />

under its patent rights nor the rights of others. Motorola products are not<br />

designed, intended, or authorized for use as components in systems<br />

intended for surgical implant into the body, or other applications intended<br />

to support or sustain life, or for any other application in which the failure<br />

of the Motorola product could create a situation where personal injury or<br />

death may occur. Should Buyer purchase or use Motorola products for<br />

any such unintended or unauthorized application, Buyer shall indemnify<br />

and hold Motorola and its officers, employees, subsidiaries, affiliates,<br />

and distributors harmless against all claims, costs, damages, and<br />

expenses, and reasonable attorney fees arising out of, directly or<br />

indirectly, any claim of personal injury or death associated with such<br />

unintended or unauthorized use, even if such claim alleges that Motorola<br />

was negligent regarding the design or manufacture of the part.<br />

Motorola and the Motorola logo are registered trademarks of Motorola, Inc.<br />

IBM is a registered trademark of IBM Corporation<br />

Macintosh is a trademark of Apple Computer, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong>Rev. 2.0<br />

MOTOROLA Acknowledgment<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

3


Freescale Semiconductor, Inc...<br />

Acknowledgment<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

4 Acknowledgment<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

List of Sections<br />

Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7<br />

List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13<br />

List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15<br />

What is a Microcontroller? . . . . . . . . . . . . . . . . . . . . . . . 17<br />

Computer Numbers and Codes . . . . . . . . . . . . . . . . . . 27<br />

Basic Logic Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . 37<br />

Computer Memory and Parallel I/O . . . . . . . . . . . . . . . 51<br />

Computer Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 65<br />

<strong>M68HC05</strong> Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . 97<br />

Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135<br />

The Paced Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159<br />

On-Chip Peripheral Systems . . . . . . . . . . . . . . . . . . . . 179<br />

Instruction Set Details . . . . . . . . . . . . . . . . . . . . . . . . . . 217<br />

Reference Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287<br />

Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299<br />

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA List of Sections<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

5


Freescale Semiconductor, Inc...<br />

List of Sections<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

6 List of Sections<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Table of Contents<br />

What is a Microcontroller?<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17<br />

Overall View of a Computer System . . . . . . . . . . . . . . . . . . . . . . . . . .18<br />

Computer System Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19<br />

Computer System Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20<br />

Central Processor Unit (CPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20<br />

Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21<br />

Computer Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21<br />

Computer Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23<br />

The Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25<br />

The Parts of Any Computer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25<br />

Kinds of Computers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25<br />

Computer Numbers and Codes<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27<br />

Binary and Hexadecimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . .28<br />

ASCII Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30<br />

Computer Operation Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30<br />

Instruction Mnemonics and Assemblers . . . . . . . . . . . . . . . . . . . . . . .32<br />

Octal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32<br />

Binary Coded Decimal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Table of Contents<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

7


Freescale Semiconductor, Inc...<br />

Table of Contents<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37<br />

Logic Levels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38<br />

CMOS Transistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39<br />

Simple Gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40<br />

Inverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40<br />

NAND Gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41<br />

NOR Gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42<br />

Transmission Gates, Buffers, and Flip Flops. . . . . . . . . . . . . . . . . . . .44<br />

Transmission Gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44<br />

Three-State Buffer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46<br />

Half Flip Flop (HFF). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49<br />

Computer Memory and Parallel I/O<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52<br />

Pigeon Hole Analogy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52<br />

How a Computer Sees Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53<br />

Kilobytes, Megabytes, and Gigabytes . . . . . . . . . . . . . . . . . . . . . . . . .54<br />

Kinds of Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54<br />

Random Access Memory (RAM). . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

Read-Only Memory (ROM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

Programmable ROM (PROM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

EPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

OTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56<br />

EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56<br />

I/O as a Memory Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57<br />

Internal Status and Control Registers . . . . . . . . . . . . . . . . . . . . . . .59<br />

Memory Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60<br />

Memory Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

8 Table of Contents<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Table of Contents<br />

Computer Architecture<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66<br />

Computer Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66<br />

CPU Registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67<br />

Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69<br />

CPU View of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70<br />

CPU Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73<br />

Detailed Operation of CPU Instructions . . . . . . . . . . . . . . . . . . . . .73<br />

Store Accumulator (Direct Addressing Mode). . . . . . . . . . . . . . .74<br />

Load Accumulator (Immediate Addressing Mode) . . . . . . . . . . .75<br />

Conditional Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76<br />

Subroutine Calls and Returns. . . . . . . . . . . . . . . . . . . . . . . . . . .76<br />

Playing Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80<br />

Resets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86<br />

RESET Pin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86<br />

Power-On Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86<br />

Watchdog Timer Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87<br />

Illegal Address Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87<br />

Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88<br />

External Interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91<br />

On-Chip Peripheral Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92<br />

Software Interrupt (SWI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92<br />

Interrupt Latency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92<br />

Nested Interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94<br />

<strong>M68HC05</strong> Instruction Set<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98<br />

Central Processor Unit (CPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98<br />

Arithmetic/Logic Unit (ALU). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98<br />

CPU Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99<br />

CPU Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99<br />

Accumulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

Index Register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

Condition Code Register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Table of Contents<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

9


Freescale Semiconductor, Inc...<br />

Table of Contents<br />

Freescale Semiconductor, Inc.<br />

Half-Carry Bit (H) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101<br />

Interrupt Mask Bit (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101<br />

Negative Bit (N). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101<br />

Zero Bit (Z) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102<br />

Carry/Borrow Bit (C) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102<br />

Program Counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103<br />

Stack Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103<br />

Addressing Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104<br />

Inherent Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105<br />

Immediate Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . .107<br />

Extended Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108<br />

Direct Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110<br />

Indexed Addressing Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112<br />

Indexed, No Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112<br />

Indexed, 8-Bit Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114<br />

Indexed, 16-Bit Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116<br />

Relative Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118<br />

Bit Test and Branch Instructions . . . . . . . . . . . . . . . . . . . . . . . . . .120<br />

Instructions Organized by Type . . . . . . . . . . . . . . . . . . . . . . . . . .120<br />

Instruction Set Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133<br />

CPU Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133<br />

Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133<br />

Instruction Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134<br />

Programming<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136<br />

Writing a Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136<br />

Flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137<br />

Mnemonic Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139<br />

Software Delay Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141<br />

Assembler Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143<br />

Object Code File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

10 Table of Contents<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Table of Contents<br />

Assembler Directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149<br />

Originate (ORG) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149<br />

Equate (EQU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149<br />

Form Constant Byte (FCB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150<br />

Form Double Byte (FDB). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150<br />

Reserve Memory Byte (RMB) . . . . . . . . . . . . . . . . . . . . . . . . . . . .151<br />

Set Default Number Base to Decimal . . . . . . . . . . . . . . . . . . . . . .152<br />

Instruction Set Dexterity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153<br />

Application Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156<br />

The Paced Loop<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159<br />

System Equates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160<br />

Register<br />

Equates for MC68HC705J1A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160<br />

Application System Equates . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161<br />

Vector Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162<br />

Reset Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162<br />

Unused Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163<br />

RAM Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165<br />

Paced Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165<br />

Loop Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167<br />

Loop System Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168<br />

Your Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168<br />

Timing Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169<br />

Stack Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170<br />

An Application-Ready Framework . . . . . . . . . . . . . . . . . . . . . . . . . . .171<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178<br />

On-Chip Peripheral Systems<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180<br />

Types of Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181<br />

Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181<br />

Serial Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Table of Contents<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

11


Freescale Semiconductor, Inc...<br />

Table of Contents<br />

Freescale Semiconductor, Inc.<br />

Analog-to-Digital Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

Digital-to-Analog Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

EEPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

Controlling Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

The MC68HC705J1A Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184<br />

A Timer Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187<br />

Using the PWM Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .195<br />

A Practical Motor Control Example . . . . . . . . . . . . . . . . . . . . . . . . . .198<br />

Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198<br />

Motor Control Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201<br />

Motor Control Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204<br />

Listing 6 <strong>—</strong> Speed Control Program Listing . . . . . . . . . . . . . . . . .210<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215<br />

Other Kinds of Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215<br />

Instruction Set Details<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219<br />

<strong>M68HC05</strong> Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221<br />

Reference Tables<br />

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287<br />

ASCII to Hexadecimal Conversion . . . . . . . . . . . . . . . . . . . . . . . . . .288<br />

Hexadecimal to Decimal Conversion. . . . . . . . . . . . . . . . . . . . . . . . .290<br />

Decimal to Hexadecimal Conversion. . . . . . . . . . . . . . . . . . . . . . . . .292<br />

Hexadecimal Values vs. <strong>M68HC05</strong> Instructions . . . . . . . . . . . . . . . .293<br />

Glossary<br />

Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299<br />

Index<br />

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

12 Table of Contents<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

List of Figures<br />

Figure Title Page<br />

1 Overall View of a Computer System . . . . . . . . . . . . . . . . . .18<br />

2 Expanded View of a Microcontroller. . . . . . . . . . . . . . . . . . .24<br />

3 N-Type and P-Type CMOS Transistors . . . . . . . . . . . . . . . .39<br />

4 CMOS Inverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40<br />

5 CMOS NAND Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41<br />

6 CMOS NOR Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42<br />

7 CMOS Transmission Gate . . . . . . . . . . . . . . . . . . . . . . . . . .44<br />

8 2:1 Data Multiplexer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45<br />

9 Three-State Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46<br />

10 Half Flip Flop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48<br />

11 Memory and I/O Circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . .58<br />

12 I/O Port with Data Direction Control . . . . . . . . . . . . . . . . . . .59<br />

13 Expanded Detail of One Memory Location. . . . . . . . . . . . . .60<br />

14 Typical Memory Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61<br />

15 <strong>M68HC05</strong> CPU Registers . . . . . . . . . . . . . . . . . . . . . . . . . .68<br />

16 Memory Map of Example Program. . . . . . . . . . . . . . . . . . . .72<br />

17 Subroutine Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . . .77<br />

18 Worksheet for Playing Computer . . . . . . . . . . . . . . . . . . . . .81<br />

19 Completed Worksheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82<br />

20 Hardware Interrupt Flowchart. . . . . . . . . . . . . . . . . . . . . . . .90<br />

21 Interrupt Stacking Order . . . . . . . . . . . . . . . . . . . . . . . . . . . .91<br />

22 <strong>M68HC05</strong> CPU Block Diagram . . . . . . . . . . . . . . . . . . . . . .98<br />

23 Programming Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99<br />

24 Accumulator (A) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

25 Index Register (X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

26 Condition Code Register (CCR) . . . . . . . . . . . . . . . . . . . . .101<br />

27 How Condition Codes are Affected<br />

by Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . .102<br />

28 Program Counter (PC) . . . . . . . . . . . . . . . . . . . . . . . . . . . .103<br />

29 Stack Pointer (SP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA List of Figures<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

13


Freescale Semiconductor, Inc...<br />

List of Figures<br />

Freescale Semiconductor, Inc.<br />

Figure Title Page<br />

30 Example Flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138<br />

31 Flowchart and Mnemonics . . . . . . . . . . . . . . . . . . . . . . . . .140<br />

32 Delay Routine Flowchart and Mnemonics . . . . . . . . . . . . .141<br />

33 Explanation of Assembler Listing . . . . . . . . . . . . . . . . . . . .145<br />

34 Syntax of an S1 Record . . . . . . . . . . . . . . . . . . . . . . . . . . .148<br />

35 S-Record File for Example Program . . . . . . . . . . . . . . . . .148<br />

36 Four Ways to Check a Switch . . . . . . . . . . . . . . . . . . . . . .153<br />

37 Flowchart of Main Paced Loop. . . . . . . . . . . . . . . . . . . . . .166<br />

38 Flowchart of RTI Service Routine. . . . . . . . . . . . . . . . . . . .167<br />

39 15-Stage Multifunction Timer Block Diagram . . . . . . . . . . .185<br />

40 PWM Waveforms with Various Duty Cycles. . . . . . . . . . . .187<br />

41 Portion of the MC68HC705J1A Timer . . . . . . . . . . . . . . . .188<br />

42 PWM With 16 Discrete Duty Cycle Outputs . . . . . . . . . . . .189<br />

43 Each TOF Interrupt Sliced into 16 Separate<br />

Time Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191<br />

44 Timer Interrupt Service Routine . . . . . . . . . . . . . . . . . . . . .192<br />

45 Real-Time Interrupt Routine Flowchart . . . . . . . . . . . . . . .193<br />

46 Timer Overflow Interrupt Flowchart . . . . . . . . . . . . . . . . . .194<br />

47 Motor Speed Controlled by a Variable Resistor . . . . . . . . .199<br />

48 Motor Speed Controlled by a Transistor. . . . . . . . . . . . . . .199<br />

49 Transistor Used as an Electronic Switch . . . . . . . . . . . . . .200<br />

50 PWM Waveforms with 50 and 80 Percent<br />

Duty Cycles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201<br />

51 Power Section of the Motor Speed Control Circuit. . . . . . .203<br />

52 Microcontroller Section of the Motor<br />

Speed Control Circuit . . . . . . . . . . . . . . . . . . . . . . . . . .203<br />

53 Revised RTI Routine Flowchar. . . . . . . . . . . . . . . . . . . . . .205<br />

54 Flowchart for Main Program Loop . . . . . . . . . . . . . . . . . . .206<br />

55 Flowchart for MotorOn/Off Routine . . . . . . . . . . . . . . . . . .208<br />

56 Flowchart for Motor Speed-Up Routine . . . . . . . . . . . . . . .209<br />

57 Flowchart for Motor Speed-Down Routine . . . . . . . . . . . . .209<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

14 List of Figures<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

List of Tables<br />

Table Title Page<br />

1 Decimal, Binary, and Hexadecimal Equivalents .....................29<br />

2 ASCII to Hexadecimal Conversion ........................................31<br />

3 Octal, Binary, and Hexadecimal Equivalents..........................33<br />

4 Decimal, BCD, and Binary Equivalents ..................................35<br />

5 Inverter Gate Operation..........................................................40<br />

6 NAND Gate-Level Operation ..................................................41<br />

7 NOR Gate Truth Table ...........................................................43<br />

8 Data Multiplexer Operation.....................................................46<br />

9 Buffer Gate Operation ............................................................47<br />

10 Vector Addresses for Resets and Interrupts<br />

on the MC68HC705J1A.....................................................89<br />

11 Register/Memory Instructions...............................................121<br />

12 Read/Modify-Write Instructions ............................................122<br />

13 Branch Instructions...............................................................123<br />

14 Control Instructions...............................................................124<br />

15 Instruction Set Summary ......................................................126<br />

16 <strong>M68HC05</strong> Instruction Set Opcode Map................................132<br />

17 RTI and COP Timer Rates (E clock = 2 MHz)......................186<br />

18 PWM Characteristics for Various RTI Rates ........................190<br />

19 Hexadecimal to ASCII Conversion ......................................289<br />

20 Hexadecimal to Decimal Conversion....................................291<br />

21 Hexadecimal to <strong>M68HC05</strong> Instruction Mnemonics...............293<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA List of Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

15


Freescale Semiconductor, Inc...<br />

List of Tables<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

16 List of Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

What is a Microcontroller?<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17<br />

Overall View of a Computer System . . . . . . . . . . . . . . . . . . . . . . . . . .18<br />

Computer System Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19<br />

Computer System Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20<br />

Central Processor Unit (CPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20<br />

Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21<br />

Computer Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21<br />

Computer Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23<br />

The Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25<br />

The Parts of Any Computer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25<br />

Kinds of Computers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25<br />

This chapter sets the groundwork for a detailed exploration of the inner<br />

workings of a small microcontroller. We will see that the microcontroller<br />

is one of the most basic forms of computer system. Although much<br />

smaller than its cousins <strong>—</strong> personal computers and mainframe<br />

computers <strong>—</strong> microcontrollers are built from the same basic elements.<br />

In the simplest sense, computers produce a specific pattern of outputs<br />

based on current inputs and the instructions in a computer program.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

17


Freescale Semiconductor, Inc...<br />

What is a Microcontroller?<br />

Overall View of a Computer System<br />

Freescale Semiconductor, Inc.<br />

Like most computers, microcontrollers are simply general-purpose<br />

instruction executors. The real star of a computer system is a program<br />

of instructions that is provided by a human programmer. This program<br />

instructs the computer to perform long sequences of very simple actions<br />

to accomplish useful tasks as intended by the programmer.<br />

Figure 1 provides a high level view of a computer system. By simply<br />

changing the types of input and output devices, this could be a view of a<br />

personal computer, a room-sized mainframe computer, or a simple<br />

microcontroller (MCU). The input and output (I/O) devices shown in the<br />

figure happen to be typical I/O devices found in a microcontroller<br />

computer system.<br />

SWITCH<br />

1 2 3 A<br />

4 5 6 B<br />

7 8 9 C<br />

< 0 > !<br />

KEYPAD<br />

INPUTS<br />

PROGRAM<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

°F<br />

TEMPERATURE<br />

SENSOR<br />

MEMORY<br />

CENTRAL<br />

PROCESSOR UNIT<br />

(CPU)<br />

CRYSTAL<br />

Figure 1. Overall View of a Computer System<br />

18 What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

CLOCK<br />

OUTPUTS<br />

LED LAMP<br />

BEEPER<br />

RELAY


Freescale Semiconductor, Inc...<br />

Computer System Inputs<br />

Freescale Semiconductor, Inc.<br />

What is a Microcontroller?<br />

Computer System Inputs<br />

Input devices supply information to the computer system from the<br />

outside world. In a personal computer system, the most common input<br />

device is the typewriter-style keyboard. Mainframe computers use<br />

keyboards and punched card readers as input devices. Microcontroller<br />

computer systems usually use much simpler input devices such as<br />

individual switches or small keypads, although much more exotic input<br />

devices are found in many microcontroller-based systems. An example<br />

of an exotic input device for a microcontroller is the oxygen sensor in an<br />

automobile that measures the efficiency of combustion by sampling the<br />

exhaust gases.<br />

Most microcontroller inputs can only process digital input signals at the<br />

same voltage levels as the main logic power source. The 0-volt ground<br />

level is called VSS and the positive power source (VDD) is typically 5 Vdc<br />

(direct current). A level of approximately 0 volts indicates a logic 0signal<br />

and a voltage approximately equal to the positive power source indicates<br />

a logic 1 signal.<br />

Of course, the real world is full of analog signals or signals that are some<br />

other voltage level. Some input devices translate signal voltages from<br />

some other level to the VDD and VSS levels needed for the<br />

microcontroller. Other input devices convert analog signals into digital<br />

signals (binary values made up of 1s and 0s) that the computer can<br />

understand and manipulate. Some microcontrollers even include such<br />

analog-to-digital converter circuits on the same integrated circuit.<br />

Transducers can be used to translate other real-world signals into logic<br />

level signals that a microcontroller can understand and manipulate.<br />

Some examples include temperature transducers, pressure sensors,<br />

light level detectors, and so forth. With such transducers, almost any<br />

physical property can be used as an input to a computer system.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

19


Freescale Semiconductor, Inc...<br />

What is a Microcontroller?<br />

Computer System Outputs<br />

Central Processor Unit (CPU)<br />

Freescale Semiconductor, Inc.<br />

Output devices are used to communicate information or actions from the<br />

computer system to the outside world. In a personal computer system,<br />

the most common output device is the CRT (cathode ray tube) display.<br />

Microcontroller systems often use much simpler output devices such as<br />

individual indicator lamps or beepers.<br />

Translation circuits (sometimes built into the same integrated circuit as<br />

the microcomputer) can convert digital signals into analog voltage levels.<br />

If necessary, other circuits can translate VDD and VSS levels that are<br />

native to an MCU into other voltage levels.<br />

The “controller” in microcontroller comes from the fact that these small<br />

computer systems usually control something as compared to a personal<br />

computer that usually processes information. In the case of the personal<br />

computer, most output is information (either displayed on a CRT screen<br />

or printed on paper). In contrast, in a microcontroller system, most<br />

outputs are logic level digital signals that are used to drive display LEDs<br />

(light-emitting diodes) or electrical devices such as relays or motors.<br />

The CPU is at the center of every computer system. The job of the CPU<br />

is to obediently execute the program of instructions that was supplied by<br />

the programmer. A computer program instructs the CPU to read<br />

information from inputs, to read information from and write information to<br />

working memory, and to write information to outputs. Some program<br />

instructions involve simple decisions that cause the program to either<br />

continue with the next instruction or to skip to a new place in the<br />

program. In a later chapter, we will look closely at the set of available<br />

instructions for a particular microcontroller.<br />

In mainframe and personal computers, there are actual layers of<br />

programs, starting with internal programs, that control the most basic<br />

operations of the computer. Another layer includes user programs that<br />

are loaded into the computer system memory when they are about to be<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

20 What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Clock<br />

Computer Memory<br />

Freescale Semiconductor, Inc.<br />

What is a Microcontroller?<br />

Clock<br />

used. This structure is very complex and would not be a good example<br />

for showing a beginner how a computer works.<br />

In a microcontroller, usually only one program is at work in a particular<br />

control application. For example, the <strong>M68HC05</strong> CPU recognizes only<br />

about 60 different instructions, but these are representative of the<br />

instruction sets of any computer system. This kind of computer system<br />

is a good model for learning the basics of computer operation because<br />

it is possible to know exactly what is happening at every tiny step as the<br />

CPU executes a program.<br />

With very few exceptions, computers use a small clock oscillator to<br />

trigger the CPU to move from one step in a sequence to the next. In the<br />

chapter on computer architecture, we will see that even the simple<br />

instructions of a microcontroller are broken down into a series of even<br />

more basic steps. Each of these tiny steps in the operation of the<br />

computer takes one cycle of the CPU clock.<br />

Several kinds of computer memory are used for various purposes in<br />

computer systems. The main kinds of memory found in microcontroller<br />

systems are:<br />

• Read-only memory (ROM)<br />

• Random access read/write memory (RAM)<br />

ROM is used mainly for programs and permanent data that must remain<br />

unchanged even when there is no power applied to the microcontroller.<br />

RAM is used for temporary storage of data and intermediate calculation<br />

results during operation.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

21


Freescale Semiconductor, Inc...<br />

What is a Microcontroller?<br />

Freescale Semiconductor, Inc.<br />

Some microcontrollers include other kinds of memory, such as:<br />

• Erasable programmable read-only memory (EPROM)<br />

• Electrically erasable programmable read-only memory<br />

(EEPROM)<br />

We will learn more about these kinds of memory in a later chapter.<br />

The smallest unit of computer memory is a single bit that can store one<br />

value of 0 or 1. These bits are grouped into sets of eight bits to make one<br />

byte. Larger computers further group bits into sets of 16 or 32 to make<br />

a unit called a word. The size of a word can be different for different<br />

computers, but a byte is always eight bits.<br />

Personal computers work with very large programs and large amounts<br />

of data, so they use special forms of memory called mass storage<br />

devices. Floppy disks, hard disks, and compact discs are memory<br />

devices of this type. It is not unusual to find several million bytes of RAM<br />

memory in a personal computer. Even this is not enough to hold the<br />

large programs and data used by personal computers, so most personal<br />

computers also include a hard disk with tens or even hundreds of<br />

millions or even billions of bytes of storage capacity. Compact discs,<br />

very similar to those used for popular music recordings, have a capacity<br />

of about 600 million bytes of read-only memory. In comparison, the small<br />

microcontroller systems we are discussing in this book typically have a<br />

total of 1,000 to 64,000 bytes of memory.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

22 What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Computer Program<br />

The Microcontroller<br />

Freescale Semiconductor, Inc.<br />

What is a Microcontroller?<br />

Computer Program<br />

Figure 1 shows the program as a cloud because it originates in the<br />

imagination of a computer programmer or engineer. This is comparable<br />

to an electrical engineer thinking up a new circuit or a mechanical<br />

engineer figuring out a new assembly. The components of a program are<br />

instructions from the instruction set of the CPU. Just as a circuit designer<br />

can build an adder circuit out of simple AND, OR, and NOT elements, a<br />

programmer can write a program to add numbers together out of simple<br />

instructions.<br />

Programs are stored in the memory of a computer system where they<br />

can be sequentially executed by the CPU. In the chapter on<br />

programming, we will learn how to write programs and prepare them for<br />

loading into the memory of a computer.<br />

Now that we have discussed the various parts of a computer system, we<br />

are ready to talk about just what a microcontroller is. The top half of<br />

Figure 2 shows a generic computer system with a portion enclosed in a<br />

dashed outline. This outlined portion is a microcontroller and the lower<br />

half of the figure is a block diagram showing its internal structure in<br />

greater detail. The crystal is not contained within the microcontroller, but<br />

it is a required part of the oscillator circuit. In some cases, a less<br />

expensive component such as a ceramic resonator or a<br />

resistor-capacitor (R-C) circuit may be used instead of this crystal.<br />

A microcontroller can be defined as a complete computer system<br />

including a CPU, memory, a clock oscillator, and I/O on a single<br />

integrated circuit chip. When some of these elements such as the I/O or<br />

memory are missing, the integrated circuit would be called a<br />

microprocessor. The CPU in a personal computer is a microprocessor.<br />

The CPU in a mainframe computer is made up of many integrated<br />

circuits.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

23


Freescale Semiconductor, Inc...<br />

What is a Microcontroller?<br />

Freescale Semiconductor, Inc.<br />

SWITCH<br />

1 2 3 A<br />

4 5 6 B<br />

7 8 9 C<br />

< 0 > !<br />

KEYPAD<br />

POWER<br />

GROUND<br />

DIGITAL<br />

INPUTS<br />

RESET<br />

CRYSTAL<br />

VDD<br />

VSS<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

°F<br />

TEMPERATURE<br />

SENSOR<br />

INPUTS<br />

ADDRESS BUS<br />

PROGRAM<br />

MEMORY<br />

CENTRAL<br />

PROCESSOR UNIT<br />

CPU<br />

CRYSTAL<br />

DATA<br />

MEMORY<br />

CENTRAL PROCESSING UNIT<br />

CPU<br />

Figure 2. Expanded View of a Microcontroller<br />

24 What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

CLOCK<br />

PROGRAM<br />

MEMORY<br />

I/O &<br />

PERIPHERALS<br />

OSCILLATOR<br />

&<br />

CLOCKS<br />

DATA BUS<br />

OUTPUTS<br />

LED LAMP<br />

BEEPER<br />

RELAY<br />

DIGITAL<br />

OUTPUTS


Freescale Semiconductor, Inc...<br />

Review<br />

The Parts<br />

of Any Computer<br />

Kinds<br />

of Computers<br />

Freescale Semiconductor, Inc.<br />

What is a Microcontroller?<br />

Review<br />

A microcontroller is a complete computer system, including a CPU,<br />

memory, a clock oscillator, and I/O on a single integrated circuit chip.<br />

The parts of any computer are:<br />

• A central processor unit (CPU)<br />

• A clock to sequence the CPU<br />

• Memory for instructions and data<br />

• Inputs to get information into the computer system<br />

• Outputs to get information out of the computer system<br />

• A program to make the computer do something useful<br />

Although all computers share the same basic elements and ideas, there<br />

are different kinds of computers for different purposes.<br />

• For instance, mainframe computers are very large computer<br />

systems that are used for big information processing jobs such as<br />

checking the tax returns for all of the taxpayers in a region.<br />

• Personal computers are small versions of mainframe computers<br />

that are used for smaller tasks such as word processing and<br />

engineering drawing.<br />

• <strong>Microcontrollers</strong> are very small single-chip computers that are<br />

used for such things as controlling a small appliance.<br />

The smallest microcontrollers are used for such things as converting the<br />

movements of a computer mouse into serial data for a personal<br />

computer. Very often microcontrollers are embedded into a product and<br />

the user of the product may not even know there is a computer inside.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

25


Freescale Semiconductor, Inc...<br />

What is a Microcontroller?<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

26 What is a Microcontroller?<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

Computer Numbers and Codes<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27<br />

Binary and Hexadecimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . .28<br />

ASCII Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30<br />

Computer Operation Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30<br />

Instruction Mnemonics and Assemblers . . . . . . . . . . . . . . . . . . . . . . .32<br />

Octal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32<br />

Binary Coded Decimal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36<br />

This chapter discusses binary, hexadecimal, octal, and binary coded<br />

decimal (BCD) numbers which are commonly used by computers.<br />

Computers work best with information in a different form than people use<br />

to solve problems. Humans typically work in the base 10 (decimal)<br />

numbering system (probably because we have 10 fingers). Digital binary<br />

computers work in the base 2 (binary) numbering system because this<br />

allows all information to be represented by sets of digits, which can only<br />

be 0s or 1s. In turn, a 1 or 0 can be represented by the presence or<br />

absence of a logic voltage on a signal line or the on and off states of a<br />

simple switch.<br />

Computers also use special codes to represent alphabetic information<br />

and computer instructions. <strong>Understanding</strong> these codes will help you<br />

understand how computers can do so much with strings of digits that can<br />

only be 1s or 0s.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

27


Freescale Semiconductor, Inc...<br />

Computer Numbers and Codes<br />

Binary and Hexadecimal Numbers<br />

Freescale Semiconductor, Inc.<br />

In decimal (base 10) numbers, the weight of each digit is 10 times as<br />

great as the digit immediately to its right. The rightmost digit of a decimal<br />

integer is the ones place, the digit to its left is the tens digit, and so on.<br />

In binary (base 2) numbers, the weight of each digit is two times as<br />

great as the digit immediately to its right. The rightmost digit of the binary<br />

integer is the ones digit, the next digit to the left is the twos digit, next is<br />

the fours digit, then the eights digit, and so on.<br />

Although computers are quite comfortable working with binary numbers<br />

of 8, 16, or even 32 binary digits, humans find it inconvenient to work with<br />

so many digits at a time. The base 16 (hexadecimal) numbering system<br />

offers a practical compromise. One hexadecimal digit can exactly<br />

represent four binary digits, thus, an 8-bit binary number can be<br />

expressed by two hexadecimal digits.<br />

The correspondence between a hexadecimal digit and the four binary<br />

digits it represents is simple enough that humans who work with<br />

computers easily learn to mentally translate between the two. In<br />

hexadecimal (base 16) numbers, the weight of each digit is 16 times as<br />

great as the digit immediately to its right. The rightmost digit of a<br />

hexadecimal integer is the ones place, the digit to its left is the sixteens<br />

digit, and so on.<br />

Table 1 demonstrates the relationship among the decimal, binary, and<br />

hexadecimal representations of values. These three different numbering<br />

systems are just different ways to represent the same physical<br />

quantities. The letters A through F are used to represent the<br />

hexadecimal values corresponding to 10 through 15 because each<br />

hexadecimal digit can represent 16 different quantities; whereas, our<br />

customary numbers only include the 10 unique symbols (0 through 9).<br />

Thus, some other single-digit symbols had to be used to represent the<br />

hexadecimal values for 10 through 15.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

28 Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Numbers and Codes<br />

Binary and Hexadecimal Numbers<br />

Table 1. Decimal, Binary, and Hexadecimal Equivalents<br />

Base 10 Decimal Base 2 Binary Base 16 Hexadecimal<br />

0 0000 0<br />

1 0001 1<br />

2 0010 2<br />

3 0011 3<br />

4 0100 4<br />

5 0101 5<br />

6 0110 6<br />

7 0111 7<br />

8 1000 8<br />

9 1001 9<br />

10 1010 A<br />

11 1011 B<br />

12 1100 C<br />

13 1101 D<br />

14 1110 E<br />

15 1111 F<br />

16 0001 0000 10<br />

17 0001 0001 11<br />

100 0110 0100 64<br />

255 1111 1111 FF<br />

1024 0100 0000 0000 400<br />

65,535 1111 1111 1111 1111 FFFF<br />

To avoid confusion about whether a number is hexadecimal or decimal,<br />

place a $ symbol before hexadecimal numbers. For example, 64 means<br />

decimal “sixty-four”; whereas, $64 means hexadecimal “six-four,” which<br />

is equivalent to decimal 100. Some computer manufacturers follow<br />

hexadecimal values with a capital H (as in 64H).<br />

Hexadecimal is a good way to express and discuss numeric information<br />

processed by computers because it is easy for people to mentally<br />

convert between hexadecimal digits and their 4-bit binary equivalent.<br />

The hexadecimal notation is much more compact than binary while<br />

maintaining the binary connotations.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

29


Freescale Semiconductor, Inc...<br />

Computer Numbers and Codes<br />

ASCII Code<br />

Computer Operation Codes<br />

Freescale Semiconductor, Inc.<br />

Computers must handle many kinds of information other than just<br />

numbers. Text (alphanumeric characters) and instructions must be<br />

encoded in such a way that the computer can understand this<br />

information. The most common code for text information is the American<br />

Standard Code for Information Interchange (or ASCII). The ASCII code<br />

establishes a widely accepted correlation between alphanumeric<br />

characters and specific binary values. Using the ASCII code, $41<br />

corresponds to capital A, $20 corresponds to a space character, etc. The<br />

ASCII code translates characters to 7-bit binary codes, but in practice<br />

the information is most often conveyed as 8-bit characters with the most<br />

significant bit equal to 0. This standard code allows equipment made by<br />

various manufacturers to communicate because all of the machines use<br />

this same code.<br />

Table 2 shows the relationship between ASCII characters and<br />

hexadecimal values.<br />

Computers use another code to give instructions to the CPU. This code<br />

is called an operation code or opcode. Each opcode instructs the CPU<br />

to execute a very specific sequence of steps that together accomplish an<br />

intended operation. Computers from different manufacturers use<br />

different sets of opcodes because these opcodes are internally<br />

hard-wired in the CPU logic. The instruction set for a specific CPU is<br />

the set of all operations that the CPU knows how to perform. Opcodes<br />

are one representation of the instruction set and mnemonics are<br />

another. Even though the opcodes differ from one computer to another,<br />

all digital binary computers perform the same kinds of basic tasks in<br />

similar ways. For instance, the CPU in the MC68HC05 MCU can<br />

understand 62 basic instructions. Some of these basic instructions have<br />

several slight variations, each requiring a separate opcode. The<br />

instruction set of the MC68HC05 is represented by 210 unique<br />

instruction opcodes. We will discuss how the CPU actually executes<br />

instructions in another chapter, but first we need to understand a few<br />

more basic concepts.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

30 Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Numbers and Codes<br />

Computer Operation Codes<br />

Table 2. ASCII to Hexadecimal Conversion<br />

Hex ASCII Hex ASCII Hex ASCII Hex ASCII<br />

$00 NUL $20<br />

SP<br />

space<br />

$40 @ $60 `<br />

grave<br />

$01 SOH $21 ! $41 A $61 a<br />

$02 STX $22 “ $42 B $62 b<br />

$03 ETX $23 # $43 C $63 c<br />

$04 EOT $24 $ $44 D $64 d<br />

$05 ENQ $25 % $45 E $65 e<br />

$06 ACK $26 & $46 F $66 f<br />

$07<br />

BEL<br />

beep<br />

$27<br />

‘<br />

apost.<br />

$47 G $67 g<br />

$08<br />

BS<br />

back sp<br />

$28 ( $48 H $68 h<br />

$09<br />

HT<br />

tab<br />

$29 ) $49 I $69 i<br />

$0A<br />

LF<br />

linefeed<br />

$2A * $4A J $6A j<br />

$0B VT $2B + $4B K $6B k<br />

$0C FF $2C<br />

,<br />

comma<br />

$4C L $6C l<br />

$0D<br />

CR<br />

return<br />

$2D<br />

–<br />

dash<br />

$4D M $6D m<br />

$0E SO $2E<br />

.<br />

period<br />

$4E N $6E n<br />

$0F SI $2F / $4F O $6F o<br />

$10 DLE $30 0 $50 P $70 p<br />

$11 DC1 $31 1 $51 Q $71 q<br />

$12 DC2 $32 2 $52 R $72 r<br />

$13 DC3 $33 3 $53 S $73 s<br />

$14 DC4 $34 4 $54 T $74 t<br />

$15 NAK $35 5 $55 U $75 u<br />

$16 SYN $36 6 $56 V $76 v<br />

$17 ETB $37 7 $57 W $77 w<br />

$18 CAN $38 8 $58 X $78 x<br />

$19 EM $39 9 $59 Y $79 y<br />

$1A SUB $3A : $5A Z $7A z<br />

$1B ESCAPE $3B ; $5B [ $7B {<br />

$1C FS $3C < $5C \ $7C |<br />

$1D GS $3D = $5D ] $7D }<br />

$1E RS $3E > $5E ^ $7E ~<br />

$1F US $3F ? $5F<br />

_<br />

under<br />

$7F<br />

DEL<br />

delete<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

31


Freescale Semiconductor, Inc...<br />

Computer Numbers and Codes<br />

Instruction Mnemonics and Assemblers<br />

Octal<br />

Freescale Semiconductor, Inc.<br />

An opcode such as $4C is understood by the CPU, but it is not very<br />

meaningful to a human. To solve this problem, a system of mnemonic<br />

instruction equivalents is used. The $4C opcode corresponds to the<br />

INCA mnemonic, which is read “increment accumulator.” Although there<br />

is printed information to show the correlation between mnemonic<br />

instructions and the opcodes they represent, this information is seldom<br />

used by a programmer because the translation process is handled<br />

automatically by a separate computer program called an assembler.An<br />

assembler is a program that converts a program written in mnemonics<br />

into a list of machine codes (opcodes and other information) that can<br />

be used by a CPU.<br />

An engineer develops a set of instructions for the computer in mnemonic<br />

form and then uses an assembler to translate these instructions into<br />

opcodes that the CPU can understand. We will discuss instructions,<br />

writing programs, and assemblers in other chapters. However, you<br />

should understand now that people prepare instructions for a computer<br />

in mnemonic form, but the computer understands only opcodes; thus, a<br />

translation step is required to change the mnemonics to opcodes, and<br />

this is the function of the assembler.<br />

Before leaving this discussion of number systems and codes, we will<br />

look at two additional codes you may have heard about. Octal (base 8)<br />

notation was used for some early computer work but is seldom used<br />

today. Octal notation used the numbers 0 through 7 to represent sets of<br />

three binary digits in the same way hexadecimal is used to represent<br />

sets of four binary digits. The octal system had the advantage of using<br />

customary number symbols, unlike the hexadecimal symbols A through<br />

F discussed earlier.<br />

Two disadvantages caused octal to be abandoned for the hexadecimal<br />

notation used today. First of all, most computers use 4, 8, 16, or 32 bits<br />

per word; these words do not break down evenly into sets of three bits.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

32 Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Numbers and Codes<br />

Octal<br />

(Some early computers used 12-bit words that did break down into four<br />

sets of three bits each.) The second problem was that octal is not as<br />

compact as hexadecimal. For example, the ASCII value for capital A is<br />

10000012 in binary, 4116 in hexadecimal, and 1018 in octal. When a<br />

human is talking about the ASCII value for A, it is easier to say “four-one”<br />

than it is to say “one-zero-one.”<br />

Table 3 demonstrates the translation between octal and binary. The<br />

“direct binary” column shows the digit-by-digit translation of octal digits<br />

into sets of three binary bits. The leftmost (ninth) bit is shown in bold italic<br />

typeface. This bold italic 0 is discarded to get the desired 8-bit result. The<br />

“8-bit binary” column has the same binary information as the direct<br />

binary column, except the bits are regrouped into sets of four. Each set<br />

of four bits translates exactly into one hexadecimal digit.<br />

Table 3. Octal, Binary, and Hexadecimal Equivalents<br />

Octal Direct Binary 8-Bit Binary Hexadecimal<br />

000 000 000 000 0000 0000 $00<br />

001 000 000 001 0000 0001 $01<br />

002 000 000 010 0000 0010 $02<br />

003 000 000 011 0000 0011 $03<br />

004 000 000 100 0000 0100 $04<br />

005 000 000 101 0000 0101 $05<br />

006 000 000 110 0000 0110 $06<br />

007 000 000 111 0000 0111 $07<br />

010 000 001 000 0000 1000 $08<br />

011 000 001 001 0000 1001 $09<br />

012 000 001 010 0000 1010 $0A<br />

013 000 001 011 0000 1011 $0B<br />

014 000 001 100 0000 1100 $0C<br />

015 000 001 101 0000 1101 $0D<br />

016 000 001 110 0000 1110 $0E<br />

017 000 001 111 0000 1111 $0F<br />

101 001 000 001 0100 0001 $41<br />

125 001 010 101 0101 0101 $55<br />

252 010 101 010 1010 1010 $AA<br />

377 011 111 111 1111 1111 $FF<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

33


Freescale Semiconductor, Inc...<br />

Computer Numbers and Codes<br />

Binary Coded Decimal<br />

Freescale Semiconductor, Inc.<br />

When mentally translating octal values to binary byte values, the octal<br />

value is represented by three octal digits. Each octal digit represents<br />

three binary bits so there is one extra bit (3 digits × 3 bits = 9 bits). Since<br />

Western-speaking people typically work from left to right, it is easy to<br />

forget to throw away the leftmost extra bit from the leftmost octal digit<br />

and end up with an extra (ninth) bit. When translating from hexadecimal<br />

to binary, it is easier because each hexadecimal digit translates into<br />

exactly four binary bits. Two hexadecimal digits exactly match the eight<br />

binary bits in a byte.<br />

Binary coded decimal (BCD) is a hybrid notation used to express<br />

decimal values in binary form. BCD uses four binary bits to represent<br />

each decimal digit. Since four binary digits can express 16 different<br />

physical quantities, there will be six bit-value combinations that are<br />

considered invalid (specifically, the hexadecimal values A through F).<br />

BCD values are shown with a $ sign because they are actually<br />

hexadecimal numbers that represent decimal quantities.<br />

When the computer does a BCD add operation, it performs a binary<br />

addition and then adjusts the result back to BCD form. As a simple<br />

example, consider the following BCD addition.<br />

910 + 110 = 1010<br />

The computer adds<br />

0000 10012 + 0000 00012 = 0000 10102<br />

But 10102 is equivalent to A16, which is not a valid BCD value. When the<br />

computer finishes the calculation, a check is performed to see if the<br />

result is still a valid BCD value. If there was any carry from one BCD digit<br />

to another or if there was any invalid code, a sequence of steps would<br />

be performed to correct the result to proper BCD form. The 0000 10102<br />

is corrected to 0001 00002 (BCD 10) in this example.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

34 Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Numbers and Codes<br />

Binary Coded Decimal<br />

Table 4. Decimal, BCD, and Binary Equivalents<br />

Decimal BCD Binary<br />

Hexadecimal<br />

(reference)<br />

0 $0 0000 $0<br />

1 $1 0001 $1<br />

2 $2 0010 $2<br />

3 $3 0011 $3<br />

4 $4 0100 $4<br />

5 $5 0101 $5<br />

6 $6 0110 $6<br />

7 $7 0111 $7<br />

8 $8 1000 $8<br />

9 $9 1001 $9<br />

1010 $A<br />

1011 $B<br />

Invalid 1100 $C<br />

BCD 1101 $D<br />

Combinations 1110 $E<br />

1111 $F<br />

10 $10 0001 0000 $10<br />

99 $99 1001 1001 $99<br />

In most cases, using BCD notation in computer calculations is inefficient.<br />

It is better to change from decimal to binary as information is entered, do<br />

all computer calculations in binary, and change the binary result back to<br />

BCD or decimal as needed for display. This is true because: First, not all<br />

microcontrollers are capable of doing BCD calculations because they<br />

need a digit-to-digit carry indicator that is not present on all computers<br />

(although Motorola MCUs do have this half-carry indicator). And,<br />

second, forcing the computer to emulate human behavior is inherently<br />

less efficient than allowing the computer to work in its native binary<br />

system.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

35


Freescale Semiconductor, Inc...<br />

Computer Numbers and Codes<br />

Review<br />

Freescale Semiconductor, Inc.<br />

Computers have two logic levels (0 and 1) so they work in the binary<br />

numbering system. Probably because people have 10 fingers, they work<br />

in the base 10 decimal numbering system.<br />

Hexadecimal numbers use the 16 symbols 0 through 9 and A through F.<br />

Each hexadecimal digit can represent a set of four binary digits exactly.<br />

Table 2 shows the decimal, binary, and hexadecimal equivalents of<br />

various values. A $ symbol is used before a hexadecimal valueor an H<br />

is used after a hexadecimal value to distinguish it from decimal numbers.<br />

ASCII is a widely accepted code that allows alphanumeric information to<br />

be represented as binary values.<br />

Each instruction or variation of an instruction has a unique opcode<br />

(binary value) that the CPU recognizes as a request to perform a specific<br />

instruction. CPUs from different manufacturers have different sets of<br />

opcodes.<br />

Programmers specify instructions by a mnemonic such as INCA. A<br />

computer program, called an assembler, translates mnemonic<br />

instructions into opcodes the CPU can understand.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

36 Computer Numbers and Codes<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37<br />

Logic Levels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38<br />

CMOS Transistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39<br />

Simple Gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40<br />

Inverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40<br />

NAND Gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41<br />

NOR Gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42<br />

Transmission Gates, Buffers, and Flip Flops. . . . . . . . . . . . . . . . . . . .44<br />

Transmission Gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44<br />

Three-State Buffer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46<br />

Half Flip Flop (HFF). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49<br />

Digital computers are made up of relatively simple logic elements<br />

sometimes called gates, which are small circuits that can be connected<br />

in various ways to manipulate logic-level signal voltages. Although this<br />

textbook is not intended to provide detailed information on logic design,<br />

some knowledge of the most basic logic elements will help you<br />

understand the inner workings of microcontrollers.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

37


Freescale Semiconductor, Inc...<br />

Basic Logic Elements<br />

Logic Levels<br />

Freescale Semiconductor, Inc.<br />

This chapter begins with a close look at the requirements for logic-level<br />

voltages. Transistors and interconnections for a typical CMOS<br />

(complementary metal-oxide semiconductor) microcontroller are<br />

discussed. A simple inverter, NAND gate, and NOR gate are explained.<br />

Finally, a transmission gate, a three-state buffer, and a flip-flop circuit<br />

are described. Virtually any part of a microcontroller can be explained in<br />

terms of these few simple logic elements.<br />

Earlier, in the discussion of what a microcontroller is, we said a level of<br />

approximately 0 volts indicates a logic 0 and a voltage approximately<br />

equal to the positive power source indicates a logic 1 signal. To be more<br />

precise, there is a voltage level below which the microcontroller<br />

manufacturer guarantees that a signal will be recognized as a valid<br />

logic 0. Similarly, there is a voltage level above which the microcontroller<br />

manufacturer guarantees that a signal will be recognized as a valid<br />

logic 1. When designing a microcontroller system, be sure that all signals<br />

conform to these specified limits, even under worst-case conditions.<br />

Most modern microcontrollers use a technology called complementary<br />

metal-oxide semiconductor (CMOS). This means the circuits include<br />

both N-type and P-type transistors. Transistors will be explained in<br />

greater detail later in this chapter.<br />

In a typical CMOS circuit, a logic 0 input may be specified as 0.0 volts to<br />

0.3 times VDD. If VDD is 5.0 volts, this translates to the range 0.0 to<br />

1.5 volts. A logic 1 input may be specified as 0.7 times VDD to VDD. If<br />

VDD is 5.0 volts, this translates to the range 3.5 to 5.0 volts.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

38 Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

CMOS Transistors<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

CMOS Transistors<br />

Figure 3 shows the symbols for an N-type and a P-type CMOS<br />

transistor. The exact characteristics of these transistors can be<br />

determined by their physical layout, size, and shape. For the purposes<br />

of this textbook, they may be treated as simple switching devices.<br />

Figure 3. N-Type and P-Type CMOS Transistors<br />

The N-type transistor in Figure 3 has its source terminal [3] connected<br />

to ground. For an N-type transistor to be on (conducting), its gate voltage<br />

[2] must be higher than its source voltage [3] by an amount known as a<br />

threshold. This N-type transistor is said to be on (conducts between<br />

terminals [1] and [3]) when there is a logic 1 voltage on its gate [2]. When<br />

the gate is at logic 0, this N-type transistor is said to be off and acts as<br />

an open circuit between terminals [1] and [3].<br />

The P-type transistor in Figure 3 has its source terminal [4] connected<br />

to VDD. For a P-type transistor to be on, its gate voltage [5] must be lower<br />

than its source voltage [4] by an amount known as a threshold. A P-type<br />

transistor is indicated by the small opened circle at its gate [5]. When<br />

there is a logic 0 voltage on the gate [5] of this P-type transistor, it is said<br />

to be on and acts like there is a short circuit between terminals [4] and<br />

[6]. When the gate is at logic 1, this P-type transistor is off and acts as<br />

an open circuit between terminals [4] and [6].<br />

It is relatively easy to assemble thousands of N- and P-type transistors<br />

on a single microcontroller integrated circuit and to connect them in<br />

various ways to perform complex logical operations. In the following<br />

paragraphs, we look at some of the most basic logic circuits that are<br />

found in a microcontroller.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

39<br />

[2]<br />

N-TYPE<br />

[1]<br />

[3]<br />

[5]<br />

VDD [4]<br />

P-TYPE<br />

[6]


Freescale Semiconductor, Inc...<br />

Basic Logic Elements<br />

Simple Gates<br />

Freescale Semiconductor, Inc.<br />

The three most basic types of logic gates found in a microcontroller are<br />

the inverter, the NAND gate, and the NOR gate. A logic designer uses<br />

various combinations of these basic gates to form more-complex logic<br />

circuits, such as those that add two binary numbers together. While this<br />

textbook is not intended to teach logic design techniques, these circuits<br />

are discussed to give you a better understanding of how a<br />

microcontroller operates on digital information.<br />

Inverter Figure 4 shows the inverter logic symbol, a truth table for an inverter,<br />

and a CMOS equivalent circuit. When a logic-level signal (0 or 1) is<br />

presented to the input [1] of an inverter, the opposite logic level appears<br />

at its output [2].<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Figure 4. CMOS Inverter<br />

Refer to the CMOS equivalent circuit at the right of Figure 4 and to<br />

Table 5 for the following discussion: When input [1] is a logic 0, N<br />

transistor [4] is off and P transistor [3] is on, connecting output [2] to VDD<br />

(logic 1). When input [1] is a logic 1, P transistor [3] is off and N transistor<br />

[4] is on, connecting output [2] to ground (logic 0).<br />

Input<br />

[1]<br />

[1] [2]<br />

Table 5. Inverter Gate Operation<br />

Transistor Output<br />

[2]<br />

[3] [4]<br />

0 On Off Connected to V DD (1)<br />

1 Off On Connected to ground (0)<br />

40 Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

Input<br />

[1]<br />

Output<br />

[2]<br />

0 1<br />

1 0<br />

[1]<br />

V DD<br />

[3]<br />

[4]<br />

[2]


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

Simple Gates<br />

NAND Gate Figure 5 shows the NAND gate logic symbol, a truth table for a CMOS<br />

NAND gate, and a CMOS equivalent circuit. When both input [1] and<br />

input [2] of the NAND gate are logic-level 1 signals, the output [3] will be<br />

a logic 0. If any of the inputs to a NAND gate are logic 0s, the output will<br />

be a logic 1.<br />

[1]<br />

[2]<br />

[3]<br />

Input Output<br />

[1] [2] [3]<br />

0 0 1<br />

0 1 1<br />

1 0 1<br />

1 1 0<br />

Figure 5. CMOS NAND Gate<br />

Refer to the CMOS equivalent circuit at the right of Figure 5 and to<br />

Table 6 for the following discussion: When both inputs [1] and [2] are<br />

logic 1s, P transistors [6] and [4] are both off and N transistors [5] and [7]<br />

are both on, so output [3] is connected to ground (logic 0). When input<br />

[1] is at logic 0, N transistor [5] is off, which disconnects output [3] from<br />

ground regardless of the condition of N transistor [7]. Also, when input<br />

[1] is logic 0, P transistor [4] is on, connecting output [3] to VDD (logic 1).<br />

Similarly, when input [2] is logic 0, N transistor [7] is off, which<br />

disconnects output [3] from ground regardless of the condition of N<br />

transistor [5]. Also, when input [2] is logic 0, P transistor [6] is on,<br />

connecting output [3] to VDD (logic 1).<br />

Input<br />

Table 6. NAND Gate-Level Operation<br />

Transistor Output<br />

[1] [2] [6] [4] [5] [7] [3]<br />

0 0 On On Off Off VDD (1)<br />

0 1 Off On Off On VDD (1)<br />

1 0 On Off On Off VDD (1)<br />

1 1 Off Off On On GND (0)<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

41<br />

[1]<br />

[2]<br />

V DD<br />

[6]<br />

V DD<br />

[4]<br />

[5]<br />

[7]<br />

[3]


Freescale Semiconductor, Inc...<br />

Basic Logic Elements<br />

Freescale Semiconductor, Inc.<br />

Although this is a simple logical function, it shows how CMOS transistors<br />

can be interconnected to perform Boolean logic on simple logic-level<br />

signals. Boolean logic is a 2-valued (0 and 1) algebraic system based on<br />

mathematical forms and relationships, and is named for the Irish<br />

mathematician who formulated it.<br />

NOR Gate Figure 6 shows the logic symbol, a truth table for a CMOS NOR gate,<br />

and a CMOS equivalent circuit. When neither input [1] nor input [2] of a<br />

NOR gate is a logic-level 1 signal, the output [3] will be a logic 1. If any<br />

input to a NOR gate is a logic 1, the output will be a logic 0.<br />

[1]<br />

[2]<br />

Input Output<br />

[1] [2] [3]<br />

0 0 1<br />

0 1 0<br />

1 0 0<br />

1 1 0<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

[3]<br />

Figure 6. CMOS NOR Gate<br />

42 Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

[1]<br />

[2]<br />

[7]<br />

V DD<br />

[6]<br />

[4]<br />

[5]<br />

[3]


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

Simple Gates<br />

Refer to the CMOS equivalent circuit at the right of Figure 6 and to<br />

Table 7 for the following discussion: When both inputs [1] and [2] are<br />

logic 0s, N transistors [5] and [7] are both off and P transistors [4] and [6]<br />

are both on, so output [3] is connected to VDD (logic 1). When input [1]<br />

is at logic 1, P transistor [4] is off, which disconnects output [3] from VDD<br />

regardless of the condition of P transistor [6]. Also, when input [1] is logic<br />

1, N transistor [5] is on, connecting output [3] to ground (logic 0).<br />

Similarly, when input [2] is logic 1, P transistor [6] is off, which<br />

disconnects output [3] from VDD regardless of the condition of P<br />

transistor [4]. Also when input [2] is logic 1, N transistor [7] is on,<br />

connecting output [3] to ground (logic 0).<br />

Table 7. NOR Gate Truth Table<br />

Input Transistor Output<br />

[1] [2] [4] [5] [6] [7] [3]<br />

0 0 On Off On Off V DD (1)<br />

0 1 On Off Off On GND (0)<br />

1 0 Off On On Off GND (0)<br />

1 1 Off On Off On GND (0)<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

43


Freescale Semiconductor, Inc...<br />

Basic Logic Elements<br />

Freescale Semiconductor, Inc.<br />

Transmission Gates, Buffers, and Flip Flops<br />

<strong>Microcontrollers</strong> include more-complex types of logic gates and<br />

functional elements than those shown in the previous section. In this<br />

section, we explore some of these more-complex structures. The first<br />

two structures <strong>—</strong> transmission gate and three-state buffer <strong>—</strong> introduce<br />

the idea of logically controlled high-impedance signals. The third <strong>—</strong> half<br />

flip flop <strong>—</strong> introduces a structure that can maintain a signal at its output<br />

even after the input signal has changed. Flip flops are vital for a<br />

microcontroller to perform counting and sequencing tasks.<br />

Transmission Gate Figure 7 shows the logic symbol, a truth table for a CMOS transmission<br />

gate, and a CMOS equivalent circuit. When control input [3] is a logic 1,<br />

the transmission gate is said to be on and whatever logic level is present<br />

on the input [1] is also seen at the output [2]. When the control input [3]<br />

is a logic 0, the transmission gate is said to be off and the output node<br />

[2] appears to be disconnected from everything (high impedance or<br />

Hi-Z).<br />

[1] [2]<br />

Control<br />

[3]<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

[3]<br />

Figure 7. CMOS Transmission Gate<br />

Refer to the CMOS equivalent circuit at the right of Figure 7 for the<br />

following discussion: When control input [3] is logic 0, the gate of N<br />

transistor [6] will be logic 0 and the gate of P transistor [5] will be logic 1<br />

(VDD). There is no voltage between ground and VDD that would cause P<br />

transistor [5] or N transistor [6] to turn on, so there is no conduction<br />

between the input [1] and the output [2]. Since output node [2] is<br />

effectively isolated from everything, it is said to be high impedance.<br />

44 Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

Input<br />

[1]<br />

Output<br />

[2]<br />

0 0 Hi-Z<br />

0 1 Hi-Z<br />

1 0 0<br />

1 1 1<br />

[1]<br />

[4]<br />

[3]<br />

[5]<br />

[6]<br />

[2]


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

Transmission Gates, Buffers, and Flip Flops<br />

When control input [3] is a logic 1, the transmission gate is said to be on<br />

and there appears to be a direct connection from the input [1] to the<br />

output [2]. If both control [3] and input [1] are at logic 1, P transistor [5]<br />

will be on and will form the connection between the input [1] and output<br />

[2]. Although the gate of N transistor [6] is a logic 1, the source [1] is also<br />

at the same voltage, so transistor [6] will be off. If control [3] is at logic 1<br />

and input [1] is at logic 0, N transistor [6] will be on and will form the<br />

connection between the input [1] and output [2]. Although the gate of P<br />

transistor [5] is a logic 0, the source [1] is also at the same voltage, so<br />

transistor [5] will be off.<br />

The transmission gate shown in Figure 7 is sometimes called an analog<br />

switch because it is capable of passing signals that fall between legal<br />

digital logic levels. For this discussion, however, we are interested only<br />

in digital logic-level signals, so we will refer to this structure as a<br />

transmission gate.<br />

Transmission gates can form data multiplexers, as shown in Figure 8.<br />

When select signal [3] is a logic 1, transmission gate [6] is on and<br />

transmission gate [7] (because of inverter [5]) is off. Thus output [4] will<br />

have the same logic level as input [1], and signals on input [2] will not<br />

affect output [4]. When select signal [3] is a logic 0, transmission gate [7]<br />

is on and transmission gate [6] is off. Thus output [4] will have the same<br />

logic level as input [2] and signals on input [1] will not affect output [4].<br />

[1]<br />

[3]<br />

[2]<br />

[5]<br />

[6]<br />

[7]<br />

Select<br />

[3]<br />

Figure 8. 2:1 Data Multiplexer<br />

Input Output<br />

[4]<br />

[1] [2]<br />

0 0 0 0<br />

0 0 1 1<br />

0 1 0 0<br />

0 1 1 1<br />

1 0 0 0<br />

1 0 1 0<br />

1 1 0 1<br />

1 1 1 1<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

45<br />

[4]


Freescale Semiconductor, Inc...<br />

Basic Logic Elements<br />

Freescale Semiconductor, Inc.<br />

Three-State Buffer Figure 9 shows the logic symbol, a CMOS equivalent circuit, and a truth<br />

table for a CMOS three-state buffer. When control input [3] is a logic 0,<br />

the buffer is said to be off and output [2] is an isolated high impedance<br />

node. When control input [3] is a logic 1, the buffer is said to be on and<br />

whatever logic level is present on the input [1] is also seen at the output<br />

[2].<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Table 8. Data Multiplexer Operation<br />

Select<br />

Input Transmission Gate Output<br />

[3]<br />

[1] [2] [6] [7] [4]<br />

0 0 0 Off On 0<br />

0 0 1 Off On 1<br />

0 1 0 Off On 0<br />

0 1 1 Off On 1<br />

1 0 0 On Off 0<br />

1 0 1 On Off 0<br />

1 1 0 On Off 1<br />

1 1 1 On Off 1<br />

Control<br />

[3]<br />

[1] [2]<br />

Input<br />

[1]<br />

[3]<br />

Output<br />

[2]<br />

0 0 Hi-Z<br />

0 1 Hi-Z<br />

1 0 0<br />

1 1 1<br />

Figure 9. Three-State Buffer<br />

Refer to the CMOS equivalent circuit at the right of Figure 9 and to<br />

Table 9 for the following discussion: When control input [3] is logic 0, the<br />

gate of N transistor [6] will be logic 0 and the gate of P transistor [5]<br />

through inverter [9], will be logic 1 (VDD), so both transistors [5] and [6]<br />

46 Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

[1]<br />

[4]<br />

[3]<br />

[9]<br />

V DD<br />

[7]<br />

[5]<br />

[6]<br />

[8]<br />

[2]


Freescale Semiconductor, Inc...<br />

Control<br />

[3]<br />

Input<br />

[1]<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

Transmission Gates, Buffers, and Flip Flops<br />

are off. Since output node [2] is effectively isolated from everything, it is<br />

said to be high impedance.<br />

When control input [3] is logic 1, the gate of N transistor [6] will be a logic<br />

1 and the gate of P transistor [5] will be logic 0. If buffer input [1] is logic<br />

0, the output of inverter [4] is logic 1, which turns on N transistor [8] and<br />

turns off P transistor [7]. With control [3] at logic 1 and input [1] at logic<br />

0, buffer output [2] is connected to ground through N transistors [6] and<br />

[8], which are both on.<br />

When control input [3] is logic 1, the gate of N transistor [6] will be a logic<br />

1 and the gate of P transistor [5] will be logic 0. If buffer input [1] is logic<br />

1, the output of inverter [4] is logic 0, which turns on P transistor [7] and<br />

turns off N transistor [8]. With control [3] and input [1] both at logic 1,<br />

buffer output [2] is connected to VDD through P transistors [7] and [5],<br />

which are both on.<br />

Table 9. Buffer Gate Operation<br />

Node Transistor Output<br />

[2]<br />

[4] [9] [5] [6] [7] [8]<br />

0 0 1 1 Off Off Off On Hi-Z<br />

0 1 0 1 Off Off On Off Hi-Z<br />

1 0 1 0 On On Off On GND (0)<br />

1 1 0 0 On On On Off V DD (1)<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

47


Freescale Semiconductor, Inc...<br />

Basic Logic Elements<br />

Half Flip Flop<br />

(HFF)<br />

Freescale Semiconductor, Inc.<br />

Figure 10 shows the logic symbol and a CMOS equivalent circuit for a<br />

half flip flop (HFF). When clock input [2] is a logic 1, transmission gate<br />

[9] is on and transmission gate [8] is off. The half flip flop is said to be<br />

transparent because input signal [1] passes directly to the Q [3] and<br />

Q-bar (Q) [4] outputs. When the clock [2] is logic 0, transmission gate [8]<br />

turns on and transmission gate [9] turns off. In this state, the half flip flop<br />

is said to be latched. Transmission gate [8], inverter [6] and inverter [7]<br />

form a stable “ring,” and the Q [3] and Q-bar [4] outputs remain at the<br />

same logic level as when the clock changed from 1 to 0.<br />

[1]<br />

[2]<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

D<br />

C<br />

HFF<br />

Q<br />

Q<br />

[3]<br />

[4]<br />

Figure 10. Half Flip Flop<br />

48 Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

[1]<br />

[2]<br />

[9]<br />

[5]<br />

[8]<br />

[6]<br />

[7]<br />

[3]<br />

[4]


Freescale Semiconductor, Inc...<br />

Review<br />

Freescale Semiconductor, Inc.<br />

Basic Logic Elements<br />

Review<br />

Although we often think about logic levels being 0 volts or 5 volts, they<br />

are actually ranges of voltages that are guaranteed by the MCU<br />

manufacturer. For a specific MCU operating with VDD equal to 5.0 volts,<br />

a logic 0 could be 0.0 to 1.5 volts and a logic 1 might be 3.5 to 5.0 volts.<br />

Always refer to the data sheets for the MCU you are using to obtain the<br />

voltage ranges of logic 0 and logic 1.<br />

CMOS MCUs are made up of thousands of N-type and P-type<br />

transistors. An N transistor is on (conducts from source to drain) when<br />

its gate is at a logic 1 and its source is at logic 0. A P transistor is on when<br />

its source is at logic 1 and its gate is at logic 0.<br />

N and P transistors can be connected in various ways to perform logical<br />

operations. Inverters, NAND gates, and NOR gates are three types of<br />

simple logic gates. The output of an inverter is always the opposite logic<br />

level of its input. The output of a NAND gate is logic 0 when all of its<br />

inputs are logic 1s. The output of a NOR gate is a logic 0 when any or all<br />

of its inputs are logic 1s.<br />

The output of a transmission gate or a three-state buffer can be logic 0,<br />

logic 1, or high impedance. An output is high impedance when it appears<br />

to be not connected to anything (an open circuit).<br />

Ahalf flip flop (HFF) has a transparent condition and a latched condition.<br />

In the transparent condition (clock input equals logic 1), the Q output is<br />

always equal to the logic level presented at the input. In the latched<br />

condition (clock input equals logic 0), the output maintains the logic level<br />

that was present when the flip flop was last in the transparent condition.<br />

Changes in the input logic level, while the flip flop is latched, do not affect<br />

the output logic level.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

49


Freescale Semiconductor, Inc...<br />

Basic Logic Elements<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

50 Basic Logic Elements<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

Computer Memory and Parallel I/O<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51<br />

Pigeon Hole Analogy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52<br />

How a Computer Sees Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53<br />

Kilobytes, Megabytes, and Gigabytes. . . . . . . . . . . . . . . . . . . . . . . . .54<br />

Kinds of Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54<br />

Random Access Memory (RAM). . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

Read-Only Memory (ROM). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

Programmable ROM (PROM). . . . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

EPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55<br />

OTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56<br />

EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56<br />

I/O as a Memory Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57<br />

Internal Status and Control Registers. . . . . . . . . . . . . . . . . . . . . . .59<br />

Memory Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60<br />

Memory Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62<br />

Before the operation of a CPU can be discussed in detail, some<br />

conceptual knowledge of computer memory is required. In many<br />

beginning programming classes, memory is presented as being similar<br />

to a matrix of pigeon holes where you can save messages and other<br />

information. The pigeon holes we refer to here are like the mailboxes in<br />

a large apartment building. This is a good analogy, but it needs a little<br />

refinement to explain the inner workings of a CPU.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong>Rev. 2.0<br />

MOTOROLA Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

51


Freescale Semiconductor, Inc...<br />

Computer Memory and Parallel I/O<br />

Pigeon Hole Analogy<br />

Freescale Semiconductor, Inc.<br />

The whole idea of any type of memory is to save information. Of course,<br />

there is no point in saving information if you don’t have a reliable way to<br />

recall that information when it’s needed. The array of mailboxes in a<br />

large apartment building could be used as a type of memory storage.<br />

You could put information into a mail box with a certain apartment<br />

number on it. When you wanted to recall that information, you could go<br />

to the mailbox with that address and retrieve the information.<br />

Next, we will carry this analogy further to explain just how a computer<br />

sees memory. We will confine our discussion to an 8-bit computer so<br />

that we can be very specific.<br />

In an 8-bit CPU, each pigeon hole (or mailbox) can be thought of as<br />

containing a set of eight on/off switches. Unlike a real pigeon hole, you<br />

cannot fit more information in by writing smaller, and there is no such<br />

thing as an empty pigeon hole (the eight switches are either on or off).<br />

The contents of a memory location can be unknown or undefined at a<br />

given time, just as the switches in the pigeon holes may be in an<br />

unknown state until you set them the first time. The eight switches would<br />

be in a row where each switch represents a single binary digit (bit). A<br />

binary 1 corresponds to the switch being on, and a binary 0 corresponds<br />

to the switch being off. Each pigeon hole (memory location) has a unique<br />

address so that information can be stored and reliably retrieved.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

52 Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

How a Computer Sees Memory<br />

Freescale Semiconductor, Inc.<br />

Computer Memory and Parallel I/O<br />

How a Computer Sees Memory<br />

In an apartment building, the addresses of the mailboxes might be<br />

100–175 for the first floor, 200–275 for the second floor, etc. These are<br />

decimal numbers that have meaning for people. As we discussed earlier,<br />

computers work in the binary number system. A computer with four<br />

address wires could uniquely identify 16 addresses because a set of four<br />

1s and 0s can be arranged in 16 different combinations. This computer<br />

would identify the addresses of the 16 memory locations (mailboxes)<br />

with the hexadecimal values $0 through $F.<br />

In the smallest MC68HC05 microcontrollers, their 10 address lines allow<br />

these computers to address 1024 unique memory locations. In<br />

comparison, the MC68HC11 general-purpose 8-bit microcontroller has<br />

16 address lines, which means it can address 65,536 unique memory<br />

locations.<br />

An 8-bit computer with 10 address lines sees memory as a continuous<br />

row of 1024, 8-bit values. The first memory location has the address<br />

00 0000 00002 and the last location has the address 11 1111 11112.<br />

These 10-bit addresses are normally expressed as two 8-bit numbers<br />

that are in turn expressed as four hexadecimal digits. In hexadecimal<br />

notation, these addresses would range from $0000 to $03FF.<br />

The computer specifies which memory location is being accessed (read<br />

from or written to) by putting a unique combination of 1s and 0s on the<br />

10 address lines. The intention to read the location or write to the<br />

location is signalled by placing a 1 (read) or a 0 (write) on a line called<br />

read/write (R/W). The information from or for the memory location is<br />

carried on eight data lines.<br />

To a computer any memory location can be written to or read from. Not<br />

all memory types are writable, but it is the job of the programmer to know<br />

this, not the computer. If a programmer erroneously instructs the<br />

computer to write to a read-only memory, it will try to do so.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

53


Freescale Semiconductor, Inc...<br />

Computer Memory and Parallel I/O<br />

Kilobytes, Megabytes, and Gigabytes<br />

Kinds of Memory<br />

Freescale Semiconductor, Inc.<br />

The smallest unit of computer memory is a single bit that can store one<br />

value of 0 or 1. These bits are grouped into sets of eight bits to make one<br />

byte. Larger computers further group bits into sets of 16 or 32 to make<br />

a unit called a word. The size of a word can be different for different<br />

computers.<br />

In the decimal world, we sometimes express very small or very large<br />

numbers by including a prefix such as milli, kilo, etc., before the unit of<br />

measure. In the binary world, we use similar prefixes to describe large<br />

amounts of memory. In the decimal system, the prefix kilo means 1000<br />

(or 10 3 ) times a value. In the binary system, the integer power of 2 that<br />

comes closest to 100010 is 2 10 = 102410. We say kilobytes but we mean<br />

Kbytes which are multiples of 102410 bytes. Although this is sloppy<br />

scientific terminology, it has become a standard through years of use.<br />

A megabyte is 2 20 or 1,048,57610 bytes. A gigabyte is 2 30 or<br />

1,073,741,82410 bytes. A personal computer with 32 address lines can<br />

theoretically address 4 gigabytes (4,294,967,29610) of memory. The<br />

small microcontrollers discussed in this textbook have only about 512<br />

bytes to 16 kilobytes of memory.<br />

Computers use several kinds of information that require different kinds<br />

of memory. The instructions that control the operation of a<br />

microcontroller are stored in a non-volatile memory so the system does<br />

not have to be reprogrammed after power has been off. Working<br />

variables and intermediate results need to be stored in a memory that<br />

can be written quickly and easily during system operation. It is not<br />

important to remember this kind of information when there is no power,<br />

so a volatile form of memory can be used. These types of memory are<br />

changed (written) and read only by the CPU in the computer.<br />

Like other memory information, input data is read by the CPU and output<br />

data is written by the CPU. I/O (input/output) and control registers are<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

54 Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Random Access<br />

Memory (RAM)<br />

Read-Only<br />

Memory (ROM)<br />

Programmable<br />

ROM (PROM)<br />

Freescale Semiconductor, Inc.<br />

Computer Memory and Parallel I/O<br />

Kinds of Memory<br />

also a form of memory to the computer, but they are different than other<br />

kinds of memory because the information can be sensed and/or<br />

changed by something other than the CPU.<br />

RAM is a volatile form of memory that can be read or written by the CPU.<br />

As its name implies, RAM locations may be accessed in any order. This<br />

is the most common type of memory in a personal computer. RAM<br />

requires a relatively large amount of area on an integrated circuit chip.<br />

Because of the relatively large chip area (and thus higher cost), usually<br />

only small amounts of RAM are included in microcontroller chips.<br />

ROM gets its information during the manufacturing process. The<br />

information must be provided by the customer before the integrated<br />

circuit, that will contain this information, is made. When the finished<br />

microcontroller is used, this information can be read by the CPU but<br />

cannot be changed. ROM is considered a non-volatile memory because<br />

the information does not change if power is turned off. ROM is the<br />

simplest, smallest, and least expensive type of non-volatile memory.<br />

PROM is similar to ROM except that it can be programmed after the<br />

integrated circuit is made. Some variations of PROM include:<br />

• Erasable PROM (EPROM)<br />

• One-time-programmable PROM (OTP)<br />

• Electrically erasable PROM (EEPROM)<br />

EPROM EPROM can be erased by exposing it to an ultraviolet light source.<br />

<strong>Microcontrollers</strong> with EPROM that can be erased have a small quartz<br />

window that allows the integrated circuit chip inside to be exposed to the<br />

ultraviolet light. The number of times an EPROM can be erased and<br />

reprogrammed is limited to a few hundred cycles, depending on the<br />

particular device.<br />

A special procedure is used to program information into an EPROM<br />

memory. Most EPROM microcontrollers also use an additional power<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

55


Freescale Semiconductor, Inc...<br />

Computer Memory and Parallel I/O<br />

Freescale Semiconductor, Inc.<br />

supply, such as +12 Vdc, during the EPROM programming operation.<br />

The CPU cannot simply write information to an EPROM location the way<br />

it would write to a RAM location.<br />

Some microcontrollers have built-in EPROM programming circuits so<br />

that the CPU in the microcontroller can program EPROM locations.<br />

When the EPROM is being programmed, it is not connected to the<br />

address and data buses the way a normal memory would be. In the<br />

pigeon hole analogy, this would be like removing the entire rack of<br />

mailboxes and taking it to a warehouse where the boxes would be filled<br />

with information. While the mailboxes are away being programmed, the<br />

people at the apartment building cannot access the mailboxes.<br />

Some EPROM microcontrollers (not the MC68HC705J1A) have a<br />

special mode of operation that makes them appear to be an industry<br />

standard EPROM memory. These devices can be programmed with a<br />

general-purpose commercial EPROM programmer.<br />

OTP When an EPROM microcontroller is packaged in an opaque plastic<br />

package, it is called a one-time programmable or OTP microcontroller.<br />

Since ultraviolet light cannot pass through the package, the memory<br />

cannot be erased. The integrated circuit chip inside an OTP MCU is<br />

identical to that in the quartz window package. The plastic package is<br />

much less expensive than a ceramic package with a quartz window.<br />

OTP MCUs are ideal for quick turn around, first production runs, and low<br />

volume applications.<br />

EEPROM EEPROM can be erased electrically by commands in a microcontroller.<br />

To program a new value into a location, you must first erase the location<br />

and then perform a series of programming steps. This is somewhat more<br />

complicated than changing a RAM location that can simply be written to<br />

a new value by the CPU. The advantage of EEPROM is that it is a<br />

non-volatile memory. EEPROM does not lose its contents when power<br />

is turned off. Unlike RAM memory, the number of times you can erase<br />

and reprogram an EEPROM location is limited (typically to 10,000<br />

cycles). The number of times you can read an EEPROM location is not<br />

limited.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

56 Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

I/O as a Memory<br />

Type<br />

Freescale Semiconductor, Inc.<br />

Computer Memory and Parallel I/O<br />

Kinds of Memory<br />

I/O status and control information is a type of memory location that<br />

allows the computer system to get information to or from the outside<br />

world. This type of memory location is unusual because the information<br />

can be sensed and/or changed by something other than the CPU.<br />

The simplest kinds of I/O memory locations are basic input ports and<br />

output ports. In an 8-bit MCU, a simple input port consists of eight pins<br />

that can be read by the CPU. A simple output port consists of eight pins<br />

that the CPU can control (write to). In practice, a simple output port<br />

location is usually implemented with eight latches and feedback paths<br />

that allow the CPU to read back what was previously written to the<br />

address of the output port.<br />

Figure 11 shows the equivalent circuits for one bit of RAM, one bit of an<br />

input port, and one bit of a typical output port having read-back<br />

capability. In a real MCU, these circuits would be repeated eight times to<br />

make a single 8-bit RAM location, input port, or output port. The half flip<br />

flops (HFF) in Figure 11 are very simple transparent flip flops. When the<br />

clock signal is high, data passes freely from the D input to the Q and<br />

Q-bar outputs. When the clock input is low, data is latched at the Q and<br />

Q-bar outputs.<br />

When the CPU stores a value to the address that corresponds to the<br />

RAM bit in Figure 11 (a), the WRITE signal is activated to latch the data<br />

from the data bus line into the flip flop [1]. This latch is static and<br />

remembers the value written until a new value is written to this location<br />

or power is removed. When the CPU reads the address of this RAM bit,<br />

the READ signal is activated, which enables the multiplexer at [2]. This<br />

multiplexer couples the data from the output of the flip flop onto the data<br />

bus line. In a real MCU, RAM bits are much simpler than shown here,<br />

but they are functionally equivalent to this circuit.<br />

When the CPU reads the address of the input port shown in Figure 11<br />

(b), the READ signal is activated, which enables the multiplexer at [3].<br />

The multiplexer couples the buffered data from the pin onto the data bus<br />

line. A write to this address would have no meaning.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

57


Freescale Semiconductor, Inc...<br />

Computer Memory and Parallel I/O<br />

Freescale Semiconductor, Inc.<br />

READ<br />

DATA BIT n<br />

(n = 0, 1. . .or 7)<br />

READ<br />

DATA BIT n<br />

(n = 0, 1. . .or 7)<br />

WRITE<br />

READ<br />

DATA BIT n<br />

(n = 0, 1. . .or 7)<br />

WRITE<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

(c) Output Port with Read-Back<br />

Figure 11. Memory and I/O Circuitry<br />

When the CPU stores a value to the address that corresponds to the<br />

output port in Figure 11 (c), the WRITE signal is activated to latch the<br />

data from the data bus line into the half flip flop [4]. The output of this<br />

latch, which is buffered by the buffer driver at [5], appears as a digital<br />

level on the output pin. When the CPU reads the address of this output<br />

port, the READ signal is activated, which enables the multiplexer at [6].<br />

This multiplexer couples the data from the output of the half flip flop onto<br />

the data bus line.<br />

58 Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

D<br />

C<br />

(b) Input Port Bit<br />

HFF<br />

(a) RAM Bit<br />

[3]<br />

[6]<br />

Q<br />

Q<br />

[4]<br />

[1]<br />

D<br />

C<br />

HFF<br />

[2]<br />

Q<br />

Q<br />

BUFFER<br />

[5]<br />

BUFFER – DRIVER<br />

PIN<br />

PIN<br />

DIGITAL<br />

INPUT<br />

DIGITAL<br />

OUTPUT


Freescale Semiconductor, Inc...<br />

Internal Status and<br />

Control Registers<br />

Freescale Semiconductor, Inc.<br />

Computer Memory and Parallel I/O<br />

Kinds of Memory<br />

Internal status and control registers are just specialized versions of I/O<br />

memory locations. Instead of sensing and controlling external pins,<br />

status and control registers sense and control internal logic level signals.<br />

Look at Figure 11 and compare the RAM bit to the output port. The only<br />

difference is that the output bit has a buffer to connect the state of the<br />

half flip flop to an external pin. In the case of an internal control bit, the<br />

buffer output is connected to some internal control signal rather than an<br />

external pin. An internal status bit is like an input port bit except that the<br />

signal that is sensed during a read is an internal signal rather than an<br />

external pin.<br />

<strong>M68HC05</strong> microcontrollers include general-purpose parallel I/O pins.<br />

The direction of each pin is programmable by a software-accessible<br />

control bit. Figure 12 shows the logic for a bidirectional I/O pin, including<br />

an output port latch and a data direction control bit.<br />

A port pin is configured as an output if its corresponding DDR (data<br />

direction register) bit is set to a logic 1. A pin is configured as an input if<br />

its corresponding DDR bit is cleared to a logic 0. At power-on or reset,<br />

all DDR bits are cleared, which configure all port pins as inputs. The<br />

DDRs are capable of being written to or being read by the processor.<br />

READ<br />

DDR BIT<br />

WRITE<br />

DDR BIT<br />

DATA BIT n<br />

(n = 0, 1. . .or 7)<br />

WRITE<br />

PORT<br />

READ<br />

PORT<br />

Figure 12. I/O Port with Data Direction Control<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

59<br />

D<br />

C<br />

D<br />

C<br />

HFF<br />

HFF<br />

Q<br />

Q<br />

Q<br />

Q<br />

BUFFER – DRIVER<br />

BUFFER<br />

PIN<br />

DIGITAL<br />

I/O


Freescale Semiconductor, Inc...<br />

Computer Memory and Parallel I/O<br />

Memory Maps<br />

Freescale Semiconductor, Inc.<br />

Since there are a thousand or more memory locations in an MCU<br />

system, it is important to have a convenient way to keep track of where<br />

things are. A memory map is a pictorial representation of the total MCU<br />

memory space. Figure 14 is a typical memory map showing the memory<br />

resources in the MC68HC705J1A.<br />

The 4-digit hexadecimal values along the left edge of Figure 14 are<br />

addresses beginning with $0000 at the top and increasing to $07FF at<br />

the bottom. $0000 corresponds to the first memory location (selected<br />

when the CPU drives all address lines of the internal address bus to logic<br />

0). $07FF corresponds to the last memory location selected (when the<br />

CPU drives all 11 address lines of the internal address bus to logic 1).<br />

The labels within the vertical rectangle identify what kind of memory<br />

(RAM, EPROM, I/O registers, etc.) resides in a particular area of<br />

memory.<br />

Some areas, such as I/O registers, need to be shown in more detail<br />

because it is important to know the names of each individual location.<br />

The whole vertical rectangle can be interpreted as a row of 2048 pigeon<br />

holes (memory locations). Each of these 2048 memory locations<br />

contains eight bits of data as shown in Figure 13.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

PORT A DATA DIRECTION REGISTER<br />

BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0<br />

DDRA7 DDRA6 DDRA5 DDRA4 DDRA3 DDRA2 DDRA1 DDRA0<br />

Figure 13. Expanded Detail of One Memory Location<br />

The first 256 memory locations ($0000–$00FF) can be accessed by the<br />

computer in a special way called direct addressing mode. Addressing<br />

modes are discussed in greater detail in <strong>M68HC05</strong> Instruction Set. In<br />

direct addressing mode, the CPU assumes that the upper two<br />

hexadecimal digits of address are 0; thus, only the two low-order digits<br />

of the address need to be explicitly given in the instruction. On-chip I/O<br />

registers and 64 bytes of RAM are located in the $0000–$00FF area of<br />

memory.<br />

60 Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

$04


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

$0000<br />

$001F<br />

$0020<br />

$00BF<br />

$00C0<br />

$00FF<br />

$0100<br />

$02FF<br />

$0300<br />

$07CF<br />

$07D0<br />

$07ED<br />

$07EE<br />

$07EF<br />

$07F0<br />

$07FF<br />

I/O<br />

32 Bytes<br />

Unused<br />

160 Bytes<br />

User & Stack<br />

RAM<br />

64 Bytes<br />

Unused<br />

512 Bytes<br />

User EPROM<br />

1232 Bytes<br />

Unimplemented<br />

30 Bytes<br />

Test ROM <strong>—</strong> 2 Bytes<br />

User Vectors<br />

(EPROM)<br />

16 Bytes<br />

See Figure 13<br />

Figure 14. Typical Memory Map<br />

Computer Memory and Parallel I/O<br />

Memory Maps<br />

Port A Data Register $00<br />

Port B Data Register $01<br />

Unused<br />

$02<br />

Unused<br />

$03<br />

Port A Data Direction Register $04<br />

Port B Data Direction Register $05<br />

Unused<br />

$06<br />

Unused<br />

$07<br />

Timer Status & Control $08<br />

Timer Counter Register $09<br />

IRQ Status & Control Register $0A<br />

Unused<br />

$0B<br />

Unused<br />

$0C<br />

Unused<br />

$0D<br />

Unused<br />

$0E<br />

Unused<br />

$0F<br />

Port A Pulldown Register $10<br />

Port B Pulldown Register $11<br />

Unused<br />

$12<br />

Unused<br />

$13<br />

Unused<br />

$14<br />

Unused<br />

$15<br />

Unused<br />

$16<br />

Unused<br />

$17<br />

EPROM Programming Register $18<br />

Unused<br />

$19<br />

Unused<br />

$1A<br />

Unused<br />

$1B<br />

Unused<br />

$1C<br />

Unused<br />

$1D<br />

Unused<br />

$1E<br />

Reserved $1F<br />

COP Register $07F0<br />

Mask Option Register $07F1<br />

Reserved<br />

Reserved $07F7<br />

Timer Vector (High Byte) $07F8<br />

Timer Vector (Low Byte) $07F9<br />

IRQ Vector (High Byte) $07FA<br />

IRQ Vector (Low Byte) $07FB<br />

SWI Vector (High Byte) $07FC<br />

SWI Vector (Low Byte) $07FD<br />

RESET Vector (High Byte) $07FE<br />

RESET Vector (Low Byte) $07FF<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong>Rev. 2.0<br />

MOTOROLA Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

61<br />

$07F2


Freescale Semiconductor, Inc...<br />

Computer Memory and Parallel I/O<br />

Memory Peripherals<br />

Review<br />

Freescale Semiconductor, Inc.<br />

In the memory map (Figure 14), the expansion of the I/O area of<br />

memory identifies each register location with the two low-order digits of<br />

its address rather than the full 4-digit address. For example, the 2-digit<br />

hexadecimal value $00 appears to the right of the port A data register,<br />

which is actually located at address $0000 in the memory map.<br />

Memories can be a form of peripheral. The uses for different types of<br />

memory were discussed earlier, but the logic required to support these<br />

memories was not considered. ROM and RAM memories are<br />

straightforward and require no support logic <strong>—</strong> other than address-select<br />

logic <strong>—</strong> to distinguish one location from another. This select logic is<br />

provided on the same chip as the memory itself.<br />

EPROM (erasable PROM) and EEPROM (electrically erasable PROM)<br />

memories require support logic for programming (and erasure in the<br />

case of EEPROM). For example, the peripheral support logic in the<br />

MC68HC705J1A is like having a PROM programmer built into the MCU.<br />

A control register includes control bits to select between programming<br />

and reading modes and to enable the high-voltage programming power<br />

supply.<br />

We can think of computer memory as an array of mailboxes, but a<br />

computer views memory as a series of 8-bit values.<br />

If a computer has n address lines, it can uniquely address 2 n memory<br />

locations. A computer with 11 address lines can address 2 11 , or 204810<br />

locations.<br />

NOTE: One kilobyte (written 1 Kbyte) is equal to 102410 bytes.<br />

Kinds of Memory<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

62 Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Memory and Parallel I/O<br />

Review<br />

• RAM <strong>—</strong> Random access memory can be read or written by a<br />

CPU. Contents are remembered as long as power is applied.<br />

• ROM <strong>—</strong> Read-only memory can be read but not changed. The<br />

contents must be determined before the integrated circuit is<br />

manufactured. Power is not required for ROM to remember its<br />

contents.<br />

• EPROM <strong>—</strong> Erasable programmable ROM can be changed by<br />

erasing it with an ultraviolet light and then programming it with a<br />

new value. The erasure and programming operations can be<br />

performed a limited number of times after the integrated circuit is<br />

manufactured. Power is not required for EPROM to remember its<br />

contents.<br />

• OTP <strong>—</strong> The chip in a one-time-programmable EPROM is identical<br />

to that in an EPROM, but it is packaged in an opaque package.<br />

Since ultraviolet light cannot get through the package, this<br />

memory cannot be erased after it is programmed.<br />

• EEPROM <strong>—</strong> Electrically erasable PROM can be changed using<br />

electrical signals and remembers its contents even when no<br />

power is applied. Typically, an EEPROM location can be erased<br />

and reprogrammed up to 10,000 times before it wears out.<br />

• I/O <strong>—</strong> I/O, control, and status registers are a special kind of<br />

memory because the information can be sensed and/or changed<br />

by something other than the CPU.<br />

• Non-Volatile Memory <strong>—</strong> Non-volatile memory remembers its<br />

contents even when there is no power.<br />

• Volatile Memory <strong>—</strong> Volatile memory forgets its contents when<br />

power is turned off.<br />

NOTE: Memory Map <strong>—</strong> A memory map is a pictorial view of all of the memory<br />

locations in a computer system.<br />

The first 256 locations in a microcontroller system can be accessed in a<br />

special way called direct addressing mode. In direct addressing mode,<br />

the CPU assumes the high order byte of the address is $00 so it does<br />

not have to be explicitly given in a program (saving the space it would<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

63


Freescale Semiconductor, Inc...<br />

Computer Memory and Parallel I/O<br />

Freescale Semiconductor, Inc.<br />

have taken and eliminating the clock cycle it would have required to<br />

fetch it).<br />

Specialty memories such as EPROM and EEPROM can be considered<br />

peripherals in a computer system. Support circuitry and programming<br />

controls are required to modify the contents of these memories. This<br />

differs from simple memories such as RAM that can be read or written in<br />

a single CPU clock cycle.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

64 Computer Memory and Parallel I/O<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66<br />

Computer Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66<br />

CPU Registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67<br />

Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69<br />

CPU View of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70<br />

CPU Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73<br />

Detailed Operation of CPU Instructions . . . . . . . . . . . . . . . . . . . . .73<br />

Store Accumulator (Direct Addressing Mode). . . . . . . . . . . . . . .74<br />

Load Accumulator (Immediate Addressing Mode) . . . . . . . . . . .75<br />

Conditional Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76<br />

Subroutine Calls and Returns. . . . . . . . . . . . . . . . . . . . . . . . . . .76<br />

Playing Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80<br />

Resets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86<br />

RESET Pin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86<br />

Power-On Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86<br />

Watchdog Timer Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87<br />

Illegal Address Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87<br />

Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88<br />

External Interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91<br />

On-Chip Peripheral Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92<br />

Software Interrupt (SWI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92<br />

Interrupt Latency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92<br />

Nested Interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

65


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Introduction<br />

Computer Architecture<br />

Freescale Semiconductor, Inc.<br />

This chapter takes us into the very heart of a computer to see what<br />

makes it work. This will be a more detailed look than you normally need<br />

to use an MCU, but it will help you understand why some things are done<br />

in a certain way.<br />

Everything the CPU does is broken down into sequences of simple<br />

steps. For instance, a clock oscillator generates a CPU clock that is used<br />

to step the CPU through these sequences. The CPU clock is very fast in<br />

human terms, so things seem to be happening almost instantaneously.<br />

By going through these sequences step by step, you will gain a working<br />

understanding of how a computer executes programs. You will also gain<br />

valuable knowledge of a computer’s capabilities and limitations.<br />

Motorola <strong>M68HC05</strong> and M68HC11 8-bit MCUs have a specific<br />

organization that is called a Von Neumann architecture after an<br />

American mathematician of the same name. In this architecture, a CPU<br />

and a memory array are interconnected by an address bus and a data<br />

bus. The address bus is used to identify which memory location is being<br />

accessed, and the data bus is used to convey information either from<br />

the CPU to the memory location (pigeon hole) or from the memory<br />

location to the CPU.<br />

In the Motorola implementation of this architecture, there are a few<br />

special pigeon holes (called CPU registers) inside the CPU, which act as<br />

a small scratch pad and control panel for the CPU. These CPU registers<br />

are similar to memory, in that information can be written into them and<br />

remembered. However, it is important to remember that these registers<br />

are directly wired into the CPU and are not part of the addressable<br />

memory available to the CPU.<br />

All information (other than the CPU registers) accessible to the CPU is<br />

envisioned (by the CPU) to be in a single row of a thousand or more<br />

pigeon holes. This organization is sometimes called a memory-mapped<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

66 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Computer Architecture<br />

I/O system because the CPU treats all memory locations alike whether<br />

they contain program instructions, variable data, or input-output (I/O)<br />

controls. There are other computer architectures, but this textbook is not<br />

intended to explore those variations.<br />

Fortunately, the Motorola <strong>M68HC05</strong> architecture we are discussing here<br />

is one of the easiest to understand and use. This architecture<br />

encompasses the most important ideas of digital binary computers; thus,<br />

the information presented in this textbook will be applicable even if you<br />

go on to study other architectures.<br />

The number of wires in the address bus determines the total possible<br />

number of pigeon holes; the number of wires in the data bus determines<br />

the amount of information that can be stored in each pigeon hole.<br />

In the MC68HC705J1A, for example, the address bus has 11 lines,<br />

making a maximum of 2048 separate pigeon holes (in MCU jargon you<br />

would say this CPU can access 2-K locations). Since the data bus in the<br />

MC68HC705J1A is eight bits, each pigeon hole can hold one byte of<br />

information. One byte is eight binary digits, or two hexadecimal digits, or<br />

one ASCII character, or a decimal value from 0 to 255.<br />

CPU Registers Different CPUs have different sets of CPU registers. The differences are<br />

primarily the number and size of the registers. Figure 15 shows the CPU<br />

registers found in an <strong>M68HC05</strong>. While this is a relatively simple set of<br />

CPU registers, it is representative of all types of CPU registers and can<br />

be used to explain all of the fundamental concepts. This chapter<br />

provides a brief description of the <strong>M68HC05</strong> registers as an introduction<br />

to CPU architecture in general. <strong>M68HC05</strong> Instruction Set addresses<br />

the instruction set of the <strong>M68HC05</strong> and includes more detailed<br />

information about <strong>M68HC05</strong> registers.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

67


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

15<br />

0 0 0<br />

0 0<br />

10<br />

7 ACCUMULATOR<br />

0<br />

INDEX REGISTER<br />

PROGRAM COUNTER<br />

STACK POINTER<br />

7 4 3 2 1 0<br />

CONDITION CODE REGISTER 1 1 1 H I N Z C<br />

Figure 15. <strong>M68HC05</strong> CPU Registers<br />

CARRY<br />

ZERO<br />

NEGATIVE<br />

I INTERRUPT MASK<br />

HALF-CARRY (FROM BIT 3)<br />

The A register, an 8-bit scratch-pad register, is also called an<br />

accumulator because it is often used to hold one of the operands or the<br />

result of an arithmetic operation.<br />

The X register is an 8-bit index register, which can also serve as a simple<br />

scratch pad. The main purpose of an index register is to point at an area<br />

in memory where the CPU will load (read) or store (write) information.<br />

Sometimes an index register is called a pointer register. We will learn<br />

more about index registers when we discuss indexed addressing<br />

modes.<br />

The program counter (PC) register is used by the CPU to keep track of<br />

the address of the next instruction to be executed. When the CPU is<br />

reset (starts up), the PC is loaded from a specific pair of memory<br />

locations called the reset vector. The reset vector locations contain the<br />

address of the first instruction that will be executed by the CPU. As<br />

instructions are executed, logic in the CPU increments the PC such that<br />

it always points to the next piece of information that the CPU will need.<br />

The number of bits in the PC exactly matches the number of wires in the<br />

address bus. This determines the total potentially available memory<br />

space that can be accessed by a CPU. In the case of an<br />

68 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

9<br />

0 0<br />

7<br />

7<br />

1 1<br />

5<br />

0<br />

0<br />

0<br />

A<br />

X<br />

SP<br />

PC<br />

CCR


Freescale Semiconductor, Inc...<br />

Timing<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Timing<br />

MC68HC705J1A, the PC is 11 bits long; therefore, its CPU can access<br />

up to 2 Kbytes (2048 bytes) of memory. Values for this register are<br />

expressed as four hexadecimal digits where the upper-order five bits of<br />

the corresponding 16-bit binary address are always 0.<br />

The condition code register (CCR) is an 8-bit register, holding status<br />

indicators that reflect the result of some prior CPU operation. The three<br />

high-order bits of this register are not used and always equal logic 1.<br />

Branch instructions use the status bits to make simple either/or<br />

decisions.<br />

The stack pointer (SP) is used as a pointer to the next available location<br />

in a last-in-first-out (LIFO) stack. The stack can be thought of as a pile<br />

of cards, each holding a single byte of information. At any given time, the<br />

CPU can put a card on top of the stack or take a card off the stack. Cards<br />

within the stack cannot be picked up unless all the cards piled on top are<br />

removed first. The CPU accomplishes this stack effect by way of the SP.<br />

The SP points to a memory location (pigeon hole), which is thought of as<br />

the next available card. When the CPU pushes a piece of data onto the<br />

stack, the data value is written into the pigeon hole pointed to by the SP,<br />

and the SP is then decremented so it points at the next previous memory<br />

location (pigeon hole). When the CPU pulls a piece of data off the stack,<br />

the SP is incremented so it points at the most recently used pigeon hole,<br />

and the data value is read from that pigeon hole. When the CPU is first<br />

started up or after a reset stack pointer (RSP) instruction, the SP points<br />

to a specific memory location in RAM (a certain pigeon hole).<br />

A high-frequency clock source (typically derived from a crystal<br />

connected to the MCU) is used to control the sequencing of CPU<br />

instructions. Typical MCUs divide the basic crystal frequency by two or<br />

more to arrive at a bus-rate clock. Each memory read or write takes one<br />

bus-rate clock cycle. In the case of the MC68HC705J1A MCU, a 4-MHz<br />

(maximum) crystal oscillator clock is divided by two to arrive at a 2-MHz<br />

(maximum) internal processor clock. Each substep of an instruction<br />

takes one cycle of this internal bus-rate clock (500 ns). Most instructions<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

69


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

CPU View of a Program<br />

Freescale Semiconductor, Inc.<br />

take two to five of these substeps; thus, the CPU is capable of executing<br />

more than 500,000 instructions every second.<br />

Listing 1. Example Program is a listing of a small example program<br />

that we will use in our discussion of a CPU. The chapter on programming<br />

provides detailed information on how to write programs. A program<br />

listing provides much more information than the CPU needs because<br />

humans also need to read and understand programs. The first column in<br />

the listing shows four digit hexadecimal addresses. The next few<br />

columns show 8-bit values (the contents of individual memory locations).<br />

The rest of the information in the listing is for the benefit of humans who<br />

need to read the listing. The meaning of all this information will be<br />

discussed in greater detail in the chapter entitled Programming.<br />

Figure 16 is a memory map of the MC68HC705J1A, showing how the<br />

example program fits in the memory of the MCU. This figure is the same<br />

as Figure 14 except that a different portion of the memory space has<br />

been expanded to show the contents of all locations in the example<br />

program.<br />

Figure 16 shows that the CPU sees the example program as a linear<br />

sequence of binary codes, including instructions and operands in<br />

successive memory locations. An operand is any value other than the<br />

opcode that the CPU needs to complete the instruction. The CPU begins<br />

this program with its program counter (PC) pointing at the first byte in the<br />

program. Each instruction opcode tells the CPU how many (if any) and<br />

what type of operands go with that instruction. In this way, the CPU can<br />

remain aligned to instruction boundaries even though the mixture of<br />

opcodes and operands looks confusing to us.<br />

Most application programs would be located in ROM, EPROM, or<br />

OTPROM, although there is no special requirement that instructions<br />

must be in a ROM-type memory to execute. As far as the CPU is<br />

concerned, any program is just a series of binary bit patterns that are<br />

sequentially processed.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

70 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Listing 1. Example Program<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

CPU View of a Program<br />

*******************************************************<br />

* Simple 68HC05 Program Example *<br />

* Read state of switch at port A bit-0; 1=closed *<br />

* When sw. closes, light LED for about 1 sec; LED on *<br />

* when port A bit-7 = 0. Wait for sw release, *<br />

* then repeat. Debounce sw 50mS on & off *<br />

* NOTE: Timing based on instruction execution times *<br />

* If using a simulator or crystal less than 4MHz, *<br />

* this routine will run slower than intended *<br />

*******************************************************<br />

$BASE 10T ;Tell assembler to use decimal<br />

;unless $ or % before value<br />

0000 PORTA EQU $00 ;Direct address of port A<br />

0004 DDRA EQU $04 ;Data direction control, port A<br />

00E0 TEMP1 EQU $C0 ;One byte temp storage location<br />

0300 ORG $0300 ;Program will start at $0300<br />

0300 A6 80 INIT LDA #$80 ;Begin initialization<br />

0302 B7 00 STA PORTA ;So LED will be off<br />

0304 B7 04 STA DDRA ;Set port A bit-7 as output<br />

* Rest of port A is configured as inputs<br />

0306 B6 00 TOP LDA PORTA ;Read sw at LSB of Port A<br />

0308 A4 01 AND #$01 ;To test bit-0<br />

030A 27 FA BEQ TOP ;Loop till Bit-0 = 1<br />

030C CD 03 23 JSR DLY50 ;Delay about 50 mS to debounce<br />

030F 1F 00 BCLR 7,PORTA ;Turn on LED (bit-7 to zero)<br />

0311 A6 14 LDA #20 ;Decimal 20 assembles to $14<br />

0313 CD 03 23 DLYLP JSR DLY50 ;Delay 50 ms<br />

0316 4A DECA ;Loop counter for 20 loops<br />

0317 26 FA BNE DLYLP ;20 times (20-19,19-18,...1-0)<br />

0319 1E 00 BSET 7,PORTA ;Turn LED back off<br />

031B 00 00 FD OFFLP BRSET 0,PORTA,OFFLP ;Loop here till sw off<br />

031E CD 03 23 JSR DLY50 ;Debounce release<br />

0321 20 E3 BRA TOP ;Look for next sw closure<br />

***<br />

* DLY50 <strong>—</strong> Subroutine to delay ~50ms<br />

* Save original accumulator value<br />

* but X will always be zero on return<br />

***<br />

0323 B7 C0 DLY50 STA TEMP1 ;Save accumulator in RAM<br />

0325 A6 41 LDA #65 ;Do outer loop 65 times<br />

0327 5F OUTLP CLRX ;X used as inner loop count<br />

0328 5A INNRLP DECX ;0-FF, FF-FE,...1-0 256 loops<br />

0329 26 FD BNE INNRLP ;6cyc*256*500ns/cyc = 0.768ms<br />

032B 4A DECA ;65-64, 64-63,...1-0<br />

032C 26 F9 BNE OUTLP ;1545cyc*65*500ns/cyc=50.212ms<br />

032E B6 C0 LDA TEMP1 ;Recover saved Accumulator val<br />

0330 81 RTS ;Return<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

71


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Freescale Semiconductor, Inc.<br />

$0000<br />

$001F<br />

$0020<br />

$00BF<br />

$00C0<br />

$00FF<br />

$0100<br />

$02FF<br />

$0300<br />

$0330<br />

$0331<br />

$07CF<br />

$07D0<br />

$07ED<br />

$07EE<br />

$07EF<br />

$07F0<br />

$07FF<br />

I/O<br />

32 Bytes<br />

Unused<br />

160 Bytes<br />

Stack RAM<br />

64 Bytes<br />

Unused<br />

512 Bytes<br />

Example<br />

Program<br />

User EPROM<br />

1232 Bytes<br />

Unimplemented<br />

30 Bytes<br />

Test ROM<br />

User Vectors<br />

(EPROM)<br />

16 Bytes<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

$0300<br />

$0301<br />

$0302<br />

$0303<br />

$0304<br />

$0305<br />

$0306<br />

$0307<br />

$0308<br />

$0309<br />

$030A<br />

$030B<br />

$030C<br />

$030D<br />

$030E<br />

$030F<br />

$0310<br />

$0311<br />

$0312<br />

$0313<br />

$0314<br />

$0315<br />

$0316<br />

$0317<br />

$0318<br />

$0319<br />

$031A<br />

$031B<br />

$031C<br />

$031D<br />

$031E<br />

$031F<br />

$0320<br />

$0321<br />

$0322<br />

$0323<br />

$0324<br />

$0325<br />

$0326<br />

$0327<br />

$0328<br />

$0329<br />

$032A<br />

$032B<br />

$032C<br />

$032D<br />

$032E<br />

$032F<br />

$0330<br />

Figure 16. Memory Map of Example Program<br />

72 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

$A6<br />

$80<br />

$B7<br />

$00<br />

$B7<br />

$04<br />

$B6<br />

$00<br />

$A4<br />

$01<br />

$27<br />

$FA<br />

$CD<br />

$03<br />

$23<br />

$1F<br />

$00<br />

$A6<br />

$14<br />

$CD<br />

$03<br />

$23<br />

$4A<br />

$26<br />

$FA<br />

$1E<br />

$00<br />

$00<br />

$00<br />

$FD<br />

$CD<br />

$03<br />

$23<br />

$20<br />

$E3<br />

$B7<br />

$C0<br />

$A6<br />

$41<br />

$5F<br />

$5A<br />

$26<br />

$FD<br />

$4A<br />

$26<br />

$F9<br />

$B6<br />

$C0<br />

$81<br />

$B7<br />

$C0<br />

$0323<br />

$0324


Freescale Semiconductor, Inc...<br />

CPU Operation<br />

Detailed<br />

Operation of CPU<br />

Instructions<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

CPU Operation<br />

Carefully study the program listing in Listing 1. Example Program and<br />

the memory map of Figure 16. Find the first instruction of the DLY50<br />

subroutine in the example program and then find the same two bytes in<br />

Figure 16.<br />

You should have found this line from near the bottom of<br />

Listing 1. Example Program.<br />

0323 B7 C0 DLY50 STA TEMP1 ;Save accumulator in RAM<br />

The highlighted section of memory at the right side of Figure 16 is the<br />

area you should have identified.<br />

This section first discusses the detailed operation of CPU instructions<br />

and then explains how the CPU executes an example program. The<br />

detailed descriptions of typical CPU instructions are intended to make<br />

you think like a CPU. We will then go through an example program using<br />

a teaching technique called “playing computer” in which you pretend you<br />

are the CPU interpreting and executing the instructions in a program.<br />

Before seeing how the CPU executes programs, it would help to know<br />

(in detail) how the CPU breaks down instructions into fundamental<br />

operations and performs these tiny steps to accomplish a desired<br />

instruction. As we will see, many small steps execute quickly and<br />

accurately within each instruction, but none of the small steps is too<br />

complicated.<br />

The logic circuitry inside the CPU would seem straightforward to a<br />

design engineer accustomed to working with TTL (transistor-transistor<br />

logic) logic or even relay logic. What sets the MCU and its CPU apart<br />

from these other forms of digital logic is the packing density. Very large<br />

scale integration (VLSI) techniques have made it possible to fit the<br />

equivalent of thousands of TTL integrated circuits on a single silicon die.<br />

By arranging these logic gates to form a CPU, you can get a<br />

general-purpose instruction executor capable of acting as a universal<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

73


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Store<br />

Accumulator<br />

(Direct Addressing<br />

Mode)<br />

Freescale Semiconductor, Inc.<br />

black box. By placing different combinations of instructions in the<br />

device, it can perform virtually any definable function.<br />

A typical instruction takes two to five cycles of the internal processor<br />

clock. Although it is not normally important to know exactly what<br />

happens during each of these execution cycles, it will help to go through<br />

a few instructions in detail to understand how the CPU works internally.<br />

Look up the STA instruction in Instruction Set Details. In the table at<br />

the bottom of the page, we see that $B7 is the direct (DIR) addressing<br />

mode version of the store accumulator instruction. We also see that the<br />

instruction requires two bytes, one to specify the opcode ($B7) and the<br />

second to specify the direct address where the accumulator will be<br />

stored. (The two bytes are shown as B7 dd in the machine code column<br />

of the table.)<br />

We will discuss the addressing modes in more detail in another chapter,<br />

but the following brief description will help in understanding how the CPU<br />

executes this instruction. In direct addressing modes, the CPU assumes<br />

the address is in the range of $0000 through $00FF; thus, there is no<br />

need to include the upper byte of the address of the operand in the<br />

instruction (since it is always $00).<br />

The table at the bottom of the STA page shows that the direct<br />

addressing version of the STA instruction takes four CPU cycles to<br />

execute. During the first cycle, the CPU puts the value from the program<br />

counter on the internal address bus and reads the opcode $B7, which<br />

identifies the instruction as the direct addressing version of the STA<br />

instruction and advances the PC to the next memory location.<br />

During the second cycle, the CPU places the value from the PC on the<br />

internal address bus and reads the low-order byte of the direct address<br />

($00 for example). The CPU uses the third cycle of this STA instruction<br />

to internally construct the full address where the accumulator is to be<br />

stored and advances the PC so it points to the next address in memory<br />

(the address of the opcode of the next instruction).<br />

In this example, the CPU appends the assumed value $00 (because of<br />

direct addressing mode) to the $00 that was read during the second<br />

cycle of the instruction to arrive at the complete address $0000. During<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

74 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Load<br />

Accumulator<br />

(Immediate<br />

Addressing Mode)<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

CPU Operation<br />

the fourth cycle of this instruction, the CPU places this constructed<br />

address ($0000) on the internal address bus, places the accumulator<br />

value on the internal data bus, and asserts the write signal. That is, the<br />

CPU writes the contents of the accumulator to $0000 during the fourth<br />

cycle of the STA instruction.<br />

While the accumulator was being stored, the N and Z bits in the condition<br />

code register were set or cleared according to the data that was stored.<br />

The Boolean logic formulae for these bits appear near the middle of the<br />

instruction set page. The Z bit will be set if the value stored was $00;<br />

otherwise, the Z bit will be cleared. The N bit will be set if the most<br />

significant bit of the value stored was a logic 1; otherwise, N will be<br />

cleared.<br />

Next, look up the LDA instruction in the instruction set appendix. The<br />

immediate addressing mode (IMM) version of this instruction appears as<br />

A6 ii in the machine code column of the table at the bottom of the<br />

page. This version of the instruction takes two internal processor clock<br />

cycles to execute.<br />

The $A6 opcode tells the CPU to get the byte of data that immediately<br />

follows the opcode and put this value in the accumulator. During the first<br />

cycle of this instruction, the CPU reads the opcode $A6 and advances<br />

the PC to point to the next location in memory (the address of the<br />

immediate operand ii). During the second cycle of the instruction, the<br />

CPU reads the contents of the byte following the opcode into the<br />

accumulator and advances the PC to point at the next location in<br />

memory (for instance, the opcode byte of the next instruction).<br />

While the accumulator was being loaded, the N and Z bits in the<br />

condition code register were set or cleared according to the data that<br />

was loaded into the accumulator. The Boolean logic formulae for these<br />

bits appear near the middle of the instruction set page. The Z bit will be<br />

set if the value loaded into the accumulator was $00; otherwise, the Z bit<br />

will be cleared. The N bit will be set if the most significant bit of the value<br />

loaded was a logic 1; otherwise, N will be cleared.<br />

The N (negative) condition code bit may be used to detect the sign of a<br />

twos-complement number. In twos-complement numbers, the most<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

75


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Conditional<br />

Branch<br />

Subroutine Calls<br />

and Returns<br />

Freescale Semiconductor, Inc.<br />

significant bit is used as a sign bit, 1 indicates a negative value, and 0<br />

indicates a positive value. The N bit may also be used as a simple<br />

indication of the state of the most significant bit of a binary value.<br />

Branch instructions allow the CPU to select one of two program flow<br />

paths, depending upon the state of a particular bit in memory or various<br />

condition code bits. If the condition checked by the branch instruction is<br />

true, program flow skips to a specified location in memory. If the<br />

condition checked by the branch is not true, the CPU continues to the<br />

instruction following the branch instruction. Decision blocks in a<br />

flowchart correspond to conditional branch instructions in the program.<br />

Most branch instructions contain two bytes, one for the opcode and one<br />

for a relative offset byte. Branch on bit clear (BRCLR) and branch on bit<br />

set (BRSET) instructions require three bytes: the opcode, a 1-byte direct<br />

address (to specify the memory location to be tested), and the relative<br />

offset byte.<br />

The relative offset byte is interpreted by the CPU as a twos-complement<br />

signed value. If the branch condition checked is true, this signed offset<br />

is added to the PC, and the CPU reads its next instruction from this<br />

calculated new address. If the branch condition is not true, the CPU just<br />

continues to the next instruction after the branch instruction.<br />

The jump-to-subroutine (JSR) and branch-to-subroutine (BSR)<br />

instructions automate the process of leaving the normal linear flow of a<br />

program to go off and execute a set of instructions and then return to<br />

where the normal flow left off. The set of instructions outside the normal<br />

program flow is called a subroutine. A JSR or BSR instruction is used to<br />

go from the running program to the subroutine. A return-from-subroutine<br />

(RTS) instruction is used, at the completion of the subroutine, to return<br />

to the program from which the subroutine was called.<br />

The Listing 2. Subroutine Call Example shows lines of an assembler<br />

listing that will be used to demonstrate how the CPU executes a<br />

subroutine call. Assume that the stack pointer (SP) points to address<br />

$00FF when the CPU encounters the JSR instruction at location $0302.<br />

Assembler listings are described in greater detail in the chapter entitled<br />

Programming.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

76 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Listing 2. Subroutine Call Example<br />

Freescale Semiconductor, Inc.<br />

“ “ “ “ “<br />

0300 A6 02 TOP LDA #$02 ;Load an immediate value<br />

0302 CD 04 00 JSR SUBBY ;Go do a subroutine<br />

0305 B7 E0 STA $E0 ;Store accumulator to RAM<br />

0307 “ “ “ “<br />

“ “ “ “ “<br />

“ “ “ “ “<br />

0400 4A SUBBY DECA ;Decrement accumulator<br />

0401 26 FD BNE SUBBY ;Loop till accumulator=0<br />

0403 81 RTS ;Return to main program<br />

Computer Architecture<br />

CPU Operation<br />

Refer to Figure 17 during the following discussion. We begin the<br />

explanation with the CPU executing the instruction LDA #$02 at address<br />

$0300. The left side of the figure shows the normal program flow<br />

composed of TOP LDA #$02, JSR SUBBY, and STA $E0 (in that order)<br />

in consecutive memory locations. The right half of the figure shows<br />

subroutine instructions SUBBY DECA, BNE SUBBY, and RTS.<br />

TOP LDA #$02<br />

JSR SUBBY<br />

STA $E0<br />

$0300 $A6 [1]<br />

[15] [9] $4A $0400<br />

[16] [10]<br />

$0301 $02 [2]<br />

[17] [11]<br />

$0302 $CD [3]<br />

[18] [12] $26 $0401<br />

$0303 $04 [4]<br />

[19]<br />

[20]<br />

[13]<br />

[14]<br />

$FD $0402<br />

$0304 $00 [5]<br />

[21] $81 $0403<br />

[6]<br />

[22]<br />

[7]<br />

[23]<br />

[8]<br />

[24]<br />

[25]<br />

$0305 $B7 [27]<br />

[26]<br />

$0306<br />

START<br />

$E0<br />

Figure 17. Subroutine Call Sequence<br />

The CPU clock cycle numbers (in square brackets) are used as<br />

references in the following explanation of Figure 17.<br />

SUBBY DECA<br />

BNE SUBBY<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

77<br />

[28]<br />

[29]<br />

[30]<br />

RTS


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Freescale Semiconductor, Inc.<br />

[1] CPU reads $A6 opcode from location $0300 (LDA<br />

immediate).<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

[2] CPU reads immediate data $02 from location $0301 into the<br />

accumulator.<br />

[3] CPU reads $CD opcode from location $0302 (JSR<br />

extended).<br />

[4] CPU reads high-order extended address $04 from $0303.<br />

[5] CPU reads low-order extended address $00 from $0304.<br />

[6] CPU builds full address of subroutine ($0400).<br />

[7] CPU writes $05 to $00FF and decrements SP to $00FE.<br />

Another way to say this is “push low-order half of return<br />

address on stack.”<br />

[8] CPU writes $03 to $00FE and decrements SP to $00FD.<br />

Another way to say this is “push high-order half of return<br />

address on stack.” The return address that was saved on the<br />

stack is $0305, which is the address of the instruction that<br />

follows the JSR instruction.<br />

[9] CPU reads $4A opcode from location $0400. This is the first<br />

instruction of the called subroutine.<br />

[10] The CPU uses its ALU to subtract one from the value in the<br />

accumulator.<br />

[11] The ALU result (A – 1) is written back to the accumulator.<br />

[12] CPU reads BNE opcode ($26) from location $0401.<br />

[13] CPU reads relative offset ($FD) from $0402.<br />

78 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

CPU Operation<br />

[14] During the LDA #$02 instruction at [1], the accumulator was<br />

loaded with the value 2; during the DECA instruction at [9],<br />

the accumulator was decremented to 1 (which is not equal<br />

to 0). Thus, at [14], the branch condition was true, and the<br />

twos-complement offset ($FD or –3) was added to the<br />

internal PC (which was $0403 at the time) to get the value<br />

$0400.<br />

[15] through [19] are a repeat of cycles [9] through [13] except that when<br />

the DECA instruction at [15] was executed this time, the<br />

accumulator went from $01 to $00.<br />

[20] Since the accumulator is now equal to 0, the BNE [19]<br />

branch condition is not true, and the branch will not be taken.<br />

[21] CPU reads the RTS opcode ($81) from $0403.<br />

[22] Increment SP to $00FE.<br />

[23] Read high order return address ($03) from stack.<br />

[24] Increment SP to $00FF.<br />

[25] Read low order return address ($05) from stack.<br />

[26] Build recovered address $0305 and store in PC.<br />

[27] CPU reads the STA direct opcode ($B7) from location<br />

$0305.<br />

[28] CPU reads the low-order direct address ($E0) from location<br />

$0306.<br />

[29] [30] The STA direct instruction takes a total of four cycles. During<br />

the last two cycles of the instruction, the CPU constructs the<br />

complete address where the accumulator will be stored by<br />

appending $00 (assumed value for the high-order half of the<br />

address due to direct addressing mode) to the $E0 read<br />

during [28]. The accumulator ($00 at this time) is then stored<br />

to this constructed address ($00E0).<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

79


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Playing Computer<br />

Freescale Semiconductor, Inc.<br />

Playing computer is a learning exercise where you pretend to be a<br />

CPU that is executing a program. Programmers often mentally check<br />

programs by playing computer as they read through a software routine.<br />

While playing computer, it is not necessary to break instructions down to<br />

individual processor cycles. Instead, an instruction is treated as a single<br />

complete operation rather than several detailed steps.<br />

The following paragraphs demonstrate the process of playing computer<br />

by going through the subroutine-call exercise of Figure 17. The<br />

playing-computer approach to analyzing this sequence is much less<br />

detailed than the cycle-by-cycle analysis done earlier, but it<br />

accomplishes the same basic goal (for instance, it shows what happens<br />

as the CPU executes the sequence). After studying the chapter on<br />

programming, you should attempt the same thing with a larger program.<br />

You begin the process by preparing a worksheet like that shown in<br />

Figure 18. This sheet includes the mnemonic program and the machine<br />

code that it assembles to. (You could alternately choose to use a listing<br />

positioned next to the worksheet.) The worksheet also includes the CPU<br />

register names across the top of the sheet. There is ample room below<br />

to write new values as the registers change in the course of the program.<br />

On this worksheet, there is an area for keeping track of the stack. After<br />

you become comfortable with how the stack works, you would probably<br />

leave this section off, but it will be instructive to leave it here for now.<br />

As a value is saved on the stack, you will cross out any prior value and<br />

write the new value to its right in a horizontal row. You must also update<br />

(decrement) the SP value. Cross out any prior value and write the new<br />

value beneath it under the SP heading at the top of the worksheet. As a<br />

value is recovered from the stack, you would update (increment) the<br />

value of SP by crossing out the old value and writing the new value<br />

below it. You would then read the value from the location now pointed to<br />

by the SP and put it wherever it belongs in the CPU (for instance, in the<br />

upper or lower half of the PC).<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

80 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Stack Pointer<br />

$00FC<br />

$00FD<br />

$00FE<br />

$00FF<br />

Freescale Semiconductor, Inc.<br />

Accumulator<br />

Cond. Codes<br />

1 1 1 H I N Z C<br />

0300 A6 02 TOP LDA #$02 ;Load an immediate value<br />

0302 CD 04 00 JSR SUBBY ;Go do a subroutine<br />

0305 B7 02 STA $E0 ;Store accumulator to RAM<br />

" " " " "<br />

" " " " "<br />

" " " " "<br />

0400 4A SUBBYDECA ;Decrement accumulator<br />

0401 26 FD BNE SUBBY ;Loop till accumulator = 0<br />

0403 81 RTS ;Return to main program<br />

LISTING of PROGRAM<br />

Index<br />

Register<br />

to be EXAMINED<br />

Figure 18. Worksheet for Playing Computer<br />

Computer Architecture<br />

Playing Computer<br />

Program<br />

Counter<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

81


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Stack Pointer<br />

$00FF<br />

$00FE<br />

$00FD<br />

$00FE<br />

$00FF<br />

Figure 19 shows how the worksheet will look after working through the<br />

whole JSR sequence. Follow the numbers in square brackets as the<br />

process is explained. During the process, many values were written and<br />

later crossed out; a line has been drawn from the square bracket to<br />

either the value or the crossed-out mark to show which item the<br />

reference number applies to.<br />

Accumulator<br />

[2] [3] [5]<br />

[7]<br />

[11]<br />

[9]<br />

$00FC<br />

$00FD<br />

$00FE $03<br />

$00FF $05<br />

[18]<br />

[19]<br />

$00E0 – RAM $00<br />

[8]<br />

[6]<br />

Freescale Semiconductor, Inc.<br />

$02<br />

$01<br />

$00<br />

Cond. Codes<br />

1 1 1 H I N Z C<br />

1 1 1 ? ? 0 0 ?<br />

1 1 1 ? ? 0 1 ?<br />

0300 A6 02 TOP LDA #$02 ;Load an immediate value<br />

0302 CD 04 00 JSR SUBBY ;Go do a subroutine<br />

0305 B7 02 STA $E0 ;Store accumulator to RAM<br />

" " " " "<br />

" " " " "<br />

" " " " "<br />

0400 4A SUBBYDECA ;Decrement accumulator<br />

0401 26 FD BNE SUBBY ;Loop till accumulator = 0<br />

0403 81 RTS ;Return to main program<br />

Figure 19. Completed Worksheet<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

[21]<br />

[14]<br />

Index<br />

Register<br />

Program<br />

Counter<br />

$0300<br />

$0302<br />

$0400<br />

$0401<br />

$0400<br />

$0401<br />

$0403<br />

$0305<br />

82 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

[15]<br />

[1]<br />

[4]<br />

[10]<br />

[12]<br />

[13]<br />

[16]<br />

[17]<br />

[20]


Freescale Semiconductor, Inc...<br />

Beginning the sequence:<br />

Next:<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Playing Computer<br />

• The PC should be pointing to $0300 [1] and the SP should be<br />

pointing to $00FF [2] (due to an earlier assumption).<br />

• The CPU reads and executes the LDA #$02 instruction (load<br />

accumulator with the immediate value $02).<br />

• Thus,youwrite$02intheaccumulatorcolumn[3]andreplacethe<br />

PC value [4] with $0302, which is the address of the next<br />

instruction.<br />

• The load accumulator instruction affects the N and Z bits in the<br />

CCR.Sincethevalueloadedwas$02,theZbitwouldbecleared,<br />

and the Nbit would be cleared [5]. This information can be found<br />

intheLoadAccumulatorfromMemoryLDAsectionofthe<br />

chapter Instruction Set Details .<br />

• Since the other bits in the CCR are not affected by the LDA<br />

instruction,wehavenowayofknowingwhattheyshouldbeatthis<br />

time, so for now we put question marks in the unknown positions<br />

[5].<br />

• The CPU reads the JSR SUBBY instruction. Temporarily,<br />

remember the value $0305, which is the address where the CPU<br />

should come back to, after executing the called subroutine. The<br />

CPU saves the low-order half of the return address on the stack.<br />

• Thus, you write $05 [6] at the location pointed to by the SP<br />

($00FF) and decrement the SP [7] to $00FE.<br />

• The CPU then saves the high-order half of the return address on<br />

the stack.<br />

• You write $03 [8] to $00FE and again decrement the SP [9], this<br />

time to $00FD.<br />

• To finish the JSR instruction, you load the PC with $0400 [10],<br />

which is the address of the called subroutine.<br />

• The CPU fetches the next instruction. Since the PC is $0400, the<br />

CPU executes the DECA instruction, the first instruction in the<br />

subroutine.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong>Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

83


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Freescale Semiconductor, Inc.<br />

• You cross out the $02 in the accumulator column and write the<br />

new value $01 [11].<br />

• You also change the PC to $0401 [12].<br />

• Because the DECA instruction changed the accumulator from $02<br />

to $01 (which is not zero or negative), the Z bit and N bit remain<br />

clear. Since N and Z were already cleared at [5], you can leave<br />

them alone on the worksheet.<br />

• The CPU now executes the BNE SUBBY instruction. Since the Z<br />

bit is clear, the branch condition is met, and the CPU will take the<br />

branch. Cross out the $0401 under PC and write $0400 [13].<br />

• The CPU again executes the DECA instruction. The accumulator<br />

is now changed from $01 to $00 [14] (which is 0 and not negative);<br />

thus, the Z bit is set, and the N bit remains clear [15].<br />

• The PC advances to the next instruction [16].<br />

• The CPU now executes the BNE SUBBY instruction, but this time<br />

the branch condition is not true (Z is set now), so the branch will<br />

not be taken. The CPU simply falls to the next instruction (the RTS<br />

at $0403).<br />

• Update the PC to $0403 [17].<br />

• The RTS instruction causes the CPU to recover the previously<br />

stacked PC. Pull the high-order half of the PC from the stack by<br />

incrementing the SP to $00FE [18] and by reading $03 from<br />

location $00FE.<br />

• Next, pull the low-order half of the address from the stack by<br />

incrementing SP to $00FF [19] and by reading $05 from $00FF.<br />

The address recovered from the stack replaces the value in the<br />

PC [20].<br />

• The CPU now reads the STA $E0 instruction from location $0305.<br />

Program flow has returned to the main program sequence where<br />

it left off when the subroutine was called.<br />

• The STA (direct addressing mode) instruction writes the<br />

accumulator value to the direct address $E0 ($00E0), which is in<br />

the RAM of the MC68HC705J1A. We can see from the worksheet<br />

that the current value in the accumulator is $00; therefore, all eight<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

84 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Playing Computer<br />

bits of this RAM location will be cleared. Since the original<br />

worksheet did not have a place marked for recording this value in<br />

RAM, you would make a place and write $00 there [21].<br />

For a larger program, the worksheet would have many more crossed out<br />

values by the time you are done. Playing computer on a worksheet like<br />

this is a good learning exercise, but, as a programmer gains experience,<br />

the process would be simplified. In the programming chapter, we will see<br />

a development tool called a simulator that automates the playing<br />

computer process. The simulator is a computer program that runs on a<br />

personal computer. The current contents of registers and memory<br />

locations are displayed on the terminal display of the personal computer.<br />

One of the first simplifications you could make to a manual worksheet<br />

would be to quit keeping track of the PC because you learn to trust the<br />

CPU to take care of this for you. Another simplification is to stop keeping<br />

track of the condition codes. When a branch instruction that depends on<br />

a condition code bit is encountered, you can mentally work backward to<br />

decide whether or not the branch should be taken.<br />

Next, the storage of values on the stack would be skipped, although it is<br />

still a good idea to keep track of the SP value itself. It is fairly common<br />

to have programming errors resulting from incorrect values in the SP. A<br />

fundamental operating principle of the stack is that over a period of time,<br />

the same number of items must be removed from the stack as were put<br />

on the stack. Just as left parentheses must be matched with right<br />

parentheses in a mathematical formula, JSRs and BSRs must be<br />

matched one for one to subsequent RTSs in a program. Errors that<br />

cause this rule to be broken will appear as erroneous SP values while<br />

playing computer.<br />

Even an experienced programmer will play computer occasionally to<br />

solve some difficult problem. The procedure the experienced<br />

programmer would use is much less formal than what was explained<br />

here, but it still amounts to placing yourself in the role of the CPU and<br />

working out what happens as the program is executed.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

85


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Resets<br />

Freescale Semiconductor, Inc.<br />

Reset is used to force the MCU system to a known starting place<br />

(address). Peripheral systems and many control and status bits are also<br />

forced to a known state as a result of reset.<br />

These internal actions occur as the result of any MCU reset:<br />

1. All data direction registers cleared to 0 (input)<br />

2. Stack pointer forced to $00FF<br />

3. I bit in the CCR set to 1 to inhibit maskable interrupts<br />

4. External interrupt latch cleared<br />

5. STOP latch cleared<br />

6. WAIT latch cleared<br />

As the computer system leaves reset, the program counter is loaded<br />

from the two highest memory locations ($07FE and $07FF in an<br />

MC68HC705J1A). The value from $07FE is loaded into the high order<br />

byte of the PC and the value from $07FF is loaded into the low order byte<br />

of the PC. This is called “fetching the reset vector.” At this point, the<br />

CPU begins to fetch and execute instructions, beginning at the address<br />

that was stored in the reset vector.<br />

Any of these conditions can cause the MC68HC705J1A MCU to reset:<br />

1. External, active-low input signal on the RESET pin<br />

2. Internal power-on reset (POR)<br />

3. Internal computer operating properly (COP) watchdog timed out<br />

4. An attempt to execute an instruction from an illegal address<br />

RESET Pin An external switch or circuit can be connected to this pin to allow a<br />

manual system reset.<br />

Power-On Reset The power-on reset occurs when a positive transition is detected on<br />

VDD. The power-on reset is used strictly for power turn-on conditions<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

86 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Watchdog Timer<br />

Reset<br />

Illegal Address<br />

Reset<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Resets<br />

and should not be used to detect any drops in the power supply voltage.<br />

A low-voltage inhibit (LVI) circuit should be used to detect loss of power.<br />

The power-on circuitry provides for a 4064-cycle delay from the time that<br />

the oscillator becomes active. If the external RESET pin is low at the end<br />

of the 4064-cycle delay timeout, the processor remains in the reset<br />

condition until RESET goes high.<br />

The computer operating properly (COP) watchdog timer system is<br />

intended to detect software errors. When the COP is being used,<br />

software is responsible for keeping a free-running watchdog timer from<br />

timing out. If the watchdog timer times out, it is an indication that<br />

software is no longer being executed in the intended sequence; thus, a<br />

system reset is initiated.<br />

A control bit in the non-volatile mask option control register can be used<br />

to enable or disable the COP reset. If the COP is enabled, the operating<br />

program must periodically write a 0 to the COPC bit in the COPR control<br />

register. Refer to the MC68HC705J1A Technical Data (Motorola order<br />

number MC68HC705J1A/D) for information about the COP timeout rate.<br />

Some members of the <strong>M68HC05</strong> <strong>Family</strong> have different COP watchdog<br />

timer systems.<br />

If a program is written incorrectly, it is possible that the CPU will attempt<br />

to jump or branch to an address that has no memory. If this happened,<br />

the CPU would continue to read data (though it would be unpredictable<br />

values) and attempt to act on it as if it were a program. These nonsense<br />

instructions could cause the CPU to write unexpected data to<br />

unexpected memory or register addresses. This situation is called<br />

program runaway.<br />

To guard against this runaway condition, there is an illegal address<br />

detect circuit in the MC68HC705J1A. If the CPU attempts to fetch an<br />

instruction from an address that is not in the EPROM ($0300–$07CF,<br />

$07F0–$07FF), internal test ROM ($07EE–$07EF), or RAM<br />

($00C0–$00FF), a reset is generated to force the program to start over.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

87


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Interrupts<br />

Freescale Semiconductor, Inc.<br />

It is sometimes useful to interrupt normal processing to respond to some<br />

unusual event. For instance, the MC68HC705J1A may be interrupted by<br />

any of these sources:<br />

1. A logic 0 applied to the external interrupt IRQ pin<br />

2. A logic 1 applied to any of the PA3–PA0 pins, provided the port<br />

interrupt function is enabled<br />

3. A timer overflow (TOF) or real-time interrupt (RTIF) request from<br />

the on-chip multifunctional timer system, if enabled<br />

4. The software interrupt (SWI) instruction<br />

If an interrupt comes while the CPU is executing an instruction, the<br />

instruction is completed before the CPU responds to the interrupt.<br />

Interrupts can be inhibited by setting the I bit in the condition code<br />

register (CCR) or by clearing individual interrupt enable control bits for<br />

each interrupt source. Reset forces the I bit to 1 and clears all local<br />

interrupt enable bits to prevent interrupts during the initialization<br />

procedure. When the I bit is 1, no interrupts (except the SWI instruction)<br />

are recognized. However, interrupt sources may still register a request<br />

that will be honored at some later time when the I bit is cleared.<br />

Figure 20 shows how interrupts fit into the normal flow of CPU<br />

instructions. Interrupts cause the processor registers to be saved on the<br />

stack and the interrupt mask (I bit) to be set, to prevent additional<br />

interrupts until the present interrupt is finished. The appropriate interrupt<br />

vector then points to the starting address of the interrupt service routine<br />

(Table 10). Upon completion of the interrupt service routine, an RTI<br />

instruction (which is normally the last instruction of an interrupt service<br />

routine) causes the register contents to be recovered from the stack.<br />

Since the program counter is loaded with the value that was previously<br />

saved on the stack, processing continues from where it left off before the<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

88 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Interrupts<br />

interrupt. Figure 21 shows that registers are restored from the stack in<br />

the opposite order they were saved.<br />

Table 10. Vector Addresses for Resets and Interrupts<br />

on the MC68HC705J1A<br />

Reset or Interrupt Source Vector Address<br />

On-Chip Timer $07F8, $07F9<br />

IRQ or Port A Pins $07FA, $07FB<br />

SWI Instruction $07FC, $07FD<br />

Reset (POR, LVI, Pin, COP, or Illegal Address) $07FE, $07FF<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

89


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Freescale Semiconductor, Inc.<br />

FROM<br />

RESET<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

YES<br />

I BIT IN CCR SET ?<br />

NO<br />

EXTERNAL INTERRUPT ?<br />

(IRQ OR PORT A)<br />

NO<br />

TIMER INTERRUPT ?<br />

NO<br />

FETCH NEXT<br />

INSTRUCTION<br />

SWI INSTRUCTION ?<br />

NO<br />

RTI INSTRUCTION ?<br />

NO<br />

EXECUTE<br />

INSTRUCTION<br />

STACK<br />

PC, X, A, CCR<br />

Figure 20. Hardware Interrupt Flowchart<br />

90 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

YES<br />

YES<br />

YES<br />

YES<br />

CLEAR IRQ<br />

REQUEST LATCH<br />

SET I BIT<br />

IN CCR<br />

LOAD PC FROM VECTOR:<br />

SWI: $07FC, $07FD<br />

IRQ OR PORT A: $07FA, $07FB<br />

TIMER: $07F8, $07F9<br />

RESTORE REGISTERS<br />

FROM STACK<br />

CCR, A, X, PC


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

STACK<br />

INTERRUPT<br />

TOWARD LOWER ADDRESSES<br />

LOWEST STACK ADDRESS IS $00C0<br />

7 0<br />

1 1 1<br />

0 0 0<br />

ACCUMULATOR<br />

Figure 21. Interrupt Stacking Order<br />

Computer Architecture<br />

Interrupts<br />

External Interrupts External interrupts come from the IRQ pin or from bits 3–0 of port A if port<br />

A is configured for port interrupts. In the MC68HC705J1A MCU, the IRQ<br />

pin sensitivity is software programmable.<br />

In the MC68HC705J1A MCU, two choices of external interrupts are<br />

available:<br />

• Edge-sensitive triggering only<br />

CONDITION CODES<br />

INDEX REGISTER<br />

PROGRAM COUNTER LOW<br />

PC HIGH<br />

TOWARD HIGHER ADDRESSES<br />

HIGHEST STACK ADDRESS IS $00FF<br />

• Negative edge- and level-sensitive triggering<br />

The MC68HC705J1A MCU uses a bit in an option register at location<br />

$07F1 to configure the IRQ pin sensitivity. The IRQ pin is low true and<br />

the port A interrupts are high true.<br />

When an interrupt is recognized, the current state of the CPU is pushed<br />

onto the stack and the I bit is set. This masks further interrupts until the<br />

present one is serviced. The address of the external interrupt service<br />

routine is specified by the contents of memory locations $07FA and<br />

$07FB.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

91<br />

0 0<br />

RETURN<br />

UNSTACK<br />

NOTE: When an interrupt occurs, CPU registers are<br />

saved on the stack in the order PCL, PCH, X, A, CCR.<br />

On a return-from-interrupt, registers are recovered<br />

from the stack in reverse order.


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

On-Chip<br />

Peripheral<br />

Interrupts<br />

Software Interrupt<br />

(SWI)<br />

Freescale Semiconductor, Inc.<br />

<strong>Microcontrollers</strong> often include on-chip peripheral systems that can<br />

generate interrupts to the CPU. The timer system in the<br />

MC68HC705J1A is an example of such a peripheral. On-chip peripheral<br />

interrupts work just like external interrupts except that there are normally<br />

separate interrupt vectors for each on-chip peripheral system.<br />

The software interrupt is an executable instruction. The action of the SWI<br />

instruction is similar to the hardware interrupts. An SWI is executed<br />

regardless of the state of the interrupt mask (I bit) in the condition code<br />

register. The interrupt service routine address is specified by the<br />

contents of memory location $07FC and $07FD in an MC68HC705J1A.<br />

Interrupt Latency Although we think of interrupts as if they cause the CPU to stop normal<br />

processing immediately in order to respond to the interrupt request, this<br />

is not quite the case. There is a small delay from when an interrupt is<br />

requested until the CPU can actually respond. First, the CPU must finish<br />

any instruction that happens to be in progress at the time the interrupt is<br />

requested. (The CPU would not know how to resume processing after<br />

the interrupt was handled if it had stopped in the middle of an<br />

instruction.) Second, the CPU must make a record of what it was doing<br />

before it responded to the interrupt. The CPU does this by storing a copy<br />

of the contents of all its registers, including the program counter, on the<br />

stack. After the interrupt has been serviced, the CPU recovers this<br />

information in reverse order and normal processing resumes.<br />

Interrupt latency is the total number of CPU cycles (time) from the initial<br />

interrupt request until the CPU starts to execute the first instruction of the<br />

interrupt service routine. This delay depends upon whether or not the I<br />

interrupt mask is set to 1 when the interrupt is requested. If the I bit is<br />

set, the delay could be indefinite and depends upon when an instruction<br />

clears the I bit so the interrupt can be recognized by the CPU. In the<br />

normal case, where the I bit is clear when the interrupt is requested, the<br />

latency will consist of finishing the current instruction, saving the<br />

registers on the stack, and loading the interrupt vector (address of the<br />

interrupt service routine) into the program counter.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

92 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Interrupts<br />

The longest instruction (execution time) in the <strong>M68HC05</strong> is the multiply<br />

(MUL) instruction, which takes 11 bus cycles. If the CPU had just started<br />

to execute a MUL instruction when an interrupt was requested, a delay<br />

of up to 11 cycles would be experienced before the CPU could respond.<br />

It takes the CPU nine bus cycles to save a copy of its registers on the<br />

stack and to fetch the interrupt vector. The total worst-case latency if I<br />

was clear and a MUL instruction just started would be 20 cycles (11 + 9).<br />

The I bit is set to 1 as the CPU responds to an interrupt so that (normally)<br />

a new interrupt will not be recognized until the current one has been<br />

handled. In a system that has more than one source of interrupts, the<br />

execution time for the longest interrupt service routine must be<br />

calculated in order to determine the worst-case interrupt latency for the<br />

other interrupt sources.<br />

Nested Interrupts In unusual cases, an interrupt service routine may take so long to<br />

execute that the worst-case latency for other interrupts in the system is<br />

too long. In such a case, instructions in the long interrupt service routine<br />

could clear the I bit to zero, thus allowing a new interrupt to be<br />

recognized before the first interrupt service routine is finished. If a new<br />

interrupt is requested while the CPU is already servicing an interrupt, it<br />

is called nesting. You must use great care if you allow interrupt nesting<br />

because the stack must have enough space to hold more than one copy<br />

of the CPU registers. On small microcontrollers like the MC68HC05K1,<br />

the stack is small and nesting of interrupts is not recommended.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

93


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Review<br />

Freescale Semiconductor, Inc.<br />

In the <strong>M68HC05</strong> architecture, five CPU registers are directly connected<br />

within the CPU and are not part of the memory map. All other information<br />

available to the CPU is located in a series of 8-bit memory locations. A<br />

memory map shows the names and types of memory at all locations<br />

that are accessible to the CPU. The expression memory-mapped I/O<br />

means that the CPU treats I/O and control registers exactly like any<br />

other kind of memory. (Some computer architectures separate the I/O<br />

registers from program memory space and use separate instructions to<br />

access I/O locations.)<br />

To get started in a known place, a computer must be reset. Reset forces<br />

on-chip peripheral systems and I/O logic to known conditions and loads<br />

the program counter with a known starting address. The user specifies<br />

the desired starting location by placing the upper and lower order bytes<br />

of this address in the reset vector locations ($07FE and $07FF on the<br />

MC68HC705J1A).<br />

The CPU uses the stack pointer (SP) register to implement a<br />

last-in-first-out stack in RAM memory. This stack holds return addresses<br />

while the CPU is executing a subroutine and holds the previous contents<br />

of all CPU registers while the CPU is executing an interrupt sequence.<br />

By recovering this information from the stack, the CPU can resume<br />

where it left off before the subroutine or interrupt was started.<br />

Computers use a high speed clock to step through each small substep<br />

of each operation. Although each instruction takes several cycles of this<br />

clock, it is so fast that operations seem to be instantaneous to a human.<br />

An MC68HC705J1A can execute about 500,000 instructions per<br />

second.<br />

A CPU sees a program as a linear sequence of 8-bit binary numbers.<br />

Instruction opcodes and data are mixed in this sequence but the CPU<br />

remains aligned to instruction boundaries because each opcode tells the<br />

CPU how many operand data bytes go with each instruction opcode.<br />

Playing computer is a learning exercise where you pretend to be a<br />

CPU that is executing a program.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

94 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Computer Architecture<br />

Review<br />

Reset can be caused by internal or external conditions. A reset pin<br />

allows an external cause to initiate a reset. A watchdog timer and an<br />

illegal address detect system can cause reset in the event software is<br />

not executing in the intended sequence.<br />

Interrupts cause the CPU to temporarily stop main program processing<br />

to respond to the interrupt. All CPU registers are saved on the stack so<br />

the CPU can go back to where it left off in the main program as soon as<br />

the interrupt is serviced.<br />

Interrupts can be inhibited globally by setting the I bit in the CCR or<br />

locally by clearing enable control bits for each interrupt source. Requests<br />

can still be registered while interrupts are inhibited so the CPU can<br />

respond as soon as the interrupts are re-enabled. SWI is an instruction<br />

and cannot be inhibited.<br />

Interrupt latency is the delay from when an interrupt is requested to<br />

when the CPU begins executing the first instruction in the interrupt<br />

response program. When a CPU responds to a new interrupt while it is<br />

already processing an interrupt (which is not normally allowed), it is<br />

called a nested interrupt.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

95


Freescale Semiconductor, Inc...<br />

Computer Architecture<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

96 Computer Architecture<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98<br />

Central Processor Unit (CPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98<br />

Arithmetic/Logic Unit (ALU). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98<br />

CPU Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99<br />

CPU Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99<br />

Accumulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

Index Register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

Condition Code Register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100<br />

Half-Carry Bit (H) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101<br />

Interrupt Mask Bit (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101<br />

Negative Bit (N). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101<br />

Zero Bit (Z) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102<br />

Carry/Borrow Bit (C) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102<br />

Program Counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103<br />

Stack Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103<br />

Addressing Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104<br />

Inherent Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105<br />

Immediate Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . .107<br />

Extended Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108<br />

Direct Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110<br />

Indexed Addressing Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112<br />

Indexed, No Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112<br />

Indexed, 8-Bit Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114<br />

Indexed, 16-Bit Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116<br />

Relative Addressing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118<br />

Bit Test and Branch Instructions . . . . . . . . . . . . . . . . . . . . . . . . . .120<br />

Instructions Organized by Type . . . . . . . . . . . . . . . . . . . . . . . . . .120<br />

Instruction Set Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133<br />

CPU Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133<br />

Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133<br />

Instruction Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

97


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Introduction<br />

Central Processor Unit (CPU)<br />

Arithmetic/Logic<br />

Unit (ALU)<br />

Freescale Semiconductor, Inc.<br />

A computer’s instruction set is its vocabulary. This chapter describes the<br />

CPU and instruction set of the <strong>M68HC05</strong>. Instruction Set Details<br />

contains detailed descriptions of each <strong>M68HC05</strong> instruction and can be<br />

used as a reference. This chapter discusses the same instructions in<br />

groups of functionally similar operations. The structure and addressing<br />

modes of the <strong>M68HC05</strong> are also discussed. Addressing modes refer to<br />

the various ways a CPU can access operands for an instruction.<br />

The <strong>M68HC05</strong> CPU is responsible for executing all software instructions<br />

in their programmed sequence for a specific application.<br />

The <strong>M68HC05</strong> CPU block diagram is shown in Figure 22.<br />

CPU<br />

CONTROL<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

ARITHMETIC<br />

LOGIC UNIT<br />

(ALU)<br />

<strong>M68HC05</strong> CPU<br />

CPU REGISTERS ACCUMULATOR<br />

INDEX REGISTER<br />

0 0 0 0 0 1 1 STACK POINTER<br />

0 0 0 PROGRAM COUNTER<br />

CONDITION CODES<br />

1 1 1 H I N Z C<br />

Figure 22. <strong>M68HC05</strong> CPU Block Diagram<br />

The arithmetic logic unit (ALU) is used to perform the arithmetic and<br />

logical operations defined by the instruction set.<br />

The various binary arithmetic operations circuits decode the instruction<br />

in the instruction register and set up the ALU for the desired function.<br />

Most binary arithmetic is based on the addition algorithm, and<br />

subtraction is carried out as negative addition. Multiplication is not<br />

performed as a discrete instruction but as a chain of addition and shift<br />

98 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Central Processor Unit (CPU)<br />

operations within the ALU under control of CPU control logic. The<br />

multiply instruction (MUL) requires 11 internal processor cycles to<br />

complete this chain of operations.<br />

CPU Control The CPU control circuitry sequences the logic elements of the ALU to<br />

carry out the required operations. A central element of the CPU control<br />

section is the instruction decoder. Each opcode is decoded to<br />

determine how many operands are needed and what sequence of steps<br />

will be required to complete the instruction. When one instruction is<br />

finished, the next opcode is read and decoded.<br />

CPU Registers The CPU contains five registers as shown in Figure 23. Registers in the<br />

CPU are memories inside the microprocessor (not part of the memory<br />

map). The set of registers in a CPU is sometimes called a programming<br />

model. An experienced programmer can tell a lot about a computer from<br />

its programming model.<br />

15 12<br />

1 1 1 H I N Z C<br />

Figure 23. Programming Model<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

99<br />

7<br />

12 7 5<br />

0<br />

0 0 0 0 0 1 1 STACK POINTER<br />

0 0 0 PROGRAM COUNTER<br />

CONDITION CODE REGISTER<br />

ACCUMULATOR<br />

INDEX REGISTER<br />

7 4 3 2 1<br />

0<br />

0<br />

0<br />

A<br />

X<br />

SP<br />

PC<br />

CC R<br />

CARRY<br />

ZERO<br />

NEGATIVE<br />

INTERRUPT MASK<br />

HALF-CARRY<br />

(FROM BIT 3)


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Accumulator The accumulator is an 8-bit general-purpose register used to hold<br />

operands, results of the arithmetic calculations, and data manipulations.<br />

It is also directly accessible to the CPU for non-arithmetic operations.<br />

The accumulator is used during the execution of a program when the<br />

contents of some memory location are loaded into the accumulator.<br />

Also, the store instruction causes the contents of the accumulator to be<br />

stored at some prescribed memory location.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Figure 24. Accumulator (A)<br />

Index Register The index register is used for indexed modes of addressing or may be<br />

used as an auxiliary accumulator. This 8-bit register can be loaded either<br />

directly or from memory, its contents can be stored in memory, or its<br />

contents can be compared to memory.<br />

Condition Code<br />

Register<br />

Freescale Semiconductor, Inc.<br />

In indexed instructions, the X register provides an 8-bit value that is<br />

added to an instruction-provided value to create an effective address.<br />

The instruction-provided value can be 0, 1, or 2 bytes long.<br />

Figure 25. Index Register (X)<br />

The condition code register contains an interrupt mask and four status<br />

indicators that reflect the results of arithmetic and other operations of the<br />

CPU. The five flags are:<br />

• Half-carry (H)<br />

• Negative (N)<br />

• Zero (Z)<br />

• Overflow (V)<br />

• Carry borrow (C)<br />

7<br />

7<br />

ACCUMULATOR A<br />

INDEX REGISTER X<br />

100 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

0<br />

0


Freescale Semiconductor, Inc...<br />

Figure 26. Condition Code Register (CCR)<br />

<strong>M68HC05</strong> Instruction Set<br />

Central Processor Unit (CPU)<br />

Half-Carry Bit (H) The half-carry flag is used for binary-coded decimal (BCD) arithmetic<br />

operations and is affected by the ADD or ADC addition instructions. The<br />

H bit is set to a 1 when a carry occurs from the low-order hexadecimal<br />

digit in bits 3–0 and the high-order digit in bits 7–4. After the binary<br />

addition of two 2-digit BCD values, this half-carry bit is one piece of<br />

information needed to restore the result to a valid BCD value.<br />

Interrupt Mask<br />

Bit (I)<br />

Freescale Semiconductor, Inc.<br />

7 4 3 2 1<br />

CONDITION CODE REGISTER 1 1 1 H I N Z C CC<br />

The I bit is not a status flag but an interrupt mask bit that disables all<br />

maskable interrupt sources when the I bit is set. Interrupts are enabled<br />

when this bit is a 0. When any interrupt occurs, the I bit is set<br />

automatically after the registers are stacked but before the interrupt<br />

vector is fetched.<br />

If an external interrupt occurs while the I bit is set, the interrupt is latched<br />

and processed after the I bit is cleared; therefore, no interrupts from the<br />

IRQ pin are lost because of the I bit being set.<br />

After an interrupt has been serviced, a return-from-interrupt (RTI)<br />

instruction causes the registers to be restored to their previous values.<br />

Normally, the I bit would be 0 after an RTI was executed. After any reset,<br />

I is set and can be cleared only by a software instruction.<br />

Negative Bit (N) The N bit is set to 1 when the result of the last arithmetic, logical, or data<br />

manipulation is negative. Twos-complement signed values are<br />

considered negative if the most significant bit is a 1.<br />

The N bit has other uses. By assigning an often-tested flag bit to the<br />

MSB of a register or memory location, you can test this bit simply by<br />

loading the accumulator with the contents of that location.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

101<br />

0<br />

CARRY<br />

ZERO<br />

NEGATIVE<br />

INTERRUPT MASK<br />

HALF-CARRY (FROM BIT 3)


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Zero Bit (Z) The Z bit is set to 1 when the result of the last arithmetic, logical, or data<br />

manipulation is 0. A compare instruction subtracts a value from the<br />

memory location being tested. If the values were equal to each other<br />

before the compare, the Z bit will be set.<br />

Carry/Borrow Bit<br />

(C)<br />

7<br />

The C bit is used to indicate if there was a carry from an addition or a<br />

borrow as a result of a subtraction. Shift and rotate instructions operate<br />

with and through the carry bit to facilitate multiple word shift operations.<br />

This bit is also affected during bit test and branch instructions.<br />

The illustration in Figure 27 is an example of the way condition code bits<br />

are affected by arithmetic operations. The H bit is not useful after this<br />

operation because the accumulator was not a valid BCD value before<br />

the operation.<br />

ACCUMULATOR CONDITION CODES<br />

0<br />

H I N Z<br />

BEFORE 1 1 1 1 1 1 1 1 ($FF) 1 1 1 0 1 1 0 0<br />

7<br />

Freescale Semiconductor, Inc.<br />

ASSUME INITIAL VALUES IN ACCUMULATOR AND CONDITION CODES:<br />

EXECUTE THIS INSTRUCTION:<br />

– – – –AB 02 ADD #2 ADD 2 TO ACCUMULATOR<br />

CONDITION CODES AND ACCUMULATOR REFLECT THE RESULTS OF THE ADD INSTRUCTION:<br />

ACCUMULATOR CONDITION CODES<br />

0<br />

H I N Z<br />

AFTER 0 0 0 0 0 0 0 1 ($01) 1 1 1 1 1 0 0 1<br />

H – Set because there was a carry from bit 3 to bit 4 of the accumulator<br />

I – No change<br />

N – Clear because result is not negative (bit 7 of accumulator is 0)<br />

Z – Clear because result is not 0<br />

C – Set because there was a carry out of bit 7 of the accumulator<br />

Figure 27. How Condition Codes are Affected by Arithmetic Operations<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

102 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

C<br />

C


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Central Processor Unit (CPU)<br />

Program Counter The program counter is a 16-bit register that contains the address of the<br />

next instruction or instruction operand to be fetched by the processor. In<br />

most variations of the <strong>M68HC05</strong>, some of the upper bits of the program<br />

counter are not used and are always 0. The MC68HC705J1A, for<br />

instance, uses only 11 bits of the program counter so the upper five bits<br />

are always 0. The number of useful bits in the program counter exactly<br />

matches the number of address lines implemented in the computer<br />

system.<br />

15 10<br />

0 0 0 0 0<br />

PROGRAM COUNTER<br />

Figure 28. Program Counter (PC)<br />

Normally, the program counter advances one memory location at a time<br />

as instructions and instruction operands are fetched.<br />

Jump, branch, and interrupt operations cause the program counter to be<br />

loaded with a memory address other than that of the next sequential<br />

location.<br />

Stack Pointer The stack pointer must have as many bits as there are address lines; in<br />

the MC68HC705J1A this means the SP is an 11-bit register. During an<br />

MCU reset or the reset stack pointer (RSP) instruction, the stack pointer<br />

is set to location $00FF. The stack pointer is then decremented as data<br />

is pushed onto the stack and incremented as data is pulled from the<br />

stack.<br />

10 7 5<br />

0<br />

0<br />

0 0 1 1 STACK POINTER<br />

Figure 29. Stack Pointer (SP)<br />

Many variations of the <strong>M68HC05</strong> allow the stack to use up to 64<br />

locations ($00FF to $00C0), but the smallest versions allow only 32<br />

bytes of stack ($00FF to $00E0). In the MC68HC705J1A, the five MSBs<br />

of the SP are permanently set to 00011. These five bits are appended to<br />

the six least significant bits to produce an address within the range of<br />

$00FF to $00C0. Subroutines and interrupts may use up to 64 (decimal)<br />

locations. If 64 locations are exceeded, the stack pointer wraps around<br />

to $00FF and begins to write over previously stored information. A<br />

subroutine call occupies two locations on the stack; an interrupt uses five<br />

locations.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

103<br />

SP<br />

0<br />

PC


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

Freescale Semiconductor, Inc.<br />

The power of any computer lies in its ability to access memory. The<br />

addressing modes of the CPU provide that capability. The addressing<br />

modes define the manner in which an instruction is to obtain the data<br />

required for its execution. Because of different addressing modes, an<br />

instruction may access the operand in one of up to six different ways. In<br />

this manner, the addressing modes expand the basic 62 <strong>M68HC05</strong><br />

<strong>Family</strong> instructions into 210 distinct opcodes.<br />

The <strong>M68HC05</strong> addressing modes that are used to reference memory<br />

are:<br />

• Inherent<br />

• Immediate<br />

• Extended<br />

• Direct<br />

• Indexed,no offset, 8-bit offset, and 16-bit offset,<br />

• Relative<br />

Inherent instructions don’t need to access memory, so they are<br />

single-byte instructions. In smaller <strong>M68HC05</strong>s, all RAM and I/O registers<br />

are within the $0000–$00FF area of memory so two-byte direct<br />

addressing mode instructions can be used. Extended addressing uses<br />

3-byte instructions to reach data anywhere in memory space. The<br />

various addressing modes make it possible to locate data tables, code<br />

conversion tables, and scaling tables anywhere in the memory space.<br />

Short indexed accesses are single-byte instructions, but the longest<br />

instructions (three bytes) permit accessing tables anywhere in memory.<br />

A general description and examples of the various modes of addressing<br />

are provided in the following paragraphs. The term effective address<br />

(EA) is used to indicate the memory address where the argument for an<br />

instruction is fetched or stored. More details on addressing modes and<br />

a description of each instruction are available in the chapter entitled<br />

Instruction Set Details.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

104 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Inherent<br />

Addressing Mode<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

The information provided in the program assembly examples uses<br />

several symbols to identify the various types of numbers that occur in a<br />

program. These symbols include:<br />

1. A blank or no symbol indicates a decimal number.<br />

2. A $ immediately preceding a number indicates it is a hexadecimal<br />

number; for example, $24 is 24 in hexadecimal or the equivalent<br />

of 36 in decimal.<br />

3. A # indicates immediate operand and the number is found in the<br />

location following the opcode. A variety of symbols and<br />

expressions can be used following the character # sign. Since not<br />

all assemblers use the same syntax rules and special characters,<br />

refer to the documentation for the particular assembler that will be<br />

used.<br />

Prefix Indicates the value that follows is . . .<br />

None Decimal<br />

$ Hexadecimal<br />

@ Octal<br />

% Binary<br />

’ Single ASCII Character<br />

For each addressing mode, an example instruction is explained in detail.<br />

These explanations describe what happens in the CPU during each<br />

processor clock cycle of the instruction. Numbers in square brackets<br />

refer to a specific CPU clock cycle.<br />

In inherent addressing mode, all information required for the operation is<br />

already inherently known to the CPU, and no external operand from<br />

memory or from the program is needed. The operands, if any, are only<br />

the index register and accumulator, and are always 1-byte instructions.<br />

Example Program Listing:<br />

0300 4c<br />

Execution Sequence:<br />

INCA ;Increment accumulator<br />

$0300 $4C [1], [2], [3]<br />

Explanation:<br />

[1] CPU reads opcode $4C <strong>—</strong> increment accumulator<br />

[2] and [3] CPU reads accumulator value, adds one to it, stores the new<br />

value in the accumulator, and adjusts condition code flag<br />

bits as necessary.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

105


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Freescale Semiconductor, Inc.<br />

The following is a list of all <strong>M68HC05</strong> instructions that can use the<br />

inherent addressing mode.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Instruction Mnemonic<br />

Arithmetic Shift Left ASLA,ASLX<br />

Arithmetic Shift Right ASRA,ASRX<br />

Clear Carry Bit CLC<br />

Clear Interrupt Mask Bit CLI<br />

Clear CLRA,CLRX<br />

Complement COMA, COMX<br />

Decrement DECA,DECX<br />

Increment INCA, INCX<br />

Logical Shift Left LSLA,LSLX<br />

Logical Shift Right LSRA, LSRX<br />

Multiply MUL<br />

Negate NEGA,NEGX<br />

No Operation NOP<br />

Rotate Left thru Carry ROLA, ROLX<br />

Rotate Right thru Carry RORA, RORX<br />

Reset Stack Pointer RSP<br />

Return from Interrupt RTI<br />

Return from Subroutine RTS<br />

Set Carry Bit SEC<br />

Set Interrupt Mask Bit SEI<br />

Enable IRQ, Stop Oscillator STOP<br />

Software Interrupt SWI<br />

Transfer Accumulator to Index Register TAX<br />

Test for Negative or Zero TSTA,TSTX<br />

Transfer Index Register to Accumulator TXA<br />

Enable Interrupt, Halt Processor WAIT<br />

106 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Immediate<br />

Addressing Mode<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

In the immediate addressing mode, the operand is contained in the byte<br />

immediately following the opcode. This mode is used to hold a value or<br />

constant which is known at the time the program is written and which is<br />

not changed during program execution. These are 2-byte instructions,<br />

one for the opcode and one for the immediate data byte.<br />

Example Program Listing:<br />

0300 a6 03 LDA #$03 ;Load accumulator w/ immediate value<br />

Execution Sequence:<br />

$0300 $A6 [1]<br />

$0301 $03 [2]<br />

Explanation:<br />

[1] CPU reads opcode $A6 <strong>—</strong> load accumulator with the value<br />

immediately following the opcode.<br />

[2] CPU then reads the immediate data $03 from location<br />

$0301 and loads $03 into the accumulator.<br />

The following is a list of all <strong>M68HC05</strong> instructions that can use the<br />

immediate addressing mode.<br />

Instruction Mnemonic<br />

Add with Carry ADC<br />

Add ADD<br />

Logical AND AND<br />

Bit Test Memory with Accumulator BIT<br />

Compare Accumulator with Memory CMP<br />

Compare Index Register with Memory CPX<br />

Exclusive OR Memory with Accumulator EOR<br />

Load Accumulator from Memory LIDA<br />

Load Index Register from Memory LDX<br />

Inclusive OR ORA<br />

Subtract with Carry SBC<br />

Subtract SUB<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

107


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Extended<br />

Addressing Mode<br />

Freescale Semiconductor, Inc.<br />

In the extended addressing mode, the address of the operand is<br />

contained in the two bytes following the opcode. Extended addressing<br />

references any location in the MCU memory space including I/O, RAM,<br />

ROM, and EPROM. Extended addressing mode instructions are three<br />

bytes, one for the opcode and two for the address of the operand.<br />

Example Program Listing:<br />

0300 c6 06 e5 LDA $06E5 ;Load accumulator from extended addr<br />

Execution Sequence:<br />

$0300 $C6 [1]<br />

$0301 $06 [2]<br />

$0302 $E5 [3] and [4]<br />

Explanation:<br />

[1] CPU reads opcode $C6 <strong>—</strong> load accumulator using extended<br />

addressing mode.<br />

[2] CPU then reads $06 from location $0301. This $06 is<br />

interpreted as the high-order half of an address.<br />

[3] CPU then reads $E5 from location $0302. This $E5 is<br />

interpreted as the low-order half of an address.<br />

[4] CPU internally appends $06 to the $E5 read to form the<br />

complete address ($06E5). The CPU then reads whatever<br />

value is contained in the location $06E5 into the<br />

accumulator.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

108 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

The following is a list of all <strong>M68HC05</strong> instructions that can use the<br />

extended addressing mode.<br />

Instruction Mnemonic<br />

Add with Carry ADC<br />

Add ADD<br />

Logical AND AND<br />

Bit Test Memory with Accumulator BIT<br />

Compare Accumulator with Memory CMP<br />

Compare Index Register with Memory CPX<br />

Exclusive OR Memory with Accumulator EOR<br />

Jump JMP<br />

Jump to Subroutine JSR<br />

Load Accumulator from Memory LDA<br />

Load Index Register from Memory LDX<br />

Inclusive OR ORA<br />

Subtract with Carry SBC<br />

Store Accumulator in Memory STA<br />

Store Index Register in Memory STX<br />

Subtract SUB<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

109


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Direct Addressing<br />

Mode<br />

Freescale Semiconductor, Inc.<br />

The direct addressing mode is similar to the extended addressing mode<br />

except the upper byte of the operand address is assumed to be $00.<br />

Thus, only the lower byte of the operand address needs to be included<br />

in the instruction. Direct addressing allows you to efficiently address the<br />

lowest 256 bytes in memory. This area of memory is called the direct<br />

page and includes on-chip RAM and I/O registers. Direct addressing is<br />

efficient in both memory and time. Direct addressing mode instructions<br />

are usually two bytes, one for the opcode and one for the low-order byte<br />

of the operand address.<br />

Example Program Listing:<br />

0300 b6 50 LDA $50 ;Load accumulator from direct address<br />

Execution Sequence:<br />

$0300 $B6 [1]<br />

$0301 $50 [2] and [3]<br />

Explanation:<br />

[1] CPU reads opcode $B6 <strong>—</strong> load accumulator using direct<br />

addressing mode.<br />

[2] CPU then reads $50 from location $0301. This $50 is<br />

interpreted as the low-order half of an address. In direct<br />

addressing mode, the high-order half of the address is<br />

assumed to be $00.<br />

[3] CPU internally appends $00 to the $50 read in the second<br />

cycle to form the complete address ($0050). The CPU then<br />

reads whatever value is contained in the location $0050 into<br />

the accumulator.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

110 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

The following is a list of all <strong>M68HC05</strong> instructions that can use the direct<br />

addressing mode.<br />

Instruction Mnemonic<br />

Add with Carry ADC<br />

Add ADD<br />

Logical AND AND<br />

Arithmetic Shift Left ASL<br />

Arithmetic Shift Right ASR<br />

Clear Bit in Memory BCLR<br />

Bit Test Memory with Accumulator BIT<br />

Branch if Bit n is Clear BRCLR<br />

Branch if Bit n is Set BRSET<br />

Set Bit in Memory BSET<br />

Clear CLR<br />

Compare Accumulator with Memory CMP<br />

Complement COM<br />

Compare Index Register with Memory CPX<br />

Decrement DEC<br />

Exclusive OR Memory with Accumulator EOR<br />

Increment INC<br />

Jump JMP<br />

Jump to Subroutine JSR<br />

Load Accumulator from Memory LDA<br />

Load Index Register from Memory LDX<br />

Logical Shift Left LSL<br />

Logical Shift Right LSR<br />

Negate NEG<br />

Inclusive OR ORA<br />

Rotate Left thru Carry ROL<br />

Rotate Right thru Carry ROR<br />

Subtract with Carry SBC<br />

Store Accumulator in Memory STA<br />

Store Index Register in Memory STX<br />

Subtract SUB<br />

Test for Negative or Zero TST<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

111


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Indexed<br />

Addressing Modes<br />

Freescale Semiconductor, Inc.<br />

In the indexed addressing mode, the effective address is variable and<br />

depends upon two factors:<br />

1. The current contents of the index (X) register<br />

2. The offset contained in the byte(s) following the opcode<br />

Three types of indexed addressing exist in the MCU:<br />

• No offset<br />

• 8-bit offset<br />

• 16-bit offset<br />

A good assembler should use the indexed addressing mode that<br />

requires the least number of bytes to express the offset.<br />

Indexed, No Offset In the indexed, no-offset addressing mode, the effective address of the<br />

instruction is contained in the 8-bit index register. Thus, this addressing<br />

mode can access the first 256 memory locations. These instructions are<br />

only one byte.<br />

Example Program Listing:<br />

0300 f6 LDX ,x ;Load accumulator from location<br />

;pointed to by index reg (no offset)<br />

Execution Sequence:<br />

$0300 $F6 [1], [2], [3]<br />

Explanation:<br />

[1] CPU reads opcode $F6 <strong>—</strong> load accumulator using indexed,<br />

no offset, addressing mode.<br />

[2] CPU forms a complete address by adding $0000 to the<br />

contents of the index register.<br />

[3] CPU then reads the contents of the addressed location into<br />

the accumulator.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

112 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

The following is a list of all <strong>M68HC05</strong> instructions that can use the<br />

indexed, no-offset addressing mode or the indexed, 8-bit offset<br />

addressing mode.<br />

Instruction Mnemonic<br />

Add with Carry ADC<br />

Add ADD<br />

Logical AND AND<br />

Arithmetic Shift Left ASL<br />

Arithmetic Shift Right ASR<br />

Bit Test Memory with Accumulator BIT<br />

Clear CLR<br />

Compare Accumulator with Memory CMP<br />

Complement COM<br />

Compare Index Register with Memory CPX<br />

Decrement DEC<br />

Exclusive OR Memory with Accumulator EOR<br />

Increment INC<br />

Jump JMP<br />

Jump to Subroutine JSR<br />

Load Accumulator from Memory LDA<br />

Load Index Register from Memory LDX<br />

Logical Shift Left LSL<br />

Logical Shift Right LSR<br />

Negate NEG<br />

Inclusive OR ORA<br />

Rotate Left thru Carry ROL<br />

Rotate Right thru Carry ROR<br />

Subtract with Carry SBC<br />

Store Accumulator in Memory STA<br />

Store Index Register in Memory STX<br />

Subtract SUB<br />

Test for Negative or Zero TST<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

113


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Indexed, 8-Bit<br />

Offset<br />

Freescale Semiconductor, Inc.<br />

In the indexed, 8-bit offset addressing mode, the effective address is<br />

obtained by adding the contents of the byte following the opcode to the<br />

contents of the index register. This mode of addressing is useful for<br />

selecting the kth element in an n element table. To use this mode, the<br />

table must begin in the lowest 256 memory locations and may extend<br />

through the first 511 memory locations (IFE is the last location which the<br />

instruction may access). Indexed 8-bit offset addressing can be used for<br />

ROM, RAM, or I/O. This is a 2-byte instruction with the offset contained<br />

in the byte following the opcode. The content of the index register (X) is<br />

not changed. The offset byte supplied in the instruction is an unsigned<br />

8-bit integer.<br />

Example Program Listing:<br />

0300 e6 05 LDA $5,x ;Load accumulator from location<br />

;pointed to by index reg (X) + $05<br />

Execution Sequence:<br />

$0300 $E6 [1]<br />

$0301 $05 [2], [3], [4]<br />

Explanation:<br />

[1] CPU reads opcode $E6 <strong>—</strong> load accumulator using indexed,<br />

8-bit offset addressing mode.<br />

[2] CPU then reads $05 from location $0301. This $05 is<br />

interpreted as the low-order half of a base address. The<br />

high-order half of the base address is assumed to be $00.<br />

[3] CPU will add the value in the index register to the base<br />

address $0005. The results of this addition is the address<br />

that the CPU will use in the load accumulator operation.<br />

[4] The CPU will then read the value from this address and load<br />

this value into the accumulator.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

114 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

The list of all <strong>M68HC05</strong> instructions that can use the indexed, 8-bit offset<br />

addressing mode is the same as the list of instructions that use indexed,<br />

no-offset addressing mode.<br />

Instruction Mnemonic<br />

Add with Carry ADC<br />

Add ADD<br />

Logical AND AND<br />

Arithmetic Shift Left ASL<br />

Arithmetic Shift Right ASR<br />

Bit Test Memory with Accumulator BIT<br />

Clear CLR<br />

Compare Accumulator with Memory CMP<br />

Complement COM<br />

Compare Index Register with Memory CPX<br />

Decrement DEC<br />

Exclusive OR Memory with Accumulator EOR<br />

Increment INC<br />

Jump JMP<br />

Jump to Subroutine JSR<br />

Load Accumulator from Memory LIDA<br />

Load Index Register from Memory LDX<br />

Logical Shift Left LSL<br />

Logical Shift Right LSR<br />

Negate NEG<br />

Inclusive OR ORA<br />

Rotate Left thru Carry ROL<br />

Rotate Right thru Carry ROR<br />

Subtract with Carry SBC<br />

Store Accumulator in Memory STA<br />

Store Index Register in Memory STX<br />

Subtract SUB<br />

Test for Negative or Zero TST<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

115


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Indexed, 16-Bit<br />

Offset<br />

Freescale Semiconductor, Inc.<br />

In the indexed, 16-bit offset addressing mode, the effective address is<br />

the sum of the contents of the 8-bit index register and the two bytes<br />

following the opcode. The content of the index register is not changed.<br />

These instructions are three bytes, one for the opcode and two for a<br />

16-bit offset.<br />

Example Program Listing:<br />

0300 d6 07 00 LDA $0700,x ;Load accumulator from location<br />

;pointed to by index reg (X) + $0700<br />

Execution Sequence:<br />

$0300 $D6 [1]<br />

$0301 $07 [2]<br />

$0302 $00 [3], [4], [5]<br />

Explanation:<br />

[1] CPU reads opcode $D6 <strong>—</strong> load accumulator using indexed,<br />

16-bit offset addressing mode.<br />

[2] CPU then reads $07 from location $0301. This $07 is<br />

interpreted as the high-order half of a base address.<br />

[3] CPU then reads $00 from location $0302. This $00 is<br />

interpreted as the low-order half of a base address.<br />

[4] CPU will add the value in the index register to the base<br />

address $0700. The results of this addition is the address<br />

that the CPU will use in the load accumulator operation.<br />

[5] The CPU will then read the value from this address and load<br />

this value into the accumulator.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

116 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

The following is a list of all <strong>M68HC05</strong> instructions that can use the<br />

indexed, 16-bit offset addressing mode.<br />

Instruction Mnemonic<br />

Add with Carry ADC<br />

Add ADD<br />

Logical AND AND<br />

Bit Test Memory with Accumulator BIT<br />

Compare Accumulator with Memory CMP<br />

Compare Index Register with Memory CPX<br />

Exclusive OR Memory with Accumulator EOR<br />

Jump JMP<br />

Jump to Subroutine JSR<br />

Load Accumulator from Memory LDA<br />

Load Index Register from Memory LDX<br />

Inclusive OR ORA<br />

Subtract with Carry SBC<br />

Store Accumulator in Memory STA<br />

Store Index Register In Memory STX<br />

Subtract SUB<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

117


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Relative<br />

Addressing Mode<br />

Freescale Semiconductor, Inc.<br />

The relative addressing mode is used only for branch instructions.<br />

Branch instructions, other than the branching versions of<br />

bit-manipulation instructions, generate two machine-code bytes: one for<br />

the opcode and one for the relative offset. Because it is desirable to<br />

branch in either direction, the offset byte is a signed twos-complement<br />

offset with a range of –127 to +128 bytes (with respect to the address<br />

of the instruction immediately following the branch instruction). If the<br />

branch condition is true, the contents of the 8-bit signed byte following<br />

the opcode (offset) are added to the contents of the program counter to<br />

form the effective branch address; otherwise, control proceeds to the<br />

instruction immediately following the branch instruction.<br />

A programmer specifies the destination of a branch as an absolute<br />

address (or label which refers to an absolute address). The Motorola<br />

assembler calculates the 8-bit signed relative offset, which is placed<br />

after the branch opcode in memory.<br />

Example Program Listing:<br />

0300 27 rr BEQ DEST ;Branch to DEST if Z = 1<br />

;(branch if equal or zero)<br />

Execution Sequence:<br />

$0300 $27 [1]<br />

$0301 $rr [2], [3]<br />

Explanation:<br />

[1] CPU reads opcode $27 <strong>—</strong> branch if Z = 1, (relative<br />

addressing mode).<br />

[2] CPU reads the offset, $rr.<br />

[3] CPU internally tests the state of the Z bit and causes a<br />

branch if Z is set.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

118 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

The following is a list of all <strong>M68HC05</strong> instructions that can use the<br />

relative addressing mode.<br />

Instruction Mnemonic<br />

Branch if Carry Clear BCC<br />

Branch is Carry Set BCS<br />

Branch if Equal BEQ<br />

Branch if Half-Carry Clear BHCC<br />

Branch if Half-Carry Set BHCS<br />

Branch if Higher BHI<br />

Branch if Higher or Same BHS<br />

Branch if Interrupt Line is High BIH<br />

Branch if Interrupt Line is Low BIL<br />

Branch if Lower BLO<br />

Branch if Lower or Same BLS<br />

Branch if Interrupt Mask is Clear BMC<br />

Branch if Minus BMI<br />

Branch if Interrupt Mask Bit is Set BMS<br />

Branch if Not Equal BNE<br />

Branch if Plus BPL<br />

Branch Always BRA<br />

Branch if Bit n is Clear BRCLR<br />

Branch if Bit n is Set BRSET<br />

Branch Never BRN<br />

Branch to Subroutine BSR<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

119


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Bit Test and Branch<br />

Instructions<br />

Instructions<br />

Organized<br />

by Type<br />

Freescale Semiconductor, Inc.<br />

These instructions use direct addressing mode to specify the location<br />

being tested and relative addressing to specify the branch destination.<br />

This text book treats these instructions as direct addressing mode<br />

instructions. Some older Motorola documents call the addressing mode<br />

of these instructions BTB for bit test and branch.<br />

Table 11 through Table 14 show the MC68HC05 instruction set<br />

displayed by instruction type.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

120 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Table 11. Register/Memory Instructions<br />

Addressing Modes<br />

Indexed<br />

(16-Bit Offset)<br />

Indexed<br />

(8-Bit Offset)<br />

Indexed<br />

(No Offset)<br />

Immediate Direct Extended<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

Function Mnem.<br />

Freescale Semiconductor, Inc.<br />

Load A from Memory LDA A6 2 2 B6 2 3 C6 3 4 F6 1 3 E6 2 4 D6 3 5<br />

Load X from Memory LDX AE 2 2 BE 2 3 CE 3 4 FE 1 3 EE 2 4 DE 3 5<br />

Store A in Memory STA <strong>—</strong> <strong>—</strong> <strong>—</strong> B7 2 4 C7 3 5 F7 1 4 E7 2 5 D7 3 6<br />

Store X in Memory STX <strong>—</strong> <strong>—</strong> <strong>—</strong> BF 2 4 CF 3 5 FF 1 4 EF 2 5 DF 3 6<br />

Add Memory to A ADD AB 2 BB 2 3 CB 3 4 FB 1 3 EB 2 4 DB 3 5<br />

Add Memory and<br />

ADC A9 2 2 B9 2 3 C9 3 4 F9 1 3 E9 2 4 D9 3 5<br />

Carry to A<br />

Subtract Memory SUB A0 2 2 B0 2 3 C0 3 4 F0 1 3 E0 2 4 D0 3 5<br />

Subtract Memory from<br />

SBC A2 2 2 B2 2 3 C2 3 4 F2 1 3 E2 2 4 D2 3 5<br />

A with Borrow<br />

AND Memory to A AND A4 2 2 B4 2 3 C4 3 4 F4 1 3 E4 2 4 D4 3 5<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

121<br />

OR Memory with A ORA AA 2 2 BA 2 3 CA 3 4 FA 1 3 EA 2 4 DA 3 5<br />

EOR A8 2 2 B8 2 3 C8 3 4 F8 1 3 E8 2 4 D8 3 5<br />

Exclusive OR Memory<br />

with A<br />

CMP A1 2 2 E11 2 3 C1 3 4 F1 1 3 E1 2 4 D1 3 5<br />

Arithmetic Compare A<br />

with Memory<br />

CPX A3 2 2 B3 2 3 C3 3 4 F3 1 3 E3 2 4 D3 3 5<br />

Arithmetic Compare X<br />

with Memory<br />

Bit Test Memory with<br />

BIT A5 2 2 B5 2 3 C5 3 4 F5 1 3 E 2 4 D5 3 5<br />

A (Logical Compare)<br />

Jump Unconditional JMP <strong>—</strong> <strong>—</strong> <strong>—</strong> BC 2 2 CC 3 3 FC 1 2 EC 2 3 DC 3 4<br />

Jump to Subroutine JSR <strong>—</strong> <strong>—</strong> <strong>—</strong> BD 2 5 CD 3 6 FD 1 5 ED 2 6 DD 3 7


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction<br />

Table 12. Read/Modify-Write Instructions<br />

Addressing Modes<br />

Indexed<br />

(8-Bit Offset)<br />

Indexed<br />

(No Offset)<br />

Inherent (A) Inherent (X) Direct<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

#<br />

Cycles<br />

#<br />

Bytes<br />

Opcode<br />

Function Mnem.<br />

Freescale Semiconductor, Inc.<br />

Increment INC 4C 1 3 5C 1 3 3C 2 5 7C 1 5 6C 2 6<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Decrement DEC 4A 1 3 5A 1 3 3A 2 5 7A 1 5 6A 2 6<br />

Clear CLR 4F 1 3 5F 1 3 3F 2 5 7F 1 5 6F 2 6<br />

Complement COM 43 1 3 53 1 3 33 2 5 73 1 5 63 2 6<br />

NEG 40 1 3 50 1 3 30 2 5 70 1 5 60 2 6<br />

Negate<br />

Twos Complement<br />

Rotate Left Thru Carry ROL 49 1 3 59 1 3 39 2 5 79 1 5 69 2 6<br />

Rotate Right Thru Carry ROR 46 1 3 56 1 3 36 2 5 76 1 5 66 2 6<br />

122 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

Logical Shift Left LSL 48 1 3 58 1 3 38 2 5 78 1 5 68 2 6<br />

Logical Shift Right LSR 44 1 3 54 1 3 34 2 5 74 1 5 64 2 6<br />

Arithmetic Shift Right ASH 47 1 3 57 1 3 37 2 5 77 1 5 67 2<br />

TST 4D 1 3 5D 1 3 3D 2 4 7D 1 4 6D 2 5<br />

Test for Negative<br />

or Zero<br />

Multiply MUL 42 1 11 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

2 5 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

See<br />

Note<br />

Bit Clear BCLR <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

2 5 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

See<br />

Note<br />

Bit Set BSET <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

NOTE: Unlike other read-modify-write instructions, BCLR and BSET use only direct addressing.


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Table 13. Branch Instructions<br />

Function Mnemonic<br />

<strong>M68HC05</strong> Instruction Set<br />

Addressing Modes<br />

Relative Addressing<br />

Mode<br />

Opcode<br />

#<br />

Bytes<br />

#<br />

Cycles<br />

Branch Always BRA 20 2 3<br />

Branch Never BRN 21 2 3<br />

Branch if Higher BH1 22 2 3<br />

Branch if Lower or Same BLS 23 2 3<br />

Branch if Carry Clear BCC 24 2 3<br />

Branch if Higher or Same<br />

(Same as BCC)<br />

BHS 24 2 3<br />

Branch if Carry Set BCS 25 2 3<br />

Branch if Lower<br />

(Same as BCS)<br />

BLO 25 2 3<br />

Branch if Not Equal BNE 26 2 3<br />

Branch if Equal BEQ 27 2 3<br />

Branch if Half-Carry Clear BHCC 28 2 3<br />

Branch if Half-Carry Set BHCS 29 2 3<br />

Branch if Plus BPL 2A 2 3<br />

Branch if Minus BMI 2B 2 3<br />

Branch if Interrupt Mask Bit is Clear BMC 2C 2 3<br />

Branch if Interrupt Mask Bit is Set BMS 2D 2 3<br />

Branch if Interrupt Line is Low BIL 2E 2 3<br />

Branch if Interrupt Line is High BIH 2F 2 3<br />

Branch to Subroutine BSR AD 2 6<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

123


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Table 14. Control Instructions<br />

Function Mnemonic<br />

Relative Addressing<br />

Mode<br />

Opcode<br />

#<br />

Bytes<br />

#<br />

Cycles<br />

Transfer A to X TAX 97 1 2<br />

Transfer X to A TXA 9F 1 2<br />

Set Carry Bit SEC 99 1 2<br />

Clear Carry Bit CLC 98 1 2<br />

Set Interrupt Mask Bit SEI 9B 1 2<br />

Clear Interrupt Mask Bit CLI 9A 1 2<br />

Software Interrupt SWI 83 1 10<br />

Return from Subroutine RTS 81 1 6<br />

Return from Interrupt RTI 80 1 9<br />

Reset Stack Pointer RSP 9C 1 2<br />

No-Operation NOP 9D 1 2<br />

Stop STOP 8E 1 2<br />

Wait WAIT 8F 1 2<br />

124 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Summary<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Instruction Set Summary<br />

Computers use an operation code or opcode to give instructions to the<br />

CPU. The instruction set for a specific CPU is the set of all opcodes that<br />

the CPU knows how to execute. For example, the CPU in the<br />

MC68HC705J1A MCU can understand 62 basic instructions, some of<br />

which have several variations that require separate opcodes. The<br />

<strong>M68HC05</strong> <strong>Family</strong> instruction set includes 210 unique instruction<br />

opcodes.<br />

The following is an alphabetical listing of the <strong>M68HC05</strong> <strong>Family</strong><br />

instructions available to the user. In listing all the factors necessary to<br />

program, the table uses these symbols:<br />

Condition Code Symbols<br />

H <strong>—</strong> Half Carry (Bit 4) ↕ <strong>—</strong> Test and Set if True,<br />

I <strong>—</strong> Interrupt Mask (Bit 3) Cleared Otherwise<br />

N <strong>—</strong> Negate (Sign Bit 2) <strong>—</strong> <strong>—</strong> Not Affected<br />

Z <strong>—</strong> Zero (Bit 1) 0 <strong>—</strong> Cleared<br />

C <strong>—</strong> Carry/Borrow (Bit 0) 1 <strong>—</strong> Set<br />

Boolean Operators<br />

( ) <strong>—</strong> Contents of (For Example, (M) + <strong>—</strong> Inclusive OR<br />

Means the Contents ⊕ <strong>—</strong> Exclusive OR<br />

of Memory Location M <strong>—</strong> <strong>—</strong> NOT<br />

– <strong>—</strong> Negation,<br />

← <strong>—</strong> is loaded with, gets Twos Complement<br />

• <strong>—</strong> Logical AND x <strong>—</strong> Multiplication<br />

CPU Registers<br />

A <strong>—</strong> Accumulator PC <strong>—</strong> Program Counter<br />

ACCA <strong>—</strong> Accumulator PCH <strong>—</strong> PC High Byte<br />

CCR <strong>—</strong> Condition Code Register PCL <strong>—</strong> PC Low Byte<br />

X <strong>—</strong> Index Register SP <strong>—</strong> Stack Pointer<br />

M <strong>—</strong> Any memory location REL <strong>—</strong> Relative Address, One Byte<br />

Addressing Modes Abbreviation Operands<br />

Inherent INH none<br />

Immediate IMM ii<br />

Direct (For Bit DIR dd<br />

Test Instructions) dd rr<br />

Extended EXT hh ll<br />

Indexed 0 Offset IX none<br />

Indexed 1-Byte Offset IX1 ff<br />

Indexed 2-Byte Offset IX2 ee ff<br />

Relative REL rr<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

125


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Source<br />

Form<br />

ADC #opr<br />

ADC opr<br />

ADC opr<br />

ADC opr,X<br />

ADC opr,X<br />

ADC ,X<br />

ADD #opr<br />

ADD opr<br />

ADD opr<br />

ADD opr,X<br />

ADD opr,X<br />

ADD ,X<br />

AND #opr<br />

AND opr<br />

AND opr<br />

AND opr,X<br />

AND opr,X<br />

AND ,X<br />

ASL opr<br />

ASLA<br />

ASLX<br />

ASL opr,X<br />

ASL ,X<br />

ASR opr<br />

ASRA<br />

ASRX<br />

ASR opr,X<br />

ASR ,X<br />

Freescale Semiconductor, Inc.<br />

Table 15. Instruction Set Summary (Sheet 1 of 6)<br />

Operation Description<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Effect on<br />

CCR<br />

H I N Z C<br />

Add with Carry A ← (A) + (M) + (C) ↕ <strong>—</strong> ↕ ↕ ↕<br />

Add without Carry A ← (A) + (M) ↕ <strong>—</strong> ↕ ↕ ↕<br />

Logical AND A ← (A) ∧ (M) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Arithmetic Shift Left (Same as LSL) C<br />

0 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Arithmetic Shift Right<br />

C<br />

<strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

b7<br />

b0<br />

BCC rel Branch if Carry Bit Clear PC ← (PC) + 2 + rel ? C = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 24 rr 3<br />

BCLR n opr Clear Bit n Mn ← 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><br />

126 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

Address<br />

Mode<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

DIR (b0)<br />

DIR (b1)<br />

DIR (b2)<br />

DIR (b3)<br />

DIR (b4)<br />

DIR (b5)<br />

DIR (b6)<br />

DIR (b7)<br />

BCS rel Branch if Carry Bit Set (Same as BLO) PC ← (PC) + 2 + rel ? C = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 25 rr 3<br />

BEQ rel Branch if Equal PC ← (PC) + 2 + rel ? Z = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 27 rr 3<br />

BHCC rel Branch if Half-Carry Bit Clear PC ← (PC) + 2 + rel ? H = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 28 rr 3<br />

BHCS rel Branch if Half-Carry Bit Set PC ← (PC) + 2 + rel ? H = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 29 rr 3<br />

BHI rel Branch if Higher PC ← (PC) + 2 + rel ? C ∨ Z = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 22 rr 3<br />

BHS rel Branch if Higher or Same PC ← (PC) + 2 + rel ? C = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 24 rr 3<br />

BIH rel Branch if IRQ Pin High PC ← (PC) + 2 + rel ? IRQ = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 2F rr 3<br />

BIL rel Branch if IRQ Pin Low PC ← (PC) + 2 + rel ? IRQ = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 2E rr 3<br />

b7<br />

b0<br />

Opcode<br />

A9<br />

B9<br />

C9<br />

D9<br />

E9<br />

F9<br />

AB<br />

BB<br />

CB<br />

DB<br />

EB<br />

FB<br />

A4<br />

B4<br />

C4<br />

D4<br />

E4<br />

F4<br />

38<br />

48<br />

58<br />

68<br />

78<br />

37<br />

47<br />

57<br />

67<br />

77<br />

11<br />

13<br />

15<br />

17<br />

19<br />

1B<br />

1D<br />

1F<br />

Operand<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

ff<br />

dd<br />

ff<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

Cycles<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

5<br />

3<br />

3<br />

6<br />

5<br />

5<br />

3<br />

3<br />

6<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5


Freescale Semiconductor, Inc...<br />

Source<br />

Form<br />

BIT #opr<br />

BIT opr<br />

BIT opr<br />

BIT opr,X<br />

BIT opr,X<br />

BIT ,X<br />

Bit Test Accumulator with Memory Byte (A) ∧ (M) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

<strong>M68HC05</strong> Instruction Set<br />

Instruction Set Summary<br />

BLO rel Branch if Lower (Same as BCS) PC ← (PC) + 2 + rel ? C = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 25 rr 3<br />

BLS rel Branch if Lower or Same PC ← (PC) + 2 + rel ? C ∨ Z = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 23 rr 3<br />

BMC rel Branch if Interrupt Mask Clear PC ← (PC) + 2 + rel ? I = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 2C rr 3<br />

BMI rel Branch if Minus PC ← (PC) + 2 + rel ? N = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 2B rr 3<br />

BMS rel Branch if Interrupt Mask Set PC ← (PC) + 2 + rel ? I = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 2D rr 3<br />

BNE rel Branch if Not Equal PC ← (PC) + 2 + rel ? Z = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 26 rr 3<br />

BPL rel Branch if Plus PC ← (PC) + 2 + rel ? N = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 2A rr 3<br />

BRA rel Branch Always PC ← (PC) + 2 + rel ? 1 = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 20 rr 3<br />

BRCLR n opr rel Branch if Bit n Clear PC ← (PC) + 2 + rel ? Mn = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> ↕<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

127<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR (b0)<br />

DIR (b1)<br />

DIR (b2)<br />

DIR (b3)<br />

DIR (b4)<br />

DIR (b5)<br />

DIR (b6)<br />

DIR (b7)<br />

BRN rel Branch Never PC ← (PC) + 2 + rel ? 1 = 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL 21 rr 3<br />

BRSET n opr rel Branch if Bit n Set PC ← (PC) + 2 + rel ? Mn = 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> ↕<br />

BSET n opr Set Bit n Mn ← 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><br />

BSR rel Branch to Subroutine<br />

Freescale Semiconductor, Inc.<br />

Table 15. Instruction Set Summary (Sheet 2 of 6)<br />

Operation Description<br />

PC ← (PC) + 2; push (PCL)<br />

SP ← (SP) – 1; push (PCH)<br />

SP ← (SP) – 1<br />

PC ← (PC) + rel<br />

Effect on<br />

CCR<br />

H I N Z C<br />

DIR (b0)<br />

DIR (b1)<br />

DIR (b2)<br />

DIR (b3)<br />

DIR (b4)<br />

DIR (b5)<br />

DIR (b6)<br />

DIR (b7)<br />

DIR (b0)<br />

DIR (b1)<br />

DIR (b2)<br />

DIR (b3)<br />

DIR (b4)<br />

DIR (b5)<br />

DIR (b6)<br />

DIR (b7)<br />

A5<br />

B5<br />

C5<br />

D5<br />

E5<br />

F5<br />

01<br />

03<br />

05<br />

07<br />

09<br />

0B<br />

0D<br />

0F<br />

00<br />

02<br />

04<br />

06<br />

08<br />

0A<br />

0C<br />

0E<br />

10<br />

12<br />

14<br />

16<br />

18<br />

1A<br />

1C<br />

1E<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd rr<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

dd<br />

<strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> REL AD rr 6<br />

CLC Clear Carry Bit C ← 0 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> 0 INH 98 2<br />

CLI Clear Interrupt Mask I ← 0 <strong>—</strong> 0 <strong>—</strong> <strong>—</strong> <strong>—</strong> INH 9A 2<br />

Address<br />

Mode<br />

Opcode<br />

Operand<br />

Cycles<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5<br />

5


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Source<br />

Form<br />

CLR opr<br />

CLRA<br />

CLRX<br />

CLR opr,X<br />

CLR ,X<br />

CMP #opr<br />

CMP opr<br />

CMP opr<br />

CMP opr,X<br />

CMP opr,X<br />

CMP ,X<br />

COM opr<br />

COMA<br />

COMX<br />

COM opr,X<br />

COM ,X<br />

CPX #opr<br />

CPX opr<br />

CPX opr<br />

CPX opr,X<br />

CPX opr,X<br />

CPX ,X<br />

DEC opr<br />

DECA<br />

DECX<br />

DEC opr,X<br />

DEC ,X<br />

EOR #opr<br />

EOR opr<br />

EOR opr<br />

EOR opr,X<br />

EOR opr,X<br />

EOR ,X<br />

INC opr<br />

INCA<br />

INCX<br />

INC opr,X<br />

INC ,X<br />

JMP opr<br />

JMP opr<br />

JMP opr,X<br />

JMP opr,X<br />

JMP ,X<br />

Clear Byte<br />

M ← $00<br />

A ← $00<br />

X ← $00<br />

M ← $00<br />

M ← $00<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

<strong>—</strong><strong>—</strong> 0 1 <strong>—</strong><br />

Compare Accumulator with Memory Byte (A) – (M) <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Complement Byte (One’s Complement)<br />

M ← (M) = $FF – (M)<br />

A ← (A) = $FF – (A)<br />

X ← (X) = $FF – (X)<br />

M ← (M) = $FF – (M)<br />

M ← (M) = $FF – (M)<br />

<strong>—</strong><strong>—</strong> ↕ ↕ 1<br />

Compare Index Register with Memory Byte (X) – (M) <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Decrement Byte<br />

M ← (M) – 1<br />

A ← (A) – 1<br />

X ← (X) – 1<br />

M ← (M) – 1<br />

M ← (M) – 1<br />

<strong>—</strong><strong>—</strong> ↕ ↕ <strong>—</strong><br />

EXCLUSIVE OR Accumulator with Memory Byte A ← (A) ⊕ (M) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Increment Byte<br />

Freescale Semiconductor, Inc.<br />

Table 15. Instruction Set Summary (Sheet 3 of 6)<br />

Operation Description<br />

M ← (M) + 1<br />

A ← (A) + 1<br />

X ← (X) + 1<br />

M ← (M) + 1<br />

M ← (M) + 1<br />

Effect on<br />

CCR<br />

H I N Z C<br />

<strong>—</strong><strong>—</strong> ↕ ↕ <strong>—</strong><br />

Unconditional Jump PC ← Jump Address <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><br />

128 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

Address<br />

Mode<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

Opcode<br />

3F<br />

4F<br />

5F<br />

6F<br />

7F<br />

A1<br />

B1<br />

C1<br />

D1<br />

E1<br />

F1<br />

33<br />

43<br />

53<br />

63<br />

73<br />

A3<br />

B3<br />

C3<br />

D3<br />

E3<br />

F3<br />

3A<br />

4A<br />

5A<br />

6A<br />

7A<br />

A8<br />

B8<br />

C8<br />

D8<br />

E8<br />

F8<br />

3C<br />

4C<br />

5C<br />

6C<br />

7C<br />

BC<br />

CC<br />

DC<br />

EC<br />

FC<br />

Operand<br />

dd<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

ff<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

Cycles<br />

5<br />

3<br />

3<br />

6<br />

5<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

5<br />

3<br />

3<br />

6<br />

5<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

5<br />

3<br />

3<br />

6<br />

5<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

5<br />

3<br />

3<br />

6<br />

5<br />

2<br />

3<br />

4<br />

3<br />

2


Freescale Semiconductor, Inc...<br />

Source<br />

Form<br />

JSR opr<br />

JSR opr<br />

JSR opr,X<br />

JSR opr,X<br />

JSR ,X<br />

LDA #opr<br />

LDA opr<br />

LDA opr<br />

LDA opr,X<br />

LDA opr,X<br />

LDA ,X<br />

LDX #opr<br />

LDX opr<br />

LDX opr<br />

LDX opr,X<br />

LDX opr,X<br />

LDX ,X<br />

LSL opr<br />

LSLA<br />

LSLX<br />

LSL opr,X<br />

LSL ,X<br />

LSR opr<br />

LSRA<br />

LSRX<br />

LSR opr,X<br />

LSR ,X<br />

Jump to Subroutine<br />

PC ← (PC) + n (n = 1, 2, or 3)<br />

Push (PCL); SP ← (SP) – 1<br />

Push (PCH); SP ← (SP) – 1<br />

PC ← Effective Address<br />

<strong>M68HC05</strong> Instruction Set<br />

Instruction Set Summary<br />

<strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><br />

Load Accumulator with Memory Byte A ← (M) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Load Index Register with Memory Byte X ← (M) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Logical Shift Left (Same as ASL) C<br />

0 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

b7<br />

b0<br />

Logical Shift Right 0<br />

C <strong>—</strong> <strong>—</strong> 0 ↕ ↕<br />

b7<br />

b0<br />

MUL Unsigned Multiply X : A ← (X) × (A) 0 <strong>—</strong> <strong>—</strong> <strong>—</strong> 0 INH 42 11<br />

NEG opr<br />

NEGA<br />

NEGX<br />

NEG opr,X<br />

NEG ,X<br />

Negate Byte (Two’s Complement)<br />

M ← –(M) = $00 – (M)<br />

A ← –(A) = $00 – (A)<br />

X ← –(X) = $00 – (X)<br />

M ← –(M) = $00 – (M)<br />

M ← –(M) = $00 – (M)<br />

<strong>—</strong><strong>—</strong> ↕ ↕ ↕<br />

NOP No Operation <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> INH 9D 2<br />

ORA #opr<br />

ORA opr<br />

ORA opr<br />

ORA opr,X<br />

ORA opr,X<br />

ORA ,X<br />

ROL opr<br />

ROLA<br />

ROLX<br />

ROL opr,X<br />

ROL ,X<br />

Freescale Semiconductor, Inc.<br />

Table 15. Instruction Set Summary (Sheet 4 of 6)<br />

Operation Description<br />

Effect on<br />

CCR<br />

H I N Z C<br />

Logical OR Accumulator with Memory A ← (A) ∨ (M) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Rotate Byte Left through Carry Bit C<br />

<strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

b7<br />

b0<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

129<br />

Address<br />

Mode<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

Opcode<br />

BD<br />

CD<br />

DD<br />

ED<br />

FD<br />

A6<br />

B6<br />

C6<br />

D6<br />

E6<br />

F6<br />

AE<br />

BE<br />

CE<br />

DE<br />

EE<br />

FE<br />

38<br />

48<br />

58<br />

68<br />

78<br />

34<br />

44<br />

54<br />

64<br />

74<br />

30<br />

40<br />

50<br />

60<br />

70<br />

AA<br />

BA<br />

CA<br />

DA<br />

EA<br />

FA<br />

39<br />

49<br />

59<br />

69<br />

79<br />

Operand<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

ff<br />

dd<br />

ff<br />

dd<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

ff<br />

Cycles<br />

5<br />

6<br />

7<br />

6<br />

5<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

5<br />

3<br />

3<br />

6<br />

5<br />

5<br />

3<br />

3<br />

6<br />

5<br />

5<br />

3<br />

3<br />

6<br />

5<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

5<br />

3<br />

3<br />

6<br />

5


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Source<br />

Form<br />

ROR opr<br />

RORA<br />

RORX<br />

ROR opr,X<br />

ROR ,X<br />

Rotate Byte Right through Carry Bit C <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

b7<br />

b0<br />

RSP Reset Stack Pointer SP ← $00FF <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> INH 9C 2<br />

RTI Return from Interrupt<br />

RTS Return from Subroutine<br />

SBC #opr<br />

SBC opr<br />

SBC opr<br />

SBC opr,X<br />

SBC opr,X<br />

SBC ,X<br />

Subtract Memory Byte and Carry Bit from<br />

Accumulator<br />

SP ← (SP) + 1; Pull (CCR)<br />

SP ← (SP) + 1; Pull (A)<br />

SP ← (SP) + 1; Pull (X)<br />

SP ← (SP) + 1; Pull (PCH)<br />

SP ← (SP) + 1; Pull (PCL)<br />

SP ← (SP) + 1; Pull (PCH)<br />

SP ← (SP) + 1; Pull (PCL)<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

A ← (A) – (M) – (C) <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

130 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

36<br />

46<br />

56<br />

66<br />

76<br />

dd<br />

↕ ↕ ↕ ↕ ↕ INH 80 9<br />

<strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> INH 81 6<br />

SEC Set Carry Bit C ← 1 <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> 1 INH 99 2<br />

SEI Set Interrupt Mask I ← 1 <strong>—</strong> 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> INH 9B 2<br />

STA opr<br />

STA opr<br />

STA opr,X<br />

STA opr,X<br />

STA ,X<br />

Store Accumulator in Memory M ← (A) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

STOP Stop Oscillator and Enable IRQ Pin <strong>—</strong> 0 <strong>—</strong> <strong>—</strong> <strong>—</strong> INH 8E 2<br />

STX opr<br />

STX opr<br />

STX opr,X<br />

STX opr,X<br />

STX ,X<br />

SUB #opr<br />

SUB opr<br />

SUB opr<br />

SUB opr,X<br />

SUB opr,X<br />

SUB ,X<br />

Store Index Register In Memory M ← (X) <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Subtract Memory Byte from Accumulator A ← (A) – (M) <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

SWI Software Interrupt<br />

Freescale Semiconductor, Inc.<br />

Table 15. Instruction Set Summary (Sheet 5 of 6)<br />

Operation Description<br />

Effect on<br />

CCR<br />

H I N Z C<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

IMM<br />

DIR<br />

EXT<br />

IX2<br />

IX1<br />

IX<br />

A2<br />

B2<br />

C2<br />

D2<br />

E2<br />

F2<br />

B7<br />

C7<br />

D7<br />

E7<br />

F7<br />

BF<br />

CF<br />

DF<br />

EF<br />

FF<br />

A0<br />

B0<br />

C0<br />

D0<br />

E0<br />

F0<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

ii<br />

dd<br />

hh ll<br />

ee ff<br />

ff<br />

PC ← (PC) + 1; Push (PCL)<br />

SP ← (SP) – 1; Push (PCH)<br />

SP ← (SP) – 1; Push (X)<br />

SP ← (SP) – 1; Push (A)<br />

SP ← (SP) – 1; Push (CCR)<br />

SP ← (SP) – 1; I ← 1<br />

PCH ← Interrupt Vector High Byte<br />

PCL ← Interrupt Vector Low Byte<br />

<strong>—</strong> 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> INH 83 10<br />

TAX Transfer Accumulator to Index Register X ← (A) <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> INH 97 2<br />

Address<br />

Mode<br />

Opcode<br />

Operand<br />

Cycles<br />

5<br />

3<br />

3<br />

6<br />

5<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3<br />

4<br />

5<br />

6<br />

5<br />

4<br />

4<br />

5<br />

6<br />

5<br />

4<br />

2<br />

3<br />

4<br />

5<br />

4<br />

3


Freescale Semiconductor, Inc...<br />

Source<br />

Form<br />

TST opr<br />

TSTA<br />

TSTX<br />

TST opr,X<br />

TST ,X<br />

Freescale Semiconductor, Inc.<br />

Table 15. Instruction Set Summary (Sheet 6 of 6)<br />

Operation Description<br />

Test Memory Byte for Negative or Zero (M) – $00 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

<strong>M68HC05</strong> Instruction Set<br />

Instruction Set Summary<br />

Effect on<br />

CCR<br />

H I N Z C<br />

TXA Transfer Index Register to Accumulator A ← (X) <strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong><strong>—</strong> INH 9F 2<br />

WAIT Stop CPU Clock and Enable Interrupts <strong>—</strong> 0 <strong>—</strong> <strong>—</strong> <strong>—</strong> INH 8F 2<br />

A Accumulator opr Operand (one or two bytes)<br />

C Carry/borrow flag PC Program counter<br />

CCR Condition code register PCH Program counter high byte<br />

dd Direct address of operand PCL Program counter low byte<br />

dd rr Direct address of operand and relative offset of branch instruction REL Relative addressing mode<br />

DIR Direct addressing mode rel Relative program counter offset byte<br />

ee ff High and low bytes of offset in indexed, 16-bit offset addressing rr Relative program counter offset byte<br />

EXT Extended addressing mode SP Stack pointer<br />

ff Offset byte in indexed, 8-bit offset addressing X Index register<br />

H Half-carry flag Z Zero flag<br />

hh ll High and low bytes of operand address in extended addressing # Immediate value<br />

I Interrupt mask • Logical AND<br />

ii Immediate operand byte + Logical OR<br />

IMM Immediate addressing mode ⊕ Logical EXCLUSIVE OR<br />

INH Inherent addressing mode ( ) Contents of<br />

IX Indexed, no offset addressing mode –( ) Negation (twos complement)<br />

IX1 Indexed, 8-bit offset addressing mode ← Loaded with<br />

IX2 Indexed, 16-bit offset addressing mode ? If<br />

M Memory location : Concatenated with<br />

N Negative flag ↕ Set or cleared<br />

n Any bit <strong>—</strong> Not affected<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

131<br />

Address<br />

Mode<br />

DIR<br />

INH<br />

INH<br />

IX1<br />

IX<br />

Opcode<br />

3D<br />

4D<br />

5D<br />

6D<br />

7D<br />

Operand<br />

dd<br />

ff<br />

Cycles<br />

4<br />

3<br />

3<br />

5<br />

4


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction<br />

Table 16. <strong>M68HC05</strong> Instruction Set Opcode Map<br />

LSB<br />

MSB<br />

0<br />

1<br />

2<br />

Freescale Semiconductor, Inc.<br />

3<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

4<br />

5<br />

6<br />

7<br />

Bit Manipulation Branch Read-Modify-Write Control Register/Memory<br />

DIR DIR REL DIR INH INH IX1 IX INH INH IMM DIR EXT IX2 IX1 IX<br />

MSB<br />

0 1 2 3 4 5 6 7 8 9 A B C D E F<br />

LSB<br />

5 5 3 5 3 3 6 5 9<br />

2 3 4 5 4 3<br />

0 BRSET0 BSET0 BRA NEG NEGA NEGX NEG NEG RTI<br />

SUB SUB SUB SUB SUB SUB<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX 1 INH<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3<br />

6<br />

2 3 4 5 4 3<br />

1 BRCLR0 BCLR0 BRN<br />

RTS<br />

CMP CMP CMP CMP CMP CMP<br />

3 DIR 2 DIR 2 REL<br />

1 INH<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3<br />

11<br />

2 3 4 5 4 3<br />

2 BRSET1 BSET1 BHI<br />

MUL<br />

SBC SBC SBC SBC SBC SBC<br />

3 DIR 2 DIR 2 REL<br />

1 INH<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5 10<br />

2 3 4 5 4 3<br />

3 BRCLR1 BCLR1 BLS COM COMA COMX COM COM SWI<br />

CPX CPX CPX CPX CPX CPX<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX 1 INH<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5<br />

2 3 4 5 4 3<br />

4 BRSET2 BSET2 BCC LSR LSRA LSRX LSR LSR<br />

AND AND AND AND AND AND<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3<br />

2 3 4 5 4 3<br />

5 BRCLR2 BCLR2 BCS/BLO<br />

BIT BIT BIT BIT BIT BIT<br />

3 DIR 2 DIR 2 REL<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5<br />

2 3 4 5 4 3<br />

6 BRSET3 BSET3 BNE ROR RORA RORX ROR ROR<br />

LDA LDA LDA LDA LDA LDA<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5<br />

2<br />

4 5 6 5 4<br />

7 BRCLR3 BCLR3 BEQ ASR ASRA ASRX ASR ASR<br />

TAX<br />

STA STA STA STA STA<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

1 INH<br />

2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5<br />

2 2 3 4 5 4 3<br />

8 BRSET4 BSET4 BHCC ASL/LSL ASLA/LSLA ASLX/LSLX ASL/LSL ASL/LSL<br />

CLC EOR EOR EOR EOR EOR EOR<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

1 INH 2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5<br />

2 2 3 4 5 4 3<br />

9 BRCLR4 BCLR4 BHCS ROL ROLA ROLX ROL ROL<br />

SEC ADC ADC ADC ADC ADC ADC<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

1 INH 2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5<br />

2 2 3 4 5 4 3<br />

A BRSET5 BSET5 BPL DEC DECA DECX DEC DEC<br />

CLI ORA ORA ORA ORA ORA ORA<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

1 INH 2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3<br />

2 2 3 4 5 4 3<br />

B BRCLR5 BCLR5 BMI<br />

SEI ADD ADD ADD ADD ADD ADD<br />

3 DIR 2 DIR 2 REL<br />

1 INH 2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5<br />

2<br />

2 3 4 3 2<br />

C BRSET6 BSET6 BMC INC INCA INCX INC INC<br />

RSP<br />

JMP JMP JMP JMP JMP<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

1 INH<br />

2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 4 3 3 5 4<br />

2 6 5 6 7 6 5<br />

D BRCLR6 BCLR6 BMS TST TSTA TSTX TST TST<br />

NOP BSR JSR JSR JSR JSR JSR<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX<br />

1 INH 2 REL 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3<br />

2<br />

2 3 4 5 4 3<br />

E BRSET7 BSET7 BIL<br />

STOP<br />

LDX LDX LDX LDX LDX LDX<br />

3 DIR 2 DIR 2 REL<br />

1 INH<br />

2 IMM 2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

5 5 3 5 3 3 6 5 2 2<br />

4 5 6 5 4<br />

F BRCLR7 BCLR7 BIH CLR CLRA CLRX CLR CLR WAIT TXA<br />

STX STX STX STX STX<br />

3 DIR 2 DIR 2 REL 2 DIR 1 INH 1 INH 2 IX1 1 IX 1 INH 1 INH<br />

2 DIR 3 EXT 3 IX2 2 IX1 1 IX<br />

INH = Inherent REL = Relative<br />

MSB<br />

0 MSB of Opcode in Hexadecimal<br />

IMM = Immediate IX = Indexed, No Offset<br />

LSB<br />

DIR = Direct IX1 = Indexed, 8-Bit Offset<br />

5 Number of Cycles<br />

LSB of Opcode in Hexadecimal 0 BRSET0 Opcode Mnemonic<br />

EXT = Extended IX2 = Indexed, 16-Bit Offset<br />

3 DIR Number of Bytes/Addressing Mode<br />

8<br />

132 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

9<br />

A<br />

B<br />

C<br />

D<br />

E<br />

F


Freescale Semiconductor, Inc...<br />

Review<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> Instruction Set<br />

Review<br />

CPU Registers The five CPU registers in the <strong>M68HC05</strong> MCUs are not locations in the<br />

memory map. The programming model for the CPU shows the five<br />

CPU registers.<br />

• The accumulator (A) is an 8-bit general-purpose register.<br />

• The index register (X) is an 8-bit pointer register.<br />

• The stack pointer (SP) is a pointer register that is decremented<br />

automatically as data is pushed onto the stack and incremented<br />

as data is pulled off of the stack.<br />

• The program counter (PC) has as many bits as there are<br />

address lines. The program counter always points at the next<br />

instruction or piece of data the CPU will use.<br />

• The condition code register (CCR) contains the four arithmetic<br />

result flags H, N, Z, and C and the interrupt mask (disable) control<br />

bit I.<br />

Addressing Modes The <strong>M68HC05</strong> CPU has six addressing modes that determine how the<br />

CPU will get the operand(s) needed to complete each instruction. The<br />

<strong>M68HC05</strong> CPU has only 62 mnemonic instructions. There are 210<br />

instruction opcodes because each different addressing mode variation<br />

of an instruction must have a unique opcode.<br />

• Inimmediate addressing mode, the operand for the instruction is<br />

the byte immediately after the opcode.<br />

• In inherent addressing mode, the CPU needs no operands from<br />

memory. The operands, if any, are the registers or stacked data<br />

values.<br />

• In extended addressing mode, the 16-bit address of the operand<br />

is located in the next two memory bytes after the instruction<br />

opcode.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

133


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Instruction<br />

Execution<br />

Freescale Semiconductor, Inc.<br />

• Indirect addressing mode, the low order eight bits of the address<br />

of the operand are located in the next byte of memory after the<br />

opcode and the high order byte of the address is assumed to be<br />

$00. This mode is more efficient than the extended addressing<br />

mode because the high order address byte is not explicitly<br />

included in the program.<br />

• In indexed addressing mode, the current value of the index<br />

register is added to a 0-, 1-, or 2-byte offset in the next 0, 1, or 2<br />

memory locations after the opcode to form a pointer to the address<br />

of the operand in memory.<br />

• Relative addressing mode is used for conditional branch<br />

instructions. The byte after the opcode is a signed offset value<br />

between –128 and +127. If the condition of the branch is true, the<br />

offset is added to the program counter value to get the address<br />

where the CPU will fetch the next program instruction.<br />

Each opcode tells the CPU the operation to be performed and the<br />

addressing mode to be used to address any operands needed to<br />

complete the instruction. The cycle-by-cycle explanations of example<br />

instructions under each addressing mode provide a view of the tiny<br />

simple steps that make up an instruction.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

134 <strong>M68HC05</strong> Instruction Set<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136<br />

Writing a Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136<br />

Flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137<br />

Mnemonic Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139<br />

Software Delay Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141<br />

Assembler Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143<br />

Object Code File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147<br />

Assembler Directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149<br />

Originate (ORG) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149<br />

Equate (EQU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149<br />

Form Constant Byte (FCB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150<br />

Form Double Byte (FDB). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150<br />

Reserve Memory Byte (RMB) . . . . . . . . . . . . . . . . . . . . . . . . . . . .151<br />

Set Default Number Base to Decimal . . . . . . . . . . . . . . . . . . . . . .152<br />

Instruction Set Dexterity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153<br />

Application Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

135


Freescale Semiconductor, Inc...<br />

Programming<br />

Introduction<br />

Writing a Simple Program<br />

Freescale Semiconductor, Inc.<br />

This chapter discusses how to plan and write computer programs. We<br />

will learn how to prepare flowcharts, write assembly language programs,<br />

and use a text editor or word processor to write computer programs.<br />

Next, a programming tool called an assembler is used to translate the<br />

program into a form the computer can use. Programming tools are<br />

computer programs for personal computers that help in the development<br />

of microcontroller computer programs. We will discuss assemblers,<br />

simulators, and a few other useful development tools.<br />

At this point, we will write a short program in mnemonic form and<br />

translate it into machine code. These are the steps:<br />

• The first step will be to plan the program and document this plan<br />

with a flowchart.<br />

• Next we will write instruction mnemonics for each block in the<br />

flowchart.<br />

• Finally, we will use an assembler to translate our example<br />

program into the codes the computer needs to execute the<br />

program.<br />

Our program will read the state of a switch connected to an input pin.<br />

When the switch is closed, the program will cause an LED (light-emitting<br />

diode) connected to an output pin to light for about one second and then<br />

go out. The LED will not light again until the switch has been released<br />

and closed again. The length of time the switch is held closed will not<br />

affect the length of time the LED is lighted.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

136 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Writing a Simple Program<br />

Although this program is simple, it demonstrates the most common<br />

elements of any MCU application program:<br />

• First, it demonstrates how a program can sense input signals such<br />

as switch closures.<br />

• Second, this is an example of a program controlling an output<br />

signal.<br />

• Third, the LED on-time of about one second demonstrates one<br />

way a program can be used to measure real time.<br />

Because the algorithm is sufficiently complicated, it cannot be<br />

accomplished in a trivial manner with discrete components. At minimum,<br />

a one-shot IC (integrated circuit) with external timing components would<br />

be required. This example demonstrates that an MCU and a<br />

user-defined program (software) can replace complex circuits.<br />

Flowchart Figure 30 is a flowchart of the example program. Flowcharts are often<br />

used as planning tools for writing software programs because they show<br />

the function and flow of the program under development. The<br />

importance of notes, comments, and documentation for software cannot<br />

be over-emphasized. Just as you would not consider a circuit-board<br />

design complete until there is a schematic diagram, parts list, and<br />

assembly drawing, you should not consider a program complete<br />

until there is a commented listing and a comprehensive<br />

explanation of the program such as a flowchart.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

137


Freescale Semiconductor, Inc...<br />

Programming<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Figure 30. Example Flowchart<br />

138 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

NO<br />

YES<br />

BEGIN<br />

SET INITIAL CONDITIONS:<br />

PORT A BIT 7 = 1 (LED OFF)<br />

MAKE PORT A BIT 7 AN OUTPUT<br />

READ SWITCH<br />

CLOSED ?<br />

YES<br />

DELAY TO DEBOUNCE<br />

TURN ON LED<br />

DELAY 1 SECOND<br />

TURN OFF LED<br />

SWITCH<br />

STILL CLOSED ?<br />

NO<br />

DELAY TO DEBOUNCE


Freescale Semiconductor, Inc...<br />

Mnemonic Source Code<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Mnemonic Source Code<br />

Once the flowchart or plan is completed, the programmer develops a<br />

series of assembly language instructions to accomplish the function(s)<br />

called for in each block of the plan. The programmer is limited to<br />

selecting instructions from the instruction set for the CPU being used (in<br />

this case the <strong>M68HC05</strong>). The programmer writes instructions in a<br />

mnemonic form that is easy to understand. Figure 31 shows the<br />

mnemonic source code next to the flowchart of our example program<br />

so you can see what CPU instructions are used to accomplish each<br />

block of the flowchart. The meanings of the mnemonics used in the right<br />

side of Figure 31 can be found in Instruction Set Details or in<br />

Instruction Set Summary.<br />

During development of the program instructions, it was noticed that a<br />

time delay was needed in three places. A subroutine was developed<br />

that generates a 50-ms delay. This subroutine is used directly in two<br />

places (for switch debouncing) and makes the 1-second delay easier to<br />

produce. To keep this figure simple, the comments that would usually be<br />

included within the source program for documentation are omitted. The<br />

comments will be shown in the completed program in Listing 3.<br />

Assembler Listing.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

139


Freescale Semiconductor, Inc...<br />

Programming<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

NO<br />

YES<br />

FLOWCHART<br />

MNEMONIC PROGRAM<br />

INIT LDA #$80<br />

Figure 31. Flowchart and Mnemonics<br />

STA PORTA<br />

STA DDRA<br />

TOP LDA PORTA<br />

BEQ TOP<br />

JSR DLY50<br />

BCLR 7,PORTA<br />

LDA #20<br />

DLYLP JSR DLY50<br />

DECA<br />

BNE DLYLP<br />

BSET 7,PORTA<br />

OFFLP BRSET 0,PORTA,OFFLP<br />

JSR DLY50<br />

BRA TOP<br />

140 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

BEGIN<br />

SET INITIAL CONDITIONS:<br />

PORT A BIT 7 = 1 (LED OFF)<br />

MAKE PORT A BIT 7 AN OUTPUT<br />

READ SWITCH<br />

CLOSED ?<br />

YES<br />

DELAY TO DEBOUNCE<br />

TURN ON LED<br />

DELAY 1 SECOND<br />

TURN OFF LED<br />

SWITCH<br />

STILL CLOSED ?<br />

NO<br />

DELAY TO DEBOUNCE<br />

AND #$01


Freescale Semiconductor, Inc...<br />

Software Delay Program<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Software Delay Program<br />

Figure 32 shows an expanded flowchart of the 50-ms delay subroutine.<br />

A subroutine is a relatively short program that performs some commonly<br />

required function. Even if the function needs to be performed many times<br />

in the course of a program, the subroutine only has to be written once.<br />

Each place where this function is needed, the programmer would call the<br />

subroutine with a branch-to-subroutine (BSR) or jump-to-subroutine<br />

(JSR) instruction.<br />

FLOWCHART MNEMONIC PROGRAM INSTRUCTION<br />

TIME (CYCLES)<br />

START<br />

SUBROUTINE<br />

SAVE ACCUMULATOR<br />

LOAD VALUE<br />

CORRESPONDING TO 50 ms<br />

DECREMENT COUNT<br />

NO COUNT<br />

EXPIRED ?<br />

YES<br />

RESTORE<br />

ACCUMULATOR<br />

RETURN FROM<br />

SUBROUTINE<br />

DLY50 STA TEMP1 4<br />

LDA #65 2<br />

OUTLP CLRX 3<br />

INNRLP DECX 3<br />

BNE INNRLP 3<br />

DECA 3<br />

BNE OUTLP 3<br />

LDA TEMP1 3<br />

RTS 6<br />

6 (JSR)<br />

[1] – INNRLP is executed 256 times per pass through outer loop.<br />

[2] – OUTLP is executed 65 times.<br />

Figure 32. Delay Routine Flowchart and Mnemonics<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

141<br />

[1]<br />

[2]


Freescale Semiconductor, Inc...<br />

Programming<br />

Freescale Semiconductor, Inc.<br />

Before starting to execute the instructions in the subroutine, the address<br />

of the instruction that follows the JSR (or BSR) is stored automatically on<br />

the stack in temporary RAM memory locations. When the CPU finishes<br />

executing the instructions within the subroutine, a<br />

return-from-subroutine (RTS) instruction is performed as the last<br />

instruction in the subroutine. The RTS instruction causes the CPU to<br />

recover the previously saved return address; thus, the CPU continues<br />

the program with the instruction following the JSR (or BSR) instruction<br />

that originally called the subroutine.<br />

The delay routine of Figure 32 involves an inner loop (INNRLP) within<br />

another loop (OUTLP). The inner loop consists of two instructions<br />

executed 256 times before X reaches $00 and the BNE branch condition<br />

fails. This amounts to six cycles at 500 ns per cycle times 256, which<br />

equals 0.768 ms for the inner loop. The outer loop executes 65 times.<br />

The total execution time for the outer loop is 65(1536+9) or<br />

65(1545) = 100,425 cycles or 50.212 ms. The miscellaneous<br />

instructions in this routine other than those in the outer loop total 21<br />

cycles; thus, the total time required to execute the DLY50 routine is<br />

50.223 ms, including the time required for the JSR instruction that calls<br />

DLY50.<br />

The on-chip timer system in the MC68HC705J1A can also be used to<br />

measure time. The timer-based approach is preferred because the CPU<br />

can perform other tasks during the delay, and the delay time is not<br />

dependent on the exact number of instructions executed as it is in<br />

DLY50.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

142 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Assembler Listing<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Assembler Listing<br />

After a complete program or subprogram is written, it must be converted<br />

from mnemonics into binary machine code that the CPU can later<br />

execute. A separate computer system, such as an IBM PC ® , is used to<br />

perform this conversion to machine language. A computer program for<br />

the personal computer, called an assembler, is used. The assembler<br />

reads the mnemonic version of the program (also called the source<br />

version of the program) and produces a machine-code version of the<br />

program in a form that can be programmed into the memory of the MCU.<br />

The assembler also produces a composite listing showing both the<br />

original source program (mnemonics) and the object code translation.<br />

This listing is used during the debug phase of a project and as part of the<br />

documentation for the software program. Listing 3. Assembler Listing<br />

shows the listing that results from assembling the example program.<br />

Comments were added before the program was assembled.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

143


Freescale Semiconductor, Inc...<br />

Programming<br />

Listing 3. Assembler Listing<br />

Freescale Semiconductor, Inc.<br />

*******************************************************<br />

* Simple 68HC05 Program Example *<br />

* Read state of switch at port A bit-0; 1 = closed *<br />

* When sw. closes, light LED for about 1 sec; LED on *<br />

* when port A bit-7 = 0. Wait for sw release, *<br />

* then repeat. Debounce sw 50 ms on & off *<br />

* NOTE: Timing based on instruction execution times *<br />

* If using a simulator or crystal less than 4 MHz, *<br />

* this routine will run slower than intended *<br />

*******************************************************<br />

$BASE 10T ;Tell assembler to use decimal<br />

;unless $ or % before value<br />

0000 PORTA EQU $00 ;Direct address of port A<br />

0004 DDRA EQU $04 ;Data direction control, port A<br />

00E0 TEMP1 EQU $C0 ;One byte temp storage location<br />

0300 ORG $0300 ;Program will start at $0300<br />

0300 A6 80 INIT LDA #$80 ;Begin initialization<br />

0302 B7 00 STA PORTA ;So LED will be off<br />

0304 B7 04 STA DDRA ;Set port A bit-7 as output<br />

* Rest of port A is configured as inputs<br />

0306 B6 00 TOP LDA PORTA ;Read sw at LSB of Port A<br />

0308 A4 01 AND #$01 ;To test bit-0<br />

030A 27 FA BEQ TOP ;Loop till Bit-0 = 1<br />

030C CD 03 23 JSR DLY50 ;Delay about 50 ms to debounce<br />

030F 1F 00 BCLR 7,PORTA ;Turn on LED (bit-7 to zero)<br />

0311 A6 14 LDA #20 ;Decimal 20 assembles to $14<br />

0313 CD 03 23 DLYLP JSR DLY50 ;Delay 50 ms<br />

0316 4A DECA ;Loop counter for 20 loops<br />

0317 26 FA BNE DLYLP ;20 times (20-19,19-18,...1-0)<br />

0319 1E 00 BSET 7,PORTA ;Turn LED back off<br />

031B 00 00 FD OFFLP BRSET 0,PORTA,OFFLP ;Loop here till sw off<br />

031E CD 03 23 JSR DLY50 ;Debounce release<br />

0321 20 E3 BRA TOP ;Look for next sw closure<br />

***<br />

* DLY50 - Subroutine to delay ~50mS<br />

* Save original accumulator value<br />

* but X will always be zero on return<br />

***<br />

0323 B7 C0 DLY50 STA TEMP1 ;Save accumulator in RAM<br />

0325 A6 41 LDA #65 ;Do outer loop 65 times<br />

0327 5F OUTLP CLRX ;X used as inner loop count<br />

0328 5A INNRLP DECX ;0-FF, FF-FE,...1-0 256 loops<br />

0329 26 FD BNE INNRLP ;6 cyc*256*500ns/cyc = 0.768 ms<br />

032B 4A DECA ;65-64, 64-63,...1-0<br />

032C 26 F9 BNE OUTLP ;1545cyc*65*500ns/cyc=50.212ms<br />

032E B6 C0 LDA TEMP1 ;Recover saved Accumulator val<br />

0330 81 RTS ;Return<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

144 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Assembler Listing<br />

Refer to Figure 33 for the following discussion. This figure shows some<br />

lines of the listing with reference numbers indicating the various parts of<br />

the line. The first line is an example of an assembler directive line. This<br />

line is not really part of the program; rather, it provides information to the<br />

assembler so that the real program can be converted properly into binary<br />

machine code.<br />

0000 PORTA EQU $00 ;Direct address of port A<br />

0300 ORG $0300 ;Program will start at $0200<br />

0306 B6 00 TOP LDA PORTA ;Read sw at LSB of Port A<br />

---- --------- ------- ---- -------- --------------------------------<br />

[1] [2] [3] [4] [5] [6]-><br />

Figure 33. Explanation of Assembler Listing<br />

EQU, short for equate, is used to give a specific memory location or<br />

binary number a name that can then be used in other program<br />

instructions. In this case, the EQU directive is being used to assign the<br />

name PORTA to the value $00, which is the address of the port A<br />

register in the MC68HC705J1A. It is easier for a programmer to<br />

remember the mnemonic name PORTA rather than the anonymous<br />

numeric value $00. When the assembler encounters one of these<br />

names, the name is replaced automatically by its corresponding binary<br />

value in much the same way that instruction mnemonics are replaced by<br />

binary instruction codes.<br />

The second line shown in Figure 33 is another assembler directive. The<br />

mnemonic ORG, which is short for originate, tells the assembler where<br />

the program will start (the address of the start of the first instruction<br />

following the ORG directive line). More than one ORG directive may be<br />

used in a program to tell the assembler to put different parts of the<br />

program in specific places in memory. Refer to the memory map of the<br />

MCU to select an appropriate memory location where a program should<br />

start.<br />

In this assembler listing, the first two fields, [1] and [2], are generated by<br />

the assembler, and the last four fields, [3], [4], [5], and [6], are the original<br />

source program written by the programmer. Field [3] is a label (TOP)<br />

which can be referred to in other instructions. In our example program,<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

145


Freescale Semiconductor, Inc...<br />

Programming<br />

Freescale Semiconductor, Inc.<br />

the last instruction was BRA TOP, which simply means the CPU will<br />

continue execution with the instruction that is labeled TOP.<br />

When the programmer is writing a program, the addresses where<br />

instructions will be located are not typically known. Worse yet, in branch<br />

instructions, rather than using the address of a destination, the CPU<br />

uses an offset (difference) between the current PC value and the<br />

destination address. Fortunately, the programmer does not have to<br />

worry about these problems because the assembler takes care of these<br />

details through a system of labels. This system of labels is a convenient<br />

way for the programmer to identify specific points in the program (without<br />

knowing their exact addresses); the assembler can later convert these<br />

mnemonic labels into specific memory addresses and even calculate<br />

offsets for branch instructions so that the CPU can use them.<br />

Field [4] is the instruction field. The LDA mnemonic is short for load<br />

accumulator. Since there are six variations (different opcodes) of the<br />

load accumulator instruction, additional information is required before<br />

the assembler can choose the correct binary opcode for the CPU to use<br />

during execution of the program.<br />

Field [5] is the operand field, providing information about the specific<br />

memory location or value to be operated on by the instruction. The<br />

assembler uses both the instruction mnemonic and the operand<br />

specified in the source program to determine the specific opcode for the<br />

instruction.<br />

The different ways of specifying the value to be operated on are called<br />

addressing modes. (A more complete discussion of addressing modes<br />

was presented in Addressing Modes.) The syntax of the operand field<br />

is slightly different for each addressing mode, so the assembler can<br />

determine the correct intended addressing mode from the syntax of the<br />

operand. In this case, the operand [5] is PORTA, which the assembler<br />

automatically converts to $00 (recall the EQU directive). The assembler<br />

interprets $00 as a direct addressing mode address between $0000 and<br />

$00FF, thus selecting the opcode $B6, which is the direct addressing<br />

mode variation of the LDA instruction. If PORTA had been preceded by<br />

a # symbol, that syntax would have been interpreted by the assembler<br />

as an immediate addressing mode value, and the opcode $A6 would<br />

have been chosen instead of $B6.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

146 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Assembler Listing<br />

Field [6] is called the comment field and is not used by the assembler to<br />

translate the program into machine code. Rather, the comment field is<br />

used by the programmer to document the program. Although the CPU<br />

does not use this information during program execution, a programmer<br />

knows that it is one of the most important parts of a good program. The<br />

comment [6] for this line of the program says ;Read sw at LSB of<br />

port A. This comment tells someone who is reading the listing<br />

something about the instruction or why it is there, which is essential for<br />

understanding how the program works. The semicolon indicates that<br />

the rest of the line should be treated as a comment (not all assemblers<br />

require this semicolon). An entire line can be made into a comment line<br />

by using an asterisk (*) as the first character in the line. In addition to<br />

good comments in the listing, it is also important to document programs<br />

with a flowchart or other detailed information explaining the overall flow<br />

and operation of the program.<br />

Object Code File We learned in Computer Architecture that the computer expects the<br />

program to be a series of 8-bit values in memory. So far, our program<br />

still looks as if it were written for people. The version the computer needs<br />

to load into its memory is called an object code file. For Motorola<br />

microcontrollers, the most common form of object code file is the<br />

S-record file. The assembler can be directed to optionally produce a<br />

listing file and/or an object code file.<br />

An S-record file is an ASCII text file that can be viewed by a text editor<br />

or word processor. You should not edit these files because the structure<br />

and content of the files are critical to their proper operation.<br />

Each line of an S-record file is a record. Each record begins with a<br />

capital letter S followed by a code number from 0 to 9. The only code<br />

numbers that are important to us are S0, S1, and S9 because other<br />

S-number codes apply only to larger systems. S0 is an optional header<br />

record that may contain the name of the file for the benefit of humans<br />

that need to maintain these files. S1 records are the main data records.<br />

An S9 record is used to mark the end of the S-record file. For the work<br />

we are doing with 8-bit microcontrollers, the information in the S9 record<br />

is not important, but an S9 record is required at the end of our S-record<br />

files. Figure 34 shows the syntax of an S1 record.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

147


Freescale Semiconductor, Inc...<br />

Programming<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

TYPE<br />

LENGTH<br />

ADDRESS OBJECT CODE DATA CHECKSUM<br />

S1 13 03 20 23 20 E3 B7 C0 A6 41 5F 5A 26 FD 4A 26 F9 B6 C0 8A<br />

CHECKSUM = ONES COMPLEMENT OF THE SUM OF ALL OF THESE BYTES<br />

Figure 34. Syntax of an S1 Record<br />

All of the numbers in an S-record file are in hexadecimal. The type field<br />

is S0, S1, or S9 for the S-record files we will use. The length field is the<br />

number of pairs of hexadecimal digits in the record excluding the type<br />

and length fields. The address field is the 16-bit address where the first<br />

data byte will be stored in memory. Each pair of hexadecimal digits in the<br />

machine code data field represents an 8-bit data value to be stored in<br />

successive locations in memory. The checksum field is an 8-bit value<br />

that represents the ones complement of the sum of all bytes in the<br />

S-record except the type and checksum fields. This checksum is used<br />

during loading of the S-record file to verify that the data is complete and<br />

correct for each record.<br />

Figure 35 is the S-record file that results from assembling the example<br />

program of Listing 3. Assembler Listing. The two bytes of machine<br />

code data that are bold are the same two bytes that were highlighted in<br />

Figure 16 and the text that follows Figure 16. These bytes were located<br />

by looking in the listing and seeing that the address where this<br />

instruction started was $0323. In the S-record file, we found the S1<br />

record with the address $0320. Moving to the right, we found the data<br />

$23 for address $0320, $20 for address $0321, $E3 for $0322, and<br />

finally the bytes we wanted for address $0323 and $0324.<br />

S1130300A680B700B704B600A40127FACD03231FC3<br />

S113031000A614CD03234A26FA1E000000FDCD03D7<br />

S11303202320E3B7C0A6415F5A26FD4A26F9B6C08A<br />

S10403308147<br />

S9030000FC<br />

Figure 35. S-Record File for Example Program<br />

148 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Assembler Directives<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Assembler Directives<br />

In this section we discuss six of the most important assembler directives.<br />

Assemblers from varying vendors differ in the number and kind of<br />

assembler directives that are supported. Always refer to the<br />

documentation for the assembler you are using.<br />

Originate (ORG) This directive is used to set the location counter for the assembler. The<br />

location counter keeps track of the address where the next byte of<br />

machine code will be stored in memory. In our example program, there<br />

was an ORG directive to set the start of our program to $0300.<br />

As the assembler translates program statements into machine code<br />

instructions and data, the location counter is advanced to point at the<br />

next available memory location.<br />

Every program has at least one ORG directive to establish the starting<br />

place in memory for the program. Most complete programs also will have<br />

a second ORG directive near the end of the program to set the location<br />

counter to the address where the reset and interrupt vectors are located<br />

($07F8–$07FF in the MC68HC705J1A). The reset vector must always<br />

be specified, and it is good practice to also specify interrupt<br />

vectors, even if you do not expect to use interrupts.<br />

Equate (EQU) This directive is used to associate a binary value with a label. The value<br />

may be either an 8-bit value or a 16-bit address value. This directive<br />

does not generate any object code.<br />

During the assembly process, the assembler must keep a cross<br />

reference list where it stores the binary equivalent of each label. When<br />

a label appears in the source program, the assembler looks in this cross<br />

reference table to find the binary equivalent. Each EQU directive<br />

generates an entry in this cross reference table.<br />

An assembler reads the source program twice. On the first pass, the<br />

assembler just counts bytes of object code and internally builds the<br />

cross reference table. On the second pass, the assembler generates the<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

149


Freescale Semiconductor, Inc...<br />

Programming<br />

Form Constant<br />

Byte (FCB)<br />

Form Double Byte<br />

(FDB)<br />

Freescale Semiconductor, Inc.<br />

listing file and/or the S-record object file. This 2-pass arrangement<br />

allows the programmer to reference labels that are defined later in the<br />

program.<br />

EQU directives should appear near the beginning of a program, before<br />

their labels are used by other program statements. If the assembler<br />

encounters a label before it is defined, it has no choice but to assume<br />

the worst case of a 16-bit address value. This would cause the extended<br />

addressing mode to be used in places where the more efficient direct<br />

addressing mode could have been used. In other cases, the indexed<br />

16-bit offset addressing mode may be used where a more efficient 8-bit<br />

or no offset indexed instruction could have been used.<br />

In the example program, there were two EQU directives to equate the<br />

labels PORTA and DDRA to their direct page addresses. Another use for<br />

EQU directives is to identify a bit position with a label like this:<br />

LED EQU %10000000 ;LED is connected to bit-7<br />

" " " "<br />

" " " "<br />

INIT LDA #LED ;There’s a 1 in LED bit position<br />

STA PORTA ;So LED will be off<br />

STA DDRA ;So LED pin is an output<br />

The % symbol indicates the value that follows is expressed in binary. If<br />

we moved the LED to a different pin during development, we would only<br />

need to change the EQU statement and reassemble the program.<br />

The arguments for this directive are labels or numbers, separated by<br />

commas, that can be converted into single bytes of data. Each byte<br />

specified in an FCB directive generates a byte of machine code in the<br />

object code file. FCB directives are used to define constants in a<br />

program.<br />

The arguments for this directive are labels or numbers, separated by<br />

commas, that can be converted into 16-bit data values. Each argument<br />

specified in an FDB directive generates two bytes of machine code in the<br />

object code file.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

150 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Reserve Memory<br />

Byte (RMB)<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Assembler Directives<br />

These assembly listing lines demonstrate ORG directives and FDB<br />

directives.<br />

" " " " " " "<br />

" " " " " " "<br />

0300 ORG $0300 ;Beginning of EPROM in 705J1A<br />

0300 B6 00 START LDA PORTA ;Read sw at LSB of port A<br />

" " " " " " "<br />

" " " " " " "<br />

041F 80 UNUSED RTI ;Return from unexpected int<br />

" " " " " " "<br />

" " " " " " "<br />

07F8 ORG $07F8 ;Start of vector area<br />

07F8 04 1F TIMVEC FDB UNUSED ;An unused vector<br />

07FA 04 1F IRQVEC FDB $041F ;Argument can be a hex value<br />

07FC 04 1F SWIVEC FDB UNUSED ;An unused vector<br />

07FE 03 00 RESETV FDB START ;Go to START on reset<br />

This directive is used to set aside space in RAM for program variables.<br />

The RMB directive does not generate object code but it normally<br />

generates an entry in the assembler’s internal cross reference table.<br />

In the example program (Listing 3. Assembler Listing), the RAM<br />

variable TEMP1 was assigned with an EQU directive. Another way to<br />

assign this variable is like this:<br />

" " " " " " "<br />

00C0 ORG $00C0 ;Beginning of RAM in 705J1A<br />

00C0 TEMP1 RMB 1 ;One byte temp storage location<br />

" " " " " " "<br />

This is the preferred way to assign RAM storage because it is common<br />

to add and delete variables in the course of developing a program. If you<br />

used EQU directives, you might have to change several statements after<br />

removing a single variable. With RMB directives, the assembler assigns<br />

addresses as they are needed.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

151


Freescale Semiconductor, Inc...<br />

Programming<br />

Set Default<br />

Number Base<br />

to Decimal<br />

Freescale Semiconductor, Inc.<br />

Some assemblers, such as the P & E Microcomputer Systems IASM<br />

assembler, assume that any value that is not specifically marked<br />

otherwise should be interpreted as a hexadecimal value. The idea is to<br />

simplify entry of numeric information by eliminating the need for a $<br />

symbol before each value. If you want the assembler to assume that<br />

unmarked values are decimal numbers, use the $BASE directive.<br />

" " " " " " "<br />

.... $BASE 10T ;Set default # base to decimal<br />

000A TEN EQU #10 ;Decimal 10 not $10 = 16<br />

" " " " " " "<br />

This directive is slightly different from the others described in this<br />

chapter. The $BASE directive starts in the leftmost column of the source<br />

program. This directive is included near the start of each example<br />

program in this textbook. If you are using an assembler that does not<br />

require this directive, you can delete it or add an asterisk (*) at the start<br />

of the line to comment the line out. When you comment a line out of the<br />

program, you change the whole line into a comment. Comments do not<br />

affect assembly of a program.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

152 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Dexterity<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Instruction Set Dexterity<br />

As in most engineering fields, more than one sequence of instructions<br />

can perform any task. A good way to learn a new instruction set is to see<br />

how many different ways you can solve some small programming<br />

problem. This is called instruction set dexterity.<br />

Figure 36 shows four different ways to check for closure of a switch<br />

connected to port A bit 0. Two of these ways were used in the example<br />

program of Listing 3. Assembler Listing. Although all of the sequences<br />

accomplish the same basic task, there are subtle differences. Usually<br />

these differences are not significant, but sometimes they can save<br />

execution time or program memory space. In a small microcontroller,<br />

memory space can be an important consideration.<br />

0000 PORTA EQU $00 ;Direct address of port A<br />

0300 ORG $0300 ;Program will start at $0300<br />

0300 B6 00 [ 3] TOP1 LDA PORTA ;Read sw at LSB of Port A<br />

0302 A4 01 [ 2] AND #$01 ;To test bit-0<br />

0304 27 FA [ 3] BEQ TOP1 ;Loop till bit-0 = 1<br />

0306 01 00 FD [ 5] TOP2 BRCLR 0,PORTA,TOP2 ;Loop here till sw ON<br />

0309 B6 00 [ 3] TOP3 LDA PORTA ;Read sw at LSB of Port A<br />

030B 44 [ 3] LSRA ;Bit-0 shifts to carry<br />

030C 24 FB [ 3] BCC TOP3 ;Loop till switch ON<br />

030E A6 01 [ 2] LDA #$01 ;1 in LSB<br />

0310 B5 00 [ 3] TOP4 BIT PORTA ;To test sw at bit-0<br />

0312 27 FC [ 3] BEQ TOP4 ;Loop till switch ON<br />

Figure 36. Four Ways to Check a Switch<br />

The numbers in square brackets are the number of CPU cycles required<br />

for the instruction on that line of the program. The TOP1 sequence takes<br />

six bytes of program space and eight cycles. The accumulator is $01<br />

when the program falls through the BEQ statement. The TOP2<br />

sequence takes only three bytes and five cycles, and the accumulator is<br />

not disturbed. (This is probably the best sequence in most cases.) The<br />

TOP3 sequence takes one less byte than the TOP1 sequence but also<br />

takes one extra cycle to execute. After the TOP3 sequence, the<br />

accumulator still holds the other seven bits from the port A read,<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

153


Freescale Semiconductor, Inc...<br />

Programming<br />

Application Development<br />

Freescale Semiconductor, Inc.<br />

although they have been shifted one position to the right. The last<br />

sequence takes six bytes and a total of eight cycles, but the loop itself is<br />

only six cycles. By working through exercises like this, you will improve<br />

your instruction set dexterity. This will be very helpful when you need to<br />

reduce a program by a few bytes to fit it into the available memory space.<br />

A simple development system for the MC68HC705J1A is offered by<br />

Motorola (M68HC705JICS). This system includes an in-circuit simulator<br />

(software and hardware circuit board). The circuit board plugs into a<br />

serial (com) port on a personal computer. A connector and cable allow<br />

the in-circuit simulator to be plugged into an application system to take<br />

the place of the microcontroller that will eventually be used. A socket is<br />

also provided that allows an EPROM or OTP version of the<br />

MC68HC705J1A to be programmed from the personal computer.<br />

A simulator is a program for a personal computer that helps during<br />

program development and debugging. This tool simulates the actions of<br />

a real microcontroller but has some important advantages. For instance,<br />

in a simulator, you have complete control over when and if the simulated<br />

CPU should advance to the next instruction. You can also look at and<br />

change registers or memory locations before going to the next<br />

instruction.<br />

Simulators do not run at real-time speed. Since the personal computer<br />

is simulating MCU actions with software programs, each MCU<br />

instruction takes much longer to execute than it would in a real MCU. For<br />

many MCU programs, this speed reduction is not noticeable. As slow as<br />

a simulator can be, it is still very fast in human terms. Some MCU<br />

programs generate time delays with software loops (like the DLY50<br />

routine in Listing 3. Assembler Listing). The 50-millisecond delay of<br />

DLY50 might take tens of seconds on some personal computers. To<br />

make the simulation run faster, you can temporarily replace the loop<br />

count value (65) with a much smaller number (for instance, 2).<br />

NOTE: Remember to put the original number back before programming the<br />

finished program into the EPROM of a real MCU.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

154 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Application Development<br />

An in-circuit simulator is a simulator that can be connected to a user<br />

system in place of the microcontroller. An ordinary simulator normally<br />

only takes input information from the personal computer and displays<br />

outputs and results on the personal computer display. An in-circuit<br />

simulator goes beyond this to emulate the input and output interfaces of<br />

the real microcontroller.<br />

Program development is easier with a simulator than a real MCU. It is<br />

easier to make program changes and try them out in the simulator than<br />

to program an EPROM device and try it out. With the real MCU, you can<br />

only see the input and output pins, and you cannot easily stop a program<br />

between instructions. But with the simulator, you can execute a single<br />

instruction at a time and look at registers and memory contents at every<br />

step. This makes it easier to see which instructions failed to perform as<br />

intended. A simulator can also inform you if the program attempts to use<br />

the value of a variable before it has been initialized.<br />

An in-circuit emulator is a real-time development tool. The emulator is<br />

built around an actual MCU, so it can execute program instructions<br />

exactly as they will be executed in the finished application. An emulator<br />

has RAM memory where the ROM or EPROM memory will be located in<br />

the final MCU. This allows you to load programs quickly into the emulator<br />

and to change these programs during development.<br />

Extra circuitry in the emulator allows you to set breakpoints in the<br />

program under development. When the program reaches one of these<br />

breakpoint addresses, the program under development is temporarily<br />

stopped and a development monitor program takes control. This<br />

monitor program allows you to look at or change CPU registers, memory<br />

locations, or control registers. An emulator typically has less visibility of<br />

internal MCU actions than a simulator, but it can run at full real-time<br />

speed. An emulator cannot normally stop clocks to internal peripheral<br />

systems like a timer, when control switches from the application program<br />

to the monitor program. A simulator can stop such clocks.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

155


Freescale Semiconductor, Inc...<br />

Programming<br />

Review<br />

Freescale Semiconductor, Inc.<br />

The process of writing a program begins with a plan. A flowchart can be<br />

used to document the plan. Mnemonic source code statements are then<br />

written for each block of the flowchart. Mnemonic source code<br />

statements can include any of the instructions from the instruction set of<br />

the microcontroller. The next step is to combine all of the program<br />

instructions with assembler directives to get a text source file.<br />

Assembler directives are program statements that give instructions to<br />

the assembler rather than to the CPU. These instructions tell the<br />

assembler things like where to locate instructions in the memory of the<br />

microcontroller. Assembler directives can also inform the assembler of<br />

the binary meaning of a mnemonic label. Six directives were discussed.<br />

• ORG <strong>—</strong> Originate directives set the starting address for the object<br />

code that follows.<br />

• EQU <strong>—</strong> Equate directives associate a label with a binary number<br />

or address.<br />

• FCB <strong>—</strong> Form constant byte directives are used to introduce 8-bit<br />

constant data values into a program.<br />

• FDB <strong>—</strong> Form double byte directives are used to introduce 16-bit<br />

data or address constants into a program.<br />

• RMB <strong>—</strong> Reserve memory byte(s) directives are used to assign<br />

labels (belonging to program variables) to RAM addresses.<br />

• $BASE 10T <strong>—</strong> Change default number base to decimal.<br />

After the complete source program is written, it is processed by an<br />

assembler to produce a listing file and an S-record object file. The listing<br />

file is part of the documentation of the program. The S-record object file<br />

can be loaded into the simulator or it can be programmed into a<br />

microcontroller.<br />

A conditional loop can produce a timed delay. The delay is dependent<br />

on the execution time of the instructions in the loop. A subroutine such<br />

as this delay routine can be used many times in a program by calling it<br />

with JSR or BSR instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

156 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Programming<br />

Review<br />

Instruction set dexterity is the ability to solve a programming problem in<br />

several different ways with different sequences of instructions. Since<br />

each sequence takes a different number of program bytes and a<br />

different number of CPU cycles to execute, you can select a sequence<br />

that is best for each situation.<br />

A simulator is an application development tool that runs on a personal<br />

computer and simulates the behavior of a microcontroller (though not at<br />

real-time speed). An in-circuit simulator takes this idea further to also<br />

simulate the I/O interfaces of the microcontroller. The in-circuit simulator<br />

can be plugged into an application circuit in place of the microcontroller.<br />

A simulator makes application development easier. It allows instructions<br />

to be executed one at a time. It also provides visibility into the contents<br />

of registers and memory and allows changes before executing a new<br />

instruction.<br />

An emulator is built around a real MCU so it can run at the full speed of<br />

the final MCU. Emulators use RAM instead of ROM or EPROM so the<br />

program under development can be modified easily during<br />

development.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

157


Freescale Semiconductor, Inc...<br />

Programming<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

158 Programming<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

The Paced Loop<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159<br />

System Equates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160<br />

Register Equates for MC68HC705J1A . . . . . . . . . . . . . . . . . . . . .160<br />

Application System Equates . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161<br />

Vector Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162<br />

Reset Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162<br />

Unused Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163<br />

RAM Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165<br />

Paced Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165<br />

Loop Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167<br />

Loop System Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168<br />

Your Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168<br />

Timing Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169<br />

Stack Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170<br />

An Application-Ready Framework . . . . . . . . . . . . . . . . . . . . . . . . . . .171<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178<br />

This chapter presents a general-purpose software structure that may be<br />

used as a framework for many microcontroller applications. Major<br />

system tasks are written as subroutines. These subroutines are<br />

organized into a loop so that each is called once per pass through the<br />

loop. At the top of the loop there is a short routine that paces the loop so<br />

that it is executed at regular intervals. A software clock is maintained as<br />

the first task in the loop. This clock can be used as an input to the other<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

159


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

System Equates<br />

Register<br />

Equates for<br />

MC68HC705J1A<br />

Freescale Semiconductor, Inc.<br />

task subroutines to decide what the routine should do on each pass<br />

through the major loop.<br />

In addition to the loop structure itself, this chapter discusses system<br />

initialization issues and software setup details so you can go directly to<br />

the routines that deal with your specific applications.<br />

Because using binary bit patterns and addresses in programs<br />

instructions is inconvenient, equate (EQU) directives are used to assign<br />

mnemonic names to register addresses and bit positions. These names<br />

can then be used in program instructions instead of the binary numbers.<br />

This makes the program easier to write and to read. When an in-circuit<br />

simulator is used to develop an application program, the mnemonic<br />

names can be used in the debug displays instead of the binary<br />

addresses.<br />

The manufacturer’s recommended names for registers and control bits<br />

are included in the paced loop program framework of Listing 4. Paced<br />

Loop Framework Program in this chapter. This allows you to write<br />

program instructions using names that make sense to people instead of<br />

obscure binary numbers and addresses.<br />

Each register is equated to its direct-page binary address with an EQU<br />

directive. Each control bit is defined in two ways:<br />

• First, an EQU directive equates the bit name to a number between<br />

7 and 0 corresponding to the bit number where each bit is located<br />

in a control register.<br />

• Second, most control bits are equated to a binary bit pattern such<br />

as 0010 0000 ($20) which can be used as a bit mask to identify the<br />

location of the bit in a register.<br />

Since you cannot equate the same name to two different binary values,<br />

the second equate uses a period after the bit name. To get a bit name’s<br />

bit number (7–0), use the name; to get a mask indicating the bit position,<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

160 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Application<br />

System Equates<br />

Freescale Semiconductor, Inc.<br />

The Paced Loop<br />

System Equates<br />

use the name followed by a period. This convention is used in the paced<br />

loop framework, but it is not necessarily a standard that is recommended<br />

by Motorola or the assembler companies.<br />

In the <strong>M68HC05</strong> instruction set, the bit manipulation instructions are of<br />

the form<br />

xxxx 14 08 ----- BSET bit#,dd ;Set bit in location dd<br />

Bit# is a number between 7 and 0 that identifies the bit within the register<br />

at location dd that is to be changed or tested.<br />

In other cases, you may want to build up a mask with several bits set,<br />

and then write this composite value to a register location. For example,<br />

suppose you want to set RTIFR, RTIE, and RT1 bits in the TSCR<br />

register. You could use these instructions.<br />

xxxx A6 16 LDA #{RTIFR.+RTIE.+RT1.};Form mask<br />

xxxx B7 08 STA TSCR ;Write mask to TSCR register<br />

The # symbol means immediate addressing mode. The expression<br />

(RTIFR.+RTIE.+RT1.) is the Boolean OR of three bit position masks.<br />

The assembler evaluates the Boolean expression during program<br />

assembly and substitutes the answer (a single 8-bit binary value) into the<br />

assembled program. These program statements would produce exactly<br />

the same results, but they are not as easy to read.<br />

xxxx A6 16 LDA #%00010110 ;Form mask<br />

xxxx B7 08 STA $08 ;Write mask to TSCR register<br />

Usually, some application-specific equate directives will be in a program<br />

to define the signals connected to I/O pins. These EQU directives should<br />

be placed after the standard MCU equate directives and before the main<br />

program starts. The paced loop framework program was developed with<br />

a particular small development PC board in mind. This system has a<br />

switch connected to port A bit 0 and an LED connected to port A bit 7,<br />

so these connections were defined with EQU directives.<br />

The switch is not used in the paced loop framework program of<br />

Listing 4. Paced Loop Framework Program, but it does no harm to<br />

include the related EQU directives. EQU directives do not generate any<br />

object code that takes up memory space in the final computer system.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

161


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Vector Setup<br />

Freescale Semiconductor, Inc.<br />

All MCU programs should set up the reset and interrupt vectors.<br />

Vectors specify the address where the CPU will start processing<br />

instructions when a reset or interrupt occurs. Reset and each interrupt<br />

source expect to find their associated vector in a specific pair of memory<br />

locations. For example, the reset vector is at the highest two locations in<br />

memory ($07FE and $07FF in the MC68HC705J1A). If you do not place<br />

values in these locations, the CPU will take whatever binary values it<br />

finds there and treat them as if they were a 2-byte address you stored<br />

there.<br />

Reset Vector The usual way to define a vector is with an FDB directive.<br />

07FE 03 00 RESETV FDB START ;Beginning of program on reset<br />

During assembly, the assembler evaluates the label START into a<br />

2-byte address and stores this address in the next two available memory<br />

locations of the program. The columns at the left of the listing line show<br />

that the address $0300 was stored at $07FE and $07FF ($03 at $07FE<br />

and $00 at $07FF).<br />

RESETV is an optional label on this program line. Although it is not used<br />

for reference by other statements in this particular program, it was<br />

included to identify this FDB directive line as the statement that defines<br />

the reset vector.<br />

The reset vector was set up to point at the label START. The in-circuit<br />

simulator system that Motorola offers as a low-cost development tool<br />

uses this information to set up the simulator screen. When a program is<br />

loaded into the simulator, the simulator looks for the address in the reset<br />

vector of the loaded program. If one is found, the simulator selects that<br />

program instruction and displays it in the source program window of the<br />

simulator. The simulator’s PC is also set to this address. If there is no<br />

reset vector, the simulator displays a warning message, saying that the<br />

reset vector was not initialized. You could still debug the program, but it<br />

would not work if it was programmed into an EPROM MCU because the<br />

program would not start at reset.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

162 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

The Paced Loop<br />

Vector Setup<br />

Unused Interrupts For interrupts that are used, the vectors can be defined just as the reset<br />

vector was defined (with an FDB directive). In the paced loop framework<br />

program, the timer interrupt is used for real-time interrupts (RTI). The<br />

external interrupt and the SWI (software interrupt) are not used.<br />

It is a good idea to set up the unused interrupt vectors just in case one<br />

of these interrupts is requested unexpectedly. This is not to say that<br />

unexpected interrupts can occur in a working computer system. Rather,<br />

it says that when a programmer is first starting out, programming<br />

mistakes could result in unintended interrupt sources being enabled and<br />

triggered.<br />

This listing shows how interrupt and reset vectors were set up in the<br />

paced loop framework program.<br />

*******************************************************<br />

* RTIF interrupt service routine<br />

*******************************************************<br />

0345 3A E0 RTICNT DEC RTIFs ;On each RTIF<br />

" " " " " " "<br />

" " " " " " "<br />

0351 80 AnRTI RTI ;Return from RTIF interrupt<br />

0351 UNUSED EQU AnRTI ;Use RTI at AnRTI for unused<br />

;interrupts to just return<br />

*******************************************************<br />

* Interrupt & reset vectors<br />

*******************************************************<br />

07F8 ORG $07F8 ;Start of vector area<br />

07F8 03 45 TIMVEC FDB RTICNT ;Count RTIFs 3/TIC<br />

07FA 03 51 IRQVEC FDB UNUSED ;Change if vector used<br />

07FC 03 51 SWIVEC FDB UNUSED ;Change if vector used<br />

07FE 03 00 RESETV FDB START ;Beginning of program on reset<br />

The first lines in this partial listing show the first and last lines of the timer<br />

interrupt service routine. The line<br />

0351 80 AnRTI RTI ;Return from RTIF interrupt<br />

shows a return-from-interrupt (RTI) instruction with the label AnRTI. The<br />

next line equates the label UNUSED to the address of the RTI instruction<br />

at AnRTI. Further down in the listing, the unused interrupt vectors for<br />

external interrupts and SWI interrupts are set up to point at this RTI<br />

instruction. During assembly, the assembler encounters the label<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

163


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Freescale Semiconductor, Inc.<br />

UNUSED and finds it should be equal to AnRTI that is in turn equal to<br />

the binary address of the RTI instruction ($0351).<br />

If an SWI interrupt were unexpectedly encountered, the CPU would save<br />

the CPU registers on the stack (temporary RAM) and load the program<br />

counter with the address $0351 from the SWI vector. The CPU would<br />

then load the instruction RTI from address $0351. The RTI instruction<br />

would tell the CPU to recover the saved CPU registers (including the<br />

program counter) from the stack. The recovered program counter value<br />

would determine what the CPU did next.<br />

An alternate way to respond to unexpected interrupts would be to reset<br />

the stack pointer (with an RSP instruction) and then jump to the same<br />

address as if a reset had occurred. This approach makes the pessimistic<br />

assumption that if an unexpected interrupt occurs, there may be other<br />

serious problems. By resetting the stack pointer and starting all over you<br />

are more likely to correct whatever caused the unexpected interrupt.<br />

While debugging a program on a simulator, there is another possible<br />

way to handle unused interrupts.<br />

" " " " " " "<br />

0351 BADINT BRA BADINT ;Infinite loop to here<br />

" " " " " " "<br />

" " " " " " "<br />

07FA 03 51 VECTOR FDB BADINT ;Hang on unexpected int<br />

" " " " " " "<br />

In this scheme, an unexpected interrupt will cause the CPU to vector to<br />

BADINT. The instruction at BADINT is an infinite loop back to BADINT,<br />

so the system will hang there. You can stop the simulator and check the<br />

CPU register values on the stack to see what the program was doing<br />

when it got the unexpected interrupt.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

164 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

RAM Variables<br />

Paced Loop<br />

Freescale Semiconductor, Inc.<br />

The Paced Loop<br />

RAM Variables<br />

Program variables change value during the course of executing the<br />

program. These values cannot be specified before the program is written<br />

and programmed into the MCU. The CPU must use program instructions<br />

to initialize and modify these values. When the program is written, space<br />

is reserved for variables in the RAM memory of the MCU, using reserve<br />

memory byte(s) (RMB) directives.<br />

First, you would put an originate (ORG) directive to set the assembler’s<br />

location counter to the address of the start of RAM in the MCU ($00C0<br />

in the MC68HC705J1A). Each variable or group of variables would be<br />

set up with an RMB directive. The RMB line is identified by the name of<br />

the variable. The assembler assigns the name (label) to the next<br />

available address. After each new variable or group of variables is<br />

assigned, the location counter is advanced to point at the next free<br />

memory location.<br />

As the program in Listing 4. Paced Loop Framework Program shows,<br />

some programmers feel it is good practice to clear all RAM locations as<br />

one of the first initialization steps after any reset. While you are<br />

debugging a system, it is useful to have a known set of starting<br />

conditions. If the entire RAM is cleared at the start of a program, it is easy<br />

to tell if any locations have been written.<br />

The paced loop is a general-purpose software structure that is suitable<br />

for a wide variety of MCU applications. The main idea is to break the<br />

overall application into a series of tasks such as keeping track of time,<br />

reading system inputs, and updating system outputs. Each task is<br />

written as a subroutine. A main loop is constructed out of<br />

jump-to-subroutine (JSR) instructions for each task. At the top of the<br />

loop is a software pacemaker. When the pacemaker triggers, the list of<br />

task subroutines is executed once and a branch instruction takes you to<br />

the top of the loop to wait for the next pacemaker trigger.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

165


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Freescale Semiconductor, Inc.<br />

Figure 37 shows a flowchart for the main paced loop. The top block is a<br />

loop that waits for the pacemaker trigger (every 100 milliseconds). The<br />

next few blocks have to do with maintaining the TIC counter. The version<br />

of this program in Listing 4. Paced Loop Framework Program has two<br />

simple main tasks, TIME and BLINK. You would remove one or both of<br />

these routines and substitute your own tasks. The only limitation on the<br />

number of main tasks is that they must all finish quickly enough so no<br />

pacemaker triggers are lost. The last block in the flowchart is just a<br />

branch back to the top of the loop to wait for the next pacemaker trigger.<br />

MAIN<br />

PACED LOOP<br />

CLEAR MSB OF TIC<br />

TIC = 10 ?<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

NO<br />

ARNC1<br />

Figure 37. Flowchart of Main Paced Loop<br />

166 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

MAIN<br />

MSB OF TIC = 1 ?<br />

YES<br />

TIC = TIC + 1<br />

YES<br />

SET TIC = 0<br />

CALL TASK ROUTINE TIME<br />

CALL TASK ROUTINE BLINK<br />

NO<br />

INSERT<br />

TASK ROUTINES<br />

HERE


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

The Paced Loop<br />

Paced Loop<br />

Loop Trigger In the paced loop program of Listing 4. Paced Loop Framework<br />

Program, the pacemaker is based on the on-chip real-time interrupt<br />

(RTI). This RTI is set to generate an interrupt to the CPU every 32.8<br />

milliseconds. The flowchart in Figure 37 shows what happens at each<br />

RTI. This interrupt activity can be thought of as if it were taking place<br />

asynchronously with respect to the main program. The most significant<br />

bit of the TIC variable is used as a flag to tell the main program when it<br />

is time to increment TIC and execute one pass through the paced loop.<br />

ENDRTI<br />

BEGIN RTIF<br />

INTERRUPT ROUTINE<br />

RTIFs = RTIFs – 1<br />

RTIFs = 0 ?<br />

Figure 38. Flowchart of RTI Service Routine<br />

The RAM variable RTIFs is used to count three real-time interrupts<br />

before setting the MSB of TIC. The main program will be watching TIC<br />

to see when the MSB becomes set.<br />

Every 32.8 ms the RTIF flag will get set, triggering a timer interrupt<br />

request. One of the duties of an interrupt service routine is to clear the<br />

flag that caused the interrupt before returning from the interrupt. If RTIF<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

167<br />

YES<br />

RTIFs = 3<br />

RTICNT<br />

SET MSB OF TIC<br />

CLEAR RTIF INTERRUPT FLAG<br />

AnRTI<br />

RETURN FROM<br />

INTERRUPT<br />

NO


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Loop System<br />

Clock<br />

Freescale Semiconductor, Inc.<br />

is not cleared before the return, a new interrupt request is generated<br />

immediately instead of waiting for the 32.8-ms trigger.<br />

The variable TIC is the most basic clock for the pacemaker. TIC counts<br />

from 0 to 10. As TIC is incremented from 9 to 10, the program recognizes<br />

this and resets TIC to 0. Except within the pacemaker itself, TIC appears<br />

to count from 0 to 9. TIC is equal to 0 on every tenth trigger of the<br />

pacemaker.<br />

The first task subroutine in the main loop is called TIME. This routine<br />

maintains a slower clock called TOC. TOC is incremented each time the<br />

paced loop executes and TIC is 0 (every tenth pass through the paced<br />

loop). TOC is set up as a software counter that counts from 0 through<br />

59. The remaining task routines after TIME can use the current values<br />

of TIC and TOC to decide what needs to be done on this pass through<br />

the paced loop.<br />

In Listing 4. Paced Loop Framework Program, the pace is keyed to<br />

the RTI which does not happen to be an integer submultiple of one<br />

second. Three RTI periods equal 98.4 milliseconds. This is pretty close<br />

to 0.1 second but not close enough to be used like a wristwatch. You<br />

could get accurate real time if you modified the paced loop program to<br />

use a different trigger source such as zero crossings of the ac<br />

(alternating current) line (60 Hz). Although the ac line is not as accurate<br />

as a crystal over short periods of time, it is very accurate over long<br />

periods. Most clocks that plug into the wall use the ac line timing as the<br />

basis for keeping time.<br />

Your Programs The task subroutines have few restrictions. Each task subroutine should<br />

do everything it needs to do, as quickly as it can, and then execute a<br />

return from subroutine (RTS). The total time required to execute one<br />

pass through all of the task subroutines must be less than two<br />

pacemaker triggers. (We will explain this in greater detail.) The important<br />

point is that a task subroutine should not wait for the occurrence of some<br />

external event like a switch to be pressed. This would defeat the<br />

timekeeping aspects of the paced loop.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

168 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Timing<br />

Considerations<br />

Freescale Semiconductor, Inc.<br />

The Paced Loop<br />

Paced Loop<br />

The paced loop can automatically provide for switch debouncing.<br />

Switches are notorious for bouncing between the closed and opened<br />

conditions as they are pressed and released. It is not at all unusual for a<br />

switch to bounce for 50 milliseconds or more as it is pressed. A<br />

microcontroller can execute instructions so fast that a single press of a<br />

switch might look like several presses to a program, unless steps are<br />

taken to account for switch bounce. There are hardware methods for<br />

debouncing switches but they require extra components and increase<br />

the cost of a product.<br />

Software can also be used to debounce a switch. The example program<br />

in Figure 31. Flowchart and Mnemonics used a simple software delay<br />

program to debounce a switch, but this routine should not be used<br />

directly in the paced loop structure because it takes too much time. In a<br />

paced loop, you can debounce a switch by reading it on consecutive<br />

passes through the paced loop. The first time you see the switch<br />

pressed, you can write a special value to a variable to indicate that a<br />

switch was tentatively pressed. (You would not consider this switch as<br />

pressed yet.) On the next pass through the paced loop, you would either<br />

mark the switch as really pressed or clear the mark to indicate that it was<br />

a false detection. Similarly, when the switch is eventually released, you<br />

can mark it as tentatively released and on the next pass mark it as really<br />

released.<br />

Ideally, you should finish all of the task subroutines in the paced loop<br />

before the next pacemaker trigger arrives. If a single pass through the<br />

loop takes longer than the pacemaker trigger period, the flag that<br />

indicates it is time to start the next pass through the main loop will<br />

already be set when you get back to the top of the loop. Nothing bad<br />

happens unless you get so far behind that a new pacemaker trigger<br />

comes before the previous one has been recognized. The paced loop<br />

remains valid unless any two consecutive passes take more than two<br />

pacemaker trigger periods.<br />

A little bit of planning can ensure that no two consecutive passes through<br />

the loop take longer than two pacemaker periods. Especially long task<br />

subroutines can be scheduled to execute during a particular paced loop<br />

pass when very little other activity is scheduled. A simple check of one<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

169


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Stack<br />

Considerations<br />

Freescale Semiconductor, Inc.<br />

of the time variables such as TIC or TOC can be used to decide whether<br />

or not to perform a particularly slow routine. If there were several things<br />

that needed to be done once per second, one could be scheduled for the<br />

TIC = 0 pass, another could be scheduled for the TIC = 2 pass, and so<br />

on.<br />

<strong>Small</strong> microcontrollers like the MC68HC705J1A have only small<br />

amounts of RAM for the stack and program variables. Interrupts take five<br />

bytes of stack RAM and each subroutine call takes two bytes on the<br />

stack. If a subroutine called another subroutine and an interrupt was<br />

requested before the second subroutine was finished, the stack would<br />

use 2+2+5 = 9 RAM bytes of the available 64. If the stack gets too deep,<br />

there is a danger that RAM variables can get written over with stack data.<br />

To avoid these problems, you should calculate the worst case depth that<br />

your stack can ever get to. In the MC68HC705J1A, the sum of all system<br />

variables plus the worst case stack depth must be less than or equal to<br />

the 64 available RAM locations.<br />

Fortunately, an interrupt causes the interrupt mask (I) bit in the condition<br />

code register to be set in response to any interrupt. This blocks<br />

additional interrupts until the I bit is cleared (normally upon return from<br />

the interrupt).<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

170 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

An Application-Ready Framework<br />

Freescale Semiconductor, Inc.<br />

The Paced Loop<br />

An Application-Ready Framework<br />

The paced loop program of Listing 4. Paced Loop Framework<br />

Program can be used as the basis for your own applications. This<br />

framework provides these main parts:<br />

• Equate statements for all MC68HC705J1A register and bit names<br />

• Application-specific equate statements<br />

• Program variables section<br />

• Initialization section (START)<br />

• Pacemaker for main loop based on RTI<br />

• Calls to task subroutines<br />

• Two simple examples of task subroutines (TIME and BLINK)<br />

• An interrupt service routine for RTIF interrupts<br />

• Vector definition section<br />

The pacemaker in this particular paced loop program triggers a pass<br />

through the main loop about once every 100 milliseconds (actually 98.4<br />

ms). This can be changed easily to some other number of real-time<br />

interrupts and the RTI rate can be changed. For applications that need<br />

real wristwatch time, the pacemaker can be modified to work from<br />

interrupts generated at zero crossings of the ac power line.<br />

Additional RMB directives should be added to the program variables<br />

section. Additional EQU statements can be added just above the<br />

program variables section to add application-specific equates.<br />

In its present form, the paced loop has only two simple task subroutines<br />

(TIME and BLINK). The TIME task just maintains a0to59count (TOC)<br />

which could be useful for measuring or generating longer time periods.<br />

The BLINK task is just a dummy routine to demonstrate how a task can<br />

use the time variable TOC to control a system action. In this case, the<br />

action is to turn on an LED when TOC is even, and turn it off when TOC<br />

is odd. To use the framework program for your own application, you<br />

should remove the BLINK task and replace it with your own tasks.<br />

The RTI service routine serves as an example of an interrupt handler<br />

and counts real-time interrupts to set the pacemaker rate.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

171


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Freescale Semiconductor, Inc.<br />

Listing 4. Paced Loop Framework Program<br />

Listing 4. Paced Loop Framework Program (Sheet 1 of 6)<br />

$BASE 10T<br />

*******************************************************<br />

* Equates for MC68HC705J1A MCU<br />

* Use bit names without a dot in BSET..BRCLR<br />

* Use bit name preceded by a dot in expressions such as<br />

* #.ELAT+.EPGM to form a bit mask<br />

*******************************************************<br />

PORTA EQU $00 ;I/O port A<br />

PA7 EQU 7 ;Bit #7 of port A<br />

PA6 EQU 6 ;Bit #6 of port A<br />

PA5 EQU 5 ;Bit #5 of port A<br />

PA4 EQU 4 ;Bit #4 of port A<br />

PA3 EQU 3 ;Bit #3 of port A<br />

PA2 EQU 2 ;Bit #2 of port A<br />

PA1 EQU 1 ;Bit #1 of port A<br />

PA0 EQU 0 ;Bit #0 of port A<br />

PA7. EQU $80 ;Bit position PA7<br />

PA6. EQU $40 ;Bit position PA6<br />

PA5. EQU $20 ;Bit position PA5<br />

PA4. EQU $10 ;Bit position PA4<br />

PA3. EQU $08 ;Bit position PA3<br />

PA2. EQU $04 ;Bit position PA2<br />

PA1. EQU $02 ;Bit position PA1<br />

PA0. EQU $01 ;Bit position PA0<br />

PORTB EQU $01 ;I/O port B<br />

PB5 EQU 5 ;Bit #5 of port B<br />

PB4 EQU 4 ;Bit #4 of port B<br />

PB3 EQU 3 ;Bit #3 of port B<br />

PB2 EQU 2 ;Bit #2 of port B<br />

PB1 EQU 1 ;Bit #1 of port B<br />

PB0 EQU 0 ;Bit #0 of port B<br />

PB5. EQU $20 ;Bit position PB5<br />

PB4. EQU $10 ;Bit position PB4<br />

PB3. EQU $08 ;Bit position PB3<br />

PB2. EQU $04 ;Bit position PB2<br />

PB1. EQU $02 ;Bit position PB1<br />

PB0. EQU $01 ;Bit position PB0<br />

DDRA EQU $04 ;Data direction for port A<br />

DDRA7 EQU 7 ;Bit #7 of port A DDR<br />

DDRA6 EQU 6 ;Bit #6 of port A DDR<br />

DDRA5 EQU 5 ;Bit #5 of port A DDR<br />

DDRA4 EQU 4 ;Bit #4 of port A DDR<br />

DDRA3 EQU 3 ;Bit #3 of port A DDR<br />

DDRA2 EQU 2 ;Bit #2 of port A DDR<br />

DDRA1 EQU 1 ;Bit #1 of port A DDR<br />

DDRA0 EQU 0 ;Bit #0 of port A DDR<br />

DDRA7. EQU $80 ;Bit position DDRA7<br />

DDRA6. EQU $40 ;Bit position DDRA6<br />

DDRA5. EQU $20 ;Bit position DDRA5<br />

DDRA4. EQU $10 ;Bit position DDRA4<br />

DDRA3. EQU $08 ;Bit position DDRA3<br />

DDRA2. EQU $04 ;Bit position DDRA2<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

172 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Listing 4. Paced Loop Framework Program (Sheet 2 of 6)<br />

DDRA1. EQU $02 ;Bit position DDRA1<br />

DDRA0. EQU $01 ;Bit position DDRA0<br />

DDRB EQU $05 ;Data direction for port B<br />

DDRB5 EQU 5 ;Bit #5 of port B DDR<br />

DDRB4 EQU 4 ;Bit #4 of port B DDR<br />

DDRB3 EQU 3 ;Bit #3 of port B DDR<br />

DDRB2 EQU 2 ;Bit #2 of port B DDR<br />

DDRB1 EQU 1 ;Bit #1 of port B DDR<br />

DDRB0 EQU 0 ;Bit #0 of port B DDR<br />

DDRB5. EQU $20 ;Bit position DDRB5<br />

DDRB4. EQU $10 ;Bit position DDRB4<br />

DDRB3. EQU $08 ;Bit position DDRB3<br />

DDRB2. EQU $04 ;Bit position DDRB2<br />

DDRB1. EQU $02 ;Bit position DDRB1<br />

DDRB0. EQU $01 ;Bit position DDRB0<br />

TSCR EQU $08 ;Timer status & control reg<br />

TOF EQU 7 ;Timer overflow flag<br />

RTIF EQU 6 ;Real time interrupt flag<br />

TOIE EQU 5 ;TOF interrupt enable<br />

RTIE EQU 4 ;RTI interrupt enable<br />

TOFR EQU 3 ;TOF flag reset<br />

RTIFR EQU 2 ;RTIF flag reset<br />

RT1 EQU 1 ;RTI rate select bit 1<br />

RT0 EQU 0 ;RTI rate select bit 0<br />

TOF. EQU $80 ;Bit position TOF<br />

RTIF. EQU $40 ;Bit position RTIF<br />

TOIE. EQU $20 ;Bit position TOIE<br />

RTIE. EQU $10 ;Bit position RTIE<br />

TOFR. EQU $08 ;Bit position TOFR<br />

RTIFR. EQU $04 ;Bit position RTIFR<br />

RT1. EQU $02 ;Bit position RT1<br />

RT0. EQU $01 ;Bit position RT0<br />

TCR EQU $09 ;Timer counter register<br />

ISCR EQU $0A ;IRQ status & control reg<br />

IRQE EQU 7 ;IRQ edge/edge-level<br />

IRQF EQU 3 ;External interrupt flag<br />

IRQR EQU 1 ;IRQF flag reset<br />

PDRA EQU $10 ;Pulldown register for port A<br />

PDIA7 EQU 7 ;Pulldown inhibit for PA7<br />

PDIA6 EQU 6 ;Pulldown inhibit for PA6<br />

PDIA5 EQU 5 ;Pulldown inhibit for PA5<br />

PDIA4 EQU 4 ;Pulldown inhibit for PA4<br />

PDIA3 EQU 3 ;Pulldown inhibit for PA3<br />

PDIA2 EQU 2 ;Pulldown inhibit for PA2<br />

PDIA1 EQU 1 ;Pulldown inhibit for PA1<br />

PDIA0 EQU 0 ;Pulldown inhibit for PA0<br />

PDIA7. EQU $80 ;Bit position PDIA7<br />

PDIA6. EQU $40 ;Bit position PDIA6<br />

PDIA5. EQU $20 ;Bit position PDIA5<br />

The Paced Loop<br />

An Application-Ready Framework<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

173


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Freescale Semiconductor, Inc.<br />

Listing 4. Paced Loop Framework Program (Sheet 3 of 6)<br />

PDIA4. EQU $10 ;Bit position PDIA4<br />

PDIA3. EQU $08 ;Bit position PDIA3<br />

PDIA2. EQU $04 ;Bit position PDIA2<br />

PDIA1. EQU $02 ;Bit position PDIA1<br />

PDIA0. EQU $01 ;Bit position PDIA0<br />

PDRB EQU $11 ;Pulldown register for port B<br />

PDIB5 EQU 5 ;Pulldown inhibit for PB5<br />

PDIB4 EQU 4 ;Pulldown inhibit for PB4<br />

PDIB3 EQU 3 ;Pulldown inhibit for PB3<br />

PDIB2 EQU 2 ;Pulldown inhibit for PB2<br />

PDIB1 EQU 1 ;Pulldown inhibit for PB1<br />

PDIB0 EQU 0 ;Pulldown inhibit for PB0<br />

PDIB5. EQU $20 ;Bit position PDIB5<br />

PDIB4. EQU $10 ;Bit position PDIB4<br />

PDIB3. EQU $08 ;Bit position PDIB3<br />

PDIB2. EQU $04 ;Bit position PDIB2<br />

PDIB1. EQU $02 ;Bit position PDIB1<br />

PDIB0. EQU $01 ;Bit position PDIB0<br />

EPROG EQU $18 ;EPROM programming register<br />

ELAT EQU 2 ;EPROM latch control<br />

MPGM EQU 1 ;MOR programming control<br />

EPGM EQU 0 ;EPROM program control<br />

ELAT. EQU $04 ;Bit position ELAT<br />

MPGM. EQU $02 ;Bit position MPGM<br />

EPGM. EQU $01 ;Bit position EPGM<br />

COPR EQU $07F0 ;COP watchdog reset register<br />

COPC EQU 0 ;COP watchdog clear<br />

COPC. EQU $01 ;Bit position COPC<br />

MOR EQU $07F1 ;Mask option register<br />

SOSCD EQU 7 ;Short osc delay enable<br />

EPMSEC EQU 6 ;EPROM security<br />

OSCRES EQU 5 ;Oscillator parallel resistor<br />

SWAIT EQU 4 ;STOP instruction mode<br />

PDI EQU 3 ;Port pulldown inhibit<br />

PIRQ EQU 2 ;Port A IRQ enable<br />

LEVEL EQU 1 ;IRQ edge sensitivity<br />

COP EQU 0 ;COP watchdog enable<br />

SOSCD. EQU $80 ;Bit position SOSCD<br />

EPMSEC. EQU $40 ;Bit position EPMSEC<br />

OSCRES. EQU $20 ;Bit position OSCRES<br />

SWAIT. EQU $10 ;Bit position SWAIT<br />

PDI. EQU $08 ;Bit position PDI<br />

PIRQ. EQU $04 ;Bit position PIRQ<br />

LEVEL. EQU $02 ;Bit position LEVEL<br />

COPEN. EQU $01 ;Bit position COPEN<br />

* Memory area equates<br />

RAMStart EQU $00C0 ;Start of on-chip RAM<br />

ROMStart EQU $0300 ;Start of on-chip ROM<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

174 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Listing 4. Paced Loop Framework Program (Sheet 4 of 6)<br />

ROMEnd EQU $07CF ;End of on-chip ROM<br />

Vectors EQU $07F8 ;Reset/interrupt vector area<br />

* Application specific equates<br />

LED EQU PA7 ;LED ON when PA7 is low (0)<br />

LED. EQU PA7. ;LED bit position<br />

SW EQU PA0 ;Switch on PA0, closed=high (1)<br />

SW. EQU PA0. ;Switch bit position<br />

*******************************************************<br />

* Put program variables here (use RMBs)<br />

*******************************************************<br />

ORG $00C0 ;Start of 705J1A RAM<br />

RTIFs RMB 1 ;3 RTIFs/TIC (3-0)<br />

TIC RMB 1 ;10 TICs make 1 TOC (10-0)<br />

;MSB=1 means RTIFs rolled over<br />

TOC RMB 1 ;1 TOC=10*96.24ms= about 1 sec<br />

*******************************************************<br />

* Program area starts here<br />

*******************************************************<br />

ORG $0300 ;Start of 705J1A EPROM<br />

* First initialize any control registers and variables<br />

START CLI ;Clear I bit for interrupts<br />

LDA #LED. ;Configure and turn off LED<br />

STA PORTA ;Turns off LED<br />

STA DDRA ;Makes LED pin an output<br />

LDA #{RTIFR.+RTIE.+RT1.}<br />

STA TSCR ;To clear and enable RTIF<br />

;and set RTI rate for 32.8 ms<br />

LDA #3 ;RTIFs counts 3->0<br />

STA RTIFs ;Reset TOFS count<br />

CLR TIC ;Initial value for TIC<br />

CLR TOC ;Initial value for TOC<br />

The Paced Loop<br />

An Application-Ready Framework<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

175


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Freescale Semiconductor, Inc.<br />

Listing 4. Paced Loop Framework Program (Sheet 5 of 6)<br />

*******************************************************<br />

* MAIN - Beginning of main program loop<br />

* Loop is executed once every 100 ms (98.4 ms)<br />

* A pass through all major task routines takes<br />

* less than 100mS and then time is wasted until<br />

* MSB of TIC set (every 3 RTIFs = 98.4 ms).<br />

* At each RTIF interrupt, RTIF cleared & RTIFs<br />

* gets decremented (3-0). When RTIFs = 0, MSB of<br />

* TIC gets set and RTIFs is set back to 3.<br />

* (3*32.8/RTIF = 98.4 ms).<br />

*<br />

* The variable TIC keeps track of 100mS periods<br />

* When TIC increments from 9 to 10 it is cleared<br />

* to 0 and TOC is incremented.<br />

*******************************************************<br />

MAIN CLRA ;Kick the watch dog<br />

STA COPR ; if enabled<br />

BRCLR 7,TIC,MAIN ;Loop here till TIC edge<br />

LDA TIC ;Get current TIC value<br />

AND #$0F ;Clears MSB<br />

INCA ;TIC = TIC+1<br />

STA TIC ;Update TIC<br />

CMP #10 ;10th TIC ?<br />

BNE ARNC1 ;If not, skip next clear<br />

CLR TIC ;Clear TIC on 10th<br />

ARNC1 EQU * ;<br />

* End of synchronization to 100 ms TIC; Run main tasks<br />

* & branch back to MAIN within 100 ms. Sync OK as long<br />

* as no 2 consecutive passes take more than 196.8 ms<br />

JSR TIME ;Update TOCs<br />

JSR BLINK ;Blink LED<br />

* Other main tasks would go here<br />

BRA MAIN ;Back to Top for next TIC<br />

** END of Main Loop ***********************************<br />

*******************************************************<br />

* TIME - Update TOCs<br />

* If TIC = 0, increment 0->59<br />

* If TIC not = 0, just skip whole routine<br />

*******************************************************<br />

TIME EQU * ;Update TOCs<br />

TST TIC ;Check for TIC = zero<br />

BNE XTIME ;If not; just exit<br />

INC TOC ;TOC = TOC+1<br />

LDA #60<br />

CMP TOC ;Did TOC -> 60 ?<br />

BNE XTIME ;If not, just exit<br />

CLR TOC ;TOCs rollover<br />

XTIME RTS ;Return from TIME<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

176 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Listing 4. Paced Loop Framework Program (Sheet 6 of 6)<br />

*******************************************************<br />

* BLINK - Update LED<br />

* If TOC is even, light LED<br />

* else turn off LED<br />

*******************************************************<br />

BLINK EQU * ;Update LED<br />

LDA TOC ;If even, LSB will be zero<br />

LSRA ;Shift LSB to carry<br />

BCS LEDOFF ;If not, turn off LED<br />

BSET LED,PORTA ;Turn on LED<br />

BRA XBLINK ;Then exit<br />

LEDOFF BCLR LED,PORTA ;Turn off LED<br />

XBLINK RTS ;Return from BLINK<br />

*******************************************************<br />

* RTIF interrupt service routine<br />

*******************************************************<br />

RTICNT DEC RTIFs ;On each RTIF decrement RTIFs<br />

BNE ENDTOF ;Done if RTIFs not 0<br />

LDA #3 ;RTIFs counts 3->0<br />

STA RTIFs ;Reset TOFS count<br />

BSET 7,TIC ;Set MSB as a flag to MAIN<br />

ENDTOF BSET RTIFR,TSCR ;Clear RTIF flag<br />

AnRTI RTI ;Return from RTIF interrupt<br />

UNUSED EQU AnRTI ;Use RTI at AnRTI for unused<br />

;interrupts to just return<br />

*******************************************************<br />

* Interrupt & reset vectors<br />

*******************************************************<br />

ORG $07F8 ;Start of vector area<br />

TIMVEC FDB RTICNT ;Count RTIFs 3/TIC<br />

IRQVEC FDB UNUSED ;Change if vector used<br />

SWIVEC FDB UNUSED ;Change if vector used<br />

RESETV FDB START ;Beginning of program on reset<br />

The Paced Loop<br />

An Application-Ready Framework<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

177


Freescale Semiconductor, Inc...<br />

The Paced Loop<br />

Review<br />

Freescale Semiconductor, Inc.<br />

Equate (EQU) directives are used to associate a label with a binary<br />

value. The binary value may be an address or a numeric constant.<br />

There are two different ways to equate a control bit, depending upon<br />

how the label will be used. For bit set, clear, and branch instructions, you<br />

want the equate to associate the label with a number between 7 and 0.<br />

For building logical masks, you want the label to be equated to a bit mask<br />

where the bit that is set is in the same bit position as the control bit.<br />

Reset and interrupt vectors should be initialized to form double byte<br />

(FDB) directives. Even if an interrupt source is not going to be used, it is<br />

a good idea to initialize the vector in case an unexpected request is<br />

generated.<br />

Space is reserved in RAM for program variables, using reserve memory<br />

byte (RMB) directives.<br />

The paced loop software structure is a good general-purpose<br />

programming structure. A loop structure is established with a pacemaker<br />

at the top of the loop. The pacemaker triggers and causes the other<br />

instructions in the loop to be executed at regular time intervals such as<br />

every 100 milliseconds. Tasks for an application are written as<br />

subroutines. A list of jump to subroutine (JSR) instructions in the main<br />

paced loop cause each task subroutine to be executed exactly once per<br />

pacemaker trigger.<br />

The routines in the main loop should be designed so the combined<br />

execution time of all routines in the loop is less than the pacemaker<br />

trigger period. An individual pass through the loop can take longer than<br />

the pacemaker trigger, provided the next pass is shorter. Loop<br />

synchronization is maintained as long as no two consecutive passes<br />

through the main loop take longer than twice the pacemaker period.<br />

In the smallest microcontrollers, the number of RAM locations available<br />

is small, so it is important to be aware of stack requirements. An interrupt<br />

requires five bytes of stack RAM and a subroutine call requires two bytes<br />

in an <strong>M68HC05</strong>.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

178 The Paced Loop<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180<br />

Types of Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181<br />

Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181<br />

Serial Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182<br />

Analog-to-Digital Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

Digital-to-Analog Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

EEPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

Controlling Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183<br />

The MC68HC705J1A Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184<br />

A Timer Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187<br />

Using the PWM Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .195<br />

A Practical Motor Control Example . . . . . . . . . . . . . . . . . . . . . . . . . .198<br />

Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198<br />

Motor Control Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201<br />

Motor Control Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204<br />

Listing 6. Speed Control Program Listing . . . . . . . . . . . . . . . . . . .210<br />

Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215<br />

Other Kinds of Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

179


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

To solve real world problems, a microcontroller must have more than just<br />

a powerful CPU, a program, and data memory resources. In addition, it<br />

must contain hardware allowing the CPU to access information from the<br />

outside world. Once the CPU gathers information and processes the<br />

data, it must also be able to effect change on some portion of the outside<br />

world. These hardware devices, called peripherals, are the CPU’s<br />

window to the outside.<br />

On-chip peripherals extend the capability of a microcontroller. An MCU<br />

with on-chip peripherals can do more than one that has only<br />

general-purpose I/O (input/output) ports. Peripherals serve specialized<br />

needs and reduce the processing load on the CPU.<br />

The most basic form of peripheral available on microcontrollers is the<br />

general-purpose I/O port. The MC68HC705J1A has 14 general-purpose<br />

I/O pins that are arranged as a single 8-bit port and a single 6-bit port.<br />

Each of the I/O pins can be used as either an input or an output. The<br />

function of each pin is determined by setting or clearing corresponding<br />

bits in a corresponding data direction register (DDR) during the<br />

initialization stage of a program. Each output pin may be driven to either<br />

a logic 1 or a logic 0 by using CPU instructions to set or clear the<br />

corresponding bit in the port data register. Also, the logic state of each<br />

input pin may be viewed by the CPU by using program instructions.<br />

On-chip peripherals provide an interface to the outside world from the<br />

CPU. Peripherals augment the CPU’s capabilities by performing tasks<br />

that the CPU is not good at. Most microcontroller peripherals perform<br />

very specific functions or tasks. For instance, a peripheral may be<br />

capable of performing frequency and pulse width measurement or it may<br />

generate output waveforms. Because most peripherals do not have any<br />

intelligence of their own, they require some amount of assistance from<br />

the CPU. To prevent peripherals from requiring constant attention from<br />

the CPU, they often perform their functions in an interrupt-driven<br />

manner. A peripheral requests service from the CPU only when it<br />

requires an additional piece of data to perform its job or when a<br />

peripheral has a piece of information that the CPU requires to do its job.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

180 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Types of Peripherals<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

Types of Peripherals<br />

Peripherals can be extremely powerful and can perform complex<br />

functions without any CPU intervention once they are set up. However,<br />

because of the cost sensitivity of most <strong>M68HC05</strong> <strong>Family</strong> members, the<br />

peripherals used on <strong>M68HC05</strong> parts require a fair amount of CPU<br />

intervention.<br />

With the exception of general-purpose I/O ports, most peripherals<br />

perform very specific tasks. These tasks can be diverse and may range<br />

from time measurement and calculation to communication with other<br />

microcontrollers or external peripherals. The following paragraphs<br />

contain a general description of some types of peripherals found on<br />

<strong>M68HC05</strong> microcontrollers.<br />

Timers Even though a wide variety of timers exist on the many members of the<br />

<strong>M68HC05</strong> <strong>Family</strong>, their basic functions relate to the measurement or<br />

generation of time-based events. Timers usually measure time relative<br />

to the internal clock of the microcontroller, although some may be<br />

clocked from an external source. With the number of parts available in<br />

the <strong>M68HC05</strong> <strong>Family</strong>, the capabilities of the timers on each part can vary<br />

greatly. For instance, the most sophisticated timer module present on<br />

the MC68HC05Bx <strong>Family</strong> can simultaneously generate two PWM<br />

outputs, measure the pulse width of two external signals, and generate<br />

two additional output pulse trains. In comparison, the simplest timer<br />

present on the MC68HC05Jx and MC68HC05Kx Families only<br />

generates two periodic interrupts; one at a fixed rate and one at a<br />

selectable rate.<br />

Much more sophisticated timer modules exist on Motorola's higher<br />

power processors. For instance, the MC68332 and MC68HC16Y1<br />

contain a time processing unit (TPU) that is a microcode programmable<br />

time processor with its own ALU (arithmetic logic unit). The TPU was<br />

designed especially for internal combustion engine control and can run<br />

an engine at a steady state with no CPU intervention.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

181


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

Serial Ports Some <strong>M68HC05</strong> <strong>Family</strong> members contain peripherals that allow the<br />

CPU to communicate bit-serially with external devices. Using a bit-serial<br />

format instead of a bit-parallel format requires fewer I/O pins to perform<br />

the communication function.<br />

Two basic types of serial ports exist on <strong>M68HC05</strong> <strong>Family</strong>:<br />

• Serial communications interface (SCI)<br />

• Serial peripheral interface (SPI)<br />

The SCI port is a universal asynchronous receiver transmitter (UART)<br />

that communicates asynchronously with other devices. This type of<br />

serial port requires the simplest hardware interface. Only two pins are<br />

required for bidirectional data transfers. Data is transmitted out of the<br />

MCU on one pin and data is received by the MCU on the other pin. Each<br />

piece of data transmitted or received by the SCI has a start bit, several<br />

data bits, and a stop bit. The start and stop bits are used to synchronize<br />

the two devices that are communicating. This type of serial interface is<br />

used most often when a microcontroller must communicate over fairly<br />

long distances. With RS-232 level translators connected to the transmit<br />

and receive pins, the SCI may be used to communicate with personal<br />

computers and other larger computers.<br />

As the name implies, the SPI port is used primarily to communicate with<br />

inexpensive external peripherals. Because the SPI communicates<br />

synchronously with other devices, bidirectional data transfers require at<br />

least three MCU pins. In addition to one pin each for transmitted and<br />

received data, a third pin provides the synchronization clock for the<br />

communicating devices. This style of serial interface is usually used to<br />

communicate with peripheral devices on the same board as the MCU.<br />

Standard SPI peripherals are available from many manufacturers.<br />

A-to-D converters, display drivers, EEPROM, and shift registers are just<br />

a few examples of available SPI peripherals.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

182 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Analog-to-Digital<br />

Converters<br />

Digital-to-Analog<br />

Converters<br />

On-Chip Peripheral Systems<br />

Controlling Peripherals<br />

As mentioned in What is a Microcontroller?, many signals that exist in<br />

the real world are not directly compatible with an MCU's I/O pins. In fact,<br />

many signals are continuously varying analog signals that cannot be<br />

directly translated into a logic 1 or 0 that the microcontroller can use.<br />

Some members of the <strong>M68HC05</strong> <strong>Family</strong> include an analog-to-digital<br />

(A-to-D) converter that can be used to convert the voltage level of analog<br />

signals into a binary number that the MCU can use.<br />

A digital-to-analog (D-to-A) converter performs just the opposite function<br />

of an A-to-D converter. It allows the MCU to convert a digital number into<br />

a proportional analog voltage or current that can be used to control<br />

various output devices in a system. Later in this chapter, we will develop<br />

an application showing how a D-to-A converter may be implemented<br />

using an on-chip timer and a software program.<br />

EEPROM Since EEPROM is a type of memory, most would not consider it a<br />

peripheral. However, the contents of an EEPROM can be altered as a<br />

program is running, and it is non-volatile memory that is electrically<br />

erasable, so it is certainly in a different class than RAM, ROM, or<br />

EPROM. Several <strong>M68HC05</strong> <strong>Family</strong> members contain EEPROM<br />

memory on the same chip as the MCU. As mentioned previously,<br />

EEPROM may even be added to a system as an external SPI peripheral.<br />

Controlling Peripherals<br />

Freescale Semiconductor, Inc.<br />

The control and status information for peripherals appears to the CPU<br />

as data bits in a memory location. Using this type of arrangement for<br />

peripheral control and status registers is known as memory mapped I/O.<br />

There is a great advantage to having peripherals appear as memory<br />

locations. Any CPU instruction that can operate on a memory location<br />

can be used to control or check the status of a peripheral. This type of<br />

I/O architecture is especially advantageous with the <strong>M68HC05</strong> <strong>Family</strong><br />

because of the CPU's bit manipulation instructions. This group of<br />

instructions gives a programmer the ability to individually set, clear, or<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

183


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

The MC68HC705J1A Timer<br />

Freescale Semiconductor, Inc.<br />

test the state of any bit in the peripheral control registers at addresses<br />

$0000–$00FF.<br />

Depending upon the type and complexity of a peripheral, its associated<br />

control and status registers may occupy one or several locations in the<br />

microcontroller's memory map. For instance, a general-purpose I/O port<br />

occupies two memory locations in a microcontroller's memory map. One<br />

byte location, called the data direction register (DDR), is used to control<br />

the function of each I/O pin. The other byte location, the port data<br />

register, is used to read the state of input pins or assert a logic level on<br />

an output pin. A complex peripheral, such as the timer in the<br />

MC68HC705C8, occupies 10 byte locations in that MCU's memory map.<br />

In The MC68HC705J1A Timer, we take a detailed look at the timer in<br />

the MC68HC705J1A. While this 15-stage multifunction timer is simple<br />

compared to many timer systems, it can perform somewhat<br />

sophisticated timing functions. A complete example is discussed,<br />

showing how this timer system can be used to generate an accurate<br />

low-frequency PWM signal.<br />

Figure 39 shows a block diagram of the MC68HC705J1A's 15-stage<br />

multifunction timer. The timer consists of three connected sections that<br />

each perform separate timing functions.<br />

The timing chain begins with the microcontroller's internal bus-rate<br />

clock, the E-clock. The E-clock is derived by dividing the crystal<br />

frequency by two. The E-clock is used to drive a fixed divide-by-four<br />

prescaler. In turn, the output of the prescaler clocks an 8-bit ripple<br />

counter. The value of this counter may be read by the CPU any time at<br />

memory location $09, the timer counter register (TCR). The counter<br />

value may not be altered by the CPU. This may seem like a simple timer;<br />

however, it is useful in many applications. When the 8-bit ripple counter<br />

overflows from $FF to $00, a timer overflow flag (TOF) status bit in the<br />

timer control and status register (TCSR) is set to a 1. The state of this<br />

status flag may be tested at any time by any of several CPU instructions.<br />

Optionally, if the timer overflow interrupt enable (TOIE) bit in the timer<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

184 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

The MC68HC705J1A Timer<br />

control and status register is set, the ripple counter overflow will<br />

generate a CPU interrupt. Therefore, the timer overflow function allows<br />

a potential interrupt to be generated. The timer overflows every 1024<br />

E-clock cycles (divide by four prescaler followed by an 8-bit, divide by<br />

256-ripple counter).<br />

INTERNAL<br />

PROCESSOR CLOCK<br />

(XTAL ÷ 2)<br />

÷ 2<br />

FIXED<br />

DIVIDE BY<br />

4<br />

LEAST SIGNIFICANT EIGHT BITS OF 15-STAGE RIPPLE COUNTER<br />

MSB LSB<br />

TIMER COUNT REGISTER<br />

TOF RTIF TOFE RTIE TOFR RTIFR RT1 RT0<br />

SERVICE (CLEAR)<br />

COP WATCHDOG<br />

÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2<br />

÷ 2<br />

RTI RATE SELECT<br />

MOST SIGNIFICANT SEVEN BITS OF 15-STAGE RIPPLE COUNTER<br />

÷ 2 ÷ 2 ÷ 2<br />

Figure 39. 15-Stage Multifunction Timer Block Diagram<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

185<br />

S Q<br />

R<br />

÷ 2<br />

÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2<br />

$0009<br />

TCR<br />

$0008<br />

TCSR<br />

COP TIMEOUT<strong>—</strong><br />

GENERATE<br />

INTERNAL MCU RESET


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

Besides providing a potential periodic interrupt, the output of the 8-bit<br />

ripple counter drives the input of an additional 7-bit ripple counter. The<br />

output from any of the last four bits of this counter may be used to<br />

generate an additional periodic interrupt. One of four rates may be<br />

selected by using a 1-of-4 selector controlled by two bits, RT1 and RT0,<br />

in the timer control and status register. Table 17 shows the four real-time<br />

interrupt rates available when operating the microcontroller at an E-clock<br />

frequency of 2.0 MHz.<br />

The final stage of the multifunction timer system has a 3-bit counter that<br />

forms the computer operating properly (COP) watchdog system. The<br />

COP system is meant to protect against software failures. When<br />

enabled, a COP reset sequence must be performed before the timeout<br />

period expires so that the COP does not time out and initiate an MCU<br />

reset. To prevent the COP from timing out and generating an MCU reset,<br />

bit 0 at memory location $07F0 (COPR) must be written to 0 before the<br />

COP reset period has expired. Because the input of the COP watchdog<br />

timer is clocked by the output of the real-time interrupt circuit, changing<br />

the RTI rate will affect the minimum COP reset period. Table 17 shows<br />

the four COP reset periods available for corresponding RTI rates.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Table 17. RTI and COP Timer Rates (E Clock = 2 MHz)<br />

RT1 RT0 RTI Rate Minimum COP Reset Period<br />

0 0 8.2 ms 57.3 ms<br />

0 1 16.4 ms 114.7 ms<br />

1 0 32.8 ms 229.4 ms<br />

1 1 65.5 ms 458.8 ms<br />

186 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

A Timer Example<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

A Timer Example<br />

In this section we will develop software that uses both the real-time<br />

interrupt and the timer overflow interrupt to produce a low-frequency<br />

pulse width modulated (PWM) signal on a general-purpose I/O pin.<br />

PWM signals are useful for a variety of control functions. They may be<br />

used to control the speed of a motor or can be easily converted to a dc<br />

level to drive an analog output device or to form part of an A-to-D<br />

converter.<br />

A PWM signal, as the name implies, has a fixed frequency but varies the<br />

width of the on and off times. Figure 40 shows three PWM signals with<br />

different duty cycles. For each signal, the waveform period T1 is<br />

constant but the on time varies (the period of time shown by T2). Duty<br />

cycle is usually expressed as a percentage (the ratio of T2 to T1).<br />

T2<br />

T2<br />

T1<br />

T1<br />

T2<br />

DUTY CYCLE = T2/T1 = 50%<br />

DUTY CYCLE = T2/T1 = 20%<br />

DUTY CYCLE = T2/T1 = 80%<br />

Figure 40. PWM Waveforms with Various Duty Cycles<br />

To generate an accurate PWM signal, two timing references are<br />

required. One timing reference sets the constant frequency of the PWM<br />

signal while the second determines the amount of time that the PWM<br />

output remains high.<br />

The basic strategy for the PWM software we will develop is as follows. A<br />

real-time interrupt (RTIF) will be used to generate the PWM period, and<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

187<br />

T1


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

÷ 2<br />

RTI RATE SELECT<br />

Freescale Semiconductor, Inc.<br />

the timer overflow (TOF) will be used to determine the PWM high time.<br />

The rest of this chapter is a detailed development of this basic idea into<br />

a working application.<br />

Begin by taking a closer look at the MC68HC705J1A's timer. Figure 41<br />

shows the timer redrawn to emphasize the portion that we are interested<br />

in. Conceptually, the eight counter stages surrounded by the gray box<br />

form the timer that we will use to generate our PWM signal.<br />

Examination of Figure 41 shows four counter stages between the timer<br />

overflow interrupt output and the first input to the RTI rate select<br />

multiplexer. This indicates that timer overflow interrupts will occur at a<br />

rate 16 times faster than the fastest selectable real-time interrupt. Using<br />

the RTI to generate the base frequency of a PWM signal and the TOF<br />

interrupt to determine the duty cycle, we would be able to generate a<br />

PWM output with 16 discrete duty cycles (including 100%) as shown in<br />

Figure 42. The numbers down the lefthand side of the figure indicate the<br />

number of TOF interrupts that will occur before the PWM output is set<br />

low. The numbers down the righthand side of the figure indicate the duty<br />

cycle of the waveform. The alert reader will note that there is no TOF<br />

interrupt count associated with the 100% duty cycle waveform. As will be<br />

shown later, this is a special case that must be tested in the RTI routine.<br />

÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2 ÷ 2<br />

Figure 41. Portion of the MC68HC705J1A Timer<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MSB LSB<br />

TIMER COUNT REGISTER<br />

TOF RTIF TOFE RTIE TOFR RTIFR RT1 RT0<br />

188 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

TCSR


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

<strong>—</strong><br />

RTI INTERRUPTS<br />

TOF INTERRUPTS<br />

On-Chip Peripheral Systems<br />

A Timer Example<br />

6.25%<br />

12.5%<br />

18.75%<br />

25.0%<br />

31.25%<br />

37.5%<br />

43.75%<br />

50.0%<br />

56.25%<br />

62.5%<br />

68.75%<br />

75.0%<br />

81.25%<br />

87.5%<br />

93.75%<br />

100%<br />

Figure 42. PWM With 16 Discrete Duty Cycle Outputs<br />

While the software to implement the illustrated PWM output is simple,<br />

having only 16 choices for pulse width limits the usefulness of this PWM<br />

to a small number of applications (where accurate control is not<br />

necessary). For example, if a motor speed control system was built<br />

using this PWM, the target speed could only be controlled to 6.25%<br />

(assuming that motor speed is directly proportional to the average<br />

applied voltage). For most motor speed control applications, a 12.5%<br />

variation in rotation speed would be unacceptable.<br />

Obviously, much finer control of the PWM duty cycle is desired. One<br />

approach might be to use a slower RTI rate. Using a slower RTI rate<br />

would result in a greater number of TOF interrupts for each RTI. For<br />

some applications, this may be an acceptable solution. However, for<br />

many applications the resulting frequency of the PWM waveform would<br />

be too low to be of practical use. Table 18 shows the four available RTI<br />

rates and the corresponding PWM frequency, the number of TOF<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

189


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

interrupts between RTIs, and the minimum variation in duty cycle that is<br />

possible.<br />

Table 18 seems to suggest that we are stuck trading off PWM frequency<br />

for duty cycle accuracy. However, the following software program can<br />

deliver much better results than expected.<br />

Re-examining the portion of the timer in Figure 41 surrounded by the<br />

gray box shows eight bits of the 15-bit timer chain. Four of the bits are<br />

accessible to the CPU as the upper four bits of the TCR. The other four<br />

bits form a divide-by-16 counter chain whose value is not directly<br />

accessible. However, by counting the number of TOF interrupts that<br />

occur after each RTI, we can always know the state of these four counter<br />

bits. By using an 8-bit number to represent the PWM duty cycle, we can<br />

achieve a duty cycle accuracy of 1 ÷ 255 or 0.4%.<br />

To get this level of control with the MC68HC705J1A timer, we cannot<br />

use an 8-bit duty cycle value directly. The 8-bit number must be<br />

separated into two components. One component will represent the value<br />

of the inaccessible four bits of the counter (the number of TOF interrupts<br />

that occur after each RTI). The other component will represent the value<br />

of the upper four bits of the TCR (the lower four bits of our counter that<br />

are directly accessible to the CPU).<br />

To make these two components easy for the software to use, the upper<br />

four bits of the desired PWM duty cycle must be placed in the lower four<br />

bits of a variable we will call PWMCoarse. This value will be used to<br />

determine during which TOF interrupt the PWM output should be set<br />

low. The lower four bits of the desired PWM duty cycle will be placed in<br />

the upper four bits of a variable we will call PWMFine. This value is used<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Table 18. PWM Characteristics for Various RTI Rates<br />

RTI Rate<br />

PWM<br />

Frequency<br />

TOF<br />

Interrupts<br />

Minimum Duty<br />

Cycle<br />

8.2 ms 122 Hz 16 6.25%<br />

16.4 ms 61.0 Hz 32 3.125%<br />

32.8 ms 30.5 Hz 64 1.56%<br />

65.5 ms 15.3 Hz 128 0.78%<br />

190 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

A Timer Example<br />

within the TOF interrupt to determine precisely when during the TOF<br />

interrupt the PWM output should be set low. By comparing the value in<br />

PWMFine to the upper four bits of the TCR, we can effectively divide<br />

each TOF interrupt into 16 separate time intervals as shown in<br />

Figure 43.<br />

RTI INTERRUPTS<br />

TOF INTERRUPTS<br />

Figure 43. Each TOF Interrupt Sliced into 16 Separate<br />

Time Intervals<br />

Now that we have described the theory involved in generating an<br />

accurate PWM waveform using the MC68HC05J1A's timer, the next<br />

step is to write the software. We begin by generating flowcharts to<br />

describe the actions necessary to produce the PWM waveform and<br />

finish by translating the flowcharts into <strong>M68HC05</strong> assembly language.<br />

The flowcharts in Figure 44, Figure 45, and Figure 46 describe the<br />

PWM software. The flowchart in Figure 45, although simple, is included<br />

for completeness and clarity. Because the MC68HC05J1A contains only<br />

one timer interrupt vector, a short routine must determine whether a<br />

timer interrupt was caused by a TOF or an RTIF interrupt and then<br />

branch to the appropriate service routine.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

191<br />

RTI<br />

TOF INTERRUPTS


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

BEGIN TIMER<br />

INTERRUPT SERVICE<br />

TOF INTERRUPT ?<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Figure 44. Timer Interrupt Service Routine<br />

As shown in Figure 45, the RTIF interrupt routine checks for two special<br />

conditions, 0% and 100% duty cycle. It then sets up the PWMFine and<br />

PWMCoarse variables for use by the TOF interrupt service routine. If a<br />

0% duty cycle is desired, the PWM output is set low and the RTIF<br />

interrupt service routine immediately returns. If a 100% duty cycle is<br />

desired, the PWM output is set high and the RTIF interrupt service<br />

routine will return immediately. If a duty cycle between 0% and 100% is<br />

desired, the variable DesiredPWM is split into the two components,<br />

PWMFine and PWMCoarse. If the resulting value of PWMCoarse is 0<br />

the program will jump to the second part of the TOF interrupt routine,<br />

which continually compares the value in PWMFine to the upper four bits<br />

of the TCR. If the value of PWMCoarse is not 0, TOF interrupts are<br />

enabled and the RTIF interrupt routine returns.<br />

192 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

NO<br />

RTIF INTERRUPT ?<br />

RETURN<br />

NO<br />

YES<br />

YES<br />

GO EXECUTE TOF<br />

INTERRUPT ROUTINE<br />

(FIGURE 46)<br />

GO EXECUTE RTIF<br />

INTERRUPT ROUTINE<br />

(FIGURE 45)


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

EXECUTE SECOND<br />

PART OF TOF INTERRUPT<br />

A<br />

SEE FIGURE 46<br />

YES<br />

BEGIN RTIF<br />

INTERRUPT ROUTINE<br />

RESET RTIF<br />

INTERRUPT FLAG<br />

DesiredPWM = 0% ?<br />

On-Chip Peripheral Systems<br />

A Timer Example<br />

Figure 45. Real-Time Interrupt Routine Flowchart<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

193<br />

NO<br />

PWM OUTPUT = 1<br />

DesiredPWM = 100% ?<br />

NO<br />

PWMFine = DesiredPWM 4<br />

PWMCourse = 0 ?<br />

NO<br />

CLEAR TOF INTERRUPT FLAG<br />

ENABLE TOF INTERRUPTS<br />

RETURN FROM<br />

INTERRUPT<br />

YES<br />

YES<br />

PWM OUTPUT = 0


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

The flowchart in Figure 46 describes the actions required for the TOF<br />

interrupt routine. The first action is to decrement the value of<br />

PWMCoarse. When PWMCoarse becomes 0, it means that the value in<br />

the upper four bits of our counter is equal to the upper four bits of<br />

DesiredPWM. Next, we continually compare the upper four bits of the<br />

TCR with the value of PWMFine (which is the lower four bits of<br />

DesiredPWM). When these two values match, the PWM output is set<br />

low, the TOF interrupt is reset and disabled, and the TOF interrupt<br />

returns.<br />

FROM<br />

FIGURE 45<br />

BEGIN TOF<br />

INTERRUPT ROUTINE<br />

PWMCourse = 0 ?<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

A<br />

PWMCourse = PWMCourse – 1<br />

Figure 46. Timer Overflow Interrupt Flowchart<br />

Listing 5. PWM Program Listing shows the assembly language listing<br />

for the three routines described by the flowcharts in Figure 44,<br />

Figure 45, and Figure 46. The translation of the flowcharts into<br />

assembly language is fairly straightforward. The possible exception is<br />

the assembly code in the RTIF interrupt routine that splits the<br />

194 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

YES<br />

TCR ≥ PWMFine ?<br />

YES<br />

SET PWM OUTPUT = 0<br />

RESET & DISABLE<br />

TOF INTERRUPT<br />

RETURN FROM<br />

INTERRUPT<br />

NO<br />

NO<br />

RESET TOF<br />

INTERRUPT FLAG


Freescale Semiconductor, Inc...<br />

Using the PWM<br />

Software<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

A Timer Example<br />

DesiredPWM variable into the PWMCoarse and PWMFine components.<br />

This routine works by using a combination of shift left and rotate left<br />

instructions that operate on the A and the X registers. The LSLA<br />

instruction shifts the most significant bit of the A register into the carry<br />

and a 0 into the least significant bit of A. The ROLX instruction places<br />

the carry (from the previous LSLA instruction) into the least significant bit<br />

of the X register. After the execution of four of these instruction pairs, the<br />

four most significant bits of the A register (DesiredPWM) will end up in<br />

the least significant four bits of the X register (PWMCoarse). The least<br />

significant four bits of the A register will end up in the most significant<br />

four bits of the A register (PWMFine).<br />

In normal circumstances, the PWM software of Listing 5. PWM<br />

Program Listing would be used as a part of a larger program. The value<br />

of DesiredPWM would be generated by some other part of the main<br />

program. To demonstrate the PWM software, the value of DesiredPWM<br />

was arbitrarily set to $80 (12810) by program instructions. If a simulator<br />

or emulator is used to study this program, you can change the value of<br />

DesiredPWM and observe the effect.<br />

The PWM program is interrupt driven. This means that the timer<br />

generates interrupt requests for the CPU to stop processing the main<br />

program and respond to the interrupt request. Since the demonstration<br />

version of this program in Listing 5. PWM Program Listing has no<br />

other main program to perform, a “branch to here” instruction was<br />

included after the clear interrupt mask (CLI) instruction. This instruction<br />

is an infinite loop. Timer interrupts will cause the CPU to periodically<br />

leave this infinite loop to respond to the timer requests and then return<br />

to executing the infinite loop.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

195


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Listing 5. PWM Program Listing (Sheet 1 of 2)<br />

Listing 5. PWM Program Listing<br />

;Equates for all 705J1 are included but not shown<br />

; in this listing<br />

;<br />

0000 Percent100 EQU $FF ;DesiredPWM value for 100% duty<br />

0000 PWM EQU PA7 ;PWM output on port A bit 7<br />

; ;update the DesiredPWM variable.<br />

00C0 ORG RAMStart<br />

00C0 DesiredPWM RMB 1 ;Desired PWM duty cycle...<br />

; expressed as the numerator of DesiredPWM/255.<br />

; 0 = continuous low 255 = continuous high.<br />

00C1 PWMCoarse RMB 1 ;Number of TOF interrupts...<br />

; before we start to compare PWMFine to value in the TCR.<br />

00C2 PWMFine RMB 1 ;When TCR matches PWMFine,...<br />

; ; the PWM is set low.<br />

; PWMFine is derived from the lower 4 bits of DesiredPWM.<br />

; These 4 bits are placed in the upper 4 bits of PWMFine.<br />

00C3 VarEnd EQU *<br />

Freescale Semiconductor, Inc.<br />

;********************************************************<br />

;<br />

0300 ORG ROMStart<br />

;<br />

0300 Start EQU *<br />

0300 9C RSP ;Reset the stack pointer<br />

0301 3F00 CLR PORTA ;Set Port A outputs to all 0's<br />

0303 A6FF LDA #$FF ;Make all Port A's pins outputs<br />

0305 B704 STA DDRA<br />

;Clear out all of RAM<br />

0307 AEC0 LDX #RAMStart ;Point to the start of RAM<br />

0309 7F ClrLoop CLR ,X ;Clear a byte<br />

030A 5C INCX ;Point to the next location<br />

;Cleared the last location?<br />

030B 26FC BNE ClrLoop ;No, Continue to clear RAM<br />

030D A680 LDA #$80 ;Corresponds to 50% (128/255)<br />

030F B7C0 STA DesiredPWM ;Establish a PWM duty cycle<br />

0311 A61C LDA #$1C ;Clear timer ints...<br />

0313 B708 STA TSCR ;and enable RTIF interrupt<br />

0315 9A CLI ;Enable interrupts<br />

0316 20FE BRA * ;Infinite loop, PWM uses ints<br />

;********************************************************<br />

;RTI sets period. @2MHz & RT1:RT0 = 0:0, period = 8.192 ms<br />

;or about 122 Hz<br />

0318 TimerInt EQU *<br />

0318 0E0804 BRSET TOF,TSCR,TOFInt ;TOF interrupt?<br />

031B 0C0812 BRSET RTIF,TSCR,RTIInt ;RTI interrupt?<br />

031E 80 RTI<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

196 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Listing 5. PWM Program Listing (Sheet 2 of 2)<br />

On-Chip Peripheral Systems<br />

A Timer Example<br />

;********************************************************<br />

;TOF interrupt response - Decrement PWMCoarse, when 0...<br />

;Compare PWMFine to TCR. When TCR passes PWMFine clear<br />

;PWM output pin and disable further TOF. RTI re-enables.<br />

;<br />

031F TOFInt EQU *<br />

031F 3AC1 DEC PWMCoarse ;Is PWMCoarse=0?<br />

0321 260A BNE ExitTOF ;No. Clear TOF and return<br />

0323 B6C2 TOFInt1 LDA PWMFine ;To compare to upper 4 of TCR<br />

0325 B109 CmpMore CMPA TCR<br />

0327 22FC BHI CmpMore ;Loop till PWMFine


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

Freescale Semiconductor, Inc.<br />

In this section, we will develop a practical application by expanding some<br />

of the software developed in this book. The example will add some<br />

external hardware to the MC68HC705K1 so that we can observe the<br />

effects of our software on the world outside the microcontroller. We will<br />

use a slightly modified version of the PWM routine that was developed<br />

in this chapter to control the speed of a small permanent-magnet direct<br />

current (DC) motor. In addition, we will use the concepts developed in<br />

the chapter titled On-Chip Peripheral Systems that allow the CPU to<br />

read the state of switches connected to the MCU’s general-purpose I/O<br />

pins.<br />

Theory DC motors are often the best choice for variable-speed motor<br />

applications. Brush DC motors are the easiest to control electronically.<br />

Electronic control of brushless DC, stepper, AC induction, and switched<br />

reluctance motors all require more-complex control circuits in addition to<br />

more power-switching devices. <strong>Small</strong>, low-cost brush DC motors are<br />

available off the shelf for many low-volume applications where custom<br />

designs would be too expensive. The reliability of brush motors is<br />

adequate for most applications. However, eventually, the brushes will<br />

wear out and need to be replaced.<br />

To vary the speed of a brush DC motor, we must vary the voltage that is<br />

applied to the motor. Several approaches can be used to accomplish<br />

this. We will examine several of the methods, explaining the major<br />

advantages and disadvantages of each.<br />

The first and most obvious approach to varying the voltage applied to a<br />

motor might be to place a variable resistor in series with the motor and<br />

the power source, as shown in Figure 47. While this approach is very<br />

simple, it has some serious disadvantages. First, the resistor’s power<br />

dissipation capabilities must be matched to the power requirements of<br />

the motor. For very small fractional-horsepower DC motors, the size of<br />

the variable resistor will be quite modest. However, as the size of the<br />

motor increases, the motor’s power requirement increases and the size<br />

and cost of the variable resistor will increase.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

198 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

MOTOR<br />

Figure 47. Motor Speed Controlled by a Variable Resistor<br />

The second major disadvantage of this type of speed control is the<br />

inability to automatically adjust the speed of the motor to compensate for<br />

varying loads. This is a primary disadvantage for applications that<br />

require precise speed control under varying mechanical loads.<br />

An electronic variation of the variable resistor form of speed control is<br />

shown in Figure 48. In this arrangement, we have replaced the variable<br />

resistor with a transistor. Here, the transistor is operated in its linear<br />

mode. When a transistor operates in this mode, it essentially behaves as<br />

an electrically controlled variable resistor. By applying a proportional<br />

analog control signal to the transistor, the "resistivity" of the transistor<br />

can be varied, which will in turn vary the speed of the motor. By using a<br />

transistor to control the speed of the motor in this manner, the magnitude<br />

of the control signal is reduced to much lower voltage and current levels<br />

that can be readily generated by electronic circuity.<br />

Figure 48. Motor Speed Controlled by a Transistor<br />

Unfortunately, using a transistor in its linear mode still retains a major<br />

disadvantage of using a variable resistor. Like a variable resistor, a<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

199<br />

R B<br />

V BB<br />

M<br />

M<br />

MOTOR


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

power transistor operating in its linear region will have to dissipate large<br />

amounts of power under varying speed and load conditions. Even<br />

though power transistors capable of handling high power levels are<br />

widely available at relatively modest prices, the power dissipated by the<br />

transistor will usually require a large heat sink to prevent the device from<br />

destroying itself.<br />

In addition to being operated as a linear device, transistors also may be<br />

operated as electronic switches. By applying the proper control signal to<br />

a transistor, the device will either be turned on or turned off. As shown in<br />

Figure 49, when the transistor is turned on, it will essentially behave as<br />

a mechanical switch allowing electric current to pass through it and its<br />

load virtually unimpeded. When turned off, no current passes through<br />

the transistor or its load. Because the transistor dissipates very little<br />

power when it is fully turned on or saturated, the device operates in an<br />

efficient manner.<br />

TRANSISTOR “ON”<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

V CC<br />

R B<br />

V BB<br />

M<br />

Figure 49. Transistor Used as an Electronic Switch<br />

It would seem that, when using a transistor to control the speed of a DC<br />

motor, we are stuck using the device in its inefficient linear mode if we<br />

want a motor to operate at something other than full speed. Fortunately,<br />

there is an alternative method of controlling the speed of a DC motor<br />

using a transistor. By using the transistor as an electronically controlled<br />

switch and applying a PWM control signal of sufficient frequency, we can<br />

control the speed of the motor. To help understand how turning a motor<br />

200 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

I C<br />

V CE ≅ 0 VOLTS<br />

V CC<br />

R B<br />

V BB = 0<br />

M<br />

TRANSISTOR “OFF”<br />

I C = 0<br />

V CE = V CC


Freescale Semiconductor, Inc...<br />

Motor Control<br />

Circuit<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

fully on and then fully off can control its speed, consider the PWM<br />

waveforms in Figure 50.<br />

+5 VOLTS<br />

0 VOLTS<br />

+5 VOLTS<br />

0 VOLTS<br />

a) DUTY CYCLE = T2/T1 = 50%<br />

b) DUTY CYCLE = T2/T1 = 80%<br />

Figure 50. PWM Waveforms with 50 and 80 Percent Duty Cycles<br />

Figure 50(a) shows a single cycle of a 50 percent duty cycle PWM<br />

waveform that is 5 volts during the first half of its period and at 0 volts<br />

during the second half. If we integrate (or average) the voltage of the<br />

PWM waveform in Figure 50(a) over its period, T1, the average DC<br />

voltage is 50 percent of 5 volts or 2.5 volts. Correspondingly, the<br />

average DC voltage of the PWM waveform in Figure 50(b), which has a<br />

duty cycle of 80 percent, is 80 percent of 5 volts or 4.5 volts. By using a<br />

PWM singal to switch a motor on and off in this manner, it will produce<br />

the same effect as applying a continuous or average DC voltage at<br />

varying levels to the motor. The frequency of the PWM signal must be<br />

sufficiently high so that the rotational inertia of the motor integrates the<br />

on/off pulses and causes the motor to run smoothly.<br />

As mentioned earlier, we will be using a slightly modified version of our<br />

PWM routine to control the speed of a small motor. However, before<br />

discussing the software involved, we need to take a look at the hardware<br />

components required to drive the motor.<br />

Figure 51 is a schematic diagram of the power section of our motor<br />

control circuit. There are a number of differences between this<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

201<br />

T2<br />

T2<br />

T1<br />

T1


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

schematic and the conceptual ones used in Figure 48 and Figure 49.<br />

We will describe these differences in the following paragraphs.<br />

The most noticeable difference is the schematic symbol for the power<br />

transistor that will be used as an electronic switch. This device is a power<br />

MOSFET. Unlike the bipolar transistor shown in Figure 48 and<br />

Figure 49, this special type of transistor is controlled by the magnitude<br />

of a voltage applied to its gate. Additionally, this particular power<br />

MOSFET, the MTP3055EL, may be completely saturated with only 5<br />

volts applied to its gate. These two characteristics allow this device to be<br />

controlled directly by a microcontroller’s output pin for many<br />

applications.<br />

Because the input iimpedance of a power MOSFET is very high (greater<br />

than 40 megaohms), a 10 KΩ resistor is placed between the MOSFET<br />

gate and ground to prevent erratic operation of the motor should the<br />

connection between the microcontroller and the gate ever become cut.<br />

The 15-volt zener diode is placed in parallel with the resistor to protect<br />

the gate of the MOSFET from possible damage from high voltage<br />

transients that may be generated in the system. The 1N4001 diode in<br />

parallel with the motor is used to snub the inductive kick of the motor<br />

each time the MOSFET is turned off. The 0.1-μf capacitor in parallel with<br />

the motor is used to reduce the electrical noise generated by the motor’s<br />

brushes.<br />

For further information on designing with power MOSFETs, it is<br />

suggested that the reader study the Theory and Applications section of<br />

the Motorola Power MOSFET Transistor Data Book (DL153).<br />

Figure 52 is a schematic diagram of the microprocessor section of the<br />

circuit that we will be using in this example. In addition to generating a<br />

PWM output, the MC68HC705K1 is reading three momentary<br />

pushbutton switches connected to its I/O pins. As the schematic shows,<br />

a single switch turns the motor on and off while two switches set the<br />

speed of the motor.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

202 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

Figure 51. Power Section of the Motor Speed Control Circuit<br />

MOTOR<br />

CONTROL<br />

SWITCHES<br />

ON/OFF<br />

SPEED<br />

DOWN<br />

SPEED<br />

UP<br />

FROM PA7 OF<br />

MC68HC705K1<br />

10 k<br />

(5)<br />

+5 V<br />

Figure 52. Microcontroller Section<br />

of the Motor Speed Control Circuit<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

203<br />

10 k<br />

0.1 F<br />

IRQ<br />

PA0<br />

PA1<br />

PA2<br />

15 V<br />

V SS<br />

+5 V<br />

M<br />

MC68HC705K1<br />

RESET<br />

1N4001<br />

MTP3055EL<br />

OSC1<br />

OSC2<br />

PA7<br />

V DD<br />

10 M<br />

4 MHz<br />

+5 V<br />

TO GATE OF<br />

MTP3055EL<br />

0.1 F<br />

27 pF<br />

27 pF


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Motor Control<br />

Software<br />

Freescale Semiconductor, Inc.<br />

One side of each switch is connected to circuit ground, while the other<br />

side of the switch is connected to an I/O pin on the MC68HC705K1<br />

microcontroller. Each of the input pins on the microcontroller is "pulled<br />

up" through a 10-kΩ resistor to +5 volts. These 10-kΩ pullup resistors<br />

keep each of the three input pins at a logic 1 when the pushbutton<br />

switches are not pressed.<br />

In this exampel circuit, the switch controls will operate in the following<br />

manner. The motor on/off switch operates as an alternate-action control.<br />

Each time the switch is pushed and released, the motor will alternately<br />

be turned on or off. When the motor is turned on, its speed will be set to<br />

the speed it was going the last time the motor was on.<br />

The speed up and speed down switches increase or decrease motor<br />

speed, respectively. To increase or decrease the speed of the motor, the<br />

respective switch must be pressed and held. The motor speed PWM will<br />

be increased or decreased at a rate of approximately 0.4 percent every<br />

24 ms. This "ramp" rate will allow the motor speed to be adjusted across<br />

its entire speed range in approximately six seconds.<br />

Figure 53 shows a flowchart that describes the new RTI interrupt<br />

software. The only functional change to the PWM routine developed<br />

earlier in this chapter is the addition of one instruction at the beginning<br />

of the RTI interrupt service routine. This instruction decrements the<br />

variable RTIDlyCnt. This variable is used by the three routines that read<br />

the input switches to develop a switch debounce delay.<br />

As mentioned in the Programming chapter, there are usually many<br />

ways to perform a specific task using the microcontroller’s instruction<br />

set. To demonstrate this, one part of the revised RTI interrupt routine has<br />

been impmlemented in a slightly different manner. Remember, looking<br />

at Listing 6. Speed Control Program Listing, that we had to split the<br />

variable DesiredPWM into two parts, PWMFine and PWMCoarse. To do<br />

this, we used a combination of shifts and rotates to place the upper four<br />

bits of the A accumulator (DesiredPWM) into the lower four bits of the X<br />

register (PWMCoarse) and the lower four bits of A into the upper four bits<br />

of A (PWMFine). This method required nine bytes of program memory<br />

and 26 CPU cycles.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

204 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

GO EXECUTE SECOND<br />

PART OF TOF INTERRUPT<br />

A<br />

see Figure 9-8<br />

BEGIN RTIF<br />

INTERRUPT ROUTINE<br />

RESET RTIF<br />

INTERRUPT FLAG<br />

DesiredPWM = 0% ?<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

Figure 53. Revised RTI Routine Flowchar<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

205<br />

YES<br />

RTIDlyCnt = RTIDlyCnt – 1<br />

NO<br />

PWM OUTPUT = 1<br />

DesiredPWM = 100% ?<br />

NO<br />

PWMFine = DesiredPWM 4<br />

PWMCourse = 0 ?<br />

NO<br />

CLEAR TOF INTERRUPT FLAG<br />

ENABLE TOF INTERRUPTS<br />

RETURN FROM<br />

INTERRUPT<br />

YES<br />

YES<br />

PWM OUTPUT = 0


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

By using the alternative approach in Listing 6. Speed Control<br />

Program Listing, we can get the same result in only three bytes of<br />

program memory and 13 CPU cycles.<br />

The RTIInt routine in Listing 6. Speed Control Program Listing<br />

demonstrates the alternative approach. The original 9-byte instrtuction<br />

sequence has been replaced with two instructions, LDX #16 and MUL.<br />

The MUL instruction multiples the value in the accumulator by the value<br />

in the index register and places the result in X:A (concatenation of X and<br />

A). Multiplying a binary number by 16 is equivalent to shifting the value<br />

left by four positions. Just as in the original implementation, the upper<br />

four bits of DesiredPWM are now in the lower four bits of the X register<br />

(PWMCoarse) and the lower four bits of the A register have been moved<br />

into the upper four bits a A (PWMFine).<br />

The flowchart in Figure 54 describes the main loop routine of our motor<br />

control module. This module checks the state of each of the three input<br />

switches. If any one of the three switches is pressed, a routine that<br />

handles the actions for that switch is called. If there are no switches<br />

pressed, the main loop is repeated.<br />

BEGIN<br />

MAIN PROGRAM<br />

MOTOR ON/OFF SW. PRESSED ?<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

Figure 54. Flowchart for Main Program Loop<br />

206 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

NO<br />

SPEED UP SW. PRESSED ?<br />

NO<br />

SPEED DOWN SW. PRESSED ?<br />

NO<br />

YES<br />

YES<br />

YES<br />

TURN MOTOR ON / OFF<br />

INCREASE MOTOR SPEED<br />

DECREASE MOTOR SPEED


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

Figure 55, Figure 56, and Figure 57 are flowcharts for the three<br />

routines that handle the actions of the three input switches. Each of<br />

these routines begins with the execution of a 50-ms switch debounce<br />

routine. As decribed in the Programming chapter, this delay is required<br />

because the mechanical bounce produced by the closure of a switch is<br />

seen by the microcontroller as multiple switch closures during the first<br />

several milliseconds after the switch is pressed. This small section of<br />

code stores the value DebounceDly into the variable RTIDlyCnt and then<br />

waits until the value is decremented to zero by the RTI interrupt service<br />

routine. When the value reaches zero, the switch is again checked to be<br />

sure a valid switch closure occurred. The value used for the delay<br />

constant (DebounceT) will produce a minimum delay of approximately<br />

50 milliseconds.<br />

The flowchart in Figure 55 describes the MotorOnOff routine. It is<br />

responsible for handling the actions of the alternate action switch that<br />

turns the motor on and off. After the switch debounce delay, this routine<br />

waits until the on/off switch is released before it performs the rest of its<br />

task and returns to the main loop. Otherwise, the main loop would detect<br />

another switch closure as soon as the MotorOnOff program finished and<br />

returned to the main program loop.<br />

The routines described by the flowcharts in Figure 56 and Figure 57<br />

operate in essentially the same manner. First, each of these routines<br />

checks to see if the motor is currently turned on. If the motor is off, the<br />

routine returns to the main program loop. Each routine then loops<br />

continuously as long as its associated switch remains pressed. Each<br />

time through the loop, the MotorPWM and DesiredPWM variables are<br />

incremented or decremented to increase or decrease the duty cycle of<br />

the PWM output. To keep the speed of the motor from increasing or<br />

decreasing too rapidly when a switch is pressed, a delay of<br />

approprixmately 25 ms is inserted each time through the loop. This<br />

25-ms delay allows the motor to be adjusted across its entire speed<br />

range in approximately six seconds.<br />

Listing 6. Speed Control Program Listing contains the assembly<br />

language listing for the routines described by the flowcharts in Figure 46<br />

and Figure 53 through Figure 57.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

207


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

TURN MOTOR<br />

ON OR OFF<br />

MOTOR ON/OFF SW. PRESSED ?<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

NO<br />

DEBOUNCE FOR 50 ms<br />

Figure 55. Flowchart for MotorOn/Off Routine<br />

208 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

YES<br />

MOTOR ON/OFF SW. PRESSED ?<br />

NO<br />

MOTOR ON ?<br />

YES<br />

TURN MOTOR OFF<br />

BY SETTING<br />

DesiredPWM = 0<br />

RETURN<br />

NO<br />

YES<br />

TURN MOTOR ON<br />

BY SETTING<br />

DesiredPWM = LAST SPEED


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

NO<br />

INCREASE<br />

MOTOR SPEED<br />

SPEED UP SW. PRESSED ?<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

Figure 56. Flowchart for Motor Speed-Up Routine<br />

NO<br />

NO<br />

MOTOR ON ?<br />

DEBOUNCE FOR 50 ms<br />

Figure 57. Flowchart for Motor Speed-Down Routine<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

209<br />

YES<br />

MOTOR PWM AT 100% ?<br />

NO<br />

RETURN<br />

YES<br />

YES<br />

DECREASE<br />

MOTOR SPEED<br />

MOTOR ON ?<br />

DEBOUNCE FOR 50 ms<br />

SPEED DOWN SW. PRESSED ?<br />

YES<br />

MOTOR PWM AT MinPWM ?<br />

RETURN<br />

YES<br />

YES<br />

NO<br />

NO<br />

DELAY FOR ABOUT 25 ms<br />

MotorPWM = MotorPWM + 1<br />

DesiredPWM = DesiredPWM +1<br />

DELAY FOR ABOUT 25 ms<br />

MotorPWM = MotorPWM – 1<br />

DesiredPWM = DesiredPWM –1


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Listing 6. Speed Control Program Listing (Sheet 1 of 5)<br />

Listing 6. Speed Control Program Listing<br />

;Equates for all 705K1 are included but not shown<br />

00FF Percent100 EQU $FF ;DesiredPWM value for 100% duty<br />

0003 RampTime EQU 3 ;Speed up/down ramp constant<br />

0007 DebounceT EQU 7 ;Switch debounce constant<br />

0010 MinPWM EQU $10 ;Minimum PWM value.<br />

0007 PWM EQU PA7 ;Port A bit 7 is PWM output<br />

0000 MotorOnOff EQU PA0 ;Sw. for the Motor On/Off<br />

0001 SpeedUp EQU PA1 ;Sw. for raising the speed<br />

0002 SpeedDn EQU PA2 ;Sw. for lowering the speed<br />

00E0 ORG RAMStart<br />

00E0 DesiredPWM RMB 1 ;Desired PWM/255 = duty cycle<br />

;0 = continuous low<br />

;255 = continuous high<br />

00E1 PWMCoarse RMB 1 ;Number of TOFs before...<br />

;start watching PWMFine vs TCR<br />

00E2 PWMFine RMB 1 ;When TCR matches PWMFine,...<br />

;set PWM output low<br />

00E3 MotorPWM RMB 1 ;Last PWM/speed while motor.on<br />

00E4 RTIDlyCnt RMB 1 ;Decremented on each RTI...<br />

;used to debounce switches<br />

00E5 MotorOnFlg RMB 1 ;1 = PWM out is on / 0 = off<br />

00E6 VarEnd EQU *<br />

;*********************************************************<br />

0200 ORG ROMStart<br />

0200 Start EQU *<br />

Freescale Semiconductor, Inc.<br />

0200 9C RSP ;Reset stack pointer in case...<br />

;we got here from an error<br />

0201 3F 00 CLR PortA ;Set up Port A outs to all 0's<br />

0203 A6 80 LDA #$80 ;Make PA7 an output<br />

0205 B7 04 STA DDRA<br />

;Clear out all of RAM<br />

0207 AE E0 LDX #RAMStart;Point to the start of RAM<br />

0209 7F ClrLoop CLR 0,x ;Clear a byte.<br />

020A 5C INCX ;Point to the next loc./ done?<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

210 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Listing 6. PWM Program Listing (Sheet 2 of 5)<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

020B 26 FC BNE ClrLoop ;No; continue to clear RAM<br />

020D A6 1C LDA #$1C ;Enable TOF & RTI interrupts<br />

020F B7 08 STA TSCR<br />

0211 A6 10 LDA #MinPWM ;Initialize PWM to min speed<br />

0213 B7 E3 STA MotorPWM<br />

0215 9A CLI ;Enable interrupts<br />

;*********************************************************<br />

;Main program loop. Read motor control switches. If a<br />

; switch is pressed, BSR to perform the requested action.<br />

; Loop continuously looking for switch closures.<br />

;<br />

0216 00 00 02 Main BRSET MotorOnOff,PortA,Main1 ;On/Off pressed?<br />

0219 AD 0C BSR DoOnOff ;If yes, go to DoOnOff<br />

021B 02 00 02 Main1 BRSET SpeedUp,PortA,Main2 ;Speed Up pressed?<br />

021E AD 25 BSR DoSpeedUp ;If yes, go to DoSpeedUp<br />

0220 04 00 F3 Main2 BRSET SpeedDn,PortA,Main ;Speed Down ?<br />

0223 AD 44 BSR DoSpeedDn ;If yes, go to DoSpeedDown<br />

0225 20 EF BRA Main ;Repeat loop continuously<br />

;*********************************************************<br />

;DoOnOff handles the closure of the Motor On/Off switch<br />

; Debounces switch and waits for release.<br />

;<br />

0227 DoOnOff EQU *<br />

0227 A6 07 LDA #DebounceT ;DebounceT * RTI time = 50mS<br />

0229 B7 E4 STA RTIDlyCnt ;Initialize software counter<br />

022B 3D E4 DoOnOff1 TST RTIDlyCnt ;RTI interrupt decrements it<br />

022D 26 FC BNE DoOnOff1 ;Loop till RTIDlyCnt = 0<br />

022F 00 00 12 BRSET MotorOnOff,PortA,DoOnOff3 ;Then check sw<br />

;If open, not a good press<br />

0232 01 00 FD BRCLR MotorOnOff,PortA,* ;Wait for sw release<br />

0235 3D E5 TST MotorOnFlg ;Motor already on?<br />

0237 26 07 BNE DoOnOff2 ;Yes, turn the motor off.<br />

0239 3C E5 INC MotorOnFlg ;No, Set ‘MotorOn’ flag<br />

023B B6 E3 LDA MotorPWM ;And get last motor speed<br />

023D B7 E0 STA DesiredPWM ;Turns on the PWM output<br />

023F 81 RTS ;Return (1 of 2)<br />

0240 3F E0 DoOnOff2 CLR DesiredPWM ;Turns the PWM output off<br />

0242 3F E5 CLR MotorOnFlg ;Clear ‘MotorOn’ flag<br />

0244 81 DoOnOff3 RTS ;Return (2 of 2)<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

211


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

Listing 6. PWM Program Listing (Sheet 3 of 5)<br />

;***********************************************************<br />

;DoSpeedUp handles the closure of the Speed Up switch<br />

; Debounces sw then increments duty cycle till release<br />

; Duty cycle incremented approx every 24 ms.<br />

; Adj across full speed range in approx 6 seconds<br />

;<br />

0245 DoSpeedUp EQU *<br />

0245 3D E5 TST MotorOnFlg ;Motor currently on?<br />

0247 26 01 BNE DoSpeedUp2 ;Yes, branch<br />

0249 81 DoSpeedUp1 RTS ;No, sws don't work if off<br />

024A A6 07 DoSpeedUp2 LDA #DebounceT ;Debounce delay approx 50 ms<br />

024C B7 E4 STA RTIDlyCnt ;Initialize software counter<br />

024E 3D E4 DoSpeedUp3 TST RTIDlyCnt ;RTI interrupt decrements it<br />

0250 26 FC BNE DoSpeedUp3 ;Loop till RTIDlyCnt = 0<br />

0252 02 00 F4 DoSpeedUp4 BRSET SpeedUp,PortA,DoSpeedUp1 ;RTS if sw off<br />

0255 B6 E3 LDA MotorPWM ;Sw pressed, do speed up<br />

0257 A1 FF CMPA #Percent100 ;Already full on?<br />

0259 27 EE BEQ DoSpeedUp1 ;If yes just return<br />

025B A6 03 LDA #RampTime ;No, get ramp time delay<br />

;(3 * 8.2Ms = 24.6)<br />

025D B7 E4 STA RTIDlyCnt ;Store to software counter<br />

025F 3D E4 DoSpeedUp5 TST RTIDlyCnt ;Ramp time delay expired?<br />

0261 26 FC BNE DoSpeedUp5 ;No, continue to wait<br />

0263 3C E3 INC MotorPWM ;Yes, increase motor speed<br />

0265 3C E0 INC DesiredPWM ;Adv the desired PWM value<br />

0267 20 E9 BRA DoSpeedUp4 ;Loop for sw still pressed<br />

;**********************************************************<br />

;DoSpeedDn handles the closure of the Speed Down switch<br />

; Debounces sw then increments duty cycle till release<br />

; Duty cycle incremented approx every 24 ms.<br />

; Adj across full speed range in approx 6 seconds<br />

;<br />

0269 DoSpeedDn EQU *<br />

0269 3D E5 TST MotorOnFlg ;Motor currently on?<br />

026B 26 01 BNE DoSpeedDn2 ;Yes, branch<br />

026D 81 DoSpeedDn1 RTS ;No, sws don't work if off<br />

026E A6 07 DoSpeedDn2 LDA #DebounceT ;Debounce delay approx 50 ms<br />

0270 B7 E4 STA RTIDlyCnt ;Initialize software counter<br />

0272 3D E4 DoSpeedDn3 TST RTIDlyCnt ;RTI interrupt decrements it<br />

0274 26 FC BNE DoSpeedDn3 ;Loop till RTIDlyCnt = 0<br />

0276 02 00 F4 DoSpeedDn4 BRSET SpeedUp,PortA,DoSpeedDn1 ;RTS if sw off<br />

0279 B6 E3 LDA MotorPWM ;Sw pressed, do speed up<br />

027B A1 10 CMPA #MinPWM ;Already at minimum speed?<br />

027D 27 EE BEQ DoSpeedDn1 ;If yes just return<br />

027F A6 03 LDA #RampTime ;No, get ramp time delay<br />

;(3 * 8.2Ms = 24.6)<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

212 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Listing 6. PWM Program Listing (Sheet 4 of 5)<br />

On-Chip Peripheral Systems<br />

A Practical Motor Control Example<br />

0281 B7 E4 STA RTIDlyCnt ;Store to software counter<br />

0283 3D E4 DoSpeedDn5 TST RTIDlyCnt ;Ramp time delay expired?<br />

0285 26 FC BNE DoSpeedDn5 ;No, continue to wait<br />

0287 3A E3 DEC MotorPWM ;Yes, decrease motor speed<br />

0289 3A E0 DEC DesiredPWM ;Reduce desired PWM value<br />

028B 20 E9 BRA DoSpeedDn4 ;Loop for sw still pressed<br />

;**********************************************************<br />

;Since RTI and TOF interrupts share 1 vector, TimerInt is<br />

;used to decide which source was requesting service.<br />

;TOFInt and RTIInt service routines are used together to<br />

;generate a PWM signal.<br />

;<br />

028D TimerInt EQU *<br />

028D 0E 08 04 BRSET TOF,TSCR,TOFInt ;TOF interrupt?<br />

0290 0C 08 12 BRSET RTIF,TSCR,RTIInt ;RTI interrupt?<br />

0293 80 RTI ;Shouldn’t get here (defensive code)<br />

;*********************************************************<br />

;TOF interrupt response - Decrement PWMCoarse, when 0...<br />

;Compare PWMFine to TCR. When TCR passes PWMFine clear<br />

;PWM output pin and disable further TOF. RTI re-enables.<br />

;<br />

0294 TOFInt EQU *<br />

0294 3A E1 DEC PWMCoarse ;Is PWMCoarse=0?<br />

0296 26 0A BNE ExitTOF ;No. Clear TOF and return<br />

0298 B6 E2 TOFInt1 LDA PWMFine ;To compare to upper 4 of TCR<br />

029A B1 09 CmpMore CMPA TCR<br />

029C 22 FC BHI CmpMore ;Loop till PWMFine 0%<br />

02B2 A1 FF CMPA #Percent100 ;Is desired PWM duty = 100%?<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

213


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

Listing 6. PWM Program Listing (Sheet 5 of 5)<br />

02B4 27 0D BEQ RTIInt3 ;Yes, Output always high<br />

02B6 AE 10 LDX #16 ;No, Put upper 4-bits of<br />

02B8 42 MUL ;DesiredPWM in low 4-bits of<br />

;X & low 4-bits of DesiredPWM<br />

;in upper 4-bits of A.<br />

02B9 B7 E2 STA PWMFine ;Save result into PWMFine<br />

02BB BF E1 STX PWMCoarse ;Save result into PWMCoarse<br />

02BD 27 D9 BEQ TOFInt1 ;If PWMCoarse=0, go to 2nd<br />

;half of TOF routine<br />

02BF 16 08 BSET TOFR,TSCR ;Clear Timer Overflow Flag<br />

02C1 1A 08 BSET TOIE,TSCR ;re-enable the TOF interrupt<br />

02C3 80 RTIInt3 RTI ;Return from RTIF interrupt<br />

;**********************************************************<br />

03F8 ORG Vectors ;Interrupt & reset vectors<br />

03F8 02 8D FDB TimerInt ;Timer interrupt routine<br />

03FA 02 00 FDB Start ;External IRQ (not used)<br />

03FC 02 00 FDB Start ;SWI vector (not used)<br />

03FE 02 00 FDB Start ;Reset vector<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

214 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Review<br />

Other Kinds<br />

of Peripherals<br />

Freescale Semiconductor, Inc.<br />

On-Chip Peripheral Systems<br />

Review<br />

A peripheral is a specialized piece of computer hardware that allows the<br />

CPU to gather information about and affect change on the system that a<br />

microcontroller is part of.<br />

General-purpose I/O ports may be programmed to act as either inputs or<br />

outputs. When a port pin is configured to act as an input, the CPU may<br />

read the logic level that is present on the port pin. When configured as<br />

an output, the CPU may set the port pin's output level to a logic 1 or<br />

logic 0.<br />

Although all microcontrollers contain some general-purpose I/O ports as<br />

peripherals, they also contain additional peripherals that perform more<br />

specific tasks.<br />

Timers <strong>—</strong> Timers are peripherals that are used to measure or generate<br />

time-related events in a microcontroller system. Timers are capable of<br />

performing frequency measurements or generating variable width pulse<br />

trains. Timers can be sophisticated or simple.<br />

Serial Ports <strong>—</strong> Sometimes microcontrollers need to communicate with<br />

specialized external peripherals or with another computer system. The<br />

communication is usually performed bit-serially (one bit of information at<br />

a time). The two most common types of serial ports are the serial<br />

communications interface (SCI) and the serial peripheral interface (SPI).<br />

The SCI communicates asynchronously with other devices and is<br />

usually used to exchange data between two computer systems. The SPI<br />

communicates synchronously with other devices and is usually used to<br />

control peripheral devices that are external to the microcontroller.<br />

Analog-to-Digital Converters <strong>—</strong> Many signals that exist outside the<br />

microcontroller are continuously varying analog signals. An<br />

analog-to-digital (A-to-D) converter is a peripheral that is used to convert<br />

these signals into a binary number that the microcontroller can use.<br />

Digital-to-Analog Converters <strong>—</strong> A digital-to-analog (D-to-A) converter<br />

performs the opposite function of an A-to-D converter. It allows the<br />

microcontroller to convert a digital number into a proportional analog<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

215


Freescale Semiconductor, Inc...<br />

On-Chip Peripheral Systems<br />

Freescale Semiconductor, Inc.<br />

voltage or current that can be used to control various output devices in<br />

a microcontroller system.<br />

EEPROM <strong>—</strong> Although EEPROM is a type of non-volatile memory, it is<br />

considered by many to be a peripheral. EEPROM is unique because its<br />

contents may be erased and rewritten under program control. Some<br />

EEPROM devices exist as a separate device that may be connected to<br />

an SPI port.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

216 On-Chip Peripheral Systems<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Freescale Semiconductor, Inc.<br />

Instruction Set Details<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219<br />

<strong>M68HC05</strong> Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221<br />

ADC <strong>—</strong> Add with Carry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222<br />

ADD <strong>—</strong> Add without Carry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223<br />

AND <strong>—</strong> Logical AND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224<br />

ASL <strong>—</strong> Arithmetic Shift Left. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225<br />

ASR <strong>—</strong> Arithmetic Shift Right . . . . . . . . . . . . . . . . . . . . . . . . . . . 226<br />

BCC <strong>—</strong> Branch if Carry Clear . . . . . . . . . . . . . . . . . . . . . . . . . . . 227<br />

BCLR n <strong>—</strong> Clear Bit in Memory . . . . . . . . . . . . . . . . . . . . . . . . . 228<br />

BCS <strong>—</strong> Branch if Carry Set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229<br />

BEQ <strong>—</strong> Branch if Equal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230<br />

BHCC <strong>—</strong> Branch if Half Carry Clear . . . . . . . . . . . . . . . . . . . . . . 231<br />

BHCS <strong>—</strong> Branch if Half Carry Set. . . . . . . . . . . . . . . . . . . . . . . . 232<br />

BHI <strong>—</strong> Branch if Higher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233<br />

BHS <strong>—</strong> Branch if Higher or Same. . . . . . . . . . . . . . . . . . . . . . . . 234<br />

BIH <strong>—</strong> Branch if Interrupt Pin is High . . . . . . . . . . . . . . . . . . . . . 235<br />

BIL <strong>—</strong> Branch if Interrupt Pin is Low . . . . . . . . . . . . . . . . . . . . . . 236<br />

BIT <strong>—</strong> Bit Test Memory with Accumulator . . . . . . . . . . . . . . . . . 237<br />

BLO <strong>—</strong> Branch if Lower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238<br />

BLS <strong>—</strong> Branch if Lower or Same . . . . . . . . . . . . . . . . . . . . . . . . 239<br />

BMC <strong>—</strong> Branch if Interrupt Mask is Clear . . . . . . . . . . . . . . . . . . 240<br />

BMI <strong>—</strong> Branch if Minus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241<br />

BMS <strong>—</strong> Branch if Interrupt Mask is Set. . . . . . . . . . . . . . . . . . . . 242<br />

BNE <strong>—</strong> Branch if Not Equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243<br />

BPL <strong>—</strong> Branch if Plus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244<br />

BRA <strong>—</strong> Branch Always . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245<br />

BRCLR n <strong>—</strong> Branch if Bit n is Clear . . . . . . . . . . . . . . . . . . . . . . 246<br />

BRN <strong>—</strong> Branch Never . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247<br />

BRSET n <strong>—</strong> Branch if Bit n is Set . . . . . . . . . . . . . . . . . . . . . . . . 248<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

217


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

Freescale Semiconductor, Inc.<br />

BSET n <strong>—</strong> Set Bit in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 249<br />

BSR <strong>—</strong> Branch to Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . 250<br />

CLC <strong>—</strong> Clear Carry Bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251<br />

CLI <strong>—</strong> Clear Interrupt Mask Bit . . . . . . . . . . . . . . . . . . . . . . . . . . 252<br />

CLR <strong>—</strong> Clear. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253<br />

CMP <strong>—</strong> Compare Accumulator with Memory . . . . . . . . . . . . . . . 254<br />

COM <strong>—</strong> Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255<br />

CPX <strong>—</strong> Compare Index Register with Memory. . . . . . . . . . . . . . 256<br />

DEC <strong>—</strong> Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257<br />

EOR <strong>—</strong> Exclusive-OR Memory with Accumulator. . . . . . . . . . . . 258<br />

INC <strong>—</strong> Increment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259<br />

JMP <strong>—</strong> Jump. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260<br />

JSR <strong>—</strong> Jump to Subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261<br />

LDA <strong>—</strong> Load Accumulator from Memory . . . . . . . . . . . . . . . . . . 262<br />

LDX <strong>—</strong> Load Index Register from Memory . . . . . . . . . . . . . . . . . 263<br />

LSL <strong>—</strong> Logical Shift Left . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264<br />

LSR <strong>—</strong> Logical Shift Right. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265<br />

MUL <strong>—</strong> Multiply Unsigned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266<br />

NEG <strong>—</strong> Negate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267<br />

NOP <strong>—</strong> No Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268<br />

ORA <strong>—</strong> Inclusive-OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269<br />

ROL <strong>—</strong> Rotate Left thru Carry. . . . . . . . . . . . . . . . . . . . . . . . . . . 270<br />

ROR <strong>—</strong> Rotate Right thru Carry . . . . . . . . . . . . . . . . . . . . . . . . . 271<br />

RSP <strong>—</strong> Reset Stack Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272<br />

RTI <strong>—</strong> Return from Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273<br />

RTS <strong>—</strong> Return from Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . 274<br />

SBC <strong>—</strong> Subtract with Carry. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275<br />

SEC <strong>—</strong> Set Carry Bit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276<br />

SEI <strong>—</strong> Set Interrupt Mask Bit . . . . . . . . . . . . . . . . . . . . . . . . . . . 277<br />

STA <strong>—</strong> Store Accumulator in Memory . . . . . . . . . . . . . . . . . . . . 278<br />

STOP <strong>—</strong> Enable IRQ, Stop Oscillator. . . . . . . . . . . . . . . . . . . . . 279<br />

STX <strong>—</strong> Store Index Register X in Memory . . . . . . . . . . . . . . . . . 280<br />

SUB <strong>—</strong> Subtract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281<br />

SWI <strong>—</strong> Software Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282<br />

TAX <strong>—</strong> Transfer Accumulator to Index Register . . . . . . . . . . . . . 283<br />

TST <strong>—</strong> Test for Negative or Zero . . . . . . . . . . . . . . . . . . . . . . . . 284<br />

TXA <strong>—</strong> Transfer Index Register to Accumulator . . . . . . . . . . . . . 285<br />

WAIT <strong>—</strong> Enable Interrupt, Stop Processor . . . . . . . . . . . . . . . . . 286<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

218 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

Instruction Set Details<br />

Introduction<br />

This section contains complete detailed information for all <strong>M68HC05</strong><br />

instructions. The instructions are arranged in alphabetical order with the<br />

instruction mnemonic set in larger type for easy reference.<br />

This nomenclature is used in the following definitions:<br />

(a) Operators<br />

( ) = Contents of Register or Memory Location Shown inside<br />

Parentheses<br />

← = Is Loaded with (Read: gets)<br />

↑ = Is Pulled from Stack<br />

↓ = Is Pushed onto Stack<br />

• = Boolean AND<br />

+ = Arithmetic Addition (Except Where Used as Inclusive-OR<br />

in Boolean Formula)<br />

⊕ = Boolean Exclusive-OR<br />

X = Multiply<br />

: = Concatenate<br />

– = Negate (Twos Complement)<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

219


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

Freescale Semiconductor, Inc.<br />

(b) CPU Registers<br />

ACCA = Accumulator<br />

CCR = Condition Code Register<br />

X = Index Register<br />

PC = Program Counter<br />

PCH = Program Counter, Higher Order (Most Significant) 8 Bits<br />

PCL = Program Counter, Lower Order (Least Significant) 8 Bits<br />

SP = Stack Pointer<br />

(c) Memory and Addressing<br />

M = A memory location or absolute data, depending on<br />

addressing mode<br />

Rel = Relative offset; for instance, the twos-complement number<br />

stored in the last byte of machine code corresponding to a<br />

branch instruction<br />

(d) Condition Code Register (CCR) Bits<br />

H = Half Carry, Bit 4<br />

I = Interrupt Mask, Bit 3<br />

N = Negative Indicator, Bit 2<br />

Z = Zero Indicator, Bit 1<br />

C = Carry/Borrow, Bit 0<br />

(e) Bit Status BEFORE Execution (n = 7, 6, 5, . . . 0)<br />

An = Bit n of ACCA<br />

Xn = Bit n of X<br />

Mn = Bit n of M<br />

(f) Bit status AFTER execution<br />

Rn = Bit n of the Result (n = 7, 6, 5, . . . 0)<br />

(g) CCR Activity Summary Figure Notation<br />

<strong>—</strong> = Bit Not Affected<br />

0 = Bit Forced to 0<br />

1 = Bit Forced to 1<br />

↕ = Bit Set or Cleared According to Results of Operation<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

220 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

<strong>M68HC05</strong> Instruction Set<br />

Freescale Semiconductor, Inc.<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

(h) Machine Coding Notation<br />

dd = Low-Order 8 Bits of a Direct Address $0000-$00FF; High<br />

Byte Assumed to be $0000<br />

ee = Upper 8 Bits of 16-Bit Offset<br />

ff = Lower 8 Bits of 16-Bit Offset or 8-Bit Offset<br />

ii = One Byte of Immediate Data<br />

hh = High-Order Byte of 16-Bit Extended Address<br />

ll = Low-Order Byte of 16-Bit Extended Address<br />

rr = Relative Offset<br />

(i) Source form notation<br />

(opr) = Operand; One or Two Bytes Depending on Address Mode<br />

(rel) = Relative Offset Used in Branch and Bit Manipulation<br />

Instructions<br />

The following pages contain complete detailed information for all<br />

<strong>M68HC05</strong> instructions. The instructions are arranged in alphabetical<br />

order with the instruction mnemonic set in larger type for easy reference.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

221


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

ADC Add with Carry ADC<br />

Operation ACCA ← (ACCA) + (M) + (C)<br />

Description Adds the contents of the C bit to the sum of the contents of ACCA and<br />

M and places the result in ACCA.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H A3 • M3 + M3 • R3 + R3 • A3<br />

Set if there was a carry from bit 3; cleared otherwise.<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

C A7 • M7 + M7 • R7 + R7 • A7<br />

Set if there was a carry from the MSB of the result; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 ↕ <strong>—</strong> ↕ ↕ ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

ADC (opr) IMM A9 ii 2<br />

ADC (opr) DIR B9 dd 3<br />

ADC (opr) EXT C9 hh ll 4<br />

ADC ,X IX F9 3<br />

ADC (opr),X IX1 E9 ff 4<br />

ADC (opr),X IX2 D9 ee ff 5<br />

222 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

ADD Add without Carry ADD<br />

Operation ACCA ← (ACCA) + (M)<br />

Description Adds the contents of M to the contents of ACCA and places the result in<br />

ACCA.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 ↕ <strong>—</strong> ↕ ↕ ↕<br />

H A3 • M3 + M3 • R3 + R3 • A3<br />

Set if there was a carry from bit 3; cleared otherwise.<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

C A7 • M7 + M7 • R7 + R7 • A7<br />

Set if there was a carry from the MSB of the result; cleared otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

ADD (opr) IMM AB ii 2<br />

ADD (opr) DIR BB dd 3<br />

ADD (opr) EXT CB hh ll 4<br />

ADD,X IX FB 3<br />

ADD (opr),X IX1 EB ff 4<br />

ADD (opr),X IX2 DB ee ff 5<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

223


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

AND Logical AND AND<br />

Operation ACCA ← (ACCA) • (M)<br />

Description Performs the logical AND between the contents of ACCA and the<br />

contents of M and places the result in ACCA. (Each bit of ACCA after the<br />

operation will be the logical AND of the corresponding bits of M and of<br />

ACCA before the operation.)<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

AND (opr) IMM A4 ii 2<br />

AND (opr) DIR B4 dd 3<br />

AND (opr) EXT C4 hh ll 4<br />

AND,X IX F4 3<br />

AND (opr),X IX1 E4 ff 4<br />

AND (opr),X IX2 D4 ee ff 5<br />

224 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

ASL Arithmetic Shift Left ASL<br />

Operation<br />

(Same as LSL)<br />

Description Shifts all bits of the ACCA, X, or M one place to the left. Bit 0 is loaded<br />

with a zero. The C bit in the CCR is loaded from the most significant bit<br />

of ACCA, X, or M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

C b7 – – – – – – b0 0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

C b7<br />

Set if, before the shift, the MSB of the shifted value was set; cleared<br />

otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

ASLA INH (A) 48 3<br />

ASLX INH (X) 58 3<br />

ASL (opr) DIR 38 dd 5<br />

ASL ,X IX 78 5<br />

ASL (opr),X IX1 68 ff 6<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

225


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

ASR Arithmetic Shift Right ASR<br />

Operation<br />

Description Shifts all of ACCA, X, or M one place to the right. Bit 7 is held constant.<br />

Bit 0 is loaded into the C bit of the CCR. This operation effectively divides<br />

a twos-complement value by two without changing its sign. The carry bit<br />

can be used to round the result.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

b7 – – – – – – b0<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

C b0<br />

Set if, before the shift, the LSB of the shifted value was set; cleared<br />

otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

226 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA<br />

C<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

ASRA INH (A) 47 3<br />

ASRX INH (X) 57 3<br />

ASR (opr) DIR 37 dd 5<br />

ASR ,X IX 77 5<br />

ASR (opr),X IX1 67 ff 6


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BCC Branch if Carry Clear BCC<br />

(Same as BHS)<br />

Operation PC ← (PC) + $0002 + Rel if (C) = 0<br />

Description Tests the state of the C bit in the CCR and causes a branch if C is clear.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BCC (rel) REL 24 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BCLR n Clear Bit in Memory BCLR n<br />

Operation Mn ← 0<br />

Description Clear bit n (n = 7, 6, 5. . . 0) in location M. All other bits in M are<br />

unaffected. M can be any RAM or I/O register address in the $0000 to<br />

$00FF area of memory (for instance., direct addressing mode is used to<br />

specify the address of the operand).<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BCLR 0,(opr) DIR (bit 0) 11 dd 5<br />

BCLR 1,(opr) DIR (bit 1) 13 dd 5<br />

BCLR 2,(opr) DIR (bit 2) 15 dd 5<br />

BCLR 3,(opr) DIR (bit 3) 17 dd 5<br />

BCLR 4,(opr) DIR (bit 4) 19 dd 5<br />

BCLR 5,(opr) DIR (bit 5) 1B dd 5<br />

BCLR 6,(opr) DIR (bit 6) 1D dd 5<br />

BCLR 7,(opr) DIR (bit 7) 1F dd 5<br />

228 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BCS Branch if Carry Set BCS<br />

(Same as BLO)<br />

Operation PC ← (PC) + $0002 + Rel if (C) = 1<br />

Description Tests the state of the C bit in the CCR and causes a branch if C is set.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BCS (rel) REL 25 rr 3<br />

This table is a summary of all branch instructions.<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BEQ Branch if Equal BEQ<br />

Operation PC ← (PC) + $0002 + Rel if (Z) = 1<br />

Description Tests the state of the Z bit in the CCR and causes a branch if Z is set.<br />

Following a CMP or SUB instruction, BEQ will cause a branch if the<br />

arguments were equal.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BEQ (rel) REL 27 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BHCC Branch if Half Carry Clear BHCC<br />

Operation PC ← (PC) + $0002 + Rel if (H) = 0<br />

Description Tests the state of the H bit in the CCR and causes a branch if H is clear.<br />

This instruction is used in algorithms involving BCD numbers.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BHCC (rel) REL 28 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BHCS Branch if Half Carry Set BHCS<br />

Operation PC ← (PC) + $0002 + Rel if (H) = 1<br />

Description Tests the state of the H bit in the CCR and causes a branch if H is set.<br />

This instruction is used in algorithms involving BCD numbers. See BRA<br />

instruction for further details of the execution of the branch.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BHCS (rel) REL 29 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BHI Branch if Higher BHI<br />

Operation C ← (PC) + $0002 + Rel if (C) + (Z) = 0<br />

for instance, if (ACCA) > (M) (unsigned binary numbers)<br />

Description Causes a branch if both C and Z are cleared. If the BHl instruction is<br />

executed immediately after execution of a CMP or SUB instruction, the<br />

branch will occur if the unsigned binary number in ACCA was greater<br />

than the unsigned binary number in M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BHI (rel) REL 22 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BHS Branch if Higher or Same BHS<br />

(Same as BCC)<br />

Operation PC ← (PC) + $0002 + Rel if (C) = 0<br />

for instance, if (ACCA) ≥ (M) (unsigned binary numbers)<br />

Description If the BHS instruction is executed immediately after execution of a CMP<br />

or SUB instruction, the branch will occur if the unsigned binary number<br />

in ACCA was greater than or equal to the unsigned binary number in M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BHS (rel) REL 24 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BIH Branch if Interrupt Pin is High BIH<br />

Operation PC ← (PC) + $0002 + Rel if IRQ = 1<br />

Description Tests the state of the external interrupt pin and causes a branch if the<br />

pin is high.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BIH (rel) REL 2F rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BIL Branch if Interrupt Pin is Low BIL<br />

Operation PC ← (PC) + $0002 + Rel if IRQ = 0<br />

Description Tests the state of the external interrupt pin and causes a branch if the<br />

pin is low.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BIL (rel) REL 2E rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BIT Bit Test Memory with Accumulator BIT<br />

Operation (ACCA) • (M)<br />

Description Performs the logical AND comparison of the contents of ACCA and the<br />

contents of M and modifies the condition codes accordingly. Neither the<br />

contents of ACCA nor M are altered. (Each bit of the result of the AND<br />

would be the logical AND of the corresponding bits of ACCA and M).<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BIT (opr) IMM A5 ii 2<br />

BIT (opr) DIR B5 dd 3<br />

BIT (opr) EXT C5 hh ll 4<br />

BIT,X IX F5 3<br />

BIT (opr),X IX1 E5 ff 4<br />

BIT (opr),X IX2 D5 ee ff 5<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

237


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BLO Branch if Lower BLO<br />

(Same as BCS)<br />

Operation PC ← (PC) + $0002 + Rel if (C) = 1<br />

for instance, if (ACCA) < (M) (unsigned binary numbers)<br />

Description If the BLO instruction is executed immediately after execution of a CMP<br />

or SUB instruction, the branch will occur if the unsigned binary number<br />

in ACCA was less than the unsigned binary number in M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BLO (rel) REL 25 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BLS Branch if Lower or Same BLS<br />

Operation PC ← (PC) + $0002 + Rel if [(C) + (Z)] = 1<br />

for instance, if (ACCA) ≤ (M) (unsigned binary numbers)<br />

Description Causes a branch if C is set or Z is set. If the BLS instruction is executed<br />

immediately after execution of a CMP or SUB instruction, the branch will<br />

occur if the unsigned binary number in ACCA was less than or equal to<br />

the unsigned binary number in M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BLS (rel) REL 23 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BMC Branch if Interrupt Mask is Clear BMC<br />

Operation PC ← (PC) + $0002 + Rel if I = 0<br />

Description Tests the state of the I bit in the CCR and causes a branch if I is clear<br />

(for instance., if interrupts are enabled). See BRA instruction for further<br />

details of the execution of the branch.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BMC (rel) REL 2C rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BMI Branch if Minus BMI<br />

Operation PC ← (PC) + $0002 + Rel if (N) = 1<br />

Description Tests the state of the N bit in the CCR and causes a branch if N is set.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BMI (rel) REL 2B rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BMS Branch if Interrupt Mask is Set BMS<br />

Operation PC ← (PC) + $0002 + Rel if (I) = 1<br />

Description Tests the state of the I bit in the CCR and causes a branch if I is set (for<br />

instance., if interrupts are disabled).<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BMS (rel) REL 2D rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BNE Branch if Not Equal BNE<br />

Operation PC ← (PC) + $0002 + Rel if (Z) = 0<br />

Description Tests the state of the Z bit in the CCR and causes a branch if Z is clear.<br />

Following a compare or subtract instruction, BEQ will cause a branch if<br />

the arguments were not equal.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BNE (rel) REL 26 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BPL Branch if Plus BPL<br />

Operation PC ← (PC) + $0002 + Rel if (N) = 0<br />

Description Tests the state of the N bit in the CCR and causes a branch if N is clear.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

This table is a summary of all branch instructions.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BPL (rel) REL 2A rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BRA Branch Always BRA<br />

Operation PC ← (PC) + $0002 + Rel<br />

Description Unconditional branch to the address given by the foregoing formula, in<br />

which Rel is the relative offset stored as a twos-complement number in<br />

the last byte of machine code corresponding to the branch instruction.<br />

PC is the address of the opcode for the branch instruction.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

The source program specifies the destination of any branch instruction<br />

by its absolute address, either as a numerical value or as a symbol or<br />

expression which can be numerically evaluated by the assembler. The<br />

assembler calculates the relative address, Rel, from the absolute<br />

address and the current value of the location counter.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BRA (rel) REL 20 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BRCLR n Branch if Bit n is Clear BRCLR n<br />

Operation PC ← (PC) + $0003 + Rel if bit n of M = 0<br />

Description Tests bit n (n = 7, 6, 5. . . 0) of location M and branches if the bit is clear.<br />

M can be any RAM or I/O register address in the $0000 to $00FF area<br />

of memory (for instance, direct addressing mode is used to specify the<br />

address of the operand).<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

The C bit is set to the state of the bit tested. When used along with an<br />

appropriate rotate instruction, BRCLR n provides an easy method for<br />

performing serial to parallel conversions.<br />

C Set if Mn = 1; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BRCLR 0,(opr),(rel) DIR (bit 0) 01 dd rr 5<br />

BRCLR 1,(opr),(rel) DIR (bit 1) 03 dd rr 5<br />

BRCLR 2,(opr),(rel) DIR (bit 2) 05 dd rr 5<br />

BRCLR 3,(opr),(rel) DIR (bit 3) 07 dd rr 5<br />

BRCLR 4,(opr),(rel) DIR (bit 4) 09 dd rr 5<br />

BRCLR 5,(opr),(rel) DIR (bit 5) OB dd rr 5<br />

BRCLR 6,(opr),(rel) DIR (bit 6) OD dd rr 5<br />

BRCLR 7,(opr),(rel) DIR (bit 7) OF dd rr 5<br />

246 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BRN Branch Never BRN<br />

Operation PC ← (PC) + $0002<br />

Description Never branches. In effect, this instruction can be considered as a 2-byte<br />

NOP (no operation) requiring three cycles for execution. Its inclusion in<br />

the instruction set is to provide a complement for the BRA instruction.<br />

The instruction is useful during program debug to negate the effect of<br />

another branch instruction without disturbing the offset byte.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

This table is a summary of all branch instructions.<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BRN (rel) REL 21 rr 3<br />

Test Boolean Mnemonic Opcode Complementary Branch Comment<br />

r > m C + Z = 0 BHI 22 r ≤ m BLS 23 Unsigned<br />

r ≥ m C = 0 BHS/BCC 24 r m BHI 22 Unsigned<br />

r


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BRSET n Branch if Bit n is Set BRSET n<br />

Operation PC ← (PC) + $0003 + Rel if bit n of M = 1<br />

Description Tests bit n (n = 7, 6, 5, 0) of location M and branches if the bit is set. M<br />

can be any RAM or I/O register address in the $0000 to $00FF area of<br />

memory (for instance, direct addressing mode is used to specify the<br />

address of the operand).<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

The C bit is set to the state of the bit tested. When used along with an<br />

appropriate rotate instruction, BRSET n provides an easy method for<br />

performing serial to parallel conversions.<br />

C Set if Mn = 1; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BRSET 0,(opr),(rel) DIR (bit 0) 00 dd rr 5<br />

BRSET 1,(opr),(rel) DIR (bit 1) 02 dd rr 5<br />

BRSET 2,(opr),(rel) DIR (bit 2) 04 dd rr 5<br />

BRSET 3,(opr),(rel) DIR (bit 3) 06 dd rr 5<br />

BRSET 4,(opr),(rel) DIR (bit 4) 08 dd rr 5<br />

BRSET 5,(opr), (rel) DIR (bit 5) 0A dd rr 5<br />

BRSET 6,(opr),(rel) DIR (bit 6) 0C dd rr 5<br />

BRSET 7,(opr),(rel) DIR (bit 7) 0E dd rr 5<br />

248 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

BSET n Set Bit in Memory BSET n<br />

Operation Mn ← 1<br />

Description Setbitn(n=7,6,5...0)inlocation M. All other bits in M are unaffected.<br />

M can be any RAM or I/O register address in the $0000 to $00FF area<br />

of memory (for instance, direct addressing mode is used to specify the<br />

address of the operand).<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BSET 0,(opr) DIR (bit 0) 10 dd 5<br />

BSET 1,(opr) DIR (bit 1) 12 dd 5<br />

BSET 2,(opr) DIR (bit 2) 14 dd 5<br />

BSET 3,(opr) DIR (bit 3) 16 dd 5<br />

BSET 4,(opr) DIR (bit 4) 18 dd 5<br />

BSET 5,(opr) DIR (bit 5) 1A dd 5<br />

BSET 6,(opr) DIR (bit 6) 1C dd 5<br />

BSET 7,(opr) DIR (bit 7) 1E dd 5<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

249


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

BSR Branch to Subroutine BSR<br />

Operation PC ← (PC) + $0002 Advance PC to return address<br />

↓ (PCL); SP ← (SP)–$0001 Push low-order return onto stack<br />

↓ (PCL); SP ← (SP)–$0001 Push high-order return onto stack<br />

PC ← (PC) + Rel Load PC with start address of<br />

requested subroutine<br />

Description The program counter is incremented by two from the opcode address,<br />

for instance, so it points to the opcode of the next instruction which will<br />

be the return address. The least significant byte of the contents of the<br />

program counter (low-order return address) is pushed onto the stack.<br />

The stack pointer is then decremented by one. The most significant byte<br />

of the contents of the program counter (high-order return address) is<br />

pushed onto the stack. The stack pointer is then decremented by one. A<br />

branch then occurs to the location specified by the branch offset.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

See BRA instruction for further details of the execution of the branch.<br />

None affected<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

BSR (rel) REL AD rr 6<br />

250 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

CLC Clear Carry Bit CLC<br />

Operation C bit ← 0<br />

Description Clears the C bit in the CCR. CLC may be used to set up the C bit prior<br />

to a shift or rotate instruction involving the C bit.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

C 0<br />

Cleared<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> 0<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

CLC INH 98 2<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

251


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

CLI Clear Interrupt Mask Bit CLI<br />

Operation I bit ← 0<br />

Description Clears the interrupt mask bit in the CCR. When the I bit is clear,<br />

interrupts are enabled. There is a one E-clock cycle delay in the clearing<br />

mechanism for the I bit so that, if interrupts were previously disabled, the<br />

next instruction after a CLI will always be executed, even if there was an<br />

interrupt pending prior to execution of the CLI instruction.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

I 0<br />

Cleared<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> 0 <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

CLI INH 9A 2<br />

252 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

CLR Clear CLR<br />

Operation ACCA ← $00 or: M ← $00 or: X ← $00<br />

Description The contents of ACCA, M, or X are replaced with 0s.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

I 0<br />

Cleared<br />

Z 1<br />

Set<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> 0 1 <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

CLRA INH (A) 4F 3<br />

CLRX INH (X) 5F 3<br />

CLR (opr) DIR 3F dd 5<br />

CLR ,X IX 7F 5<br />

CLR (opr),X IX1 6F ff 6<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

253


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

CMP Compare Accumulator with Memory CMP<br />

Operation (ACCA) – (M)<br />

Description Compares the contents of ACCA to the contents of M and sets the<br />

condition codes, which may be used for arithmetic and logical<br />

conditional branching. The contents of both ACCA and M are<br />

unchanged.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

C A7 • M7 + M7 • R7 + R7 • A7<br />

Set if absolute value of the contents of memory is larger than the absolute<br />

value of the accumulator; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

CMP (opr) IMM A1 ii 2<br />

CMP (opr) DIR B1 dd 3<br />

CMP (opr) EXT C1 hh ll 4<br />

CMP ,X IX F1 3<br />

CMP (opr),X IX1 E1 ff 4<br />

CMP (opr),X IX2 D1 ee ff 5<br />

254 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

COM Complement COM<br />

Operation ACCA ← (ACCA) = $FF – (ACCA) or: M ← (M) = $FF – (M) or:<br />

X ← X = $FF – (X)<br />

Description Replaces the contents of ACCA, X, or M with its ones complement.<br />

(Each bit of the contents of ACCA, X, or M is replaced with the<br />

complement of that bit.)<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ 1<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

C 1<br />

Set<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

COMA INH (A) 43 3<br />

COMX INH (X) 53 3<br />

COM (opr) DIR 33 dd 5<br />

COM ,X IX 73 5<br />

COM (opr),X IX1 63 ff 6<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

255


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

CPX Compare Index Register with Memory CPX<br />

Operation (X) – (M)<br />

Description Compares the contents of the index register with the contents of memory<br />

and sets the condition codes, which may be used for arithmetic and<br />

logical branching. The contents of both ACCA and M are unchanged.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

C IX7 • M7 + M7 • R7 + R7 • IX7<br />

Set if the absolute value of the contents of memory is larger than the<br />

absolute value of the index register; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

I CPX (opr) IMM A3 ii 2<br />

CPX (opr) DIR B3 dd 3<br />

CPX (opr) EXT C3 hh ll 4<br />

CPX,X IX F3 3<br />

CPX (opr),X IX1 E3 ff 4<br />

CPX (opr),X IX2 D3 ee ff 5<br />

256 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

DEC Decrement DEC<br />

Operation ACCA ← (ACCA) – $01 or: M ← (M) – $01 or: X ← (X)-$01<br />

Description Subtract one from the contents of ACCA, X, or M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing Modes<br />

Machine Code,<br />

and Cycles<br />

Freescale Semiconductor, Inc.<br />

The N and Z bits in the CCR are set or cleared according to the result of<br />

this operation. The C bit in the CCR is not affected; therefore, the only<br />

branch instructions that are useful following a DEC instruction are BEQ,<br />

BNE, BPL, and BMI.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

DECA INH (A) 4A 3<br />

DECX INH (X) 5A 3<br />

DEC (opr) DIR 3A dd 5<br />

DEC ,X IX 7A 5<br />

DEC (opr),X IX1 6A ff 6<br />

DEX is recognized by the assembler as being equivalent to DECX.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

257


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

EOR Exclusive-OR Memory with Accumulator EOR<br />

Operation ACCA ← (ACCA) ⊕ (M)<br />

Description Performs the logical exclusive-OR between the contents of ACCA and<br />

the contents of M and places the result in ACCA. (Each bit of ACCA after<br />

the operation will be the logical exclusive-OR of the corresponding bits<br />

of M and ACCA before the operation.)<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

EOR (opr) IMM A8 ii 2<br />

EOR (opr) DIR B8 dd 3<br />

EOR (opr) EXT C8 hh ll 4<br />

EOR ,X IX F8 3<br />

EOR (opr),X IX1 E8 ff 4<br />

EOR (opr),X IX2 D8 ee ff 5<br />

258 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

INC Increment INC<br />

Operation ACCA ← (ACCA) + $01 or: M ← (M) + $01 or: X ← (X) + $01<br />

Description Add one to the contents of ACCA, X, or M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

The N and Z bits in the CCR are set or cleared according to the results<br />

of this operation. The C bit in the CCR is not affected; therefore, the only<br />

branch instructions that are useful following an INC instruction are BEQ,<br />

BNE, BPL, and BMI.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

INCA INH (A) 4C 3<br />

INCX INH (X) 5C 3<br />

INC (opr) DIR 3C dd 5<br />

INC ,X IX 7C 5<br />

INC (opr),X IX1 6C ff 6<br />

INX is recognized by the assembler as being equivalent to INCX.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

259


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

JMP Jump JMP<br />

Operation PC ← Effective Address<br />

Description A jump occurs to the instruction stored at the effective address. The<br />

effective address is obtained according to the rules for EXTended,<br />

DIRect, or INDexed addressing.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

JMP (opr) DIR BC dd 2<br />

JMP (opr) EXT CC hh ll 3<br />

JMP ,X IX FC 2<br />

JMP (opr), X IX1 EC ff 3<br />

JMP (opr),X IX2 DC ee ff 4<br />

260 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

JSR Jump to Subroutine JSR<br />

Operation PC ← (PC) + n n = 1, 2, 3 depending on address mode<br />

↓ (PCL); SP ← SP – $0001 Push low-order return address onto stack<br />

↓ (PCH); SP ← SP – $0001 Push high-order return address onto<br />

stack<br />

PC ← Effective Addr Load PC with start address of<br />

requested subroutine<br />

Description The program counter is incremented by n so that it points to the opcode<br />

of the instruction that follows the JSR instruction (n = 1, 2, or 3<br />

depending on the addressing mode). The PC is then pushed onto the<br />

stack, eight bits at a time, least significant byte first. Unused bits in the<br />

program counter high byte are stored as ones on the stack. The stack<br />

pointer points to the next empty location on the stack. A jump occurs to<br />

the instruction stored at the effective address. The effective address is<br />

obtained according to the rules for EXTended, DIRect, or INDexed<br />

addressing.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

JSR (opr) DIR BD dd 5<br />

JSR (opr) EXT CD hh ll 6<br />

JSR ,X IX FD 5<br />

JSR (opr), X IX1 ED ff 6<br />

JSR (opr),X IX2 DD ee ff 7<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

261


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

LDA Load Accumulator from Memory LDA<br />

Operation ACCA ← (M)<br />

Description Loads the contents of memory into the accumulator. The condition<br />

codes are set according to the data.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

LDA (opr) IMM A6 ii 2<br />

LDA (opr) DIR B6 dd 3<br />

LDA (opr) EXT C6 hh ll 4<br />

LDA ,X IX F6 3<br />

LDA (opr),X IX1 E6 ff 4<br />

LDA (opr),X IX2 D6 ee ff 5<br />

262 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

LDX Load Index Register from Memory LDX<br />

Operation X ← (M)<br />

Description Loads the contents of the specified memory location into the index<br />

register. The condition codes are set according to the data.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

LDX (opr) IMM AE ii 2<br />

LDX (opr) DIR BE dd 3<br />

LDX (opr) EXT CE hh ll 4<br />

LDX ,X IX FE 3<br />

LDX (opr),X IX1 EE ff 4<br />

LDX (opr),X IX2 DE ee ff 5<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

263


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

LSL Logical Shift Left LSL<br />

Operation<br />

(Same as ASL)<br />

Description Shifts all bits of the ACCA, X, or M one place to the left. Bit 0 is loaded<br />

with 0. The C bit in the CCR is loaded from the most significant bit of<br />

ACCA, X, or M.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

C b7 – – – – – – b0 0<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

C b7<br />

Set if, before the shift, the MSB of ACCA or M was set; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

LSLA INH (A) 48 3<br />

LSLX INH (X) 58 3<br />

LSL (opr) DIR 38 dd 5<br />

LSL ,X IX 78 5<br />

LSL (opr),X IX1 68 ff 6<br />

264 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

LSR Logical Shift Right LSR<br />

Operation<br />

Description Shifts all bits of ACCA, X, or M one place to the right. Bit 7 is loaded with<br />

0. Bit 0 is shifted into the C bit.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

0<br />

Freescale Semiconductor, Inc.<br />

b7 – – – – – – b0<br />

N 0<br />

Cleared.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

C b0<br />

Set if, before the shift, the LSB of ACCA, X, or M was set; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

265<br />

C<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> 0 ↕ ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

LSRA INH (A) 44 3<br />

LSRX INH (X) 54 3<br />

LSR (opr) DIR 34 dd 5<br />

LSR ,X IX 74 5<br />

LSR (opr),X IX1 64 ff 6


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

MUL Multiply Unsigned MUL<br />

Operation X:A ← X x A<br />

Description Multiplies the eight bits in the index register by the eight bits in the<br />

accumulator to obtain a 16-bit unsigned number in the concatenated<br />

index register and accumulator. After the operation, X contains the upper<br />

8 bits of the 16-bit result.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H 0<br />

Cleared<br />

C 0<br />

Cleared<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 0 <strong>—</strong> <strong>—</strong> <strong>—</strong> 0<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

MUL INH 42 11<br />

266 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

NEG Negate NEG<br />

Operation ACCA ← – (ACCA); or: X ← – (X); or: M ← – (M)<br />

Description Replaces the contents of ACCA, X, or M with its twos complement. Note<br />

that the value $80 is left unchanged.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

C R7 + R6 + R5 + R4 + R3 + R2 + R1 + R0<br />

Set if there is a borrow in the implied subtraction from 0; cleared otherwise.<br />

The C bit will be set in all cases except when the contents of<br />

ACCA, X, or M (prior to the NEG operation) is $00.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

NEGA INH (A) 40 3<br />

NEGX INH (X) 50 3<br />

NEG (opr) DIR 30 dd 5<br />

NEG ,X IX 70 5<br />

NEG (opr),X IX1 60 ff 6<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

267


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

NOP No Operation NOP<br />

Description This is a single-byte instruction that causes only the program counter to<br />

be incremented. No other registers are affected.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

NOP INH 9D 2<br />

268 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

ORA Inclusive-OR ORA<br />

Operation ACCA ← (ACCA) + (M)<br />

Description Performs the logical inclusive-OR between the contents of ACCA and<br />

the contents of M and places the result in ACCA. Each bit of ACCA after<br />

the operation will be the logical inclusive-OR of the corresponding bits of<br />

M and of ACCA before the operation.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

ORA (opr) IMM AA ii 2<br />

ORA (opr) DIR BA dd 3<br />

ORA (opr) EXT CA hh ll 4<br />

ORA ,X IX FA 3<br />

ORA (opr),X IX1 EA ff 4<br />

ORA (opr),X 1X2 DA ee ff 5<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

269


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

ROL Rotate Left thru Carry ROL<br />

Operation<br />

Description Shifts all bits of ACCA, X, or M one place to the left. Bit 0 is loaded from<br />

the C bit. The C bit is loaded from the MSB of ACCA, X, or M. The rotate<br />

instructions include the carry bit to allow extension of the shift and rotate<br />

operations to multiple bytes. For example, to shift a 24-bit value left one<br />

bit, the sequence {ASL LOW, ROL MID, ROL HIGH} could be used<br />

where LOW, MID, and HIGH refer to the low-order, middle, and<br />

high-order bytes of the 24-bit value, respectively.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

C b7 – – – – – – b0 C<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

C b7<br />

Set if, before the rotate, the MSB of ACCA or M was set; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

ROLA INH (A) 49 3<br />

ROLX INH (X) 59 3<br />

ROL (opr) DIR 39 dd 5<br />

ROL ,X IX 79 5<br />

ROL (opr),X IX1 69 ff 6<br />

270 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

ROR Rotate Right thru Carry ROR<br />

Operation<br />

Description Shift all bits of ACCA, X, or M one place to the right. Bit 7 is loaded from<br />

the C bit. The rotate operations include the carry bit to allow extension<br />

of the shift and rotate operations to multiple bytes. For example, to shift<br />

a 24-bit value right one bit, the sequence {LSR HIGH, ROR MID, ROR<br />

LOW} could be used where LOW, MID, and HIGH refer to the low-order,<br />

middle, and high-order bytes of the 24-bit value, respectively.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

C<br />

b7 – – – – – – b0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

C b0<br />

Set if, before the rotate, the LSB of ACCA, X, or M was set; cleared<br />

otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

271<br />

C<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

RORA INH (A) 46 3<br />

RORX INH (X) 56 3<br />

ROR (opr) DIR 36 dd 5<br />

ROR ,X IX 76 5<br />

ROR (opr),X IX1 66 ff 6


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

RSP Reset Stack Pointer RSP<br />

Operation SP ← $00FF<br />

Description Resets the stack pointer to the top of the stack.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

RSP INH 9C 2<br />

272 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

RTI Return from Interrupt RTI<br />

Operation SP ← (SP) + $0001; ↑ CCR Restore CCR from stack<br />

SP ← (SP) + $0001; ↑ ACCA Restore ACCA from stack<br />

SP ← (SP) + $0001; ↑ X Restore X from stack<br />

SP ← (SP) + $0001; ↑ PCH Restore PCH from stack<br />

SP ← (SP) + $0001; ↑ PCL Restore PCL from stack<br />

Description The condition codes, accumulator, the index register, and the program<br />

counter are restored to the state previously saved on the stack. The 1-bit<br />

will be reset if the corresponding bit stored on the stack is 0.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 ↕ ↕ ↕ ↕ ↕<br />

Set or cleared according to the byte pulled from the stack.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

RTI INH 80 9<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

273


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

RTS Return from Subroutine RTS<br />

Operation SP ← (SP) + $0001; ↑ PCH Restore PCH from stack<br />

SP ← (SP) + $0001; ↑ PCL Restore PCL from stack<br />

Description The stack pointer is incremented by one. The contents of the byte of<br />

memory that is pointed to by the stack pointer is loaded into the<br />

high-order byte of the program counter. The stack pointer is again<br />

incremented by one. The contents of the byte of memory at the address<br />

now contained in the stack pointer is loaded into the low-order eight bits<br />

of the program counter.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

RTS INH 81 6<br />

274 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

SBC Subtract with Carry SBC<br />

Operation ACCA ← (ACCA) – (M) – (C)<br />

Description Subtracts the contents of M and the contents of C from the contents of<br />

ACCA and places the result in ACCA.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if result is $00; cleared otherwise.<br />

C A7 • M7 + M7 • R7 + R7 • A7<br />

Set if absolute value of the contents of memory plus previous carry<br />

is larger than the absolute value of the accumulator; cleared<br />

otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

SBC (opr) IMM A2 ii 2<br />

SBC (opr) DIR B2 dd 3<br />

SBC (opr) EXT C2 hh ll 4<br />

SBC ,X IX F2 3<br />

SBC (opr),X IX1 E2 ff 4<br />

SBC (opr),X IX2 D2 ee ff 5<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

275


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

SEC Set Carry Bit SEC<br />

Operation C bit ← 1<br />

Description Sets the C bit in the CCR. SEC may be used to set up the C bit prior to<br />

a shift or rotate instruction that involves the C bit.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

C 1<br />

Set<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> 1<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

SEC INH 99 2<br />

276 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

SEI Set Interrupt Mask Bit SEI<br />

Operation I bit ← 1<br />

Description Sets the interrupt mask bit in the CCR. The microprocessor is inhibited<br />

from servicing interrupts while the I bit is set.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

I 1<br />

Set<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> 1 <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

SEI INH 9B 2<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

277


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

STA Store Accumulator in Memory STA<br />

Operation M ← (ACCA)<br />

Description Stores the contents of ACCA in memory. The contents of ACCA remain<br />

unchanged.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N A7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z A7 • A6 • A5 • A4 • A3 • A2 • A1 • A0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

STA (opr) DIR B7 dd 4<br />

STA (opr) EXT C7 hh ll 5<br />

STA ,X IX F7 4<br />

STA (opr),X IX1 E7 ff 5<br />

STA (opr),X IX2 D7 ee ff 6<br />

278 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

STOP Enable IRQ, Stop Oscillator STOP<br />

Description Reduces power consumption by eliminating all dynamic power<br />

dissipation. This results in: 1) timer prescaler cleared, 2) timer interrupts<br />

disabled, 3) timer interrupt flag cleared, 4) external interrupt request<br />

enabled, and 5) oscillator inhibited.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

When the RESET or IRQ input goes low, the oscillator is enabled, a<br />

delay of 1920 processor clock cycles is initiated allowing the oscillator to<br />

stabilize, the interrupt request vector or reset vector is fetched, and the<br />

service routine is executed, depending on which signal was applied.<br />

External interrupts are enabled following the STOP command.<br />

I 0<br />

Cleared<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> 0 <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

STOP INH 8E 2<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

279


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

STX Store Index Register X in Memory STX<br />

Operation M ← (X)<br />

Description Stores the contents of X in memory. The contents of X remain<br />

unchanged.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N X7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z X7 • X6 • X5 • X4 • X3 • X2 • X1 • X0<br />

Set if result is $00; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

STX (opr) DIR BF ii 4<br />

STX (opr) EXT CF hh ii 5<br />

STX ,X IX FF 4<br />

STX (opr),X IX1 EF ff 5<br />

STX (opr),X IX2 DF ee ff 6<br />

280 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

SUB Subtract SUB<br />

Operation ACCA ← (ACCA) – (M)<br />

Description Subtracts the contents of M from the contents of ACCA and places the<br />

result in ACCA.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ ↕<br />

N R7<br />

Set if MSB of result is set; cleared otherwise.<br />

Z R7 • R6 • R5 • R4 • R3 • R2 • R1 • R0<br />

Set if all bits of the result are cleared; cleared otherwise.<br />

C A7 • M7 + M7 • R7 + R7 • A7<br />

The C bit (carry flag) in the condition code register gets set if the absolute<br />

value of the contents of memory is larger than the absolute<br />

value of the accumulator; cleared otherwise.<br />

Source<br />

Forms<br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

SUB (opr) IMM A0 ii 2<br />

SUB (opr) DIR B0 dd 3<br />

SUB (opr) EXT C0 hh ll 4<br />

SUB ,X IX F0 3<br />

SUB (opr),X IX1 E0 ff 4<br />

SUB (opr),X IX2 D0 ee ff 5<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

281


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

SWI Software Interrupt SWI<br />

Operation PC ← (PC) + $0001 Advance PC to return address<br />

↓ (PCL); SP ← (SP) – $0001 Push low-order return address<br />

onto stack<br />

↓ (PCH); SP ← (SP) – $0001 Push high-order return address<br />

onto stack<br />

↓ (X); SP ← (SP) – $0001 Push index register onto stack<br />

↓ (ACCA); SP ← (SP) – $0001 Push accumulator onto stack<br />

↓ (CCR); SP ← (SP) – $0001 Push CCR onto stack<br />

I bit ← 1<br />

PCH ← ($xFFC) Vector fetch (x = 1 or 3 depending on<br />

PCL ← ($xFFD) <strong>M68HC05</strong> device)<br />

Description The program counter is incremented by one. The program counter,<br />

index register, and accumulator are pushed onto the stack. The CCR<br />

bits are then pushed onto the stack, with bits H, I, N, Z, and C going into<br />

bit positions 4–0 and bit positions 7, 6, and 5 containing ones. The stack<br />

pointer is decremented by one after each byte of data is stored on the<br />

stack. The interrupt mask bit is then set. The program counter is then<br />

loaded with the address stored in the SWI vector (located at memory<br />

locations n–0002 and n–0003, where n is the address corresponding to<br />

a high state on all lines of the address bus). The address of the SWI<br />

vector can be expressed as $xFFC:$xFFD, where x is 1 or 3 depending<br />

on the <strong>M68HC05</strong> device being used. This instruction is not maskable by<br />

the I bit.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

I 1<br />

Set<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> 1 <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

SWI INH 83 10<br />

282 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

TAX Transfer Accumulator to Index Register TAX<br />

Operation X ← (ACCA)<br />

Description Loads the index register with the contents of the accumulator. The<br />

contents of the accumulator are unchanged.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

TAX INH 97 2<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

283


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

TST Test for Negative or Zero TST<br />

Operation (ACCA) – $00 or: (X) – $00 or: (M) – $00<br />

Description Sets the condition codes N and Z according to the contents of ACCA, X,<br />

or M. The contents of ACCA, X, and M are not altered.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

N M7<br />

Set if the MSB of the contents of ACCA, X, or M is set; cleared otherwise.<br />

Z M7 • M6 • M5 • M4 • M3 • M2 • M1 • M0<br />

Set if the contents of ACCA, X, or M is $00; cleared otherwise.<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> ↕ ↕ <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

TSTA INH (A) 4D 3<br />

TSTX INH (X) 5D 3<br />

TST (opr) DIR 3D dd 4<br />

TST ,X IX 7D 4<br />

TST (opr),X IX1 6D ff 5<br />

284 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

<strong>M68HC05</strong> Instruction Set<br />

TXA Transfer Index Register to Accumulator TXA<br />

Operation ACCA ← (X)<br />

Description Loads the accumulator with the contents of the index register. The<br />

contents of the index register are not altered.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

None affected<br />

Source<br />

Forms<br />

H I N Z C<br />

1 1 1 <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

TXA INH 9F 2<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

285


Freescale Semiconductor, Inc...<br />

Instruction Set Details<br />

WAIT Enable Interrupt, Stop Processor WAIT<br />

Description Reduces power consumption by eliminating most dynamic power<br />

dissipation. The timer, the timer prescaler, and the on-chip peripherals<br />

continue to operate because they are potential sources of an interrupt.<br />

Wait causes enabling of interrupts by clearing the I bit in the CCR and<br />

stops clocking of processor circuits.<br />

Condition Codes<br />

and Boolean<br />

Formulae<br />

Source Forms,<br />

Addressing<br />

Modes, Machine<br />

Code, and Cycles<br />

Freescale Semiconductor, Inc.<br />

Interrupts from on-chip peripherals may be enabled or disabled by local<br />

control bits prior to execution of the WAIT instruction.<br />

When the RESET or IRQ input goes low or when any on-chip system<br />

requests interrupt service, the processor clocks are enabled, and the<br />

reset, IRQ, or other interrupt service request is processed.<br />

I 0<br />

Cleared<br />

Source<br />

Forms<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

H I N Z C<br />

1 1 1 <strong>—</strong> 0 <strong>—</strong> <strong>—</strong> <strong>—</strong><br />

Addressing<br />

Mode<br />

Machine Code HCMOS<br />

Cycles<br />

Opcode Operand(s)<br />

WAIT INH 8F 2<br />

286 Instruction Set Details<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Contents<br />

Introduction<br />

Freescale Semiconductor, Inc.<br />

Reference Tables<br />

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287<br />

ASCII to Hexadecimal Conversion . . . . . . . . . . . . . . . . . . . . . . . . . .288<br />

Hexadecimal to Decimal Conversion. . . . . . . . . . . . . . . . . . . . . . . . .290<br />

Decimal to Hexadecimal Conversion. . . . . . . . . . . . . . . . . . . . . . . . .292<br />

Hexadecimal Values vs. <strong>M68HC05</strong> Instructions . . . . . . . . . . . . . . . .293<br />

This section includes these conversion lookup tables:<br />

• Hexadecimal to ASCII<br />

• Hexadecimal to decimal<br />

• Hexadecimal to <strong>M68HC05</strong> instruction mnemonics<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

287


Freescale Semiconductor, Inc...<br />

Reference Tables<br />

ASCII to Hexadecimal Conversion<br />

Freescale Semiconductor, Inc.<br />

The American Standard Code for Information Interchange (ASCII)<br />

provides a widely accepted standard for encoding alphanumeric<br />

information as binary numbers. The original code was designed as a<br />

7-bit code with an additional parity bit. Since most modern computers<br />

work best with 8-bit values, the code has been adapted slightly so that it<br />

is expressed as 8-bit values. The low order seven bits are the original<br />

ASCII code and the eighth bit is 0.<br />

The first 32 codes contain device control codes such as carriage return<br />

and the audible bell code. Many of these are special codes for old<br />

teletype transmissions which have similar meanings on a modern<br />

terminal or have slipped into disuse.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

288 Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Reference Tables<br />

ASCII to Hexadecimal Conversion<br />

.<br />

Table 19. Hexadecimal to ASCII Conversion<br />

Hex ASCII Hex ASCII Hex ASCII Hex ASCII<br />

$00 NUL $20 SP space $40 @ $60 ` grave<br />

$01 SOH $21 ! $41 A $61 a<br />

$02 STX $22 “ quote $42 B $62 b<br />

$03 ETX $23 # $43 C $63 c<br />

$04 EOT $24 $ $44 D $64 d<br />

$05 ENQ $25 % $45 E $65 e<br />

$06 ACK $26 & $46 F $66 f<br />

$07 BEL beep $27 ‘ apost. $47 G $67 g<br />

$08 BS back sp $28 ( $48 H $68 h<br />

$09 HT tab $29 ) $49 I $69 i<br />

$0A LF linefeed $2A * $4A J $6A j<br />

$0B VT $2B + $4B K $6B k<br />

$0C FF $2C , comma $4C L $6C l<br />

$0D CR return $2D - dash $4D M $6D m<br />

$0E SO $2E . period $4E N $6E n<br />

$0F SI $2F / $4F O $6F o<br />

$10 DLE $30 0 $50 P $70 p<br />

$11 DC1 $31 1 $51 Q $71 q<br />

$12 DC2 $32 2 $52 R $72 r<br />

$13 DC3 $33 3 $53 S $73 s<br />

$14 DC4 $34 4 $54 T $74 t<br />

$15 NAK $35 5 $55 U $75 u<br />

$16 SYN $36 6 $56 V $76 v<br />

$17 ETB $37 7 $57 W $77 w<br />

$18 CAN $38 8 $58 X $78 x<br />

$19 EM $39 9 $59 Y $79 y<br />

$1A SUB $3A : $5A Z $7A z<br />

$1B ESCAPE $3B ; $5B [ $7B {<br />

$1C FS $3C < $5C \ $7C |<br />

$1D GS $3D = $5D ] $7D }<br />

$1E RS $3E > $5E ^ $7E ~<br />

$1F US $3F ? $5F _ under $7F DEL delete<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

289


Freescale Semiconductor, Inc...<br />

Reference Tables<br />

Hexadecimal to Decimal Conversion<br />

Freescale Semiconductor, Inc.<br />

To convert a hexadecimal number (up to four hexadecimal digits) to<br />

decimal, look up the decimal equivalent of each hexadecimal digit in<br />

Table 20. The decimal equivalent of the original hexadecimal number is<br />

the sum of the weights found in the table for all hexadecimal digits.<br />

Example: Find the decimal equivalent of $3E7.<br />

The decimal equivalent of the 3 in the third hex digit is 768.<br />

The decimal equivalent of the E in the second hex digit is 224.<br />

The decimal equivalent of the 7 in the first hex digit is 7.<br />

768<br />

224<br />

+ 7<br />

= 999<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

$3E7 = 999 10<br />

290 Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Reference Tables<br />

Hexadecimal to Decimal Conversion<br />

Table 20. Hexadecimal to Decimal Conversion<br />

15 Bit 8 7 Bit 0<br />

15 12 11 8 7 4 3 0<br />

4th Hex Digit 3rd Hex Digit 2nd Hex Digit 1st Hex Digit<br />

Hex Decimal Hex Decimal Hex Decimal Hex Decimal<br />

0 0 0 0 0 0 0 0<br />

1 4,096 1 256 1 16 1 1<br />

2 8,192 2 512 2 32 2 2<br />

3 12,288 3 768 3 48 3 3<br />

4 16,384 4 1,024 4 64 4 4<br />

5 20,480 5 1,280 5 80 5 5<br />

6 24,576 6 1,536 6 96 6 6<br />

7 28,672 7 1,792 7 112 7 7<br />

8 32,768 8 2,048 8 128 8 8<br />

9 36,864 9 2,304 9 144 9 9<br />

A 40,960 A 2,560 A 160 A 10<br />

B 45,056 B 2,816 B 176 B 11<br />

C 49,152 C 3,072 C 192 C 12<br />

D 53,248 D 3,328 D 208 D 13<br />

E 57,344 E 3,484 E 224 E 14<br />

F 61,440 F 3,840 F 240 F 15<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

291


Freescale Semiconductor, Inc...<br />

Reference Tables<br />

Decimal to Hexadecimal Conversion<br />

Freescale Semiconductor, Inc.<br />

To convert a decimal number (up to 65,53510) to hexadecimal, find the<br />

largest decimal number in Table 20 that is less than or equal to the<br />

number you are converting. The corresponding hexadecimal digit is the<br />

most significant hexadecimal digit of the result. Subtract the decimal<br />

number found from the original decimal number to get the remaining<br />

decimal value. Repeat the procedure using the remaining decimal value<br />

for each subsequent hexadecimal digit.<br />

Example: Find the hexadecimal equivalent of 77710.<br />

The largest decimal number from Table 20, that is less than or equal to<br />

77710, is 76810. This corresponds to a $3 in the third hexadecimal digit.<br />

Subtract this 76810 from 77710 to get the remaining decimal value 910.<br />

Next look in the column for the next lower order hexadecimal digit (2nd<br />

hex digit in this case). Find the largest decimal value that is less than or<br />

equal to the remaining decimal value. The largest decimal value in this<br />

column that is less than or equal to 910 is 0, so you would place a 0 in<br />

the second hex digit of your result.<br />

910 minus 0 is the remaining decimal value 910.<br />

Next look in the column for the next lower order hexadecimal digit (first<br />

hex digit in this case). Find the largest decimal value that is less than or<br />

equal to the remaining decimal value. The largest decimal value in this<br />

column that is less than or equal to 910 is 9, so you would place a 9 in<br />

the first hex digit of your result.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

777 10 = $309<br />

292 Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Hexadecimal Values vs. <strong>M68HC05</strong> Instructions<br />

Reference Tables<br />

Hexadecimal Values vs. <strong>M68HC05</strong> Instructions<br />

Table 21 lists all hexadecimal values from $00 to $FF and the equivalent<br />

<strong>M68HC05</strong> instructions with their addressing modes. Since there are only<br />

210 <strong>M68HC05</strong> instructions, 46 of the hexadecimal values do not<br />

correspond to a legal instruction.<br />

Table 21. Hexadecimal to <strong>M68HC05</strong> Instruction Mnemonics (Sheet 1 of 5)<br />

Operand Instruction<br />

Freescale Semiconductor, Inc.<br />

Addressing<br />

Mode<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

$00 BRSET0 Direct $20 BRA Relative<br />

$01 BRCLR0 Direct $21 BRN Relative<br />

$02 BRSET1 Direct $22 BHI Relative<br />

$03 BRCLR1 Direct $23 BLS Relative<br />

$04 BRSET2 Direct $24 BCC Relative<br />

$05 BRCLR2 Direct $25 BCS Relative<br />

$06 BRSET3 Direct $26 BNE Relative<br />

$07 BRCLR3 Direct $27 BEQ Relative<br />

$08 BRSET4 Direct $28 BHCC Relative<br />

$09 BRCLR4 Direct $29 BHCS Relative<br />

$0A BRSET5 Direct $2A BPL Relative<br />

$0B BRCLR5 Direct $2B BMI Relative<br />

$0C BRSET6 Direct $2C BMC Relative<br />

$0D BRCLR6 Direct $2D BMS Relative<br />

$0E BRSET7 Direct $2E BIL Relative<br />

$0F BRCLR7 Direct $2F BIH Relative<br />

$10 BSET0 Direct $30 NEG Direct<br />

$11 BCLR0 Direct $31 <strong>—</strong> <strong>—</strong><br />

$12 BSET1 Direct $32 <strong>—</strong> <strong>—</strong><br />

$13 BCLR1 Direct $33 COM Direct<br />

$14 BSET2 Direct $34 LSR Direct<br />

$15 BCLR2 Direct $35 <strong>—</strong> <strong>—</strong><br />

$16 BSET3 Direct $36 ROR Direct<br />

$17 BCLR3 Direct $37 ASR Direct<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

293


Freescale Semiconductor, Inc...<br />

Reference Tables<br />

Freescale Semiconductor, Inc.<br />

Table 21. Hexadecimal to <strong>M68HC05</strong> Instruction Mnemonics (Sheet 2 of 5)<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

$18 BSET4 Direct $38 LSL Direct<br />

$19 BCLR4 Direct $39 ROL Direct<br />

$1A BSET5 Direct $3A DEC Direct<br />

$1B BCLR5 Direct $3B <strong>—</strong> <strong>—</strong><br />

$1C BSET6 Direct $3C INC Direct<br />

$1D BCLR6 Direct $3D TST Direct<br />

$1E BSET7 Direct $3E <strong>—</strong> <strong>—</strong><br />

$1F BCLR7 Direct $3F CLR Direct<br />

$40 NEGA Inherent $60 NEG Indexed 1<br />

$41 <strong>—</strong> <strong>—</strong> $61 <strong>—</strong> <strong>—</strong><br />

$42 <strong>—</strong> <strong>—</strong> $62 <strong>—</strong> <strong>—</strong><br />

$43 COMA Inherent $63 COM Indexed 1<br />

$44 LSRA Inherent $64 LSR Indexed 1<br />

$45 <strong>—</strong> <strong>—</strong> $65 <strong>—</strong> <strong>—</strong><br />

$46 RORA Inherent $66 ROR Indexed 1<br />

$47 ASRA Inherent $67 ASR Indexed 1<br />

$48 LSLA Inherent $68 LSL Indexed 1<br />

$49 ROLA Inherent $69 ROL Indexed 1<br />

$4A DECA Inherent $6A DEC Indexed 1<br />

$4B <strong>—</strong> <strong>—</strong> $6B <strong>—</strong> <strong>—</strong><br />

$4C INCA Inherent $6C INC Indexed 1<br />

$4D TSTA Inherent $6D TST Indexed 1<br />

$4E <strong>—</strong> <strong>—</strong> $6E <strong>—</strong> <strong>—</strong><br />

$4F CLRA Inherent $6F CLR Indexed 1<br />

$50 NEGX Inherent $70 NEG Indexed 0<br />

$51 <strong>—</strong> <strong>—</strong> $71 <strong>—</strong> <strong>—</strong><br />

$52 <strong>—</strong> <strong>—</strong> $72 <strong>—</strong> <strong>—</strong><br />

$53 COMX Inherent $73 COM Indexed 0<br />

$54 LSRX Inherent $74 LSR Indexed 0<br />

$55 <strong>—</strong> <strong>—</strong> $75 <strong>—</strong> <strong>—</strong><br />

$56 RORX Inherent $76 ROR Indexed 0<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

294 Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Reference Tables<br />

Hexadecimal Values vs. <strong>M68HC05</strong> Instructions<br />

Table 21. Hexadecimal to <strong>M68HC05</strong> Instruction Mnemonics (Sheet 3 of 5)<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

$57 ASRX Inherent $77 ASR Indexed 0<br />

$58 LSLX Inherent $78 LSL Indexed 0<br />

$59 ROLX Inherent $79 ROL Indexed 0<br />

$5A DECX Inherent $7A DEC Indexed 0<br />

$5B <strong>—</strong> <strong>—</strong> $7B <strong>—</strong> <strong>—</strong><br />

$5C INCX Inherent $7C INC Indexed 0<br />

$5D TSTX Inherent $7D TST Indexed 0<br />

$5E <strong>—</strong> <strong>—</strong> $7E <strong>—</strong> <strong>—</strong><br />

$5F CLRX Inherent $7F CLR Indexed 0<br />

$80 RTI Inherent $A0 SUB Immediate<br />

$81 RTS Inherent $A1 CMP Immediate<br />

$82 <strong>—</strong> <strong>—</strong> $A2 SBC Immediate<br />

$83 SWI Inherent $A3 CPX Immediate<br />

$84 <strong>—</strong> <strong>—</strong> $A4 AND Immediate<br />

$85 <strong>—</strong> <strong>—</strong> $A5 BIT Immediate<br />

$86 <strong>—</strong> <strong>—</strong> $A6 LDA Immediate<br />

$87 <strong>—</strong> <strong>—</strong> $A7 <strong>—</strong> <strong>—</strong><br />

$88 <strong>—</strong> <strong>—</strong> $A8 EOR Immediate<br />

$89 <strong>—</strong> <strong>—</strong> $A9 ADC Immediate<br />

$8A <strong>—</strong> <strong>—</strong> $AA ORA Immediate<br />

$8B <strong>—</strong> <strong>—</strong> $AB ADD Immediate<br />

$8C <strong>—</strong> <strong>—</strong> $AC <strong>—</strong> <strong>—</strong><br />

$8D <strong>—</strong> <strong>—</strong> $AD BSR Relative<br />

$8E STOP Inherent $AE LDX Immediate<br />

$8F WAIT Inherent $AF <strong>—</strong> <strong>—</strong><br />

$90 <strong>—</strong> <strong>—</strong> $B0 SUB Direct<br />

$91 <strong>—</strong> <strong>—</strong> $B1 CMP Direct<br />

$92 <strong>—</strong> <strong>—</strong> $B2 SBC Direct<br />

$93 <strong>—</strong> <strong>—</strong> $B3 CPX Direct<br />

$94 <strong>—</strong> <strong>—</strong> $B4 AND Direct<br />

$95 <strong>—</strong> <strong>—</strong> $B5 BIT Direct<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

295


Freescale Semiconductor, Inc...<br />

Reference Tables<br />

Freescale Semiconductor, Inc.<br />

Table 21. Hexadecimal to <strong>M68HC05</strong> Instruction Mnemonics (Sheet 4 of 5)<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

$96 <strong>—</strong> <strong>—</strong> $B6 LDA Direct<br />

$97 TAX Inherent $B7 STA Direct<br />

$98 CLC Inherent $B8 EOR Direct<br />

$99 SEC Inherent $B9 ADC Direct<br />

$9A CLI Inherent $BA ORA Direct<br />

$9B SEI Inherent $BB ADD Direct<br />

$9C RSP Inherent $BC JMP Direct<br />

$9D NOP Inherent $BD JSR Direct<br />

$9E <strong>—</strong> <strong>—</strong> $BE LDX Direct<br />

$9F TXA Inherent $BF STX Direct<br />

$C0 SUB Extended $E0 SUB Indexed 1<br />

$C1 CMP Extended $E1 CMP Indexed 1<br />

$C2 SBC Extended $E2 SBC Indexed 1<br />

$C3 CPX Extended $E3 CPX Indexed 1<br />

$C4 AND Extended $E4 AND Indexed 1<br />

$C5 BIT Extended $E5 BIT Indexed 1<br />

$C6 LDA Extended $E6 LDA Indexed 1<br />

$C7 STA Extended $E7 STA Indexed 1<br />

$C8 EOR Extended $E8 EOR Indexed 1<br />

$C9 ADC Extended $E9 ADC Indexed 1<br />

$CA ORA Extended $EA ORA Indexed 1<br />

$CB ADD Extended $EB ADD Indexed 1<br />

$CC JMP Extended $EC JMP Indexed 1<br />

$CD JSR Extended $ED JSR Indexed 1<br />

$CE LDX Extended $EE LDX Indexed 1<br />

$CF STX Extended $EF STX Indexed 1<br />

$D0 SUB Indexed 2 $F0 SUB Indexed 0<br />

$D1 CMP Indexed 2 $F1 CMP Indexed 0<br />

$D2 SBC Indexed 2 $F2 SBC Indexed 0<br />

$D3 CPX Indexed 2 $F3 CPX Indexed 0<br />

$D4 AND Indexed 2 $F4 AND Indexed 0<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

296 Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Reference Tables<br />

Hexadecimal Values vs. <strong>M68HC05</strong> Instructions<br />

Table 21. Hexadecimal to <strong>M68HC05</strong> Instruction Mnemonics (Sheet 5 of 5)<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

Operand Instruction<br />

Addressing<br />

Mode<br />

$D5 BIT Indexed 2 $F5 BIT Indexed 0<br />

$D6 LDA Indexed 2 $F6 LDA Indexed 0<br />

$D7 STA Indexed 2 $F7 STA Indexed 0<br />

$D8 EOR Indexed 2 $F8 EOR Indexed 0<br />

$D9 ADC Indexed 2 $F9 ADC indexed 0<br />

$DA ORA Indexed 2 $FA ORA Indexed 0<br />

$DB ADD Indexed 2 $FB ADD Indexed 0<br />

$DC JMP Indexed 2 $FC JMP Indexed 0<br />

$DD JSR Indexed 2 $FD JSR Indexed 0<br />

$DE LDX Indexed 2 $FE LDX Indexed 0<br />

$DF STX Indexed 2 $FF STX Indexed 0<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

297


Freescale Semiconductor, Inc...<br />

Reference Tables<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

298 Reference Tables<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Glossary<br />

1 K <strong>—</strong> One kilobyte or 102410 bytes. Similar to the use of the prefix in<br />

kilogram, which means 1000 grams in the decimal numbering<br />

system. 1024 is 210 .<br />

8-bit MCU <strong>—</strong> A microcontroller where data is communicated over a data<br />

bus made up of eight separate data conductors. Members of the<br />

<strong>M68HC05</strong> <strong>Family</strong> of microcontrollers are 8-bit MCUs.<br />

A <strong>—</strong> Abbreviation for accumulator in the <strong>M68HC05</strong> MCU<br />

accumulator <strong>—</strong> An 8-bit register in the CPU of the <strong>M68HC05</strong>. The<br />

contents of this register may be used as an operand of an<br />

arithmetic or logical instruction.<br />

addressing mode <strong>—</strong> The way that the CPU obtains (addresses) the<br />

information needed to complete an instruction. The <strong>M68HC05</strong><br />

CPU has six addressing modes:<br />

• Inherent <strong>—</strong> The CPU needs no additional information from<br />

memory to complete the instruction.<br />

• Immediate <strong>—</strong> The information needed to complete the<br />

instruction is located in the next memory location(s) after<br />

the opcode.<br />

• Direct <strong>—</strong> The low-order byte of the address of the operand<br />

is located in the next memory location after the opcode, and<br />

the high-order byte of the operand address is assumed to<br />

be $00.<br />

• Extended <strong>—</strong> The high-order byte of the address of the<br />

operand is located in the next memory location after the<br />

opcode, and the low-order byte of the operand address is<br />

located in the next memory location after that.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

299


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

• Indexed <strong>—</strong> The address of the operand depends upon the<br />

current value in the X index register and a 0-, 8-, or 16-bit,<br />

instruction-provided value.<br />

• Relative <strong>—</strong> Used for branch instructions to specify the<br />

destination address where processing will continue if the<br />

branch condition is true.<br />

address bus <strong>—</strong> The set of conductors that are used to select a specific<br />

memory location so the CPU can write information into the<br />

memory location or read its contents. If a computer has 11 wires<br />

in its address bus, it can address 211 or 204810 memory<br />

locations. In most <strong>M68HC05</strong> MCUs, the address bus is not<br />

accessible on external pins.<br />

ALU <strong>—</strong> Arithmetic logic unit. This is the portion of the CPU of a computer<br />

where mathematical and logical operations take place. Other<br />

circuitry decodes each instruction and configures the ALU to<br />

perform the necessary arithmetic or logical operations at each<br />

step of an instruction.<br />

ASCII <strong>—</strong> American Standard Code for Information Interchange. A widely<br />

accepted correlation between alphabetic and numeric characters<br />

and specific 7-bit binary numbers. Refer to Table 19.<br />

Hexadecimal to ASCII Conversion.<br />

analog <strong>—</strong> A signal that can have voltage level values that are neither the<br />

V SS level nor the V DD level. For a computer to use such signals,<br />

they must be converted into a binary number that corresponds to<br />

the voltage level of the signal. An analog-to-digital converter can<br />

be used to perform this conversion. By contrast, a digital signal<br />

has only two possible values, 1 (≈V DD ) or 0 (≈V SS ).<br />

application programs <strong>—</strong> Software programs that instruct a computer to<br />

solve an application problem<br />

arithmetic logic unit <strong>—</strong> This is the portion of the CPU of a computer<br />

where mathematical and logical operations take place. Other<br />

circuitry decodes each instruction and configures the ALU to<br />

perform the necessary arithmetic or logical operations at each<br />

step of an instruction.<br />

300 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

assembler <strong>—</strong> A software program that translates source code<br />

mnemonics into opcodes that can then be loaded into the<br />

memory of a microcontroller.<br />

Glossary<br />

assembly language <strong>—</strong> Instruction mnemonics and assembler<br />

directives that are meaningful to programmers and can be<br />

translated into an object code program that a microcontroller<br />

understands. The CPU uses opcodes and binary numbers to<br />

specify the operations that make up a computer program. These<br />

numbers are not meaningful to people, so programmers use<br />

assembly language mnemonics to represent instructions.<br />

Assembler directives provide additional information such as the<br />

starting memory location for a program. Labels are used to mean<br />

an address or binary value.<br />

base 2 <strong>—</strong> Binary numbers that use only the two digits, 0 and 1. Base 2<br />

is the numbering system used by computers.<br />

base 10 <strong>—</strong> Decimal numbers that use the 10 digits, 0 through 9. This is<br />

the customary numbering system used by people.<br />

base 16 <strong>—</strong> The hexadecimal numbering system. The 16 characters (0<br />

through 9 and the letters A through F) are used to represent<br />

hexadecimal values. One hexadecimal digit can exactly<br />

represent a 4-bit binary value. Hexadecimal is used by people to<br />

represent binary values because it is easier to use a 2-digit<br />

number than the equivalent 8-digit binary number. Refer to<br />

Table 1. Decimal, Binary, and Hexadecimal Equivalents.<br />

BCD <strong>—</strong> Binary coded decimal is a notation that uses binary values to<br />

represent decimal quantities. Each BCD digit uses four binary<br />

bits. Six of the possible 16 binary combinations are considered<br />

illegal.<br />

binary <strong>—</strong> The numbering system used by computers because any<br />

quantity can be represented by a series of 1s and 0s. Electrically,<br />

these 1s and 0s are represented by voltage levels of<br />

approximately VDD and VSS respectively.<br />

bit <strong>—</strong> A single binary digit. A bit can hold a single value of 0 or 1.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

301


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

black box <strong>—</strong> A hypothetical block of logic or circuitry that performs some<br />

input to output transformation. A black box is used when the input<br />

to output relationship is known but the means to achieve this<br />

transformation is not known or is not important to the discussion.<br />

branch instructions <strong>—</strong> Computer instructions that cause the CPU to<br />

continue processing at a memory location other than the next<br />

sequential address. Most branch instructions are conditional.<br />

That is, the CPU will continue to the next sequential address (no<br />

branch) if a condition is false or continue to some other address<br />

(branch) if the condition is true.<br />

breakpoint <strong>—</strong> During debugging of a program, it is useful to run<br />

instructions until the CPU gets to a specific place in the program<br />

and then enter a debugger program. A breakpoint is established<br />

at the desired address by temporarily substituting a software<br />

interrupt (SWI) instruction for the instruction at that address. In<br />

response to the SWI, control is passed to a debugging program.<br />

byte <strong>—</strong> A set of exactly eight binary bits<br />

C <strong>—</strong> Abbreviation for carry/borrow in the condition code register of the<br />

<strong>M68HC05</strong>. When adding two unsigned 8-bit numbers, the C bit is<br />

set if the result is greater than 255 ($FF).<br />

CCR <strong>—</strong> Abbreviation for condition code register in the <strong>M68HC05</strong>. The<br />

CCR has five bits (H, I, N, Z, and C) that can be used to control<br />

conditional branch instructions. The values of the bits in the CCR<br />

are determined by the results of previous operations. For<br />

example, after a load accumulator (LDA) instruction, Z will be set<br />

if the loaded value was $00.<br />

central processor unit <strong>—</strong> The part of a computer that controls<br />

execution of instructions<br />

checksum <strong>—</strong> A value that results from adding a series of binary<br />

numbers. When exchanging information between computers, a<br />

checksum gives an indication about the integrity of the data<br />

transfer. If values were transferred incorrectly, it is very unlikely<br />

that the checksum would match the value that was expected.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

302 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

clock <strong>—</strong> A square wave signal that is used to sequence events in a<br />

computer<br />

Glossary<br />

CMOS <strong>—</strong> Complimentary metal oxide semiconductor. A silicon<br />

semiconductor processing technology that allows fabrication of<br />

both N-type and P-type transistors on the same integrated circuit.<br />

Most modern microcontrollers use CMOS technology.<br />

computer program <strong>—</strong> A series of instructions that cause a computer to<br />

do something<br />

computer system <strong>—</strong> A CPU plus other components needed to perform<br />

a useful function. A minimum computer system includes a CPU,<br />

a clock, memory, a program, and input/output interfaces.<br />

condition code register <strong>—</strong> The CCR has five bits (H, I, N, Z, and C) that<br />

can be used to control conditional branch instructions. The values<br />

of the bits in the CCR are determined by the results of previous<br />

operations. For example, after a load accumulator (LDA)<br />

instruction, Z will be set if the loaded value was $00.<br />

CPU <strong>—</strong> Central processor unit. The part of a computer that controls<br />

execution of instructions<br />

CPU cycles <strong>—</strong> A CPU clock cycle is one period of the internal bus-rate<br />

clock. Normally, this clock is derived by dividing a crystal<br />

oscillator source by two or more so the high and low times will be<br />

equal. The length of time required to execute an instruction is<br />

measured in CPU clock cycles.<br />

CPU registers <strong>—</strong> Memory locations that are wired directly into the CPU<br />

logic instead of being part of the addressable memory map. The<br />

CPU always has direct access to the information in these<br />

registers. The CPU registers in an <strong>M68HC05</strong> are:<br />

• A <strong>—</strong> 8-bit accumulator<br />

• X <strong>—</strong> 8-bit index register<br />

• CCR <strong>—</strong> condition code register containing the H, I, N, Z,<br />

and C bits<br />

• SP <strong>—</strong> stack pointer<br />

• PC <strong>—</strong> program counter<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

303


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

CRT <strong>—</strong> Cathode ray tube. Also used as an informal expression to refer<br />

to a complete communication terminal that has a keyboard and a<br />

video display<br />

cycles <strong>—</strong> See CPU cycles.<br />

data bus <strong>—</strong> A set of conductors that are used to convey binary<br />

information from a CPU to a memory location or from a memory<br />

location to a CPU. In the <strong>M68HC05</strong>, the data bus is eight bits.<br />

decimal <strong>—</strong> Base 10 numbers use the digits 0 through 9. This is the<br />

numbering system normally used by humans.<br />

development tools <strong>—</strong> Software or hardware devices that are used to<br />

develop computer programs and application hardware. Examples<br />

of software development tools include text editors, assemblers,<br />

debug monitors, and simulators. Examples of hardware<br />

development tools include emulators, logic analyzers, and PROM<br />

programmers. An in-circuit simulator combines a software<br />

simulator with hardware interfaces.<br />

digital <strong>—</strong> A binary logic system where signals can have only two states,<br />

0 (≈V SS ) or 1 (≈V DD ).<br />

direct address <strong>—</strong> Any address within the first 256 addresses of memory<br />

($0000 through $00FF). The high-order byte of these addresses<br />

is always $00. Special instructions allow these addresses to be<br />

accessed using only the low-order byte of their address. These<br />

instructions automatically fill in the assumed $00 value for the<br />

high-order byte of the address.<br />

direct addressing mode <strong>—</strong> Direct addressing mode uses a<br />

program-supplied value for the low-order byte of the address of<br />

an operand. The high-order byte of the operand’s address is<br />

assumed to be $00, so it does not have to be explicitly specified.<br />

direct page <strong>—</strong> The first 256 bytes of memory, $0000 through $00FF<br />

EEPROM <strong>—</strong> Electrically erasable, programmable read-only memory. A<br />

non-volatile type of memory that can be erased and<br />

reprogrammed by program instructions. Since no special power<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

304 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Glossary<br />

supplies or ultra-violet light source is needed, the contents of this<br />

kind of memory can be changed without removing the MCU from<br />

the application system.<br />

effective address <strong>—</strong> The address where an instruction operand is<br />

located. The addressing mode of an instruction determines how<br />

the CPU calculates the effective address of the operand.<br />

embedded <strong>—</strong> When an appliance contains a microcontroller, the MCU<br />

is said to be an embedded controller. Often, the end user of the<br />

appliance is not aware (or does not care) that there is a computer<br />

inside.<br />

EPROM <strong>—</strong> Erasable, programmable read-only memory. A non-volatile<br />

type of memory that can be erased by exposure to an ultra-violet<br />

light source. MCUs that have EPROM are easily recognized<br />

because the package has a quartz window to allow exposure to<br />

the ultra-violet light. If an EPROM MCU is packaged in an opaque<br />

plastic package, it is called a one-time-programmable (OTP)<br />

MCU because there is no way to expose the EPROM to<br />

ultra-violet light.<br />

extended addressing mode <strong>—</strong> In this addressing mode, the high-order<br />

byte of the address of the operand is located in the next memory<br />

location after the opcode. The low-order byte of the operand’s<br />

address is located in the second memory location after the<br />

opcode.<br />

fetching a vector <strong>—</strong> When the CPU is reset or responds to an interrupt,<br />

the contents of a specific pair of memory locations is loaded into<br />

the program counter and processing continues from the loaded<br />

address. The process of reading these two locations is called<br />

fetching the vector.<br />

flowchart <strong>—</strong> A symbolic means to show the sequence of steps required<br />

to perform an operation. A flowchart not only tells what needs to<br />

be done, but also the order that the steps should be done in.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

305


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

H <strong>—</strong> Abbreviation for half-carry in the condition code register of the<br />

<strong>M68HC05</strong>. This bit indicates a carry from the low-order four bits<br />

of an 8-bit value to the high-order four bits. This status indicator<br />

is used during BCD calculations.<br />

half flip flop <strong>—</strong> A half flip flop (HFF) has a transparent condition and a<br />

latched condition. In the transparent condition (clock input equal<br />

logic 1), the Q output is always equal to the logic level presented<br />

at the input. In the latched condition (clock input equals logic 0),<br />

the output maintins the logic level that was present when the flip<br />

flop was last in the transparent condition.<br />

hexadecimal <strong>—</strong> The base 16 numbering system. The 16 characters (0<br />

through 9 and the letters A through F) are used to represent<br />

hexadecimal values. One hexadecimal digit can exactly<br />

represent a 4-bit binary value. Hexadecimal is used by people to<br />

represent binary values because it is easier to use a 2-digit<br />

number than the equivalent 8-digit binary number. Refer to<br />

Table 1. Decimal, Binary, and Hexadecimal Equivalents.<br />

high order <strong>—</strong> The leftmost digit(s) of a number. Five is the high-order<br />

digit of the number 57.<br />

I <strong>—</strong> Abbreviation for interrupt mask bit in the condition code register of<br />

the <strong>M68HC05</strong><br />

I/O <strong>—</strong> Input/output interfaces between a computer system and the<br />

external world. A CPU reads an input to sense the level of an<br />

external signal and writes to an output to change the level on an<br />

external signal.<br />

immediate addressing mode <strong>—</strong> In immediate addressing mode, the<br />

operand is located in the next memory location(s) after the<br />

opcode.<br />

inherent addressing mode <strong>—</strong> In inherent addressing mode, the CPU<br />

already inherently knows everything it needs to know to complete<br />

the instruction. The operands (if there are any) are in the CPU<br />

registers.<br />

in-circuit simulator <strong>—</strong> A simulator with hardware interfaces that allows<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

306 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Glossary<br />

connection into an application circuit. The in-circuit simulator<br />

replaces the MCU and behaves as a real MCU would. The<br />

developer has greater control and visibility of internal MCU<br />

operations because they are being simulated by instructions in<br />

the host computer. An in-circuit simulator, like other simulators, is<br />

not as fast as a real MCU.<br />

indexed addressing mode <strong>—</strong> In indexed addressing mode, the current<br />

value of the index register is added to a 0-, 8-, or 16-bit value in<br />

the instruction to get the effective address of the operand. There<br />

are separate opcodes for 0-, 8-, and 16-bit variations of indexed<br />

mode instructions, so the CPU knows how many additional<br />

memory locations to read after the opcode.<br />

index register (X) <strong>—</strong> An 8-bit CPU register in the <strong>M68HC05</strong> that is used<br />

in indexed addressing mode. X, its abbreviation, can also be used<br />

as a generalpurpose<br />

8-bit register (in addition to the 8-bit accumulator).<br />

input/output <strong>—</strong> Interfaces between a computer system and the external<br />

world. A CPU reads an input to sense the level of an external<br />

signal and writes to an output to change the level on an external<br />

signal.<br />

instruction decoder <strong>—</strong> The portion of a CPU that receives an<br />

instruction opcode and produces the necessary control signals so<br />

that the rest of the CPU will perform the desired operations.<br />

instructions <strong>—</strong> Instructions are operations that a CPU can perform.<br />

Instructions are expressed by programmers as assembly<br />

language mnemonics. A CPU interprets an opcode and its<br />

associated operand(s) as an instruction.<br />

instruction set <strong>—</strong> The instruction set of a CPU is the set of all<br />

operations that the CPU knows how to perform. One way to<br />

represent an instruction set is with a set of shorthand mnemonics,<br />

such as LDA meaning load A. Another representation of an<br />

instruction set is the set of opcodes that are recognized by the<br />

CPU.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

307


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

inverter <strong>—</strong> A simple logic circuit that produces an output logic level that<br />

is the opposite of the level presented to its input.<br />

kilobyte <strong>—</strong> One kilobyte is 102410 bytes. Similar to the use of the prefix<br />

in kilogram, which means 1000 grams in the decimal numbering<br />

system. 1024 is 210 .<br />

label <strong>—</strong> A name that is assigned (by a programmer) to a specific<br />

address or binary value. When a program containing a lable is<br />

assembled, the label is replaced by the binary value it represents.<br />

Programs typically include many labels.<br />

latch <strong>—</strong> A logic circuit that maintains a stable output state even after the<br />

input has been removed or changed. A clock control input<br />

determines when the latch will capture the input state and apply<br />

it to the output.<br />

least significant bit <strong>—</strong> LSB, the rightmost digit of a binary value<br />

listing <strong>—</strong> A program listing shows the binary numbers that the CPU<br />

needs alongside the assembly language statements that the<br />

programmer wrote. The listing is generated by an assembler in<br />

the process of translating assembly language source statements<br />

into the binary information that the CPU needs.<br />

logic 1 <strong>—</strong> A voltage level approximately equal to the VDD power supply<br />

logic 0 <strong>—</strong> A voltage level approximately equal to VSS (ground)<br />

low order <strong>—</strong> The rightmost digit(s) of a number. Seven is the low-order<br />

digit of the number 57.<br />

LSB <strong>—</strong> Least significant bit. The rightmost digit of a binary value<br />

machine codes <strong>—</strong> The binary codes that are processed by the CPU as<br />

instructions. Machine code includes both opcodes and operand<br />

data.<br />

mainframe computer <strong>—</strong> A large computer system that is usually<br />

confined to a special room. Mainframe computers are used for<br />

large information processing jobs likemaintaining a database of<br />

all policyholders for an insurance company.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

308 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Glossary<br />

mass storage <strong>—</strong> A very large capacity storage device such as a<br />

magnetic disk. Information in a mass storage device takes longer<br />

to access than information in the memory map of a CPU.<br />

MCU <strong>—</strong> Microcontroller unit. A complete computer system, including a<br />

CPU, memory, a clock oscillator, and I/O on a single integrated<br />

circuit.<br />

memory location <strong>—</strong> In the <strong>M68HC05</strong>, each memory location holds one<br />

byte of data and has a unique address. To store information into<br />

a memory location, the CPU places the address of the location on<br />

the address bus, the data information on the data bus, and<br />

asserts the write signal. To read information from a memory<br />

location, the CPU places the address of the location on the<br />

address bus and asserts the read signal. In response to the read<br />

signal, the selected memory location places its data onto the data<br />

bus.<br />

memory map <strong>—</strong> A pictorial representation of all memory locations in a<br />

computer system. A memory map is similar to a city street map in<br />

that it shows where things are located.<br />

memory-mapped I/O <strong>—</strong> In this type of system, I/O and control registers<br />

are accessed in the same way as RAM or ROM memory<br />

locations. Any instruction that can be used to access memory can<br />

also be used to access I/O registers.<br />

microcontroller <strong>—</strong> A complete computer system, including a CPU,<br />

memory, a clock oscillator, and I/O on a single integrated circuit.<br />

microprocessor <strong>—</strong> A microprocessor is similar to a microcontroller<br />

except that one or more of the subsystems needed to make a<br />

complete computer system is not included on the same chip with<br />

the CPU. A microprocessor typically includes a CPU and a clock<br />

oscillator but does not include program memory or I/O registers.<br />

mnemonic <strong>—</strong> Three to five letters that represent a computer operation.<br />

For example, the mnemonic form of the load accumulator<br />

instruction is LDA.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

309


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

monitor program <strong>—</strong> A software program that is intended to assist in<br />

system development. A typical monitor program allows a user to<br />

examine and change memory or CPU register contents, set<br />

breakpoints, and selectively execute application programs.<br />

most significant bit <strong>—</strong> The leftmost digit of a binary value<br />

MSB <strong>—</strong> Most significant bit. The leftmost digit of a binary value<br />

N <strong>—</strong> Abbreviation for negative, a bit in the condition code register of the<br />

<strong>M68HC05</strong>. In twos-complement computer notation, positive<br />

signed numbers have a 0 in their MSB and negative numbers<br />

have a 1 in their MSB. The N condition code bit reflects the sign<br />

of the result of an operation. After a load accumulator instruction,<br />

the N bit will be set if the MSB of the loaded value was a 1.<br />

NAND gate <strong>—</strong> A basic logic circuit. The output of a NAND gate is a logic<br />

0 when all of its inputs are logic 1s. The output of a NAND gate is<br />

a logic 1 if any of its inputs are logic 0.<br />

non-volatile <strong>—</strong> A type of memory that does not forget its contents when<br />

power is turned off. ROM, EPROM, and EEPROM are all<br />

non-volatile memories.<br />

NOR gate <strong>—</strong> A basic logic circuit. The output of a NOR gate is a logic 0<br />

when any of its inputs are logic 1s. The output of a NOR gate is a<br />

logic 1 if all of its inputs are logic 0.<br />

object code file <strong>—</strong> A text file containing numbers that represent the<br />

binary opcodes and data of a computer program. An object code<br />

file can be used to load binary information into a computer<br />

system. Motorola uses the S-record file format for object code<br />

files. See Figure 35. S-Record File for Example Program.<br />

octal <strong>—</strong> Base 8 numbers that use the characters 0 through 7 to<br />

represent sets of three binary bits. Octal is seldom used in<br />

modern computer work.<br />

one <strong>—</strong> A logic high level (≈V DD )<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

310 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Glossary<br />

ones complement <strong>—</strong> To get the logical ones-complement of a binary<br />

value, invert each bit.<br />

opcode <strong>—</strong> A binary code that instructs the CPU to do a specific<br />

operation in a specific way. The <strong>M68HC05</strong> CPU recognizes 210<br />

unique 8-bit opcodes that represent addressing mode variations<br />

of 62 basic instructions.<br />

operand <strong>—</strong> An input value to a logical or mathematical operation<br />

oscillator <strong>—</strong> A circuit that produces a constant frequency square wave<br />

that is used by the computer as a timing and sequencing<br />

reference. A microcontroller typically includes all elements of this<br />

circuit except the frequency-determining component(s), the<br />

crystal or R-C (resistor-capacitor)components.<br />

OTP <strong>—</strong> See OTPROM.<br />

OTPROM <strong>—</strong> A non-volatile type of memory that can be programmed but<br />

cannot be erased. An OTPROM is an EPROM MCU that is<br />

packaged in an opaque plastic package. It is called a<br />

one-time-programmable MCU because there is no way to expose<br />

the EPROM to ultra-violet light.<br />

parity <strong>—</strong> An extra bit in a binary word that is intended to indicate the<br />

validity of the remaining bits in the word. In even parity, the parity<br />

bit is set or cleared as needed to make the total number of logic<br />

1s in the word (including the parity bit) equal to an even number<br />

(0, 2, 4, etc.).<br />

PC <strong>—</strong> Abbreviation for program counter, a CPU register in the <strong>M68HC05</strong><br />

MCU. Also used as an abbreviation for personal computer.<br />

personal computer <strong>—</strong> A small computer system that is normally used<br />

by a single person to process information<br />

playing computer <strong>—</strong> A learning technique in which you pretend to be a<br />

CPU that is executing the instructions of a program<br />

pointer register <strong>—</strong> An index register is sometimes called a pointer<br />

register because its contents are used in the calculation of the<br />

address of an operand. A straightforward example is an<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

311


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

indexed-no offset instruction where the X register contains the<br />

direct address of (points to) the operand.<br />

program <strong>—</strong> A set of computer instructions that cause a computer to<br />

perform an application task<br />

program counter <strong>—</strong> The program counter (PC) is the CPU register that<br />

holds the address of the next instruction or operand that the CPU<br />

will use.<br />

programming model <strong>—</strong> The registers of a particular CPU. The<br />

programming model of the <strong>M68HC05</strong> CPU is shown in Figure 23.<br />

Programming Model.<br />

PROM <strong>—</strong> Programmable read-only memory. A non-volatile type of<br />

memory that can be programmed after it is manufactured.<br />

EPROM and EEPROM are two types of PROM memory.<br />

pulled <strong>—</strong> The act of reading a value from the stack. In the <strong>M68HC05</strong>, a<br />

value is pulled by this sequence of operations: First, the stack<br />

pointer register is incremented so that it points at the last value<br />

that was saved on the stack. Next the value that is at the address<br />

contained in the stack pointer register is read into the CPU.<br />

pushed <strong>—</strong> The act of storing a value at the address contained in the<br />

stack pointer register and then decrementing the stack pointer so<br />

it points at the next available stack location<br />

RAM <strong>—</strong> Random access memory. Any RAM location can be read or<br />

written by the CPU. The contents of a RAM memory location<br />

remain valid until the CPU writes a different value or until power<br />

is turned off.<br />

read <strong>—</strong> Transfer the contents of a memory location to the CPU<br />

record <strong>—</strong> One line of an object code text file. See S-record.<br />

312 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Glossary<br />

registers <strong>—</strong> Memory locations that are wired directly into the CPU logic<br />

instead of being part of the addressable memory map. The CPU<br />

always has direct access to the information in these registers.<br />

The CPU registers in an <strong>M68HC05</strong> are:<br />

• A <strong>—</strong> 8-bit accumulator<br />

• X <strong>—</strong> 8-bit index register<br />

• CCR <strong>—</strong> condition code register containing the H, I, N, Z,<br />

and C bits<br />

• SP <strong>—</strong> stack pointer<br />

• PC <strong>—</strong> program counter<br />

Memory locations that hold status and control information for<br />

on-chip peripherals are called I/O and control registers.<br />

relative addressing mode <strong>—</strong> Relative addressing mode is used to<br />

calculate the destination address for branch instructions. If the<br />

branch condition is true, the signed 8-bit value after the opcode is<br />

added to the current value of the program counter to get the<br />

address where the CPU will fetch the next instruction.<br />

relative offset <strong>—</strong> An 8-bit, signed twos-complement value that is added<br />

to the program counter when a branch condition is true. The<br />

relative offset is located in the byte after a branch opcode.<br />

reset <strong>—</strong> Reset is used to force a computer system to a known starting<br />

point and to force on-chip peripherals to known starting<br />

conditions.<br />

reset vector <strong>—</strong> The contents of the last two memory locations in an<br />

<strong>M68HC05</strong> MCU are called the reset vector. As the MCU leaves<br />

reset, the program counter is loaded with the contents of these<br />

two locations so the first instruction after reset will be fetched from<br />

that address.<br />

ROM <strong>—</strong> Read-only memory. A type of memory that can be read but<br />

cannot be changed (written). The contents of ROM must be<br />

specified before manufacturing the MCU.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

313


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

S-record <strong>—</strong> A Motorola standard format used for object code files. See<br />

Figure 35. S-Record File for Example Program.<br />

simulator <strong>—</strong> A computer program that copies the behavior of a real<br />

MCU<br />

source code <strong>—</strong> See source program<br />

source program <strong>—</strong> A text file containing instruction mnemonics, labels,<br />

comments, and assembler directives. The source file is<br />

processed by an assembler to produce a composite listing and an<br />

object file representation of the program.<br />

SP <strong>—</strong> Abbreviation for stack pointer, a CPU register in the <strong>M68HC05</strong><br />

MCU<br />

stack <strong>—</strong> A mechanism for temporarily saving CPU register values<br />

during interrupts and subroutines. The CPU maintains this<br />

structure with the stack pointer register which contains the<br />

address of the next available storage location on the stack. When<br />

a subroutine is called, the CPU pushes (stores) the low-order and<br />

high-order bytes of the return address on the stack before starting<br />

the subroutine instructions. When the subroutine is done, a<br />

return-from-subroutine (RTS) instruction causes the CPU to<br />

recover the return address from the stack and continue<br />

processing where it left off before the subroutine. Interrupts work<br />

in the same way except all CPU registers are saved on the stack<br />

instead of just the program counter.<br />

stack pointer <strong>—</strong> A CPU register that holds the address of the next<br />

available storage location on the stack<br />

subroutine <strong>—</strong> A sequence of instructions that need to be used more<br />

than once in the course of a program. The last instruction in a<br />

subroutine is a return-from-subroutine (RTS) instruction. At each<br />

place in the main program where the subroutine instructions are<br />

needed, a jump- or branch-to-subroutine (JSR or BSR)<br />

instruction is used to call the subroutine. The CPU leaves the flow<br />

of the main program to execute the instructions in the subroutine.<br />

When the RTS instruction is executed, the CPU returns to the<br />

main program where it left off.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

314 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Glossary<br />

three-state buffer <strong>—</strong> The output of a three-state buffer can be a logic 0,<br />

a logic 1, or a high impedance (as if connected to nothing). An<br />

enable input controls the high impedance (off) state vs. the low<br />

impedance (on) state. When the buffer is on, the output has the<br />

same logic level as the input (1 or 0). When the buffer is off, the<br />

output acts like an open circuit.<br />

transducer <strong>—</strong> A device that converts some physical property, such as<br />

pressure, into electrical signals that can be used by a computer<br />

transmission gate <strong>—</strong> A basic logic circuit used in microcontrollers. A<br />

transmission gate works like a series switch that is controlled by<br />

a logic level signal. When the control input is a logic 0, the<br />

transmission gate acts like an open circuit. When the control input<br />

is a logic 1, the transmission gate acts like a short circuit.<br />

twos complement <strong>—</strong> A means of performing binary subtraction using<br />

addition techniques. The most significant bit of a twos<br />

complement number indicates the sign of the number (1 indicates<br />

negative). The twos complement negative of a number is<br />

obtained by inverting each bit in the number and then adding 1 to<br />

the result. For example, the twos complement negative of<br />

0000 0011 (310) is 1111 1100 + 0000 0001 = 1111 1101.<br />

variable <strong>—</strong> A value that changes during the course of executing a<br />

program<br />

V DD <strong>—</strong> The positive power supply to a microcontroller, typically 5 volts dc<br />

vector <strong>—</strong> A pointer (address) that indicates where the CPU should<br />

continue processing instructions after an interrupt or reset<br />

V SS <strong>—</strong> The 0 volt dc power supply return for a microcontroller<br />

volatile <strong>—</strong> A type of memory that forgets its contents when power is<br />

turned off. RAM is a type of volatile memory. In modern<br />

microcontrollers, it takes very little power to maintain the contents<br />

of a RAM under good conditions. In some cases, the contents of<br />

RAM and registers may appear to be unchanged after a short<br />

interruption of power.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

315


Freescale Semiconductor, Inc...<br />

Glossary<br />

Freescale Semiconductor, Inc.<br />

word <strong>—</strong> A group of binary bits. Some larger computers consider a set of<br />

16 bits to be a word, but this is not a universal standard.<br />

write <strong>—</strong> The transfer of a byte of data from the CPU to a memory<br />

location<br />

X <strong>—</strong> Abbreviation for index register, a CPU register in the <strong>M68HC05</strong><br />

MCU<br />

Z <strong>—</strong> Abbreviation for zero, a bit in the condition code register of the<br />

<strong>M68HC05</strong>. A compare instruction subtracts the contents of the<br />

tested value from a register. If the values were equal, the result of<br />

this subtraction would be zero, so the Z bit would be set. After a<br />

load accumulator instruction, the Z bit will be set if the loaded<br />

value was $00.<br />

zero <strong>—</strong> A logic low level (V SS )<br />

zero crossings <strong>—</strong> When an alternating current signal goes from a<br />

positive to a negative or from a negative to a positive value, it is<br />

called a zero crossing. The 60-Hz ac power line crosses zero<br />

every 8.33 milliseconds.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

316 Glossary<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Index<br />

A<br />

Accumulator (A). . . . . . . . . . . . . . . . . . . 68, 74, 75, 100, 102, 133, 299<br />

Address bus. . . . . . . . . . . . . . . . . . . 53, 54, 56, 63, 66, 68, 74, 75, 300<br />

Addressing mode. . . . . . . . . . . . . . . . . . . . 98, 104, 133, 146, 150, 299<br />

direct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110<br />

extended . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108<br />

immediate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 107<br />

indexed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112<br />

inherent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105<br />

relative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118<br />

Analog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 300<br />

Analog switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45<br />

Analog-to-digital converter (A-to-D). . . . . . . . . . . . . . . . . 183, 187, 215<br />

Architecture<br />

computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66<br />

Von Neumann . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66<br />

Arithmetic logic unit (ALU) . . . . . . . . . . . . . . . . . . . . . . . . . . 78, 98, 300<br />

ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 36, 67, 105, 288, 300<br />

code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30<br />

conversion to/from hexadecimal . . . . . . . . . . . . . . . . . 31, 288, 289<br />

Assembler . . . . . . . . . . . . . . . . . . 32, 36, 105, 143, 146, 147, 156, 301<br />

directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145, 149, 156<br />

listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143, 144, 145, 156, 308<br />

Assembly language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139, 301<br />

B<br />

Base 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27, 28, 301<br />

Base 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28, 301<br />

Base 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27, 28, 301<br />

Base 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

317


Freescale Semiconductor, Inc...<br />

Index<br />

Freescale Semiconductor, Inc.<br />

Binary . . . . . . . . . . . . . . . . . . . . . . . . 28, 30, 32, 36, 98, 105, 150, 301<br />

conversion to/from decimal, hexadecimal . . . . . . . . . . . . . . . . . . 29<br />

conversion to/from octal, hexadecimal . . . . . . . . . . . . . . . . . . . . .33<br />

Binary coded decimal<br />

conversion to/from decimal, binary . . . . . . . . . . . . . . . . . . . . . . . 35<br />

Binary coded decimal (BCD) . . . . . . . . . . . . . . . . 27, 34, 101, 102, 301<br />

Bit . . . . . . . . . . . . . . . . 22, 52, 54, 60, 62, 76, 150, 160, 161, 183, 301<br />

carry/borrow (C) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102<br />

half-carry (H) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101<br />

interrupt mask (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101<br />

negative (N) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101<br />

zero (Z). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102<br />

Black box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74, 302<br />

Branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76, 79, 84<br />

instructions . . . . . . . . . . . . . . . . . . . . . . . . . . 69, 119, 120, 123, 302<br />

Branch-to-subroutine (BSR) . . . . . . . . . . . . . . . . . . . . 76, 85, 141, 250<br />

Breakpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155, 302<br />

Buffer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44<br />

three-state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46, 49<br />

Bus<br />

address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66, 68, 74<br />

data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66, 75<br />

Byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 54, 67, 148, 302<br />

gigabyte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54<br />

kilobyte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54<br />

megabyte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54<br />

C<br />

C (bit in condition code register) . . . . . . . . . . . . . . . . 68, 102, 195, 302<br />

Central processor unit (CPU) . . . . . . . . . . . . . . . . .18, 20, 25, 302, 303<br />

Checksum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148, 302<br />

Clock . . . . . . . . . . . . . . . . . . .21, 25, 69, 74, 77, 94, 105, 181, 184, 303<br />

CMOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38, 39, 303<br />

N-type transistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39<br />

P-type transistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

318 Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Index<br />

Computer<br />

architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66<br />

kinds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25<br />

memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52<br />

parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25<br />

playing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80, 94<br />

program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20, 23, 303<br />

system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18, 303<br />

Computer program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20, 23, 303<br />

Computer system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18, 303<br />

Condition code register (CCR) . . . . . . . . . . . . . . . . 68, 69, 75, 86, 100,<br />

101, 133, 302, 303<br />

Conditional branch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76, 84, 142<br />

COP<br />

watchdog timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87, 95<br />

CPU. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80, 98<br />

control circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99<br />

instruction set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126<br />

instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73<br />

operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73<br />

register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67, 99<br />

view of a program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70<br />

CRT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20, 304<br />

Cycles . . . . . . . 21, 69, 74, 75, 77, 87, 92, 94, 105, 110, 142, 153, 303<br />

D<br />

Data bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56, 66, 75, 304<br />

Data multiplexer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45<br />

Decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28, 34, 105, 152, 304<br />

conversion to/from hexadecimal . . . . . . . . . . . . . 29, 290, 291, 292<br />

conversion to/from hexadecimal, binary. . . . . . . . . . . . . . . . . 29, 35<br />

Development tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154, 304<br />

Dexterity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153<br />

Digital. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 27, 304<br />

Digital-to-analog converter (D-to-A). . . . . . . . . . . . . . . . . 183, 187, 215<br />

Direct address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74, 76, 79, 304<br />

Direct addressing mode. . . . . . . . . . . . . 60, 64, 74, 104, 110, 111, 120,<br />

134, 146, 150, 304<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

319


Freescale Semiconductor, Inc...<br />

Index<br />

Freescale Semiconductor, Inc.<br />

Direct page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110, 304<br />

Duty cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187, 201<br />

E<br />

EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . .22, 56, 63, 183, 216, 304<br />

Effective address. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100, 104, 305<br />

Embedded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25, 305<br />

EPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 55, 63, 70, 155, 305<br />

Equate directive (EQU) . . . . . . . . . . . . . . . . . . . . . . . . . . 145, 149, 160<br />

Extended address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78<br />

Extended addressing mode . . . . . . . . . . .104, 108, 109, 133, 150, 305<br />

F<br />

FCB directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150<br />

FDB directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150, 162, 178<br />

Fetching a vector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305<br />

Fetching the reset vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86<br />

Flip flop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44<br />

Flowchart . . . . . . . . . . . . . . . . . . . . . .76, 136, 137, 138, 147, 156, 305<br />

G<br />

Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37, 40<br />

buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47<br />

inverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40<br />

NAND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40, 41<br />

NOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40, 42<br />

transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44, 49<br />

Gigabyte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54<br />

H<br />

H (bit in condition code register) . . . . . . . . . . . . . . . . 68, 101, 102, 306<br />

Half flip flop (HFF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48, 49, 57, 306<br />

Hexadecimal . . . . . . . . . . . . . . . . . . . . . . . . . . 28, 29, 36, 105, 148, 152, 306<br />

conversion to /from decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290<br />

conversion to/from ASCII . . . . . . . . . . . . . . . . . . . . . . .31, 288, 289<br />

conversion to/from decimal . . . . . . . . . . . . . . . . . . . . . . . . .291, 292<br />

conversion to/from decimal, binary . . . . . . . . . . . . . . . . . . . . . . . 29<br />

conversion to/from octal, binary . . . . . . . . . . . . . . . . . . . . . . . . . . 33<br />

values vs. <strong>M68HC05</strong> instructions. . . . . . . . . . . . . . . . . . . . . . . . 293<br />

High order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86, 306<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

320 Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Index<br />

I<br />

I (bit in condition code register) . . . . . . . . 68, 86, 88, 95, 101, 170, 306<br />

Immediate addressing mode . . . . . . .75, 104, 107, 133, 146, 161, 306<br />

In-circuit emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155<br />

In-circuit simulator . . . . . . . . . . . . . . . . . . . . . . . . . . 155, 157, 160, 306<br />

Index register (X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68, 100, 112, 133<br />

Indexed addressing mode . . . . . . . . . . . . . . . . 100, 104, 112, 134, 307<br />

16-bit offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116<br />

8-bit offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114<br />

no offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112<br />

Indexed instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115, 117<br />

Inherent addressing mode. . . . . . . . . . . . . . . . . . . . 104, 105, 133, 306<br />

Inherent instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106<br />

Input, see input-output<br />

Input/output (I/O) . . . . . . . . . . 18, 25, 54, 57, 59, 63, 67, 180, 306, 307<br />

Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . 69, 70, 73, 92, 93, 98, 103<br />

branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76<br />

branch-to-subroutine (BSR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76<br />

decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99<br />

jump-to-subroutine (JSR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76<br />

return-from-subroutine (RTS). . . . . . . . . . . . . . . . . . . . . . . . . . . . 76<br />

Instruction decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307<br />

Instruction set . . . . . . . . . . . . . . . . 30, 36, 98, 104, 139, 156, 221, 307<br />

dexterity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153<br />

mnemonics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32, 293<br />

opcode map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132<br />

summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125, 126<br />

Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 21, 23, 30, 36, 219, 307<br />

assembly language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139<br />

bit status. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220<br />

bit test and branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120<br />

branch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123<br />

CCR activity summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220<br />

condition code register (CCR) bits . . . . . . . . . . . . . . . . . . . . . . . 220<br />

CPU registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220<br />

machine coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221<br />

memory and addressing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220<br />

operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

321


Freescale Semiconductor, Inc...<br />

Index<br />

Freescale Semiconductor, Inc.<br />

read-modify-write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122<br />

register/memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121<br />

source form notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221<br />

Interrupt . . . . . . . . . . . . . . . . . . . . . . . . 88, 95, 101, 103, 163, 170, 180<br />

external . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91<br />

hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90<br />

latency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92, 95<br />

nested . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93, 95<br />

on-chip peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92<br />

software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92, 95<br />

sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88<br />

stacking order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91<br />

vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61, 149, 162, 178<br />

Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88<br />

Inverter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40, 49, 308<br />

J<br />

Jump-to-subroutine (JSR) . . . . . . . . . . . . . . . 76, 82, 85, 141, 165, 261<br />

K<br />

Kilobyte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54, 63, 308<br />

L<br />

Label . . . . . . . . . . . . . . . . . . . . . . . . 145, 146, 149, 156, 162, 165, 308<br />

Latch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308<br />

Least significant bit (LSB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195, 308<br />

Listing . . . . . . . . . . . . . . . 70, 73, 76, 80, 143, 144, 145, 150, 156, 308<br />

Logic<br />

elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37<br />

levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38<br />

Logic 0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308<br />

Logic 0, see Zero<br />

Logic 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308<br />

Logic 1, see One<br />

Logic level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19, 49<br />

Low order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86, 308<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

322 Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Index<br />

M<br />

Machine code . . . . . . . . . . . 32, 75, 136, 143, 145, 147, 148, 150, 308<br />

Mainframe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18, 20, 23, 25, 308<br />

Mass storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 309<br />

MCU, see microcontroller<br />

Megabyte. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54<br />

Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21, 25, 52, 54<br />

analogy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52<br />

EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 56, 63<br />

EPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 55, 63<br />

how a computer sees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53<br />

I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57, 58, 59, 63<br />

map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60, 61, 64<br />

non-volatile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54, 64<br />

OTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56, 63<br />

peripheral. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62<br />

PROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55<br />

RAM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21, 55, 63<br />

ROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21, 55, 63<br />

volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54, 64<br />

Memory array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66<br />

Memory location . . . . . . . . . . . . . . . . . . . 60, 68, 69, 70, 100, 183, 309<br />

Memory map . . . . . . . . . . . . . . . . . . . . 60, 61, 64, 70, 72, 94, 184, 309<br />

Memory-mapped I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94, 183, 309<br />

Microcontroller (MCU). . . . . . . . . . . . . . . . 17, 19, 23, 24, 66, 299, 309<br />

Microprocessor (MPU). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23, 309<br />

Mnemonic . . . . . . . . . . 32, 36, 133, 136, 139, 143, 146, 160, 293, 309<br />

Mode<br />

addressing . . . . . . . . . . . . . . . . . . . . . . .98, 104, 133, 146, 150, 299<br />

direct addressing . . . . . . . . . . . 60, 64, 74, 104, 110, 134, 146, 304<br />

extended addressing . . . . . . . . . . . . . . . . . 104, 108, 133, 150, 305<br />

immediate addressing . . . . . . . . . 75, 104, 107, 133, 146, 161, 306<br />

indexed addressing . . . . . . . . . . . . . . . . . . .100, 104, 112, 134, 307<br />

inherent addressing . . . . . . . . . . . . . . . . . . . . . . . . . . 104, 105, 133<br />

relative addressing. . . . . . . . . . . . . . . . . . . . 76, 104, 118, 134, 313<br />

Monitor program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155, 310<br />

Most significant bit (MSB) . . . . . . . . . . . . . . . . . . . . . 75, 101, 195, 310<br />

Multiplexer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45, 46<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong>Rev. 2.0<br />

MOTOROLA Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

323


Freescale Semiconductor, Inc...<br />

Index<br />

Freescale Semiconductor, Inc.<br />

N<br />

N (bit in condition code register) . . . . . . . . . . . . . . 68, 75, 84, 101, 310<br />

NAND gate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41, 49, 310<br />

Negative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310<br />

Non-volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54, 64, 87, 310<br />

NOR gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42, 49, 310<br />

O<br />

Object code file . . . . . . . . . . . . . . . . . . . . . . . . 143, 147, 150, 156, 310<br />

S-record. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147<br />

Octal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32, 105, 310<br />

conversion to/from hexadecimal, binary . . . . . . . . . . . . . . . . . . . .33<br />

Offset (indexed). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112, 114, 116<br />

One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 27, 38, 40, 49, 310<br />

Ones complement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148, 311<br />

Opcode . . . . . 30, 32, 36, 70, 74, 78, 94, 104, 125, 133, 134, 146, 311<br />

Opcode map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132<br />

Operand. . . . . . . . . . . . . . . 68, 70, 74, 94, 99, 100, 103, 134, 146, 311<br />

ORG directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149, 165<br />

Oscillator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21, 23, 69, 87, 311<br />

OTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55, 56, 63, 154<br />

OTPROM, see OTP<br />

Output, see input-output<br />

P<br />

Paced loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160, 161, 165<br />

loop trigger. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167<br />

program example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171, 172<br />

software use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169, 178<br />

switch debouncing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169<br />

system clock. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168<br />

timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169<br />

Parity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288, 311<br />

PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311<br />

Peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64, 180, 181, 215<br />

analog-to-digital converter (ADC). . . . . . . . . . . . . . . . . . . . 183, 215<br />

control of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183<br />

digital-to-analog converter (DAC). . . . . . . . . . . . . . . . . . . . 183, 215<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong>Rev. 2.0<br />

324 Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Index<br />

EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183, 216<br />

I/O port, general-purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180<br />

memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62<br />

on-chip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180<br />

serial port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182, 215<br />

timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181, 215<br />

Personal computer (PC) . . . . . . . . . . . . . 18, 23, 25, 85, 143, 154, 311<br />

Playing computer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73, 80, 94, 311<br />

Pointer register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68, 311<br />

Port<br />

general-purpose I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . 59, 187, 215<br />

serial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182, 215<br />

serial communications interface (SCI) . . . . . . . . . . . . . . . . . . . . 182<br />

serial peripheral interface (SPI) . . . . . . . . . . . . . . . . . . . . . . . . . 182<br />

Program. . . . . . . . . . . . . . . . . . . 20, 23, 70, 72, 80, 141, 147, 156, 312<br />

Program counter (PC) . . . . . . . . . . . . 68, 74, 76, 83, 88, 103, 133, 312<br />

Program runaway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87<br />

Programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99, 133, 312<br />

PROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55, 312<br />

Pulled. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69, 103, 312<br />

Pulse width modulated (PWM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187<br />

Pushed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69, 78, 103, 312<br />

R<br />

RAM . . . . . . . . . . . . . .21, 55, 57, 63, 85, 142, 151, 156, 170, 178, 312<br />

variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165<br />

Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20, 57, 312<br />

Record. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312<br />

Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66, 80, 88, 313<br />

accumulator (A) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68, 100, 133<br />

condition code (CCR). . . . . . . . . . . . . . . . . . . 69, 75, 100, 133, 303<br />

CPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67, 68, 99, 133, 303<br />

I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60<br />

index (X). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68, 100, 133, 307<br />

internal status and control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59<br />

pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68, 311<br />

program counter (PC) . . . . . . . . . . . . . . . . . . . . . . . . . 68, 103, 133<br />

stack pointer (SP). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94, 103, 133<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

MOTOROLA Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

325


Freescale Semiconductor, Inc...<br />

Index<br />

Freescale Semiconductor, Inc.<br />

Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154, 160<br />

Relative addressing mode . . . . . . . . . . . . . . . . . 76, 104, 118, 134, 313<br />

Relative instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119<br />

Relative offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76, 78, 146, 313<br />

Reset . . . . . . . . . . . . . . . . . . . . . . . . . .59, 68, 86, 94, 95, 103, 186, 313<br />

conditions that cause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86<br />

illegal address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87<br />

internal actions resulting from . . . . . . . . . . . . . . . . . . . . . . . . . . . 86<br />

power-on reset (POR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86<br />

RESET pin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86<br />

vector . . . . . . . . . . . . . . . . . . . . 68, 86, 94, 149, 162, 163, 178, 313<br />

watchdog timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87<br />

Return address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78, 83<br />

Return-from-interrupt (RTI) . . . . . . . . . . . . . . . . . . . . . . . . 88, 101, 163<br />

Return-from-subroutine (RTS). . . . . . . . . . . . . . 76, 77, 79, 84, 85, 142<br />

RMB directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151, 165, 178<br />

ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21, 55, 63, 70, 313<br />

S<br />

Serial communications interface (SCI) . . . . . . . . . . . . . . . . . . . . . . 182<br />

Serial peripheral interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182<br />

Serial port<br />

SCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182<br />

SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182<br />

Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85, 154, 157, 164, 314<br />

Software<br />

PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195<br />

Software delay program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141<br />

Software interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282<br />

Software interrupt (SWI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61, 88, 95<br />

Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156, 314<br />

mnemonic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139, 156<br />

Source program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143, 145, 314<br />

S-record. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147, 148, 150, 156, 314<br />

Stack . . . . . . . . . . . . . . . 69, 78, 79, 83, 88, 91, 94, 142, 170, 178, 314<br />

Stack pointer (SP) . . . . . . . . . . . . . . . . . . 69, 76, 78, 94, 103, 133, 314<br />

Subroutine . . . . . . . . . . . . 76, 77, 80, 94, 103, 139, 141, 142, 170, 314<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

326 Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Index<br />

System equates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160<br />

application-specific. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161<br />

equate directives (EQU). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178<br />

register equates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160<br />

T<br />

Three-state buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46, 49, 315<br />

Timer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181, 215<br />

example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187<br />

Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69<br />

Transducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 315<br />

Transistor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39, 49<br />

N-type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39<br />

P-type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39<br />

Transmission gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44, 49, 315<br />

Twos complement. . . . . . . . . . . . . . . . . . . . . 75, 76, 79, 101, 310, 315<br />

V<br />

Variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165, 315<br />

V DD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 86, 315<br />

Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61, 86, 162, 315<br />

reset. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162<br />

Volatile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54, 64, 315<br />

V SS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 315<br />

W<br />

Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 54, 316<br />

Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20, 57, 75, 316<br />

Z<br />

Z (bit in condition code register) . . . . . . . . . . . . . . 68, 75, 84, 102, 316<br />

Zero. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 27, 38, 40, 49, 316<br />

Zero crossing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168, 316<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong>Rev. 2.0<br />

MOTOROLA Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

327


Freescale Semiconductor, Inc...<br />

Index<br />

Freescale Semiconductor, Inc.<br />

<strong>M68HC05</strong> <strong>Family</strong> <strong>—</strong> <strong>Understanding</strong> <strong>Small</strong> <strong>Microcontrollers</strong> <strong>—</strong> Rev. 2.0<br />

328 Index<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

MOTOROLA


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>—</strong> NOTES <strong>—</strong><br />

For More Information On This Product,<br />

Go to: www.freescale.com


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

<strong>—</strong> NOTES <strong>—</strong><br />

For More Information On This Product,<br />

Go to: www.freescale.com


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

For More Information On This Product,<br />

Go to: www.freescale.com


Freescale Semiconductor, Inc...<br />

Freescale Semiconductor, Inc.<br />

Motorola reserves the right to make changes without further notice to any products herein. Motorola makes no warranty, representation or guarantee regarding the suitability of its<br />

products for any particular purpose, nor does Motorola assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability,<br />

including without limitation consequential or incidental damages. "Typical" parameters which may be provided in Motorola data sheets and/or specifications can and do vary in different<br />

applications and actual performance may vary over time. All operating parameters, including "Typicals" must be validated for each customer application by customer's technical experts.<br />

Motorola does not convey any license under its patent rights nor the rights of others. Motorola products are not designed, intended, or authorized for use as components in systems<br />

intended for surgical implant into the body, or other applications intended to support or sustain life, or for any other application in which the failure of the Motorola product could create a<br />

situation where personal injury or death may occur. Should Buyer purchase or use Motorola products for any such unintended or unauthorized application, Buyer shall indemnify and hold<br />

Motorola and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of,<br />

directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges that Motorola was negligent regarding the<br />

design or manufacture of the part. Motorola and are registered trademarks of Motorola, Inc. Motorola, Inc. is an Equal Opportunity/Affirmative Action Employer.<br />

How to reach us:<br />

USA/EUROPE/Locations Not Listed: Motorola Literature Distribution, P.O. Box 5405, Denver, Colorado 80217, 1-800-441-2447 or<br />

1-303-675-2140. Customer Focus Center, 1-800-521-6274<br />

JAPAN: Nippon Motorola Ltd.: SPD, Strategic Planning Office, 141, 4-32-1 Nishi-Gotanda, Shinagawa-ku, Tokyo, Japan. 03-5487-8488<br />

ASIA/PACIFIC: Motorola Semiconductors H.K. Ltd., 8B Tai Ping Industrial Park, 51 Ting Kok Road, Tai Po, N.T., Hong Kong. 852-26629298<br />

Mfax, Motorola Fax Back System: RMFAX0@email.sps.mot.com; http://sps.motorola.com/mfax/;<br />

TOUCHTONE, 1-602-244-6609; US and Canada ONLY, 1-800-774-1848<br />

HOME PAGE: http://motorola.com/sps/<br />

For More Information On This Product,<br />

Go to: www.freescale.com<br />

Mfax is a trademark of Motorola, Inc.<br />

© Motorola, Inc., 1998<br />

<strong>M68HC05</strong>TB/D

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!