Chapter 1: Introduction To HCS12/MC9S12 The HCS12 Microcontroller Han-Way Huang Minnesota State University, Mankato September 2009

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

The HCS12/MC9S12 Microcontroller

Chapter 1: Introduction to HCS12/MC9S12


The HCS12 Microcontroller
Han-Way Huang
Minnesota State University, Mankato
September 2009

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-1

The HCS12/MC9S12 Microcontroller

Number System Issue

Computer hardware uses binary numbers to perform all operations.


To display numbers produced by the computer, both the octal and hexadecimal
numbers have been used to shorten the representation.
Human beings are used to decimal number system. Conversion is often needed
to convert numbers between the internal (binary) and external (decimal)
representations.
It is common for mixed use of different number bases.
A prefix is used to indicate the base of a number.

Modulus Math
Computers perform modulus math because it uses a limited number of bits
(e.g., 8, 16, or 32 bits) to represent numbers.
Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-2

The HCS12/MC9S12 Microcontroller

What is a computer?
Software
Hardware
Computer Hardware Organization

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-3

The HCS12/MC9S12 Microcontroller

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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-4

The HCS12/MC9S12 Microcontroller

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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-5

The HCS12/MC9S12 Microcontroller

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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-6

The HCS12/MC9S12 Microcontroller

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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-7

The HCS12/MC9S12 Microcontroller

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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-8

The HCS12/MC9S12 Microcontroller

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).
Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-9

The HCS12/MC9S12 Microcontroller

Random-access memory
There are four types of RAM:
Dynamic random-access memory (DRAM):
The one-bit cell of DRAM consists of one transistor and one capacitor.
The information is stored in the capacitor whereas the transistor controls the access of
the information stored in the capacitor.
Periodic refresh is required to maintain the contents of a DRAM chip
Static random-access memory (SRAM):
The one-bit of SRAM consists of 4 to 6 transistors.
No periodic refresh operation is required to maintain the information stored in SRAM.
Magneto RAM (MRAM):
The one-bit cell of MRAM consists of two magnetic plates each of which can hold a
magnetic field, separated by a thin insulating layer. One of the plates is a permanent
magnet set to a particular polarity.
A MRAM chip combines a magnetic device with standard silicon-based microelectronics
to achieve the attributes of non-volatility, high-speed operation, and unlimited read and
write endurance

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-10

The HCS12/MC9S12 Microcontroller

Ferroelectric RAM (FRAM)


FRAM uses the state change of ferroelectric crystal to store information.
FRAM technology is mainly developed by Ramtron International.
FRAM is nonvolatile and has an access time about 55 ns in 2007.
Due to its lower density, FRAM is mainly used in niche applications.

Read-Only Memory

ROM is nonvolatile.
There are several types of ROM technologies.

Mask-Programmed ROM (MROM)


The contents of MROM is programmed when it is manufactured.
To be cost-effective, many thousands of copies of MROM chip must be manufactured.
MROM is one of the major ROM technologies to hold application programs
Programmable ROM (PROM)
PROM was invented in 1956 by Win-Tsing Chow.
The setting of each bit is locked by a fuse or antifuse.
PROM is programmed by blowing a fuse or an antifuse.
PROM can only be programmed once using a special programmer.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-11

The HCS12/MC9S12 Microcontroller

Erasable programmable ROM (EPROM)


EPROM was invented by Dov Frohman in 1971.
An EPROM cell must be erased before it is programmed.
EPROM is electrically programmable many times.
EPROM is erased by ultraviolet light (through a window).
EPROM can only be erased in bulk (whole chip in one erasure operation).
Electrically erasable programmable ROM (EEPROM)
EEPROM was invented by George Perlegos of Intel in 1983.
An EEPROM cell must be erased before it is programmed.
EEPROM is electrically erasable and programmable many times.
EEPROM can be erased one location, one row, or whole chip in one operation
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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-12

The HCS12/MC9S12 Microcontroller

Computer software

Computer programs are known as software


A program is a sequence of instructions

Machine instruction

A sequence of binary digits which can be executed by the processor


0001 1000 0000 0110: A [A] + [B]
0100 0011:
A [A] + 1
1000 0110 0000 0110: A 6

Hard to understand, enter, debug, and maintain for human being

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-13

