Professional Documents
Culture Documents
Microprocessor Operation
These concepts are not confined to the H8/300H but extend to all other
kinds of microprocessors.
http://resource.renesas.com Page 1
CPU (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 4-
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.
http://resource.renesas.com Page 2
instruction, it fetches the next instruction. After all, the CPU infinitely repeats
the following cycle of operations:
- Instruction fetch
- Instruction decoding
- Instruction execution
Any microprocessor has a program counter in its CPU. The program
counter always holds the "address of the next instruction to be executed."
When the CPU reads an instruction, the program is automatically updated to
indicate the address of the next instruction in sequence. The program counter
thus ensures that instructions stored in memory will be executed in correct
sequence.
1.3 Memory
Memory devices are broadly classified into two categories: ROM (read-
only memory) and RAM (random access memory).
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
http://resource.renesas.com Page 3
the 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.
Advantages
- Suitable for volume production
- Low cost
Disadvantages
- Long lead-time from ordering to completion
- Not reprogrammable once built
Advantage
- Erasable and programmable and thus convenient for testing and debugging
Disadvantage
- Expensive because of the use of a special package
Advantage
- Cheaper than EPROM and suitable for small-batch production
Disadvantage
- Not erasable and programmable
http://resource.renesas.com Page 4
Advantages
- Onboard reprogrammable
- Ready for infinite times of reprogramming
Disadvantage
- Expensive
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
Advantages
- Fast
- Low power consumption and suitable for battery backup
Disadvantages
- Expensive
- Small storage capacity
Advantage
- Cheap and large-sized
Disadvantage
- Refreshing required
http://resource.renesas.com Page 5
Table 1.1 Kinds and features of memory devices
Kind Features
Large-sized, cheap, volume production use, custom
Mask ROM
fabrication, not reprogrammable
Programmable and erasable by ultraviolet
EPROM
irradiation. Testing, debugging
Low-volume production use, one-time
ROM OTPROM
programmable
Electrically programmable, onboard
EEPROM
reprogrammable
Electrically erasable and programmable, cheaper
Flash memory
and larger sized than EEPROM
Stored data preserved under voltage input alone,
Static RAM
fast, battery backup use
RAM
Refreshing required to preserve data, large-sized,
Dynamic RAM
cheap
http://resource.renesas.com Page 6
Figure 1.3 Single-chip microprocessor and multi-chip microprocessor
http://resource.renesas.com Page 7
also connected to 16- and 32-bit microprocessors, as well as 8-bit
microprocessors.
3. Mention one advantage and one disadvantage for each of the following
kinds of memory:
Dynamic RAM
Advantage (Inexpensive, large-sized )
Disadvantage (Refreshing required )
http://resource.renesas.com Page 8
4. Mention one advantage and one disadvantage for the microprocessor.
Advantage (Compact, inexpensive )
http://resource.renesas.com Page 9
Chapter 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."
http://resource.renesas.com Page 10
Numeric 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.
we get
Decimal representation =
a7*27 + a6*26 + a5*25 + a4*24 + a3*23 + a2*22 + a1*21+ a0*20
Table 2.1 lists the lengths of unsigned binary numbers and the ranges
they can represent.
http://resource.renesas.com Page 11
Examples of addition of unsigned binary numbers are given below. To
their right are their decimal equivalents.
01001010 74
+ 00111000 + 56
10000010 130
and,
10001001 173
+ 01111010 + 122
1 00000011 3
http://resource.renesas.com Page 12
01001111 79
− 01010000 − 80
11111111 255
00000000 represents +0
00000001 represents +1
00000010 represents +2
--------
01111111 represents +127
Thus, signed binary values having their most significant bit being 0 are treated
positive.
http://resource.renesas.com Page 13
Thus, 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:
-2N-1 to +2N-1-1
Table 2.2 lists the lengths of signed binary numbers and the ranges they
can represent.
The most significant bit of a signed binary number is called a "sign bit"
because it denotes a sign.
http://resource.renesas.com Page 14
The result is correct if only the low-order 8 digits are considered.
10000000 - 128
− 00100000 − + 32
1 01100000 + 96
http://resource.renesas.com Page 15
10110001
+ 01001110
11111111
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:
11110100, or -12
Short for "Binary Coded Decimal," BCD code means a decimal number
expressed in binary. The BCD code represents each decimal digit with a string
of four binary digits. For example, decimal 156 is expressed as 0001 0101
0110 in BCD code.
The addition of BCD coded characters must deliver a decimal result
like 48 + 24 = 72 as in the example:
01001000 48
+ 00100100 + 24
01110010 72
It involves addition different from the ordinary addition of binary numbers. To
this end, special instructions are needed to perform BCD calculations.
http://resource.renesas.com Page 16
To 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."
[Audiovisual guidance]
http://resource.renesas.com Page 17
binary 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.
Example:
H'12 H' identifies a hexadecimal number. It is 18 in decimal and
00010010 in binary.
B'01011100 B' identifies a binary number. It is 5C in hexadecimal and 92 in
decimal.
110 Decimal number. It is 6E in hexadecimal and 01101110 in
binary.
http://resource.renesas.com Page 18
1. Fill out the blanks below.
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 )
Borrow ( No )
Overflow ( Yes )
http://resource.renesas.com Page 19
Chapter 3
H8/300H Series Overview, and H8/3048
http://resource.renesas.com Page 20
Figure 3.2: Product Lineup of H8/300H series
http://resource.renesas.com Page 21
Figure 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.
http://resource.renesas.com Page 22
Internal ROM
It has a 128k byte, writable flash memory with a single 5V power
supply. This enables onboard writing.
For writing to the flash memory, a boot mode using a serial interface is
supported. Writing with a user-defined program is also available.
Internal RAM
It has an internal, 4k byte RAM.
http://resource.renesas.com Page 23
by 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.
http://resource.renesas.com Page 24
• For the vector area, refer to "Exception Handling".
• For the memory indirect branch addresses, refer to "Addressing Modes
of Instructions".
• The internal I/O registers are used for peripheral functions such as the
SCI and timer.
Memory indirect
Addressing for branching by storing the destination address in the
memory and specifying it.
This is written in the following format:
@@ address
Memory indirect is available only for the JSR and JMP instructions. An
even-numbered address between H’000000 and H’0000FC can be specified for
an instruction. Not all addresses can be used freely, however, since other
exception handling functions also use the same range.
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.
http://resource.renesas.com Page 25
The 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.
http://resource.renesas.com Page 26
Figure 3.5: Memory Map in External Extension Mode
http://resource.renesas.com Page 27
- 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
http://resource.renesas.com Page 28
Chapter 4
Writing a Simple Program in an Assembly Language
http://resource.renesas.com Page 29
Figure 4.1: CPU Internal Registers
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
http://resource.renesas.com Page 30
This is illustrated in Figure 4.2.
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.
http://resource.renesas.com Page 31
This will not influence the R1.Only 16-bit results are obtained.
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 even-
numbered address first. This means that an even-numbered address is always
stored in the PC (see "Data in the memory").
http://resource.renesas.com Page 32
Conditional test in a program is performed by these four flags. Any
condition can be tested using them.
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.
http://resource.renesas.com Page 33
In 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.
http://resource.renesas.com Page 34
1. ( 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
R0.
8. ( F ) The Z flag in the CCR is set to zero when calculation results in
zero.
Since this flag is named "Zero", it is set to 1 when calculation results in
zero.
9. ( T ) The N flag in the CCR is set to zero when the calculation results are
positive.
Since this flag is named "Negative", it is set to 1 when the calculation
results are negative. Zero when positive.
10. ( F )The C flag in the CCR is set to zero when calculation results in a
carry.
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.
http://resource.renesas.com Page 35
4.2 Instruction Configuration
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 general-purpose register, from a general-
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.
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.
http://resource.renesas.com Page 36
Samples
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,
changing the CCR.
Conditional branch instruction
BRA instruction
The BRA (BRanch Always) instruction is called "unconditional branch
instruction". Executing this instruction results in branching to the specified
address. Branching is similar to "jumping". It causes jumping forward or
backward, skipping some instructions.
The destination address is specified by giving it a name ("symbol")
Sample
BRA ABC Unconditionally branches to the symbol ABC.
Instruction
ABC: Instruction
BGT instruction
The BGT (Branch Greater Than) instruction is one type of conditional
branch instruction. It compares data as a "signed 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.
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
http://resource.renesas.com Page 37
4.3 Adder Program
This, however, simply stores the addition results in the R1L and they
are not written in the H'2002 address of the memory.
MOV.B R1L, @H'2002
Use the above instruction to write the addition results in the H'2002
address of the memory.
MOV.B @H'2000,R1L
MOV.B @H'2001,R1L
ADD.B R0L,R1L
MOV.B R1L,@H'2002
http://resource.renesas.com Page 38
After 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:
MOV.B @H'2000,R1L
MOV.B @H'2001,R1L
ADD.B R0L,R1L
MOV.B R1L,@H'2002
ABC: BRA ABC
http://resource.renesas.com Page 39
This source program is converted into machine instructions by the
assembler as follows:
Address Machine instruction Instruction
.CPU 300HA
.SECTION PROG,CODE,LOCATE=H'1000
H'001000 6A082000 MOV.B @H'2000,R0L
H'001004 6A092001 MOV.B @H'2001,R1L
H'001008 0889 ADD.B R0L,R1L
H'00100A 6A892002 MOV.B R1L,@H'2002
H'00100E 40FE ABC: BRA ABC
.END
http://resource.renesas.com Page 40
4.3.2 Rules on Source Programs
Configuration of an instruction
An instruction is configured as follows:
Note that some instructions have only one operand (destination operand) or
none at all.
http://resource.renesas.com Page 41
Samples:
MOV.B R0L,R1L Good sample
mov.b r0l,r1l Good sample
Mov.b @h'1000,R1h Good sample
MOV.B R0L,R1L Bad sample (no space or tab at the beginning)
MOV.BR0L,R1L Bad sample (instruction and operand are not
separated by one or more spaces or tabs)
http://resource.renesas.com Page 43
This method is useful when numeric values and addresses are fixed and
will not be changed. List 4.3 shows a program rewritten with this method.
The .RES control instruction is used to reserve an area for writing in the
RAM.A RAM address is generally specified not by the .EQU control
instruction but by a combination of .RES and .SECTION control instructions.
This is because the .RES control instruction has the following benefits:
The beginning address can be freely changed using .SECTION
Data areas can be easily inserted or deleted
http://resource.renesas.com Page 44
DATA2: .RES.W 1
ANSWER: .RES.B 1
If the RAM starts from the H'2000 address as with the program
described earlier, write as follows to reserve an area for writing there:
.SECTION WORK,DATA,LOCATE=H'2000
DATA1: .RES.B 1
DATA2: .RES.B 1
ANSWER: .RES.B 1
http://resource.renesas.com Page 45
List 4.4: Program Using .RES
http://resource.renesas.com Page 46
DATA2: .DATA.W 1000
ANSWER: .DATA.B 10
In the above example, since the WORK section is located at the H'1100
address, DATA1, DATA2 and ANSWER represent the H'1100, H'1104 and
H'1106 addresses respectively.
If the ROM is also located at the H'1100 address, and if "10" and "100"
to be added should be provided separately, write as follows to prepare a
separate section for storing the addition results in:
.SECTION ROM_DATA,DATA,LOCATE=H'1100
DATA1: .DATA.B 10
DATA2: .DATA.B 100
.SECTION RAM_DATA,DATA,LOCATE=H'2000
ANSWER: .RES.B 1
The above makes DATA1 represent the H'1100 address including "10"
("H'0A" in hexadecimal notation), DATA2 represent the H'1101 address
including "100" ("H'64" in hexadecimal notation) and ANSWER represent the
H'2000 address.
http://resource.renesas.com Page 47
List 4.5: Program Using .DATA
http://resource.renesas.com Page 48
result of subtraction, two data blocks are not equal. If C is 0, no borrow has
occurred. These two conditions are satisfied simultaneously when:
R0L > R1L
The BHI instruction performs branching when the data on the right
(R0L) is greater than that on the left (R1L) based on comparison by the CMP
instruction.
http://resource.renesas.com Page 49
List 4.6: Program to Test Collating Sequence
http://resource.renesas.com Page 50
1. After an ADD instruction is executed, each flag in the CCR (N, Z, V and
C) changes to reflect the results.
Answer how N, Z, V and C change after the following addition assuming
that R0L = H'80 and R1L = H'80 before the instruction is executed.
And what are the contents of the R0L? Use hexadecimal notation for them
and 0 or 1 for N, Z, V and C.
Answer
R0L = H'00, N = 0, Z = 1, V = 1, C = 1
The results are R0L = H'00 (Z = 1)
A carry occurs (C = 1)
The most significant bit of the R0L is 0 and positive (N = 0)
Since Negative + Negative = Positive, an overflow occurs (V = 1)
Answer
R0L = H'81, N = 1, Z = 0, V = 1, C = 0
The results are R0L = H'81, not zero (Z = 0)
No carry occurs (C = 0)
The most significant bit of the R0L is 1 and negative (N = 1)
Since Positive + Positive = Negative, an overflow occurs (V = 1)
2. After a SUB instruction is executed, each flag in the CCR (N, Z, V and
C) changes to reflect the results.
Answer how N, Z, V and C change after the following subtraction
assuming that R0L = H'70 and R1L = H'80 before the instruction is
executed.
And what are the contents of the R0L? Use hexadecimal notation for them
and 0 or 1 for N, Z, V and C.
Answer
R0L = H'F0, N = 1, Z = 0, V = 1, C = 1
The results are R0L = H'F0 and not zero (Z = 0)
A carry occurs (C = 1)
http://resource.renesas.com Page 51
The most significant bit of the R0L is negative (N = 1)
Since Negative - Positive = Negative, an overflow occurs (V = 1)
Answer
R0L = H'5F, N = 0, Z = 0, V = 0, C = 0
The results are R0L = H'5F, not zero (Z = 0)
No carry occurs (C = 0)
The most significant bit of the R0L is 0 and positive (N = 0)
Since Positive - Positive = Positive, no overflow occurs (V = 0)
http://resource.renesas.com Page 52
Chapter 5
H8/300H Instructions and Addressing Mode
5.1 Instructions
Instruction types
This section lists instructions provided in the H8/300H CPU. Not only
instructions but all descriptions in this chapter are common to the entire
H8/300H series including the H8/3048.
- Arithmetic instructions
Instruction Meaning Description Sample program
ADD ADD binary Binary addition
SUB SUBtract binary Binary subtraction -
ADDX ADD with eXtend carry Binary addition with a carry -
SUBX SUBtract with eXtend carry Binary subtraction with a carry -
INC INCrement Increment
DEC DECrement Decrement
http://resource.renesas.com Page 53
ADDS ADD with Sign extension Binary address data addition
SUBS SUBtract with Sign extension Binary address data subtraction -
DAA Decimal Adjust Add Decimal adjustment (addition) -
DAS Decimal Adjust Subtract Decimal adjustment (subtraction) -
MULXU MULtiply eXtend as Unsigned Unsigned multiplication
MULXS MULtiply eXtend as Signed Signed multiplication
DIVXU DIVide eXtend as Unsigned Unsigned division
DIVXS DIVide eXtend as Signed Signed division
CMP CoMPare Comparison
NEG NEGate Sign change -
EXTS EXTend as Signed Signed extension -
EXTU EXTend as Unsigned Unsigned extension
- Logical instructions
Instruction Meaning Description Sample program
AND AND logical Logical product
OR inclusive OR logical Logical sum
XOR eXclusive OR logical Exclusive logical sum
NOT NOT (logical complement) Logical negation -
http://resource.renesas.com Page 54
BSR Branch to SubRoutine Branches to a subroutine -
RTS ReTurn from Subroutine Returns from a
subroutine
- Shift/Rotate instructions
Instruction Meaning Description Sample program
SHAL SHift Arithmetic Left Arithmetic left shift -
SHAR SHift Arithmetic Right Arithmetic right shift -
SHLL SHift Logical Left Logical left shift -
SHLR SHift Logical Right Logical right shift -
ROTL ROTate Left Left rotation -
ROTR ROTate Right Right rotation -
ROTXL ROTate with eXtend carry Left Left rotation with a carry -
ROTXR ROTate with eXtend carry Right Right rotation with a carry -
http://resource.renesas.com Page 55
STC STore from Control register Stores data from the CCR -
ANDC AND Control register Logical product with the CCR -
ORC inclusive OR Control register Logical sum with the CCR -
XORC eXclusive OR Control register Exclusive logical sum with the -
CCR
NOP No OPeration No operation -
http://resource.renesas.com Page 56
address, the contents are incremented
after instruction execution.
Addressing for handling the contents of
the memory.
Predecrement Although the contents of a general-
@-ERn
register indirect purpose register is used as a memory
address, the contents are decremented
before instruction execution.
Addressing for branching by storing the
Memory indirect @@aa destination address in the memory and —
specifying it.
Program counter Addressing for specifying a branch
Symbol
relative destination address.
* n: General register number xx: Numeric value
aa: Address disp: Displacement
http://resource.renesas.com Page 57
CODE --- Code section
DATA --- Data section
Samples
.SECTION SCT1,CODE,LOCATE=H'1000 --- (1)
MOV.W R0,R1
MOV.W R2,R3
RTS
.SECTION SCT2,DATA,LOCATE=H'2000 --- (2)
ABC: .RES.W 1
(1) SCT1 specifies a code section and H'1000 as the start
address.
(2) SCT2 specifies a data section and H'2000 as the start
address.
Sets an address.
Samples
.SECTION SCT1,DATA,LOCATE=H'0000
DATA1: .DATA.W H'1234
.ORG H'0020 ------------- (1)
DATA2: .DATA.W H'5678
.ORG
.ORG H'0040 ------------- (2)
DATA3: .DATA.W H'ABCD
(1) Sets an address to H'0020.
DATA2 is allocated to the H'0020 address.
(2) Sets an address to H'0040.
DATA3 is allocated to the H'0040 address.
2.Setting of
.EQU Sets a value for a symbol.
symbol value
Reserves integer data based on the specified size.
Following ".", B (Byte), W (Word) or L (Long word) can be
specified as the size of the data to be set. Unless specified, "W" is
.DATA assumed.
Put integer data to be set in the operand. You can specify multiple
3.Setting of
integer data blocks using "," as a separator.
data
The symbol represents the start address of the reserved data.
Reserves string data.
Specify the string to be reserved in the operand. Characters must be
.SDATA
enclosed with "". You can specify up to 255 characters.
The symbol represents the start address of the reserved string data.
Reserves an integer data area based on the specified size.
Following ".", B (Byte), W (Word) or L (Long word) can be
4.Reservation
specified as the size of the area to be reserved. Unless specified,
of .RES
"W" is assumed.
data area
Put the count of areas to be reserved in the operand.
The symbol represents the start address of the reserved area.
5.Specification Specifies the target CPU for the source program to be assembled.
.CPU
of CPU The H8/300H CPU types are as follows:
http://resource.renesas.com Page 58
300HA:20 ------ Operates in a 1Mbyte memory space
300HA:24 ------ Operates in a 16Mbyte memory space
(":24" may be omitted)
Indicates the end of a source program.
Put this instruction in the end of a source program. If part of a
6. Other .END
source program continues following this instruction, it is ignored
and not assembled.
This section describes how to read the instruction table detailing each
instruction.
The instructions described in 5.1 have specific data size and addressing
mode they can execute, and how each flag in the CCR changes after execution
differs. These detailed instruction specifications are described in the instruction
table.
The following shows how to read it.
http://resource.renesas.com Page 59
If the data size is byte as shown above, the execution state count is 2 for
all of the three. This means that there is no difference in speed. If the data size
is long word (32 bit), however, the counts change as follows:
Execution state count
(1) MOV.L #0,ER0 6
(2) SUB.L ER0,ER0 2
(3) XOR.L ER0,ER0 4
From the above, you can see that SUB.L ER0,ER0 has the shortest
execution time.
When the same process can be achieved with several methods as shown
above, you should check their execution state counts (and instruction lengths)
to select the one with the smallest count.
Answers
(1) The MOV instruction cannot transfer immediate data directly to the
memory.
(2) Post-increment register indirect can only be specified for the source
operand.
(3) Predecrement register indirect can only be specified for the destination
operand.
(4) Immediate data can only be specified for the source operand.
(5) The specified data size is wrong. Since the R0 and R1 are 16-bit general-
purpose registers, the data size must be "W".
(6) Correct.
(7) A decimal number of "500" cannot be handled when the size is byte (8
bits).
(8) The ADD instruction cannot directly calculate data in the memory.
2. After the following instruction is executed, by how many will the value
of the ER0 be decremented from that before execution.
MOV.B R1H,@-ER0
http://resource.renesas.com Page 60
Answer: 1
One is subtracted when the size of the data to be transferred is byte.
3. All of the following instructions clear the ER0 to zero. Which has the
shortest length?
(1) MOV.L #0,ER0
(2) SUB.L ER0,ER0
(3) XOR.L ER0,ER0
Answer: (2)
(1) is 6-byte long. (2) is 2-byte long. (3) is 4-byte long.
So, (2) SUB.L ER0,ER0 is the shortest.
http://resource.renesas.com Page 61
Chapter 6
Sample Programming in an Assembly Language
This chapter introduces some sample programs so that you can actually
develop programs using various instructions.
Programs can be developed in several ways and there is no single right
answer. During development, you will have many questions such as "Can the
same be achieved by another method?" and "What will happen by doing this?".
Rather than just worrying, go on and develop the program in mind and execute
it using the simulator. If the results are the same as those obtained by the
sample program, your program is also the right one. Developing a different
program by modifying a sample program is another effective way of learning.
This chapter will help you understand how various instructions and
addressing modes described in Chapter 5 work for program development.
http://resource.renesas.com Page 62
The CMP instruction can compare 8-, 16- and 32-bit data. The comparison
targets, however, must be general-purpose registers, or immediate and a
general-purpose register. Before executing the CMP instruction, store data in a
general-purpose register.
http://resource.renesas.com Page 63
The conditional branch instruction can also be used independent of the
CMP instruction. To branch using the conditional branch instruction only, the
status of each flag in the CCR changed by the previous instruction determines
whether to branch or not.
The table below shows the conditions for the conditional branch instruction:
http://resource.renesas.com Page 64
Table 6.2: Conditions for Conditional Branch Instruction
http://resource.renesas.com Page 65
http://resource.renesas.com Page 66
6.2 Programs Containing a Loop
Repetitive (looping) processing in assembly language is achieved using
the conditional branch instruction. This section introduces programs containing
loops.
http://resource.renesas.com Page 67
It is usually recommended that the loop counter be decremented for
repetitive processing. This is because if the loop counter is incremented as
shown in sample program 1, the CMP instruction is required to judge whether
to repeat the processing or not.
On the other hand, if the counter is decremented, the CMP instruction is
not required since the end of repetition can be determined based on whether the
counter is zero or not. This is because Z in the CCR becomes 1 for this type of
instruction when the loop counter becomes 0 (otherwise, Z is 0).
This enables an instruction "to repeat processing if Z in the CCR is 0".
In this case, you can use the BNE instruction to branch if Z in the CCR is 0.
http://resource.renesas.com Page 68
You can also stop looping by judging that the address of the memory
has reached a specific value.
http://resource.renesas.com Page 69
6.3 Subroutines
If the same collection (function) of instructions is executed several
times in a program, writing the function every time makes the program difficult
to understand. In addition, it makes the program larger since it also increases
the instruction count.
http://resource.renesas.com Page 70
Moving execution to a separated function is referred to as "calling a
subroutine (subroutine call)" and subroutine call instructions (BSR and JSR
instructions) are used for this purpose. They are written as follows:
http://resource.renesas.com Page 71
Let's consider how the RTS instruction returns execution to the source.
If one subroutine is called from different places, execution is returned to
the respective places by the RTS instruction.
Before introducing the principle of subroutine operation, introduction of
the stack is indispensable.
http://resource.renesas.com Page 72
Instruction to return from subroutine (RTS instruction)
The RTS instruction at the end of a subroutine writes the address stored in the stack by the
subroutine call instruction to the PC. This enables the instruction next to the subroutine call
instruction to be executed next to the RTS instruction. In other words, processing is returned to
the source of the subroutine.
http://resource.renesas.com Page 73
1. You want to branch if R0 is less than R1 after the following instruction.
Which branch instruction do you use? It is assumed, however, that
unsigned data are stored in R0 and R1.
CMP.W R0,R1
Answer: BHI
The destination operand plays the main role in comparison using the CMP
instruction.
In this question, R1 is used as the destination operand.
Assuming that R1 is greater than R0, you should use BHI.
http://resource.renesas.com Page 74
Answer: 1
Since 0 is stored in the ER0 register as a result of subtraction, Z in the CCR is
1.
BNE LOOP
In the R1L register, "9" is entered as the counter to repeat the loop nine times.
The counter is decremented by 1 per processing, which is repeated until the
counter becomes zero. The BNE (branch if not zero) instruction is used to loop
unless zero.
MOV.B R0L,@RESULT
Since the minimum data is stored in the R0L register after being compared 9
times, it is written to the RESULT address.
http://resource.renesas.com Page 75
JSR @MIN
is used to jump to it.
BLE RETURN
This subroutine compares the contents of the R0H and R0L registers assuming
them to be signed and puts the smaller in the R0L register.
If the contents of R0L are equal to or smaller than as a result of comparison,
processing is returned from the subroutine without any operation. As a
conditional test instruction, use the one assuming data to be signed.
RTS
Instruction to return from a subroutine.
DATA.B 99,0,-5,39,-2,68,-16,5,20
Defines 8-bit data. DATA.B is mainly used to represent data in the ROM.
http://resource.renesas.com Page 76
6.5 Post-increment Register Indirect/Predecrement Register
Indirect
http://resource.renesas.com Page 77
Predecrement register indirect
Predecrement register indirect is used to store values sequentially in
consecutive areas in the memory.
To be more specific, it stores "the last address of the target memory + 1" in a
general-purpose register, decrements the value in the register by the byte count
of the data to be handled and writes information to the address.
http://resource.renesas.com Page 78
6.6 Multiplication and Division Programs
Multiplication instructions
There are two types of multiplication instructions: one for unsigned
values (MULXU instruction) and the other for signed values (MULXS
instruction).
http://resource.renesas.com Page 79
Division instructions
There are also two types of division instructions: one for unsigned
values (DIVXU instruction) and the other for signed values (DIVXS
instruction).
For the division instructions, the results are not guaranteed in the following
cases:
When the quotient exceeds 1 byte for byte-size division (overflow)
When the quotient exceeds 2 bytes for word-size division (overflow)
When divided by zero
http://resource.renesas.com Page 80
6.7 Logical Operation
Although a specific 1 bit among data can also be handled using the
logical instruction described earlier, its target is general-purpose registers only.
On the contrary, the bit handling instruction can handle not only data stored in
a general-purpose register but also directly handle the contents of the memory
between H'FFFF00 and H'FFFFFF addresses using absolute address
addressing.
http://resource.renesas.com Page 81
1. Point out the errors in the following instructions. (Some instructions,
however, may not include errors.)
Instruction
(1) DIVXU.B #H'15,R0
(2) MOV.B @(#H'10,R0L),R0H
(3) BSET #1,@H'FFFFF0
(4) BSET #1,@H'200000
(5) BTST #7,R0L
(6) BTST #8,R0L
(7) BTST.W #3,R0
Answers
(1) Only a general-purpose register can be specified for the DIVXU
instruction operand.
(2) Register indirect with displacement can specify 32-bit general-
purpose registers only.
(3) Correct.
(4) Although the bit handling instruction can specify H'FFFF00 to
H'FFFFFF using an absolute address, other addresses must be specified by
register indirect.
(5) Correct.
(6) Available bit numbers are between 0 and 7.
(7) The bit handling instruction can handle byte data only.
(2) How can you rewrite only the least significant 2 bits in 1-byte data to 1?
Answer:
Execute OR with #B'00000011
OR operation is used to set specific bits to 1 since it results in 1 if either of the
two is 1.
Use the BSET instruction to set only one bit to 1.
(3) How can you invert only the least significant 2 bits in 1-byte data?
Answer:
Execute XOR with #B'00000011
http://resource.renesas.com Page 82
EOR with 0 results in no change but with 1 in inversion. Utilizing this function,
you can use EOR operation with 1 to invert specific bits. Use the BNOT
instruction to invert one bit only.
(4) How can you check the statuses of the least significant 2 bits in 1-byte
data and ignore the rest?
Answer:
Execute AND with #B'00000011
After setting untargeted bits to 0 using AND operation, you can judge whether
the target bits are zero or not, or compare them with specific bit patterns using
the CMP instruction.
(5) How can you check the status of only the least significant bit in R0L
and ignore the rest?
Answer:
BTST #0,R0L
When the target is one bit only, the BNE or BEQ instruction is used following
the BTST instruction to judge whether it is zero or not.
Answer
The count of bits set to 1 in 1-byte data at the T_DATA address
The key instruction is:
BTST R0H,R4L
on the fourth line. It tests 1 bit in R4L using R0H as the bit number. It changes
R0H from 7 to 0 and checks whether 8 bits in R4L are 1 or 0 sequentially from
the most to least significant bits. Since R0L is incremented by 1 per bit having
a value of "1", the count of 1s in the T_DATA address is stored in R0L.
http://resource.renesas.com Page 83
Chapter 7
Exception Handling
Note:
Some IC signal names have bars over them to indicate negative logic.
In the contents of the CD-ROM, however, no bar can be placed on them since
they are written in HTML format. Still, bars are added to negative logic signals
in figures included as images.
The following are negative logic signals:
http://resource.renesas.com Page 84
Figure 7.1: Exception Handling Operation Overview
7.2 Reset
7.2.1 Reset Sources and Operation
Reset is generated for two purposes. One is to start execution from a
particular program after turning the microcomputer on. This program is called
the "main routine". The other is to stop any program being executed by the
CPU and resume processing from the same state when the power is turned on
again. Figure 7.3 shows reset sources:
http://resource.renesas.com Page 85
Figure 7.3: Reset Sources
The CPU shows the operation described in Figure 7.4 after reset is generated.
http://resource.renesas.com Page 86
After reset is generated, the CPU stops any program being executed,
loads the reset vector to the PC and fetches an instruction from this address to
execute it. In other words, reset generation always causes the main routine
stored in the address indicated by the reset vector to be executed. After reset is
generated to start main routine execution, processing cannot be returned to the
original program to continue. Reset generation means that the microcomputer
starts processing from the initial state.
When the microcomputer starts operation, users are required to set the
program to be executed first, namely the start address of the main routine, as
the reset vector in the lower three addresses of the four-address area starting
from address 0. This area is referred to as the "reset vector address".
Although you can generate reset any time, there is one time that you
must generate it. That is, when the microcomputer is turned on.
Simply turning it on will not set all registers in the CPU to specific values and
even the value in the PC cannot be determined. This means that the CPU
cannot determine from which address the program should be executed when
the microcomputer is turned on.
Therefore, reset must be generated prior to any other processing after
power-on. This is called "power-on reset".
Figure 7.5: Overview of Reset Pin External Circuit and Voltage Waveform
http://resource.renesas.com Page 87
Precautions on storing the reset vector
For a program to operate properly after power-on reset, the reset vector
must be stored in the reset vector addresses before the microcomputer is turned
on. This means that the reset vector addresses must be in the ROM whose
contents remain even after power-off. You must store the reset vector in this
ROM.
As shown in Figure 7.6, use the DATA.L assembler control instruction
to store the reset vector. In this example, the main routine (symbol MAIN) is
placed starting from the H'001000 address and the value of H'001000 indicated
by the MAIN symbol is stored as the reset vector in the 4-address area between
the H'000000 and H'000003 addresses.
7.3 Interrupts
7.3.1 Interrupt Overview
http://resource.renesas.com Page 88
Figure 7.7: Interrupt Operation
http://resource.renesas.com Page 89
Figure 7.8: External Interrupt Request Input Pins
The H8/3048 has seven pins to input external interrupt requests in all
and six of them are named "IRQ0" to "IRQ5". The remaining pin is named
"NMI".
In the case of external interrupts, a request is regarded to be generated
when the low-level voltage is applied to one of these interrupt request input
pins. To prevent an external interrupt request from being generated, apply the
high-level voltage to these pins.
For example, if the low-level voltage is applied to the IRQ3 input pin,
an external interrupt request of the "IRQ3" type is regarded to be generated. If
the interrupt is enabled at this time, the interrupt is generated to move
execution to the IRQ3 interrupt handling routine.
Accordingly, you can execute a program to handle requests only when
required by creating an external circuit to apply the low-level voltage to a
corresponding interrupt request input pin when a sensor has prepared data to be
read by the microcomputer.
Disabling/Enabling Interrupts
As indicated by the above description saying "If the interrupt is enabled
at this time", interrupt requests, irrespective of whether they are external or
internal, are not necessarily accepted by the microcomputer. The following
describes the interrupt mask bit in the CCR, which controls whether to accept
interrupt requests or not.
http://resource.renesas.com Page 90
Whether to enable or disable interrupts is controlled by the most
significant bit in the CCR (I bit), or the interrupt mask bit. This I bit can be set
to 1 or 0 using an LDC instruction or others.
When the I bit is 1, interrupts are masked, or disabled, and interrupt operation
is not performed even if an interrupt request is generated. When the I bit is 0,
interrupts are enabled and interrupt operation is performed if an interrupt
request is generated.
This I bit collectively disables or enables all external and internal
interrupts excluding the NMI. The NMI is an acronym for non-maskable
interrupt, an interrupt which cannot be disabled even if the I bit is set to 1.
In addition, interrupts other than the NMI can be individually enabled
or disabled by the respective bits. They are described in the next section.
7.3.3 IRQs
(1) Electrical characteristics of IRQs
http://resource.renesas.com Page 91
(2) Internal I/O registers of IRQs
Next, you will learn about internal I/O registers of IRQs. Internal I/O
registers collectively refer to registers for using various internal peripheral
functions. Each register is placed at a specific address in the memory and given
a specific name.
This lesson contains many descriptions of various internal I/O registers.
In the microcomputer, external interrupt operation is controlled by the internal
peripheral function named "interrupt controller". Two internal I/O registers for
using this interrupt controller are described below. First, the IRQ enable
register (IER) is described, together with how to interpret the explanatory
diagram.
Using bits 5 to 0 of the IRQ enable register (IER), you can enable or
disable six IRQ interrupts individually. To enable the IRQ0 interrupt, for
example, you should set the least significant IRQ0 enable bit (IRQ0E) to 1. As
a matter of course, you should set the I bit in the CCR to 0 as well.
Since all bits of this register are set to 0 by default, all IRQ interrupts
are disabled without modification after resetting. To allow a specific IRQ
interrupt, set the relevant bit of the IER to 1 using an instruction.
http://resource.renesas.com Page 92
The other register relating to IRQ interrupts is the IRQ sense control
register (ISCR). Using bits 5 to 0 of this register, you can set the sense level to
low or the trailing edge for six IRQ interrupts individually.
These two differ as follows. When it is set to the low level, the system
assumes that interrupt requests are continuously generated while the low-level
voltage is applied to an IRQ interrupt request input pin. When it is set to the
trailing edge, on the other hand, the system assumes that an interrupt request is
generated only when the low-level voltage is first applied to an IRQ interrupt
request input pin and does not assume continuous generation even if the low-
level voltage continues to be applied. Select a setting appropriate for your
system.
http://resource.renesas.com Page 93
Figure 7.15: Storage of the PC and CCR Values in the Stack
During stacking, the first operation after interrupt generation, the 8-bit
CCR value is stored in the address obtained by decrementing the stack pointer
by 4 and the 24-bit PC value in the following three addresses. This operation is
almost the same as for subroutine call instructions (JSR and BSR instructions)
and stores the PC value at interrupt generation as the return address in the
stack.
It differs from the subroutine call instructions in that the CCR value at
interrupt generation is also stored in the stack. The reason is as follows: since
the I bit in the CCR is set to 1 (interrupt disabled) after interrupt generation, the
I bit that has been 0 during original program execution is stored in the stack to
restore the CCR from the stack when returning to the original program and
resetting the I bit to 0 (interrupt enabled).
During loading the vector to the PC, the second operation after interrupt
generation, the vector method is employed as with reset operation.
In the case of interrupts, the start address of each interrupt handling routine is
also referred to as the interrupt vector and this 24-bit interrupt vector is stored
http://resource.renesas.com Page 94
in the lower three addresses of the four interrupt vector addresses in the
memory, which are determined by the interrupt type.
For example, the NMI interrupt vector addresses are the four starting
from H'00001C and the IRQ0 interrupt vector addresses are from H'000030.
This mechanism enables execution to be moved to different interrupt handling
routines according to the type of interrupt generated. For the vector address of
each interrupt, refer to Table 7.1 in the following section.
As the last operation after interrupt generation, the CCR sets the I bit in
the CCR to 1 to disable (mask) interrupts. This means that after an interrupt is
generated to move execution to an interrupt handling routine, any other
generated interrupt will be accepted. Another interrupt is accepted only after
the current interrupt routine is completed and the RTE instruction, the last
instruction in an interrupt routine, is executed.
The NMI interrupt, however, is always accepted irrespective of the I bit in the
CCR.
Figure 7.17: Restoration of the CCR and PC Values from the Stack
You must specify the RTE instruction at the end of every interrupt
handling routine and its execution enables processing to return to the original
program to continue. The RTE instruction works almost the same as the RTS
instruction, an instruction to return from a subroutine. The difference is that the
RTE also restores the CCR value stored in the stack.
Although the RTE and RTS instructions have similar names and operations,
they are completely different and misuse of them will cause a program to
http://resource.renesas.com Page 95
malfunction. Remember to put the RTE instruction at the end of an interrupt
handling routine and the RTS instruction at the end of a subroutine.
Also note that if an interrupt handling routine arbitrarily uses a general-
purpose register, the value in the register changes when execution is returned to
the original program, causing the normal process to fail. To prevent this from
happening, develop a program so as to start processing in an interrupt handling
routine after storing the general-purpose register to be used in the stack and
restore the register from the stack before returning to the original program
using the RTE instruction. The PUSH instruction is used to store a general-
purpose register in the stack and the POP instruction to restore it. Use these
instructions properly to store and restore general-purpose registers.
The vector addresses are located in the area between H'000000 (start
address of the memory space) and H'0000F3. This address area is called the
"vector area". In this area, only data called "vectors" that represent the start
address of each interrupt handling routine can be placed and no other data or
program is allowed. The addresses marked "system reservation" in the table
refer to free areas, which have no definite vectors to be placed.
As described earlier, be sure to generate reset when turning the
microcomputer on. Reset loads the reset vector stored in the reset vector
addresses (between H'000000 and H'000003) within the vector area to the PC.
In the reset vector addresses, the defined reset vector must have been stored
before power-on. This means that the reset vector addresses must be in the
ROM in which the reset vector is written.
Whether to set other exception handling vector addresses in the ROM
or RAM depends on the system. When using the microcomputer as an internal
controller, however, other exception handling vector addresses are generally
located in the same ROM as the reset vector addresses.
http://resource.renesas.com Page 96
Table 7.1: H8/3048 Exception Handling Sources
http://resource.renesas.com Page 97
C Language sample
#include <machine.h>; /* include embedded function */
/* Internal I/O register address define */
#define IER (*(volatile unsigned char *)0xFFFFF4)
#define ISCR (*(volatile unsigned char *)0xFFFFF5)
/* define variavle */
unsigned long counter ;
/* stack area size */
#pragma stacksize 0x100
/* Power ON Reset function */
__entry(vect=0) void main(void)
{
counter= 0 ; /* clear counter */
ISCR |= 0x01 ; /* select interrupt signal falling edge */
IER |= 0x01 ; /* enable IRQ0 terminal */
set_ccr(0) ; /* clear interrupt mask */
while(1) ; /* endless loop */
}
/* IRQ0 function */
// vector 12 IRQ0
__interrupt(vect=12) void INT_IRQ0(void)
{
counter++ ;
}
http://resource.renesas.com Page 98
1. What happens if an enabled interrupt request is generated while the
CPU is executing a program?
(A) The current instruction is interrupted for exception handling.
(B) Exception handling is performed after the current instruction is
completed.
(C) Exception handling is performed after the program is completed.
Answer: (B)
(A) disables the original program to properly continue even after execution is
returned from the interrupt handling routine to the next instruction of the
original program.
(C) refers to the situation where interrupts are disabled. If (C) is true, no
interrupt can be generated for a program containing a loop since it is never
completed.
Answer: (C)
The H8/3048 has both reset and interrupt exception handling routines.
Answer: (A)
(B) Exception handling sets the I bit to 1.
(C) Interrupts do not initialize the internal I/O registers.
Answer: (C)
(A) refers to the vector address for reset only.
(B) refers to the vector.
http://resource.renesas.com Page 99
(D) refers to the stack.
Answer: (B)
You must generate reset to input the start address of a particular program in
the PC.
(A) does not determine to which address execution is returned by the RTE
instruction at the end of the NMI interrupt handling routine.
As for (C), a program to initialize the SP cannot be started without generating
reset.
Answer: (C)
The reset IC is used to apply the low-level voltage to the reset pin for a certain
period of time after power-on.
Answer: (A)
(B) refers to the vector address for reset and (C) to that for IRQ1.
As for (D), no data or programs except vectors should be stored in the vector
area.
8. By how many does the value in the SP (ER7) change immediately before
and after an interrupt is generated?
(A) +2
(B) +4
(C) -2
(D) -4
<Interrupts>
Write a program to use an external interrupt as you have learned in
Chapter 7 and run it on the training board. Work out through the following
steps:
• Complete the exercise source program by filling out its blanks.
• Make sure that the program runs successfully on the training board.
• If the program will not run as specified in the exercise, consult the
sample answer and make necessary changes to it before rerunning it.
Turning on jumper J6 on the training board will connect SW28 and the
CPU IRQ5 pin to each other. Press SW28 to input a low-level voltage to the
IRQ5 pin; release it to input a high-level voltage to the IRQ5 pin.
Eight LEDs are connected by way of the 74LV574 placed at address
H'FFFF12. The following correspondence exists between the value written to
each bit of address H'FFFF12 and the resulting on/off status of the LED:
0 ... LED on
1 ... LED off
All eight LEDs come initially off. Each time you press SW28, the eight LEDs
increment as an eight-digit binary incremental counter. Once all the eight have
lit up, they go out to continue incrementing further again.
• Configure the program to raise one IRQ5 interrupt each time you press
SW28. To this end, IRQ5 must be configured so it will be sensed at the
trailing edge. Remember also to enable the IRQ5 interrupt.
• The IRQ5 interrupt handler increments the value at the COUNT address
in internal RAM by one.
• Allowing for the correspondence between the value of 0 or 1 written to
address H'FFFF12 and the resulting on/off status of the LED, it is
necessary to create the one's complement of the value that is
incremented by one and write it to address H'FFFF12.
Input function
"1" can be read from I/O port registers when the "High" voltage is
applied to I/O port pins or "0" when "Low". Based on the values read from I/O
port registers, the on/off statuses of the switches connected to pins or the
high/low status of the comparator output can be determined.
Output function
The "Low" voltage is output from I/O port pins when the values written
in I/O port registers are "0" or "High" when "1". Based on the values written in
I/O port registers, the LEDs connected to pins or relays can be turned on and
off.
The H8/3048 has ports 1 to B as I/O ports available for parallel data
input/output. Port 7 is for input only.
These ports have data direction registers (DDRs) and data registers
(DRs) as shown in Table 8.1.
The H8/3048 employs the memory-mapped I/O method for data
input/output. This method has no dedicated data input/output instructions such
as "data input instructions" or "data output instructions" and instructions for
writing/reading to/from the memory are also used for data input/output. For
data input/output functions (various registers), addresses are allocated as with
the memory. In the case of the H8/3048, the H'FFFF10 to H'FFFFFF addresses
are used for data input/output functions (various registers).
Accordingly, the MOV and bit handling instructions can be used as
they are and reading/writing from/to addresses between H'FFFF10 and
H'FFFFFF are regarded as being data input/output. In I/O ports, data are output
from pins by writing them in the DR of each port and the statuses of pins can
be input by reading data from the DR.
Depending on the DDR setting, the I/O port pins can be used for either
input or output. Write "1" in the DDR of the target bit to use the corresponding
pin for output or "0" for input.
Since the DDR is a write-only register, the setting cannot be read even
if the MOV instruction is used. If an instruction for reading is executed by
mistake, "1" is always read irrespective of the setting.
Figure 8.4 shows an example to set port A to output to control LEDs on/off.
In a pin set to output by the DDR, the voltage varies with the contents
of the DR. When LEDs are turned on using port A as shown in Figure 8.4,
write "1" in the corresponding bit of the PADDR to set the corresponding pin
of port A to output. To turn the LEDs on, write "0" in the corresponding bit of
Port A for turning LEDs on and off using this circuit is initialized as follows:
PADDR: .EQU H'FFFFD1 ; Defines the PADDR address
PADR: .EQU H'FFFFD3 ; Defines the PADR address
MOV.B #B'00000011,R0L ; Prepares a setting in R0L
MOV.B R0L,@PADR ; Sets the PA1 and PA0 default output values (off)
in PADR
MOV.B R0L,@PADDR ; Writes "1" in lower 2 bits of PADDR and sets
PA1 and PA0 to output pins
Write "1" in the lower 2 bits of the PADR and then set the lower 2 pins
of port A to output. This turns both LED1 and LED2 off by default.
To turn only LED1 on/off, use the following instructions:
C Language Sample
PADDR: .EQU H'FFFFD1 ; Defines the PADDR address
PADR: .EQU H'FFFFD3 ; Defines the PADR address
MOV.B #B'00000011,R0L ; Prepares a setting in R0L
MOV.B R0L,@PADR ; Sets the PA1 and PA0 default output values (off) in PADR
MOV.B R0L,@PADDR ; Writes "1" in lower 2 bits of PADDR and
sets PA1 and PA0 to output pins
PADR = 0x03 ;
PADDR = 0x03 ;
Figure 8.5 shows an example for setting port B to input to judge the
switch on/off status.
As for the pins set to input by the DDR and port 7, the statuses can be
read by reading the DR. To read the status of the switch connected to port B as
shown in Figure 8.5, write "0" in the corresponding bit of the PBDDR and set
the corresponding pin of port B to input to read the PBDR. At this time, "0" is
read from the PBDR if the switch is turned on, or "1" if turned off. If nothing is
done after resetting, the DDR is set to "0", or input, by default. The switch
status can be checked only by reading the DR.
Port B for judging the switch on/off status using this circuit is
initialized as follows: In the following example, unused pins and all 8 pins of
port B are set to input.
To check both SW1 and SW2 statuses and call the OUTPUT subroutine
after they are turned on, use the following instructions (the contents of the
OUTPUT subroutine is not shown below):
As with the sample output for port A, it is recommended that the bit
handling instruction be used to judge a single switch status or a combination of
MOV and logical instructions to judge several switches simultaneously.
Answer: (B)
(A) Data of several bits cannot simultaneously be input or output using one
signal line.
(C) This is the function of an interrupt input pin.
Answer: (C)
Since the H8/3048 employs the memory-mapped I/O method, the DR of an I/O
port is located at a specific address in the memory.
Answer: (A)
(B) and (C) are observed when a pin is set to output. An input pin is in High-Z
state.
Answer: H'FFFFC6
Refer to "Table 8.1: I/O Port Register Map" in 8-2. Although you need not
remember the address, remember which table you should refer to.
Answer: H'FFFFD1
Refer to "Table 8.1: I/O Port Register Map" in 8-2. Although you need not
remember the address, remember which table you should refer to.
All eight LEDs come initially off. Each time you press SW12, a software timer
causes the eight LEDs to increment as an eight-digit binary incremental
counter at regular intervals of time. Once all the eight have lit up, they go out
to continue incrementing further again.
• Because processing does not advance further until you press SW12
once, allow the program to continue looping as long as SW12 is tested
off, and let it exit the loop when SW12 is tested on.
• Program the software timer as a subroutine to gain time. Write a
program that loads a general-purpose register with a given value at the
start of the subroutine and then loops to decrement that value by one
until the result of decrementing equals 0, when the program should exit
the loop and leave the subroutine. The loop count determines how
much time the subroutine gains. Too short time gained would make
changes in the display status of the LEDs look faster beyond visual
recognition.
• As the main routine loops, the value at the COUNT address in internal
RAM is incremented by one after each interval of time allowed by the
software timer.
• Allowing for the correspondence between the value of 0 or 1 written to
address H'FFFF12 and the resulting on/off status of the LED, it is
necessary to create the one's complement of the value that is
incremented by one and write it to address H'FFFF12.
Although parallel data described in the previous chapter are used for
inputting and outputting digital voltages, this chapter explains the A/D
converter used for inputting analog voltages. A/D conversion is used for
inputting from temperature sensors.
In this chapter, you should fully understand the methods for checking
and clearing the status flag (ADF) when A/D conversion has been completed or
in other cases since they are frequently used.
1. You want to convert the voltage externally input into 9-bit (512-step)
digital numeric values using the A/D converter. What do you do?
(A) Since the resolution of the A/D converter is fixed at 10 bits, 9-bit
conversion results cannot be obtained.
(B) Use the ADCSR to change the resolution setting from the default of 10
bits to 9 bits.
(C) Use the upper 9 bits from the 10-bit A/D conversion results.
2. How many methods can you use to start conversion by the A/D
converter?
(A) Only one method to externally input a signal.
(B) Only one method to use an instruction.
(C) Two methods to externally input a signal and use an instruction.
Answer: (C)
The H8/3048 is capable of starting conversion with two methods.
3. Where are the A/D conversion results of analog voltages input to the
AN3 pin stored?
Answer: ADDRD
Refer to "Figure 9.1: A/D Converter Block Diagram" in 9.1 to understand the
relationship between the analog input pin and the ADDR.
4. How is the ADF changed after the first A/D conversion regarding the
selected input channel is completed?
Answer: B'011
Understand how the ADCSR is used by referring to "Figure 9.2: A/D Control
Status Register (ADCSR)" in 9.2.
<A/D converters>
Write a program to use an A/D converter as you have learned in
Chapter 9 and run it on the training board. Work out through the following
steps:
• Complete the exercise source program by filling out its blanks.
• Make sure that the program runs successfully on the training board.
Convert the analog voltage input to the AN4 pin from analog to digital at a
134-state conversion speed in scan mode and display the conversion result as
an 8-bit binary value on the eight LEDs. As you turn the trimmer to vary the
analog input voltage, the LED display should vary accordingly.
• Configure the A/D converter for scan mode, 134-state, and AN4
selection before starting the conversion process.
• The program should loop by waiting until each run of A/D conversion
completes, or ADF equals 1, and reading the conversion result for
output on the LEDs.
• Write the display data resulting from the conversion process to address
H'FFFF12. Allowing for the correspondence between the value of 0 or
1 written to address H'FFFF12 and the resulting on/off status of the
LED, it is necessary to invert the conversion result and write it to
address H'FFFF12.
There are two 8-bit D/A data registers (DADR0 and DADR1). When
analog output is enabled, data in the D/A data register are D/A converted for
output from the analog output pin.
1. You want to convert 10-bit digital numeric values into voltages in 1024
steps using the D/A converter. What do you do?
(A) Since the resolution of the D/A converter is fixed at 8 bits, they cannot
be converted into voltages in 1024 steps.
(B) Use the ADCR to change the resolution setting from the default of 8
bits to 10 bits.
(C) Multiply the 8-bit D/A conversion results by 1.25 (10/8).
Answer: (A)
(B) This is not available in the H8/3048.
(C) The D/A conversion results remain in 8-bit units (256 steps) even after this
multiplication.
2. How many methods can you use to start conversion by the D/A
converter?
(A) Only one method to externally input a signal.
(B) Only one method to use an instruction.
(C) Two methods to externally input a signal and use an instruction.
Answer: (B)
In the case of the H8/3048, conversion can only be started using an instruction.
3. From which pin are the voltages obtained by D/A converting numeric
data in the DADR1 output?
4. You want to D/A convert both channels 0 and 1. How do you set the
upper 3 bits of the DACR?
5. How is the DACR changed after the first D/A conversion of the specified
channel is completed?
<D/A converters>
Write a program to use a D/A converter as you have learned in Chapter
10 and run it on the training board. Work out through the following steps:
• Complete the exercise source program by filling out its blanks.
• Make sure that the program runs successfully on the training board.
• If the program will not run as specified in the exercise, consult the
sample answer and make necessary changes to it before rerunning it.
The training board houses a circuit in which an operational amplifier
connected to the DA0 pin turns on an LED, so that the D/A converter output
voltage can control the brightness of the LED.
The higher the output voltage, the brighter the LED glows; the lower
the output voltage, the dimmer the LED becomes. A voltage of approximately
2 V is required to turn on the LED. The LED will stay out under an output
voltage of 0 to 2 V. Changes in the LED brightness would be more easily
identifiable visually by changing the output voltage from 1 V to 5 V.
Note
The following register has negative logic bit names:
As such, the serial port is mostly used for input/output with more
distant parties, for which connection using a large number of signal lines is
unsuitable. Data input/output using the serial port is often referred to as
"communication", with input being referred to as "reception" and output
"transmission".
Serial data communication is conducted via the RS-232C and USB
(Universal Serial Bus) ports, used to connect PCs and peripherals, as well as
data communication using Ethernet or telephone lines.
The H8/3048 has two SCI channels which can be set to either start-stop
synchronization or clock synchronization. Although the figure below shows a
block diagram of only one SCI channel, both have the same configuration and
use.
For reception, the data input in the receive pin (RxD) are stored in the
receive shift register (RSR) bit by bit through some modification such as
removing the start or stop bit. After that, the SCI moves the data from the RSR
to the received data register (RDR) when the stop bit arrives and the CPU reads
them using the MOV instruction.
All of the upper 5 bits of this register serve as the status flag. If you are
not sure about the use, review how to use each flag. The lower 3 bits are not
described here since they are not used so often.
TDRE The transmit data register empty (TDRE) flag is used for transmitting data. Refer to
Figure 11.7 for the transmission procedure.
Before writing the data to be transmitted in the transmit data register (TDR), make sure
that the TDRE is set to 1. If it is still set to 0, you should not write data in the TDR yet.
If the TDRE is set to 1, write the data to be transmitted in the TDR, then be sure to clear
the TDRE to 0 using the BCLR instruction or by other means.
When the TDRE is cleared to 0, the SCI starts transmission. It automatically sets the
TDRE to 1 after completely moving the data from the TDR to the TSR. This is why you
should write data in the TDR after the TDRE has been set to 1.
Figure 11.7 shows the transmission and reception procedures described above
in the form of flowcharts.
Receive error
The SSR has three error flags. Although no transmission errors occur
since data are unilaterally sent, errors sometimes occur during reception since
the receiver may fail to receive what the sender has transmitted. The three
types of errors all occur during reception.
Now that descriptions of the SCI registers are completed, let's proceed to some
sample uses of the SCI.
As for the hardware, the H8/3048 signal level (5V, 0V) must be
changed to the RS-232C level (±12V). For this purpose, an RS-232C line
driver/receiver IC as shown in Figure 11.10 is used.
Although an RS-232C device also has modem control signals in
addition to TxD and RxD pins, it is assumed that these signals be not used
since they are not supported by the H8/3048.
Now that the register default values are determined, the following
explains a sample program to initialize the registers.
This program is designed as a subroutine to be called from the main
routine. Since reception will fail if the reception-related status flags have been
set before each register is initialized or reception is started, clear the flags after
dummy-reading the SSR. They are not cleared unless zero is written after
reading. Dummy read refers to reading conducted only for satisfying this
condition even though flag values need not be checked.
If B'00000000 is written at this time, the TDRE is also cleared,
mistakenly transmitting data stored in the TDR, if any. To prevent this, write
C Language Sample
#include "iodefine.h"
void initSCI(void) ;
void initSCI(void)
{
int i ;
C Language Sample
#include <machine.h>
#include "iodefine.h" /* include I/O address define
header file */
/* stack area size */
#pragma stacksize 0x100
void initSCI(void) ;
/* Power ON Reset function */
__entry(vect=0) void main(void)
{
initSCI() ;
PA.DDR = 0x01 ; /* PA0 output */
set_ccr(0) ; /* enable interrupt */
while(1) ; /* endless loop */
}
void initSCI(void)
{
int i ;
SCI0.SCR.BYTE = 0 ; /* stop SCI0,use internal clock */
SCI0.SMR.BYTE = 0 ; /* asynchronouse mode,8 bit
data,1stop,no-parity,1/1clock */
SCI0.BRR = 64 ; /**/
for(i=0;i<350;i++) ;
SCI0.SSR.BYTE &= 0x80 ; /* clear receive flags */
SCI0.SCR.BYTE = 0x70 ; /* enable receive
interrupt,transmit and receive enable */
}
// vector 52 ERI0
__interrupt(vect=52) void INT_ERI0(void)
{
SCI0.SSR.BYTE &= 0xc0 ; /* clear receive error flags */
}
// vector 53 RXI0
__interrupt(vect=53) void INT_RXI0(void)
{
SCI0.SSR.BIT.RDRF = 0 ; /* clear RDRF , stop interrupt
request */
PA.DR.BIT.B0 = ~PA.DR.BIT.B0 ; /* invert PA0 terminal */
}
Answer
It takes (longer) time for inputting/outputting the same bit count of data.
It uses (fewer) signal lines.
Refer to the comparison between the serial and I/O ports described in 11.1
Answer
The sender and receiver must use the same (data format) and (communication
speed).
Otherwise, an error occurs on reception and communication fails.
Answer
Check that the (TDRE) flag in the (SSR) is set to 1, and if not, wait until it
becomes 1. If it is set to 1, write the data to be transmitted in the (TDR) and
then set the (TDRE) flag to (0).
Refer to "Figure 11.7: Data Transmission/Reception Flowcharts" in 11.3.3.
Answer
It occurs when the next data is received with the (RDRF) flag in the (SSR) set
to (1).
Listen to the explanation provided in "Figure 11.8: Receive Error" in 11.3.3.
5. What are the three factors which determine the SCI communication
speed?
Answer
(CKS1 and CKS0 (clock select bits) in the SMR)
(Value in the BRR)
(Crystal oscillating frequency )
Refer to 11.3.2.
3
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
• Since ASCII code "A" is represented by H'41, "B" by H'42, and "C" by
H'5A, transmit from H'41 to H'5A in sequence to print characters A
through Z.
Interval timer
Specific interrupt handling can be triggered at a certain interval. A
timer used in this way is specifically referred to as an "interval timer".
The ITU has five channels from 0 to 4. Each channel can be used as an
independent timer since it is equipped with registers, pulse output/input pins
and external clock input pins corresponding to the timer counter, compare
register and input capture register. You can also interlock multiple channels.
Each channel has almost the same configuration. Figure 12.2 shows the
channel 0 block diagram.
Since each channel has almost the same structure and use, let's take
channel 0 as an example. Table 12.1 shows registers relating to channel 0. The
"0" suffix represents registers dedicated for channel 0 and other channels also
have the same types of registers. The timer counter 0 (TCNT0) is a 16-bit, up
counter. There are two general registers 0, A and B (GRA0 and GRB0), which
are available as either a compare or input capture register. Since these three 16-
bit registers are located at even-numbered addresses in the memory, they are
read and written in word-size units.
Table 12.1: ITU Channel 0 Register Configuration
The timer prescaler bits (TPSC2 to TPSC0) are used to select the clock
to be counted. The internal clock can be selected from the system clock φ and
the clocks having periods that are twice, four times and eight times as long, and
the external clock from four external clock input pins from TCLKA to
TCLKD.
The clock edge bits (CKEG1 and CKEG0) are used to select the clock
edge for counting up when an external clock is selected. If you set to count a
clock at both edges, the count is incremented by 2 per clock input.
The counter clear bits (CCLR1 and CCLR0) are used to set the timing
for clearing the TCNT0 count value. If they are set at 00, the TCNT0 is not
cleared but continues counting up permanently. When the count value reaches
H'FFFF, however, it automatically returns to H'0000. If they are set at 01 or 10,
the TCNT0 is cleared when a compare match or input capture is generated by
the GRA0 or GRB0. This setting is convenient for generating compare matches
at a certain interval, in other words, for enabling the interval timer, since a
value once input to the GRA0 or GRB0 need not be rewritten every time a
compare match is generated.
Note the following precaution if you set to clear the TCNT0 when a
compare match is generated. The TCNT0 count value is not cleared
immediately after it reaches the GRA0 or GRB0 value. Instead, it is cleared
when the next clock is input or when the counter is counted up next after both
values match. Setting of "11" represents synchronous clearing. This lesson,
however, omits descriptions of complicated functions such as interlocking of
multiple ITU channels.
The I/O control bits A2 to A0 (IOA2 to IOA0) are used to set the uses
of the GRA0 and TIOCA0 pins, and the I/O control bits B2 to B0 (IOB2 to
IOB0) to set the uses of the GRB0 and TIOCB0 pins. Since they are set in the
same way, the following explains how to set the lower 3 bits only.
If they are set at 000, the GRA0 is used as an output compare register,
which, however, does not output pulses. This setting is used for the interval
timer. If they are set at 001, the low-level voltage is output by the TIOCA0 pin
when a compare match occurs. If they are set at 010, the high-level voltage is
output. If they are set at 011, toggle is output by the TIOCA0 pin when a
compare match occurs. Toggle output refers to outputting by inverting the
current high/low level.
If they are set at 100, the GRA0 is used as an input capture register and
input is captured when the leading edge is input to the TIOCA0 pin. If they are
set at 101, input is captured when the trailing edge is input to the TIOCA0 pin.
Settings 110 and 111 are the same and input is captured at both leading and
trailing edges.
- TCR0 setting
For ITU operation, the internal clock φ (phi)/8 is used to set the GRA0
to 25ms and the TCNT0 is used for counting. A compare match occurs every
25ms to clear the TCNT0 and start next counting operation.
To enable this, the TCR0 is set as follows. Setting the φ/8 clock enables 25ms
setting using 16 bits of the GRA.
- GRA0 setting
If the φ/8 clock source is selected when the CPU clock is set at 20MHz,
one count is equal to 0.5 microsecond. Since 25ms (25000 microseconds) are
equal to 62500 counts, set GRA0 to 62499 (62500 - 1).
The reason why one is subtracted is that it takes one-clock time after a
compare match occurs until the TCNT0 is cleared.
- TIOR0 setting
Since output pins are not used, the TIOR0 is set as follows:
http://resource.renesas.com Page 153
- TIER0 setting
As for interrupts, only the IMIA0 is used.
- TSTR setting
Start channel 0 after all settings are completed.
C Language Sample
#include <machine.h>
#include "iodefine.h" /* include I/O address define
header file */
/* stack area size */
#pragma stacksize 0x100
void initITU(void) ;
/* Power ON Reset function */
__entry(vect=0) void main(void)
{
initITU() ;
PA.DDR = 0x01 ; /* PA0 output */
set_ccr(0) ; /* enable interrupt */
while(1) ; /* endless loop */
}
void initITU(void)
{
ITU0.TCR.BYTE = 0x23 ; /* counter clear GRA,1/8 clock */
ITU0.TIER.BIT.IMIEA = 1 ; /* enable IMIA interrupt */
ITU0.GRA = 62500-1 ; /* 20MHz/40Hz(25ms)/8 = 62500 */
ITU.TSTR.BIT.STR0 = 1 ; /* start ITU0 */
}
// vector 24 IMIA0
__interrupt(vect=24) void INT_IMIA0(void)
{
ITU0.TSR.BIT.IMFA = 0 ; /* clear IMFA , stop interrupt
request */
PA.DR.BIT.B0 = ~PA.DR.BIT.B0 ; /* inverse PA0 terminal */
}
Answer
To trigger specific interrupt handling at a certain interval. ¼(Interval timer)
function
To output the high/low-level voltage from an external pin after a certain period
of time.¼(Output compare) function
To capture the timer counter value when the leading/trailing edge is input in an
external pin to another register. ¼(Input capture) function
Refer to 12.1 to understand the general timer overview.
2. The following describes the compare match function of the ITU. Enter
an appropriate word in parentheses.
Answer
In ITU channel 0, a compare match is generated by the GRA0 when the time
obtained by multiplying the (period) of pulses counted by the (TCNT0) by the
value in the GRA0 has elapsed.
Refer to 12.1 and 12.2 to understand how a compare match is generated.
Answer
The (IMIB0) interrupt can be generated.
The (high/low-level voltage) can be output from the (TIOCB0) external pin.
Listen to the explanation provided in "Figure 12.2: ITU Channel 0 Block
Diagram" in 12.2 to understand how channel 0 generates two compare
matches, A and B.
4. You want to generate an interrupt using the GRA0 of ITU channel 0 but
without outputting pulses when a compare match occurs. How do you set
the TIOR0 and TIER0?
Answer
Write (000) to the 3 bits from (I/O control bits A2 to A0 (bit 2 to bit 0)) of the
TIOR0.
Write (1) to the (IMIEA) bit of the TIER0.
Refer to 12.3.2 and 12.3.4 to understand how the TIOR0 and TIER0 are used.
6. What happens if the TSTR of the ITU is set to stop counting by the
timer counter? Select the one you think is appropriate in parentheses.
Answer
A compare match (does not occur) when the set time has elapsed.
An interrupt is (not generated) when the set time has elapsed (even) if enabled.
Pulses are (not output) when the set time has elapsed (even) if enabled.
Refer to 12.3 to understand what happens when counting by the timer counter
is stopped.
<Timer 1>
Write a program to use ITU as you have learned in Chapter 12 and run
it on the training board. Work out through the following steps:
• Complete the exercise source program by filling out its blanks.
• Make sure that the program runs successfully on the training board.
• If the program will not run as specified in the exercise, consult the
sample answer and make necessary changes to it before rerunning it.
Use timer channel 0 to create a time interval of 1 second and let the 8-bit
LEDs at address H'FFFF12 increment at 1-second intervals.
Timer channel 0 must be a 20 ms interval timer.
• Use the internal clock divided by eight as a timer counter clock. (The
CPU clock is 20 MHz.)
• A compare match with GRA0 clears the timer counter.
• Use an interrupt that is raised by a compare match with GRA0.
• The LEDs increment each time a 20 ms interrupt routine has run 50
times.
(Initialization)
• TCR0: Configure to clear the timer counter on compare match with
GRA0 and to use the internal clock divided by eight.
• No interrupt is used.
Although the H8/3048 has an internal ROM and RAM, external memory
expansion may be required due to insufficient capacity. The following
describes how to connect the memory and CPU and match their speeds using
EPROM and SRAM as examples.
When expanding the H8/3048 memory, you can determine how much
memory capacity to be added and how to use the data bus. The mode set pins
(MD0, MD1 and MD2) are used to select one of seven operating modes, which
determine the uses of the address bus, data bus and read/write signals.
Table 13.1 shows each mode:
The internal ROM and RAM are always connected to the CPU through
the 16-bit data bus irrespective of the mode setting. "Disabled" in the "Internal
ROM" column means that the internal ROM, though it exists, is disabled by
being disconnected from the CPU. In this case, an external ROM must be
connected. When the internal ROM and RAM are enabled, no external memory
can be added to the same address. If the same address is used, the internal
memory has priority, disabling reading/writing from/to the externally
connected memory.
Table 13.2 shows the pins relating to memory connection provided for
the H8/3048 and their functions.
Address strobe
Indicates that an address is valid and that it is external when it is at low
level. It is not set at low level if an address is internal (internal ROM or RAM).
When the CPU is reading or writing data from/to the internal ROM or RAM,
reading/writing is not available externally. The RD, HWR and LWR signals
are not changed to low level, either.
Read/write signals
When the data bus width is 8 bits (RD and HWR are used):
Data buses D8 to D15 are used, and D0 to D7 are not.
The RD signal is used as the read signal for both 8- and 16-bit widths,
the HWR signal is used for writing to an even-numbered address, and the LWR
signal is used for writing to an odd-numbered address. During 16-bit data
writing, both the HWR and LWR signals are output. For details, refer to the
section describing connection between the CPU and memory.
Figure 13.1 shows the read timing from an external memory. This is an
example for 24-bit address and 16-bit data buses. Reading is completed in a 3-
system clock time (3-state access).
To read data or instructions from the memory, the setup time and hold time of
the read data must be satisfied.
Since the H8/3048 has an internal wait controller, waits can be inserted
in various ways.
By default, the wait controller is enabled for all areas and three states
are set to be inserted in programmable wait mode. In other words, three wait
states are forcibly inserted in all areas. Change the setting as soon as possible
after resetting if necessary.
In read mode, the CPU reads data from the EPROM and sets both CE
and OE at low level.
In output disable mode, the memory is selected but read data is not
output to an I/O pin. The I/O pin is set in high impedance mode (disconnected
state).
In standby mode, the memory is not selected. When CE is set at high
level, the system is set in standby mode irrespective of the OE setting. Since
the memory IC is not operating in this mode, power consumption is low.
Table 13.3 shows the HN27C4001G read timing. There are 100ns and
120ns types.
There are 19 address pins, using 512 kbytes, and 8 data pins, using 8
bits per address.
In read mode, the CPU reads data from the SRAM and sets both CS and
OE at low level.
In output disable mode, the memory is selected but read data is not
output to an I/O pin. The I/O pin is set in high impedance mode (disconnected
state).
Write operation takes two forms. When CS is set at low to set WE at
low, OE is capable of writing at either high or low. Writing with OE set at high
is called the "OE clock" mode since it is set at low for reading and high for
writing. Writing with OE set at low, on the other hand, is called the "OE low
fixed" mode since it is set at low for both reading and writing. Writing is
generally conducted in OE clock mode.
In non-selection mode, the memory is not selected. When CS is set at
high, the system is set in non-selection mode irrespective of the OE or WE
setting. Since the memory IC is not operating in this mode, power consumption
is low.
Table 13.5 shows the HM628512BI read cycle. There are 70ns and 80ns types.
Table 13.5: HM628512BI Read Cycle
First, let's consider how to connect a CPU to a memory using an 8-bit data bus.
Address assignment
What should be determined first is to which addresses the memory is to
be connected.
As described in "Exception Handling", a program operating based on
resetting and the reset vector must be located in a ROM. Since the reset vector
is between H'000000 and H'00003, a ROM must be connected to starting from
the H'000000 address if only one ROM is connected.
In internal ROM enable mode, the internal ROM addresses are from
H'000000 to H'01FFFF. In internal ROM disable mode, the HN27C4001G
Accordingly, you can determine that it is the EPROM address when the
upper 5 bits (A23 to A19) of the address are all 0. On the other hand, you can
determine that the address of the address bus is valid when AS is set at low
level. As a result, CE of the EPROM is set at low level under the following
conditions:
AS is at low level and A23 to A19 are also at low level
Figure 13.12 shows a sample circuit to satisfy them.
The most important parameters in CPU read timings are read data setup
and hold times.
Data are output from the memory only when all the access time, OE
output delay time and CE output delay time requirements are satisfied.
Address delay time + Access time = 30ns + 100ns (max.)
= 130ns (max.) ...............
Refer to Figure 13.18
Time until the address strobe is output + Decoder delay time + CE output delay
time
= 50ns + 30ns (max.) + 10.5ns (max.) + 100ns (max.)
= 190.5ns (max.) ............... Refer to Figure 13.20
The longest time for data to be output is 190.5ns due to the CE output
delay time. As for the setup time, it causes no problem since the calculation
results are 190.5ns (max.) against the requirement of 240ns (max.), providing
an allowance of about 50ns.
This method has a wide range of uses and can be applied to various
connections between the CPU and the memory. The setup time required by the
CPU, however, may not be satisfied if the address decoder delay time is long.
In addition, the address strobe (AS) is used in the address decode circuit and
delayed AS tightens the requirement for the setup time.
To simplify a decode circuit, the H8/3048 is provided with signals from
CS0 to CS7. These signals are obtained by decoding the upper 3 bits of the
CPU addresses. Since 3 bits are decoded, the 16Mbyte memory space is
divided into 8 equal parts. These are called area 0 to area 7. When the CPU
accesses area 0, CS0 is set at low level. This is shown in Table 13.9.
Table 13.9: Address Decoding Using Chip Select Signals
In the case of the H8/3048, the CS0 to CS7 signals are available instead
of the AS signals. In this case, 3 bits of addresses need not be decoded. In
addition, the CS0 to CS7 signals are output faster than the AS signals and at the
same timing as address signals, allowing for setup time and enabling slower
memory to be connected.
In order to use the CS0 to CS7 signals, you must set the bus controller
accordingly.
Since CS0 to CS3 are commonly used with port 8, set the
corresponding bit of the P8DDR at 1 (output).
CS4 to CS7, on the other hand, can be used as chip select signals
simply by setting the chip select control register.
CS7E, CS6E, CS5E and CS4E correspond to CS7, CS6, CS5 and CS4,
respectively, and are switched to chip select pins when 1 is written.
When connecting a CPU and a memory using a 16-bit data bus, use a
pair of memories having 8 bits per address, or use a memory having 16 data
pins. The example described here uses a pair of memories having 8 bits per
address, which are connected as memories at even- and odd-numbered
addresses, respectively.
To read/write 16-bit data, two addresses must be accessed
simultaneously. It is impossible, however, to output two addresses to the
address bus simultaneously. Remember that the upper digits of 16-bit data in
the memory must be stored in an even-numbered address and the lower in an
odd-numbered address (even-numbered address + 1). An "even-numbered
address" and an "even-numbered address + 1" are the same from A23 to A1 in
binary notation and only A0 differs. No problem arises if A0 is not used, since
A1 to A23 are the same. A0 = 0 represents an even number and A0 = 1 an odd
number. A combination of an "odd-numbered address" and an "odd-numbered
address + 1 (even-numbered address)" does not make A1 to A23 the same,
disabling access as 16-bit data. This situation is not limited to the H8/3048 but
common to all 16-bit microcomputers.
HWR and LWR are used instead of the A0 signal. HWR = L represents
8-bit writing to an even-numbered address (A0 = 0) and LWR = L means that
to an odd-numbered address (A0 = 1).
HWR = LWR = L means writing to both even- and odd-numbered
addresses. Only the RD signal is used for reading by the CPU. During reading,
data are always handled as 16 bits without distinguishing between 8- and 16-bit
data. Sixteen bits are read even if the MOV.B instruction is used for reading
from the memory. Non-used 8 bits are not read into the CPU although they are
output from the memory. Reading data not required for the memory will not
have a negative effect. For writing, however, 8- and 16-bit writing must be
clearly distinguished since it changes the contents of the memory. This is
shown in Table 13.10.
Table 13.10: Reading/Writing on 16-bit Data Bus
Figure 13.27: Connection Between CPU and SRAMs (16-bit Data Bus)
The capacity becomes 1Mbyte since two SRAMs are connected and the
addresses are between H'100000 and H'1FFFFF since CS0 = 0 and A20 = 1.
Address, CS and OE signals are commonly input to two memories. For
the memory at an even-numbered address, HWR is input to the WE pin and the
data pins are connected to the upper 8 bits of the CPU. As for the memory at an
odd-numbered address, on the other hand, LWR is input to the WE pin and the
data pins are connected to the lower 8 bits of the CPU.
Four buffers are configured into two pairs and each output pin is
controlled by the OE1 or OE2 signal, which is transferred from input to output
when the OE1 or OE2 pin is set at low. When the OE1 or OE2 pin is set at
high, the output pins are set in the high-impedance state.
Since the OE1 and OE2 pins are used as 8-bit input-only pins, it is
assumed here that they are short-circuited to serve as one OE pin. Eight input
pins are connected to external signals and eight output pins to the CPU data
bus. Input data is output to the data bus when OE is set at low and the
following two conditions are satisfied at the same time:
• The CPU outputs the address of this input-only port
When using this IC for output ports, connect D0 to D7 to the data bus
and use O0 to O7 as signals to be externally output. When the OE pin is set at
high, output enters the high-impedance state. The OE pin, however, is fixed at
low here since the output pins need not be set to high impedance.
When LE is changed from high to low, the output data at that time is
maintained.
LE is changed to high when the following two conditions are satisfied:
• The CPU outputs the address of this output-only port
• The HWR signal of the CPU is set at low and in write state
Accordingly, the LE signal is created based on address-decoded and
HRW signals. Unlike the memory, the LE signal is regarded to have both CS
and WE functions. Figure 13.32 shows connection outline to the CPU.
Address decoder
Both input and output ports have a capacity of only one address when
considered as memory. If you attempt to fully decode them so that they only
have one address, all of the 21 address buses must be decoded, making an
extremely complicated circuit. Here, we use CS7 only and omit the address
decoder. As a result, addresses between H'E00000 and H'FFFFFF of area 7 are
all used for expanded I/O ports excluding those for the internal RAM and I/O.
They are used as input ports for reading and output ports for writing. As for
parts that have overlapping addresses, the internal RAM or I/O port has
priority, disabling the CS7, RD and HWR signals to be output.
Addresses between H'FFFF10 and H'FFFF1B on the boundary between
the internal RAM and I/O ports are assumed to be used here. The 8-bit absolute
addressing instruction is available for these addresses. Figure 13.33 shows an
expanded I/O circuit.
Since the CS7 pin is set to input at resetting, pull it up to the power supply.
Since it has 10 address pins, it has a capacity of 1024 addresses (1k) from 0 to
1023.
Eight bits are used for one address since there are 8 data pins.
The memory IC starts operation when CE (Chip Enable) is set at low level.
Reading is enabled when OE (Output Enable) is set at low level.
The memory IC starts operation when CS (Chip Select) is set at low level.
Writing is enabled when WE (Write Enable) is set at low level.
By decoding the upper 8 bits of addresses, the entire memory space can be
divided into 8 equal parts.
Signals correspond to addresses as follows:
CS0: Addresses whose upper 3 bits are 000 (H'000000 to H'1FFFFF)
CS1: Addresses whose upper 3 bits are 001 (H'200000 to H'3FFFFF)
......
CS7: Addresses whose upper 3 bits are 111 (H'E00000 to H'FFFFFF)