Chapter 1: Introduction To HCS12/MC9S12 The HCS12 Microcontroller Han-Way Huang Minnesota State University, Mankato September 2009
Chapter 1: Introduction To HCS12/MC9S12 The HCS12 Microcontroller Han-Way Huang Minnesota State University, Mankato September 2009
Han-Way Huang
September 2009
Modulus Math
• Computers perform modulus math because it uses a limited number of bits
(e.g., 8, 16, or 32 bits) to represent numbers.
What is a computer?
Software
Hardware
Control Unit
Datapath
Arithmetic
Logic Unit Memory
Output Input
Program Data
Units Units
Registers Storage Storage
The Processor
• A processor consists of arithmetic logic unit (ALU), control unit, and
registers.
• The ALU can perform many different operations.
• An ALU that can perform 4 operations is shown in Figure 1.1.
A B
n n
OpCode
CIN 1 0pcode
inverters
2 00 = ADD
0 1 Compa ra tor 01 = SUB
MUX2 (=?) 01 10 = AND
11 = OR
0 1
MUX1 ci n SUM
Adder
00
Y
Ca rry
X Cout
01
MUX3
n
n Result
AND 10
OR
n 11
Figure 1.1An ALU that implements ADD, SUB, AND, and OR opera tions
Control Unit
• The control unit decodes machine codes and performs the operations specified
by the machine code.
• A machine instruction has several fields. A mandated field is the opcode field.
• Instruction execution is timed by a clock signal.
• The frequency of the clock signal used in an 8-bit or 16-bit microcontroller is from
a few mega hertz to several hundred mega Hertz
• The control unit uses a program counter to keep track of the address of the
instruction to be executed next.
• The fetched instruction is placed in the instruction register when is decoded,
and executed.
• Program instructions are normally stored in sequential locations in memory.
• The processor may not execute instructions in sequential due to the need to
execute instructions based on the condition or the need to repeat a certain group
of instructions.
• The processor uses conditional and unconditional branch (or jump)
instructions to change the program flow.
Registers
• A register is a storage location inside the CPU.
• A register is used to hold data or address during the execution of an instruction.
• A register, very close to the ALU, provides fast access to operands for program execution.
• Some processors provide a dedicated register (often called an accumulator) as one of the
operands of most of the instructions.
• Freescale HCS12, Microchip PIC18, Intel 8051 microcontrollers use this approach.
• Other processors provide many general-purpose registers in the CPU that can be used
as operands of instructions.
• Atmel AVR and AVR32, Microchip PIC24 and PIC32, Freescale Coldfire use this approach.
Microprocessor
• A processor implemented in a single integrated circuit (IC).
• The first microprocessor, Intel 4004, was introduced in 1968.
• The Intel 8008, the first 8-bit microprocessor from Intel, was introduced in 1972.
• A microprocessor requires peripheral ICs to interface with I/O devices.
• A microprocessor does not have peripheral functions such as timers, A/D converters,
D/A converters, parallel I/O ports, and memory.
• The designer need to add peripheral ICs and memory devices to the microprocessor
in order to build a product.
Microcontroller
• A microcontroller incorporates the processor and one or more of the following
peripheral functions and memory in one very large scale integrated circuit (VLSI):
1. Memory
2. Timer functions
3. Serial communication interface such as UART, I2C, SPI, CAN, and Ethernet
4. A/D converter
5. D/A converter
6. Direct memory access
7. Parallel I/O ports
8. Memory component interface
9. Software debug support
• Microcontrollers have been used in almost every product that requires a certain
amount of intelligence.
• Microcontrollers have been used as controllers for displays, printers, keyboards,
modems, charge card phones, palm-top computers, home appliances such as
washing machines and microwave ovens, automobile control, and so on.
Embedded Systems
• An embedded system is a computer designed to perform a dedicated function.
• An embedded system performs one or a few predefined tasks, with very specific
requirements.
• Embedded systems are often mass-produced and benefitting from economic scale.
• Software written for embedded systems is often called firmware.
Memory Technologies
Magnetic Memory
• Magnetic drums, magnetic tapes, and magnetic disks are the three major magnetic
memory devices that have been invented.
• Only magnetic disks are still being widely used today
• Hard disks are rarely used in an embedded system
Optical Memory
• Compact disk (CD) and digital video disc (DVD) are two major optical memory devices.
• CD was introduced in 1982.
• CD has two versions: CD-R and CD-RW.
• The capacity of a 12-inch CD is 700 MB.
• A 12-inch DVD has the capacity of 4.7 GB
• DVD has two versions: DVD-R and DVD-RW versions.
Semiconductor Memory
• Semiconductor memory can be classified on the basis of volatility and read-writability.
• On the basis of volatility, semiconductor memory is divided into volatile and
non-volatile memory.
• On the basis of read-writability, semiconductor memory is divided into random-access
memory (RAM) and read-only memory (ROM).
Random-access memory
There are four types of RAM:
Read-Only Memory
• ROM is nonvolatile.
• There are several types of ROM technologies.
Flash memory
• Flash memory was invented by Fujio Masuoka in 1984.
• Flash memory is electrically erasable and programmable many times.
• Flash memory can only be erased in bulk or a sector at a time
• Flash can achieved the density of DRAM and is the most widely used nonvolatile memory
technology.
Computer software
Machine instruction
Assembly language
High-level language
• Syntax of a high-level language is similar to English
• A translator (called compiler) is required to translate the program written in a high-level
language:
1. There are two types of compilers: native compiler and cross compiler.
2. High-level languages allow the user to work on the program logic at higher level
and achieve higher productivity.
Source code
• A program written in assembly or high-level language
Object code
• The output of an assembler or compiler
RD OE
W
WE
R
data
Read Operation
• The processor places the address of the memory location that it intends to read on
the address bus and asserts the RD signal.
• The RD signal enables the memory chip to send out the contents of the selected location
on the data bus and the processor gets the data.
Write Operation
• The processor places the address of the location that it intends to write on the address
bus.
• The processor also places the data that it intends to write on the data bus and asserts
the WE signal.
• In responds to the WE signal, the memory chip stores the data on the data bus in the
selected memory location.
Program Execution
To understand how processor executes the program, we need to answer the following
questions:
Where does the processor start to execute program after power on?
• From a fixed location such as address 0: PIC18, Atmel avr, and 8051
• Fetch the starting address from a fixed location: HCS12
D Q
CLK
set reset
1 Bra nch
offset
0 1
Bra nch
MUX1 16
a dder Qs
16 CLK Clk
PC
MUX2
16
1 0
Jump 16 Ds Power
reset
ta rget On
Jump
Figure 1.4 A simplified block dia gra m of the progra m counter (PC)
of a n 8-bit microcontroller
Da ta meory
Progra m memory
Address bus 75 Da ta memory
0 x0 0 0 0 20 Address bus xx
00 0 x20 xx
75
xx
21
xx
14
xx
90
xx
CPU 20
xx
00
E0 CPU xx
54 xx
03 xx
Before rea d 70 Before write xx
0 x75 02 xx
0 x0 00 0 Progra m memory 05 0 x0 0 03
xx
PC da ta bus 20 PC 0x75
0 x0 0 xx
After rea d D5 Da ta memory xx
After write
21 da ta bus xx
0x0 0 0 1 0 x0 0 03
0A xx
PC PC
Figure 1.5 Instruction 1—opcode rea d cycle Figure 1.6 Instruction 1—da ta memory write cycle
Progra m memory
Address bus 75
0 x0 0 0 6 20
00
75
21
14
90
CPU 20
00
E0
54
03
Before rea d 70
0 x90 02
0 x0 0 06
Progra m memory 05
PC da ta bus 20
After rea d D5
21
0 x00 07
0A
PC
Step 2
The opcode (0xE0) returned in Step 1 requires the processor to perform a read operation
Using the address stored in ptr register. Processor X places the contents of ptr register
on the data memory address bus and request a read operation.
Step 3
Data memory returns the byte in data memory pointed to by ptr register. This byte is
placed in A.
Da ta meory
Da ta memory
Address bus xx
[ptr] xx
xx
xx
xx
xx
xx
CPU xx
xx
xx
xx
[ptr]: contents of ptr xx
xx
Mem[[ptr]]: contents of memory
Mem[[ptr]] xx
loca tion pointed to by ptr
Da ta memory xx
da ta bus xx
xx
15 X 0 Index register X
15 Y 0 Index register Y
15 SP 0 Sta ck pointer
15 PC 0 Progra m counter
Inherent Mode
- Instructions that use this mode do not uses extra bytes to specify operands because
the instructions either do not need operands or all operands are CPU registers.
- Operands are implied by the opcode.
- Examples
NOP
INX
DECA
Immediate Mode
- Operands for instructions that use immediate mode are included in the instruction.
- CPU does not access memory for operands.
- For example,
LDAA #$55
LDX #$1000
Direct Mode
- This mode can only specify memory locations in the range of 0 - 255.
- This mode uses only one byte to specify the operand address.
- Examples,
LDAA $20
LDAB $40
Extended Mode
- This mode uses a 16-bit value to specify memory address is in the instruction.
- For example,
LDAA $4000 ; A ← [$4000]
LDX $FE60 ; X ← [$FE60]:[$FE61]
Relative Mode
- Used only by branch instructions.
- Short and long conditional branch instructions use exclusively relative mode.
- BRCLR and BRSET instructions can also use relative mode to specify branch target.
- A short branch instructions consists of an 8-bit opcode and a signed 8-bit offset. The
short relative mode can specify a range of -128 ~ +127.
- A long branch instruction consists of an 8-bit opcode and a signed 16-bit offset. The
range of the long relative mode is from -32768 ~ +32767.
- A programmer uses a symbol to specify the branch target and the assembler will figure
out the actual branch offset (distance) from the instruction that follows branch
instruction.
Copyright © 2010 Delmar Cengage Learning H. Huang Transparency No.1-38
The HCS12/MC9S12 Microcontroller
- For example,
minus …
…
bmi minus
…
Indexed Mode
- This mode uses the sum of an index register (X, Y, PC, or SP) and an offset to specify the
address of an operand.
- The offset can be a 5-bit, 9-bit, and 16-bit signed value or the value in accumulator A,
B, or D.
- Automatic pre- or post-increment or pre- or post-decrement by -8 to +8 are options.
- PC can be used as the index register for all but auto-increment or auto-decrement mode.
- Indirect indexing with 16-bit offset or accumulator D as the offset is supported.
- The EXG instruction exchanges the contents of a pair of registers or accumulators. For
example,
exg A, B
exg D,X
exg A,X ; A X[7:0], X $00:[A]
exg X,B ; X $00:[B], B X[7:0]
- The SEX instruction sign-extend an 8-bit two’s complement number into a 16-bit
number so that it can be used in 16-bit signed operations. For example,
SEX A,X
Move Instructions
- These instructions move data bytes or words from a source to a destination in memory.
- Six combinations of immediate, extended, and index addressing modes are allowed to
specify the source and destination addresses:
IMM EXT, IMM IDX, EXT EXT,
EXT IDX, IDX EXT, IDX IDX
- For example,
movb $100,$800
movw 0,X, 0,Y