The HCS12/MC9S12 Microcontroller

Assembly language

Defined by assembly instructions


An assembly instruction is a mnemonic representation of a machine instruction
ABA: A [A] + [B]
DECA: A [A] 1

Assembly programs must be translated into machine instructions before it can be


executed -- translated by an assembler
There are two kinds of assembler: native assembler and cross assembler.
Programmers need to work on the program logic at a very low level and cannot
achieve high productivity.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-14

The HCS12/MC9S12 Microcontroller

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

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-15

The HCS12/MC9S12 Microcontroller

line

addr.

1:

machine code

source code

= 00002000

org $2000

2:

2000

B6 1000

ldaa $1000

3:

2003

BB 1001

adda $1001

4:

2006

BB 1002

adda $1002

5:

2009

7A 1100

staa $1100

6:

Copyright 2010 Delmar Cengage Learning

end

H. Huang Transparency No.1-16

The HCS12/MC9S12 Microcontroller

Memory System Operation

A block diagram of a memory system is shown in Figure 1.2.


Each memory location has two components: address and contents.
The organization of a memory chip is indicated by m x n, where m is
the number of locations in the chip and n indicates the number of bits in one location.
The notation [reg] and [addr] specifies the contents of a register and the memory location
at addr, respectively.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-17

The HCS12/MC9S12 Microcontroller

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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-18

The HCS12/MC9S12 Microcontroller

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
How does the processor update the program counter (PC)?
Increment the program counter by the length of the instruction just executed.
Increment or decrement by the amount specified in the current instructionconditional
and unconditional branch instructions.
Set to the new address specified by the current instructionjump (or goto) instruction or
subroutine call instruction.

Program Counter Circuit

A common building block of PC is the D flip-flop with set and reset capability (Figure 1.3).
The program counter circuit that can be forced to 0, incremented by 1, incremented by
a field in the instruction, and loaded with a jump target is shown in Figure 1.4.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-19

The HCS12/MC9S12 Microcontroller

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-20

The HCS12/MC9S12 Microcontroller

Architecture of the Processor X

Processor X has a separate data memory and program memory spaces.


Processor X is used to explain the instruction execution process.
The instruction set of processor X is given in Table 1.3.
Processor X has an 8-bit accumulator A and a 16-bit register ptr.
Processor X instructions can use an 8-bit value to specify a data memory location from
0 to 255 to be accessed.
For data memory locations with address higher than 255, the processor uses the 16-bit
register ptr to specify the address.
Processor X uses a MDR register to hold data to be written and read from data memory.
The instruction set implemented by processor X is given in Table 1.3.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-21

The HCS12/MC9S12 Microcontroller

Instruction sequence example


ld 0x20,#0
; place 0 in data memory at 0x20
ld 0x21,#20 ; place 20 in data memory at 0x21
ld ptr,#0x2000 ; load 0x2000 into the ptr register
loop: ld A,@ptr
; load the memory contents pointed to by ptr
and A,#0x03
; and the value 0x03 with accumulator A
bnz next
; branch if the result is not 0
inc 0x20
; increment the memory location at 0x20 by 1
next: dbnz 0x21,loop ; decrement memory location at 0x21 and branch
; if the contents of 0x21 is not zero yet
Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-22

The HCS12/MC9S12 Microcontroller

Instruction Execution Process


Assume that processor X executes the instruction sequence in the previous out of reset.
Instruction ld 0x20,#0 (machine code 75 20 00)
Step 1
Processor X places 0 (contents of PC) on the address bus to perform a read from program
program memory.
Step 2
The 8-bit value at the location 0x0000 is the instruction opcode 0x75. At the end of this
read cycle, the PC is incremented to 0x0001. The opcode byte 0x75 is fetched.
Figure 1.5 shows the opcode read cycle.
Step 3
Recognizing that 0x75 is the opcode of a load instruction, processor performs two more read
operations. The first read returns the address 0x20 whereas the second read returns the
value 0.
Step 4
Processor places 0x20 on the data memory address bus and places 0 on the data bus and
then performs a write operation to write the value 0 to location 0x20.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-23

The HCS12/MC9S12 Microcontroller

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-24

The HCS12/MC9S12 Microcontroller

