SH66XX Programming Guide V10
SH66XX Programming Guide V10
2/126
Ver1.0
Ver1.0
Ver1.0
ADCM ................................................................................................................................................... 13
ADD ...................................................................................................................................................... 11
ADDM ................................................................................................................................................... 11
ADI ........................................................................................................................................................ 13
ADIM ..................................................................................................................................................... 14
AND ...................................................................................................................................................... 20
ANDIM .................................................................................................................................................. 21
ANDM ................................................................................................................................................... 20
BA0 ....................................................................................................................................................... 26
BA1 ....................................................................................................................................................... 27
BA2 ....................................................................................................................................................... 27
BA3 ....................................................................................................................................................... 28
BC ......................................................................................................................................................... 28
CALL..................................................................................................................................................... 29
DAA....................................................................................................................................................... 14
DAS....................................................................................................................................................... 19
EOR ...................................................................................................................................................... 23
EORIM.................................................................................................................................................. 24
EORM................................................................................................................................................... 24
HALT..................................................................................................................................................... 31
JMP....................................................................................................................................................... 25
LDA ....................................................................................................................................................... 10
LDI........................................................................................................................................................... 9
NOP ...................................................................................................................................................... 32
OR......................................................................................................................................................... 22
ORIM .................................................................................................................................................... 23
ORM...................................................................................................................................................... 22
RTNI...................................................................................................................................................... 30
RTNW................................................................................................................................................... 29
SBC....................................................................................................................................................... 17
SBCM ................................................................................................................................................... 17
SBI ........................................................................................................................................................ 18
SBIM ..................................................................................................................................................... 19
STA ....................................................................................................................................................... 10
STOP .................................................................................................................................................... 31
SUB....................................................................................................................................................... 15
SUBM ................................................................................................................................................... 16
5/126
Ver1.0
TJMP .................................................................................................................................................... 29
6/126
Ver1.0
Chapter One
Analysis of SH6610 Instructions
The following are SH6610 instructions, categorized and explained according to their
respective functions. When you need an instruction for a certain function, you can look up
the instruction in its function category. Youd better browse through all the instructions listed
here, because even though you cant remember all of them at once, you can still have an
impression to remind you of such instruction when needed. Some people dont even know
about some very good instructions that he can use, just because he didnt browse through all
of them. This ignorance may cause considerable waste of space on a system without a big
enough ROM, which is so regrettable. I will explain as simply as possible to improve your
learning efficiency. Of course, it is normal to understand or remember only part of the
contents after reading once. And you are certain often revert to this book because it is a
collection of instructions.
Instructions
Instruction is a series of codes that can be recognized by CPU, and then CPU will
operate according to the given instruction.
Operand
Apart from instructions to tell CPU how to operate, the operation object must be
designated. The object for CPU to operate with is called Operand. Therefore, a complete
instruction must include two items, both instruction and operand.
Of the above, items inside [ ] are used according to nature of the instruction. Some
instructions require only one operand, while some require two. Instruction and operand shall
be separated by a space, and operands shall be separated from each other by a ,.
Execution Time of Instruction
Execution time for SH6610 instructions is one instruction cycle, which is one fourth of the
system working frequency.
7/126
Ver1.0
The above are all of the instructions of SH6610 series, which add up to only 40 in
number, but never overlook them! A variety of consumer electrical products on the market
are created with them, e.g. calculator, remote controller, watch, toy, etc.
Program Counter
Accumulator
Carry Flag
Data Memory
RAM bank
Stack
Table Branch Register
Program Address
Immediate Data
Logic AND
Logic OR
Logic EOR
8/126
Ver1.0
LDI MxI
01111 iiii xxx xxxx
Not affected
AC , Mx I
Explanation
LDI is a very frequently used instruction. It loads immediate data I to accumulator and
data memory. However, due to this 4-bit system, the preset range of the immediate data I is
00H ~ 0FH(0 ~ 15), and that of Mx is 00H~7FH.
[ Example ]
LDI
20H05H
after execution:
AC=05H
Content of data memory $20H=05H
Programming Tip
There isnt any specially defined register for users in SH6610 system, but we can use its
powerful data memory as registers in our program designing. In the above example, I have
used data memory $20H for a register. However, if they are expressed only by address and
without respective names, the design of the program will be very confusing. Heres a tip for
you: you can use the pseudo-instruction EQU to define each data address.
AAA
Name of memory variable
EQU
20H
PseudoInstruction
AAA05H
9/126
Ver1.0
Instruction: STA
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
STA loads the value of accumulator to data memory. When executing this instruction,
CPU does the transmission only and the carry flag is not affected.
[ Example ] Save the value of AC in $21H
LDI
STA
:
20H05H
21H00H
:
;AC=05H$20H=05H
;$21H=05H
Programming Tip
If operand 2 is the immediate data I, then the program can be written in the following
ways:
LDI
LDI
LDI
20H,0AH
20H,10
20H,1010B
Instruction: LDA
Format:
Instruction Code:
Carry Flag:
Operation:
;expressed in hex
;expressed in decimal
;expressed in binary
Explanation
LDA loads the value of data memory to accumulator. This instruction does not affect
the carry flag.
[ Example ] Load the value of $20H to AC
:
LDI
20H,05H
;$20H=05H,AC=05H
LDI
21H,0FH
;$21H=0FH,AC=0FH
LDA
20H,0
;AC=05H
:
Programming Tip
When writing a program, we often use labels as jumping destinations in the program.
Label names can be defined by user according to the following rules:
I.
II.
10/126
Ver1.0
Format:
Instruction Code:
Carry Flag:
Operation:
;$20H=05H,AC=05H
;$21H=06H,AC=06H
;AC=05H
;AC=0BH,CY=0,$21H=06H
:
;$20H=0BH,AC=0BH
;$21H=06H,AC=06H
;AC=0BH,CY=0
;AC=01H,CY=1,$21H=06H
Explanation
Instruction ADDM adds up the values of data memory and accumulator and saves the
result in both the accumulator and the data memory. The ADDM operation affects carry flag:
when the result of ADDM exceeds 0FH, the carry flag is set to 1; otherwise the value CY is 0.
Therefore we can decide whether theres a carry by the value of the carry flag after addition.
[ Example ] 05H +06H
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,00H
ADDM 21H,0
;$20H=05H,AC=05H
;$21H=06H,AC=06H
;AC=05H
;AC=0BH,$21H=0BH,CY=0
11/126
Ver1.0
:
[ Example ] 0BH + 06H
:
LDI
20H,0BH
LDI
21H,06H
LDA
20H,00H
ADDM 21H,0
:
;$20H=0BH,AC=0BH
;$21H=06H,AC=06H
;AC=0BH,CY=0
;AC=01H,$21H=01H,CY=1
Programming Tip
When you are reading the examples, I suggest that you call them into ICE after compiling
to watch the change in each of the registers step by step.
Instruction: ADC
Format:
Instruction Code:
Carry Flag:
Operation:
Function: to add up the value of Data Memory, Carry Flay and the
value of Accumulator, and then save the result in the
Accumulator
ADC Mxbbb
00000 0bbb xxx xxxx
CY
AC Mx + AC + CY
Explanation
Instruction ADC adds up the value of data memory, carry flag and the value of
accumulator, and saves the result in the accumulator. The ADC operation affects the carry
flag: when the result of ADC exceeds 0FH, the carry flag is set to 1; otherwise the value of
CY is 0. Therefore we can decide whether theres a carry by the value of the carry flag after
addition.
[ Example ] 05H +06H , CY=1
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,00H
ADC
21H,0
:
;CY=1
;$20H=05H,AC=05H
;$21H=06H,AC=06H
;AC=05H
;AC=0CH,$21H=06H,CY=0
;CY=0
;$20H=0BH,AC=0BH
;$21H=06H,AC=06H
;AC=0BH
;AC=01H,$21H=06H,CY=1
Programming Tip
When carry is not considered, youd better use ADD rather than ADC, in order to avoid
extra uncertainty due to the addition of CY value (because CY can be either 1 or 0).
12/126
Ver1.0
Instruction: ADCM
Format:
Instruction Code:
Carry Flag:
Operation:
Function: to add up the value of Data Memory, Carry Flag and the
value of Accumulator, and then save the result in both
the Accumulator and the Data Memory
ADCM Mxbbb
00000 1bbb xxx xxxx
CY
AC , Mx Mx + AC + CY
Explanation
Instruction ADCM adds up the value of data memory, carry flag and the value of
accumulator, and saves the result in both the accumulator and the data memory. The
ADCM operation affects the carry flag: when the result of ADCM exceeds 0FH, the carry flag
is set to1; otherwise the value of CY is 0. Therefore we can decide whether theres a carry
by the value of the carry flag after addition.
[ Example ] 05H +06H , CY=1
:
;CY=1
LDI
20H,05H
;$20H=05H,AC=05H
LDI
21H,06H
;$21H=06H,AC=06H
LDA
20H,00H
;AC=05H
ADCM
21H,0
;AC=0CH,$21H=0CH,CY=0
:
[ Example ] 0BH + 06H , CY=0
LDI
LDI
LDA
ADCM
Instruction: ADI
Format:
Instruction Code:
Carry Flag:
Operation:
:
;CY=0
20H,0BH ;$20H=0BH,AC=0BH
21H,06H
;$21H=06H,AC=06H
20H,00H
;AC=0BH,
21H,0
;AC=01H,$21H=01H,CY=1
:
Explanation
Instruction ADI adds up the value of data memory and immediate data I, and saves the
result in accumulator. The ADI operation affects carry flag: when the result of ADI exceeds
0FH, the carry flag is set to1; otherwise the value of CY is 0. Therefore we can decide
whether theres a carry by the value of the carry flag after addition.
[ Example ] $20H=05H , I=04H
:
LDI
20H,05H
ADI
20H,04H
:
[ Example ] $20H=0AH , I=07H
:
LDI
20H,0AH
ADI
20H,07H
:
;$20H=05H,AC=05H,CY=0
;AC=09H,$20H=05H,CY=0
;$20H=0AH,AC=0AH,CY=0
;AC=01H,$20H=0AH,CY=1
13/126
Ver1.0
Instruction: ADIM
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
Instruction ADIM adds up the value of data memory and immediate data I, and saves the
result in both accumulator and the data memory. The ADIM operation affects carry flag:
when the result of ADIM exceeds 0FH, the carry flag is set to1; otherwise the value of CY is 0.
Therefore we can decide by the value of the carry flag after addition whether there is a carry.
[ Example ] $20H=05H , I=04H
:
LDI
ADIM
:
[ Example ] $20H=0AH , I=07H
:
LDI
ADIM
:
Instruction: DAA
Format:
Instruction Code:
Carry Flag:
Operation:
20H,05H
20H,04H
;$20H=05H,AC=05H
;AC=09H,$20H=09H,CY=0
20H,0AH
20H,07H
;$20H=0AH,AC=0AH
;AC=01H,$20H=01H,CY=1
Explanation
Instruction DAA acts by adjusting the value of data memory to decimal after addition and
saving the result to both accumulator and the data memory. Its adjusting method is if the
value of the data memory is greater than 9 or if CY= 1, then add 6 to the data memory and
set the carry flag to 1.
[ Example ] 06H + 05H , and do DAA adjustment
:
LDI
20H,06H
LDI
21H,05H
LDA
20H,0
ADD
21H,0
DAA
21H
:
14/126
;AC=06H,$20H=06H
;AC=05H,$21H=05H
;AC=06H
;AC=0BH,CY=0
;AC=01H,$21H=01H,CY=1
Ver1.0
Instruction: SUB
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
SUB subtracts the value of accumulator from the value of data memory and saves the
result in the accumulator. When executing SUB, if the value of data memory is less than the
value of accumulator, a borrow will take place and CY will be set to 0. On the contrary, if
the value of data memory is greater than the value of accumulator, borrow will not happen
and CY will be set to 1. Therefore we can decide by the value of CY whether there is a
borrow after execution of SUB. Besides, subtraction in the system is done through addition,
i.e. when subtracting a number, it is actually adding the numbers binary complement.
[ Example ] 06H - 05H
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SUB
21H,0
:
[ Example ] 05H - 06H
:
LDI
20H,05H
LDI
21H,06H
LDA
21H,0
SUB
20H,0
:
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=1,CY=1,$21H=06H
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=06H
;AC=0FH,CY=0,$20H=05H
15/126
Ver1.0
Instruction: SUBM
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of SUBM is almost the same as SUB, it subtracts current value of
accumulator from the value of data memory and saves the result in the accumulator as well
as in the data memory. When executing SUBM, if the value of data memory is less than the
value of accumulator, a borrow will take place and CY will be set to 0. On the contrary, if
the value of data memory is greater than the value of accumulator, borrow will not happen
and CY will be 1. Therefore we can decide by the value of CY whether there is a borrow
after execution of SUBM.
[ Example ] 06H - 05H
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SUBM 21H,0
:
[ Example ] 05H - 06H
:
LDI
20H,05H
LDI
21H,06H
LDA
21H,0
SUBM 20H,0
:
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=01H,CY=1,$21H=01H
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=06H
;AC=0FH,CY=0,$20H=0FH
16/126
Ver1.0
Instruction: SBC
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of SBC is to subtract the value of accumulator from the value of
data memory, add the value of carry flag, and then save the result in the accumulator.
When executing SBC, if the value of data memory is less than the value of accumulator, a
borrow will take place and the CY will be set to 0. On the contrary, if the value of data
memory is greater than the value of accumulator, borrow will not happen and the CY will be 1.
Therefore we can decide by the value of CY whether there is a borrow after execution of
SBC.
[ Example ] CY=06 - 5=?
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SBC
21H,0
:
;CY=0
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=01H,CY=0,$21H=06H
;CY=1
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=02H,CY=0,$21H=06H
Instruction: SBCM
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of SBCM is to subtract the value of accumulator from the value of
data memory, add the value of carry flag, and then save the result in both the accumulator
and data memory. When executing SBCM, if the value of data memory is less than the
value of accumulator, a borrow will take place and the CY will be set to 0. On the contrary,
if the value of data memory is greater than the value of accumulator, borrow will not happen
and the CY will be set to 1. Therefore we can decide by the value of CY whether there is a
borrow after execution of SBCM.
17/126
Ver1.0
;CY=0
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H,CY=0
;AC=01H,CY=0,$21H=01H
;CY=1
;AC=05H,$20H=05H
;AC=06H,$21H=06H
20H,0
;AC=05H,CY=1
21H,0
;AC=02H,CY=0,$21H=02H
Instruction: SBI
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of SBI is to subtract immediate data I from the value of data
memory, and save the result in accumulator. When executing SBI, if the value of the data
memory is less than the immediate data, a borrow will take place and CY will be set to 0.
On the contrary, if the value of data memory is greater than the immediate data, borrow will
not happen and CY will be 1. Therefore we can decide by the value of CY whether there is
a borrow after execution of SBI.
[ Example ] $20H=05H , I=04H
:
LDI
20H,05H
SBI
20H,04H
:
[ Example ] $20H=02H , I=07H
:
LDI
20H,02H
SBI
20H,07H
:
;$20H=05H,AC=05H,CY=0
;AC=01H,$20H=05H,CY=1
;$20H=02H,AC=02H,CY=0
;AC=0BH,$20H=02H,CY=0
18/126
Ver1.0
Instruction: SBIM
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of SBIM is to subtract immediate data I from the value of data
memory, and save the result in both accumulator and the data memory. When executing
SBIM, if the value of data memory is less than the immediate data, a borrow will take place
and CY will be set to 0. On the contrary, if the value of data memory is greater than the
immediate data, borrow will not happen and CY will be 1. Therefore we can decide by the
value of CY whether there is a borrow after execution of SBIM.
[ Example ] $20H=05H , I=04H
:
LDI
20H,05H
SBIM
20H,04H
:
[ Example ] $20H=02H , I=07H
:
LDI
20H,02H
SBIM
20H,07H
:
Instruction: DAS
Format:
Instruction Code:
Carry Flag:
Operation:
;$20H=05H,AC=05H
;AC=01H,$20H=01H,CY=1
;$20H=02H,AC=02H
;AC=0BH,$20H=0BH,CY=0
Explanation
Instruction DAS acts by adjusting the value of data memory to decimal after
subtraction and saving the result to both accumulator and the data memory. Its adjusting
method is if the value of data memory is greater than 9 or if CY=0, then add 0AH to the data
memory and set CY to 0.
19/126
Ver1.0
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
In AND operation, the result will be 1(true) only if both of the two operands are 1(true).
Its logic table is as follows:
b
0
1
0
1
AND
0
0
0
1
However, in real instruction the logic operand has 4 bits rather than 1 bit. The
instruction AND is to AND the values of data memory with accumulator, and the result is
saved in the accumulator.
Format:
Instruction Code:
Carry Flag:
Operation:
20H,0110B
21H,0101B
20H,0
;AC=06H,$20H=06H
;AC=05H,$21H=05H
;AC=0100B,$20H=06H
Explanation
System movement of the instruction ANDM is almost the same as AND, but saving the
operation result in data memory as well as in accumulator.
20/126
Ver1.0
Format:
Instruction Code:
Carry Flag:
Operation:
;AC=0110B,$20H=0110B
;AC=0101B,$21H=0101B
;AC=0100B,$20H=0100B
Explanation
System movement of the instruction ANDIM is to change operand 2 (accumulator) of
instruction AND to immediate data I. This instruction is in immediate mode, so the address
of data memory can only be set to bank 0( $000H ~ $07FH ). The operation result is saved
in both accumulator and the data memory.
[ Example ] $20H=0110B , I=0011B
:
LDI
20H,0110B
;AC=06H,$20H=0110B
ANDIM 20H,0011B
;AC=0010B,$20H=0010B
:
Programming Tip
ANDIM itself has a special function MASK.
we can clear this bit to 0 with ANDIM like this:
20H1011B
21/126
Ver1.0
Instruction: OR
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
In OR operation, the result will be 1(true) if either one of the two operands is 1(true).
Its logic table is as follows:
Logic operation table for OR
a
0
0
1
1
b
0
1
0
1
OR
0
1
1
1
However, in real instruction the logic operand also has 4bits rather than 1 bit. The
instruction OR is to OR the values of data memory with accumulator, and the result is saved
in the accumulator.
[ Example ] 0001B | 0100B
:
LDI
20H,0001B
LDI
21H,0100B
OR
20H,0
:
Instruction: ORM
Format:
Instruction Code:
Carry Flag:
Operation:
;$20H=0001B,AC=0001B
;$21H=0100B,AC=0100B
;$20H=0001B,AC=0101B
Explanation
System movement of the instruction ORM is almost the same as OR, but saving the
operation result in data memory as well as in accumulator.
[ Example ] 0001B | 0100B
:
LDI
20H,0001B
LDI
21H,0100B
ORM
20H,0
:
;$20H=0001B,AC=0001B
;$21H=0100B,AC=0100B
;$20H=0101B,AC=0101B
22/126
Ver1.0
Programming Tip
In program designing, if a certain bit of a variable needs to be set to 1 and the other bits
must not be affected, then this can be done by the instruction OR. Because any bit that has
done OR operation with 0 can keep its original value, while those with 1 will have the value of
1.
Instruction: ORIM
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of the instruction ORIM is to do logic OR operation with the value of
data memory and Immediate Data I, and save the result in both accumulator and the data
memory. This instruction is also in immediate mode.
[ Example ] Set bit 3 of the value of $20H to 1
ORIM 20H1000B
After execution: $20H=1xxxB
Instruction: EOR
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of EOR is to do logic Exclusive OR operation with the values of data
memory and accumulator, and save the result in the accumulator. EOR is usually referred
to as Exclusive OR, because the operation result will be 1 only if the two operands have
different values; otherwise the result will be 0. The logic table for EOR is as follows:
Logic operation table for EOR
a
0
0
1
1
b
0
1
0
1
EOR
0
1
1
0
;$20H=0011B,AC=0011B
;$21H=0101B,AC=0101B
;$20H=0011B,AC=0110B
23/126
Ver1.0
Instruction: EORM
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
System movement of the instruction EORM is almost the same as EOR, i.e. doing EOR
action with the values of data memory and accumulator, but saving the operation result in the
data memory as well as in the accumulator.
Format:
Instruction Code:
Carry Flag:
Operation:
;$20H=0011B,AC=0011B
;$21H=0101B,AC=0101B
;$20H=0110B,AC=0110B
Explanation
Operand 2 of the instruction EORIM should be immediate data. This instruction is to do
logic EOR operation with the value of data memory and immediate data I, and to save the
result in both the accumulator and the data memory.
[ Example ] $20H=0011BI=0101B
:
LDI
20H,0011B
;$20H=0011B,AC=0011B
EORIM 20H,0101B
;$20H=0110B,AC=0110B
:
Programming Tip
After reading all of the logic instructions, you may wonder why there havent the NOT
instruction (inverse)? If there havent such an instruction in SH6610 series, what can I do?
Dont worry, Programming Tip is going to tell you how to use other instructions to perform the
NOT function. The operation of NOT is to change 0 1 or 1 0 on each bit. Here, the
EOR instruction can help us to get the inversed value by doing EOR operation with
immediate data (0FH) and the variable that wants to be done NOT. Youll understand
clearly after reading the following example.
24/126
Ver1.0
20H,0FH
Explanation
Instruction JMP jumps to a designated address to execute program. However,
addressing capability of SH6610 series CPU is limited to 4K words (0000H~0FFFH), so the
jumping range of JMP can only reach 4K(0FFFH). Address beyond 4K shall be reached by
switching banks. Theres detailed explanation in later chapters for how to switch banks.
The JMP instruction is similar to the GOTO instruction in BASIC program.
[ Example ] PC=40H , Jump to 0E00H
JMP
0E00H
LOOP
:
ORG 0340H
LOOP : NOP
NOP
:
Instruction: BAZ
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
If the value of AC is 0, then after executing the BAZ instruction, PC will go to the
designated address X to execute program, the range of X being from $000H to $7FFH. It
continues to execute the next line if the value of AC is 1
25/126
Ver1.0
DEC20H:
INC21H
LDI
SBIM
BAZ
JMP
ADIM
Instruction: BA0
Format:
Instruction Code:
Carry Flag:
Operation:
20H,0FH
20H,01H
INC21H
DEC20H
:
21H,01H
:
;$20H=0FH
;AC,$20H $20H -1
;ifAC=0 jump to INC21H
;else jump to DEC20H
;$21H+1
Explanation
If bit 0 of AC is 1, then after executing BA0 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H to
$0FFFH. It continues to execute the next line if bit 0 of AC is 0.
DEC20H:
LDI
20H,0FH
;$20H=0FH,AC=0FH
BIM
20H,01H
;$20H,AC$20H -1
BA0
INC21H
JMP
DEC20H
:
INC21H:
ADIM
21H,01H
;$21H,AC$21H+1
26/126
Ver1.0
Instruction: BA1
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
If bit 1 of AC is 1, then after executing BA1 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H to
$0FFFH. It continues to execute the next line if bit 1 of AC is 0.
[ Example ] if $20H(bit 1)=1 then goto INC21H
:
LDI
20H,0FH
;$20H=0FH,AC=0FH
DEC20H:
SBIM
20H,01H
;$20H,AC$20H -1
BA1
INC21H
;ifAC(bit1)=1,jump to INC21H
JMP
DEC20H
;else jump to DEC20H
:
INC21H: ADIM
21H,01H
;$21H,AC$21H+1
:
Instruction: BA2
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
If bit 2 of AC is 1, then after executing BA2 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H or
$0FFFH. It continues to execute the next line if bit 1 of AC is 0.
27/126
Ver1.0
Instruction: BA3
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
If bit 3 of AC is 1, then after executing BA3 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H to
$0FFFH. It continues to execute the next line if bit 1 of AC is 0.
Explanation
If CY is 1, then after executing instruction BC, PC will go to the designated address X to
execute program, the range of X being from $000H to $7FFH. It continues to execute the
next line if CY is not 1. The instruction BC is often used after addition or subtraction to
decide whether there is a carry or borrow. You should especially note that for addition, CY
is set to 1 when there is a carry, while for subtraction CY is set to1 when there isnt any
borrow. Therefore you should be careful when dealing with program flows.
28/126
Ver1.0
Instruction: TJMP
Format:
Instruction Code:
Carry Flag:
Operation:
by
Explanation
The destination address for Instruction TJMP is composed by PCs bit 8~bit 11, TBR
and AC value (please refer to instruction RTNW for program example).
Example: PC=300H, TBR=01H, AC=02H, then the rule for composing a destination
address is as follows:
address=PC(bit8~bit 11) TBR AC
If:
PC =300H
TBR=01H
AC=02H
Then the destination address is: 3 1 2 H
Instruction: CALL
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
Instruction CALL is used to call a subprogram. First it saves the values of CY and PC+1
to stack for returning to the calling program, then goes to the designated address X ( $0000H
~ $07FFH or $0800H ~ $0FFFH) to execute program. Instructions RTNW or RTNI can be
used to return to the calling program. When using CALL to call a subprogram, you should
especially note how many layers of stack have already been used, because SH6610 series
only provide 4-layer stacks. If more than 4 layers are used, serious error will be occurred
when returning to the calling program!
Instruction: RTNW
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
RTNW is an instruction to get data from stack to PC for returning to the calling program,
and at the same time put the value of H into TBR and the value of L into AC. This
instruction is often used to get stationary data.
29/126
Ver1.0
EQU
EQU
001A
001B
001C
001D
LDI
LDI
CALL
ORG
0300
TJMP
(PC11~PC8),TBR,AC
0301
RTNW
0302
RTNW
0303
RTNW
0304
RTNW
0305
Instruction: RTNI
Format:
Instruction Code:
Carry Flag:
Operation:
OEH
20H
:
:
TBR00H ;put index value (high nibble) 0 into TBR.
TEMP02 ;put index value (low nibble) 2 into AC
300H
;call subprogram.
:
:
:
300H
; get destination address $0302H according to
00H,01H
00H,02H
04H,05H
09H,08H
:
Explanation
Instruction RTNI is mainly used for returning from interrupt or subprogram. It fills CY
and PC with values of stack (CY and returning address) when returning. Whats the
difference between RTNI and RTNW? We can find that when returning by RTNW, only the
returning address in the stack is fetched into PC, but CY value is not fetched. And RTNW
fetches another two values (HTBR, LAC), which RTNI does not do. Therefore you can
choose from the two instructions according to your needs.
[ Example ] To exchange two numbers
000E
0020
0021
0022
1 TBR
2 REGX
3 REGY
4 TEMP
5 ;*********************
0005 780E 12 RESET :
0006 7920 13
0007 7A21 14
0008 C00A 15
17 ;**********************
000A 3820 18 SWAPXY
000B 3C22 19
000C 3821 20
000D 3C20 21
000E 3822 22
000F 3C21 23
0010 D400 24
EQU
EQU
EQU
EQU
0EH
20H
21H
22H
LDI
LDI
LDI
CALL
TEMP,00H
REGX,02H
REGY,04H
SWAPXY
LDA
STA
LDA
STA
LDA
STA
RTNI
REGX,00H
;AC=02H
TEMP,00H
;TEMP=02H
REGY,00H
;AC=04H
REGX,00H
;REGX=04H
TEMP,00H
;AC=02H
REGY,00H
;REG2=02H
;return to main program
30/126
;set TEMP=00h
;set RegX=02h
;set RegY=04h
; ;call subprogram
Ver1.0
Instruction: HALT
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
After executing instruction HALT, CPU will be halt while its surrounding circuit (counter,
oscillation circuit) continues working. The instruction HALT is usually used to stop CPU
temporarily in order to save power. In HALT mode, when any of the system interrupts
occurs, CPU will be released from HALT mode and continue to work.
[ Example ] HALT program, to enable PORT B interrupt to wake up the program
IEX
EQU
IRQ
EQU
PORTB EQU
LDI
LDI
LDI
HALT
NOP
00H
01H
09H
:
PORTB,0FH
IEX,0001B
IRQ,00H
:
Instruction: STOP
Format:
Instruction Code:
Carry Flag:
Operation:
Explanation
Executing instruction STOP will stop the whole chip from working, including oscillation
circuit. Only PORT interrupt and external interrupt can release CPU from STOP mode, so
you must enable an interrupt before entering STOP mode, otherwise the system cant be
waked up from STOP mode.
31/126
Ver1.0
00H
01H
09H
:
PORTB,0FH
IEX,0001B
IRQ,00H
:
Instruction: NOP
Format:
Instruction Code:
Carry Flag:
Operation:
Function: to do nothing
NOP
1111 1111 111 1111
Not affected
No
Explanation
Instruction NOP means doing nothing in its instruction cycle and it is often used for time
delay. Because it does nothing when executing, you dont worry if it will affect any current
status.
32/126
Ver1.0
CHAPTER TWO
System Software Application
In this chapter, we will use the SH6610 instructions explained in the first chapter to write
various application program, so that you can understand this system further more through
these application programs.
2-1
Data Transmission
The following programs mainly use indirect addressing mode to fill the data memory
(80H~85H) with desired values. In SH6610 system, the address of data memory exceeding
7FH can not be reached by immediate addressing, so the data can only be read and written
by indirect addressing mode.
33/126
Ver1.0
2-2
4/8-Bit Addition
Addition has its absolute importance in program designing and is needed everywhere.
Here lists 4-bit and 8-bit addition programs for reference.
[Example2-2.1] 4-bit addition (5h+4h)
;************************************************************************
;File name: ADD4.ASM
;Description: This program is two 4 bit number addition program,
;
then save result to RUT,(RUT=X+Y)
;Input Arguments: X , Y
;Output Arguments: RUT
;************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X
EQU
20H
;set be added "X" register
Y
EQU
21H
;set added "Y" register
RUT
EQU
22H
;set summation "RUT" register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,05H
;set X=05H
LDI
Y,04H
;set Y=04H
LDA
X,00H
ADD
Y,00H
;X+Y
STA
RUT,00H
;save (X+Y) result to RUT
END
;************************************************************************
34/126
Ver1.0
35/126
Ver1.0
2-3
4/8-Bit Subtraction
Subtraction is also used very often in program designing. 4-bit and 8-bit subtraction
examples are listed respectively in the following example. If you want to use subtraction over
8 bit, you can only accord to the example in your practical use.
[Example2-3.1] 4-bit subtraction (5H-4H)
;*************************************************************************
;File name: SUB4.ASM
;Description: This program is two 4 bit number subtraction program,then save
;
result to RUT,(RUT=X-Y)
;Input Arguments: X , Y
;Output Arguments: RUT
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
X
EQU
20H
;minuend register
Y
EQU
21H
;subtrahend register
RUT
EQU
22H
;summation register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,05H
;set minuend X = 05h
LDI
Y,04H
;set subtrahend Y = 04h
LDA
Y,00H
;get subtrahend Y
SUB
X,00H
;X-Y
STA
RUT,00H
;save the result to RUT
;*************************************************************************
36/126
Ver1.0
37/126
Ver1.0
2-4
4/8-Bit Multiplication
There isnt any instruction for multiplication in SH6610 system, so you may wonder
how to do multiplication? In fact, although theres not such an instruction, we can still
perform this function with existing instructions. Most of multiplication instructions in other
systems are composed by addition and some judging instructions, and packaged by system
designing engineers. Therefore, we provide here with 4/8-bit program examples written in
this method for your reference:
[Example2-4.1] 4-bit multiplication
;*************************************************************************
;File name: MUL4.ASM
;Description: This program is two 4 bit number multiplication program,save
;
result to RUT_H,RUT_L
;Input Arguments: X , Y
;Output Arguments: RUT_H,RUT_L
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
X
EQU
20H
;multiplicand register
Y
EQU
21H
;multiplier register
RUT_H
EQU
22H
;summation high 4 bits register
RUT_L
EQU
23H
;summation low 4 bits register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,05H
;set X = 05h
LDI
Y,04H
;set Y = 04h
LDI
RUT_L,00H
;set RUT_L = 00h
LDI
RUT_H,00H
;set RUT_H = 00h
;
AGAIN:
SBIM
Y,01H
;Y - 1 -->Y
BC
ADD_RUT_L
;Y>0 ? yes, go to ADD_RUT_L
JMP
MUL_OK
;no,end multiplication
;
ADD_RUT_L:
LDA
X,00H
;get multiplicand X
ADDM
RUT_L,00H
;add X to RUT_L
BC
ADD_RUT_H
;carry ? , yes ,goto ADD_RUT_H
JMP
AGAIN
;no,goto AGAIN
;
ADD_RUT_H:
ADIM
RUT_H,01H
;add CY to RUT_H
JMP
AGAIN
MUL_OK:
;*************************************************************************
38/126
Ver1.0
39/126
Ver1.0
2-5
4/8-Bit Division
There isnt an instruction for division in this SH6610 system, so we again need to use
existing instructions to perform this function. As you all know, division means the number of
times one number can be subtracted by another number. Therefore we can conclude that
division can be performed with subtraction and other instructions. We have two examples
that are relatively representative for 4/8-bit division:
[Example2-5.1] 4-bit division
;*************************************************************************
;File name: DIV4.ASM
;Description: This program is two 4 bit number division program,then save the
;
quotiert to QUO register , save remainder to REM register.
;Input Arguments: X , Y
;Output Arguments: QUO,REM
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X
EQU
20H
;dividend register
Y
EQU
21H
;divisor register
QUO
EQU
22H
;quotiert register
REM
EQU
23H
;remainder register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,0FH
;set X=0fh
LDI
Y,03H
;set Y=03h
LDI
QUO,00H
;clear QUO
LDI
REM,00H
;clear REM
;__________________________
AGAIN:
LDA
Y,00H
;get divisor number
SUBM
X,00H
;use subtraction to do divisor
BC
ADD_QUO
;X>Y ? ,yes,goto ADD_QUO
JMP
DIV_OK
;no,goto DIV_OK
ADD_QUO:
STA
REM,00H
;save remainder to REM
ADIM
QUO,01H
;quottiert + 1,save to QUO
JMP
AGAIN
DIV_OK:
;ending divisor
;************************************************************************
Ver1.0
;
quotiert to QUO_H,QUO_L register , save remainder to REM_H,REM_L
;
register.
;Input Arguments: X_H ,X_L , Y_H , Y_L
;Output Arguments: QUO_H,QUO_L,REM_H,REM_L
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X_H
EQU
20H
;dividend high 4 bits register
X_L
EQU
21H
;dividend low 4 bits register
Y_H
EQU
22H
;divisor high 4 bits register
Y_L
EQU
23H
;divisor low 4 bits register
QUO_H
EQU
30H
;quotiert high 4 bits register
QUO_L
EQU
31H
;quotiert low 4 bits register
REM_H
EQU
32H
;remainder high 4 bits register
REM_L
EQU
33H
;remainder low 4 bits register
TMP
EQU
24H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X_H,0FH
;set divided =FFH
LDI
X_L,0FH
LDI
Y_H,01H
;set divisor =10H
LDI
Y_L,00H
LDI
QUO_L,00H
;clear QUO_L
LDI
QUO_H,00H
;clear QUO_H
LDI
REM_L,00H
;clear REM_L
LDI
REM_H,00H
;clear REM_H
;__________________________
AGAIN:
LDA
Y_L,00H
;get divisor low nibble
SUBM
X_L,00H
;division low nibble
;
LDA
Y_H,00H
;get divisor high nibble
SBCM
X_H,00H
;division high nibble
BC
ADD_QUO_L
;divided > divisor ? yes,goto
JMP
DIV_OK
;no,goto DIV_OK
;
ADD_QUO_L:
STA
REM_H,00H
;save remainder high nibble to
;
LDA
X_L,00H
;get remainder low nibble
STA
REM_L,00H
;save remainder low nibble to REM_L
;
ADIM
QUO_L,01H
;quotiert low nibble +1
LDI
TMP,00H
;clear AC=0
ADCM
QUO_H,00H
;add CY to QUO_H
JMP
AGAIN
;
DIV_OK:
;
;*************************************************************************
41/126
Ver1.0
2-6
In the following example, the main function is to do a left movement by n bit(s) for a 4/8-bit
value. However, there isnt such a specific instruction provided in SH6610 system. So
according to the principle of left movement, moving left by n bit(s) is equal to being multiplied
by 2n, and the multiplied result is equal to the result from moving left by n bit(s).
[Example 2-6.1] 4-bit left move by n bit (s) (n=1~3)
;*************************************************************************
;File name: LEFT4.ASM
;Description: This program is 4 bit left program .
;Input Arguments: LFT,N
;Output Arguments: RUT
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
LFT
EQU
20H
;left register
RUT
EQU
22H
;result register
N
EQU
23H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
LFT,1010B
;set left number LFT=1010b
LDI
N,01H
;left 1 bit
11
CALL
SHIFT4
;call shift4 program
LDI
LFT,1010B
;set left number LFT=1010b
LDI
N,02H
;left 2 bit
CALL
SHIFT4
LDI
LFT,1010B
;set left number LFT=1010b
LDI
N,03H
;left 3 bit
CALL
SHIFT4
JMP
$
;**************************
;SHIFT LEFT N BIT SUB PROGRAM
;**************************
SHIFT4
SBIM
N,01H
;N>0,then goto LEFT
1
BC
LEFT
;else goto ENDLEFT
JMP
ENDLEFT
;
LEFT:
LDA
LFT,00H
;get left data
ADDM
LFT,00H
;left one bit
STA
RUT,00H
;save to RUT
JMP
SHIFT4
;
ENDLEFT:
RTNI
;return to main program
;*************************************************************************
42/126
Ver1.0
Ver1.0
ENDLEFT:
RTNI
;return to main program
;*************************************************************************
2-7
In the following example, the main function is to do a right movement by n bit(s) for a
4/8-bit value. However, there isnt such a specific instruction provided in SH6610 system.
So according to the principle of right movement, moving right by n bit(s) is equal to being
divided by 2n, and the divided result is equal to the result from moving right by n bit(s).
[Example2-7.1] 4-bit right move by n bit(s) (n=1~3)
;*************************************************************************
;File name: RIGHT4.ASM
;Description: This program is 4 bit right program .
;Input Arguments: RIGB, N
;Output Arguments: RESB
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
RIGB
EQU
20H
;right register
VAR1
EQU
21H
;variable 1 register
RESB
EQU
22H
;result register
N
EQU
23H
;N=1~3
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
RIGB,1010B
;set right number=1010b
LDI
N,01H
;right 1 bit
CALL
RIGHT4
;call right4 origram
LDI
RIGB,1010B
;set right number=1010b
LDI
N,02H
;right 2 bit
CALL
RIGHT4
;call right4 origram
LDI
RIGB,1010B
;set right number=1010b
LDI
N,03H
;right 3 bit
CALL
RIGHT4
;call right4 origram
JMP
$
;**************************
;SHIFT RIGHT N BIT SUB PROGRAM
;**************************
RIGHT4
LDI
VAR1,02H
;set divid number
RIGHT:
SBIM
N,01H
;if n-1<0,then right ok!
BC
AGAIN1
;else right 1 bit
JMP
ENDRIGH
;jump to ENDRIGH
;__________________________
AGAIN1:
LDI
RESB,00H
;divid 2 to get right 1 bit
AGAIN:
LDA
VAR1,00H
SUBM
RIGB,00H
BC
AD_RESB
LDA
RESB,00H
;get right N-1 bit number
STA
RIGB,00H
;save to RIGB
JMP
RIGHT
AD_RESB:
ADIM
RESB,01H
;RESB add one
44/126
Ver1.0
AGAIN
;right again
JMP
ENDRIGH:
RTNI
;return to main program
*************************************************************
45/126
Ver1.0
Ver1.0
LDA
RESL,00H
;RESL-->RIG0
STA
RIG0,00H
JMP
RIGHT
ADD_RESL:
ADIM
RESL,01H
;quotient low nibble + 1
BC
ADD_RESH
LDI
TMP,0
;clear AC
ADCM
RESH,01H
;quotient high nibble + 1
JMP
AGAIN
ENDRIGH:
RTNI
;return to main program
;*************************************************************************
47/126
Ver1.0
48/126
Ver1.0
49/126
Ver1.0
2-9
[Example2-9.1] BinaryBCD
;*************************************************************************
;File name: BINBCD.ASM
;Description: This program is BINARY code convert to BCD program,result
;
save to BDC2,BCD1,BCD0
;Input Arguments: BIN1 ,BIN0
;Output Arguments: BCD2, BCD1, BCD0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
BIN1
EQU
20H
;Binary high nibble register
BIN0
EQU
21H
;Binary low nibble register
BCD2
EQU
22H
;BCD hundred register
BCD1
EQU
23H
;BCD ten register
BCD0
EQU
24H
;BCD number register
TEMP
EQU
25H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;_____________15H=21_________
LDI
BIN1,01H
;set binary = 15h
LDI
BIN0,05H
CALL
CHANGE
;call conversion program
;_____________TEMP=80__________
LDI
BIN1,05H
;set binary = TEMP
LDI
BIN0,00H
CALL
CHANGE
;call conversion program
;_____________FFH=255_________
LDI
BIN1,0FH
;set binary = FFh
LDI
BIN0,0FH
CALL
CHANGE
;call conversion program
JMP
$
;***************************
;Binary to BCD conversion program
;***************************
CHANGE:
LDI
BCD2,00H
;clear result register
LDI
BCD1,00H
LDI
BCD0,00H
;
LDA
BIN0,00H
;get binary code
ADDM
BCD0,00H
;add Binary low nibble to BCD0
DAA
BCD0
LDI
TEMP,00H
;clear AC
ADCM
BCD1,00H
;add CY to BCD1
DOCG:
SBIM
BIN1,01H
;BIN1-1 >0 ?
BC
CG2
;yes,goto CG2
JMP
ENDCH
;no,goto ENDCH
50/126
Ver1.0
CG2:
ADIM
BCD0,06H
;add 16 to result
DAA
BCD0
LDI
TEMP,00H
;clear AC
ADCM
BCD1,00H
;add CY to BCD1
DAA
BCD1
;adjust BCD1
LDI
TEMP,00H
;clear AC
ADCM
BCD2,00H
;add CY to BCD2
ADIM
BCD1,01H
DAA
BCD1
;adjust BCD1
LDI
TEMP,00H
;clear AC
ADCM
BCD2,00H
;add CY to BCD2
JMP
DOCG
ENDCH:
RTNI
;return to main program
;*************************************************************************
51/126
Ver1.0
Ver1.0
JMP
DOCG
ENDCH:
RTNI
;************************************************************************
53/126
Ver1.0
2-10
Square Operation
In this application program, we look up the square number in a Table. The use of Table
is very important in this system, and we usually get the required data by looking up in a table
especially when storing and taking a large quantum of data.
[Example2-10.1] Looking up in a table to get the square numbers (for 0-9)
;*************************************************************************
;File name: SQUARE.ASM
;Description: This program use Table to get "0-9" square numbers,
;
result savt to SQU_H,SQU_L
;Input Arguments: TMP
;Output Arguments: SQU_H,SQU_L
;*************************************************************************
;REGISTER TABLE DEFINE
;**************************
TBR
EQU
0EH
;**************************
;RAM REGISTER TABLE
;**************************
TMP
EQU
20H
;variable register
SQU_H
EQU
21H
;square numbers high nibble register
SQU_L
EQU
22H
;square numbers low nibble register
AC_B
EQU
23H
;AC buffer register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;______________________________
LDI
TMP,00H
;get "0" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,02H
;get "2" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,04H
;get "4" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,06H
;get "6" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,09H
;get "9" square numbers
CALL
GET_SQU
JMP
$
;******************************
;get square numbers sub program
;******************************
GET_SQU:
LDI
TBR,00H
;set TJMP address
LDI
AC_B,01H
LDA
TMP,00H
;get TMP
ADD
AC_B,00H
54/126
Ver1.0
CALL
STA
LDA
STA
0500H
SQU_L,00H
TBR,00H
SQU_H,00H
RTNI
;*******************************
ORG
0500H
;square numbers data Table
TJMP
;jump by TJMP address ,to get data
RTNW
00H,00H
;0 ;return data
RTNW
00H,01H
;1
RTNW
00H,04H
;2
RTNW
00H,09H
;3
RTNW
01H,06H
;4
RTNW
02H,05H
;5
RTNW
03H,06H
;6
RTNW
04H,09H
;7
RTNW
06H,04H
;8
RTNW
08H,01H
;9
;************************************************************************
55/126
Ver1.0
Ver1.0
ADDM
JMP
HEX0,00H
ENDCH
;
ENDCH:
RTNI
;return main program
;*************************************************************************
57/126
Ver1.0
Ver1.0
;*************************************************************************
59/126
Ver1.0
2-12
Ver1.0
SBIM
ASC,0AH
;convert ASCII(A-F) to Decimal
ADDM
DEC0,00H
;save Decimal high nibble to DEC1
DAA
DEC0
;save Decimal low nibble to DEC0
LDI
ASC,0
ADCM
DEC1,00H
ENDCH:
RTNI
;return to main program
;***********************************************************************
61/126
Ver1.0
62/126
Ver1.0
2-13
Ver1.0
LDA
ADDM
CALL
STA
LDA
STA
TMP,00H
AC_B,00H
0500H
COD_L,00H
TBR,00H
COD_H,00H
ENDCH:
RTNI
;
;*********************************
;seven segment hex code data area
;*********************************
ORG
0500H
TJMP
;__________common cathode table__
RTNW
03H,0FH
;get data
;save LED code low nibble to COD_L
;save LED code high nibble to
;COD_H
;return to main program
RTNW
00H,06H
RTNW
05H,0BH
RTNW
04H,0FH
RTNW
06H,06H
RTNW
06H,0DH
RTNW
07H,0DH
RTNW
00H,07H
RTNW
07H,0FH
RTNW
06H,0FH
;__________common anode table____
ORG
0510H
RTNW
0CH,00H
RTNW
0FH,09H
RTNW
0AH,04H
RTNW
0BH,00H
RTNW
09H,09H
RTNW
09H,02H
RTNW
08H,02H
RTNW
0FH,08H
RTNW
08H,00H
RTNW
09H,00H
;0
;1
;2
;3
;4
;5
;6
;7
;8
;9
64/126
Ver1.0
2-14
65/126
Ver1.0
Ver1.0
ADCM
ADD_H,00H
DAA
STA
ADD_H
SUM1,00H
LDI
TMP,0
ADCM
SUM2,00H
;
;addition carry number CY to
;summation
;high nibble register
;
RTNI
;return to main program
;*************************************************************************
67/126
Ver1.0
2-15
68/126
Ver1.0
Ver1.0
;
SUBH:
;
ENDSUB:
BC
LDI
SUBH
N,01H
LDA
SUBM
DAS
STA
BC
LDI
JMP
SUB_H,00H
MIN_H,00H
MIN_H
REH,00H
ENDSUB
N,01H
ENDSUB
LDA
BAZ
LDI
LDI
LDA
SUB
STA
LDA
SUB
STA
N,00H
RETSUB
MIN_H,9
MIN_L,10
REH,00H
MIN_H,00H
REH,00H
REL,00H
MIN_L,00H
REL,00H
;
RETSUB:
RTNI
;return to main program
;*************************************************************************
70/126
Ver1.0
2-16
Ver1.0
Ver1.0
;
ADDEQS:
JMP
ENDMUL
LDA
ADDM
DAA
MUL_A0,00H
RUT_0,00H
RUT_0
LDI
ADCM
DAA
TEMP,0
RUT_1,00H
RUT_1
;clear AC =0
;add CY
;adjust AC for add,save to RUT_1,AC
LDI
ADCM
DAA
TEMP,0
RUT_2,00H
RUT_2
;clear AC
;add CY to RUT_2
;adjust RUT_2
LDI
ADCM
TEMP,0
RUT_3,00H
;clear AC
;add CY to RUT_3
LDA
ADDM
DAA
MUL_A1,00H
RUT_1,00H
RUT_1
;RUT_1=MUL_A1+RUT1
;adjust RUT_1
LDI
ADCM
DAA
TEMP,0
RUT_2,00H
RUT_2
;clear AC
;add CY to RUT_2
;adjust RUT_2
LDI
ADCM
JMP
TEMP,0
RUT_3,00H
DOMUL
;clear AC
;add CY to RUT_3
;goto DOMUL
;
ENDMUL:
RTNI
;return to main program
;************************************************************************
73/126
Ver1.0
2-17
74/126
Ver1.0
Ver1.0
DODIV:
LDA
DIV_B0,00H
;(division low nibble)
SUBM
DIV_A0,00H
;DIV_A0-DIV_B0 --> DIV_A0
DAS
DIV_A0
;adjust AC for sub
BC
ADD_QUO
SBIM
DIV_A1,01H
BC
ADD_QUO
JMP
DIV_OK
ADD_QUO:
LDA
DIV_A1,0
;save remainder high nibble
STA
REM_H,0
;to REM_H
LDA
DIV_A0,0
;save remainder low nibble
STA
REM_L,0
;to REM_L
ADIM
QUO_L,01H
;quotient low nibble + 1,save to
DAA
QUO_L
;QUO_H
LDI
50H,0
;clear AC
ADCM
QUO_H,01H
;quotient high nibble + 1,save to
JMP
DIVH
;QUO_L
DIV_OK:
RTNI
;return to main program
;*************************************************************************
76/126
Ver1.0
2-18
Compare routine
There isnt a direct Compare instruction in SH6610 system, so when you want to do
Compare operation, we can use subtraction and Carry Flag to perform this function. The
following example performs the Compare function in this way.
Ver1.0
2-19 Delay
In program designing, a subprogram for delay is often used to do nothing at a given
period. How to calculate the delay time is very important. Suppose an instruction cycle is
2us, then we can delay 2N us by executing N times of instruction cycles.
[Example2-19.1] Subprogram for delay
;*************************************************************************
;File name: DELAY.ASM
;Description: This program is to delay a long time ,you can use parmeter to
;
decide how long to delay.
;Input Arguments: CT2,CT1,CT0,PARM
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
CT0
EQU
20H
;variable 0 register
CT1
EQU
21H
;variable 1 register
CT2
EQU
22H
;variable 2 register
PARM
EQU
23H
;parmeter register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;___________delay 12 ms____
LDI
PARM,1
;set parmeter =0
CALL
DELAY
;call delay program
;___________delay 24 ms____
LDI
PARM,2
CALL
DELAY
JMP
$
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
PARM,01H
;judge parmater is >0
BC
$+2
RTNI
;return to main program
LDI
CT2,0AH
;set counter2=0ah
LDI
CT1,0FH
;set counter1=0fh
LDI
CT0,0FH
;set counter0=0fh
SBIM
CT0,01H
;counter0 down 1
BC
$-1
SBIM
CT1,01H
;counter1 down 1
BC
$-4
SBIM
CT2,01H
;counter2 down 1
BC
$-7
JMP
DELAY
;
;*************************************************************************
78/126
Ver1.0
2-20 Loop
In the following example, we will use Loop to perform three tasks. First: fill in data
memory $80H~$8FH with desired values. Second: add up all the values in data memory
$80H~$8FH. Third: get the largest value from data memory $80H~$8FH.
[Example2-20.1] Loop subprogram
;*************************************************************************
;File name: LOOP.ASM
;Description: This program is three kinds loop program,(fill memory with data
;
routine,sum of data ,maxinum value)
;Input Arguments: DPH,DPM,DPL
;Output Arguments: MAX,SUM1,SUM0
;*************************************************************************
;System register define
;**************************
INX
EQU
0FH
DPL
EQU
10H
DPM
EQU
11H
DPH
EQU
12H
;**************************
;Data register table define
;**************************
TMP
EQU
20H
;vraiable register
MAX
EQU
21H
;max number register
SUM1
EQU
22H
;summation high nibble register
SUM0
EQU
23H
;summation low nibble register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;-------------------------;CALL LOOP 1
;-------------------------CALL
LOOP1
;-------------------------;CALL LOOP 2
;-------------------------CALL
LOOP2
;-------------------------;CALL LOOP 3
;-------------------------CALL
LOOP3
;-------------------------JMP
$
;**************************
; LOOP 1
; Fill Memory $80h~$8fh with data
;**************************
LOOP1:
LDI
TMP,03H
;set write data =03h
79/126
Ver1.0
LDI
LDI
LDI
LDA
STA
ADIM
BC
JMP
RTNI
DPH,01H
DPM,00H
DPL,00H
TMP,00H
INX,00H
DPL,01H
ENDLP1
$-4
;get data
;write data(03h) into data memory
;index address +1
;if index address > $8fh,end fill data
ENDLP1:
;return to main program
;
;**************************
;LOOP 2
;SUM OF DATA form $80h~$8fh
;**************************
LOOP2:
LDI
SUM0,00H
;set high 4 bit =00h
LDI
SUM1,00H
;set low 4 bit =00h
LDI
DPH,01H
;set index address=80h
LDI
DPM,00H
LDI
DPL,00H
LDA
INX,00H
;get data from data memory
ADDM
SUM1,00H
BC
ADDSUM0
ADIM
DPL,01H
;index address +1
BC
ENDLP1
JMP
$-5
ADDSUM0:
ADIM
SUM0,01H
JMP
$-4
ENDLP2:
RTNI
;return to main program
;**************************
;LOOP 3
;GET MAXIMUM VALUE between $80h~$8fh;
;**************************
LOOP3
LDI
MAX,00H
LDI
DPH,01H
;set index address =80h
LDI
DPM,00H
LDI
DPL,00H
LDA
INX,00H
;get data from data memory
SUB
MAX,00H
;compare which number is more
BC
ADD_DPL
;bigger
LDA
INX,00H
;save the max number to MAX
STA
MAX,00H
;register
ADD_DPL:
ADIM
DPL,01H
;index address +1
BC
ENDLP3
;if index address >$8fh,goto ENDLP3
JMP
$-7
ENDLP3:
RTNI
;return to main program
;************************************************************************
80/126
Ver1.0
2-21
In this application program, random numbers are generated by the system timer.
[Example2-21.1] Generating four random numbers
;************************************************************************
;File name: RANDOM1.ASM
;Description: This program is use SH6610 timer0 to generate 4 random number
;Input Arguments: TM0,T0L,T0H
;Output Arguments: RAN0,RAN1,RAN2,RAN3
;*************************************************************************
;System register table define
;**************************
INT
EQU
00H
IRQ
EQU
01H
TM0
EQU
02H
T0L
EQU
04H
T0H
EQU
05H
;**************************
;RAM REGISTER TABLE
;**************************
RAN0
EQU
20H
;random 0 register
RAN1
EQU
21H
;random 1 register
RAN2
EQU
22H
;random 2 register
RAN3
EQU
23H
;random 3 register
CNT1
EQU
24H
;variable register
CNT2
EQU
25H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
JMP
TIMER0
;timer 0 interrupt
NOP
NOP
;**************************
RESET:
LDI
TM0,0101B
;set timer 0 freq.
LDI
T0L,00H
;clear TOL
LDI
T0H,00H
;clear TOH
LDI
INT,04H
;enable timer 0 interrupt
LDI
IRQ,00H
;clear interrupt flag
CALL
RANDOM
;call get random sub_program
JMP
$-1
;**************************
;TIMER 0 INTERRUPT SUB PROGRAM
;**************************
TIMER0
LDI
INX,04H
;enable timer 0 interrupt
LDI
IRQ,00H
;clear interrupt request flag
RTNI
;**************************
;GET FOUR RANDOM NUMBER
;**************************
RANDOM
CALL
DELAY
;call delay time program
LDA
T0H,00H
;read timer 0 high nibble number
STA
RAN0,00H
;save to RAN0
81/126
Ver1.0
LDA
T0L,00H
;read timer 0 low nibble number
STA
RAN1,00H
;save to RAM1
CALL
DELAY
;call delay time program
LDA
T0H,00H
;read timer 0 high nibble number
STA
RAN2,00H
;save to RAN2
LDA
T0L,00H
;read timer 0 low nibble number
STA
RAN3,00H
;save to RAM3
LDA
RAN0,00H
;get random data
STA
T0L,00H
;initial TOL
LDA
RAN3,00H
;get random data
STA
T0H,00H
;initial TOH
RTNI
;****************************
;DELAY FOR A LONG TIME
;****************************
DELAY
;
LDI
CNT1,0AH
LDI
CNT2,0FH
SBIM
CNT2,01H
BC
$-1
SBIM
CNT1,01H
BC
$-4
RTNI
;*************************************************************************
82/126
Ver1.0
2-22
The main function of Check sum is to check whether certain data has been changed so
as to ensure data validity. For instance, we need to know whether our data has been
damaged after moving or transmission, we can perform Check sum twice, both before and
after moving or transmission, and then compare the two Check sum results. If they are
identical, the data is correct.
In the following example, we will get the check sum of the data in data memory
$80h~$9Fh. Doing logic exclusive or (EOR) and adding up the EOR results of the original
data, we will obtain the check sum value we need.
[Example2-22.1] Check sum of data
;*************************************************************************
;File name: CHKSUM.ASM
;Description: This program is to get check sum from data memory $80h~$9fh,
;
that check sum save to SUM4,SUM3,SUM2,SUM1,SUM0
;Output Arguments: SUM4,SUM3,SUM2,SUM1,SUM0
;*************************************************************************
;SYSTEM REGISTER TABLE DEFINE
;**************************
INX
EQU
0FH
;Pseude index register
DPL
EQU
10H
;Data pointer for INX low nibble
DPM
EQU
11H
;Data pointer for INX middle nibble
DPH
EQU
12H
;Data pointer for INX high nibble
;**************************
;RAM REGISTER TABLE DEFINE
;**************************
TMP
EQU
20H
;variable register
SUM4
EQU
21H
;summation 4 register
SUM3
EQU
22H
;summation 3 register
SUM2
EQU
23H
;summation 2 register
SUM1
EQU
24H
;summation 1 register
SUM0
EQU
25H
;summation 0 register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
CALL
CHKSUM
;call CHKSUM sub program
JMP
$
;**************************
;Check sum of data ($80H-$9FH)
;**************************
CHKSUM:
LDI
SUM4,00H
;set SUM4=0
LDI
SUM3,00H
;set SUM3=0
LDI
SUM2 00H
;set SUM2=0
LDI
SUM1,00H
;set SUM1=0
LDI
SUM0,00H
;set SUM0=0
;-------------------------LDI
DPH,01H
;set index address=$80h
LDI
DPM,00H
83/126
Ver1.0
LDI
DPL,00H
;-------------------------NEXT_DA:
LDA
INX,00H
;get data from data memory
STA
TMP,00H
;save to TMP
;-------------------------EORIM
TMP,0FH
;TMP^0fh -->TMP
ADDM
SUM0,00H
;SUM0+AC-->SUM0
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM1,00H
;add CY to SUM1
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM2,00H
;add CY to SUM2
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM3,00H
;add CY to SUM3
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM4,00H
;add CY to SUM4
;-------------------------ADIM
DPL,01H
;index address +1
LDI
TMP,00H
;clear AC=0
ADCM
DPM,00H
;if CY =1,index address + 10H
SBI
DPM,02H
;if index address =0AH,then ending
BAZ
ENDCHK
JMP
NEXT_DA
;goto NEXT_DA(to get next data)
ENDCHK:
RTNI
;return to main program
;************************************************************************
84/126
Ver1.0
CHAPTER THREE
System Hardware Application
In the previous chapter, weve learnt about system software application. Now we are
going to introduce the hardware applications of SH6610 system, e.g. timer, system interrupt,
keyboard and I/O port etc.
3-1 Timer
There are two 8-bit counters (up timer 0 and up timer 1) in SH6610 system. Their
starting values can be inputted from outside and the count value can be read out. You
should note that the system counter starts counting immediately after the system is powered
on, so you should pay special attention to presetting its value in your practical use. Timer
can also be used to generate an interrupt, which will be inrtrduced in detail in later chapters.
Some system registers must be set up before using a timer, as listed in the following table:
[Table 3-1.1] Timer controlled register
Address
$02
$03
$04
BIT3 BIT2
------ TM0.2
------ TM1.2
BIT1
TM0.1
TM1.1
T0L
$05
T0H
$06
T1L
$07
T1H
BIT0
TM0.0
TM1.0
R/W
Explanation
R/W Timer 0 mode select register
R/W Timer 1 mode select register
R/W Timer 0 write in/timing register,
Lower 4 bits
R/W Timer 0 write in/timing register,
Higher 4 bits
R/W Timer 1 write in/timing register,
Lower 4 bits
R/W Timer 1 write in/timing register,
Higher 4 bits
85/126
Ver1.0
3-1.1 Timer 0
Timer Mode Register 0 must be selected before using Timer 0. Timer register is used to
select the counters frequency, as listed in the following table:
[Table3-1.1.1] Timer0 frequency table
TM0.2
TM0.1
TM0.0
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
Divided by
frequency
/2048
/512
/128
/32
/8
/4
/2
/1
Frequency source
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
After setting up timer mode register, if you want to preset the starting value for the
timer, you can write the desired value into the timer load register, lower 4 bits first and then
higher 4 bits. However, if you read data from the timer, you must read the higher 4 bits first,
and then the lower 4 bits.
EQU
EQU
:
:
LDI
LDI
LDI
:
02H
EQU
05H
TM0,03H
T0L,0AH
T0H,00H
86/126
Ver1.0
3-1.2 Timer 1
Timer Mode Register (TM1) must be selected before using Timer 1. Timer1 mode
register is used to select the frequency to be inputted to timer, as listed in the following table:
[Table3-1.2.1] Timer1 frequency table
TM1.2
TM1.1
0
0
0
0
1
1
1
1
TM1.0
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
Divided by
frequency
/2048
/512
/128
/32
/8
/4
/2
/1
Frequency source
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
After setting up timer1 mode register, if you want to preset the starting value for the
timer1, you can write the desired value into the timer1 load register, lower 4 bits first and then
higher 4 bits. However, if you read data from the timer, you must read the higher 4 bits first,
and then the lower 4 bts.
[Example3-1.2] Setting up timer 1
TM1
T1L
T1H
EQU
EQU
EQU
:
:
LDI
LDI
LDI
02H
04H
05H
TM1,03H
T1L,0AH
T1H,00H
BIT4
R/W
R/W
R/W
R/W
R/W
R/W
R/W
87/126
explanation
I/O port
I/O port
I/O port
I/O port
I/O port
I/O port
Ver1.0
08H,0
08H,0
***** TIMER*****
Press MODE key four times
from normal time display,
the unit changes to a timer 00-0000(min,sec,centisecond),
press HR key to start the
timer ,press MIN key to stop the
timer,then press SEC key to clear
[Chart 3-2.2] Bidirection Type
Besides, when I/O pins are used to drive load, it should be noted that SH6610s driving
capability is relatively weak. So other control circuits must be added for loads that require
stronger driving capabilities. SH6610s drive current is 1mA and sink current is 5mA (I/O
driving capability may differ from different systems, and this is the drive capability for
SH66P20).
88/126
Ver1.0
$01
IRQ
IRQT0
TM0.1
TM0.1
R/W
Explanation
R/W Interrupt enable flag (external
interrupt, timer0 interrupt, timer1
interrupt and port B, C interrupt)
R/W When interrupt occurs, its
corresponding request bit will be set
to 1
When interrupt occurs, interrupt request flag will be set to 1. If the corresponding
interrupt bit is enabled before, the interrupt will occur in priority order. When interrupt occurs,
PC and CY will be saved in stack, and the value of PC will be replaced by the corresponding
interrupt vector address. In interrupt service program, if you want the system to accept
interrupt signals continuously, then before the program returns with instruction RTNI, you
should clear the interrupt request flag (0) and enable the interrupt enable flag.
Set up interrupt
service program
address
enable
interruput
if interruput
service
occurs
89/126
go to
interruput
service
program
Ver1.0
Write in
timer
starting
value
90/126
Enable timer
interrupt
Ver1.0
(2) Bounce:
When a key is pressed, there must be mechanical vibrations to make the touch point
open and close for many times before a stable touching is achieved, the bouncing time
of being pressed and released varying according to type of switch. If there is only one
switch, generally RS reciprocal switch is used to debounce. If software is used to
process the bouncing of keyboard, just to use a delay period. If special keyboard IC is
used, bouncing circuit is naturally included in IC. The typical procedure is when a key
press is detected, to wait for some time to check again. If the key press still exist, it is
recognized as a valid press. In this way, bouncing will not be recognized as
multi-pressing by continual detection even if CPU speed is very fast. This can also
avoid misrecognition of the instant disturbance signal of the low electric level.
(3)
Ver1.0
When a key is pressed while some other key is still not released, there are two general
processing procedures. One is called two-key lockout, i.e. a recognized valid key
press must be released before the system can accept another key press. The other
one is called two-key rollover, i.e. keys pressed at the same time are all recognized as
valid and processed in order.
(4)
When a key is pressed and not released, there are also two processing procedures.
One is to recognize it as one key press until it is released, no matter how long it has
been pressed. The other is to detect every other period (about 0.5 second) and
recognize as a valid key press.
(5)
Processing time:
In fact, pressing and releasing a key can be seen as two different statuses. Even for
the same key, we can still use program to send different keyboard data on pressing and
on releasing.
The above explanations are for questions you will have when designing keyboard
detection programs. If you are using special keyboard IC, software design can certainly be
simplified. Using I/O port to detect key press makes software complicated, while designing
becomes more freely.
92/126
Ver1.0
Ver1.0
;
CHKEY:
CLRKEY:
CLRKB1:
ENDKEY:
BAZ
JMP
CHKEY
CLRKEY
;jump to CHKEY
;else jump to CLRkey
LDA
SUB
BAZ
LDA
STA
EORIM
STA
JMP
LDI
LDI
KEY,00H
KEY_B,00H
CLRKB1
KEY,00H
KEY_B,00H
KEY,0FH
KEY_NUM,00H
ENDKEY
KEY_B,00H
KEY_NUM,00H
RTNI
;return to main program
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
PARM,01H
;judge parameter is >0
BC
$+2
RTNI
;return to main program
LDI
CT2,0AH
LDI
CT1,0FH
LDI
CT0,0FH
SBIM
CT0,01H
BC
$-1
SBIM
CT1,01H
BC
$-4
SBIM
CT2,01H
BC
$-7
JMP
DELAY
;*************************************************************************
94/126
Ver1.0
Ver1.0
;
CHKEY:
SENTKEY:
;
DELKEY:
;
CLRKEY:
LDI
LDA
STA
SBI
BAZ
LDI
CALL
LDA
SUB
BAZ
JMP
PORT_A,0FH
PORT_A,00H
KEY,00H
KEY,0FH
CLRKEY
PARM,3
DELAY
PORT_A,00H
KEY,00H
CHKEY
CLRKEY
LDA
SUB
BAZ
LDA
STA
EORIM
STA
JMP
KEY,00H
KEY_B,00H
DELKEY
KEY,00H
KEY_B,00H
KEY,0FH
KEY_NUM,00H
ENDKEY
LDI
CALL
JMP
PARM,0FH
DELAY
SENTKEY
LDI
LDI
KEY_B,00H
KEY_NUM,00H
;clear KEY_B
;clear KEY_NUM
ENDKEY:
RTNI
;return to main program
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
PARM,01H
;judge paramater is >0
BC
$+2
RTNI
;return to main program
LDI
AA2,0AH
LDI
AA1,0FH
LDI
AA0,0FH
SBIM
AA0,01H
BC
$-1
SBIM
AA1,01H
BC
$-4
SBIM
AA2,01H
BC
$-7
JMP
DELAY
;************************************************************************
96/126
Ver1.0
In [Chart 3-4.4.1], PB0~PB3 send out scan code, and PA0~PA3 receive them. We
can decide which key has been pressed according to scan code and receive code, and
display its value in binary with PC0~PC3 outputting to 4 LEDs.
[Example3-4.4.1] 4X4 Matrix Keyboard Scan
;*************************************************************************
;File name: KEY4X4.ASM
;Description: This program is a 4x4 keyboard scan program ,and show the
;
key number to 4 LED display
;Input Arguments: S_CODE
;Output Arguments: KEY_NUM
;*************************************************************************
;SYSTEM REGISTER DEFINE
;**************************
PORTA
EQU
08H
;I/O port_A define
PORTB
EQU
09H
;I/O port_B define
PORTC
EQU
0AH
;I/O port_C define
TBR
EQU
0EH
;**************************
;DATA MEMORY TABLE DEFINE
;**************************
CT0
EQU
20H
;variable 0 register
97/126
Ver1.0
CT1
EQU
21H
CT2
EQU
22H
PARM
EQU
23H
TEMP
EQU
24H
KY1
EQU
25H
KEY_F
EQU
26H
KEY_NUM
EQU
27H
S_CODE
EQU
28H
B_AC
EQU
29H
B_TBR
EQU
2AH
PORTB_B
EQU
2BH
KY2
EQU
2CH
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
CALL
SCANKEY4
LDA
KEY_F,00H
BAZ
$-2
LDA
KEY_NUM,00H
STA
PORTC,00H
JMP
RESET
;**************************
SCANKEY4
LDI
PORTA,0FH
LDI
PORTB,0FH
LDI
S_CODE,01H
SCAN:
LDA
S_CODE,00H
STA
PORTB_B,00H
EORIM
PORTB_B,0FH
STA
PORTB,00H
LDA
PORTA,00H
STA
KY1,00H
SBI
KY1,0FH
BAZ
NT_CODE
LDI
PARM,3
CALL
DELAY
LDA
PORTA,00H
STA
KY2,00H
SBI
KY2,0FH
BAZ
NT_CODE
LDA
KY1,00H
SUB
KY2,00H
BAZ
PAB1
NT_CODE:
LDA
S_CODE,00H
ADDM
S_CODE,00H
BAZ
R_SAN32
JMP
SCAN
;********************************
;use TABLE to get really key number
;********************************
PAB1:
LDI
B_TBR,0FH
LDI
B_AC,00H
98/126
;variable 1 register
;variable 2 register
;parameter register
;variable register
;key variable register
;key flag register
;key number register
;scan code register
;ac buffer register
;tbr buffer register
;portb buffer register
;key variable register
Ver1.0
PAB2:
;
PAB3:
LDA
STA
LDA
CALL
SUB
BAZ
JMP
LDA
SUB
BAZ
JMP
PAB4:
LDA
STA
LDI
JMP
ADDAC1:
ADIM
JMP
R_SAN3:
LDI
JMP
R_SAN32:
LDI
LDI
R_SAN31:
RTNI
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
BC
RTNI
LDI
LDI
LDI
SBIM
BC
SBIM
BC
SBIM
BC
JMP
;
;*********************************
;key number define table
;*********************************
ORG
TJMP
ORG
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
B_TBR,00H
TBR,00H
B_AC,00H
0700H
KY2,00H
PAB3
ADDAC1
TBR,00H
PORTB_B,00H
PAB4
ADDAC1
B_AC,00H
KEY_NUM,00H
KEY_F,01H
R_SAN31
B_AC,01H
PAB2
KEY_NUM,00H
R_SAN31
KEY_NUM,00H
KEY_F,00H
PARM,01H
$+2
CT2,0AH
CT1,0FH
CT0,0FH
CT0,01H
$-1
CT1,01H
$-4
CT2,01H
$-7
DELAY
0700H
07F0H
1110B,1110B
1110B,1101B
1110B,1011B
1110B,0111B
1101B,1110B
1101B,1101B
1101B,1011B
1101B,0111B
1011B,1110B
1011B,1101B
1011B,1011B
1011B,0111B
99/126
;0
;1
;2
;3
;4
;5
;6
;7
;8
;9
;A
;B
Ver1.0
RTNW
0111B,1110B
;C
RTNW
0111B,1101B
;D
RTNW
0111B,1011B
;E
RTNW
0111B,0111B
;F
;*************************************************************************
100/126
Ver1.0
101/126
Ver1.0
102/126
Ver1.0
g
0
0
1
1
1
1
1
0
1
1
1
1
1
1
1
1
f
1
0
0
0
1
1
1
0
1
1
1
1
0
0
1
1
e
1
0
1
0
0
0
1
0
1
0
1
1
1
1
1
1
d
1
0
1
1
0
1
1
1
1
1
0
1
1
1
1
0
c
1
1
0
1
1
1
1
1
1
1
1
1
0
1
0
0
b
1
1
1
1
1
0
0
1
1
1
1
0
0
1
0
0
103/126
a
1
0
1
1
0
1
1
1
1
1
1
0
0
0
1
1
Hex code
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7FH
6FH
77H
7CH
58H
5EH
79H
71H
Font
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Ver1.0
Common-anode seven-segment display connects the anodes of all LEDs, which need an
input of 0 to light. Its internal structure is shown in [Chart 3-5.2.2]. [Table3-5.2.2] is a
character table for 0~F on common-anode seven-segment display.
[Chart 3-5.2.2] Common-anode seven-segment display
g
1
1
0
0
0
0
0
1
0
0
0
0
0
0
0
0
f
0
1
1
1
0
0
0
1
0
0
0
0
1
1
0
0
e
0
1
0
1
1
1
0
1
0
1
0
0
0
0
0
0
d
0
1
0
0
1
0
0
0
0
0
1
0
0
0
0
1
c
0
0
1
0
0
0
0
0
0
0
0
0
1
0
1
1
b
0
0
0
0
0
1
1
0
0
0
0
1
1
0
1
1
104/126
a
0
1
0
0
1
0
0
0
0
0
0
1
1
1
0
0
Hex code
C0H
F9H
A4H
B0H
99H
92H
82H
F8H
80H
90H
88H
83H
A7H
A1H
86H
8EH
Font
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Ver1.0
Ver1.0
CT2
EQU
PARM
EQU
;******************************
ORG
JMP
NOP
NOP
NOP
NOP
;******************************
RESET:
LDI
NT:
LDI
27H
28H
;variable 2 register
;parameter register
00H
RESET
LED_N,00H
N,0
CALL
SEVCODE
LDA
STA
COD_L,00H
PORTA,00H
LDA
STA
COD_H,00H
PORTB,00H
PARM,0FH
DELAY
;
LDI
CALL
;
ADIM
LED_N,01H
SBI
LED_N,0AH
BAZ
RESET
JMP
NT
;******************************
;get seven segment display code
;sub program
;******************************
SEVCODE:
LDI
AC_B,01H
LDA
N,00H
STA
TBR,00H
LDA
LED_N,00H
ADDM
AC_B,00H
CALL
0500H
STA
COD_L,00H
LDA
TBR,00H
STA
COD_H,00H
ENDCH:
RTNI
;*******************************
; DELAY 12 x PARM ms
;*******************************
DELAY:
SBIM
PARM,01H
BC
$+2
RTNI
LDI
CT2,0AH
LDI
CT1,0FH
LDI
CT0,0FH
SBIM
CT0,01H
BC
$-1
SBIM
CT1,01H
BC
$-4
SBIM
CT2,01H
106/126
;counter down
Ver1.0
BC
JMP
$-7
DELAY
;
;*********************************
;seven segment hex caode data area
;*********************************
ORG
0500H
TJMP
;jump by TJMP address
;__________common cathode table__
RTNW
03H,0FH
;0 ;return data (AC=0eh,TBR=07h)
RTNW
00H,06H
;1
RTNW
05H,0BH
;2
RTNW
04H,0FH
;3
RTNW
06H,06H
;4
RTNW
06H,0DH
;5
RTNW
07H,0DH
;6
RTNW
00H,07H
;7
RTNW
07H,0FH
;8
RTNW
06H,0FH
;9
;*************************************************************************
107/126
Ver1.0
108/126
Ver1.0
109/126
Ver1.0
If external circuit sets /CS=0, it means to use ADC converter. Then /WR input pin
changes from high level to low level, data in converter register is cleared and in the meantime,
INTR pin changes to high level and /WR returns to high level from low. At this time the
converter starts to convert data. When all data has been converted, /INTR is automatically
set to low level, indicating the conversion is finished. Then the external circuit can set /RD
pin to low level and can successfully read digital data out. Finally /CS is set to high level
again, the whole conversion is completed.
Theres no signal control pins which can connect directly to surrounding IC in NT6XXX
series system, so when NT6XXX system is to be used for controlling conversion from analog
to digital, its I/O must be simulated to /WR and /RD pins. To use NT6XXX I/O ports as /WR
and /RD pins, we must write software program to simulate control signals of conversion. In
this way we can use NY6XXX to successfully control ADC0801 for conversion.
3-6.2.1 ADC0801 Function Experiment
This experiment is to convert analog voltage to digital signal. We will use ADC0801 to
convert analog voltage to digital signal. When ADC0801 converts external analog voltage
signal to digital, /INTR goes from high to low, where the program can decide that the
conversion has finished and send the digital signal outputted from ADC0801 to LED display
for us to observe the converted voltage digital value.
[Chart 3-6.2.1] ADC0801 application circuit chart
110/126
Ver1.0
Ver1.0
; display
;read data high nibble from
; PORT_C
STA
O_PORT_H,00H
;out data to PORT_E for
JMP
AGAIN
; LED display
;********************************************************************************
LDA
D_PORT_H,00H
112/126
Ver1.0
113/126
Ver1.0
;push AC
;clear interrupt flag
;enable Timer 0 interrupt
SUB
TEMP,00H
;if (TEMP-CNT1) > 0,then out"1"to
BC
$+3
;PORT_A,else out"0"to PORT_A
LDI
PORT_A,00H
JMP
$+2
LDI
PORT_A,01H
LDA
AC_B,00H
;pop AC
RTNI
;return to main program
;*************************************************************************
115/126
Ver1.0
116/126
Ver1.0
Ver1.0
;*******************************
ADIM
CNT1,01H
BC
CNG
SUB
TEMP,00H
BC
$+3
LDI
PORT_A,00H
;set PORT_A =0
JMP
$+2
LDI
PORT_A,01H
;set port_B =1
JMP
RT1
;*******************************
;to generate next duty cycle
;*******************************
CNG
LDA
CNT2,00H
;if CNT2 != 0 ,then goto UP
BAZ
UP
;------------------------------;to get next duty cycle
; (15/16T-->1/16T)
;------------------------------SBIM
TEMP,01H
;get next duty cycle
BC
RT1
LDI
CNT2,00H
;set CNT2=0
LDI
TEMP,00H
;set TEMP=0
JMP
RT1
;-----------------------------;to get next duty cycle
; (1/16T-->15/16T)
;------------------------------UP:
ADIM
TEMP,01H
;get next duty cucle
BC
$+2
JMP
RT1
LDI
CNT2,01H
;set CNT2=1
LDI
TEMP,0FH
;set TEMP=0FH
RT1:
RTNI
;return to main program
;*************************************************************************
118/126
Ver1.0
***** TIMER*****
Press MODE key four times from normal time display,
the unit changes to a timer 00-00-00(min,sec,centisecond),
press HR key to start the timer ,press MIN key to stop the
timer,then press SEC key to clear the timer(00-00-00)
Press MODE key again to switch back to normal time
VOL2
1
0
1
0
VOL
100%
75%
25%
0%
You must disable PSG when the system is entering STOP or HALT mode. Its related
control bits are show in [Table3-8.1.2].
119/126
Ver1.0
$1A
$1B
P1.1
P2.1
bit 0
C1.0
C1.4
C2.0
C2.4
C2.8
C2.12
CH1EN
P1.0
P2.0
R/W
W
W
W
W
W
W
R/W
R/W
R/W
Remarks
PSG channel 1 low digit
PSG channel 1 high digit
PSG channel 2 low digit
PSG channel 2
PSG channel 2
PSG channel 2 high digit
bit 0:PSG channel 1 enable
bit 1:PSG channel 2 enable
bit 2,bit 3:Volume Control
(initial is 0,no sound)
PSG 1 Prescaler
PSG 2 Prescaler
Prescaler is another very important controller for PSG control. Its clock source is a
clock of 32KHz frequency, and it divide the setup value to obtain the required frequency, as
shown in [Table3-8.1.3]:
[Table3-8.1.3] Prescaler table
P.1
P.0
Prescaler Divide Ratio
0
0
1
0
1
2
1
0
4
1
1
8
Clock Source
32k Hz
32k Hz
32k Hz
32k Hz
Actual Clock
32k Hz
16k Hz
8k Hz
4k Hz
120/126
Ver1.0
130.813
146.832
164.816
174.614
138.591
155.563
185.000
196.000
220.000
246.942
261.626
293.655
329.628
349.288
391.955
440.000
493.883
523.251
587.330
659.255
698.456
783.991
207.652
233.082
277.183
311.127
369.994
415.305
466.164
554.364
622.254
739.989
122
109
97
92
115
103
86
82
73
65
61
54
49
46
41
36
32
31
27
24
23
20
77
69
58
51
43
39
34
29
26
22
Tone Freq.
131.148
146.789
164.948
173.913
139.130
155.340
186.046
195.122
219.178
246.154
262.295
296.296
326.530
347.826
390.243
444.444
500.000
526.129
592.592
666.666
695.652
800.000
207.792
231.884
275.862
313.725
327.093
410.256
470.588
551.724
615.384
727.272
121/126
0x13,
0x17
0
1
5
3
1
9
A
7
1
4
9
A
B
E
8
A
5
B
B
C
9
C
A
C
D
6
6
3
9
E
7
3
0x14,
0x18
2
5
4
3
6
7
6
2
2
4
4
5
5
5
5
1
2
4
3
5
3
4
7
1
4
5
7
6
6
2
7
7
Error
0.25
0.03
0.08
0.4
0.39
0.14
0.5
0.4
0.3
0.3
0.2
0.9
0.9
0.4
0.4
1.0
1.2
1.3
0.8
1.1
0.4
2.0
0.06
0.5
0.5
0.8
0.6
1.2
0.9
0.5
1.1
1.7
Ver1.0
Ver1.0
JMP
$-10
;play music again
;******************************
;get music code
;******************************
GETCODE:
LDI
AC_B,01H
;set AC ,TBR
LDI
TBR,00H
LDA
MUS_N,00H
ADDM
AC_B,00H
CALL
0500H
;call TJMP to get music freq. code
STA
FREQ_L,00H
;code low nibble save to FREQ_L
LDA
TBR,00H
STA
FREQ_H,00H
;code high nibble save to FREQ_H
ENDCH:
RTNI
;*******************************
; DELAY 12 x PARM ms
;*******************************
DELAY:
SBIM
PARM,01H
;judge paramater is >0
BC
$+2
RTNI
;return to main program
LDI
AA2,0AH
LDI
AA1,0FH
LDI
AA0,0FH
SBIM
AA0,01H
BC
$-1
SBIM
AA1,01H
BC
$-4
SBIM
AA2,01H
BC
$-7
JMP
DELAY
;
;*********************************
;seven segment hex caode data area
;*********************************
ORG
0500H
TJMP
;__________MUSIC TABLE___________
RTNW
09H,04H
;Do ;return data (AC=04h,TBR=09h)
RTNW
0AH,05H
;Re
RTNW
0BH,05H
;Me
RTNW
0EH,05H
;Fa
RTNW
08H,05H
;So
RTNW
0AH,01H
;La
RTNW
05H,02H
;Si
RTNW
0BH,04H
;DO^
;*************************************************************************
123/126
Ver1.0
Ver1.0
JMP
AGAIN
CALL
BU_BU
LDI
CALL
SBIM
BC
LDI
JMP
TMP,0FH
DELAY
TMP,01H
$-2
FLAG1,00H
AGAIN
19H,0FH
1AH,0001B
1BH,0001B
14H,0AH
13H,00H
18H,0AH
17H,00H
BU:
;
;*****************************
CHE_CHE
;
LDI
LDI
LDI
LDI
LDI
LDI
LDI
RTNI
;****************************
CHA_CHA
LDI
LDI
LDI
;
LDI
LDI
;
LDI
LDI
RTNI
;*********************************
BU_BU
LDI
LDI
LDI
LDI
LDI
LDI
LDI
RTNI
;**********************************;
;DELAY TIME SUB PROGRAM
;**********************************;
DELAY
LDA
STA
DELAY3:
LDI
DELAY2:
LDI
DELAY1:
SBIM
BC
CALL
SBIM
BC
SBIM
19H,0FH
1AH,0010B
1BH,0010B
14H,0AH
13H,00H
18H,0AH
17H,00H
1AH,00H
1BH,00H
13H,0CH
14H,04H
19H,0FH
17H,0CH
18H,0CH
FLAG,00H
COUNT03,00H
COUNT01,01H
COUNT02,08H
COUNT02,01H
DELAY1
DELAY_1
COUNT01,01H
DELAY2
COUNT03,01H
125/126
Ver1.0
BC
DELAY3
RTNI
;**********************************;
;DELAY TIME SUB PROGRAM
;**********************************;
DELAY_1
LDI
COUNT33,0DH
DELAY33
LDI
COUNT11,0FH
DELAY22
LDI
COUNT22,0FH
DELAY11
SBIM
COUNT22,01H
BC
DELAY11
SBIM
COUNT11,01H
BC
DELAY22
SBIM
COUNT33,01H
BC
DELAY33
RTNI
;*************************************************************************
126/126
Ver1.0