Giáo trình hệ thống nhúng
Trang 1Chapter 1 Microprocessor Operation
This chapter lets you understand that the microprocessor is built of a CPU, memory and data input/output ICs, that it operates on a "stored program" basis, and that it is available in multi-chip/single-chip architectures
These concepts are not confined to the H8/300H but extend to all other kinds of microprocessors
1.1 Microprocessor Configuration
The microprocessor is said to be a "computer built around ICs."
Mainframes, minis, and microprocessors all share the same principles of operation and vary only in their scale, speed and architecture The minimum components required to build a computer are the CPU, memory and I/O devices as shown in Figure 1.1
Figure 1.1 Microprocessor configuration
All these components of a microprocessor are fabricated of a single IC Such ICs are coupled to build a computer Three minimum ICs needed to make
up a microprocessor are the CPU, memory, and peripheral IC
Trang 2CPU (Central Processing Unit)
The CPU forms the nucleus of any computer by executing instructions Microprocessors are grouped into 4-bit, 8-bit, 16-bit, and 32-bit
microprocessors according to the length of bits they can handle at a time A bit microprocessor can handle four binary digits in a single instance of calculation, but as many as eight digits in two instances and 16 in four instances
The microprocessor is also known as a "MPU (microprocessing unit)",
"microprocessor", or simply "processor."
Memory (Memory)
A device on which instructions and data are stored Without memory, programs and data cannot be used In a microprocessor, ROM (read-only memory) and RAM (random access memory) are used
Input device (Input)
A data input device The keyboard and mouse of a PC, for example, are data input devices With a built-in controller, switches and sensors are input devices
These input devices cannot be directly connected to a CPU, but they must be attached to the CPU by way of a "peripheral IC," containing connection circuitry Depending on the kind of input device to be connected to the CPU, an appropriate peripheral IC is used
Output device (Output)
A data output device The display and printer of a PC, for example, are data output devices With a built-in controller, display LEDs, motors, heaters and so on are output devices Like input devices, output devices are attached to
a CPU by way of a "peripheral IC." Depending on the kind of output device to
be connected to the CPU, an appropriate peripheral IC is used
Input devices and output devices are collectively called "peripherals."
1.2 Stored Program Computers
The stored program computer provides a most precise concept of the operating principles of a computer It might be safely said that "All modern computers are stored program computers." The stored program computer was first conceptualized in 1947 by John von Neumann and is also known as a
"Neumann computer."
In the stored program computer, the CPU reads instructions stored in memory in sequence, decodes and executes them
The act of the CPU reading an instruction from memory is called
"fetch." Interpreting the fetched instruction to see what operation it defines is called "decode." The CPU then proceeds to perform, or "execute," the operation defined by the instruction When the CPU has finished executing the
Trang 3instruction, it fetches the next instruction After all, the CPU infinitely repeats the following cycle of operations:
Figure 1.2 Operating principles of a stored program computer
1.3 Memory
Memory devices are broadly classified into two categories: ROM only memory) and RAM (random access memory)
(read-You can only read stored data from ROM but cannot write to it Stored data is preserved intact, however, when the microprocessor is switched off Use ROM to store valuable data that needs to be protected from erasure in times of power failures, typically, programs Instructions are stored in ROM Each meaningful collection of instructions is a program Any microprocessor would
be inoperable unless it comes up with "programs available for ready use" when switched on ROM fills this need
Data can be written to and read from RAM as desired Stored data would be lost, however, once the microprocessor is switched off Even when
Trang 4the microprocessor is switched on again, previous data is no longer left Hence, RAM is used as temporary data storage Programs may also be placed in RAM, but will be lost once the microprocessor is switched off To run programs in RAM, it is necessary to attach an external storage device, such as a floppy disk
or hard disk drive, and transfer the programs to RAM from external storage to RAM when the microprocessor is switched on
Mask ROM (Mask ROM)
When a memory IC is manufactured in the factory, programs are written to it A mask is a plate of glass imprinted with patterns of wirings and transistors used in the IC manufacturing process Users have a semiconductor manufacture custom-build a mask to manufacture a memory IC
Advantages
- Suitable for volume production
- Low cost Disadvantages
- Long lead-time from ordering to completion
- Not reprogrammable once built
EPROM (Erasable & Programmable ROM)
Stored data can be erased by ultraviolet irradiation To this end, a special package with a glass window is used An EPROM writer is used to write to EPROM EPROM is erasable and programmable about 100 times Advantage
- Erasable and programmable and thus convenient for testing and debugging Disadvantage
- Expensive because of the use of a special package
OTPROM (One Time Programmable ROM)
An EPROM chip housed in an inexpensive plastic package Stored data cannot be erased by ultraviolet irradiation because no glass window can be attached to the plastic package OPTROM can be written only once, but it comes by far cheaper than EPROM Programs are debugged in EPPROM and, when finalized, moved to OTPROM for volume production
Advantage
- Cheaper than EPROM and suitable for small-batch production Disadvantage
- Not erasable and programmable
EEPROM (Electrically Erasable & Programmable ROM)
EEPROM can be electrically erased and programmable, and can be reprogrammed when mounted on a board as a finished product EEPROM is reprogrammable about tens of thousand times
Trang 5Flash memory (Flash Memory)
A variation of flash memory, which is cheaper and larger-sized Advantage
- Cheaper and larger-sized than EEPROM Disadvantages
- Unable to write address by address, unlike EEPROM
- Memory IC divided into blocks for erasure and reprogramming block by block
Static RAM (Static RAM)
RAM with its storage circuit built of flip-flops Given a supply voltage, static RAM preserves stored data intact It dissipates least power when out of use Because six transistors are used to build its flip-flops, static RAM offers less storage capacity than does ROM
- Small storage capacity
Dynamic RAM (Dynamic RAM)
Simplified storage circuitry with only one capacitor and one transistor
to provide each bit of memory, and hence larger-sized than static RAM Charges on the capacitors, however, drain with time, resulting ultimately in loss of stored data Before such loss, all stored data must be read out and refreshed Power alone does not allow dynamic RAM to retain stored data but requires refreshing for that purpose
Advantage
- Cheap and large-sized Disadvantage
- Refreshing required Table 1.1 summarizes features of key ROM and RAM devices
Trang 6Table 1.1 Kinds and features of memory devices
ROM
Mask ROM Large-sized, cheap, volume production use, custom
fabrication, not reprogrammable EPROM Programmable and erasable by ultraviolet
irradiation Testing, debugging OTPROM Low-volume production use, one-time
programmable EEPROM Electrically programmable, onboard
reprogrammable Flash memory Electrically erasable and programmable, cheaper
and larger sized than EEPROM
RAM
Static RAM Stored data preserved under voltage input alone,
fast, battery backup use Dynamic RAM Refreshing required to preserve data, large-sized,
cheap
1.4 Single-Chip/Multi-Chip Microprocessors
Putting a CPU, ROM, RAM, and data input/output circuitry into a single IC will make a single-chip microprocessor Single-chip microprocessors come compact and cheap, but do not allow users to choose built-in functions at their option Single-chip microprocessors are also known as "microcomputer units (MCUs)," because they are made of a single IC
On the other hand, a computer fabricated from a mix of a CPU, memory and data input/output devices is called a "multi-chip microprocessor." Multi-chip microprocessors offer users greater freedom in their component choice Multi-chip microprocessors will prove more advantageous for larger systems involving complexities of input/output See how a single-chip microprocessor and a multi-chip microprocessor differ in Figure 1.3
Trang 7
Figure 1.3 Single-chip microprocessor and multi-chip microprocessor
Single-chip microprocessors are used as built-in controllers When a single-chip microprocessor is switched on, the control program stored in its internal ROM launches instantly Internal RAM is used as temporary storage The internal data input/output circuits, too, have been chosen to support the single-chip microprocessor as s controller
Kinds of internal ROM
Early single-chip microprocessors included only mask ROM and were available only for use in mass-produced products
Subsequently EPROM has made inroads into single-chip microprocessors as internal ROM As testing and debugging was carried out in erasable and programmable packages with windows and inexpensive plastic-packaged OTPROM used on commercialization, single-chip microprocessors came to be used in products manufactured in small batches as well
Now, single-chip microprocessors with internal EEPROM and flash memory are available for use in various applications As the IC manufacturing technology has been advancing from year to year, with continuing increases in the sizes of internal ROM and RAM, state-of-the-art data input/output
functions have been used in single-chip microprocessors in an expanding of applications
Microprocessors and memory
A memory device organized into 8 bits per address is used Each sequence of 8 bits is called a "byte," and memory sizes are stated in the unit byte
64K bytes of memory could be connected to classical 8-bit microprocessors as standard This memory size indicates 8 bits per address in a memory space of 64K (65536 addresses) Eight bits of memory per address are
Trang 8also connected to 16- and 32-bit microprocessors, as well as 8-bit microprocessors
1 What are the three key ICs needed to make up a microprocessor?
(CPU (MPU ) )
(Memory )
(Peripheral IC)
Mainframes and minis share the same computer architecture
Microprocessors are characterized by the fact that three components of the computer - namely, the CPU, memory, and input/output circuit (peripheral IC)
- are each fabricated as an IC
2 Fill the blanks with appropriate words or phrases
The (stored program computer) provides a most precise concept of the
operating principles of a computer This was first conceptualized in 1947 by
(John von Neumann)
The stored program computer materializes the principles of a computer
in which the CPU reads instructions from memory and executes them This is also known as a "Neumann computer," because of its conceptualization by John von Neumann
3 Mention one advantage and one disadvantage for each of the following
kinds of memory:
Mask ROM Advantage (Inexpensive, large-sized )
Disadvantage (Available only on order, not reprogrammable)
EPROM Advantage (Erasable and programmable)
Dynamic RAM
Advantage (Inexpensive, large-sized )
Disadvantage (Refreshing required )
Mask ROM is volume-produced at factory at low cost, and features
circuit simplicity, offering large-sized memory Mask ROM can be manufactured only by order and is not reprogrammable after it is built
EPROM allows about 100 times of erasure and reprogramming using a special writer, but the special package with a glass window adds to its cost Using capacitors as storage devices, DRAM is simple in circuitry, inexpensive, and large-sized, but what is inconvenient, it must be refreshed so that charges on the capacitors do not drain
Trang 9
4 Mention one advantage and one disadvantage for the microprocessor
Advantage (Compact, inexpensive )
Disadvantage (Limited internal functionality )
(Small memory size Not suitable for use in larger systems )
A single-chip microprocessor has all functions assembled on a single
chip and therefore comes compact and more inexpensive than purchasing its components separately Its scale of integration is limited and is not suitable for mounting extra-large-sized memory
Trang 10Chapter 2 Knowledge of Binary Numbers Prerequisite to Writing
a Program
There are close links between the computer and binary numbers This chapter covers the minimum knowledge of binary numbers prerequisite to writing a program in an assembler language The concept of binary numbers is not restricted to the H8/300H but broadly pertains to computers in general
Without a correct understanding of the topics covered here, you would not be able to write correct programs The key concepts of "Signed binary numbers,"
"Carry," and "Overflow," among other things, would be needed instantly
Even when you have finished with this chapter, refer back to it from time to time as needed
The reason why binary numbers are used in the computer is that the computer is built of digital circuitry Digital circuitry concerns only two states - whether a voltage of interest is higher or lower than a given voltage - and not any intermediate voltage A higher-voltage state is designated by H, a lower-voltage state by L As the computer is a calculator, the two states of H and L can be more conveniently expressed in numeric terms as 1 and 0 in binary All binary numbers that the computer handles correspond to H and L in digital circuitry
The unit bit, or binary digit, is used to count binary numbers For example, a reference to 8 bits means 8 digits in binary A sequence of 8 bits is called a "byte."
2.1 Kinds of Data Handled by Microprocessors
Before starting to consider how data is represented in binary numbers, let's see what kinds of data are available Here, data is broadly grouped into numeric data and character data as shown in Figure 2.1
Figure 2.1 Kinds of data
Trang 11Numeric data is classified into unsigned binary numbers, signed binary numbers that distinguish between positive and negative, and BCD code used to express decimal numbers
Character data is used to print or display characters, or enter characters from the keyboard The ASCII code is mainly used in microprocessors
2.2 Numeric Representation in Unsigned Binary Number
Unsigned binary numbers do not allow for the positive or negative signs To promote understanding, unsigned binary number may be
conceptually converted to decimal numbers to express values Consider an eight-digit binary number as an example
Since each digit of a binary number has a weight of 2, the least significant digit is 20, or 1, the next digit is 21, or 2, and still the next digit is 22,
or 4 Thus, the weight doubles on each carry In converting an 8-bit binary number to a decimal number, assuming
we get Decimal representation =
a7*27 + a6*26 + a5*25 + a4*24 + a3*23 + a2*22 + a1*21+ a0*20
10110010 in binary, for example, is converted to a decimal equivalent as 1*27 + 0*26 + 1*25 + 1*24 + 0*23 + 0*22 + 1*21 + 0*20
= 128 + 32 + 16 + 2 = 178
All-one data "11111111" is the largest number that can be expressed in
an 8-bit format It corresponds to 255 in decimal Eight-bit unsigned binary numbers can represent decimal numbers from 0 to 255
Generally, N-bit unsigned binary numbers can represent the range of 0
Trang 12Examples of addition of unsigned binary numbers are given below To their right are their decimal equivalents
1000001000111000
01001010+
13056
74+and,
0000001101111010100010011
3122
173+
This calculation, on the other hand, yields an incorrect result The resultant 9-bit value of 255 may appear correct, but the fact is that "calculating
in 8-bit terms within the computer delivers a result in 8-bit terms only." You can stretch or contract the length of a value as long as you work on its calculation on paper or in your brains, but not in the computer You need to remember the length of arithmetic circuitry in the computer at all times
The addition of an extra digit to the length of a result is called a "carry."
Figure 2.2 Carry in a binary number
Next, examples of subtraction of unsigned binary numbers are given below To their right are their decimal equivalents
0001111010010111
10110101
−
30151
181
− and,
Trang 13
1111111101010000
01001111
−
25580
79
−
This calculation yields an incorrect result due to its failure to subtract a given value from a smaller value, where a 1 was leased from the ninth bit of the lower value This is called a "borrow."
When a carry or borrow occurs in the course of a calculation in the computer, they are stored in status (in the H8/300H, the condition code) When writing a program, include a condition test instruction to define what specific action should be taken if a carry or borrow is encountered in the execution of a calculation instruction
Generally, no distinction is made between a carry and a borrow in the computer, but they are collectively called a "carry."
2.3 Numeric Representation in Signed Binary Number
Signed binary numbers distinguish between positive and negative, but they cannot be prefixed with a sign, as in +10110001 or -11001110 This is because the computer operates on the basis of digital circuitry in which only two voltage states H and L exist, as explained earlier As H and L are designated by 1 and 0, the positive and negative states must be designated by a combination of 1 and 0, as well
To promote understanding, the most significant bit of data is assumed
to designate a sign Eight-bit data is expressed as -(a7*27)+ a6*26 + a5*25 + a4*24 + a3*23 + a2*22 + a1*21+ a0*20Only the most significant bit is treated as being negative Accordingly,
00000000 represents +0
00000001 represents +1
00000010 represents +2 -
11111111 represents -1
Trang 14Thus, signed binary values having their most significant bit being 1 are treated negative
Generally, N-bit signed binary numbers can represent values in the following range:
Figure 2.3 8-bit binary numbers
Examples of addition of signed binary numbers are given below
0000000011111111000000011
+
011
+
−
++
Trang 15The result is correct if only the low-order 8 digits are considered
0110000000100000100000001
−
96+
32+
128-
−
This calculation, on the other hand, gives an incorrect result, because the correct result of +144 cannot be represented by an 8-bit signed binary number Such a state of a result exceeding a predetermined length is called an
"overflow."
Next, examples of subtraction of signed binary numbers are given below
1111111101010010
01010001
−
1-
82+
81+
−
No overflow has occurred in this calculation
0010000000100000
10000000
−
96+
32+
128-
−
Subtracting a positive value from a negative value should always deliver a negative value, but an overflow is seen in this case because the result
is positive Summing up, the following four conditions
Positive value + Positive value = Negative value Negative value + Negative value = Positive value Positive value - Negative value = Negative value Negative value - Positive value = Positive value are called "overflows."
It is important to note the length of any signed binary number, because simply prefixing a signed binary number with 0 could result in an inverted sign
as in the following example:
8-bit 1000 0010 is -126 9-bit 0 1000 0010 is +130
Twos complement notation
Signed binary numbers are also called a "twos complement notation." If summing up two values produces a carry, with the result of 0, then a twos complement relation exists between the two values as in the following example:
0000000001001111101100011
+ These two values have the same absolute value but differ only in their sign Ones complements, too, exist
Trang 1610110001+
If summing up two values results in a complete sequence of 1s, a ones complement relation is said to exist between them These two values have their 0s and 1s inverted
Changing the sign of a signed binary number can be easily done by creating its ones complement and then adding 1 to it This is the same as calculating a complement As an example, consider converting +12 to -12 +12 can be expressed in an 8-bit format as:
00001100 First, create the ones complement of 00001100:
11110011 Then, add 1 to it to get:
01001000+
7224
48+
It involves addition different from the ordinary addition of binary numbers To this end, special instructions are needed to perform BCD calculations
2.5 ASCII Code
The ASCII (American Standard Code for Information Interchange) code is used to represent alphanumeric characters and symbols The ASCII code represents each character with 8 bits Table 2.3 is an ASCII code table For example, A is represented by 01000001 and a by 01100001 When you type 123 from the keyboard and then press the return key, the following four characters are entered:
Character code of 1 00110001 Character code of 2 00110010 Character code of 3 00110011 Character code of the return 00001101
Trang 17To handle these characters as unsigned binary 123, they must be converted to the binary number:
01111011
On the other hand, to display signed binary number
10000000
on the display as -128, it must be converted to the four characters:
Character of the minus sign 00101101 Character code of 1 00110001
Character code of 2 00110010 Character code of 8 0011100
Note that the ASCII code is characters, and 1 through 9 are "digits" and not "numeric values."
1100100 Hexadecimal numbers save on extra lengths and make values easier
to read Memory addresses are expressed in hexadecimal in most situations Binary notation also comes an effective way of representation as long
as economizing on lengths is concerned, but it involves a laborious binary conversion process Hexadecimal numbers are easy to convert to and from
Trang 18binary numbers, because each hexadecimal digit simply represents a string of four binary digits
Table 2.4 gives the correspondence among binary, hexadecimal, and decimal numbers
Table 2.4 Correspondence among binary, hexadecimal and decimal
numbers Binary Hexadecimal Decimal
Trang 191 Fill out the blanks below
Binary notation (8 bits)
Decimal notation (unsigned)
Decimal notation (signed)
Hexadecimal notation
Decimal notation (unsigned) 100 Binary notation 100 = 64 + 32 + 4 = 01100100 Decimal notation (signed) = positive + 100, because the most significant bit is 0 Hexadecimal notation 0110 = 6 and 0100 = 4, hence H'64
Decimal notation (signed) -3 Binary notation -3 = -128 + 64 + 32 + 16+ 8 + 4 + 1 = 11111101 Decimal notation (unsigned) 128 + 64 + 32 + 16+ 8 + 4 + 1 = 253 Hexadecimal notation 1111 = F and 1101 = D, hence H'FD
Hexadecimal notation H'80 Binary notation 10000000 = -128 Decimal notation (unsigned) 10000000 = 128 Decimal notation (signed) = negative -128, because the most significant bit is 1
2 Perform the operations given below and calculate the results to a length
of 8 bits Answer also whether a carry or borrow has been produced from the operations and whether an overflow has occurred from the results when they are viewed as signed binary numbers
Carry ( Yes )
Overflow ( No )
The result is 9 digits, with a carry
+72 + (-49) = +23, or positive + negative = positive with no overflow
Borrow ( No )
Overflow ( Yes )
No borrow and no carry
-116 - (+43) = +65, or negative - positive = positive, with an overflow
Trang 20Chapter 3 H8/300H Series Overview, and H8/3048
This chapter describes the use of an H8/3048 microcomputer from the H8/300H family The H8/3048 is a high-performance microcomputer having ROM, RAM and peripheral functions
It will help you understand the functions of this product
3.1 H8/300H Series Product Map
The H8/300H series is a original high-performance, single-chip microcomputer having a 16-bit CPU as its core together with peripheral functions required for system configuration Figure 3.1 shows the product lineup of the H8 family
Figure 3.1: Product Lineup of H8 Family
Figure 3.2 shows the product lineup of the H8/300H series
Trang 21Figure 3.2: Product Lineup of H8/300H series 3.2 H8/3048 Overview
In this course, an H8/3048 is used as the sample processor from the H8/300H family The H8/3048 includes the following three types: one having a mask ROM or OTPROM as an internal ROM (simply referred to as the
H8/3048), an H8/3048F having an internal writable flash memory with two power supplies (5V and 12V) and an H8/3048F-ONE having an internal writable flash memory with a single power supply (5V) The only difference among them is the type of internal ROM, the rest are almost the same
Although this chapter describes the overview of the H8/3048F-ONE, the descriptions in this and following chapters are common to all types Note, however, that the training board has been developed based on the H8/3048F-ONE
Figure 3.3 shows the H8/3048F-ONE internal block
Trang 22Figure 3.3: H8/3048F-ONE Internal Block
Features of CPU
- 16-bit CPU which serves as a general-purpose register machine
Equipped with 16-bit × 16 general-purpose registers
(Also available in 8-bit × 16 + 16-bit × 8 or 32-bit × 8 form.)
- High-speed CPU
The maximum operating frequency of the H8/3048F-ONE is 25MHz and addition/subtraction can be executed in 80ns and multiplication/division in 560ns
The CPU is operated based on clock signals and the higher the clock signal frequency, the faster the operation The time of one 25MHz clock signal pulse is 0.04 microsecond (40ns), which is called
"1 state" Addition/subtraction are completed in two states and multiplication/division in 14 states
- Equipped with a maximum 16M bytes of address space
(CPU, instructions and programs are described in Chapters 4, 5 and 6.)
Available both as single-chip and multi-chip microcomputer
It can be used as a single-chip microcomputer thanks to the internal ROM, RAM and data I/O functions in addition to the CPU
It can also be used as a multi-chip microcomputer when an external memory is added due to insufficient ROM or RAM capacity When used as a multi-chip microcomputer, it has 24 address pins and up to 16M bytes of memory can be added Addresses consume 16M, 8 bits per address Since there are 16 data pins, 16 bits can be read or written simultaneously
Trang 23It has an internal, 4k byte RAM
I/O ports: 70 I/O pins and 8 input-only pins
The I/O ports can be used to input switch on/off statuses or signals from various sensors When used as output ports, they can control blinking of a display lamp or turn a motor or heater on and off The I/O ports are widely used as general-purpose I/O functions
On the training board, they are used to read switch statuses and control LED displays
Internal SCI (serial communication interface) × 2 channels
Channel 0 is available for a smart card interface
For start-stop synchronization, it is used for RS-232C and other communication functions It is connected to a PC to enable data exchange
On the training board, it is connected to a PC via RS-232C to send a created program from a PC to the board or input a command for debugging
Internal ITU (integrated timer unit) composed of 16-bit timer × 5 channels
Pulse outputs from up to 12 pins and up to 10 types of pulse inputs can
Internal watch-dog timer
Program runaway can be detected Since control using a microcomputer
is based on programs, an extremely dangerous situation may occur if a program should run away This function is indispensable for creating a highly reliable system
Internal A/D converter with 10-bit resolution × 8 channels
The A/D converter is designed to input analog voltages instead of digital voltages For example, signals from a temperature sensor are input not
Trang 24by high/low digital voltages but analog voltages These signals are read after being converted into 10-bit binary numbers
On the training board, 0 to 5V analog voltages can be input with added volume
Internal 8-bit D/A converter × 2 channels
Unlike the A/D converter, the D/A converter is designed to output analog voltages It is also used to output desired waveforms or adjust volume
or hue of a TV
On the training board, LEDs can be driven with D/A-converted output voltages to control brightness
Internal DMA controller × 4 channels (max.)
Used for high-speed data transfer It enables data to be transferred faster than with the CPU It is generally used with a timer and other communication functions
Internal refresh controller
Refreshing is required to retain the data in the dynamic RAM This microprocessor has an internal controller for this purpose
The H8/3048F-ONE can be used as a single-chip microcomputer In this form, no external memory can be added and only the internal one is available The operating mode when it is used as a single-chip microcomputer
is referred to as "single-chip mode" Figure 3.4 shows the memory map in single-chip mode In this mode, the memory addresses are expressed with 20 bits (5 digits in hexadecimal notation)
Figure 3.4: Memory Map in Single-chip Mode
Trang 25• For the vector area, refer to "Exception Handling"
• For the memory indirect branch addresses, refer to "Addressing Modes
an instruction Not all addresses can be used freely, however, since other exception handling functions also use the same range
Sample: JMP @@H'10 Branches to the address stored in the H'10 address
Although the destination is written with 32 bits in the memory, only the lower 24 bits are used An even address must be specified by an instruction The address of the memory storing the destination can also be specified using a symbol The assembler converts a written symbol into an address, which is assumed to be the destination
Sample: JMP @@WORK Branches to the address written in the WORK address
Trang 26The H8/3048F-ONE can also be used as a multi-chip microcomputer The operating mode in this case is referred to as "external extension mode" In external extension mode, a memory or peripheral IC can be connected
externally Figure 3.5 shows the memory map in external extension mode Since there are 24 address buses, up to 16M bytes of external memory can be added Addresses are expressed with 6-digit, hexadecimal numbers
The addresses of the internal ROM, RAM and internal I/O register, however, are fixed and cannot be changed If the address of the memory to be added is the same as that of the internal ROM, RAM or internal I/O register, the internal function has priority, disabling reading/writing from/to the external memory
Trang 27Figure 3.5: Memory Map in External Extension Mode
• For areas 0 to 7, refer to "Connecting CPU to Memory" When creating
a program, you need not take areas 0 to 7 into account
Although this chapter has described the overview of the H8/3048F-ONE, table 3.1 shows the differences between the H8/3048, H8/3048F and
H8/3048F-ONE as well as their features for summary
Table 3.1: Differences Between the H8/3048, H8/3048F and H8/3048F-ONE and Their Features
Flash memory (writable with single 5V power
supply) 128Kbyte
Power supply voltage
Trang 28- TPC capable of outputting up to 16-bit pulses
- Watch-dog timer for detecting program runaway
- A/D converter with 10-bit resolution x 8 channels
- 8-bit D/A converter x 2 channels
- DMA controller x 4 channels (max.)
- Refresh controller
1 Enter an appropriate word in parentheses
The H8/3048 has an internal ( 16 )-bit CPU and up to ( 16M ) bytes of
memory can be connected
The H8/3048 belongs to the H8/300H series and employs a 16-bit CPU
It has up to 24 address pins and the memory addresses are expressed in 24-bit binary numbers This means that 16M (mega) bytes of memory, or 16,777,216 addresses can be connected
2 Enter an appropriate word in parentheses
( SCI ) has a variety of internal I/O functions and can be connected to a PC.( ITU ) is used to control pulse motors
SCI is an acronym for Serial Communication Interface used for communication
ITU is an acronym for Integrated Timer pulse Unit which serves as a timer with a pulse I/O function Its typical application is to control pulse motors
Trang 29Chapter 4 Writing a Simple Program in an Assembly Language
This chapter gives an overview of a program developed in an assembly language used by the H8/300H Only basic instructions are introduced here to help you understand how a program proceeds and how the contents of the registers and memory change as it progresses
To understand these subjects, you need a knowledge of binary numbers described in Chapter 2 Learn how a program is configured and proceeds before going on to the next chapter which explains instructions in detail
The assembly language is the most basic programming language and corresponds to machine instructions one-to-one, making it the most suitable language for understanding microcomputer operation Although C-language is also becoming popular in the microcomputer field, studying programs written
in the assembly language will be very helpful for developing a program with C-language afterward The CPU can execute machine instructions only No matter in which language a program is written, it must be converted into machine instructions in the end Since machine instructions are collections of 0s and 1s, it is difficult to develop a program directly with machine language For this reason, assembly language is used since it enables machine language
to be expressed in easily understandable alphabets For example, a machine instruction to add the R0L and R1L as an arithmetic register is expressed as follows in 16 bits:
4.1 CPU Internal Registers
Before developing a program with assembly language, you need to know what kinds of registers and functions the CPU has Figure 4.1 shows the CPU internal registers of the H8/300H
Trang 30Figure 4.1: CPU Internal Registers
The internal registers are classified into general-purpose and control registers
The general-purpose registers are used to calculate data and store addresses
The control register is further classified into the PC (program counter)
to control program progress and the CCR (condition code register) to test conditions
How to use general-purpose registers
The CPU has 8 general-purpose registers, each capable of storing digit binary numbers.In addition to 32-bit data, they can also store 16- or 8-bit data
32-When 32-bit data is stored, they are described as follows in an instruction, using 8 registers in all:
ER0, ER1, ER2, ER3, ER4, ER5, ER6, ER7
When 16-bit data is stored, they are described as follows in an instruction, using registers as 16 units in all:
E0, E1, E2, E3, E4, E5, E6, E7, R0, R1, R2, R3, R4, R5, R6, R7
When 8-bit data is stored, they are described as follows in an instruction, using registers as 16 units in all:
R0H, R0L, R1H, R1L, R2H, R2L, R3H, R3L, R4H, R4L, R5H, R5L, R6H, R6L, R7H, R7L
Trang 31This is illustrated in Figure 4.2
Figure 4.2: General-purpose Register
Example of calculation by general-purpose registers
In this example, an add instruction is used to show how general-purpose registers are actually used
ADD.B R0L,R1H is an instruction for 8-bit addition ADD represents
"ADDition" and B represents "Byte" (8 bits) The contents of the R1H and R0L are added and the results are stored in the R1H
This will not influence the E1 or R1L Only 8-bit results are obtained Any 8-bit register is available for this calculation For example, you can specify the same register like "ADD.B R1L,R1L" In this case, the R1L is doubled
ADD.W R0,E1 is an instruction for 16-bit addition ADD represents
"ADDition" and W represents "Word" (16 bits) The contents of the E1 and R0 are added and the results are stored in the E1
Trang 32This will not influence the R1.Only 16-bit results are obtained
ADD.L ER0,ER1 is an instruction for 32-bit addition.ADD represents
"ADDition" and L represents "Long word" (32 bits) The contents of the ER1 and ER0 are added and the results are stored in the ER1
The 32-bit results are stored in the ER1
SP (stack pointer)
A special function has been added to the ER7 as a stack pointer The ER7 is usually not used for calculation but as a stack pointer The stack pointer function is described in detail in "Subroutines" and "Interrupt Operations"
PC (program counter)
n the program counter, the "address of the instruction to be executed next" is always stored and the data is automatically updated every time the CPU reads instructions Since the addresses are 24 bits, the PC also has 24-bit configuration Programmers need not pay special attention to how the PC is configured Every time an instruction is read, the address of the next instruction
is automatically stored
In the case of the H8/300H, an instruction is always read from an numbered address first This means that an even-numbered address is always stored in the PC (see "Data in the memory")
even-CCR (condition code register)
This is used to control interrupts or test conditions Although it is an bit register, every bit has a different meaning Interrupt control is described in detail in "Exception Handling"
This section describes the part used for conditional test Every time an instruction is executed, the N, Z, V and C bits change to reflect the results Conditions are tested based on their changes An instruction to be tested exists separately The N, Z, V and C bits are called "flags"
N (Negative) flag: When the execution results are regarded to be a signed binary number, set to 1 if it is negative, or 0 if positive
Z (Zero) flag: Set to 1 if the execution results are zero, otherwise, 0
V (oVerflow) flag: When the execution results are regarded to be a signed binary number, set to 1 if it overflows, otherwise, 0
C (Carry) flag: Set to 1 if execution results in a carry or borrow, otherwise, 0
Trang 33Conditional test in a program is performed by these four flags Any condition can be tested using them
Conditional test using the CCR
As for two numeric values, X and Y, let's consider how to test their collating sequence.To test the collating sequence, subtraction is used By subtracting Y from X, the sequence can be tested based on how N, Z, V and C
Data in the memory
The following describes how to store 8-, 16- and 32-bit data into the memory
Not only the H8/300H but all 16-bit microcomputers use 8 bits of the memory per address So, one 8-bit data block exactly occupies one address
One 16-bit data block occupies two addresses The upper 8 bits are stored in a smaller address and the lower 8 bits in a larger one The smaller one must be an even-numbered address Although each data block is stored separately in two addresses, the smaller one is regarded to be the address storing the data For example, "16-bit data in the H'1000 address" means that the upper 8 bits are stored in the H'1000 address and the lower in the H'1001 address
Trang 34In an instruction to read or write 16-bit data, you should specify an even-numbered address (smaller address) If you attempt to read or write 16-bit data by specifying an odd-numbered address, reading/writing will fail For the reason why this restriction applies, refer to "Connecting CPU to Memory (16-bit Data Bus)"
In the case of the H8/300H, an instruction is always read in 16-bit units This means that an instruction must be stored in an even-numbered address H8/300H machine instructions are composed in 16-bit integral multiples If the first instruction falls in an even-numbered address, the subsequent instructions also fall in even-numbered addresses
One 32-bit data block occupies four addresses of the memory Since the H8/300H cannot read or write 32-bit data at a time, data are divided into 16-bit units for reading/writing In this case, the first data must also fall in an even-numbered address Likewise, the most significant 8 bits are stored in the smallest address and the least significant 8 bits in the largest one
[Explanation with motion pictures and sound]
Trang 351 ( T ) There are eight 32-bit general-purpose registers in all
There are eight general-purpose registers, from ER0 to ER7
2 ( T ) The ER7 is a stack pointer
Among the general-purpose registers, only the ER7 has a special stack pointer function.
3 ( F ) The CCR is a 16-bit register
The CCR (Condition Code Register) is a control registers with 8-bit configuration
4 ( F ) The PC stores the instruction currently being executed
The PC (Program Counter) does not store instructions but the
"address" of the instruction to be executed next
5 ( F ) Although the ER0 can perform addition, the ER6 cannot
All general purpose registers from ER0 to ER7 can handle the same instructions.(The ER7, however, has a special stack pointer function)
6 ( T ) The least significant 8 bits of the ER0 is the R0L
The upper 16 bits of the ER0 are the E0 and the lower 16 bits are the
R0.And the upper 8 bits of the R0 are the R0H and the lower 8 bits are the R0L
7 ( F ) The upper 16 bits of the ER0 is the R0
The upper 16 bits of the ER0 are the E0 and the lower 16 bits are the
Since this flag is named "Carry", it is set to 1 when calculation results
in a carry Otherwise, zero
11 ( T )One address of the memory is 8 bits
Except for special microcomputers such as 4-bit types, 8 bits (1 byte) of
the memory are used per address
12 ( T )8-bit data can be stored in both even- and odd-numbered addresses
Since 8-bit data exactly occupies one address of the memory, it can be stored in either an even- or odd-numbered address
13 ( T )16-bit data must be stored in an even-numbered address
Since the H8/3048 reads and writes 16 bits of data at a time, the upper
8 bits must be stored in an even-numbered address and the lower 8 bits in the next address If 16-bit data is stored in an odd-numbered address and the next even-numbered address, reading/writing will fail
Trang 364.2 Instruction Configuration
This section describes some basic instructions used in assembly language And the subsequent sections explain how to develop a program using them
MOV instruction
The MOV (MOVe data) instruction is used for data transfer Although
"transfer" may sound like moving the original data, the function of this instruction is similar to copying and the original data remains
It is available from the memory to a purpose register, from a purpose register to the memory, between general-purpose registers and from data to a general-purpose register This instruction is most frequently used in a program
general-Samples MOV.B R0L,R1L Transfers 8-bit data from the R0L to the R1L
MOV.B @H'1000,R0L Transfers the 8 bits in the H'1000 address to the R0L
MOV.B R1L,@H'2000 Transfers the R1L to the 8 bits in the H'2000 address
MOV.B #1,R0L Inputs (transfers) data "1" in the R0L
ADD instruction
The ADD (ADD binary) instruction is used for addition The results are stored in the general-purpose register written on the right
Samples ADD.B R0L,R1L Adds the R1L and R0L and stores the results in the R1L.
ADD.B #H'12,R0L Adds the R0L and H'12 (18 in decimal notation) and stores
the results in the R0L
SUB instruction
The SUB instruction (SUBtract binary) is used for subtraction It subtracts the contents of the general-purpose register written on the left from those on the right and stores the results in the register written on the right Sample
SUB.B R0L,R1L Subtracts the R0L from the R1L and stores the results in
the R1L
CMP instruction
The CMP (CoMPare) instruction is used for comparison It performs subtraction not to obtain the results but simply for comparison What matters most is not what the answer is but how N, Z, V and C in the CCR change after subtraction In other words, the CMP instruction simply performs subtraction and changes N, Z, V and C in the CCR
A CMP instruction must be followed by a conditional branch instruction This is because comparison is meaningless without conditional test
Trang 37Samples CMP.B R0L,R1L Subtracts the R0L from the R1L, changing the CCR
Conditional branch instruction CMP.B #H'12,R0L Subtracts H'12 (18 in decimal notation) from the R0L,
The destination address is specified by giving it a name ("symbol") Sample
BRA ABC Unconditionally branches to the symbol ABC.
Instruction ABC: Instruction
CMP.B R0L,R1L Compares the R1L with the R0L
BGT ABC If the R1L is greater, branches to the symbol ABC
Instruction Otherwise, the next instruction is executed
Instruction ABC: Instruction
BHI instruction
The BHI (Branch HIgh) instruction is another type of conditional branch instruction It compares data as an "unsigned binary number" and branches to the specified instruction if it is greater Otherwise, it does nothing and the next instruction is executed
Sample CMP.B R0L,R1L Compares the R1L with the R0L.
BHI ABC If the R1L is greater, branches to the symbol ABC
Instruction Otherwise, the next instruction is executed
Instruction ABC: Instruction
Trang 384.3 Adder Program4.3.1 How to Develop a Source Program
This section describes how to develop a source program to add 8-bit data with assembly language
It is assumed that 8-bit unsigned binary numbers are stored in the H'2000 and H'2001 addresses of the memory Here, you will create a program
to add these two data blocks and write the results in the H'2002 address Up to
8 bits of results are obtained even if addition results in a carry, generating 9 bits
Since addition is performed, the following instruction is used:
ADD.B R0L,R1L
Any 8-bit register can be used as general-purpose registers other than the R0L or R1L
To perform addition using this instruction, you must input data to be added in the R0L and R1L beforehand To input data from the H'2000 address of the memory to the R0L general-purpose register, use the following instruction:
MOV.B @H'2000,R0L
To input data from the H'2001 address to the R1L general-purpose register, use the following instruction:
MOV.B @H'2001,R1L H'2000 represents an address in hexadecimal notation Memory addresses are generally expressed in this notation "@" is a mandatory prefix to indicate a memory address
The above instructions should be arranged as follows for addition:
Trang 39After reading one instruction, the CPU automatically stores the address
of the next instruction in the PC and reads the next instruction after execution
is completed Since the CPU does not understand whether the next address has
an instruction or not, it assumes that there must be an instruction in the next address and executes it even after executing the above four instructions This results in a runaway since the CPU executes non-existing instructions To prevent this, use the BRA instruction as follows:
ABC: BRA ABC
The above instruction leads to unlimited execution of the BRA instruction This prevents the program from proceeding and running away The program, however, is still incomplete
This program does not indicate at which address of the memory the program itself should be located It is indicated using an assembler control instruction The assembler control instruction is not executed by the CPU but used to instruct an assembler, which is machine language conversion software
.SECTION PROG,CODE,LOCATE=H'1000
Use the control instruction shown above Every assembler control instruction is prefixed with "." (period) With it, you can easily distinguish between assembler control instructions and those executed by the CPU (execution instructions)
.SECTION indicates the section control instruction, PROG represents the section name (section can be named originally based on certain rules), CODE refers to the instruction code, and LOCATE=H'1000 specifies that instructions should be located starting from the H'1000 address of the memory The CPU control instruction to specify the CPU type is also required since the assembler for the H8/300H is compatible with several CPU types In addition, the END control instruction must be written on the last line
Finally, a complete program is written as shown in List 4.1
List 4.1: Simplest Program
Trang 40This source program is converted into machine instructions by the assembler as follows:
Address Machine instruction Instruction
.CPU 300HA SECTION PROG,CODE,LOCATE=H'1000
.END
The machine instruction is expressed in hexadecimal notation Since one address of the memory is 8 bits, it is expressed with a 2-digit, hexadecimal number Since the machine instruction "MOV.B @H'2000,R0L" is eight digits, four addresses of the memory are used to store it (called "4-byte instruction")
In the case of the H8/300H, the shortest instruction is 2 bytes and the longest is
10 bytes
The first MOV instruction is stored in the four addresses starting from H'1000 (H'001000) and the next MOV instruction in the four addresses from H'1004 The ADD instruction is stored from H'1008, the next MOV instruction from H'100A and the last BRA instruction from H'100E Since CPU,
.SECTION and END are control instructions, they do not correspond to machine instructions
Let's consider how the contents of general-purpose registers and the memory change when a program is executed It is assumed that H'4C (B'01001100) is stored in the H'2000 address and H'40 (B'01000000) in the H'2001 address Also, the contents of the R0L general-purpose register is assumed to be H'00 and those of the R1L to be H'00
[Simulation]