Instruction ld 0x21,#20 (machine code 75 21 14)


The execution of this instruction is identical to that of the previous instruction.
PC is incremented to 0x0006 after the execution of this instruction.
Instruction ld ptr,#0x2000 (machine code 90 20 00)
Step 1
Processor X places the PC value (0x06) on the program memory address bus and makes
request to read the program memory. Figure 1.7 shows the opcode read cycle.
Step 2
The opcode returned from location at 0x06 is 0x90 and PC is incremented to 0x07.
Step 3
The opcode 0x90 indicates that processor X should access program memory to fetch the
16-bit value stored after the opcode byte. Processor performs two more read cycles from
program memory and places these two bytes in the ptr register. PC is incremented to 0x09.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-25

The HCS12/MC9S12 Microcontroller

Instruction ld A,@ptr (machine code E0)


Step 1
Processor X places the PC value (0x09) on the program memory address bus and requests
to read the program memory. At the end of this step, PC is incremented to 0x0A.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-26

The HCS12/MC9S12 Microcontroller

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.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-27

The HCS12/MC9S12 Microcontroller

Instruction and A,#0x03 (machine code 54 03)


Step 1
Processor X places the PC value on the program memory address bus to request a read.
After this step is completed, PC is incremented to 0x0B.
Step 2
The opcode byte (0x54) returned in Step 1 requires the processor X to perform another
read operation from program memory.
Step 3
Processor X places the PC value on program memory address bus and request to perform
a read operation. PC is incremented to 0x0C after this step.
Step 4
Program memory returns the value 0x03 to the processor.
Step 5
The processor X then performs an AND operation on the contents of A and the value 0x03
and places the result in A

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-28

The HCS12/MC9S12 Microcontroller

Instruction bnz next (machine code is 70 02)


Step 1
Processor X places the PC value (0x0C) on the program memory address bus and performs
a read operation. After this step, PC is incremented to 0x0D.
Step 2
The opcode (0x70) returned in Step 1 requires processor X to performs another read to
fetch the branch offset.
Step 3
Processor X places the value PC (0x0D) on the program memory address bus and performs
a read. After this step the program memory returns the value 0x02 to the processor. PC
is incremented to 0x0E.
Step 4
Processor checks whether the A value is zero and decides whether branch should be
taken. If A contains a nonzero value, the next instruction will be skipped.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-29

The HCS12/MC9S12 Microcontroller

Instruction inc 0x20 (machine code 05 20)


Step 1
Processor X places the PC contents (0x0E) on the program memory address bus and
Performs a read operation. At the end of the read operation, PC is incremented to 0x0F.
Step 2
The returned opcode (0x05) requires processor X to performs another read to fetch an
8-bit address from program memory.
Step 3
Processor X places the contents of PC on the program memory address bus to request
another read from program memory. PC is incremented to 0x10
Step 4
Processor X places the value 0x20 on the data memory address bus and requests a read.
The returned value is placed in MDR register.
Step 5
Processor X adds one to the MDR register.
Step 6
Processor X places the contents of MDR on data memory data bus and 0x20 on the address
bus and requests a write operation. At the end the value in MDR register is written into
data memory location at 0x20.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-30

The HCS12/MC9S12 Microcontroller

Instruction dbnz 0x21, loop (machine code is D5 21 0A)


Step 1
Processor X places the contents of PC on the program memory address bus and requests
a read operation. At the end of this step PC is incremented to 0x11.
Step 2
The opcode (0xD5) returned in step 1 request processor X to perform two more read
operation to fetch a byte of data memory address and a byte of branch offset from
program memory. At the end of these two read operations, PC is incremented to 0x13.
Step 3
Processor places the value of 0x21 on the data memory address bus to perform a read.
At the end of this read, the contents of data memory location at 0x21 is returned in MDR.
Step 4
Processor decrements the contents of MDR by 1 and then places this result on the data
memory data bus. At the same time, it also places the value of 0x21 on the data memory
address bus and requests a write operation.
Step 5
If the value stored in MDR is not zero, processor X adds 0x0A to the PC and places the sum
back in PC (this causes a branch). Otherwise, PC is not changed.

Copyright 2010 Delmar Cengage Learning

H. Huang Transparency No.1-31

You might also like