You are on page 1of 141

Micom Car Rally Kit

Program
Explanation Manual
kit05.c Version

1.00E Edition
19/06/2006
Japan Micom Car Rally Executive Committee
Program Explanation Manual “kit05.c”

Important Notice

Copyright
*Copyright concerning this manual belongs to the Japan Micom Car Rally Executive Committee.
*This manual is protected by the copyright law and the international copyright agreement.

Prohibited Use
User must not do the following things.
*Selling, advertisement to sell, use, trading, reproduction of this manual for the third party
*Assigning the usage right or approval of this manual to the third party
*Change or remove part or whole of this manual
*Translate this manual without authorization
*Use wherein harm might be caused to life and human body by using the content of this manual

Reprinting, reproduction
Prior approval of Japan Micom Car Rally Executive Committee is necessary for reprinting and reproduction of
this manual.

Liability Restrictions
The information described in this manual is carefully prepared to ensure accuracy. However, Japan Micom Car
Rally Executive Committee does not assume any responsibility, if any damage occurs due to the description
error of this manual.

Other
In this manual the model car controlled by microcomputer is defined as the micom car. The information
contained in this manual represents information at the time of publication of the manual, and Japan Micom Car
Rally Executive Committee is liable to occasionally change this information or specification described in this
manual without prior notice. Refer to the information published on the official homepage
(http://www.mcr.gr.jp/) of Micom Car Rally when manufacturing micon car.

Contact Address
Head Office, Renesas Technology Micom Car Rally Executive Committee
162-0824 Tokyo Shinjukuku, Ageba cho,2-1 Karuko saka, MN Building
TEL (03)-3266-8510
E-mail : official@mcr.gr.jp

- I -
Program Explanation Manual “kit05.c”

Index (Table of contents)


1. Outline.........................................................................................................................................1
2. Specification of Micom car Kit...................................................................................................2
2.1. Appearance......................................................................................................................................................... 2
2.2. Power supply configuration of standard kit ....................................................................................................... 3
2.3. Power supply configuration for raising the drive system voltage. ..................................................................... 4
2.4. Sensor signal ...................................................................................................................................................... 5
2.5. Motor Control .................................................................................................................................................... 8
2.6. Servo Control ................................................................................................................................................... 17
2.7. LED Circuit...................................................................................................................................................... 18
2.8. Switch Circuit .................................................................................................................................................. 19
3. Program Explanation “kit05.c” .................................................................................................20
3.1. Program list ...................................................................................................................................................... 20
3.2. Start .................................................................................................................................................................. 26
3.3. Import of external file(Include)........................................................................................................................ 26
3.4. Definition of I/O register of built-in peripheral function for H8/3048F-ONE ................................................. 27
3.5. Other symbol definitions.................................................................................................................................. 30
3.6. Prototype declaration ....................................................................................................................................... 32
3.7. Global Variable Declaration ............................................................................................................................. 33
3.8. H8/3048F-ONE Built-in function Initialization:init function .......................................................................... 34
3.9. Interrupt setting of ITU0 every 1ms................................................................................................................. 41
3.10. Setting of reset-synchronized PWM mode..................................................................................................... 56
3.11. Waiting: timer function .................................................................................................................................. 69
3.12. Sensor state reading: sensor_inp function ...................................................................................................... 70
3.13. Change bit: bit_change function .................................................................................................................... 74
3.14. Crossline Check: check_crossline function.................................................................................................... 76
3.15. Reading of DIP switch: dipsw_get function................................................................................................... 77
3.16. Readingof push switch: pushsw_get function ................................................................................................ 78
3.17. Control of LED: led_out function .................................................................................................................. 80
3.18. Motor Speed Control: speed function ............................................................................................................ 82
3.19. Servo Steering Operation: handle function .................................................................................................... 87
3.20. Main Program Start ........................................................................................................................................ 88
3.21. About pattern method..................................................................................................................................... 89
3.22. First part of pattern method (while statement and switch statement) ............................................................. 90
3.23. Pattern contents .............................................................................................................................................. 91
3.24. Pattern 0 : Switch input waiting ..................................................................................................................... 93
3.25. Pattern 1 : 1 second waiting after the switch is pushed .................................................................................. 94
3.26. Pattern 11: Normal Trace ............................................................................................................................... 95
3.27. Pattern 12: Check of end of large turn to the right ..................................................................................... 102
3.28. Pattern 13:Check of end of large turn to the left ........................................................................................ 105
3.29. Processing at the time of crossline detection................................................................................................ 108
3.30. Processing at the time of 1st crossline detection ........................................................................................... 109
3.31. Pattern 23 : Trace 1 after crossline ............................................................................................................... 111
3.32. Pattern 31,32 : Left crank clear processing .................................................................................................. 115
3.33. Pattern 41, 42: Right crank clear processing ................................................................................................ 118
3.34. None of the above patterns........................................................................................................................... 121
3.35. Calculation Method for Right and Left Rotation Difference of Motor ........................................................ 122
4. Program Explanation “kit05start.src” .....................................................................................126
- II -
Program Explanation Manual “kit05.c”

4.1. Program List................................................................................................................................................... 126


4.2. “.CPU” command........................................................................................................................................... 127
4.3. Setting of external reference .......................................................................................................................... 127
4.4. Declaration of section V................................................................................................................................. 127
4.5. Declaration of section P ................................................................................................................................. 131
4.6. End ................................................................................................................................................................. 131
5. Program Explanation “kit05.sub” ...........................................................................................132
5.1. Program List................................................................................................................................................... 132
5.2. input Command.............................................................................................................................................. 132
5.3. lib Command.................................................................................................................................................. 132
5.4. output Command............................................................................................................................................ 132
5.5. print Command .............................................................................................................................................. 133
5.6. start Command ............................................................................................................................................... 133
5.7. exit Command ................................................................................................................................................ 134
6. File Conversion Mechanism....................................................................................................135
6.1. File ................................................................................................................................................................. 135
6.2. File Change .................................................................................................................................................... 136
7. Bibliography............................................................................................................................137

- III -
Program Explanation Manual “kit05.c”

1. Outline

This manual explains about Micom car rally kit Vol.3 and drive program kit05.c designed in 2005.

Recently, in the program of the microcomputer, (excluding the cases when timing and the execution speed are
strict), there has been substantial development in the high-level language, especially in C language. It is because
more students learn C language in information process classes, program coded by C language out of hardware can
be used when CPU is replaced, and it is able to perform hardware control better than other languages.
This manual describes how the micom car control program with the development environment moves/controls
CPU and related micom car.

-1-
Program Explanation Manual “kit05.c”

2. Specification of Micom car Kit.


2.1. Appearance

Sensor board Control system


battery box
Motor Drive
board Right gear
PortB

Servo C
B
Sub PortA
board
Port7

CPU board

Left gear
To Servo
A
Driving system
Battry box

Micom car consists of CPU board, sensor board, sensor sub board, motor drive board (Circuit control part) of
control system and right motor, left motor, servo motor of driving system.

SW LED LED

Right motor
Sensor A Sensor B CPU board C Motor drive
board sub board H8/3048F-ONE board Left motor

servo

Black and white of course is read, and it is converted into digital signal of “0" or “1" and
Sensor board
output. Output signal is white: “0", black: “1". 8 sensors are attached.

Output by reversing input signal. Output signal becomes white colour: “1", black colour: “0"
because input signal is signal from sensor. As the output is an open collector output, it is
Sensor sub board
pulled-up in the sub board. Since the signal from sensor board is not very clear, sensor sub
board has implications of wave form shaping and chattering prevention also.
Sensor value is read from port 7, output value of right and left motor and cut angle of servo is
calculated and outputted to motor drive board, connected to port B.
Program is created for finding out how the output value of the motor and the servo is decided
CPU board
from this sensor value.
Port A is not used in standard kit. Therefore, the equipments for tune up such as the
DIPswitch, LED, and the rotary encoders etc. , can be connected.
Weak electric signal from CPU board is converted into strong electric signal to operate the
Motor drive motor. The drive of the servo also uses the power supply for the motor.
board Push switch is connected and the program is created in such that, pushing this switch starts
the micom car. In addition, 2 LED are attached and can be used for debugging.

* Control system (CPU)power supply ...4 pieces of secondary AA batteries (1.2V x 4 nos. =4.8V) are used
*Driving system (motor and servo) power supply ...4 pieces of secondary AA batteries or 4 pieces of alkaline
Battery batteries (1.5V x 4 nos. =6.0V) are used.
ÚCPU power supply shall have the voltage of 5.0V)10%.

-2-
Program Explanation Manual “kit05.c”

2.2. Power supply configuration of standard kit.

In a standard kit, Power supply system is seperated in a control system and drive system and CPU cannot be reset,
regardless of the quantity of the electric current consumed on the motor & servo side.
Power supply configuration of standard kit is as shown in following diagrams.

Right motor
PB
5V
P
A
CPU board Motor drive board
P
7

Left motor
Sensor sub board
5V

It is 5V to the servo.

4 Nos. AA batteries for 4 Nos. AA batteries for


Sensor board 5V 5V
control system power supply driving system power

such as CPU, sensor, and supply such as right and

control system circuit part of left motor, and servo.

motor drive board

Flow of power supply system is shown as follows.

Motor drive board


Sensor 5V Sensor sub 5V 5V Control Driving Right motor
CPU board
board board system system Left motor

circuit circuits Servo

Control system power supply


LM2940-5 LM350

Driving system power supply


5V 5V

Control system Driving system


power supply power supply
4 Nos. AA batteries 4 Nos. AA batteries

-3-
Program Explanation Manual “kit05.c”

2.3. Power supply configuration for raising the drive system voltage.

The rotation speed of the motor can be increased by raising voltage of drive system (If no.of batteries are
increased). If 6 batteries are used for motor power supply, the voltage is 7.2V and if 8 batteries are used then the
voltage will become 9.6V. However, more batteries cannot be added, since the maximum number of batteries that
can be used cannot exceed 8. The battery is common for control system as well as drive system. There is no
problem on the motor side even with 9.6V. However since the certified operation voltage for CPU is between
4.5-5.5V, it will get damaged if exceeded. Three terminal regulators are installed and the control system voltage of
CPU is adjusted to a constant of 5.0V.
However, when battery is common (shared), motor etc. consume large amount of current and if the voltage drops
below 4.5V, the CPU gets reset. Care must be taken while resetting the CPU, when the battery is shared.
When parts of "LM350 addition set" are added, common battery's voltage of 6V or more is used and LM2940-5
generates voltage of 5V that is used by the control system of CPU etc. and LM350 generates voltage of 6V used
by the servo.

6V is made with three terminal regulators for

the control system 5V and servo


5.0V

Right motor
PB
9.6V
P
A Motor drive board
CPU board
P
7 +
LM350 addition set
Left motor

Sensor sub board 9.6V

6V to the servo
9.6V is for the
9.6V secondary battery
Sensor board
use. In case of
8 common powers alkaline battery it
Supply batteries becomes 12V.

Power supply system flow is shown in the following diagram.

Motor drive board


9.6V
Sensor 5.0V Sensor sub 5.0V 5.0V Control Driving Right motor
CPU board 9.6V
Board board system system Left motor

circuit circuits
5.0V
Control system power supply 6V
LM2940-5 LM350 Servo
Driving system power supply
9.6V

Power supply
8 nos. AA batteries

-4-
Program Explanation Manual “kit05.c”

2.4. Sensor signal


*Mechanism (structure)
How is white and black distinguished on the course in the sensor board?.
Actually, the phenomenon of “white reflects light (rays) and black absorbs the
light" is used. Sensors are mainly divided in 2 parts such as part that emits the
light and the part that absorbs the light and focuses light to the course. If a
White
course where light was focused is white, light reflects and is received by the
It reflects
light receiving part. Thus, it is determined “White" and LED (red lamp) glows.
Light is absorbed and is not received by the light receiving part , if a course
where light was focused is black, and is determined as “Black" and LED does
not glow. Incidentally, the light emitted to the course is not seen by naked eyes
(human eyes) as it is infrared light.
Black
The sensitivity can be adjusted with the blue quadrangular part called a volume.
LED is lightened properly on the white line in the course and it is adjusted in
It does not reflect
such a way that it is not visible in a black space. In program kit05.c of the kit, if
LED is adjusted to glow in the gray color it can run properly.
*Principle of operation of photo sensor (S7136) (From the data sheet).

(a) Oscillator, Timing signal generation circuit


The standard starting point output is obtained by the electrical charge and
discharge of a built-in condenser by a fixed current. The oscillation output is
input to the timing signal generation circuit, and pulse for the LED drive and
various timing pulses for the digital signal processing are generated.
(b) LED drive circuit
It is a circuit to drive light emitting diode by the pulse for the LED drive
generated with the timing signal generation circuit. The drive duty ratio is 1/16.
(c) Photo diode and preamp circuit
The photo diode is of a chip type. A photoelectric current of the photo diode is
converted into voltage through the preamp circuit. By using an original exchange
amplifier for the preamp circuit, the timing range corresponding to DC and the
low frequency light interference is expanded, and the signal detection sensitivity
is improved.
(d) C coupling, buffer amplifier, and standard voltage generation circuit
The low frequency light interference is further removed by the C coupling and at
the same time the offset of the preamp is also removed. Amplified to comparator
level with buffer amp, and the comparator level signal is generated by the
standard voltage generation circuit.
(e) Comparator Circuit
The hysteresis function is added to Comparator Circuit, and chattering due to a
slight change of the input light is prevented.
(f) Signal processing circuit
The signal processing circuit is composed of a gate circuit and digital integral
circuit. The gate circuit is a circuit, which distinguishes the pulse of the detection
input at the time of synchronous detection, prevents the malfunction due to the
asynchronous light interference. Moreover, since it is not possible to remove with
the gate circuit; the synchronous light interference has been removed with the
digital integral circuit of latter part.
(g) Output circuit
It is a circuit, which outputs the signal processing circuit output after buffering.

Truth Table

OUTPUT
INPUT
LEVEL
Light ON LOW
Light
HIGH
OFF

-5-
Program Explanation Manual “kit05.c”

*Circuit

To CPU
board

Sensor board Sensor sub board

1.U1 is photo sensor. Light receiving part and oscillation circuit of infrared ray’s emmission LED are combined.
2.Infrared rays emmission LED (LED 2) is connected with 1 pin of U1. The emitted light from LED2 is
received by U1. Output adjustment of infrared rays emmission LED is done by volume VR1.
3.It is 3 pin of U1 which outputs whether or not the light is received. LED (LED1) is connected and it can be
confirmed visually whether it is “0" or “1".
4.When the light of infrared rays emmission LED reaches U1 (The course is white), “0" is output from 3 pin.
LED1 glows because anode side of LED1 is “+" and cathod side is “ –“ .
5.If the infrared rays emmission LED does not reach U1 (The course is a black), “1" is output (Refer to following
no.6 for details). LED1 does not glow because the anode side of LED1 is always “+" and the cathode side
becomes"+".
6.Actually, 3 pin of U1 is open collector output though it has been mentioned that if light does not reach it is “1".
Usually in the digital world, “0"=0V, and “1"=5V. Open collector output is open state except when “0"=0V, and
it is called non-connected state. There cannot be a value which it is neither “0" nor “1" in the digital world.
Therefore, when photo sensor is opened it is made in such a way to become “1" by pull-up resistor (RA11) on
the sensor sub board.
7.Output of 3 pins of U1 becomes “0" when course lights up LED1 in white and becomes “1" output when course
turns off LED1 in black. However, it is reversed by NOT circuit (U11) as it is easy to understand that white="1"
and black="0". While incorporating in CPU white= “1" and black= “0".

-6-
Program Explanation Manual “kit05.c”

*Actual incorporation

c Status under the sensor board is assumed to be “black-white-white-white-black-black-black-black" from left


side.
d Arrangement of the sensors of the sensor board is from the left side with bit 0, 1, 2, 3, 4, 5, 6, and 7. Output
signal is, white: “0" and black: “1". When writing from left side in binary number it becomes bit7, 6,5,4,3,2,1,0,
thus becomes “11110001" (It becomes 0xf1 in hexadecimal number ). The right and left have been reversed.
e Black (LED turning off) is “1" output and white (LED lighting) is “0" output and it is opposite to the common
sense. Then, the signal is reversed on the sensor sub board and then a waveform shaping is done. Signal which
is output from sensor sub board becomes “00001110" ( It becomes 0x0e in hexadecimal number ) and it is the
reversed value of input signal.
f Output from sensor sub board is read from port 7 of CPU board. At this time, bit array right and left appear to
have reversed making difficult to understand. Therefore the bit is replaced by programme (bit_change function).
The change value for right and left is used when using in the program.

b it 0 1 2 3 4 5 6 7
B W W W B B B B
7 6 5 4 3 2 1 0
B B B B W W W B 1

S en so r b o a rd T o di gital va lu e

7 6 5 4 3 2 1 0
1 1 1 1 0 0 0 1 2
(Hexadeci mal no : 0xf1)

In ve rsio n
Se n so r su b
b o ard
7 6 5 4 3 2 1 0
0 0 0 0 1 1 1 0 3
(Hexadeci mal no : 0x0e
C PU Rig ht an d left
b o ard re placem ent

7 6 5 4 3 2 1 0
0 1 1 1 0 0 0 0 4

(Hexadeci mal no : 0x70


P ortB
This value is used in program!!

P o rt7 Po rtA

B: Black
W: White

-7-
Program Explanation Manual “kit05.c”

2.5. Motor Control


CPU Board
Drive syste m Control syste m
B attery Box B attery Box

PortB

Port7 PortA

LED 0
LED 1
Servo SW
M otor
Drive Board

Left Ge ar Right Ge ar

*Role of Motor Drive Board

The motor drive board operates the motor according to the instructions of the microcomputer. The signal “turn
motor, stop motor" from the microcomputer is very weak, and even if the motor is directly connected to that signal
wire, there is no response from the motor. This weak signal is converted to signal which induces a large current of
few hundreds to few thousands mA which is necessary for operating the motor.

× ○
Motor
CPU board
M CPU board
Drive Board M

The motor does not turn because the Convert to large current, motor is
signal is weak. activated!

*Principle of Speed Control

It is OK if voltage is added when motor is to be rotated. If the motor is to be stopped, there is no need to add the
voltage. Then, how can the fine speed adjustment such as intermediate speed or 10%, 20%--etc of the speed be
achieved ? The voltage can be dropped by using the volume. However, the volume which has very large thermal
capacity , is necessary since a large current may flow to the motor. The surplus voltage which is not added to the
motor, is consumed as a large heat radiation on the resistor. Here, repeated ON, OFF operations at high speed
carries out the control as if the intermediate voltage is being outputted. The repeated ON, OFF operations control
the change of ON and OFF ratio under the constant cycle. This is called “Pulse width modulation".
It is abbreviated as PWM control. ON ratio for pulse width is called duty ratio. When the ON width in cycle is
made to 50%, it is called duty ratio 50%. In other words , it is called PWM 50% or simply motor 50%.

-8-
Program Explanation Manual “kit05.c”

Pulse width

ON width OFF width

Duty ratio = ON width/pulse width (ON width+OFF width)


For example, when ON width is 60ms for pulse of 100ms, it becomes,
Duty ratio = 60ms/100ms = 0.6 = 60%
Becomes 100% when all ON and 0% when all OFF.

Though “PWM" feels somewhat difficult to hear, but manual “Connect", “Disconnect" to the motor and battery
wire is also called PWM. The motor rotates fast when the connected time is long. The motor rotates only a little
when the disconnection time is long. “Connect", “Disconnect" operation can be carried out in just few seconds
in case of humans, but in case of microcomputer, it is carried out within a few milliseconds.

Battery
Motor
OFF ON

Let’s assume that the waveform by which 0V and 5V are output is as shown in the figure below. The longer the
ON time for 1 cycle, the average value becomes larger. If all are made 5V then average is also 5V, this is the
maximum voltage.
What does the the average become if the time of turning on is adjusted to 50% as half of 1 cycle? When average is
taken 5V x 0.5 = 2.5V, the voltage appears to have changed.
Thus when ON time is made 90%, 80%, ---0% of 1 cycle, the average voltage goes on decreasing and becomes 0
in the end.
When this signal is connected to the motor, the rotation speed of the motor can be changed a little, and subtle
speed control is possible. The brightness of LED can be changed by PWM when connected with LED. If CPU is
used, this work can be done in the units of microsecond and milliseconds. A very smooth motor control can be
achieved by this micro or milli order control.

Duty ratio Voltage


1 cycle
5V
conversion
25% = 1.25V
0V
5V
50% = 2.5V
0V
5V
75% = 3.75V
0V
5V
100% = 5V
0V
5V
0% = 0V
0V

Why is it that the speed of the motor is controlled by pulse width control and not voltage control? Though the
CPU is good at handling the digital values “0" and “1", analog value like xV is difficult. Hence it behaves as
though voltage is controlled by changing width of “0" and “1".This is PWM control.

-9-
Program Explanation Manual “kit05.c”

*Principle of Normal rotation, Reverse and Brake

In the motor drive board (Vol.3), the “normal rotation, reverse and brake" are controlled. Motor control is
possible by changing the volatge added to the terminal of motor as shown in the figure below.

Operation Terminal 1 Terminal 2


Normal rotation GND connection +connection
Reverse +connection GND connection
Brake GND connection GND connection

1 2 1 2 1 2
M M M

正転
Normal rotation 逆転
Reverse ブレーキ
Brake

*H Bridge Circuit
The actual method is follows. As shown in the following figure, keeping motor in the center, 4 switches are
fixed in H shape. It is called “H bridge circuit" since H is formed.
Normal rotation, reverse and brake are controlled by turning each of these 4 switches ON/OFF.
10V 10V 10V 10V 10V 10V

M M M

正転
Normal rotation 逆転
Reverse ブレー
Brake キ
*The switch of H bridge circuit is made FET.
FET can flow current when voltage is impose on D to S regardless N or P-channel.
This component which changes into the switch is
done by FET. P-channel FET (2SJ type) is used
for plus side of power supply and N-channel FET
(2SK type) is used for minus side.
P-channel FET passes the current between D-S
(drain source) when VG (gate voltage)< VS
(source voltage)
N-channel FET passes the current between D-S
(drain source) when VG (gate voltage)> VS
(source voltage)
That is to say, either N-channel or P-channel, FET
passes the current when the voltage between D-S
is added.

- 10 -
Program Explanation Manual “kit05.c”

Normal rotation, reverse, brake operations are carried out by changing the voltage added to these 4 FET gates.

Reverse Normal rotation

0V 10V 10V 0V

0V 10V 10V 0V

Brake1 Brake 2

0V 0V 10V 10V

0V 0V 10V 10V

Care should be taken that the left and right FET should not be turned ON simultaneously. Since it is connected
without any load to GND from 10V, it issimilar to becoming short. It will however, either burn the FET or line.

If the voltage at 4 gates in above figures, is observed, it is seen that voltage added to P-channel FET and
N-channel FET on the right side, P-channel FET and N-channel FET on the left side is common.Hence, the
following circuit was prepared.

A B Operation
0V 0V Brake
0V 10V Reverse
Normal
10V 0V
rotation
0V 0V Brake
*Power supply voltage of 10V for motor or GND
voltage of 0V is applied to G (gate) terminal.
Precaution must be taken as the control signal “0"
and “1" are different from these gate voltage.

FET became very hot when this circuit was actually joined and the PWM waveform was added and operated. Why
was it so? When the signal is input from the gate of FET, and when ON/OFF operation is carried out between the
drain and source , according to the figure on the left “Ideal waveform", the normal rotation and brake seem to
change smoothly because P-channel FET and N-channel FET respond immediately.
However, there is time delay and they do not actually operate at once. This delay is longer when FET is ON->OFF

- 11 -
Program Explanation Manual “kit05.c”

than when OFF->ON. Hence, as per the figure on the right below “actual waveform", the delay is small but, both
FET become ON and becomes the same as the short status.
理想的な波形
Ideal waveform Actual waveform
実際の波形
Normal Normal
モータ Brake
ブレーキ rotation
正転 Brake
ブレーキ モータ Brake
ブレーキ rotation
正転 Brake
ブレーキ
Motor Motor
Gate
ゲート Gate
ゲート
200ns 87ns
PchFET ON PchFET ON
Operation
動作 OFF Operation
動作 OFF
120ns 225ns
NchFET ON NchFET ON
動作
Operation OFF 動作
Operation OFF

ショート
Short-circuit ショー ト
Short-circuit

Duration of the delay time of turning ON to actually starting reaction is called “Turn on delay time", from turing
ON for the first time to becoming actually ON is called “Rise time". Delay from turning off to actually starting the
action is called “Turn off delay time"and from turing OFF for the first time to becoming actually OFF is called
“Fall time".
Time until actually becoming OFF->ON is “turn on delay time + rise time", time until ON->OFF is “turn on delay
time + fall time". The delay time in the above figure to the right is this time. An electrical characteristic of FET is
shown below for the reference.
2SJ530 (P-channel)

OFF->ON is
delayed by 87ns

ON->OFF is
delayed by 200ns

2SK2869(N-channel)

OFF->ON is
delayed by 120ns

ON->OFF is
delayed by 225ns

- 12 -
Program Explanation Manual “kit05.c”

*Short-circuit prevention circuit of P-channel and N-channel


The solution is not to simultaneously turn ON/ OFF the P -channel FET and N -channel FET on the A side of the
circuit diagram, and to delay some time so as not to short the circuit.

Motor Brake Normal rotation Brake


2 3
C 10V
(PchIN) 0V

1 4
D 10V
(NchIN) 0V

PchFET ON
Operation OFF

NchFET ON
Operation OFF

Both are not turned ON.

The part where this time is delayed is made from integrating circuit. Since there are lots of special books about
integrating circuit, please refer to those.

Delay time can be calculated as,


Time constant T = CR [s].
Here, since it is 9.1k ohm, 4700pF, when calculated, T
= 9.1 x 103 x 4700 x 10-12 = 42.77 [us]

74HC series is considered to be “1"


Input (1 pin of U2) when there is input voltage of 3.5V
or more.
When the waveform was actually
observed, and the time to become
3.5V was measured, it was about
50us. In the “actual waveform"
50us figure, though there is a gap/lapse of
3 pin of U2 only 225ns, delay time of 50us is
created in the integrating circuit.
3.5V This is because, other than FET,
delay time of digital transistor
(Another name:Digital relay)for
voltage conversion, delay due to
capacitance of gate of FET is also
included.

- 13 -
Program Explanation Manual “kit05.c”

The circuit which combines integrating circuit to FET is as follows.

E
A

D
F

Integrating Circuit Voltage conversion FET Motor

Conversion of input 0V-> Output 10V


(open-collector) and
Input 5V-> Output 0V is done with
digital transistor.

Refer to upper figure about A,B,....E,F.


Motor Brake Normal rotation Brake

A "1"(5V)
"0"
50us 50us
B "1"(5V)
"0"
Integrating
Circuit
C "1"(5V)
=AandB "0"

D "1"(5V)
=AorB "0"
10V
E
(PchIN) 0V
Voltage
10V
Conversion
F
(NchIN) 0V

PchFET ON
動作 OFF
FET
NchFET ON
動作 OFF

出力
Motor 0V 10V 0V
電圧

Free Free
Both are not turned ON.

- 14 -
Program Explanation Manual “kit05.c”

* When Changing from Brake->Normal Rotation

1.When signal from port is “0" then brake, when “1" it is normal rotation.Can change from brake to normal
rotation (A point).
2.The waveform which is delayed by 50us by integrating circuit is output from B point.
3.C point outputs waveform of A and B.
4.D point outputs waveform of A or B.
5.E point outputs, the signal for which the voltage is converted with a digital transistor. 0V-5V signal of C point is
converted into 10V-0V signal.
6.F point similarly converts 0V-5V signal of D point into 10V-0V signal.
7.When signal of A is converted from “0" -> “1", the gate of FET2 becomes 10V->0V and FET2 is turned OFF.
However, due to delay time it gets delayed and becomes OFF. At this point, the motor enters a free state because
FET1 and FET2 are OFF.
8.After changing the signal of A, the gate of FET1 becomes 0V->10V this time becomes OFF after 50us. 10V is
added to the motor and does normal rotation.

* When changing from Normal Rotation->Brake

1.When signal from A is changed from “1"-> “0", the gate of FET1 is 10V->0V and FET1 is OFF. However, due
to delay time it gets delayed and switches OFF. At this point, the motor enters a free state because FET1 and
FET2 are OFF.
2.The signal of A is changed and 50us later, the gate of FET2 is 0V->10V and the status of FET2 becomes to
ON.
Thus, when operation is selected, a short circuit can be prevented by making the status of both FET and
turning OFF, free.

*Voltage of 10V added to the gate is an example. Actually it is same as power supply volatge (VBAT)

- 15 -
Program Explanation Manual “kit05.c”

*Circuit of Motor Drive Board

The actual circuit is added the circuit for normal rotation/ reverse change, besides integrating circuit and FET
circuit.The following circuit is for left motor. PB1 is terminal which adds PWM, and PB2 changes normal
rotation/ reverse rotation.
FET for left motor

Integrating circuit

“0": Brake “1": Operate (To the direction of PB2)

“0": normal rotation “1": reverse rotation Digital relay

Gate of Gate of Gate of Gate of CN4 CN4 Motor


A B C
FET1 FET2 FET3 FET4 2 pin 1 pin operation
10V 10V 10V 10V
0 0 0V 0V Brake
(OFF) (ON) (OFF) (ON)
10V 0V 10V 10V Free
0 1 0V Free
(OFF) (OFF) (OFF) (ON) (Open)
0V 0V 10V 10V Normal
1 1 0 10V 0V
(ON) (OFF) (OFF) (ON) Rotation
10V 0V 10V 10V Free
0 1 0V Free
(OFF) (OFF) (OFF) (ON) (Open)
10V 10V 10V 10V
0 0 0V 0V Brake
(OFF) (ON) (OFF) (ON)

Gate of Gate of Gate of Gate of CN4 CN4 Motor


A B C
FET1 FET2 FET3 FET4 2 pin 1 pin operation
10V 10V 10V 10V
0 0 0V 0V Brake
(OFF) (ON) (OFF) (ON)
10V 10V 10V 0V Free
0 1 0V Free
(OFF) (ON) (OFF) (OFF) (Open)
10V 10V 0V 0V
1 1 1 0V 10V Reverse
(OFF) (ON) (ON) (OFF)
10V 10V 10V 0V Free
0 1 0V Free
(OFF) (ON) (OFF) (OFF) (Open)
10V 10V 10V 10V
0 0 0V 0V Brake
(OFF) (ON) (OFF) (ON)
ÚA,B,C:"0"=0V,"1"=5V

- 16 -
Program Explanation Manual “kit05.c”

Ú About Free
To avoid short circuit of PchFET and NchFET, Free is made from integrating circuit. Therefore, it is not
possible carry out Free in the program. Stopping the motor drive board Vol.3 is entirely with brake.
To change Free time, the value of C and R of integrating circuit is changed.

2.6. Servo Control

The servo isadded the pulse of timecycle 16[ms], and the angle of the servo is decided by the ON range of the
pulse. As for the relation between the rotating angle of the servo and the pulse range of turning ON, the relation is
as shown in the figure below. However there may be differences depending on the maker and the type of servo.
0
00度

0 0
Left 90
左90度 右Right
90度 90

0.7m s 1.5m s 2.3m s

16m s 16m s 1 6m s

* Cycle is16[ms]
* The center is ON pulse of 1.5ms and change of servo angle of +-900 is every +-0.8ms.

The PWM signal is generated in a reset synchronous PWM mode of the H8 microcomputer, and the servo is
controlled.

Servo out
Up: When power supply is
directly connected
Down: When 3 terminals are used

1.The PWM signal is output from bit 5 of port B. The ON width changes when the program changes the value of
ITU4_BRB.
2.Though the port and 1 pin of servo can be directly connected but, tentatively, OR circuit which remains as a

- 17 -
Program Explanation Manual “kit05.c”

buffer is incorporated. For instance, by mistake when power supply is connected to 1 pin or the noise mixes and
the terminal breaks, and if it is a direct connection, the port of the microcomputer will break and is fatal. If it is
74HC32, it is 1 for 30 ¥ but CPU is very expensive and since it is 14 pins but CPU is 100 pins, it can be easily
exchanged.
3.2 pin is power supply for the servo. When power supply for the motor is less than 4 batteries, the upper part of
JP1 is bypassed and connects directly with the power supply. In case of more voltage, the ratings of the servo
are exceeded; the voltage is adjusted to constant 6V with 3 terminal regulators which can throw 3A current
LM350. JP1 is short to the down side.

2.7. LED Circuit


3 LED are attached to the motor drive board. Out of those, 2 can be
switched ON/ OFF by microcomputer.
The cathode of LED is connected directly with the port of the
microcomputer. The current limit resistance is 1k ohm.
Forward voltage 2.8V, power supply 20mA can be supplied to TLR123.
Current limit resistance becomes
Resistance = (power supply voltage - voltage added to LED)/ Current
supplied to LED
=(5-2.8)/0.02
=175 ohm

Actually, the resistance of 1k ohm is connected by reducing the battery usage and the current limit of the port.
Current = (Power supply voltage - voltage added to LED)/ resistance
= (5-2.8)/1000=2.2 [mA]

ON! OFF
No current
flow!!

0V 5V

If “0" is output to PB7, the cathode side of If “1" is output to PB7, the cathode side of
LED becomes 0V, the current flows, and LED LED becomes 5V, the potential difference at
glows. both ends of LED becomes 0V, no current
flows and LED does not glow.

- 18 -
Program Explanation Manual “kit05.c”

2.8. Switch Circuit


1 push switch is attached to the motor drive board.

OFF ON

"1" "0"

The switch is pulled up by 10k ohm If the switch is not pushed, “1" is When the switch is pushed, “0" is
and is connected with bit 0 of port input to PB0 through the pull-up input to PB0 through GND.
B. resistor.

- 19 -
Program Explanation Manual “kit05.c”

3. Program Explanation “kit05.c”


3.1. Program list
1 :
/****************************************************************************/
2 :
/* Micom Car Rally Trace Program 2005 Version */
3 :
/* 2005.04 Micom Car Rally Executive Committee */
4 :
/****************************************************************************/
5 :
/*
6 :This kit05.c program connects to motor drive board (Vol.3).
7 :The following contents have been changed with respect to kit04.c.
8 :
- The port connected to CPU board has been changed from J2 (Port A) to J3 (Port B).
9 :
- Motor mode function has been removed (scrapped) (always brake)
10 :
- Motor is able to perform normal rotation, reverse rotation and brake control.
(Reverse rotation was not possible previously)
11 : - 2 step process after crossline detection (Pattern 23, 24)
12 : */
13 :
14 : /*======================================*/
15 : /* Include */
16 : /*======================================*/
17 : #include <machine.h>
18 : #include "h8_3048.h"
19 :
20 : /*======================================*/
21 : /* Symbol definition */
22 : /*======================================*/
23 :
24 : /* Set Constants */
25 : #define TIMER_CYCLE 3071 /* Timer cycle 1ms */
26 : /* When it is to be used by f/8 */
27 : /* f / 8 = 325.5[ns] */
28 : /* Therefore, TIMER_CYCLE */
29 : /* = 1[ms] / 325.5[ns] */
30 : /* = 3072 */
31 : #define PWM_CYCLE 49151 /* PWM cycle 16ms */
32 : /* Therefore, PWM_CYCLE */
33 : /* = 16[ms] / 325.5[ns] */
34 : /* = 49152 */
35 : #define SERVO_CENTER 5000 /* Center value of Servo */
36 : #define HANDLE_STEP 26 /* 1 degree part value */
37 :
38 : /* Mask value setting x: With Mask (Invalid) o:Without mask (Valid) */
39 : #define MASK2_2 0x66 /* xooxxoox */
40 : #define MASK2_0 0x60 /* xooxxxxx */
41 : #define MASK0_2 0x06 /* xxxxxoox */
42 : #define MASK3_3 0xe7 /* oooxxooo */
43 : #define MASK0_3 0x07 /* xxxxxooo */
44 : #define MASK3_0 0xe0 /* oooxxxxx */
45 : #define MASK4_0 0xf0 /* ooooxxxx */
46 : #define MASK0_4 0x0f /* xxxxoooo */
47 : #define MASK1_1 0x81 /* oxxxxxxo */
48 :
49 : /*======================================*/
50 : /* Prototype declaration */
51 : /*======================================*/
52 : void init(void );
53 : void timer( unsigned long timer_set );
54 : int check_crossline( void );
55 : unsigned char sensor_inp( unsigned char mask );
56 : unsigned char dipsw_get( void );
57 : unsigned char pushsw_get( void );
58 : void led_out( unsigned char led );
59 : void speed( int accele_l, int accele_r );
60 : void handle( int angle );
61 : char unsigned bit_change( char unsigned in );
62 :
63 : /*======================================*/
64 : /* Global Variable Declaration */
65 : /*======================================*/
66 : unsigned long cnt0; /* used in 'timer' function */
67 : unsigned long cnt1; /* used in main */
68 :
69 : /************************************************************************/
70 : /* Main program */
71 : /************************************************************************/
72 : void main( void )
73 : {
74 : int i;
75 : int pattern;
76 :
77 : /* Microcomputer function initialization */
78 : init(); /* Initialization */
79 : set_ccr( 0x00 ); /* Whole interrupt enable */
80 :
81 : /* State initialization of micom car */
82 : handle( 0 );

- 20 -
Program Explanation Manual “kit05.c”

83 : speed( 0, 0 );
84 : pattern = 0;
85 : cnt1 = 0;
86 :
87 : while( 1 ) {
88 : switch( pattern ) {
89 :
90 : /*****************************************************************
91 : About pattern
92 : 0: Switch input wait
93 : 1: Wait for 1 second after the switch is pressed.
94 : 11: Usual trace
95 : 12: Check for the end of long turn to the right
96 : 13: Check for the end of long turn to the left
97 : 21: Processing when first crossline is detected
98 : 22: Skip second crossline
99 : 23: Trace 1 after crossline
100 : 24: Trace 2 after crossline, crank detection
101 : 31: Left crank Clear process Waits till it stabilizes
102 : 32: Left crank clear process Check for end of turn
103 : 41: Right crank clear process Waits till it stabilizes
104 : 42: Right crank clear process Check for end of turn
105 : *****************************************************************/
106 :
107 : case 0:
108 : /* Switch input waiting */
109 : if( pushsw_get() ) {
110 : pattern = 1;
111 : cnt1 = 0;
112 : break;
113 : }
114 : if( cnt1 < 100 ) { /* LED blinking process */
115 : led_out( 0x1 );
116 : } else if( cnt1 < 200 ) {
117 : led_out( 0x2 );
118 : } else {
119 : cnt1 = 0;
120 : }
121 : break;
122 :
123 : case 1:
124 : /* 1 second waiting after the switch is pushed */
125 : if( cnt1 < 500 ) {
126 : /* 1.0 seconds starting ago LED1:"OFF" , LED0:"ON" */
127 : led_out( 0x1 );
128 : } else if( cnt1 < 1000 ) {
129 : /* 0.5 seconds starting ago LED1:"ON" , LED0:"OFF" */
130 : led_out( 0x2 );
131 : } else {
132 : /* Start!! */
133 : led_out( 0x0 );
134 : pattern = 11;
135 : cnt1 = 0;
136 : }
137 : break;
138 :
139 : case 11:
140 : /* Usual trace */
141 : if( check_crossline() ) { /* Crossline Check */
142 : pattern = 21;
143 : break;
144 : }
145 : switch( sensor_inp(MASK3_3) ) {
146 : case 0x00:
147 : /* Center -> Straight */
148 : handle( 0 );
149 : speed( 100 ,100 );
150 : break;
151 :
152 : case 0x04:
153 : /* Slightly left inclined -> Slight turn to the right */
154 : handle( 5 );
155 : speed( 100 ,100 );
156 : break;
157 :
158 : case 0x06:
159 : /* Little left inclined -> Small turn to the right */
160 : handle( 10 );
161 : speed( 80 ,69 );
162 : break;
163 :
164 : case 0x07:
165 : /* Left inclined from the middle -> Middle turn to the right */
166 : handle( 15 );
167 : speed( 50 ,40 );
168 : break;
169 :
170 : case 0x03:
171 : /* Large inclined to the left -> Large turn to the right */
172 : handle( 25 );
173 : speed( 30 ,21 );

- 21 -
Program Explanation Manual “kit05.c”

174 : pattern = 12;


175 : break;
176 :
177 : case 0x20:
178 : /* Slightly inclined to the right -> Slight turn to the left */
179 : handle( -5 );
180 : speed( 100 ,100 );
181 : break;
182 :
183 : case 0x60:
184 : /* A little inclined to the right -> Small turn to the left */
185 : handle( -10 );
186 : speed( 69 ,80 );
187 : break;
188 :
189 : case 0xe0:
190 : /* Right inclined from the middle veering -> Middle turn to the left */
191 : handle( -15 );
192 : speed( 40 ,50 );
193 : break;
194 :
195 : case 0xc0:
196 : /* Large inclined to the right -> Large turn to the left */
197 : handle( -25 );
198 : speed( 21 ,30 );
199 : pattern = 13;
200 : break;
201 :
202 : default:
203 : break;
204 : }
205 : break;
206 :
207 : case 12:
208 : /* Check of large turning to the right completion */
209 : if( check_crossline() ) { /* Crossline check even during turning */
210 : pattern = 21;
211 : break;
212 : }
213 : if( sensor_inp(MASK3_3) == 0x06 ) {
214 : pattern = 11;
215 : }
216 : break;
217 :
218 : case 13:
219 : /* Check of large turning completion to the left */
220 : if( check_crossline() ) { /* Crossline check even during large turn */
221 : pattern = 21;
222 : break;
223 : }
224 : if( sensor_inp(MASK3_3) == 0x60 ) {
225 : pattern = 11;
226 : }
227 : break;
228 :
229 : case 21:
230 : /* Process when first crossline is detected */
231 : led_out( 0x3 );
232 : handle( 0 );
233 : speed( 0 ,0 );
234 : pattern = 22;
235 : cnt1 = 0;
236 : break;
237 :
238 : case 22:
239 : /* Second is skipped. */
240 : if( cnt1 > 100 ) {
241 : cnt1 = 0;
242 : pattern = 23;
243 : }
244 : break;
245 :
246 : case 23:
247 : /* 1 of trace after crossline */
248 : if( cnt1 > 300 ) {
249 : cnt1 = 0;
250 : pattern = 24;
251 : break;
252 : }
253 : switch( sensor_inp(MASK3_3) ) {
254 : case 0x00:
255 : /* Center -> Straight */
256 : handle( 0 );
257 : speed( 40 ,40 );
258 : break;
259 : case 0x04:
260 : case 0x06:
261 : case 0x07:
262 : case 0x03:
263 : /* Tending to the left -> Turn to the right */
264 : handle( 8 );

- 22 -
Program Explanation Manual “kit05.c”

265 : speed( 40 ,36 );


266 : break;
267 : case 0x20:
268 : case 0x60:
269 : case 0xe0:
270 : case 0xc0:
271 : /* Tending to the right -> Turn to the left */
272 : handle( -8 );
273 : speed( 36 ,40 );
274 : break;
275 : default:
276 : break;
277 : }
278 : break;
279 :
280 : case 24:
281 : /* 2 of trace after crossline and crank detection */
282 : switch( sensor_inp(MASK3_3) ) {
283 : case 0xe0:
284 : /* Judgment of left crank -> To the left crank clearing process */
285 : led_out( 0x1 );
286 : handle( -38 );
287 : speed( 10 ,50 );
288 : pattern = 31;
289 : cnt1 = 0;
290 : break;
291 : case 0x07:
292 : /* Judgment of right crank -> The process for getting over right crank */
293 : led_out( 0x2 );
294 : handle( 38 );
295 : speed( 50 ,10 );
296 : pattern = 41;
297 : cnt1 = 0;
298 : break;
299 : case 0x00:
300 : /* Center -> Straight */
301 : handle( 0 );
302 : speed( 40 ,40 );
303 : break;
304 : case 0x04:
305 : case 0x06:
306 : case 0x03:
307 : /* Tending to the left -> Turn to the right */
308 : handle( 8 );
309 : speed( 40 ,36 );
310 : break;
311 : case 0x20:
312 : case 0x60:
313 : case 0xc0:
314 : /* Tending to the right -> Turn to the left */
315 : handle( -8 );
316 : speed( 36 ,40 );
317 : break;
318 : default:
319 : break;
320 : }
321 : break;
322 :
323 : case 31:
324 : /* Left crank clear process Wait a little till it becomes stable. */
325 : if( cnt1 > 200 ) {
326 : pattern = 32;
327 : cnt1 = 0;
328 : }
329 : break;
330 :
331 : case 32:
332 : /* Left crank clear process Check of end of turn */
333 : if( sensor_inp(MASK3_3) == 0x60 ) {
334 : led_out( 0x0 );
335 : pattern = 11;
336 : cnt1 = 0;
337 : }
338 : break;
339 :
340 : case 41:
341 : /* Right crank clear processing Waits a little until stabilizing */
342 : if( cnt1 > 200 ) {
343 : pattern = 42;
344 : cnt1 = 0;
345 : }
346 : break;
347 :
348 : case 42:
349 : /* Right crank clear processing Turning completion check */
350 : if( sensor_inp(MASK3_3) == 0x06 ) {
351 : led_out( 0x0 );
352 : pattern = 11;
353 : cnt1 = 0;
354 : }
355 : break;

- 23 -
Program Explanation Manual “kit05.c”

356 :
357 : default:
358 : /* When the pattern is not applied to any case, return to waiting state */
359 : pattern = 0;
360 : break;
361 : }
362 : }
363 : }
364 :
365 : /************************************************************************/
366 : /* H8/3048F-ONE Built in Peripheral Function Initialization */
367 : /************************************************************************/
368 : void init( void )
369 : {
370 : /* I/O port Setting */
371 : P1DDR = 0xff;
372 : P2DDR = 0xff;
373 : P3DDR = 0xff;
374 : P4DDR = 0xff;
375 : P5DDR = 0xff;
376 : P6DDR = 0xf0; /* DIP SW on CPU board */
377 : P8DDR = 0xff;
378 : P9DDR = 0xf7; /* Communication Port */
379 : PADDR = 0xff;
380 : PBDR = 0xc0;
381 : PBDDR = 0xfe; /* Motor Drive Board Vol.3 */
382 : /* As P7 of the sensor board is an exclusive input, there are no input output settings. */
383 :
384 : /* ITU0 Interrupt at every 1ms */
385 : ITU0_TCR = 0x23;
386 : ITU0_GRA = TIMER_CYCLE;
387 : ITU0_IER = 0x01;
388 :
389 : /* ITU3, 4 reset-synchronized PWM mode for right-left motor and servo */
390 : ITU3_TCR = 0x23;
391 : ITU_FCR = 0x3e;
392 : ITU3_GRA = PWM_CYCLE; /* Setting of cycle */
393 : ITU3_GRB = ITU3_BRB = 0; /* PWM Setting of left motor*/
394 : ITU4_GRA = ITU4_BRA = 0; /* PWM Setting of right motor*/
395 : ITU4_GRB = ITU4_BRB = SERVO_CENTER; /* PWM Setting of servo */
396 : ITU_TOER = 0x38;
397 :
398 : /* Count start of ITU */
399 : ITU_STR = 0x09;
400 : }
401 :
402 : /************************************************************************/
403 : /* ITU0 Interrupt process */
404 : /************************************************************************/
405 : #pragma interrupt( interrupt_timer0 )
406 : void interrupt_timer0( void )
407 : {
408 : ITU0_TSR &= 0xfe; /* Flag clear */
409 : cnt0++;
410 : cnt1++;
411 : }
412 :
413 : /************************************************************************/
414 : /* Timer main unit */
415 : /* Argument Timer value 1=1ms */
416 : /************************************************************************/
417 : void timer( unsigned long timer_set )
418 : {
419 : cnt0 = 0;
420 : while( cnt0 < timer_set );
421 : }
422 :
423 : /************************************************************************/
424 : /* Sensor state detection */
425 : /* Argument Mask value */
426 : /* Return value Sensor value */
427 : /************************************************************************/
428 : unsigned char sensor_inp( unsigned char mask )
429 : {
430 : unsigned char sensor;
431 :
432 : sensor = P7DR;
433 :
434 : /* Since bit0 is for left and bit7 is for right in the new sensor board which is reversed */
435 : /* to the previous sensor board, the bit has been replaced to maintain compatibility. */
436 : sensor = bit_change( sensor ); /* Bit replacement */
437 : sensor &= mask;
438 :
439 : return sensor;
440 : }
441 :
442 : /************************************************************************/
443 : /* Crossline detection processing */
444 : /* Return value 0: no crossline 1: crossline exists */
445 : /************************************************************************/
446 : int check_crossline( void )

- 24 -
Program Explanation Manual “kit05.c”

447 : {
448 : unsigned char b;
449 : int ret;
450 :
451 : ret = 0;
452 : b = sensor_inp(MASK2_2);
453 : if( b==0x66 || b==0x64 || b==0x26 || b==0x62 || b==0x46 ) {
454 : ret = 1;
455 : }
456 : return ret;
457 : }
458 :
459 : /************************************************************************/
460 : /* Reading of DIP switch value */
461 : /* Return value Switch value 0 - 15 */
462 : /************************************************************************/
463 : unsigned char dipsw_get( void )
464 : {
465 : unsigned char sw;
466 :
467 : sw = ~P6DR; /* Reading of DIP switch */
468 : sw &= 0x0f;
469 :
470 : return sw;
471 : }
472 :
473 : /************************************************************************/
474 : /* Push switch value reading */
475 : /* Return value Switch value ON: "1" and OFF: "0" */
476 : /************************************************************************/
477 : unsigned char pushsw_get( void )
478 : {
479 : unsigned char sw;
480 :
481 : sw = ~PBDR; /* Reading of port having a push switch */
482 : sw &= 0x01;
483 :
484 : return sw;
485 : }
486 :
487 : /************************************************************************/
488 : /* LED control */
489 : /* Argument Switch value (LED1,LED0)=(bit1,bit0) "0":OFF , "1":ON */
490 : /* Example 0x3->(LED1,LED0)=(ON,ON) : 0x2->(LED1,LED0)=(ON,OFF) */
491 : /************************************************************************/
492 : void led_out( unsigned char led )
493 : {
494 : unsigned char data;
495 :
496 : led = ~led;
497 : led <<= 6;
498 : data = PBDR & 0x3f;
499 : PBDR = data | led;
500 : }
501 :
502 : /************************************************************************/
503 : /* Speed Control */
504 : /* Argument Left motor: -100 - 100 , Right motor: -100 - 100 */
505 : /* 0:Stop,100:normal rotation 100%,-100:Reverse 100% */
506 : /************************************************************************/
507 : void speed( int accele_l, int accele_r )
508 : {
509 : unsigned char sw_data;
510 : unsigned long speed_max;
511 :
512 : sw_data = dipsw_get() + 5; /* DIP switch read */
513 : speed_max = (unsigned long)(PWM_CYCLE-1) * sw_data / 20;
514 :
515 : /* Left motor */
516 : if( accele_l >= 0 ) {
517 : PBDR &= 0xfb;
518 : ITU3_BRB = speed_max * accele_l / 100;
519 : } else {
520 : PBDR |= 0x04;
521 : accele_l = -accele_l;
522 : ITU3_BRB = speed_max * accele_l / 100;
523 : }
524 :
525 : /* Right motor */
526 : if( accele_r >= 0 ) {
527 : PBDR &= 0xf7;
528 : ITU4_BRA = speed_max * accele_r / 100;
529 : } else {
530 : PBDR |= 0x08;
531 : accele_r = -accele_r;
532 : ITU4_BRA = speed_max * accele_r / 100;
533 : }
534 : }
535 :
536 : /************************************************************************/
537 : /* Servo steering operation */

- 25 -
Program Explanation Manual “kit05.c”

538 : /* Argument Servo operation angle: -90 ~ 90 */


539 : /* -90:90 to the left turn , 0:straight ,90:90 to the right turn */
540 : /************************************************************************/
541 : void handle( int angle )
542 : {
543 : ITU4_BRB = SERVO_CENTER - angle * HANDLE_STEP;
544 : }
545 :
546 : /************************************************************************/
547 : /* Bit change */
548 : /* Argument Value to be changed */
549 : /* RReturn value Changed value */
550 : /************************************************************************/
551 : char unsigned bit_change( char unsigned in )
552 : {
553 : unsigned char ret;
554 : int i;
555 :
556 : for( i=0; i<8; i++ ) {
557 : ret >>= 1; /* Right shift of return value */
558 : ret |= in & 0x80; /* Ret bit7 = in bit7 */
559 : in <<= 1; /* Left shift of argument */
560 : }
561 : return ret;
562 : }
563 :
564 : /************************************************************************/
565 : /* end of file */
566 : /************************************************************************/

3.2. Start

1 :/****************************************************************************/
2 :/* Micom car Rally Trace Program 2005 Version */
3: /*2005.04 Micom car Rally Executive Committee */
4 :/****************************************************************************/

Initial section is comment field. Wavy line [/*] is the beginning of the comment and [*/]is the end of the comment.
Since the text between the start of the comment sign and end is ignored, it is convenient to write the note for a
program.

3.3. Import of external file(Include)

14 : /*======================================*/
15 : /* Include */
16 : /*======================================*/
17 : #include <machine.h>
18 : #include "h8_3048.h"

“#include” is the include command. Include command is for importing the external file. Imported file is enclosed
within [< >] or [“ ”] and described in that. Here, “machine.h” and “h8_3048.h” are imported. Difference between
[< >] and [ ““ ] is as follows.

The file to be imported is searched in the usual include folder.


<>
File prepared by the compiler (development environment) is used here.
The current folder is searched first (folder with C program file) and if file is not available here, the
“ ”
file to be imported is searched in the usual include folder. Automatically created file is used here.

17 : #include <machine.h>
First, “machine.h” file is read. This file is a built-in function, which offers specialized function in CPU, which
cannot be described by C language.

- 26 -
Program Explanation Manual “kit05.c”

18: #include "h8_3048.h"


Next, "h8_3048.h" file is read. This file defines the symbols of H8/3048F port. Lots of the parts in each program
are common which makes the program lengthy and long size of the program. Hence the separate file, like this, is
prepared.

3.4. Definition of I/O register of built-in peripheral function for H8/3048F-ONE

I/O register definitions are described in "h8_3048.h" file.

1 : /**************************************************************************************/
2 :/*Definition of I/O register of built-in peripheral function for H8/3048F-ONE Ver1.02 */
3 : /* 2005.04 Micom car Rally Executive Committee */
4 : /**************************************************************************************/
5 :
6 : #define P1DDR (*(unsigned char*)0xfffc0)
7 : #define P1DR (*(unsigned char*)0xfffc2)
8 : #define P2DDR (*(unsigned char*)0xfffc1)
9 : #define P2DR (*(unsigned char*)0xfffc3)
10 : #define P3DDR (*(unsigned char*)0xfffc4)
11 : #define P3DR (*(unsigned char*)0xfffc6)
12 : #define P4DDR (*(unsigned char*)0xfffc5)
13 : #define P4DR (*(unsigned char*)0xfffc7)
14 : #define P5DDR (*(unsigned char*)0xfffc8)
15 : #define P5DR (*(unsigned char*)0xfffca)
16 : #define P6DDR (*(unsigned char*)0xfffc9)
17 : #define P6DR (*(unsigned char*)0xfffcb)
18 : #define P7DR (*(unsigned char*)0xfffce)
19 : #define P8DDR (*(unsigned char*)0xfffcd)
20 : #define P8DR (*(unsigned char*)0xfffcf)
21 : #define P9DDR (*(unsigned char*)0xfffd0)
22 : #define P9DR (*(unsigned char*)0xfffd2)
23 : #define PADDR (*(unsigned char*)0xfffd1)
24 : #define PADR (*(unsigned char*)0xfffd3)
25 : #define PBDDR (*(unsigned char*)0xfffd4)
26 : #define PBDR (*(unsigned char*)0xfffd6)
27 : #define P2PCR (*(unsigned char*)0xfffd8)
28 : #define P4PCR (*(unsigned char*)0xfffda)
29 : #define P5PCR (*(unsigned char*)0xfffdb)
30 :
31 : #define ITU_STR (*(unsigned char*)0xfff60)
32 : #define ITU_SNC (*(unsigned char*)0xfff61)
33 : #define ITU_MDR (*(unsigned char*)0xfff62)
34 : #define ITU_FCR (*(unsigned char*)0xfff63)
35 : #define ITU_TOER (*(unsigned char*)0xfff90)
36 : #define ITU_TOCR (*(unsigned char*)0xfff91)
37 :
38 : #define ITU0_CNT (*(unsigned int *)0xfff68)
39 : #define ITU0_TCR (*(unsigned char*)0xfff64)
40 : #define ITU0_GRA (*(unsigned int *)0xfff6a)
41 : #define ITU0_GRB (*(unsigned int *)0xfff6c)
42 : #define ITU0_IER (*(unsigned char*)0xfff66)
43 : #define ITU0_TSR (*(unsigned char*)0xfff67)
44 : #define ITU0_TIOR (*(unsigned char*)0xfff65)
45 :
46 : #define ITU1_CNT (*(unsigned int *)0xfff72)
47 : #define ITU1_TCR (*(unsigned char*)0xfff6e)
48 : #define ITU1_GRA (*(unsigned int *)0xfff74)
49 : #define ITU1_GRB (*(unsigned int *)0xfff76)
50 : #define ITU1_IER (*(unsigned char*)0xfff70)
51 : #define ITU1_TSR (*(unsigned char*)0xfff71)
52 : #define ITU1_TIOR (*(unsigned char*)0xfff6f)
53 :
54 : #define ITU2_CNT (*(unsigned int *)0xfff7c)
55 : #define ITU2_TCR (*(unsigned char*)0xfff78)
56 : #define ITU2_GRA (*(unsigned int *)0xfff7e)
57 : #define ITU2_GRB (*(unsigned int *)0xfff80)
58 : #define ITU2_IER (*(unsigned char*)0xfff7a)
59 : #define ITU2_TSR (*(unsigned char*)0xfff7b)
60 : #define ITU2_TIOR (*(unsigned char*)0xfff79)
61 :
62 : #define ITU3_CNT (*(unsigned int *)0xfff86)
63 : #define ITU3_TCR (*(unsigned char*)0xfff82)
64 : #define ITU3_GRA (*(unsigned int *)0xfff88)
65 : #define ITU3_GRB (*(unsigned int *)0xfff8a)
66 : #define ITU3_BRA (*(unsigned int *)0xfff8c)
67 : #define ITU3_BRB (*(unsigned int *)0xfff8e)
68 : #define ITU3_IER (*(unsigned char*)0xfff84)
69 : #define ITU3_TSR (*(unsigned char*)0xfff85)
70 : #define ITU3_TIOR (*(unsigned char*)0xfff83)

71 :

- 27 -
Program Explanation Manual “kit05.c”

72 : #define ITU4_CNT (*(unsigned int *)0xfff96)


73 : #define ITU4_TCR (*(unsigned char*)0xfff92)
74 : #define ITU4_GRA (*(unsigned int *)0xfff98)
75 : #define ITU4_GRB (*(unsigned int *)0xfff9a)
76 : #define ITU4_BRA (*(unsigned int *)0xfff9c)
77 : #define ITU4_BRB (*(unsigned int *)0xfff9e)
78 : #define ITU4_IER (*(unsigned char*)0xfff94)
79 : #define ITU4_TSR (*(unsigned char*)0xfff95)
80 : #define ITU4_TIOR (*(unsigned char*)0xfff93)
81 :
82 : #define SCI0_SMR (*(unsigned char*)0xfffb0)
83 : #define SCI0_BRR (*(unsigned char*)0xfffb1)
84 : #define SCI0_SCR (*(unsigned char*)0xfffb2)
85 : #define SCI0_TDR (*(unsigned char*)0xfffb3)
86 : #define SCI0_SSR (*(unsigned char*)0xfffb4)
87 : #define SCI0_RDR (*(unsigned char*)0xfffb5)
88 : #define SCI0_SCMR (*(unsigned char*)0xfffb6)
89 :
90 : #define SCI1_SMR (*(unsigned char*)0xfffb8)
91 : #define SCI1_BRR (*(unsigned char*)0xfffb9)
92 : #define SCI1_SCR (*(unsigned char*)0xfffba)
93 : #define SCI1_TDR (*(unsigned char*)0xfffbb)
94 : #define SCI1_SSR (*(unsigned char*)0xfffbc)
95 : #define SCI1_RDR (*(unsigned char*)0xfffbd)
96 :
97 : #define AD_DRA (*(unsigned int *)0xfffe0)
98 : #define AD_DRB (*(unsigned int *)0xfffe2)
99 : #define AD_DRC (*(unsigned int *)0xfffe4)
100 : #define AD_DRD (*(unsigned int *)0xfffe6)
101 : #define AD_CSR (*(unsigned char*)0xfffe8)
102 : #define AD_CR (*(unsigned char*)0xfffe9)

Hereafter, abbreviation

“#define” is a definition function and defines the replacement of a certain character string with other character
strings.
For example,

#define PADDR (*(unsigned char*)0xfffd1)

Meaning of the above is that whenever “PADDR” character string occurs in the program it is replaced with
“(*(unsigned char*)0xfffd1)” character string. During the program, the compiler executes the program by
converting

PADDR = 0xff;

to

(*(unsigned char*)0xfffd1) = 0xff;

I/O register, which controls built-in peripheral functions including these I/O ports, is allocated in the address
identically of ROM or RAM. This is the part indicated by 0xfff1c-0xfffff block numbers which are the I/O
registers in the following diagram.
For instance, “1010 0101” signal is output from the output pin of port A if 0xa5(“1010 0101” written in binary
number) is written in 0xfffd3 block number, when 0xfffd3 block number is defined with PADR and set for the
output.
Since it becomes difficult to understand if all block numbers are directly specified by numerical values, it is
defined that “PADR is the value of 0xfffd3 block number”.
Note:0xfffd3 block number means address 0xfffd3.

- 28 -
Program Explanation Manual “kit05.c”

If it is to be illustrated by diagram it is as follows.


0 1ffff fef10 fff0f fff1c fffff

Flash ROM RAM I/O


Memory
(128kB) (4kB) Register

・ROM, RAM, I/O register all are memory.


・0xfff1c-0xfffff block number is used as a built-in peripheral function.
・For instance, 0xfffd3 block number has been decided as PADR. fff1c … fffd3 … fffff
・Terminal of this block number and port A are directly connected.
・Read and write of port A is same as read and write of 0xfffd3.
・For instance, when 0xa5 is written in 0xfffd3 block number, 0xa5 is output PADR 1 0 1 0 0 1 0 1
from the terminal of port A.

PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0

5V 0V 5V 0V 0V 5V 0V 5V

Voltage output from terminal


Meaning of “(*(unsigned char*)0xfffd3)”

#define PADR 0xfffd3 PADR is the value called 0xfffd3.

#define PADR *0xfffd3 PADR is the 0xfffd3 block number.

‚
PADR is 8-bit width 0xfffd3 block number without code
#define PADR (unsigned char*)0xfffd3
(unsigned char).
‚
PADR is value of 8-bit width 0xfffd3 block number without
#define PADR *(unsigned char*)0xfffd3
code.
‚

#define PADR (*(unsigned char*)0xfffd3) It is the same, but is entirely enclosed by parentheses.

Finally, it is entirely enclosed by parentheses. Why?


For instance, if it is

PADR++;

it gets converted to

(*(unsigned char*)0xfffd3) ++;

This means add 1 to 0xfffd3 block number value (increment the value 0xfffd3 block number by 1).
However, if it not entirely enclosed by parentheses, it becomes

*(unsigned char*)0xfffd3++;

And it means add 1 to 0xfffd3 block number (increment 0xfffd3 block number by 1). As 0xfffd3 block number is a

- 29 -
Program Explanation Manual “kit05.c”

fixed value, there is an error.


Thus, there is a convention to enclose all contents defined by # define in parentheses so that it can be understood
as to which operation is to be performed on which part according to the priority level of the calculation.

All the unused I/O registers other than the I/O register used for micom car control are covered in detail in
h8_3048.h. For details look up the meaning of register in “H8/3048 series and H8/3048F-ZTATTM
(H8/3048F,H8/3048F-ONE) hardware manual”.

3.5. Other symbol definitions

20 : /*======================================*/
21 : /* Symbol definitions */
22 : /*======================================*/
23 :
24 : /* Constant settings */
25 : #define TIMER_CYCLE 3071 /* Timer cycle 1ms */
26 : /* When it is to be used by φ / 8 */
27 : /* φ / 8 = 325.5[ns] */
28 : /* Therefore, TIMER_CYCLE = */
29 : /* = 1[ms] / 325.5[ns] */
30 : /* = 3072 */
31 : #define PWM_CYCLE 49151 /* PWM cycle 16ms */
32 : /* Therefore, PWM_CYCLE */
33 : /* = 16[ms] / 325.5[ns] */
34 : /* = 49152 */
35 : #define SERVO_CENTER 5000 /* Center value of Servo */
36 : #define HANDLE_STEP 26 /* 1 degree part value */
37 :
38 : /* Mask value setting x: With Mask (Invalid) o:Without mask (Valid) */
39 : #define MASK2_2 0x66 /* */
40 : #define MASK2_0 0x60 /* */
41 : #define MASK0_2 0x06 /* */
42 : #define MASK3_3 0xe7 /* */
43 : #define MASK0_3 0x07 /* */
44 : #define MASK3_0 0xe0 /* */
45 : #define MASK4_0 0xf0 /* */
46 : #define MASK0_4 0x0f /* */
47 : #define MASK1_1 0x81 /* */

- 30 -
Program Explanation Manual “kit05.c”

Timer cycle is a value to set 1[ms] time in ITU0. ITU marks (ticks) the time with the
operation clock of CPU.
As RY3048F-ONE board crystal is 24.576[MHz], 1 clock cycle becomes 1÷(24.576 106) =
40.69[ns].
TIMER_CYCLE In addition, clock is divided, and is equipped with the function to lower the frequency and
this time it is used by dividing it in 8. Therefore, the value to be counted to set the time to
1[ms] is (1 10-3) ÷ (40.69 10-9) ÷ 8 = 3,072.
Value to be set becomes 3,071, because it must be less by 1 count than the calculated value.
Described later.

PWM cycle is added to Right motor, Left motor and servo. The PWM cycle is of 16[ms]. And
becomes
PWM_CYCLE
(16 10-3) ÷ (40.69 10-9) ÷ 8 = 49,152
Value to be set becomes 49,151. Described later.

Servo Center is a value when the servo faces straight. It is straight when
the ON pulse is just about 1.5[ms].( Refer to on page17) And becomes
(1.5 10-3) ÷ (40.69 10-9) ÷ 8 = 4,608
However, the value of servo center is different in all micom cars due to
SERVO_CENTER the impact of error in the servo itself or the engagement with jagged shaft
which is inserted in the servo horn, etc. To illustrate, it is like human
fingerprints;each is unique. Therefore, here, the value is limited
temrorarily to 5,000. This value is adjusted or changed to face straight SServo horn
when going straight.
Steering step of servo is the increasing or decreasing value when the servo moves by 1
degree. As it is a pulse of 2.3ms when the servo turns at 900 to the right, it is
(2.3 10-3) ÷ (40.69 10-9) ÷ 8 = 7,065
As it is a pulse of 2.3ms when the servo turns at 900 to the left, it is
(0.7 10-3) ÷ (40.69 10-9) ÷ 8 = 2,150
HANDLE_STEP (Right 900) − (left 900) = 7,065-2,150 = 4,915 is a value which moves by 180 degrees. If it is
divided by 180, the counter value per 1 degree can be found out.
4,915 ÷ 180 = 27
When value is accurately calculated 27 is the value of the servo 1 degree. However, it was 26
in the previous kit04.c program and has been kept as 26 taking into consideration the
compatibility with the previous program.
It is a value, which masks the status of the sensor. It means that in case of “Mask _0”,
number of left sensors and 0 number of right sensors are enabled and the others are masked.
MASK2_2
As “Mask2_2” is defined as “0x66”, bit6, 5, 2, 1 are maintained as they are in “0110 0110”
binary numbers and the others are forcibly set to “0”.
As “Mask2_0” is defined as “0x60”, bit6, 5, are maintained as they are in “0110 0000” binary
MASK2_0
numbers and the others are forcibly set to “0”.
As “Mask0_2” is defined as “0x06”, bit2, 1 are maintained as they are in “0000 0110” binary
MASK0_2
numbers and the others are forcibly set to “0”.
As “Mask3_3” is defined as “0xe7”, bit7, 6, 5, 2, 1, 0 are maintained as they are in “1110
MASK3_3
0111” binary numbers and the others are forcibly set to “0”.
As “Mask0_3” is defined as “0x07”, bit2, 1, 0, are maintained as they are in “0000 1110”
MASK0_3
binary numbers and the others are forcibly set to “0”.
As “Mask3_0” is defined as “0xe0”, bit7, 6, 5, are maintained as they are in “1110 0000”
MASK3_0
binary numbers and the others are forcibly set to “0”.
As “Mask4_0” is defined as “0xf0”, bit7, 6, 5, 4 are maintained as they are in “1111 0000”
MASK4_0
binary numbers and the others are forcibly set to “0”.
As “Mask0_4” is defined as “0x0f”, bit3, 2, 1, 0 are maintained as they are in “0000 1111”
MASK0_4
binary numbers and the others are forcibly set to “0”.
As “Mask1_1” is defined as “0x81”, bit7, 0 are maintained as they are in “1000 0001” binary
MASK1_1
numbers and the others are forcibly set to “0”.
Mask is described later.

- 31 -
Program Explanation Manual “kit05.c”

3.6. Prototype declaration

49 : /*======================================*/
50 : /* Prototype declaration */
51 : /*======================================*/
52 : void init( void );
53 : void timer( unsigned long timer_set );
54 : int check_crossline( void );
55 : unsigned char sensor_inp( unsigned char mask );
56 : unsigned char dipsw_get( void );
57 : unsigned char pushsw_get( void );
58 : void led_out( unsigned char led );
59 : void speed( int accele_l, int accele_r );
60 : void handle( int angle );
61 : char unsigned bit_change( char unsigned in );

Prototype declaration is the declaration that checks the number and type of arguments of the user defined
function before its use. Function prototype is as the function definition which is added “;” at the end of the
statement. Function prototype declares following information.
Function “speed”
Return value void
First argument of int type
Second argument of int type

void speed( int accele_l, int accele_r ); Ends with “;”.

As explained above, prototype declaration is used for checking number and type of function arguments. Thus, the
dummy argument name is not necessary. The following definition is also allowed.

void speed( int , int );

But the dummy argument name helps to decode the program while reading. It is a good practice to write it.
Moreover, just remember that "copy the function declaration line as it is and add a semicolon at the end" should
be done instead of writing the dummy argument names explicitly so that errors will be reduced.
Naturally, function 'speed' should have same arguments and return value as that of the prototype declaration.
The following sample is the function definition of the speed function.
void speed( int accele_l, int accele_r )
{
Program;
}

In this way, all the user-defined functions are declared in the prototype declaration.

- 32 -
Program Explanation Manual “kit05.c”

Though it is an error if the function is called without prototype declaration, the compiler executes strange
operation without noticing the error, as it does not understand whether function argument and return value are
correct. For that, function name, arguments and return type are declared in advance so that compiler understands
that the function exists. (Figure below)

void test( int *a ); <- Prototype Declaration


void main( void ) void main( void )
{ {
int i; int i;
i = 10; Compiled i = 10; Compile
test( i ); though there test( i ); error!
} }
is an error
void test( int *a ) void test( int *a )
{ {
printf(“%d¥n”,*a ); printf(“%d¥n”,*a );
} }

The correct way is “test(&i );".

3.7. Global Variable Declaration


63 : /*======================================*/
64 : /* Global Variable Declaration */
65 : /*======================================*/
66 : unsigned long cnt0; /* used in ‘timer’ function */
67 : unsigned long cnt1; /* used in main */

Global variables are declared outside the function and can be referred from any function. General variables
declared inside the function are called as local variables and only that function can refer to it. An example is
illustrated below.

void a( void ); /* Prototype Declaration */

int timer; /* Global Variable */

void main( void )


{
int i;

timer = 0;
i = 10;
printf(“%d¥n”,timer ); <- 0 is displayed
a();
printf(“%d¥n”,timer ); <-Value 20 set in the function a
is displayed as timer is a global variable
printf(“%d¥n”,i ); <-Value 10 set in this function is displayed
as ‘i’is also declared in function ‘a’ but
is not changed as it is a local variable
}

void a( void )
{
int i;
i = 20;
timer = i;
}

- 33 -
Program Explanation Manual “kit05.c”

kit05.c Two global variables are declared in the program.

Variable Name Type Usage Method

cnt0 unsigned long Used to count 1 ms in the timer function.

cnt1 unsigned long Used by programmer to set the timing in the program.

Detailed contents are explained in respective functions.

If the program is explained sequentially, ‘main’ function comes first but here it is explained last.

3.8. H8/3048F-ONE Built-in function Initialization: init function

The functions built-in H8/3048F-ONE microcomputer are initialized here.’init’ is the abbreviation for ‘initialize’
and means initialization.

365 : /************************************************************************/
366 : /* H8/3048F-ONE Built in Peripheral Function Initialization */
367 : /************************************************************************/
368 : void init( void )
369 : {
370 : /* I/O port Setting */
371 : P1DDR = 0xff;
372 : P2DDR = 0xff;
373 : P3DDR = 0xff;
374 : P4DDR = 0xff;
375 : P5DDR = 0xff;
376 : P6DDR = 0xf0; /* DIP SW on CPU Board */
377 : P8DDR = 0xff;
378 : P9DDR = 0xf7; /* Communication Port */
379 : PADDR = 0xff;
380 : PBDR = 0xc0;
381 : PBDDR = 0xfe; /* Motor Drive Board Vol.3 */
382 : /* As P7 of the sensor board is an exclusive input, there are no input output settings. */

I/O port is the abbreviation for Input/Output Port and it means the ‘port where input and output is carried out’. In
this context it is ‘a place where input and output operations are carried out collectively’ .I/O port input output
settings are set at the start of the program.

There are 11 I/O ports from 1 ~ B and basically they are 8 bit ports but there are some ports with less than 8 bits.
The ports from 1-B are composed of DDR and DR registers respectively and input-output direction settings, and
input and output of data is performed one by one. But the port 7 is an exclusive input port and there is no P7DDR.
Port 7 is necessarily an input port.

- 34 -
Program Explanation Manual “kit05.c”

The connector and port location of RY3048F-ONE board is shown below.

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
P84 P82 P80 P65 NMI P60 P52 P50 P26 P24 P22 P20 P16 P14 P12 P10 GND Ex, P84 indicate bit 4 of
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 the port 8
V cc P83 P81 P66 P64 *RES P53 P51 P27 P25 P23 P21 P17 P15 P13 P11 P37

1 Vcc 2 P36

3 P35 4 P34

5 P33 6 P32

7 P31 8 P30

9 P47 10 P46

11 P45 12 P44

13 P43 14 P42

15 P41 16 P40

17 P94 18 P92

19 P90 20 GND

1 Vcc 2 PB7

3 PB6 4 PB5

5 PB4 6 PB3

7 PB2 8 PB1

9 PB0 10 GND

9 7 5 3 1 9 7 5 3 1
P70 P72 P74 P76 Vcc PA0 PA2 PA4 PA6 Vcc
10 8 6 4 2 10 8 6 4 2
GND P71 P73 P75 P77 GND PA1 PA3 PA5 PA7

T The number of bits of each port


Number of Number of
Port Port
bits bits
1 8 7 8 (input only)
2 8 8 5
3 8 9 6
4 8 A 8
5 4 B 8
6 7

Input-Output Pin: 78 bits, Exclusive Input Pin: 8 bits


But P63 ~ 0 are used for DIPswitch and P91, P93, P95 are used for communication.

- 35 -
Program Explanation Manual “kit05.c”

Input-Output Setting

381 : PBDDR = 0xfe; /* Motor Drive Board Vol.3 */

Explaining port B as an example, PB in the PBDDR means port B. There are P1DDR ~ PBDDR. The part in
changes from 1 to B. However, there is no P7DDR.
DDR is the abbreviation for ‘Data Direction Register’ and it decides the input-output direction. Input –output
setting is done using 1 bit.
Corresponding Bit”0”: Input Pin (Default Value)
Corresponding Bit “1”: Output Pin

Bit: 7 6 5 4 3 2 1 0
Input-Output Input-Output Input-Output Input-Output Input-Output Input-Output Input-Output Input-Output
PBDDR: Setting of Setting of Setting of Setting of Setting of Setting of Setting of Setting of
port bit 7 port bit 6 port bit 5 port bit 4 port bit 3 port bit 2 port bit 1 port bit 0

Motor drive board is connected to port B. Confirm the connective conditions for each bit.
Pin Signal, DDR
Details “0” “1” Input-output
No. Direction Value
1 − + 5V
2 Board <-PB7 LED1 ON OFF Output 1
3 Board<-PB6 LED0 ON OFF Output 1
4 Board<-PB5 Servo Signal PWM Signal Output 1
5 Board<-PB4 Right Motor PWM Stop PWM Motion Output 1
Right Motor Rotational Normal Reverse
6 Board<-PB3 Output 1
Direction Rotation Rotation
Left Motor Rotational Normal Reverse
7 Board<-PB2 Output 1
Direction Rotation Rotation
8 Board<-PB1 Left Motor PWM Stop PWM Motion Output 1
9 Board->PB0 Push Button Pushed Not Pushed Input 0
10 − GND

Bit 0 is input setting and others are output setting. As input is set to ‘0’ and output is set
1 7
to ’1’ thus
1 6
PBDDR = 1111 1110 = 0xfe.
1 5

1 4
P
B
D
1 3
D
R

1 2

1 1

0 0

1 7

1 6

1 5
Earlier it was explained that address was assigned to the I/O register and block number is
top address of the I/Oregister.
0
1 4
x
f
f
As PBDDR has block number 0xfffd4, 0xfffd4 directly relates to the part that decides the
1 3
f

input-output of port B and input-output direction is decided by the value written in the
d
4
1 2
block number.
1 1

0 0
- 36 -
Program Explanation Manual “kit05.c”

To output data and read

380 : PBDR = 0xc0;

Port B is explained as an example. PB of the PBDR means port B. There are P1DR ~ PBDR. The part in
changes from 1 to B.
DR is the abbreviation for ‘Data Register’ and is the register that outputs the data to the pin or inputs the data from
the pin. In case of the pin which is set for input port (DDR=0), voltage level at that pin is obtained by reading the
data from DR.
Corresponding Bit”0”: Voltage input at the pin is”0”(Low Level).
Corresponding Bit “1”: Voltage input at the pin is”1”(High Level).
In case of the pin which is set for output port (DDR=1), voltage level at that pin is output by writing the data into
DR.
Corresponding Bit “0”: Voltage output from the pin is”0”(Low Level).
Corresponding Bit “1”: Voltage output from the pin is”1”(High Level).
Bit: 7 6 5 4 3 2 1 0

Output data Output data Output data Output data Output data Output data Output data Output data
setting to setting to setting to setting to setting to setting to setting to setting to
PBDR: bit 7 /Input bit 6 /Input bit 5 /Input bit 4 /Input bit 3 /Input bit 2 /Input bit 1 /Input bit 0 /Input
data read data read data read data read data read data read data read data read
from bit 7 from bit 6 from bit 5 from bit 4 from bit 3 from bit 2 from bit 1 from bit 0

About Voltage Levels


When I/O port is set for input, it is possible to read the external voltage signal. It is a digital value and "1" (high
level) indicates voltage of 2.0V or more and "0" (low level) indicates voltage of 0.8V or below. Any voltage input
in the range above 0.8 ~ less 2.0Vcannot be recognized as a valid level, because we cannot judge the point where
it becomes “0” or “1” in this range. Voltage like these will result in incorrect microcomputer operations; hence
always use a circuit that outputs the voltage within the valid range.

External
5.0V
Input voltage range
“1”
External recognized as“1”
Voltage 2.0V
0.8V Input voltage range
“0”
0V recognized as“0”

In case output setting, a regulated voltage is output from the objective pin if data is written to the port address which
microcomputer needs to output. Voltage 3.5 V or more is output from the pin for the bit is "1" (high level) and
voltage 0.4V or less is output from the pin for the bit is "0" (Low level).

External
“1” 5.0V Output Voltage
3.5V “1”
Micro-
computer
0.4V Output Voltage
“0” 0V “0”

- 37 -
Program Explanation Manual “kit05.c”

Incidentally, TTL (74LS Series IC’s) and CMOS (74HC Series IC's) voltage levels are as shown in the figure
below. It is necessary to note that both are not the microcomputer I/O port voltage levels.

5.0V 5.0V “1” 5.0V 5.0V


4.9V
“1”
“1” “1”
3.5V
2.7V
2.0V
1.5 V

0.8 V
“0” 0.4V “0” “0” 0.1V “0”
0V 0V
Outpu 0V Input Outpu 0V Input
TTL Levels CMOS Levels

About output currents

The I/O permissible output current of H8/3048F-ONE is as follows.

Permissible
Item
Current
Ports 1, 2, 5, and B 10mA
Permissible output low level current (Per pin)
Other output pins 2.0mA
Total of 28pins in ports 1, 2, 5 and B 80mA
Permissible output low level current (total) Total of all output pins , including the
120mA
above
Permissible output high level current (Per pin) All output pins 2.0mA
Permissible output high level current (total) Total of all output pins 40mA
Conditoions: Vcc=3.0 ~ 3.6V, AVcc=3.0 ~ 3.6V, VREF=3.0 ~ AVcc, Vss=AVss=0V or
Vcc=5.0±10%, AVcc=5.0±10%, VREF=4.5 ~ AVcc, Vss=AVss=0V

Connect the current-limiting resistor with the output pin to drive the Darlington transistor and LED directly. When
the Low level signal is output to port B, the current of 10mA/Pin flows out.When 10 mA current flows out of all
the eight pins, the current of 80mA is consumed only by port B. It is not possible to flow the current in the ports
1,2, and 5 as ' Permissible Output low level current (total) out of ports 1, 2, 5, and B is 80 mA'.
Permissible output low level current out of each pin corresponding to ports 1, 2, and 5 is 10 mA but no current can
flow in case of high level, as permissible high level current is only 2.0 mA. Take precautions while designing a
circuit.

- 38 -
Program Explanation Manual “kit05.c”

Setting Value of PBDR


What will be the output value at port B immediately after reset?

Pin Signal,
Details “0” “1” DR Value
Number Direction
1 − + 5V
2 Board <-PB7 LED1 ON OFF 1
3 Board<-PB6 LED0 ON OFF 1
4 Board<-PB5 Servo signal PWM Signal 0
5 Board<-PB4 Right Motor PWM Stop PWM Motion 0
Right Motor Rotational Normal Reverse
6 Board<-PB3 0
Direction Rotation Rotation
Left Motor Rotational Normal Reverse
7 Board<-PB2 0
Direction Rotation Rotation
8 Board<-PB1 Left Motor PWM Stop PWM Motion 0
9 Board->PB0 Push Button Switch Pushed Not Pushed Both are acceptable.
10 − GND

To turn off LED, “1” is output first. To stop the motor, “0” is output and the temporary
1 7 5V
“0” is put in the servo as both are acceptable. Since PB0 is for input, even if write
1 6 5V
anything, the value does not change. However, “0” is written as there may be a doubt that
0 5 0V
something happens if “1” is written. PBDR = 1100 0000 = 0xc0.
P 0 4 0V
B
D
R 0 3 0V

0 2 0V

0 1 In the program 0xc0 is set in PBDR first and then 0xfe is set in PBDDR. Why should we
0V

0 0 set the data before input-output settings? This relates to the register values immediately
after reset. According to the hardware manual, the initial value of PBDDR is 0x00 , so all
pins of port B become to input port. Initial value of the PBDR is 0x00. Thus, if the port is set for output in PBDR
first, 0x00 value of the PBDR is output. LED becomes ON for a moment as it is switched ON by "0". There may
not be a problem as LED is turned OFF immediately after next PBDR write but there may be some wrong
operations because of that momentary lighting effect if some other device is connected. Thus PBDR settings
should be set first and the LED should be turned OFF and then the PBDDR settings should be set to avoid such
minute mistakes

- 39 -
Program Explanation Manual “kit05.c”

When the bit set to output is read, present output value is read. For example, Input-output pin settings are as
shown in the following chart when switch conditions of the motor drive board is to be read.
Output bit reads the present output value.

Input-output Present output


Direction value Input value

1 7 1

0 6 0

1 5 1

P 1 4 1
B
D
R 0 3 0

0 2 0

1 1 1

1 0 1

Input bit reads the present input value.

This time if PBDR is read, ‘logical-OR’ed value of present output value and input value is read as shown in the
following chart.
Bit 7 6 5 4 3 2 1 0
Value being output 1 0 1 1 0 0 1
Input Value 1
Read value of PBDR 1 0 1 1 0 0 1 1

PBDR = 1011 0011 = 0xb3.

Other Ports

Setup
Port
value
P1DDR 0xff No connection
P2DDR 0xff No connection
P3DDR 0xff No connection
P4DDR 0xff No connection
P5DDR 0xff No connection
Bit 3 ~ 0 are DIPswitch inputs on the CPU board and others are not
P6DDR 0xf0
connected.
Sensor board is connected.
P7
There is no P7DDR as port 7 is an exclusive input port.
P8DDR 0xff No connection
Bit 3 is communication data output and bit 1 is communication data input,
P9DDR 0xf7
others are not connected.
PADDR 0xff No connection
PBDDR 0xfe Motor Drive Board

Why should a bit that is not connected, should make as an output port? In worst case, the internal circuit parts of
the H8 microcomputer get damaged because of the external noise if pin is set to input when nothing is connected.
The input pins with which nothing is connected are either pulled up/down or output setting is done without
connecting anything to the pins.

- 40 -
Program Explanation Manual “kit05.c”

3.9. Interrupt setting of ITU0 every 1ms

Why is interrupt necessary?

The program performs various processes. There is no problem if


Start the process is completed immediately. For example, it is assumed
that the push switch is pushed on after switch check process is
completed. At the most, there is delay of 2 [us] because process 1
Switch Check Process
Processing Time 10us
and 2 take only about 1[us] time. So, the detection of the pushing
action onto the push switch is checked in switch check process,
Repeats at High
Process1 and each process is carried out properly. Terefore, in the normal
speed
Processing Time 1us process, time of process 1 and 2 are the impeccable time.

Process 2
Processing Time 1us

Then, what should be done if processing takes time?


Start It is assumed that the push switch is pushed on after switch check
process is completed. The process 1 and 2 take a total time of
about 300 [ms] and the next switch check is carried out after
Switch Check Process
Processing Time 10us
300[ms]. And so, the swich may be turned off during 300[ms],
namely there may be a possibility of omission in detection of
Repeats with
Process1 switch at the end of 300 [ms]. If this is a sensor check of micom
Low speed
Processing Time 100ms car, it is omitted by detecting omission. It is serious problem.
Actually, though the process that takes time of 100 [ms] and 200
Process 2 [ms] is problem, it has been explained here as an example.
Processing Time 200ms

The “Interrupt” appears here


Interrupt for the first time to solve the
Start
Program Start problem above.When program
When switch
is started, it is set as “if the
pressed during Process1 Process
process 1 or 2 Processing Time 100ms switch pin changes from “1” to
“0”; interruption is generated”
Process2 Interrupt (Actually, it is necessary to set
Processing Time 200ms Program End the switch pin in IRQ0 ~ 5 pin
or NMI pin). After setting, even
if programs of process 1 and
process 2 are executed, the process is interrupted and it jumps to the interrupt program set beforehand. After the
interrupt process is completed, the process returns to interrupted program, and is restarted. When the switch is
pressed, the process cannot be omitted. In the situation, (switch is pressed here) the interrupt is used to execute a
specific program immediately.

When compared with the human being it is like operating a PC from the place which cannot be seen and voice will
not reach it from the cash counter while observing the shop. Therefore, it is necessary to check whether customer
has arrived or not every now and then. It is not efficient without applying to personal computer operation.
Then, the chime was prepared with the sticker "Dear customer, please push the chime”. Now it can be understood

- 41 -
Program Explanation Manual “kit05.c”

that customer has come and it is not necessary to go and check whether the person has come or not.
To check every now and then
It is called “Polling” in computer language. As regular observation is a must, there is a time loss if there are too
many parts to be observed.
To check when chime rings
It is called “Interrupt” in computer language. It is efficient as it checks only when the chime rings. But it is
necessary to confirm whether chime has rung or not. It is similar to the interrupt setting. Responding to the chime
ring and going up to the watch stand is similar to the interrupt program.

Interrupt Setting Procedurel

The procedure for interrupts program is-,


1. Setting the type of the interrupt and enable that interrupt.
2. Vector address setting (src file)
3. “.IMPORT” setting(src file)
4. Writing interrupt program
5. “#pragma interrupt” setting
6. Enable all interrupt
It is necessary to perform in this order.

Which interrupt is to be used?


This time, accurate timer is produced. It generates an interrupt after every 1ms and increments the variable in the
interrupt program by 1.The time for an action can be realized by checking that variable in the normal program.
For example, in the case of ”Wait until variable becomes 3000”, it is necessary to wait 3000 ms as variable
increments by 1 every 1ms. Here the 3 seconds timer explanation is complete.
In previous example, external switch triggers an interrupt. It is possible to generate an interrupt from built-in
peripheral functions besides the external interrupt in the H8/3048F-ONE. For example, interrupt is generated in
following cases.

At the end of the A/D conversion


Communication Data reception
For every 1 ms using ITU timer
At the end of communication data transmission

In this way it is possible to generate multiple interrupts. When an interrupt is generated, program jumps to the
interrupt routine (function) set in advance. Here, the interrupt is generated after every 1 ms using ITU. There are
five ITU channels from 0 ~ 4. ITU0is used here.

To begin with, what is ITU?

“ITU” is the abbreviation for “Integrated Timer Unit”. ”Integrated” means accumulated.
Accumulated means ”large number of things gathered together”. Here, it is a timer with a large number of
functions. In one word, it is a high efficiency timer.
ITU0 ~ 4 are the five built-in channels in the H8/3048F-ONE. They are used in various applications depending
on the settings.
Interval Timer … Sets 1ms timer
PWM Output … Controls the motor Speed
3 phase PWM Output … Controls the 3 phase Motor
Input Capture input … Measurement of waveform pulse width

- 42 -
Program Explanation Manual “kit05.c”

The functions used in the micom car are interval timer and PWM output. Accurate time measurement is done
using interval timer, and servo and motor control are performed using PWM output.

Program

384 : /* ITU0 Interrupt for every 1 ms */


385 : ITU0_TCR = 0x23;
386 : ITU0_GRA = TIMER_CYCLE;
387 : ITU0_IER = 0x01;
Omitted
400 : ITU_STR = 0x09;

ITU0 Register

Meaning of the registers that will be used hereafter -

Register Name Meaning

It acts like a second hand of the clock. The counter value is


ITU0_CNT Timer counter incremented by one after a fixed interval. This becomes a
standard value.
Decides the timing when ITU0_CNT is made 0 or incremented
ITU0_TCR Timer control register
by 1.
Here general means versatile. Versatile means, “it can be used in
large number of fields”. ITU0_GRA and ITU0_CNT are
compared automatically and if the values match, an interrupt is
generated and by the interrupt , PWM signal is changed, the
ITU0_GRA General Purpose register A
interval of the interval timer is changed, and so on and it can be
used in different applications depending on the settings. Thus it
is called as general. There are two general purpose registers and
are named as A and B respectively.
It is the second register named as B which is similar to the
ITU0_GRB General Purpose register B
register A.
Interrupt means a break in the line (disturb) and enable means to
Timer Interrupt Enable
ITU0_IER activate the operation. It is a register that decides whether to
Register
enable operation or not. -> permit interrupt or not.
ITU0_CNT stops first. This register starts ITU0_CNT. It acts
ITU_STR Timer Start Register
like a switch.
Though status means the position of human being in society it
ITU0_TSR Timer Status Register means a state or condition in Computers. Using this it is possible
to get the state of the ITU0.

ITU0_TCR(Timer Control Register) Setting Contents

This register sets -


Timing at which ITU0_CNT is made 0.
Timing at which ITU0_CNT is incremented by 1.

It acts like a second hand of the clock. The counter value is incremented by one after a fixed interval. This value
is always monitored; eg.generates an interrupt when it reaches say 1000.

- 43 -
Program Explanation Manual “kit05.c”

The meaning of each bit is as shown below -

Bit: 7 6 5 4 3 2 1 0
ITU0_TCR: − CCLR1 CCLR0 CKEG1 CKEG0 TPSC2 TPSC1 TPSC0
Setup value: 0 0 1 0 0 0 1 1
Hexadecimal :
2 3

Bit6,5: Counter Clear1,0


Selects the counter clear factor.
CCLR1 CCLR0 Explanation
0 0 Prohibits CNT Clear
0 1 Clears CNT by compare match/input capture of GRA.
1 0 Clears CNT by compare match/input capture of GRB.
1 1 Synchronous clear

This is the setting for “Timing to set 0 to ITU0_CNT ”. Compare-match means setting it in such a way that if
ITU0_CNT matches (ITU0_GRA + 1), ITU0_CNT is cleared. One might wonder why it should be incremented by
1, but it has been set in “Regulations of ITU". At the time of “ITU0_CNT=(ITU0_GRA + 1)", matching is judged
to be occured. Therefore, if one wants to do something when ITU0_CNT becomes 1000(Interrupt or the pulse is
generated), one must put 999 into ITU0_GRA. Interrupt or the pulse is generated by always comparing whether
ITU0_CNT is ( 999 + 1 ) or not.
Bit2 ~ 0: Timer Prescalar 2 ~ 0
Selects the counter clock of CNT.
TPSC2 TPSC1 TPSC0 Explanation
0 0 0 Internal Clock: Count at φ
0 0 1 Internal Clock: Count at φ / 2
0 1 0 Internal Clock: Count at φ / 4
0 1 1 Internal Clock: Count at φ / 8
1 0 0 External Clock A: Count at TCLKA(PA0) pin
1 0 1 External Clock B: Count at TCLKB(PA1) pin
1 1 0 External Clock C: Count at TCLKC(PA2) pin
1 1 1 External Clock D: Count at TCLKD(PA3) pin

This setting sets the time for incrementing ITU0_CNT.


Internal clock φ is the crystal value. Crystal is the standard for all
Crystal
other timings. Micom car rally kit CPU board RY3048F-ONE has a
built-in 24.576MHz crystal. ITU0_CNT is incremented by 1 in 1/8
φ . Let's calculate all four patterns. The value that can be set in
ITU0_GRA is from 0 - 65535 i.e. unsigned 16 Bit. The maximum
value is calculated together. By the way, the crystal error is ±100ppm.
As ppm is 1/1,000,000, error is 100/1,000,000, 1/10,000 or 0.01%.

The external clock count means to count by connecting external pulse


to the port and not the crystal. It is not explained here since it is not used.

- 44 -
Program Explanation Manual “kit05.c”

TPSC2 TPSC1 TPSC0 Explanation


Internal Clock: Count using φ
Because it is reciprocal when counting a timing with 24.576MHz,
0 0 0
a period of one count = 1 / (24.576 106) = 40.69 ns
Maximum value is 40.69 65535=2.666ms.

Internal Clock: Count using φ/2


0 0 1 a period of one count = 1 / (24.576 106/2) = 81.38[ns]
Maximum value is 81.38[ns] 65535 = 5.333[ms]

Internal Clock: Count using φ/4


0 1 0 a period of one count = 1 / (24.576 106/4) = 162.76[ns]
Maximum value is 162.76[ns] 65535 = 10.67[ms]

Internal Clock: Count using φ/8


0 1 1 a period of one count = 1 / (24.576 106/8) = 325.52[ns]
Maximum value is 325.52[ns] 65535 = 21.33[ms]
The relation between frequency ”f” and period “T” are reciprocal each other. ( f=1/T )

The selection procedure is as follows: -


Make TPSC=”000” if interrupt interval is less than 2.666 ms.
Make TPSC=”001” if interrupt interval is less than 5.333 ms.
Make TPSC=”010” if interrupt interval is less than 10.67 ms.
Make TPSC=”011” if interrupt interval is less than 21.33 ms.
If the interrupt interval is more than 21.33 ms, generate an new interrupt interval with less than this interval
corresponding to the program which execute processing after a certain time of interrupt.

It should ideally be set to “000” because it is 1ms, but taking into consideration the compatibility with kit04.c,
“011” is set in the TPSC this time. The time at which ITU0_CNT advances by 1 is 325.52ns.

Setting contents of the ITU0_GRA (General Purpose Register A)

Interrupt interval is set in ITU0_GRA.


In this case, the interrupt interval is 1ms. The count till the lapse of 1 ms is equal to (1 10-3) ÷ (325.52 10-9)
=3072 because ITU0_CNT increments by one every 325.52ns as set in the ITU0_TCR.
Because an actual comparison is done by “ITU0_CNT=(ITU0_GRA + 1)” as shown before, one small value
“3071” is set in ITU0_GRA.
“Clear the CNT by compare match/input capture of GRA” is set in the ITU0_TCR. In short, ITU0_CNT becomes
0 when “ITU0_CNT=(ITU0_GRA + 1)”. Then counting is started again from 1 and when it reaches 3072 the
counter is cleared to 0 i.e. the counter is cleared every 1ms.

Setting contents of the ITU0_IER (Timer interrupt enable register)


“Enable” means interrupt validness. It is a register that activates an interrupt and decides whether what flag
interrupt shall be allow or not.

Bit: 7 6 5 4 3 2 1 0
ITU0_IER: − − − − − OVIE IMIEB IMIEA
Setup value: 0 0 0 0 0 0 0 1
Hexadecimal:
0 1

Bit2: OVerflow Interrupt Enable (OVIE)

- 45 -
Program Explanation Manual “kit05.c”

Enables or disables the interrupt requested by the OVF frag in TSR when OVF is set to 1.
OVIE Explanation
0 OVI interrupt requested by OVF is disabled ( Initial value)
1 OVI interrupt requested by OVF is enabled
This time we does not use this OVF interrupt, then there is no relation to this interrupt.
In case of no relation, the original value ( = Default value = Initial value ) is retained.

Bit1: Input capture/compare Match Interrupt Enable B (IMIEB)


Enables or disables the interrupt requested by the IMFB frag in TSR when IMFB is set to 1.
IMIEB Explanation
0 IMIB interrupt requested by IMFB is disabled ( Initial value)
1 IMIB interrupt requested by IMFB is enabled
This time there is no relation. The original value is kept, as it is when there is no relation.

Bit0: Input capture/compare Match Interrupt Enable A (IMIEA)


Enables or disables the interrupt requested by the IMFA frag in TSR when IMFA is set to 1.
IMIEA Explanation
0 IMIA interrupt requested by IMFA is disabled ( Initial value)
1 IMIA interrupt requested by IMFA is enabled
There are alphabets that cannot be easily understood, OVIE,IMIEB,IMIEA and so on. It is necessary
to explain ITU0_TSR before this explanation is done.

Setting contents of the ITU0_TSR (Timer Status Register)

Bit: 7 6 5 4 3 2 1 0
ITU0_TSR: − − − − − OVF IMFB IMFA

Bit2: OVerflow Flag (OVF)


This status flag indicates CNT overflow or underflow.
OVF Explanation
[Cleaning condition]
0
Read OVF when OVF=1, then write 0 in OVF
[Setting condition]
1
CNT overflowed (h’FFFF->h’0000) or underflows (h’0000->h’FFFF)
This time there is no relation. The original value is retained, as it is when there is no relation.

Bit1: Input capture/compare Match Flag B (IMFB)


This status flag indicates GRB compare match or input capture events.
IMFB Explanation
[Cleaning condition] ( Initial value)
0
Read IMFB when IMFB = 1, then write 0 in IMFB
[Setting condition]
(1) CNT=GRB when GRB functions as an output compare register.
1
(2) CNT value is transfferred to GRB by an input capture signal, when GRB functions
as an input capture register.
This time there is no relation. The original value is retained, as it is when there is no relation.

- 46 -
Program Explanation Manual “kit05.c”

Bit0: Input capture/compare Match Flag A(IMFA)


This status flag indicates GRAcompare match or input capture events.
IMFA Explanation
[Cleaning condition] ( Initial value)
0
Read IMFA when IMFA = 1, then write 0 in IMFA
[Setting condition]
(1) CNT=GRA when GRA functions as an output compare register.
1
(2) CNT value is transfferred to GRA by an input capture signal, when GRA functions
as an input capture register.

This is “IMFA” which is Bit0 of ITU0_IER mentioned earlier. “Input capture/compare Match Flag A” is
abbreviated and called as IMFA. Still it is not understood what it exactly is. In short, Bit0 of ITU0_TSR becomes
“1” at ITU0_CNT=(ITU0_GRA + 1). "
Bit0 of ITU0_TSR becomes “1” and if Bit0 of ITU0_IER is “1”, interrupt is generated. The interrupt generation is
not triggered by”ITU0_CNT=(ITU0_GRA + 1)" but is triggered when Bit0 of ITU0_TSR becomes “1” after
“ITU0_CNT=(ITU0_GRA + 1)”.

Set to “1”for generating an


interrupt.

ITU0_IER 00000001
Interrupt is
generated when both
bits are “1”.
ITU0_TSR 00000001

Becomes “1”
whenITU0_CNT=(ITU0_GRA+1)

Setting contents of the ITU_STR (Timer Start Register)


Bit: 7 6 5 4 3 2 1 0
Start Start Start Start Start
ITU_STR: − − − counting counting counting counting counting
ITU4_CNT ITU3_CNT ITU2_CNT ITU1_CNT ITU0_CNT

Setup value: 0 0 0 0 1 0 0 1
Value to be
ORed in
0 9
program:

Bit4-0: Counter State 4 ~ 0


Selects ITUx_CNT counter start/stop.
STR4-0 Explanation
0 Stops counting CNT of ITUx.
1 Starts counting CNT of ITUx.
Note: x is from 0 ~ 4.
ITU0_CNT has not yet started counting. Setting this register starts it. ITU_STR has not number as like in
ITU0_CNT. Only one ITU_STR sets common setting in the ITU0 ~ 4. Bit0 is made "1" since ITU0 is started this
time. Also, Bit3 is also made "1" since ITU3 is used.

- 47 -
Program Explanation Manual “kit05.c”

When compared to the alarm clock


It is explained again by comparing ITU0 to the alarm clock, as it is hard to understand.
65535 It is possible to compare ITU0_CNT to the second hand(of the
0 1 2
65534 clock). However, one count will not be necessarily incremented in
… …
… a second. When the value of CNT is increasing, the value set by
… TPSC of ITU0_TCR becomes->TPSC2~0 ="011"->Selected φ / 8
->as the frequency of crystal: f = φ = 24.576MHz, so time interval
of one count T= 1/f =1/(φ / 8)
CNT value = 1/(24.576 106/8)
= 325.52[ns]
As the width of CNT is 16 bits and the type specifier is (unsigned
char), the maximum value becomes 65535.

0
ITU3_GRA sets time required for alarm. According to the previous
setting, CNT increases one by one with 325.52[ns]. As time
required for alarm is set by 1[ms] , so 3072 is set in the GRA but
according to the standard operating procedure of ITU,
"ITU0_CNT=(ITU0_GRA + 1)" is the actual required timing for
GRA+1
CNT value alarm.
(3072)
(1 10-3) / (325.52 10-9)=3072
1ms till Therefore, 3071 of 1 small value is set in the ITU0_GRA.
becoming 3072

The ordinary clock goes through a rotation, but on the way through
0
When a rotation, ITU can forcefully adjust the second hand to 0. Since it
becoming 3072, is set as "CNT is cleared in compare match/ input capture of GRA
it is cleared to “ by ITU0_TCR, if it becomes ITU0_CNT=(ITU0_GRA + 1) then
0. CNT becomes 0. It requires 1[ms] to become 3072 , and then it is
cleared to 0 at once and ITU0_CNT=(ITU0_GRA + 1) becomes in
GRA+1
CNT value each 1[ms].
(3072)

ITU0_TSR bit0="1"
Bell

Bit 0 of ITU0_TSR becomes 1 when"ITU0_CNT=(ITU0_GRA +


1)", but, there is no action. Another operation is necessary to ring
the bell.

- 48 -
Program Explanation Manual “kit05.c”

ITU0_TSR bit0="1"
ITU0_IER bit0="1" In order to ring the alarm bell the Bit 0 of ITU0_IER is set to
Bell "1", and an interrupt is generated. When the bell rings,the
microcomputer shifts to the interrupt process.

Battery is put in the alarm Setting completed! But, actually, there is no battery in the clock.
(Clock is stared) The clock in which battery is put is moved. This is the setting 0x01
ITU STR = 0x01 into the timer start register ITU_STR. However, as ITU3 is used
too, 0x09 is actually set.

kit05start.src setting

It is necessary to operate the "kit05start.src" file to use interrupt.

Bypass destination when the interrupt is generated.

When interrupt is generated, “The execution of the program jumps to the interrupt program (function) which is set
in advance”, and that is described already on page 41. what means that?
This time, execution is moved to interrupt_timer0 function which is a private function. Using “interrupt” as the
name of a private function, is ok. In H8/3048F-ONE, the address of 0x0000-0x00ff block number is called as
vector address area. An address in which the interrupt program is stored, is written in the previously decided
address of vector address. When the interrupt is generated, the executio of the program jamps to that address.
The following table lists it. For example, interrupt (IMIEA0 interrupt) generated when the CNT of ITU is in
agrrement with (GRA + 1), reads the value written in address 0x0060 and jump to that address.

IMIEA0 interrupt Reads value, written in Jump to the read address


generation adress 0x0060

The summarized table for interrupt types and vector address is displayed below.

Generating Vector
Interrupt source Vector address IPR Priority
origin no.
Reset External pin 0 H'0000 ~ H'0003 −−−

- 49 -
Program Explanation Manual “kit05.c”

NMI 7 H'001C ~ H'001F −−− High


IRQ0 12 H'0030 ~ H'0033 IPRA7 ↑
IRQ1 13 H'0034 ~ H'0037 IPRA6
IRQ2 External pins 14 H'0038 ~ H’003B
IPRA5
IRQ3 15 H'003C ~ H’003F
IRQ4 16 H'0040 ~ H’0043
IRQ5 17 H'0044 ~ H’0047
IPRA4
18 H'0048 ~ H’004B
Reserve −−−−−−−−−
19 H'004C ~ H’004F
WOVI (Interval timer) Watch dog timer 20 H'0050 ~ H’0053
CMI (Compare match) Refresh controller 21 H'0054 ~ H’0057
IPRA3
22 H'0058 ~ H’005B
Reserve −−−−−−−−−
23 H'005C ~ H’005F
IMIA0 (Compare match/input capture
24 H'0060 ~ H’0063
A0)
IMIB0 (Compare match/input capture ITU
Channel 0 25 H'0064 ~ H’0067 IPRA2
B0)
OVI0 (Overflow 0) 26 H'0068 ~ H’006B
Reserve −−−−−−−−− 27 H'006C ~ H’006F
IMIA1 (Compare match/input capture
28 H'0070 ~ H’0073
A1)
ITU
IMIB1 (Compare match/input capture
Channel 1 29 H'0074 ~ H’0077 IPRA1
B1)
OVI1 (Overflow 1) 30 H'0078 ~ H’007B
Reserve −−−−−−−−− 31 H'007C ~ H’007F
IMIA2 (Compare match/input capture
32 H'0080 ~ H’0083
A2)
IMIB2 (Compare match/input capture ITU
Channel 2 33 H'0084 ~ H’0087 IPRA0
B2)
OVI2 (Overflow 2) 34 H'0088 ~ H’008B
Reserve −−−−−−−−− 35 H'008C ~ H’008F
IMIA3 (Compare match/input capture
36 H'0090 ~ H’0093
A3)
ITU
IMIB3 (Compare match/input capture
Channel 3 37 H'0094 ~ H’0097 IPRB7
B3)
OVI3 (Overflow 3) 38 H'0098 ~ H’009B
Reserve −−−−−−−−− 39 H'009C ~ H’009F
IMIA4 (Compare match/input capture
40 H'00A0 ~ H’00A3
A4)
ITU
IMIB4 (Compare match/input capture
Channel 4 41 H'00A4 ~ H’00A7 IPRB6
B4)
OVI4 (Overflow 4) 42 H'00A8 ~ H’00AB
Reserve −−−−−−−−− 43 H'00AC ~ H’00AF
DEDN0A 44 H'00B0 ~ H’00B3
DEDN0B DMAC 45 H'00B4 ~ H'00B7
IPRB5
DEDN1A Group 0 46 H'00B8 ~ H'00BB
DEDN1B 47 H'00BC ~ H'00BF
48 H'00C0 ~ H'00C3
49 H'00C4 ~ H'00C7
Reserve −−−−−−−−−− −−−−
50 H'00C8 ~ H'00CB
51 H'00CC ~ H'00CF
ERI0 (Recieve error 0) SCI 52 H'00D0 ~ H'00D3 IPRB3
RXI0 (Receive data full 0) Channel 0 53 H'00D4 ~ H'00D7

- 50 -
Program Explanation Manual “kit05.c”

TXI0 (Tansmit data empty 0 ) 54 H'00D8 ~ H'00DB


TEI0 (Transmit end 0) 55 H'00DC ~ H'00DF
ERI1 (Recieve error 1) 56 H'00E0 ~ H'00E3
RXI1 (Receive data full 1) SCI 57 H'00E4 ~ H'00E7
IPRB2
TXI1 (Tansmit data empty 1) Channel 1 58 H'00E8 ~ H'00EB ↓
TEI1 (Transmit end 1) 59 H'00EC ~ H'00EF
ADI (A/D end) A/D 60 H'00F0 ~ H'00F3 IPRB1 Low

"Kit05start.src" file has the description of this vector address.


Src file describes the program and the vector address. Wen microcomputer is reset (it is called as start up routine),
this program is executed at the beginning. The language is assembly. Description of the contents that cannot be
described by C language, and the program executed before execution of main function, are described.

Reset Execution of Main function execution


kit05start.src of kit05.c

Details are explained later.


The jump destination address is declared in the order of; vector nos. 0 to 60 in "kit05start.src". Vector addresses
of all interrupts are described and the interrupts, which are not used, are described as "RESERVE". According to
above table, as vector no. of IMIA0 interrupt of ITU0 is 24, jump destination function name "_interrupt_timer0 "
is described in the .DATA.L of 25th(as started from 0).

8: .SECTION V ; Beginning of vector section


9: .DATA.L RESET_START ; 0 h'000000 Reset
10 : .DATA.L RESERVE ; 1 h'000004 System reservation
11 : .DATA.L RESERVE ; 2 h'000008 System reservation
(Omitted)
31 : .DATA.L RESERVE ; 22 h'000058 System reservation
32 : .DATA.L RESERVE ; 23 h'00005c System reservation
33 : .DATA.L _interrupt_timer0 ; 24 h'000060 ITU0 IMIA0
34 : .DATA.L RESERVE ; 25 h'000064 ITU0 IMIB0
35 : .DATA.L RESERVE ; 26 h'000068 ITU0 OVI0
(Omitted)
68 : .DATA.L RESERVE ; 59 h'0000ec SCI1 TEI1
69 : .DATA.L RESERVE ; 60 h'0000f0 A/D ADI

“. DATA.L” command is the command of writing data of Long unit (4 bytes). For example,

.DATA.L h'12345678 ; "h'" in the assembly language is "0x" of C language.

If the block no. is assumed as 0x100, above data is written as following.


0x0100 block no 0x12
0x0101 block no 0x34
0x0102 block no 0x56
0x0103 block no 0x78
The function name; even though originally numerical data, can be described. If function name is described, it
becomes the address which is stored that function.
For example, if interrupt_timer0 function is in 0x05a0 and if the address written by ".DATA.L" is 0x0060, then,

0x0060 0x00
0x0061 0x00
0x0062 0x05
0x0063 0xa0

- 51 -
Program Explanation Manual “kit05.c”

Reset is also interrupt. Reset is vector number 0. The flow of program execution jumps to RESET_START after
reset.

When calling C language program file from the assembly program file.

According to the rule, the function name must start with the “_”(underscore), when calling the function of C
program file (kit05.c) from the assembly program file (kit05start.src). It is stated as “_interrupt_timer0” in the
assembly program and, “interrupt_timer0” in C language program.

“_interrupt_timer0” of assembly program = “interrupt_timer0” of the C language program

Declaration of .IMPORT
The compiler looks for converting the declared “_interrupt_timer0" into an address.

33 : .DATA.L _interrupt_timer0 ; 24 h'000060 ITU0 IMIA0

But the error message “it does not exist” is displayed , because this function is in the “Kit05.c” as “ 405 : #pragma
interrupt ( interrupt_timer0 ) and 406 : void interrupt_timer0( void ) “.
Then, the name “_ interrupt_timer0” exists in separate file by using the “. IMPORT” command and “Search again
later” is instructed to the compiler.

6: .IMPORT _interrupt_timer0

Summaries of kit05start.src file

The vector number is different from the contents, which generates interrupt.
Jump destination function is described in the corresponding vector number.
However, “_ (underscore)” is added at the beginning of the function name.
Whether this name is exists in the external file by the “.IMPORT” command is conveyed.

- 52 -
Program Explanation Manual “kit05.c”

interrupt_timer0 function 407 ~ 412 line


407 : void interrupt_timer0( void )
408 : {
409 : ITU0_TSR &= 0xfe; /* Flag clear */
410 : cnt0 ++ ;
411 : cnt1 ++ ;
412 : }
The interrupt_timer0 function is executed by calling at 1ms interval by the interrupt. cnt0 and cnt1 incremented
by 1. The duration (in milliseconds) can be measured by checking the value in the main function, as this
interrupt_timer0 function is called at every 1ms interval.
In line no. 409, ITU0_TSR carries out AND operation by 0xfe. When the interrupt has genarated, bit 0 of
ITU0_TSR becomes to “1” automatically. But, the bit cannot be change to "0" automatically. So, the bit is made
“0 “ as the preparation for the next conformation by ITU0_CNT=(ITU0_GRA + 1).

ITU0_TSR = 0x00;

How do you think about the above statement for the bit clear ? Regrettably, this is not allowed.
Let’s refer to the ITU0_TSR (IMFA) bit 0 description again.
Setting contents of ITU0_TSR (Timer status register)

Bit : 7 6 5 4 3 2 1 0
ITU0_TSR: − − − − − OVF IMFB IMFA

Bit0: Input capture/compare Match Flag A(IMFA)


This status flag indicates GRAcompare match or input capture events.
IMFA Explanation
[Cleaning condition] ( Initial value)
0
Read IMFA when IMFA = 1, then write 0 in IMFA
[Setting condition]
(1) CNT=GRA when GRA functions as an output compare register.
1
(2) CNT value is transfferred to GRA by an input capture signal, when GRA functions
as an input capture register.

“After reading IMFA flag” is the key point. If the value is written as it is like "ITU0_TSR = 0x00;",
it is not “Read”. Therefore, even if 0x00 is written, bit 0 does not become "0". Then, AND operation is done.
Bit 7 6 5 4 3 2 1 0
ITU0_TSR ? ? ? ? ? ? ? 1
Value by
which AND 1 1 1 1 1 1 1 0
operated
Result 0
Note: means “No change”.

“ ITU0_TSR &= 0xfe; ” means “ ITU0_TSR = ITU0_TSR & 0xfe; “. There is ITU0_TSR in the right hand
assignment expression in the assignment statement. ITU0_TSR in the assignment expression means that the
value of ITU0_TSR is read in the CPU register once, and the new value of the expression is made by logical AND
operation, and the new value is written into ITU0_TSR by “ ITU0_TSR = ”. So,”write after read” has been
carried out. Moreover, there is a merit of not changing the values other than bit 0 by AND operation .
What will happen if ITU0_TSR bit 0 is not set to “1”? Interrupt is generated, when -
Bit 0 of ITU0_IER is “1” and bit 0 of ITU0_TSR is “1”.
When any interrurt is enabled, this check is done always after execution cycle of every command translated
machine language level.

- 53 -
Program Explanation Manual “kit05.c”

Flow of interrupt check


mov.b #h'00,r1 Checking of 7 flags of external interrupt source and 30 flags
of internal interrupt source.
Checking of interrupt flag If flag is raised, the address corresponding with the flag and
For details refer to stored in the interrupt vector table, is read out and jumps to the
Process flow

address in which the interrupt program.is stored.


cmp.b r1,r2 Flow of processing returns to place from which was formerly
jumped when interrupt program is completed and flag is
Checking of interrupt flag checked. If all interrupt flags are "0" then flow of processing
For details refer to shifts to next program.

sub.b #h'56,r3 Program instruction


Processing between the program instruction and
the next program instruction

Now, notice about "Return and Recheck". If bit 0 of ITU0_TSR is not cleared, it is judged that the interrupt is
generated then, and the flow of processing backs again to the interrupt program at once, in spite of very short time
for less than 1 ms. This means that only interrupt program keeps on executing without executing the main program
and it becomes infinite loop. It is necessary to clear corresponding status flag always in the interrupt program.
The process example is displayed below when bit0 of ITU0_IER ="1" and bit0 of ITU0_TSR = ”1” is
detected.

mov.b #h'00,r1

Detection bit0 of ITU0_IER ="1" and


bit0 of ITU0_TSR ="1 #pragma interrupt( interrupt_timer0 )
Address reading from vector address 24 void interrupt_timer0 (void)
Jump to that address {
Process flow

ITU0_TSR &= 0xfe;


When returning, the interrupt flag is cnt0++;
checked again. If all are "0", it is as cnt1++;
follows }

cmp.b r1,r2

Same process

sub.b #h'56,r3

- 54 -
Program Explanation Manual “kit05.c”

● Line 406 pragma interrupt declaration

406 : #pragma interrupt( interrupt_timer0 )

Actually it is necessary to declare, "pragma interrupt" in the function, to which the processing flow jumps by
interrupt. The function name is written in the parenthesis as "# pragma interrupt (interrupt function name)".
In C language, when the function is called, the processing flow goes to the function and returns automatically to
the place where the function has been called, at the completion of the function.
C language is translated to the assembly language by C language compiler as following diagram. In assembly
language, RTS(ReTurn from Subroutine) command is the command that the processing flow returns to the place
where the function is called.
Conversion to assembler

void abc( void ) _abc:


{

RTS <- Returns by RTS


} command

In C language interrupt program is also a function but actually the assembler command that the processing flow
is returned from interrupt program is not a RTC command, it is a RTE(ReTurn from Exeption) command. This is a
command which returns to the place where interrupt program is called, after rewriting the value of CCR and PC
which are stored automatically when interrupt is generated.
※CCR: Condition Cord Register PC:Program Counter
Conversion to assembler

#pragma interrupt( abc ) _abc:


void abc( void )
{

RTE <-Returned
} from interrupt

In C language, the function ends by “}”, whether it is an interrupt function or not. But in the assembly language,
the return command changes depending on that the function is an interrupt function or not. This specification is a
“pragma declaration”.
In the case of jumping by the interrupt, “#pragma” is declared in the function , then it is OK.
It is not possible to call the function that declares “pragma interrupt”, from the usual functions like main function
etc.
Here, interrupt setting is complete. Let’s use the interrupt.

- 55 -
Program Explanation Manual “kit05.c”

3.10. Setting of reset-synchronized PWM mode

389 : /* ITU3, 4 reset-synchronized PWM mode for right-left motor and servo */
390 : ITU3_TCR = 0x23;
391 : ITU_FCR = 0x3e;
392 : ITU3_GRA = PWM_CYCLE; /* Setting of cycle */
393 : ITU3_GRB = ITU3_BRB = 0; /* PWM Setting of left motor */
394 : ITU4_GRA = ITU4_BRA = 0; /* PWM Setting of right motor */
395 : ITU4_GRB = ITU4_BRB = SERVO_CENTER; /* PWM Setting of servo */
396 : ITU_TOER = 0x38;
397 :
398 : /* Count start of ITU*/
399 : ITU_STR = 0x09;

Here, PMW for controlling left motor, right motor and servo is set. PMW cycle is adjusted to 16ms fited into the
servo cycle. It is OK if PMW cycle of the motor is approximately 1[ms] (a motor moves most smoothly) but as the
cycle cannot be made only in common, it matches with servo.
There are five channels of ITU. One PWM wave of per one ITU can be output usually.
In H8/3048F-ONE there is a setting called reset-synchronized PMW mode. This mode is the mode that output 3
toggled PMW waveforms by combining and using ITU3 and ITU4. Further, the wave that reverses the 3 toggled
PWM waveformes are output at the same time. PWM output is possible from total 6 pins. The defect
(shortcoming) is that it has to be made the same for all 3 cycles.

B0
ITU3
B0
PWM mode
B1
ITU3 and
ITU4 B2
ITU4
B2
PWM mode
Reset B4
synchronized
B3

※ B0 means bit #0 of B5
port B
single PWM mode Reset synchronized PWM

Output of 2 PWM by 2 ITU is possible Output of 3 PWM by 2 ITU and output of


its respective reverse waves is possible.

○ About toggle operation


Toggle operation is like ON/OFF switching. Following action is reverse action of previous.
That is to say, it is the opration that is never continuing the same action.
In reset-synchronized PWM mode, the PWM outputs toggle at compare match of ITU3_CNT with ITU3_GRB,
ITU4_GRA, and ITU4_GRB respectively, and all toggle when counter is cleared.

- 56 -
Program Explanation Manual “kit05.c”

Setting contents of ITU3_TCR (Timer Control Register)


This register sets following things
Timing which adjusts ITU3_CNT to 0
Time which increment ITU3_CNT by 1

The meaning of each bit is as follows.


Bit: 7 6 5 4 3 2 1 0
ITU3_TCR: − CCLR1 CCLR0 CKEG1 CKEG0 TPSC2 TPSC1 TPSC0
Set value: 0 0 1 0 0 0 1 1
Hexadecimal:
2 3

Bit 6 and 5: Counter clear 1 (CCLR1) & Counter clear 0 (CCLR0)


The counter clear source of CNT is selected.
CCLR1 CCLR0 Explanation
0 0 Clear prohibition of CNT
0 1 CNT is cleared by compare match/input capture of GRA
1 0 CNT is cleared by compare match/input capture of GRB
1 1 Synchronous clear

It is a setting of clearing ITU3_CNT when it becomes ITU3_CNT = (ITU3_GRA + 1).

Bit 2~0: Timer prescaler 2~0


The counter clock of CNT is selected.

TPSC2 TPSC1 TPSC0 Explanation


Internal clock: It is counted by φ.
It is counted by 24.576MHz. When it is adjusted to time, it shall
0 0 0 be reciprocal number
1 / (24.576 106) = 40.69[ns]
Maximum value is 40.69[ns] 65535 = 2.666[ms]
Internal clock: It is counted by φ / 2
0 0 1 1 / (24.576 106/2) = 81.38[ns]
Maximum value is 1.38[ns] 65535 = 5.333[ms]

Internal clock: It is counted by φ / 4


0 1 0 1 / (24.576 106/4) = 162.76[ns]
Maximum value is 162.76[ns] 65535 = 10.67[ms]

Internal clock: It is counted by φ / 8


0 1 1 1 / (24.576 106/8) = 325.52[ns]
Maximum value is 325.52[ns] 65535 = 21.33[ms]
1 0 0 External clock A: It is counted by TCLKA pin (PA0).
1 0 1 External clock B: It is counted by TCLKB pin (PA1).
1 1 0 External clock C: It is counted by TCLKC pin (PA2).
1 1 1 External clock D: It is counted by TCLKD pin (PA3).

This time cycle is of 16[ms]. To adjust the cycle to 16ms TPSC is ="011".

- 57 -
Program Explanation Manual “kit05.c”

Setting contents of ITU_FCR (Timer Function Control Register)

In this register, the setting for channels 3 and 4 are carried out. One is mode selection and the other is setting of
buffer operation.

Bit: 7 6 5 4 3 2 1 0
ITU_FCR: − − CMD1 CMD0 BFB4 BFA4 BFB3 BFA3
Set value: 0 0 1 1 1 1 1 0
Hexadecimal:
3 e

Bit 5 and 4: Combination mode 1 and 0 (CMD1, CMD0)


These bits select whether channels 3 and 4 operate in normal mode, complementary PWM mode, or
reset-synchronized PWM mode.
CMD1 CMD0 Explanation
0 0
Channels 3 and 4 operate normally.
0 1
1 0 Channels 3 and 4 operate together in complementary PWM mode.
1 1 Channels 3 and 4 operate together in reset-synchronized PWM mode.

We can select normal mode, complementary PWM mode, and reset-synchronized PWM mode using ITU3and 4.
This time, as reset-synchronized PWM mode is used it is adjusted to "11". Other bits are described later.

Setting contents of ITU_TOER (Timer Output master Enable Register)

Bit: 7 6 5 4 3 2 1 0
ITU_TOER: − − EXB4 EXA3 EB3 EB4 EA4 EA3
Set value: 0 0 1 1 1 0 0 0
Hexadecimal
3 8

Bit 5: Master Enable TOCXB4 (EXB4)


Enables or disables ITU output at pin TOCXB4.
EXB4 Explanation
TOCXB4 output is disabled regardless of TFCR settings
0 ( TOCXB4 operates as a generic input/output pin).
If XTGD=0, EXB4 is cleared to 0 when input capture A occurs in channel 1.
1 TOCXB4 is enabled for output according to TFCR settings.

Bit 4: Master Enable TOCXA4 (EXA4)


Enables or disables ITU output at pin TOCXA4.
EXA4 Explanation
TOCXA4 output is disabled regardless of TFCR settings
0 ( TOCXA4 operates as a generic input/output pin).
If XTGD=0, EXA4 is cleared to 0 when input capture A occurs in channel 1.
1 TOCXB4 is enabled for output according to TFCR settings.

- 58 -
Program Explanation Manual “kit05.c”

Bit 3: Master Enable TIOCB3 (EB3)


Enables or disables ITU output at pin TIOCB3.
EB3 Explanation
TIOCB3 output is disabled regardless of TIOR3 and TFCR settings
0 (TIOCB3 operates as a generic input/output pin).
If XTGD=0, EB3 is cleared to 0 when input capture A occurs in channel 1.
1 TIOCB3 is enabled for output according to TIOR3 and TFCR settings.

Bit 2: Master Enable TIOCB4 (EB4)


Enables or disables ITU output at pin TIOCB4.
EB4 Explanation
TIOCB4 output is disabled regardless of TIOR4 and TFCR settings
0 (TIOCB4 operates as a generic input/output pin).
If XTGD=0, EB4 is cleared to 0 when input capture A occurs in channel 1.
1 TIOCB4 is enabled for output according to TIOR4 and TFCR settings.

Bit 1: Master Enable TIOCA4 (EA4)


Enables or disables ITU output at pin TIOCA4.
EA4 Explanation
TIOCA4 output is disabled regardless of TIOR4, TMDR, and TFCR settings
0 (TIOCA4 operates as a generic input/output pin).
If XTGD=0, EA4 is cleared to 0 when input capture A occurs in channel 1.
1 TIOCA4 is enabled for output according to TIOR4, TMDR, and TFCR settings.

Bit 0: Master Enable TIOCA3 (EA3)


Enables or disables ITU output at pin TIOCA3.
EA3 Explanation
TIOCA3 output is disabled regardless of TIOR3, TMDR, and TFCR settings
0 (TIOCA3 operates as a generic input/output pin).
If XTGD=0, EA3 is cleared to 0 when input capture A occurs in channel 1.
1 TIOCA3 is enabled for output according to TIOR3, TMDR, and TFCR settings.

Contents of the formal hardware manual are written as it is, but there are lots of difficult expressions. In summary,
If in the manual hence those expressions are not understood well. If reset-synchronized PWM mode is used, PWM
waveform is automatically output from PB5~0. At this time, PWM output can be OFF by setting this register. If
PWM waveform is not necessary, it is preferable to be used as a normal I/O port.
It is rewritten in an easily comprehensible manner.

TOER 7 6 5 4 3 2 1 0
PB5 is a PB4 is a PB1 is a PB3 is a PB2 is a PB0 is a
normal normal normal normal normal normal
When "0" − −
input-output input-output input-output input-output input-output input-output
port port. port. port. port. port.
PB5 is a PWM PB4 is a PWM PB1 is a PWM PB3 is a PWM PB2 is a PWM PB0 is a PWM
When "1" − −
output output output output output output

Setting at
0 0 1 1 1 0 0 0
this time

PB5 is used as PWM for Servo, PB4 is used as PWM for Right motor and PB1 is used as PWM for Left motor.
This time, only the reversing PWM signal is used. Except this, others are used as a normal input-output port.

- 59 -
Program Explanation Manual “kit05.c”

Contents of ITU3_CNT, ITU4_CNT, ITU3_GRA, ITU3_GRB, ITU4_GRA, ITU4_GRB


When reset-synchronized PWM mode is used, function of each register becomes as shown in the table below.

Register Function when reset-synchronized PWM mode is used

ITU3_CNT It is used as a counter.

ITU4_CNT It is unused.

ITU3_GRA PWM cycle is set.

Setting of PWM waveform , which is output, from TIOCA3 and TIOCB3 pins.
ITU3_GRB TIOCA3 is a PB0 pin. Waveform of duty ratio set by ITU3_GRB is output from PB0 pin.
TIOCB3 is a PB1 pin and reversing waveform of PB0 pin is output.

Setting of PWM waveform , which is output, from TIOCA4 and TIOCXA4 pins.
ITU4_GRA TIOCA4 is a PB2 pin. Waveform of duty ratio set by ITU4_GRA is output from PB2 pin.
TIOCXA4 is a PB4 pin and reversing waveform of PB2 pin is output.

Setting of PWM waveform , which is output, from TIOCB4 and TIOCX B4 pins.
ITU4_GRB TIOCB4 is a PB3 pin. Waveform of duty ratio set by ITU4_GRB is output from PB3 pin.
TIOCX B4 is a PB5 pin and reversing waveform of the PB3 pin is output.
Output state is explained by the illustration.
0
GRB3 ITU_TOER Current output signal
+1
PB0

GRA3
"0" PB0
+1
CNT3
PB1
PB1

PB2

GRA3
"0" PB2
+1
CNT3
PB4
GRA4 PB4
+1
0

PB3

GRA3
"0" PB3
+1
CNT3
PB5
PB5
Synchro
-nized GRB4+1

1. In reset-synchronized PWM mode, three pairs of complementary PWM waveform are produced with
ITU3_CNT in common.
2. PWM output pins are decided each pins assignment already and pins which affect ITU3_GRB become PB0 and
PB1, pins which affect ITU4_GRA become PB2 and PB4, and pins that affect ITU4_GRB become PB3 and
PB5 respectively. PB0, PB2 and PB3 are PWM waveforms which start from logical level “0”, and PB1, PB4
and PB5 are output reverse/complementary waveform of each pin output.
3. ITU_TOER can select using pin for a PWM output or for an input-output port. This time PB1, PB4 and PB5 are
used. PB0, PB2 and PB3 actually do not output in PWM waveform but those are explained assuming that it is
to be output.

- 60 -
Program Explanation Manual “kit05.c”

0
GRB3 ITU_TOER Current output signal
+1
PB0

GRA3
"1" PB0
+1
CNT3
PB1
PB1

PB2

GRA3
"0" PB2
+1
CNT3
PB4
GRA4 PB4
+1
0

PB3

GRA3
"0" PB3
+1
CNT3
PB5
PB5

GRB4+1

4. Logical level of output waveform of PB0 and PB1reverses when becomes “TU3_CNT=(ITU3_GRB + 1)”. Till
now PB1 was “1” but this time it reverses and becomes “0”. ON width of PB1 becomes period of 1 count of
ITU3_CNT (ITU3_GRB + 1).

0
GRB3 ITU_TOER Current output signal
+1
PB0

GRA3
"1" PB0
+1
CNT3
PB1
PB1

PB2

GRA3
"1" PB2
+1
CNT3
PB4
GRA4 PB4
+1
0

PB3

GRA3
"0" PB3
+1
CNT3
PB5
PB5

GRB4+1

5. Output waveform of PB2 and PB4 reverses when becomes "ITU3_CNT=(ITU4_GRA + 1)". Till now PB4 was
"1" but this time it reverses and becomes "0". ON width of PB4 becomes period of 1 count of ITU3_CNT
(ITU4_GRA + 1).

- 61 -
Program Explanation Manual “kit05.c”

0
GRB3 ITU_TOER Current output signal
+1
PB0

GRA3
"1" PB0
+1
CNT3
PB1
PB1

PB2

GRA3
"1" PB2
+1
CNT3
PB4
GRA4 PB4
+1
0

PB3

GRA3
"1" PB3
+1
CNT3
PB5
PB5

GRB4+1

6. Output waveform of PB3 and PB5 reverses when becomes "ITU3_CNT=(ITU4_GRB + 1)". Till now PB5 was
"1" but this time it reverses and becomes "0". ON width of PB5 becomes
period of 1 count of ITU3_CNT (ITU4_GRB + 1).

0 Current output signal


GRB3 ITU_TOER
+1
PB0

GRA3
"0" PB0
+1
CNT3
PB1
PB1

PB2

GRA3
"0" PB2
+1
CNT3
PB4
GRA4 PB4
+1
0

PB3

GRA3
"0" PB3
+1
CNT3
PB5
PB5

GRB4+1

7. Entire waveform reverses all at once when “ITU3_CNT = (ITU3_GRA + 1)”.

- 62 -
Program Explanation Manual “kit05.c”

When ITU3_CNT=(ITU3_GRA+1)
ITU3_CNT becomes 0.

0
GRB3 ITU_TOER Current output signal
+1
PB0

GRA3
"0" PB0
+1
CNT3
PB1
PB1

PB2

GRA3 "0" PB2


+1
CNT3
PB4
GRA4 PB4
+1
0

PB3

GRA3
"0" PB3
+1
CNT3
PB5
PB5

GRB4+1

8. ITU3_CNT becomes 0 and begins counting because we set in ITU3_TCR


"if becomes ITU3_CNT=(ITU3_GRA + 1) ITU3_CNT is clear ".

Beside, there are some shortfalls when reset-synchronized PWM mode is used. It is explained.

Waveform might be incorrect.

1 Cycle

Assumed shape of waves

Output shape of waves

Shape was not Shape was reversed


reversed here. here.

To change the duty ratio, ITU3_GRB, ITU4_GRA and ITU4_GRB are rewritten. If rewriting of each general
register and compare match of ITU3_CNT with each general register (ITU3_CNT= each general register+1), are
overlapped, the rewriting of each general register is given priority and so waveform is not reversed because the
compare match is ignored.

- 63 -
Program Explanation Manual “kit05.c”

void main( void ) 0


{
init();
while( 1 ) { CNT3
ITU3_GRB = 4999; GRA3
PB0
… +1
… PB1
}
}
GRB3+1

At this time, program is being executed At this time, value of ITU3_CNT

When ITU3_GRB=4999 of program comes and at the same time


value becomes ITU3_CNT= (ITU3_GRB+1), priority is given to
write the value in GRB, and wave shape is not reversed !!

It is equipped with a function to prevent this. It is a register called as buffer register.

Register name Explanation


It is a buffer of ITU3_GRA (ITU3 buffer register A). This time, ITU3_GRA does not go with
ITU3_BRA
buffer, as it does not change when it is set once for cycle setting.
It is a buffer of ITU3_GRB (ITU3 buffer register B). In the program, value is written in
ITU3_BRB ITU3_BRB without writing the value in ITU3_GRB and competition with compare match is
evaded.
It is a buffer of ITU4_GRA (ITU4 buffer register A). In the program, value is written in
ITU4_BRA ITU4_BRA without writing the value in ITU4_GRA and competition with compare match is
evaded.
It is a buffer of ITU4_GRB (ITU4 buffer register B). In the program, value is written in
ITU4_BRB ITU4_BRB without writing the value in ITU4_GRB and competition with compare match is
evaded.

Value of buffer register automatically transfers to general register after waveform is reversed when buffer
register is to be set for the use. Waveform reverses, moment at the time of which value becomes
ITU3_CNT=(ITU3_GRB + 1). Competition with compare match does not occur because value is written in buffer
register from first to last in the main program.

void main( void ) 0


{
init();
while( 1 ) { CNT3
ITU3_BRB = 4999; GRA3
PB0
… +1
… PB1
}
}
GRB3+1

BRB3

Value becomes ITU3_CNT=(ITU3_GRB+1) and waveform reverses.


Afterwards, the value of ITU3_BRB is automatically transferred to ITU3_GRB.
The situation that waveform cannot be reversed by the competition disappears because ITU3_GRB is
not rewritten directly.

Setting like “Automatic transfer” is a function of ITU_FCR which is explained just before.

- 64 -
Program Explanation Manual “kit05.c”

Set contents of ITU_FCR (Timer Function Control Register)


Bit: 7 6 5 4 3 2 1 0
ITU_FCR: − − CMD1 CMD0 BFB4 BFA4 BFB3 BFA3
Setup value: 0 0 1 1 1 1 1 0
Hexadecimal :
3 e

Bit 5 and 4:Combinations Mode 1 and 0 (CMD1,CMD0)


These bits select whether channels 3 and 4 operate in normal mode, complementary PWM mode,
or reset-synchronized PWM mode.
CMD1 CMD0 Explanation
0 0
Channels 3 and 4 operate normally.
0 1
1 0 Channels 3 and 4 operate together in complementary PWM mode.
1 1 Channels 3 and 4 operate together in reset-synchronized PWM mode.

This is as explained above.

Bit3: Buffer Mode B4 (BFB4)


Selects whether ITU4_GRB operates normally in channel 4, or whether ITU4_GRB is buffered
by ITU4_BRB.
BFB4 Explanation
0 ITU4_GRB operates normally.
1 ITU4_GRB is buffered by ITU4_BRB.

Since ITU4_GRB is used as duty setting, setting above is assumed to be buffer operation.

Bit2: Buffer Mode A4 (BFA4)


Selects whether ITU4_GRA operates normally in channel 4, or whether ITU4_GRA is buffered
by ITU4_BRA.
BFA4 Explanation
0 ITU4_GRA operates normally.
1 ITU4_GRA is buffered by ITU4_BRA.

Since ITU4_GRA is used as duty setting, setting above is assumed to be buffer operation.

Bit1: Buffer Mode B3(BFB3)


Selects whether ITU3_GRB operates normally in channel 4, or whether ITU3_GRB is buffered
by ITU3_BRB.
BFB3 Explanation
0 ITU3_GRB operates normally.
1 ITU3_GRB is buffered by ITU3_BRB.

Since ITU3_GRB is used as duty setting, setting above is assumed to be buffer operation.

- 65 -
Program Explanation Manual “kit05.c”

Bit0: Buffer Mode A3 (BFA3)


Selects whether ITU3_GRA operates normally in channel 4, or whether ITU3_GRA is buffered
by ITU3_BRA.
BFA3 Explanation
0 ITU3_GRA operates normally.
1 ITU3_GRA is buffered by ITU3_BRA.

If once ITU3_GRA is set, there is nothing to change because it is used for a period setting register and the period
is constant. Since there is no competition, ITU3_GRA operates normally.

392: ITU3_GRA = PWM_CYCLE; /* period setting */

The period is set. Since PWM_CYCLE is set in 49151 by define sentence, this value is assigned.

393: ITU3_GRB = ITU3_BRB = 0; /* PWM setting of Left motor */

The PWM output, which is set by ITU3_GRB, is output from PB0 and its reversing signal is output from PB1.
The PB0 is turned off and the left motor drive circuit is connected with PB1. The value “0” is written because at
the first, the motor is stopped. At the same time buffer register is cleared. This is a one time limit to write the value
in ITU3_GRB.

394 : ITU4_GRA = ITU4_BRA = 0; /* PWM setting of Right motor */

PWM output signal and its reversing signal are output from PB2 and PB4 that are set by ITU4_GRA. PB2 is OFF
and the right motor drive circuit is connected with PB4. Value “0” is written as at the beginning, the right motor is
kept stopped. Buffer register is also cleared at the same time. This is a one time limit to write the value in
ITU4_GRA.

395 : ITU4_GRB = ITU4_BRB = SERVO_CENTER; /* PWM setting of Servo */

PWM output signal and its reversing signal are output from PB3 and PB5 that are set by ITU4_GRB. PB3 is OFF
and the Servo is connected with PB5. At the beginning, center value is written, as it is preferable to point the
steering in straight direction. Buffer register is also written at the same time. This is a one time limit to write the
value in ITU4_GRB.

- 66 -
Program Explanation Manual “kit05.c”

0% cannot be output

When compare match occurs, toggle operation is carried out.


The waveform of PB1, which is reversed by compare match , is made an example.
Logical level changing timing is,
When ITU3_CNT = (ITU3_GRB + 1), it becomes "1"->"0". (Logical level “1” is reversed to “0”.)
When ITU3_CNT = (ITU3_GRA + 1, it becomes "0"->"1". (Logical level “0” is reversed to “1”.)
This timing decides the time of cycle.

The value “0” is set in ITU3_GRB which duty ratio is decided, because it is to be 0%. ITU3_GRA is assumed to
be 999.

1Cycle 1 Cycle

Output waveform

Value of ITU3_CNT 0 1000→0

It becomes "1"→"0" in CNT=(GRB+1)=(0+1)=1. It becomes "0"→"1"


(”1” is reversed to “0”.) in CNT=(GRA+1)=(999+1)=1000.
Even if GRB = 0, during only one count of CNT, it (“0” is reversed to “1”.)
becomes “1” because GRB is always added one count.
And 325.52 [ns] of this time, is always becomes "1".

Perfect 0% is not possible like this. Always during 1 count of ITU3_CNT becomes ON. Since at this time it is
period of 16 [ms], it can be entirely ignored as using motor control, becaused it is (325.52 10-9) / (16 10-3) =
0.00002 = 0.002%. It is no problem that we say it 0%.

100% cannot be output


When compare match occurs, toggle operation is carried out.
The waveform of PB1, which is reversed by compare match , is made an example.
Logical level changing timing is,
When ITU3_CNT = (ITU3_GRB + 1), it becomes "1"->"0". (Logical level “1” is reversed to “0”.)
When ITU3_CNT = (ITU3_GRA + 1), it becomes "0"->"1". (Logical level “0” is reversed to “1”.)
This timing decides the time of cycle.

Since it is to be 100% duty ratio, ITU3_GRA’s value which is corresponding with the period of this PWM
waveform, is set in ITU3_GRB. ITU3_GRA is assumed to be 999.

1Cycle 1 Cycle

Output waveform

Value of ITU3_CNT 1000->0 1000->0

Reverse by (ITU3_CNT=(GRB+1)=(999+1)=1000
and ITU3_CNT=(GRA+1)=(999+1)=1000).
Reversing is done only once because it agrees at the
same time.

- 67 -
Program Explanation Manual “kit05.c”

When the values of ITU3_GRA (cycle) and ITU3_GRB (duty ratio) are the same like this, the corresponding
timing with ITU3_CNT becomes the same and reversing is carried out only once. It becomes waveform, which is
not the PWM of cycle 16 [ms] and waveform of the result becomes, 1 cycle 0%, next 1 cycle 100%.

So, 1 smaller value than ITU3_GRA is written in ITU3_GRB.

1 cycle 1 cycle

Output waveform

Value of ITU3_CNT 0 1000→0


999

It becomes "1"-> "0" in It becomes “0”->”1” in CNT=(GRA+1)=(999+1)=1000


CNT=(GRB+1)=(998+1)=999 (reverse)
(reverse) Always 1 count of CNT is turned OFF and this time it
turned OFF during 325.52[ns].

Thus, 100% completion cannot be output. Always one count of ITU3_CNT becomes OFF.

The right motor, left motor and servo are controlled with any consideration for these.

- 68 -
Program Explanation Manual “kit05.c”

3.11 Waiting: timer function

When this function is called, waiting is done in this line. As a usage method, numerical value is input in the
argument of the timer function in milliseconds.

413 : /************************************************************************/
414 : /* Timer main unit */
415 : /* Argument Timer value 1=1ms */
416 : /************************************************************************/
417 : void timer( unsigned long timer_set )
418 : {
419 : cnt0 = 0;
420 : while( cnt0 < timer_set );
421 : }

For instance, if it is assumed that the argument is 1000, timer_set becomes 1000. It becomes

420 : while( 0 < 1000 );

as cnt0 becomes 0 on 419th line. In such cases, continuation conditional expression in brackets is always satisfied,
and there seems to be no further process. Recall the place where cnt0 is operated. cnt0 does +1 by generating the
interrupt of each 1ms in the middle of executing interrupt_timer0 function. Hence after 1ms it becomes

420 : while( 1 < 1000 );

, and as time passes after 1000ms it becomes

420 : while( 1000 < 1000 ); <-The continuation conditional expression does not come into effect!!

and the expression in this brackets is judged as false (not consisted) and goes to the next line. As a result, flow of
program execution waits for 1000ms at 420th line. And the role of the timer is done according to the name of the
function.
For instance, if waiting of 2 seconds is preferred, it becomes

timer ( 2000 );

because 2 seconds =2000ms.


However, the timer function is "Wait idling for specified time" function. As micom car runs on course, if the
waiting is done without seeing the sensor for as long as 2 seconds, it runs off the course. Therefore, time is
measured by another method without using the timer function in the micom car program. Details are described
later.

- 69 -
Program Explanation Manual “kit05.c”

3.12. Sensor state reading : sensor_inp function

This function reads “White”, “Black” information from sensor board. Argument is a value by which the sensor is
masked.

423 : /************************************************************************/
424 : /* Sensor state detection */
425 : /* Argument Mask value */
426 : /* Return value Sensor value */
427 : /************************************************************************/
428 : unsigned char sensor_inp( unsigned char mask )
429 : {
430 : unsigned char sensor;
431 :
432 : sensor = P7DR;
433 :
434 : /* Since bit0 is for left and bit7 is for right in the new sensor board which is reversed */
435 : /* to the previous sensor board, the bit has been replaced to maintain compatibility. */
436 : sensor = bit_change( sensor ); /* Bit replacement */
437 : sensor &= mask;
438 :
439 : return sensor;
440 : }

430 : unsigned char sensor;


It is a local variable to maintain the input sensor value. Variable that maintains the port value is assumed as
unsigned char type (Width of 8 bits without code).

432 : sensor = P7DR;


Sensor value is input from port 7 and maintained in sensor variable because output of sensor is connected in port 7
connector of CPU board. For instance, if sensor state is “ ” ( : black : white), it becomes
sensor=0xf8.

State of sensor
00011111
P7DR 11111000

Reading
sensor 11111000

If the sensor state is read, it is "00011111", but which is being actually read is the reversing of left and right
i.e."11111000". Sensor bit position and reading bit position is in reverse, i.e. right of sensor bit position is left of
reading bit position.
436 : sensor = bit_change( sensor );
Still, if the state is "00011111", it is clear that it is easy to consider also in program as "0001111". Right and left
can be replaced in program for this purpose. It is bit_change function, which does this replacing operation.
"sensor" in the beginning is value after conversion and "sensor" in the bracket is value before conversion.

- 70 -
Program Explanation Manual “kit05.c”

State of sensor
00011111
P7DR 11111000

Reading
sensor 11111000
Sensor is replaced by
bit_change function.
sensor 00011111

437 : sensor & = mask;


AND operation is done for sensor value and mask value and sensor is mask processed. “ sensor &= mask;” is
same as “sensor = sensor & mask”.

About mask process

"sensor" is covered with the mask. Checked sensor state must be minimized by compulsorily making the bit other
than the examined bit to "0".
The unit of 1 port is 8 bits therefore it cannot be checked only by 1 bit (It is possible if the method called as bit
field is used and here it is not used). It becomes a check of group 8 bits.
For instance, when checking that bit 7 at the left end of sensor is “1” or not, if it is done as

if( Sensor value ==0x80 ) {


/* If bit 7 is "1", statements in braces are executed. */
}

It is considered OK. However, we can’t understand whether the value of bit 6~0 will become “1” or 0”.
For instance, if bit 7 is “1” and bit 0 is also “1”, then it becomes
Sensor value = 10000001(Binary number)=0 x 81(Hexadecimal number)
It cannot be judged whether bit 7 is "1" or “0”, only by checking if it is 0 x 80 in program. In such cases, operation
called "Mask" is done, as it is not possible to check properly. When they say mask, it is the one associated with
mask that is used at the time of having cold. The mask for the cold is used to prevent the bacteria from spreading,
however the mask mentioned here resembles in only the physical appearance of the mask. If mask is put on mouth
cannot be seen. It is hidden. The mask mentioned here has only the meaning “Cover”.
In the previous example, only bit 7 is required to know because other bits (from bit0 to 6) are covered by having
mask and covered up, in other words it is made to ignore.

- 71 -
Program Explanation Manual “kit05.c”

Then, how to ignore it? In actual control, it is only made compulsorily “0”.
In the program, the logical multiplication (product) of the logical operation, that is, logical AND is done.
Logical AND is an operation in which, “1” is output when both A and B are “1” (each numerical value of “0” or
”1”). A is considered as sensor value.
A(Sensor value) B A and B
0 0 0
0 1 0
1 0 0
1 1 1

Here, note when B is “0”.


A(Sensor value) B A and B
0 0 0
1 0 0

Even if A (sensor value) may be in any value , when B is "0", the result is sure to become "0".
Next, note when B is “1”.
A(Sensor value) B A and B
0 1 0
1 1 1

If B is “1”, the result becomes the value of A (sensor value).


If actually replaced, A is equivalent to sensor value and B is equivalent to mask value. Mask value makes
necessary bit in "1" and unnecessary bit in "0". And, unnecessary bit is assumed to be "0" in program because
when logical AND is done, unnecessary bit is sure to become "0".

Thus, mask must compulsorily make the unnecessary bit "0" by logical AND.
For instance, when sensor state is "black black black white white white white white", sensor value is "00011111".
It is assumed that only bit 2 and bit 1 are necessary to check and other are not necessary.

bit 7 6 5 4 3 2 1 0
Unnecessary Unnecessary Unnecessary Unnecessary Unnecessary Necessary Necessary Unnecessary

Mask value of unnecessary bit is made "0" and logical AND is done to adjust the unnecessary part to "0".
Therefore, mask value only has to rewrite unnecessary part on "0" and necessary part on "1" of the above table.

bit 7 6 5 4 3 2 1 0
Mask
0 0 0 0 0 1 1 0
value

If "00000110" is converted in Hexadecimal number, it becomes "0 x 06". The table below is the calculation
method and the result.

State of sensor
00011111
Sensor value 00011111
Mask value 00000110 )AND

Result 00000110 Return value

- 72 -
Program Explanation Manual “kit05.c”

For instance, it becomes as follows when it is required to check whether bit 2 ="1", bit 1 = "0" or not.

If (sensor_inp (0x06) == 0x04) {


/* If bit 2="1" and bit 1 ="0", statements in braces are executed. */
}

Only bit 2 and bit 1 can be examined with ease because it is known that bits other than bit 2 and bit 1 become "0"
compulsorily by mask.

Mask pattern

It is explained that the mask value is put in the argument of the sensor_inp function. Actually, the mask value is
limited in case of creating program of the micom car. It is defined in advance.

38 : /* Mask value setting ×: There is mask(Invalid) : No mask(Valid) */


39 : #define MASK2_2 0x66 /* × ×× × */
40 : #define MASK2_0 0x60 /* × ××××× */
41 : #define MASK0_2 0x06 /* ××××× × */
42 : #define MASK3_3 0xe7 /* ×× */
43 : #define MASK0_3 0x07 /* ××××× */
44 : #define MASK3_0 0xe0 /* ××××× */
45 : #define MASK4_0 0xf0 /* ×××× */
46 : #define MASK0_4 0x0f /* ×××× */
47 : #define MASK1_1 0x81 /* ×××××× */

"MASK _ " means that the number of “1” on the left side are bits, and the number of “1” on the right side are
bits and the remaining are made compulsorily “0”.

It can be rewritten as

if( sensor_inp( MASK0_2 ) == 0x04 ) {


/* If bit 2= "1", bit1="0", statements in braces are executed. */
}

because the mask pattern that checks previous bit2 and bit1 is as per the above table “MASK0_2”.

- 73 -
Program Explanation Manual “kit05.c”

3.13. Change bit: bit_change function

The bit is changed by "bit_change function" and "bit_change function" is used in the sensor_inp function, so
"bit_change function" is also explained along with this. Argument “in” is the 8_bit value before changing the bit.
The return value is a value after changing the bit.

546 : /************************************************************************/
547 : /* Bit change */
548 : /* Argument Value to be changed */
549 : /* Return value Changed value */
550 : /************************************************************************/
551 : char unsigned bit_change( char unsigned in )
552 : {
553 : unsigned char ret;
554 : int i;
555 :
556 : for( i = 0; i < 8; i++ ) {
557 : ret >>= 1; /* Right shift of return value */
558 : ret |= in & 0x80; /* Ret bit7 = in bit7 */
559 : in <<= 1; /* Left shift of argument */
560 : }
561 : return ret;
562 : } Range repeated with for

553 : unsigned char ret;


Variable “ret” in which the return value is saved.

554 : int i;
Variable "i" for working

556 : for( i = 0; i < 8; i++ ) {


It means “ i ” is set to 0 and processed in “for { }” statement, after processing “ i ”is made +1, if “i”<8 then
repeat. Since “ i “ starts from 0, repeats 8 times between 0~7. One byte is processed by repeating 8 times.

557 : ret >>= 1; /* Right shift of return value */


Return value "ret" shifts one bit to the right. This is the same as dividing by 2. The value of bit0 which
disappears by shifting right is thrown away, and "0" enters in bit7.

558 : ret |= in & 0x80; /* Ret bit7 = in bit7 */


The value of bit7 of source data “in” and bit7 of “ret” are operated in logical OR. Since bit7 of “ret” is always
"0", value of bit7 of “in” is forwarded to bit7.

559 : in <<= 1; /* Left shift of argument */


Source data "in" shifts one bit to the left. This is the same as multiplying with 2.

560 : }
Close bracket of loop in “for statement”. Rows 557~559 are repeated.

- 74 -
Program Explanation Manual “kit05.c”

For instance, when 0xf8 is changed right for left and the function is called as below,

c = bit_change( 0xf8 );

0x1f is substituted for variable c. Calculation process becomes an image like the figure below.

i in ret Explanation

00000000 Though “ret” is shifted to right, there is no change


0 → because all bits are “0” at the beginning.

11111000 10000000 Bit7 of “in” and bit7 of “ret” are operated in logical
OR.

11110000 It shifts one bit to the left, “0” is entered in bit0.


01000000 “ret” shifts to the right, the prior “1” moves to bit6 and
1 → “0” is inserted in bit7.

11110000 11000000 Bit7 of “in” and bit7 of “ret” are operated in logical
OR.

11100000 “in” shifts one bit to the left, “0” is entered in bit0.

00011111 “ret” shifts to the right, prior “0” moves to the bit6,
7 → “0” is inserted in bit7.

00000000 00011111 Bit7 of “in” and bit7 of “ret” are operated in logical
OR. No change since bit7 of “in” is “0” and also bit7
of “ret” is “0”.
00000000
← “in” shifts one bit to the left, “0” is entered in bit0.

End, the bit change value is assumed to be a return


8 00011111
value.

The bit change is completed, and “0x1f" of the return value is substituted in variable c.

- 75 -
Program Explanation Manual “kit05.c”

3.14. Crossline Check: check_crossline function

There are 2 crosslines before 50cm ~ 100cm of crank. These are called crosslines. A special function to detect this
crossline was created. The return value is, "1" if there is crossline and "0" if no crossline.

442 : /************************************************************************/
443 : /* Crossline detection processing */
444 : /* return value 0: no crossline 1: crossline exists */
445 : /************************************************************************/
446 : int check_crossline( void )
447 : {
448 : unsigned char b;
449 : int ret;
450 :
451 : ret = 0;
452 : b = sensor_inp(MASK2_2);
453 : if( b==0x66 || b==0x64 || b==0x26 || b==0x62 || b==0x46 ) {
454 : ret = 1;
455 : }
456 : return ret;
457 : }

451 : ret = 0;
Variable “ret” in which the return value is saved has been initialized. If it is judged as crossline, “1” is entered in
this variable and if as no crossline, “0” is entered in this variable. Since at present it is unknown, it is temporarily
judged as no crossline and “0” is entered.

452 : b = sensor_inp(MASK2_2);
The sensor is read, and saved in variable b. Since the mask value of the sensor is "MASK2_2=0x66", 4 sensors,
such as left middle 2 and right middle 2 are read as shown below.

453 : if( b==0x66 || b==0x64 || b==0x26 || b==0x62 || b==0x46 ) {


In “if” statement, there are parentheses just after “if”. Expression between parentheses is called “cotrolling
expression” normally. But stated quite simply, it is called “conditionaly expression” or only “expression”.
The state of the sensor is checked.
When sensor is any of 0x66 or 0x64 or 0x26 or 0x62 or 0x46, the value of “cotrolling expression” is “true”, and
if sensor is neither, the value of “cotrolling expression” is “false”. If each state is illustrated, it becomes as shown
in the following figure.

- 76 -
Program Explanation Manual “kit05.c”

0x 6 6
0 1 1 0 0 1 1 0

becomes "0" due to


defective adjustment.

0x 6 4 0x 62

0 1 1 0 0 1 0 0 0 1 1 0 0 0 1 0

0 x2 6 0x 46

0 0 1 0 0 1 1 0 0 1 0 0 0 1 1 0

becomes "0" due to defective adjustment.

When sensor board enters to clossline from the direct front and sensor input-pattern become to 0x66, all 4 sensors
become "1" and this is judged unquestionably a crossline. When sensor-pattern is 0x64 or 0x26, the board enters
obliquely from left or right, and it is judged as crossline since more than 3 have become "1". When sensor-pattern
is 0x62, it seems 0x64 or 0x66 in the figure. The sensor can adjust the sensitivity by the volume. It is not a
problem if it is adjusted such that all sensors light up at the same time under the same condition but, when the
sensitivity of the second sensor from the right (shown with the arrow in figure 0x62) is weak due to insufficient
adjustment; the state of the sensor is judged to be 0x62 and not 0x64. However, 0x62 is also included as the
sensor-pattern of detected clossline because there is no problem as shown in figure. 0x46 is also similar.

3.15. Reading of DIP switch: dipsw_get function


It is a function which reads the 4-bit value of the DIP switch in CPU board.

459 : /************************************************************************/
460 : /* Reading of DIP switch value */
461 : /* Return value Switch value 0 ~15 */
462 : /************************************************************************/
463 : unsigned char dipsw_get( void )
464 : {
465 : unsigned char sw;
466 :
467 : sw = ~P6DR; /* Reading of DIP switch */
468 : sw &= 0x0f;
469 :
470 : return sw;
471 : }

- 77 -
Program Explanation Manual “kit05.c”

467 : sw = ~P6DR; /* Reading of DIP switch */


Data is read from port 6 that has the DIP switch. If DIP switch is OFF, the value from the port is "1" and if DIP
switch is ON, then "0". Just because OFF corresponding to "0" and ON corresponding to "1" is easy to understand,
it is reversed by “ ~ ”. “~” is called “tilde-key”, and when the ^-key which is left side of \-key is pushed with
shift key, it becomes ~.

468 : sw &= 0x0f;


Since the DIP switch is only bit3 ~ 0, the part of bit7 ~ 4 is masked and forcibly made "0".

For instance, it is assumed the state of DIP switch to be OFF, OFF, ON, and ON.

c = dipsw_get();

The function is called like above statement. In this case, it becomes an image like the figure below.
0x03 is entered in variable c.

Read
P6DR ????1100
P63
P62
P61
P60

Inversion (by “tilde”)

OFF sw ????0011

Mask
ON
sw 00000011
State of switch
Return value

sw 00000011

3.16. Reading of push switch: pushsw_get function

It is a function which reads the state of the push switch of the motor drive board. The push switch circuit is as
follows.

To CPU board

The push switch circuit is connected with bit 0 of port B. In the circuit, when the push switch is ON, bit 0 is input
"0" and when OFF, bit 0 is input "1". But this function must be such that return value becomes “1” when switch is
ON and "0" when switch is OFF.

- 78 -
Program Explanation Manual “kit05.c”

473 : /************************************************************************/
474 : /* Push switch value reading */
475 : /* Return value Switch value ON: “1” and OFF: “0” */
476 : /************************************************************************/
477 : unsigned char pushsw_get( void )
478 : {
479 : unsigned char sw;
480 :
481 : sw = ~PBDR; /* Reading of port having a push switch */
482 : sw &= 0x01;
483 :
484 : return sw;
485 : }

481 : sw = ~PBDR; /* Reading of port having a push switch */


Data is read from port B that has a push switch. If the push switch is OFF, the value from bit 0 of the port B is “1”
and if the push switch is ON, then "0". So, it is inverted by "~". "~" is called “tilde-key”, and when the ^-key
which is left side of \-key is pushed with shift key, it becomes ~.

482 : sw & = 0x01;


Since the push switch is only bit0, the bit7-1 part is mandatorily made "0" by masking. The mask value becomes
'0000 0001' in binary number and 0x01 in hexadecimal number.

The figure below shows an example of pushing the switch.

PBDR

7 ?

6 ?

5 ? PBDR ? ? ? ? ? ? ? 0

Inversion
4 ?
sw ? ? ? ? ? ? ? 1
3 ?
Mask
Return
2 ? 0 0 0 0 0 0 0 1
Value
ON
1 ?

0 0
Push ? something unknown
switch

- 79 -
Program Explanation Manual “kit05.c”

3.17. Control of LED: led_out function

Three LEDs are attached to the motor drive board. Out of those, two LEDs can be switched ON/OFF by the
microcomputer.

LED1 LED0

To CPU board
Glows when power
supply is given

It is connected with bit7 and bit6 of port B. LED connected with bit7 is "LED1" in the program and LED
connected with bit6 is "LED0" in the program.
In this function, the argument and method of lighting LED is as shown in the following table.

Argument In binary number LED1 LED0


0 00 Switch off Switch off
1 01 Switch off Switch on
2 10 Switch on Switch off
3 11 Switch on Switch on

In short, when the argument is made a binary number, the first digit is made for control of LED0 and second digit
for LED1. It is switched off when signal is "0"and switched on when signal is "1".

487 : /************************************************************************/
488 : /* LED control */
489 : /* Argument Switch value (LED1, LED0) = (bit1 : bit0) :"0": OFF, "1": ON */
490 : /* Example 0x3->(LED1, LED0)=(ON, ON) : 0x2->(LED1, LED0)=(ON, OFF) */
491 : /************************************************************************/
492 : void led_out( unsigned char led )
493 : {
494 : unsigned char data;
495 :
496 : led = ~led;
497 : led <<= 6;
498 : data = PBDR & 0x3f;
499 : PBDR = data | led;
500 : }

496 : led = ~led;


The argument "1" is ON and "0" is OFF, but LED is actually ON when the signal is "0" and OFF when signal is
"1". Therefore, the signal is reversed here so as to be reasonable.

- 80 -
Program Explanation Manual “kit05.c”

497 : led <<= 6;


Though argument enters as the value of bit 1 and bit 0, since the actual LED is in bit 7 and bit 6, the value must
be shifted by 6 bits to left.

498 : data = PBDR & 0x3f;


The present output value of port B is read. And bit 7 and bit 6 for LED output, is forcibly made "0" by
masking 0x3f.

499 : PBDR = data | led;


The LED data which is controlled at this time is newly written in port B.
There is no effect except on bit 7 and bit 6 by this rewriting in port B.

For example,

led_out ( 0x02 );

function is assumed to be called. In this case, it becomes an image like the figure below. LED1 is ON, LED0 is
OFF.

led data
0 0 0 0 0 0 1 0 Value of port B LED1 LED0
PBDR
Inversion by “~” Read
0 7
1 1 1 1 1 1 0 1 ? ? ? ? ? ? ? ?
ON! OFF
1 6
Mask of 0x3f
6 bit shift
0 1 0 0 0 0 0 0 0 0 ? ? ? ? ? ? ? 5

? 4

Logical OR
? 3

? 2
0 1 ? ? ? ? ? ?
? 1

? 0
?is no change

- 81 -
Program Explanation Manual “kit05.c”

3.18. Motor Speed Control: speed function

It is a function which controls the left motor and right motor. The argument can be set from 100 to -100 and
normal rotation 100% corresponds to argument value “100”, reverse rotation 100% corresponds to argument
value “-100”, and braking state corresponds to argument value “0”.
The connection of the motor drive board is confirmed again.

Signal,
Pin no. Details “0” “1” Details
direction
1 - +5V
2 board<-PB7 LED1 ON OFF
3 board<-PB6 LED0 ON OFF
Duty ratio setting by
4 board<-PB5 Servo signal PWM signal
ITU4_BRB
Duty ratio setting by
5 board<-PB4 Right motor PWM Stop Operate
ITU4_BRA
Direction of right Normal Reverse
6 board<-PB3
motor rotation rotation rotation
Direction of left Normal Reverse
7 board<-PB2
motor rotation rotation rotation
Duty ratio setting by
8 board<-PB1 Left motor PWM Stop Operate
ITU3_BRB
9 board->PB0 Push switch Pushed Not pushed
10 - GND

As shown in the figure, PB4 is for right motor PWM, PB1 is for left motor PWM, PB3 is for rotation direction
control of right motor, and PB2 is for rotation direction control of left motor. Moreover, since reset-synchronized
PWM mode is used, the value of ITU4_BRA is changed to change the duty ratio of PB4 and the value of
ITU3_BRB is changed to change the duty ratio of PB1.

Buffer is ITU3_BRB

0
GRB3 ITU_TOER
+1
PB0

GRA3 "0" PB0 Usual I/O port

+1
CNT3
PB1
PB1 To left motor

PB2

GRA3 "0" PB2 Usual I/O port

+1
CNT3
PB4
GRA4 PB4 To right
+1 motor
0
Buffer is ITU4_BRA
PB3

GRA3 "0" PB3 Usual I/O port


+1
CNT3
PB5
PB5 To Servo

interlock GRB4+1

- 82 -
Program Explanation Manual “kit05.c”

502 : /************************************************************************/
503 : /* Speed Control */
504 : /* Argument Left motor: -100 ~ 100 , Right motor: -100 ~ 100 */
505 : /* 0:Stop,100:normal rotation 100%,-100:Reverse 100% */
506 : /************************************************************************/
507 : void speed( int accele_l, int accele_r )
508 : {
509 : unsigned char sw_data;
510 : unsigned long speed_max;
511 :
512 : sw_data = dipsw_get() + 5; /* DIP switch read */
513 : speed_max = (unsigned long)(PWM_CYCLE-1) * sw_data / 20;
514 :
515 : /* Left motor */
516 : if( accele_l >= 0 ) {
517 : PBDR &= 0xfb;
518 : ITU3_BRB = speed_max * accele_l / 100;
519 : } else {
520 : PBDR |= 0x04;
521 : accele_l = -accele_l;
522 : ITU3_BRB = speed_max * accele_l / 100;
523 : }
524 :
525 : /* Right motor */
526 : if( accele_r >= 0 ) {
527 : PBDR &= 0xf7;
528 : ITU4_BRA = speed_max * accele_r / 100;
529 : } else {
530 : PBDR |= 0x08;
531 : accele_r = -accele_r;
532 : ITU4_BRA = speed_max * accele_r / 100;
533 : }
534 : }

512 : sw_data = dipsw_get() + 5; /* DIP switch read */


“dipsw_get” function returns the value of DIP switch on CPU board to the above right expression. The value is 0 ~
15. Since there is “+5” in the right expression, the value of sw_data becomes 5 ~ 20, depending on the DIP switch
value.
513 : speed_max = (unsigned long)(PWM_CYCLE-1) * sw_data / 20;
A maximum speed is decided by this statement. This “speed_max” becomes 100%-speed value.
When above statement is described with another way, it becomes as follows.

1 2 3

sw_data(5 ~ 20)
speed_max = (unsigned long) Value when 100%
20

1 Since 2 is PWM_CYCLE-1, it becomes 49151-1=49150. Since the numerator of 3 is 20 or less and 5 or more,
it becomes 49150 20=983000. Since the (unsigned int) type range is exceeded, correct calculation is not
possible as it becomes over flow when it becomes more than 65536. Therefore, it is temporarily converted into
the unsigned long type. The calculation result does not exceed 65536 and also must not exceed during the
calculation.
2 100% is set as 1 small value than ITU3_GRA as explained in reset-synchronized PWM mode setting. Since
PWM_CYCLE is a value set in ITU3_GRA, the value PWM_CYCLE - 1 becomes the 100% value.
3 The ratio of the maximum speed is changed by the DIP switch. The ratio is as follows.

- 83 -
Program Explanation Manual “kit05.c”

DIP switch Ratio of


Decimal
Calculation The maximum
P63 P62 P61 P60 number
speed
0 0 0 0 0 5/20 25%
0 0 0 1 1 6/20 30%
0 0 1 0 2 7/20 35%
0 0 1 1 3 8/20 40%
0 1 0 0 4 9/20 45%
0 1 0 1 5 10/20 50%
0 1 1 0 6 11/20 55%
0 1 1 1 7 12/20 60%
1 0 0 0 8 13/20 65%
1 0 0 1 9 14/20 70%
1 0 1 0 10 15/20 75%
1 0 1 1 11 16/20 80%
1 1 0 0 12 17/20 85%
1 1 0 1 13 18/20 90%
1 1 1 0 14 19/20 95%
1 1 1 1 15 20/20 100%
From executed result of the 513th line, the value which is decided by the setting of the DIP switch, and is
corresponding to the ratio of maximum speed, is substituted in the “speed_max”.

515 : /* Left motor */


516 : if( accele_l >= 0 ) {
517 : PBDR &= 0xfb;
518 : ITU3_BRB = speed_max * accele_l / 100;
519 : } else {

It is left motor control. First of all, it is checked whether accele_l, which is the argument of left motor PWM of the
speed function, is 0 or more. If it is more than 0, bit 2 for the left motor rotation direction control is made "0"
because it is normal rotation.

Bit 7 6 5 4 3 2 1 0
PBDR before
? ? ? ? ? ? ? 1
change
AND value 1 1 1 1 1 0 1 1 ->0 xfb
PBDR after
0
change

= no change
Next, the duty ratio is set. Though setting is carried out by ITU3_GRB, in appearance, buffer register changes
the setting instead of general register in the program. The buffer of ITU3_GRB becomes ITU3_BRB.
It becomes,
ITU3_BRB =(Value substituted for buffer register corresponding to ratio of DIP switch)
(% of speed function) ÷100
= speed_max accele_l ÷100

The point is, the ratio set by the speed function is not output to the motor but "DIP switch ratio ratio set by
speed function" is output to the motor.

- 84 -
Program Explanation Manual “kit05.c”

516 : if( accele_l >= 0 ) {


Omission
519 : } else {
520 : PBDR |= 0x04;
521 : accele_l = -accele_l;
522 : ITU3_BRB = speed_max * accele_l / 100;
523 : }

Next is explained about “ else statement”. When the judgment of the 516th line does not be applied, this part is
executed. Since the judgment is “Is accele_l 1 or more?”, when else statement is executed, “accele_l” is less
than 0, that is to say, “accele_l” is minus.
Bit 2 for the left motor rotation direction control is made “1” because left motor rotation is reversed. Logical
OR operation is used to make only bit2 as “1”.
Bit 7 6 5 4 3 2 1 0
PBDR before
? ? ? ? ? ? ? 1
change
OR value 0 0 0 0 0 1 0 0 -> 0x04
PBDR after
1
change

= no change

Next, the duty ratio is set. The sign has been changed into the plus on the 521th line before calculation because
accele_l is minus. Next, the duty ratio is set in ITU3_BRB same as before.

525 : /* Right motor */


526 : if( accele_r >= 0 ) {
527 : PBDR &= 0xf7;
528 : ITU4_BRA = speed_max * accele_r / 100;
529 : } else {

After left motor, about right motor control is explained. First of all, it is checked whether accele_r, which is the
argument of right motor PWM of the speed function, is 0 or more. If it is more than 0, bit 3 for the right motor
rotation direction control is made "0", because right motor rotates normally.

Bit 7 6 5 4 3 2 1 0
PBDR before
? ? ? ? ? ? ? 1
change
AND value 1 1 1 1 0 1 1 1 -> 0xf7
PBDR after
0
value

= no change

Next, the duty ratio is set. Though setting is carried out by ITU4_GRA, in appearance, buffer register changes the
setting instead of general register in the program. The buffer of ITU4_GRA becomes ITU4_BRA.

It becomes,
ITU4_BRA =(Value substituted for buffer register corresponding to ratio of DIP switch)
(% of speed function) ÷100
= speed_max accele_r ÷100

The point is, the ratio set by the speed function is not output to the motor but "DIP switch ratio ratio set by
speed function" is output to the motor.

- 85 -
Program Explanation Manual “kit05.c”

526 : if( accele_r >= 0 ) {


Omission
529 : } else {
530 : PBDR |= 0x08;
531 : accele_r = -accele_r;
532 : ITU4_BRA = speed_max * accele_r / 100;
533 : }

Next is explained about “ else statement”. When the judgment of 526th line does not be applied, this part is
executed. Since the judgment is “Is accele_r 1 or more?”, when else statement is executed, “accele_r” is less
than 0, that is to say, “accele_r” is minus.
Bit 3 for the right motor rotation direction control is made “1” because right motor rotation is reversed. Logical
OR operation is used to make only bit 3 as “1”.

Bit 7 6 5 4 3 2 1 0
PBDR before
? ? ? ? ? ? ? 1
change
OR value 0 0 0 0 1 0 0 0 -> 0x08
PBDR after
1
change

= no change

Next, the duty ratio is set. The sign has been changed into the plus in 531th line before calculation because accele_r
is minus. Next, the duty ratio is set in ITU4_BRA same as before.
For example, assume that the following program was executed.

speed( 70, 100 ); /* Left 70% ratio Right 100% ratio */

0 0 1 1 When the DIP switch is as shown in the figure, since the value read from the
switch is 3,
P63
P62
P61
P60

Left motor PWM value = (3+5)/20 70/100


OFF = 0.4 0.7
= 0.28
ON = 28%
State of switch Right motor PWM value = (3+5)/20 100/100
= 0.4 1.0
= 0.40
= 40%

The rotation becomes as the following,


Left motor->28% normal rotation, 72% brake
Right motor->40% normal rotation, 60% brake

Because one cycle is 16 ms, the time interval of operation becomes as the following.
Left motor->4.48[ms] normal rotation, 11.52[ms] brake
Left motor->6.40[ms] normal rotation, 9.60[ms] brake

In motor drive board Vol.3, when the motor stops, the operation is only brake. There is no free (open) state.

- 86 -
Program Explanation Manual “kit05.c”

3.19. Servo Steering Operation: handle function

This is a function which controls the angle of the servo. Though the argument is in range of -90 to 90 degree of the
servo rotating angle, since servo does not actually rotate to that extent, practical range is thought to be -45 to 45
degree.

536 : /************************************************************************/
537 : /* Servo steering operation */
538 : /* Argument Servo operation angle: -90 ~ 90 */
539 : /* -90: 90o to the left turn ,0: straight ,90: 90o to the right turn */
540 : /************************************************************************/
541 : void handle( int angle )
542 : {
543 : ITU4_BRB = SERVO_CENTER - angle * HANDLE_STEP;
544 : }

The connection of the motor drive board is confirmed again.

Signal,
Pin no. Details “0” “1” Details
direction
1 - +5V
2 board<-PB7 LED1 ON OFF
3 board<-PB6 LED0 ON OFF
Duty ratio is set with
4 board<-PB5 Servo signal PWM signal
ITU4_BRB
Duty ratio is set with
5 board<-PB4 Right motor PWM Stop Operation
ITU4_BRA
Right motor rotation Normal Reverse
6 board<-PB3
direction rotation rotation
Left motor rotation Normal Reverse
7 board<-PB2
direction rotation rotation
Duty ratio is set with
8 board<-PB1 Left motor PWM Stop Operation
ITU3_BRB.
9 board->PB0 Push switch Pushed Not pushed
10 - GND

As shown in the table, PB5 is for servo control. Since reset-synchronized PWM mode is used, the value of
ITU4_BRB is changed to change the duty ratio of PB5.

543 : ITU4_BRB = SERVO_CENTER - angle * HANDLE_STEP;

Center value angle Increment of 1 degree


of servo

SERVO_CENTER is center value of the servo. The only angle value which is set in variable “angle”, is increased
or decreased from center value. However, one unit of ITU4_BRB is not one degree. In HANDLE_STEP, there is a
value corresponding to 1o. Therefore, variable “angle” is multiplied by HANDLE_STEP.

- 87 -
Program Explanation Manual “kit05.c”

3.20. Main Program Start

It is the Main function. It is called from the start-up routine and for the first time, the program of C language is
executed from here.

69 : /************************************************************************/
70 : /* Main Program */
71 : /************************************************************************/
72 : void main( void )
73 : {
74 : int i;
75 : int pattern;
76 :
77 : /* Initialization of microcomputer function */
78 : init(); /* Initialization */
79 : set_ccr( 0x00 ); /* Whole interrupt enable */
80 :
81 : /* State initialization of micom car*/
82 : handle( 0 );
83 : speed( 0, 0 );
84 : pattern = 0;
85 : cnt1 = 0;

78 : init(); /* Initialization */
The “init” function is executed, and the I/O register of the built-in peripheral function of H8/3048F-ONE is
initialized.

79 : set_ccr( 0x00 ); /* Whole interrupt enable */


The interrupt of the entire CPU is enabled. Though the interrupt of ITU0 has been enabled in the “init” function,
actually, interrupt has not been generated yet only by setting each peripheral function (for instance, ITU0_IER
etc.). The interrupt of the entire microcomputer is enabled by making bit7 as "0" and the bit is the bit7 of the 8-bit
register named CCR (Condition Code Register) which is internal register of CPU. The name interrupt mask bit is
given to bit7, and the initial value is "1".
Composition contents of CCR (Condition Code Register)

Bit: 7 6 5 4 3 2 1 0
CCR: I UI H U N Z V C
Set value: 0 - - - - - - -
Hexadecimal
number:
0 0
CCR cannot be directly accessed in C program unlike the variable, because it is a register in CPU. Therefore, the
function which operates the value is prepared. This function is “set_ccr”. This function is defined in the machine.h
file. Therefore, this C program includes the machine.h file for this purpose. If the contents of machine.h are shown,
the description is,

extern void set_ccr(unsigned char);

The argument of an unsigned char type is substituted. Though it is good that only bit7 is made "0", since there is
no influence here even if "0" is written in the rest, 0x00 is substituted..

- 88 -
Program Explanation Manual “kit05.c”


CCR 0 0 0 0 0 0 0 0

ITU0

ITU0_IER 0 0 0 0 0 0 0 1

"1" Interrupt is
generated!!
ITU0_TSR 0 0 0 0 0 0 0 1

ITU1

Communication

A/D

etc...

The entire interrupt is controlled by I bit of CCR.

81 : /* State initialization of micom car */


82 : handle( 0 );
83 : speed( 0, 0 );
84 : pattern = 0;
85 : cnt1 = 0;

Next, the state of the micom car is initialized.


* Steering is 0o.
* Speed is left 0%,right 0%.
* The variable “pattern” is 0.
* The variable “cnt1” is 0.

3.21. About pattern method

Kit05.c is a method of executing the program by branching to the pattern number. The pattern number is decided
beforehand according to the number set in the pattern variable.
For example, pattern 0 is “switch input wait processing”, and pattern 1 is “wait for 1 second processing” after
switch is pushed---etc. If this method is used, since the program is divided into each pattern, it becomes easy to
understand. The pattern method can be said, "Making the program into blocks".
Even when adding a new function, it becomes easy to remodel to add the program by allocating a new number.
Hereafter, outline, actual operation, remodeling example of pattern method are shown.

- 89 -
Program Explanation Manual “kit05.c”

3.2.2. First part of pattern method (while statement and switch statement)
87 : while( 1 ) {
88 : switch( pattern ) {
107 : case 0:
Process at pattern 0
121 : break;
122 :
123 : case 1:
Process at pattern 1
135 : break;
Pair Pair
Each pattern process

357 : default:
358 : /* When it is not any pattern, return to standby state. */
359 : pattern = 0;
360 : break;
361 : }
362 : }

About “while” statement


"While (1){" of 87th line and "}" of 362nd line is pair and "switch (pattern) {" of 88th line and "}" of 361st line
becomes pair. Usually when there is open bracket "{", the contents up to the corresponding closing bracket "}" are
written with shifting 4 characters to the right so that it becomes easy to understand. It is called “Indent”. This
program is also indented moving only 4 characters to the right. (break;,pattern=0;) However, “while” statement
and “switch-case” statement are written in same column in spite of “while” statement including “switch-case”
statement. When the program becomes long in the brackets, it becomes 2nd line after exceeding the right edge of
the screen and can not be seen easily. In order to prevent this, “while” statement and “switch-case” statement are
written in same column. Originally, there is no problem in compiling because the column is moved to make it easy
for human being to understand. In all respects, it is preferable if 4 characters are indented to right from 88th to
361st line if you still feel uncomfortable.

"While (expression)" is a control statement executing next command beyond put-together (bundled) command in
{ } if the value of the expression is " false ", and inner command in { } if the value of the expression is "true".

while ( If the expression is true) { while ( If the expression is false ) {


... ...
... ...
... ...
} }
...

“True” and “False” is defined as


Explanation Example
True(Yes) Correct / ≠ 0 or Yes 3<5 3==3 1 2 3 -1 -2 -3
False (No) Incorrect / =0 or No 5<3 3==6 0
Program becomes “while (1)”. Because one is always "True", it is repeated infinitely in { } of while. If infinite
loop is done by Windows program etc., though it becomes troublesome that application program cannot be ended,
it does not matter because this program is a program only to move (operate) the micom car. If the micom car runs
on course (or runs off), human being can switch it off.

- 90 -
Program Explanation Manual “kit05.c”

The microcomputer becomes uncontrollable by going to the area where program is not written when it ends the
program without doing end process appropriately. As for the microcomputer, it does not end by repeating the thing
that to do not do anything (infinite loop), and stops the moving operation in low power consumption mode which
is called sleep mode and it is usual to wait for the timing which returns by any trigger.

About “switch-case” statement


Value of variable pattern is checked by the switch statement, and it branches towards versatility. The principle is as
follows.

Constant number of pattern and each case


switch( pattern ) {
statement is compared and if it matches then
case 0:
process is done by jumping to that case
/* If it is pattern =0, this line is executed. */
position.
break;
case 1:
This process ends when meets with break
/* If it is pattern =1, this line is executed. */
statement or end terminal “}” of switch
break;
statement.
default:
/* If it is not any of above, then this line is
The default statement is executed when
executed. */
inapplicable even to the constant number of
break;
any case statement. Incidentally, nothing will
}
be executed either when there is no default
statement.

3.23. Pattern contents

The pattern decided by kit05.c and its content are summarized here.

Pattern Process contents Remarks


0 Switch input waiting 0~10 is assumed as process before program
1 Wait for one second after the switch is pushed. run.
11 Common trace
11~20 is assumed as process during common
12 Checking of end of large turn to the right.
program run.
13 Checking of end of large turn to the left.
21 Process at the time of first crossline detection.
22 Second crossline is skipped.
After the crossline is found, 21~30 is
23 No.1 process of trace after crossline. assumed as process up to the right angle.
Crank detection and No.2 process of trace after
24
crossline.
Wait a little till doing left crank clearing
31
process stability. 31~40 is assumed as left crank process.
32 Check of left crank clear process turning end.
Wait a little till doing right crank clearing
41
process stability. 41~50 is assumed as right crank process.
42 Check of right crank clear process turning end.

Thus the pattern number and process contents are decided and program is created.
As per the remarks column, number 0 level is a process before program run and number 10 level is a process of
usual program run and it becomes easy to understand when brief process contents are decided at each number 10
level.
In kit04.c, pattern 23 is "Trace and crank detection after crossline" and pattern 24 is not there. In kit kit05.c,
process is done by dividing in pattern 23 and 24.

- 91 -
Program Explanation Manual “kit05.c”

The flow of the pattern is summarized in the flow chart below. It is easy to grasp problem if we created and
analyze the program with considering the flow of pattern always.

Program start

Pattern 0
switch input waiting

Pattern 1
1 second waiting

Pattern 11 Left large bend


Common trace process Right large bend

Pattern 12 Pattern 13
Crossline Check of large right Check of large left
bending end Large bend end. bending end Large bend end
detection
Crossline detection Crossline detection

Pattern 21
Process at the time of first
crossline detection

Pattern 22
2nd crrossline is
skipped

Pattern 23
The first of trace after
crossline

Pattern 24
Crank detection and the
second of trace after
crossline

Left crank detection Right crank detection


Pattern 31 Pattern 41
0.2 seconds waiting 0.2 seconds waiting

Pattern 32 Pattern 42
Check of left crank Check of right crank
bending end bending end

- 92 -
Program Explanation Manual “kit05.c”

3.24. Pattern 0 : Switch input waiting

Here, it is a state to wait for input of switch. If switch is pushed, the flow of the program shifts to pattern 1, else
LED0 and LED1 are made to glow alternately because we cannot understand whether CPU is stopped or CPU is a
state of really waiting. First of all switch detection part is shown below.

case 0:
/* Switch input waiting */
if( pushsw_get() ) {
pattern = 1;
cnt1 = 0;
break;
}

If the switch is pushed, the value of “pushsw_get” function becomes “1”, and the value is judged “True”. And
statements in brackets are executed. Then, “pattern” becomes 1.

The program, which blinks LED, is added after this. Blinking is adjusted to repeated process of 0.1 second LED0
lights and for next 0.1 second LED1 lights.

if( cnt1 < 100 ) { <-Is cnt1 0~99?


led_out( 0x1 ); <-Then, only LED0 lights
} else if( cnt1 < 200 ) { <-Is cnt1 100~199?
led_out( 0x2 ); <-Then, only LED1 lights
} else { <-If other than this (more than 200)
cnt1 = 0; <-cnt1is adjusted to 0
}

Ordinary variable which is called local variable, for instance pattern variable, if set once, does not change value
until changed for the next time. In kit05.c, only the cnt0 variable and the cnt1 variable are exceptions, and they are
called global variable. Cnt0 and cnt1 are done +1 in the interrupt_timer0 function. The interrupt_timer0 function is
executed at each 1ms. Therefore, time can be measured by using this variable.

If the switch detection and LED blinking program are combined, it becomes the program as follows.

107 : case 0:
108 : /* Switch input waiting */
109 : if( pushsw_get() ) {
110 : pattern = 1;
111 : cnt1 = 0;
112 : break;
113 : }
114 : if( cnt1 < 100 ) { /* LED blinking process */
115 : led_out( 0x1 );
116 : } else if( cnt1 < 200 ) {
117 : led_out( 0x2 );
118 : } else {
119 : cnt1 = 0;
120 : }
121 : break;

Break statement of 121st line is the statement for ending the case 0.

- 93 -
Program Explanation Manual “kit05.c”

Condition by which pattern changes

・To pattern 1 if the switch is pushed.

In case timer function is used without using cnt1, what will be the case?
Statements which are shown with arrow and include two “timer()” functions, correspond to the statements from
114th line to 121st line in “kid05.c”.

if( pushsw_get() ) {
pattern = 1;
cnt1 = 0;
break;
}
timer( 100 );
led_out( 0x1 );
timer( 100 );
led_out( 0x2 );
break;

It became simple. This method may be preferable. However, timer function does not do anything except waiting.
Therefore, if the switch is pushed for a split second during execution of “timer( )” function, the switch is not
pushed already when “pushsw_get( )” function that checks the switch is called. Here is a possibility of the missing
detection of the pushing the switch. Since it is 0.2 seconds in this program, there is no missing as far as the
pushing of 0.2 or more seconds is operated. But, if the timer of second order is used ( e.g. timer(2000); ), the
missing error must be happened because the time of not checking the switch becomes too much.

3.25. Pattern 1 : 1 second waiting after the switch is pushed.

Simply, it is routine to wait for 1 second after the switch is pushed. It is made to wait a little because there is a
possibility that when the micom car runs out rapidly after pushing the switch it may collide with the hand which
pushed the switch. Since waiting idle is boring (gets on nerves), LED0 lights for 0.5 seconds, and LED1 lights
for 0.5 seconds and then the micom car starts.

121 : case 1:
122 : /* 1 second waiting after the switch is pushed */
123 : if( cnt1 < 500 ) {
124 : /* 1.0 seconds starting ago LED1: "OFF",LED0: "ON" */
125 : led_out( 0x1 );
126 : } else if( cnt1 < 1000 ) {
127 : /* 0.5 seconds starting ago LED1: "ON",LED0: "OFF" */
128 : led_out( 0x2 );
129 : } else {
130 : /* Start!! */
131 : led_out( 0x0 );
132 : pattern = 11;
133 : cnt1 = 0;
134 : }
135 : break;

Condition by which pattern changes

* To pattern 11 after one second

- 94 -
Program Explanation Manual “kit05.c”

3.26. Pattern 11: Normal Trace


Pattern 11 is a state to trace on the course.
First of all, the state of the assumed sensor is considered. Though there are 8 sensors, if all are used there is large
increase in the state of the sensor and more judgments are needed about states of the sensor. Hence, mask is
applied with "MASK3_3" and the state of the course is detected by left 3 and by right 3 sensors.
Next, the angle of the steering and PWM of left and right motor is considered. When the sensor is at the center, the
speed is raised. The more the sensor shifts from the center, the more the steering turns and the speed drops.
For the present, 10 cases are considered as below.

Value when Left Right


Hexadecimal Steering
State of course and sensor sensor is motor motor
number angle PWM PWM
read

1 ●●●××●●● 00000000 0x00 0 100 100

2 ●●●××○●● 00000100 0x04 5 100 100

3 ●●●××○○● 00000110 0x06 10 80 69

4 ●●●××○○○ 00000111 0x07 15 50 40

5 ●●●××●○○ 00000011 0x03 25 30 21

6 ●●○××●●● 00100000 0x20 -5 100 100

7 ●○○××●●● 01100000 0x60 -10 69 80

8 ○○○××●●● 11100000 0xe0 -15 40 50

9 ○○●××●●● 11000000 0xc0 -25 21 30

- 95 -
Program Explanation Manual “kit05.c”

Moreover, there is a crossline in the course of the micom car. Since there is check_crossline function which
detects the crossing line, it is used.

Left Right
Value when Hexadecimal Steering
State of course and sensor motor motor
sensor is read number angle PWM PWM

10 ×○○××○○× 01100110 0x66

The program is written based on this table.

● Sensor Reading

145 : switch( sensor_inp(MASK3_3) ) {

The state of the sensor is read. Since 3 right and 3 left sensors are read, MASK3_3 is used.

●Direct Advance

146 : case 0x00:


147 : /* Center -> Straight */
148 : handle( 0 );
149 : speed( 100 ,100 );
150 : break;

The sensor is in the state of “0x00". This state is a state which advances straight as shown in the figure below.
Micom car advances by servo angle 0°, left motor 100%, right motor 100%.

0 0 0 0 0 0 0 0

- 96 -
Program Explanation Manual “kit05.c”

●It is slightly left inclined.

152 : case 0x04:


153 : /* Slightly left inclined->Slight turn to the right. */
154 : handle( 5 );
155 : speed( 100 ,100 );
156 : break;

The sensor is in the state of “0x04". This state is a state in which the micom car is slightly coming towards left as
shown in the figure below. The servo advances by 5° to the right, left motor 100%, right motor 100% and the
micom car comes near the center.

0 0 0 0 0 1 0 0

●A little inclined to the left.

158 : case 0x06:


159 : /* Little left inclined->Small turn to the right. */
160 : handle( 10 );
161 : speed( 80 ,69 );
162 : break;

The sensor is in the state of “0x06". This state is a state in which the micom car is coming a little towards the left
as shown in the figure below. The servo advances by 10° to the right, left motor 80%, right motor 69% and the
micom car comes near to the center while decelerating.


0 0 0 1 1
0 0

- 97 -
Program Explanation Manual “kit05.c”

●Left inclined from the middle veering

164 : case 0x07:


165 : /* Left inclined from the middle->Middle turn to the right */
166 : handle( 15 );
167 : speed( 50 ,40 );
168 : break;

The sensor is in the state of “0x07". This state is a state in which the micom car is coming towards left from the
middle veering as shown in the figure below. The servo advances by 15° to the right, left motor 50%, right motor
40% and the micom car comes near to the center while decelerating.

0 0 1 1 1
0 0 0

●More inclined to the left

170 : case 0x03:


171 : /* Large inclined to the left->Large turn to the right */
172 : handle( 25 );
173 : speed( 30 ,21 );
175 : break;

The actual program has 174th line. It is described later.


The sensor state is “0x03”. In this state the micom car is largely coming towards left as shown in the figure below.
Servo advances by 25° to the right, left motor 30%, right motor 21% and the micom car comes near to the center
while considerably decelerating.

0 1 1
0 0 0
0 0

- 98 -
Program Explanation Manual “kit05.c”

●Slightly inclined to the right

177 : case 0x20:


178 : /* Slightly inclined to the right->Slight turn to the left */
179 : handle( -5 );
180 : speed( 100 ,100 );
181 : break;

The sensor state is “0x20”. In this state the micom car is slightly coming towards right as shown in the figure
below. The servo advances by 5° to the left, left motor 100%, right motor 100% and the micom car comes near to
the center.

0 0 1 0 0 0 0 0

●A little inclined to the right

183 : case 0x60:


184 : /* A little inclined to the right->Small turn to the left */
185 : handle( -10 );
186 : speed( 69 ,80 );
187 : break;

The sensor state is “0x60". In this state the micom car is coming a little towards the right as shown in the figure
below. The servo advances by 10° to the left, left motor 69%, right motor 80% and the micom car comes near the
center while decelerating.

0 1 1
0 0 0
0 0

- 99 -
Program Explanation Manual “kit05.c”

●Right inclined from the middle veering

189 : case 0xe0:


190 : /* Right inclined from the middle veering->Middle turn to the left */
191 : handle( -15 );
192 : speed( 40 ,50 );
193 : break;

The sensor state is “0xe0”. In this state the micom car is coming a little to the right as shown in the figure below.
The servo advances by 15° to the left, left motor 40%, right motor 50% and the micom car comes near the center
while decelerating.

1 1
1 0
0 0
0 0

●More inclined to the right

195 : case 0xc0:


196 : /* Large inclined to the right->Large turn to the left */
197 : handle( -25 );
198 : speed( 21 ,30 );
200 : break;

The actual program has 199th line. It is described later.


The sensor state is “0xc0”. In this state the micom car is greatly coming towards the right as shown in the figure
below. The servo advances by 25° to the left, left motor 21%, right motor 30% and the micom car comes near to
the center while considerably decelerating.

1 1
0 0
0 0
0 0

- 100 -
Program Explanation Manual “kit05.c”

●Crossline Check

141 : if( check_crossline() ) { /* Crossline Check */


142 : pattern = 21;
143 : break;
144 : }

When the return value of check_crossline function is “0”, there is no crossline, when the value is “1”, the first
crossline has been detected. When the crossline is detected the pattern is adjusted to 21 and the switch-case
statement is ended by the break statement. Since the crossline check is important, the check is placed first in
pattern 11, pattern 12, and pattern 13.

●Other

202 : default:
203 : break;

When it is other than the patterns up to now, it jumps to this default part. Nothing is done.

- 101 -
Program Explanation Manual “kit05.c”

3.27. Pattern 12: Check of end of large turn to the right

Sensor state 0x03 is sensor detection state when the micom car is coming greatly to the left. Therefore, when the
micom car is expanded by the curve any further, there is possibility of the car becomes like the figure below.

1 1 0 1
0 0 0 0 0
0 0 0
0 0 1 0

State 0x03 at the time


of large turn to the right
The car has come more to the left than 0x03,
even though the car is still turning to the right.

195 : case 0xc0:


196 : /* Large inclined to the right - >
Large turn to the left */
0 197 : handle(- 25 );


0 198 : speed( 21 ,30 );

0 199 : pattern = 13;

1 200 : break;

However, the program says turn to the left.

Senser state has become 0xc0


by inclining more to the left

Steering comes
off the axle!

When the steering is turned to


left as per the program, Since it is actually inclining to the left, if the servo
is turned to the left, the car goes off course easily
and the wheel jumps from the course.

Even though there is a large incline towards the left, in the program there is a wrong judgment of "large inclined
to the right". Since the servo turns in the opposite direction when wrong judgment is done, the wheel jumps from
the course.
Here, when large turn is done to the right, the steering keeps turning largely to the right until the micom car is
returned back to a certain state of the sensor. Judging place of this “certain state of the sensor” is pattern 12.

Case 0x03 part of pattern 11

170 : case 0x03:


171 : /* Large incline to the left->Large turn to the right */
172 : handle( 25 );
173 : speed( 30 ,21 );
174 : pattern = 12; <-Move to pattern 12
175 : break; :This statement is added only in this case ox03.

Moves to pattern 12 when the sensor becomes 0x03.

- 102 -
Program Explanation Manual “kit05.c”

In pattern 12, let’s consider how the state is evolved to return to the normal run pattern 11.

0 0
0 0 0
1 1 1 1 0
0 0 0
0 0

To pattern 12 since 0x03 Continue turning in pattern 12


1 0
0 0 0 0 0
0 0 0 0 0
0 1 1

Still turning Keep turning though about to fall

1 0
0 0 1
0 0 0

Return to pattern 11 when this state is attained

When the program is set such that it returns to the pattern 11 and sensor state becomes 0x06, the misunderstanding
like the previous one can be avoided.

213 : case 12:


214 : /* Right turn completion check */
218 : if( sensor_inp(MASK3_3) == 0x06 ) {
219 : pattern = 11;
220 : }
221 : break;

Though we consider that the program is complete, remember that crossline check was carried out in pattern 11.
Is it not necessary in pattern 12?

- 103 -
Program Explanation Manual “kit05.c”

0 0 0 0 0 0 1 1




0 0


Continue turning to the right in


pattern 12 since state 0x03 is attained The state is still in 0x03, soon will become 0x06,
and the process returns to the usual trace.

1 1 1 0
0 1 1 1
If the micom car plunges into the crossline
in the state of 0x03 and if there is no crossline check,
the state is judged as the processing of other than 0x06.
And it passes through without notice and the wheel jumps
from the course.

Thus the crossline is occasionally detected even during processing in pattern 12. Therefore, the program for
crossline detection part is put in pattern 12.

207 : case 12:


208 : /* Check of large turning to the right completion */
209 : if( check_crossline() ) { /* Crossline check even during turning */
210 : pattern = 21;
211 : break;
212 : }
213 : if( sensor_inp(MASK3_3) == 0x06 ) {
214 : pattern = 11;
215 : }
216 : break;

The program of pattern 12 is complete here.

- 104 -
Program Explanation Manual “kit05.c”

3.28. Pattern 13: Check of end of large turn to the left


State 0xc0 of the sensor is sensor detection state at the time of largest incline to the left. Therefore, there is a
possibility to become like the following figure when the curve expands any further.

1 0
1 1 0 0
0 0 0 0
0 0 0 1
0 0

State 0xc0 at the time of large turn Inclined more to the right than 0xc0.
Though still turning to the left.

170 : case 0x03:


171 : /* Large incline to the left- >
l arge turn to the right */

0 172 : handle( 25 );

0 173 : speed( 30 ,21 );

0 174 : pattern = 12;

1 175 : break;

However the program shows "turn to the right"

Becomes 0x03 by inclining more to the right

Steering comes
off the axle!

When the steering is turned to Since it is actually inclining to the right, when the servo
the left according to the program... is turned to the right, it goes off the course and
the wheel jumps from the course.

Even though there is a large incline towards the right, in the program there is a wrong judgment of "large
inclined to the left". Since the servo turns in the opposite direction when wrong judgment is done, the wheel jumps
from the course.
Therefore, when large turn is done to the left, steering keeps turning largely to the left until the micom car is
returned back to a certain state of the sensor. Judging place of this “certain state of the sensor” is pattern 13.

Case 0xc0 part of pattern 11

case 0xc0:
/* Large incline to the right-> Large turn to the left */
handle( -25 );
speed( 21 ,30 );
pattern = 13; <- Move to pattern 13
break; :This statement is added only in this case ox0c.

Moves to pattern 13 when the sensor becomes 0xc0.

- 105 -
Program Explanation Manual “kit05.c”

In pattern 13, let’s consider how the state is evolvedi to return to normal run pattern 11.


0 0
1 1 0
0 0 0 0
0 0 1
0 0 1

To pattern 13 since 0xc0 is attained Continue turning in pattern 13

0 0

0 0
0 0
0 0 0
0 0

1 1

Still turning
Continue turning even though about to fall


1 1
0 0
0 0

Return to pattern 11 when this state is attained

When set such that it returns to the pattern 11 when sensor state becomes 0x06, the misunderstanding like the
previous one can be avoided.

218 : case 13:


219 : /* Check of large turning completion to the left */
224 : if( sensor_inp(MASK3_3) == 0x60 ) {
225 : pattern = 11;
226 : }
227 : break;

Though we consider that the program is complete, remember that crossline check was carried out in pattern 11.
Is it not necessary in pattern 13?

- 106 -
Program Explanation Manual “kit05.c”

1 1 0 0 0 0 0 0


1 0

0 0

To pattern 13 since 0xc0 is


attained Continue turning to left The state is still in 0xc0, soon will become 0x60,
and the process return to the usual trace.

1 1 1 1
1 1 1 0 0

If the micom car plunges into the crossline


in the state of 0xc0 and if there is no crossline check,
the state is judged as the processing of other than 0x60.
And it passes through without notice and the steering
comes off its axle.

Thus the crossline is occasionally detected even during processing in pattern 13. There, the program for crossline
detection part is put in pattern 13.

218 : case 13:


219 : /* Check of large turning completion to the left */
220 : if( check_crossline() ) { /* Crossline check even during large turn */
221 : pattern = 21;
222 : break;
223 : }
224 : if( sensor_inp(MASK3_3) == 0x60 ) {
225 : pattern = 11;
226 : }
227 : break;

The program of pattern 13 is complete here.

- 107 -
Program Explanation Manual “kit05.c”

3.29. Processing at the time of crossline detection

Program start

Pattern 0
switch input waiting

Pattern 1
1 second waiting

Pattern 11 Left large bend


Common trace process Right large bend

Pattern 12 Pattern 13
Crossline Check of large right Check of large left
bending end Large bend end. bending end Large bend end
detection
Crossline detection Crossline detection

Pattern 21
Process at the time of first
crossline detection

Pattern 22
2nd crrossline is
skipped

Pattern 23
The first of trace after
crossline

Pattern 24
Crank detection and the
second of trace after
crossline

Left crank detection Right crank detection


Pattern 31 Pattern 41
0.2 seconds waiting 0.2 seconds waiting

Pattern 32 Pattern 42
Check of left crank Check of right crank
bending end bending end

The part enclosed in the square is the processing until the right angle is detected after the crossline is detected.
Checks if there is crossline in pattern 11, 12, 13. Move to pattern 21 as soon as crossline is detected.

- 108 -
Program Explanation Manual “kit05.c”

3.30. Pattern 21 : Processing at the time of 1st crossline detection.

When does the flow of the program move to pattern 21 in what type of state ? If you recall the part that moved
from pattern 11 to pattern 21, it was a program like below.

141 : if( check_crossline() ) { /* crossline check */


142 : pattern = 21;
143 : break;
144 : }

When the sensor board has detected the crossline, the flow becomes to pattern 21 as a figure below.
5 0 ~1 0 0 c m

Position of
slow down start

After the crossline, about 50~100cm ahead, the crank (Right angle), which is the worst difficulties in the course,
is shown. First of all, what is necessary to be done? Since the car is running along the course untill now, the car is
keeping on a considerable speed. It is impossible to turn in right angle at that speed directly. Then, first of all, the
brakes are applied. After crossline, the steering is adjusted to 0 degrees because it is known that the straight line
course is there.
The breaks are applied till "position of slow down start" and after that it is considered to proceed by going slow.
After the first crossline is found, till proceeding the slow going part, there is a state as shown in following figure.

- 109 -
Program Explanation Manual “kit05.c”

No.4
No.3
No.2
No.1

No.1 shows first crossline, No.2 shows black, No.3 shows second crossline, and No.4 shows the slow down
starting part in black.
Program that distinguishes whether the course has changed as white->black->white->black till No.4, is necessary
and that releases the braking in No.4 part, is necessary, too. It seems to be complex somehow.
The concept has been changed a little. Crossline is detected and the car proceeds up to No.4 position. Is it about
10cm? If it is about 10cm then little temperization is done with timer function and the car proceeds with inertia
and it seems to finish without a difficult sensor judging. The time ... as the experiment is not done on this time, it
is indescribable. For the time being, detailed time is assumed to be finely adjusted by running as for 0.1 second. It
is made in such a way that after crossline detection LED is lighted and it can be known from outside that it has
entered in pattern 21.

If summarized,
・LED0,1 are lighted
・Steering is adjusted to 0 degree.
・Break is applied by adjusting left and right motor PWM to 0%.
・0.1 second waiting
・Moves to the following pattern after some duration

This is made into program by pattern 21.

case 21:
led_out( 0x3 );
handle( 0 );
speed( 0 ,0 );
if( cnt1 > 100 ) {
pattern = 22; /* To pattern 22 after 0.1 seconds*/
}
break;

Visual inspection is performed on completion for any defects. Why! There is a doubtful part in above program.
When cnt1 becomes more than 100 (When passing by 100 milliseconds), it is made to move to pattern 22.
Therefore, when pattern 21 is carried out , cnt1 should be 0. For instance, if cnt1 is 1000 when program has moved
to pattern 21, cnt1 is assumed to be more than 100 and pattern 21 is hardly executed (at about 0.1 seconds). There
is no guarantee of cnt1 being 0. Then, one more pattern has to be increased. Pattern is divided in two parts, one is
pattern 21 that takes charge of applying braking operation and clearing of “cnt1”, and another is pattern 22 that
takes charge of whether 0.1 second has passed or not.

- 110 -
Program Explanation Manual “kit05.c”

Let’s put together our thoughts again

Performed by pattern 21
・LED0,1 are turned on
・Steering is adjusted to 0 degree
・Brake is applied by adjusting left and right motor PWM to 0%.
・The pattern is moved to next
・cnt1 is cleared

Performed by pattern 22.


・Check whether cnt1 has become more than 100 or not
・If it has become more than 100, then pattern is moved to next

Changed part is where the comment is described with boldface type.


Program is created again as mentioned above.

229 : case 21:


230 : /* Process when first crossline is detected*/
231 : led_out( 0x3 );
232 : handle( 0 );
233 : speed( 0 ,0 );
234 : pattern = 22;
235 : cnt1 = 0;
236 : break;
237 :
238 : case 22:
239 : /* Second is skipped. */
240 : if( cnt1 > 100 ) {
241 : cnt1 = 0;
242 : pattern = 23;
243 : }
244 : break;

The program shown above is reviewed on completion for any defects. Brake is applied at pattern 21, cnt1
concerned with standard time interval, is cleared and then moved to pattern 22. Whether 0.1 seconds passed is
checked in pattern 22. If it passed, then execution of the program moves to pattern 23.
Here the program from detecting the crossline to slow down starting is completed.

3.31. Pattern 23 : Trace 1 after crossline

In pattern 21, 22, after the crossline is detected, brake is applied for 0.1 second and 2 crosslines are passed. The
process after that is done in pattern 23.
As the crossline has passed, next is crank (right angle) detection. When crank is found, the car slowly proceeds
since the steering has to turn immediately. Normal trace is necessary, as straight line should be traced in between
crossline and the right angle. This time it is as per the following figure.

- 111 -
Program Explanation Manual “kit05.c”

After crossline, when the course becomes left crank by doing


trace, sensor state becomes “0xe0” as shown in left figure.
Then, the “0xe0” state is judged as left crank.
At this time, steering is turned to left at maximum extent (full
extent) because, if the steering is not turned to left at maximum
extent, wheels stray its right racing course by swerving to
course outside. It is necessary to ensure from the actual micom
car about the maximum turn that steering can withstand, as the
turning angle differs according to the manufacturing of micom
car. If steering is turned on, it may collide with (strike) the
chassis. At that time there is the maximum angle. Several times
it is measured by protractor. It was approximately 40 degrees.
○○○× ×●● ●
If the angle is so, it collides with (strike) the chassis part, so it
is adjusted to 38 degrees in program considering the margin of
2 degrees. If it does not collide (strike) even if turning up to 60
degrees, it is considered preferable to stop at about 60 degrees.
If the angle is more than that, there is possibility that it cannot
-> 0xe0 be turned skillfully because angle of tire is too large towards
the proceeding direction and tire gets slipped.
How are you rotating the motor? It is anticipated to rotate
somewhat more on right motor and somewhat less on left
motor as it turns to left. For the time being, it is adjusted as left
motor 10% and right motor 50%, as at what % it should be
done couldn’t be understood unless it is done actually. If it is
summarized, it becomes as follows.
Steering: -38 degrees Left motor: 10% Right motor : 50%

It is right angle at right side. Concept is same as left crank.


●● ●×× ○○○
If summarized, it becomes as follows.
Steering: 38°
Left motor : 50% Right motor: 10%

-> 0x07

At the time of going straight ahead, sensor state is “0x00”.


This is judged as state of straight advancement. There is no
scope for doubt because steering is straight. Problem is PWM
value of motor. Here nothing can be said about the speed if it is
●● ●××●●● -> 0x00 not made to run actually. When crank is detected, speed must
be controlled such that it can turn at right angles hence for the
time being it is kept at 40% and then it is finely adjusted by
actually running. If summarized, it becomes as follows.
Steering : 0 degrees
Left motor : 40% Right motor : 40%

The situation when micom car is inclined to left side is


●●●××○●● -> 0x04 considered. The 4 states are shown in left figure. It is inclined
to left from center slightly by slightly. The state of the sensor is
set without increasing any further, because it is understood that
●●●××○○● -> 0x06 there is only one straight line after crossline, though sensor
state is considered to incline more to left side. The steering is
turned to right since the micom car strays to left side. If
●●●××○○○ -> 0x07 steering angle is small, and the difference from center line is
large, the car cannot return to the cnter line. The sensor swings
noisily to right and left when the steering is too large. Exact
●●●××●○○ -> 0x03 angle is difficult to adjust. For the time being, any state is
adjusted to 8 degrees. If they are summarized, they are
Steering : 8 degrees, Left motor : 40%, and Right motor : 36%.

- 112 -
Program Explanation Manual “kit05.c”

The situation when micom car is inclined to right side is


considered. The 4 states are shown in left figure. It is inclined
●● ○××●●● -> 0x20
to right from center slightly by slightly. The state of the sensor
is set without increasing any further, because it is understood
●○○××●●●
that there is only one straight line after crossline, though sensor
-> 0x60
state is considered to incline more to right.
The steering is turned to left since the micom car strays to
○ ○○× ×● ●●
right. If steering angle is small, and the difference from center
-> 0xe0
line is large, the car cannot return to the center line. The sensor
swings flapping to right and left when it is too large. Exact
-> 0xc0
angle is difficult to adjust. For the time being, any state is
○○●××●●●
adjusted to 8 degrees. Summarized as:
Steering: -8 degrees
Left motor: 36% Right motor: 40%

It is difficult to create a program. There are two kinds of states in “0xe0”. There were two kinds of “0x07”, too.
“0xe0” state is shown in following figure.

○○○××●●●

○○○××●●●

Both are "0 xe0"!!

The problem which is seen here is handled as follows:


* After the slow down starting, for a while as pattern 23, sensor state 0xe0 and 0x07 are assumed to be curved.
* Afterwards, as pattern 24, sensor state 0xe0 and 0x07 are assumed to be crank detection.
It is an image same as the figure below.

Part processed by
pattern 24
5 0 ~1 0 0 c m

Part processed by
pattern 23

Position of
slow down start

- 113 -
Program Explanation Manual “kit05.c”

Timing for moving to pattern 24 from pattern 23 is judged by time. Up to what extent is it good? It cannot be
described in theory. It is ineffective when it is short, and the car reaches the crank part when it is too long. This
time it is adjusted to 0.3 seconds and afterwards it will be adjusted by actual running.
Pattern 24 is a process till right angle detection. The process is shifted to pattern 31 after controlling the steering
and motor when the left crank is detected. The process is shifted to pattern 41 after controlling the steering and
motor when the right crank is detected.
Though the showing has become lengthy, pattern 23, 24 will become as follows if programming is done.

246 : case 23:


247 : /* 1 of trace after crossline */
248 : if( cnt1 > 300 ) {
249 : cnt1 = 0;
250 : pattern = 24; Moves to pattern 24 after 0.3 seconds.
251 : break;
252 : }
253 : switch( sensor_inp(MASK3_3) ) {
254 : case 0x00:
255 : /* Center->Straight */
256 : handle( 0 );
257 : speed( 40 ,40 );
258 : break;
259 : case 0x04:
If written by continuing the case
260 : case 0x06:
261 : case 0x07: It means that time of 0x04 or 0x06 or 0x07 or
262 : case 0x03: 0x03.
263 : /* Tending to the left->Turn to the right */
264 : handle( 8 );
265 : speed( 40 ,36 );
266 : break;
267 : case 0x20:
If written by continuing the case
268 : case 0x60:
269 : case 0xe0: It means that time of 0x20 or 0x60 or 0xe0 or 0xc0.
270 : case 0xc0:
271 : /* Tending to the right->Turn to the left */
272 : handle( -8 );
273 : speed( 36 ,40 );
274 : break;
275 : default:
276 : break;
277 : }
278 : break;
279 :
280 : case 24:
281 : /* 2 of trace after crossline and crank detection */
282 : switch( sensor_inp(MASK3_3) ) {
283 : case 0xe0:
284 : /* Judgment of left crank->To the left crank clearing process */
285 : led_out( 0x1 );
286 : handle( -38 );
287 : speed( 10 ,50 );
288 : pattern = 31;
289 : cnt1 = 0;
290 : break;

- 114 -
Program Explanation Manual “kit05.c”

291 : case 0x07:


292 : /* Judgment of right crank->The process for getting over right crank */
293 : led_out( 0x2 );
294 : handle( 38 );
295 : speed( 50 ,10 );
296 : pattern = 41;
297 : cnt1 = 0;
298 : break;
299 : case 0x00:
300 : /* Center->Straight */
301 : handle( 0 );
302 : speed( 40 ,40 );
303 : break;
304 : case 0x04:
305 : case 0x06:
306 : case 0x03:
307 : /* Tending to the left->Turn to the right */
308 : handle( 8 );
309 : speed( 40 ,36 );
310 : break;
311 : case 0x20:
312 : case 0x60:
313 : case 0xc0:
314 : /* Tending to the right->Turn to the left */
315 : handle( -8 );
316 : speed( 36 ,40 );
317 : break;
318 : default:
319 : break;
320 : }
321 : break;

3.32. Pattern 31, 32 : Left crank clear processing

When sensor becomes “0xe0” in pattern 24, the steering turns with large angle by judgment that it is the left crank.
And we consider the micom car shall be able to get over the crank. But, there may be the following problem that
until when, the steering continues the large turning to left.
It is as shown in following figure.
●●●×× ○○ ●




×
●●

×


××



●○

○○○× ×●●●

When sensor state becomes "0x60",


turning is ended and returned to pattern 11.

- 115 -
Program Explanation Manual “kit05.c”

In case of “0xe0”, it is turned to left with a large angle but as it is in speed, it takes an expanded turn. It is
returned to pattern 11 by ending the turning when sensor comes near to the center line and becomes “0x60”.
This is made into a program.

case 31:
if( sensor_inp(MASK3_3) == 0x60 ) {
pattern = 11;
}
break;

Program is completed and the micom car is made to run. If this is done, the moment the sensor state has become
“0xe0”, the steering is turned to the left. When it was continued to turn like this, immediately it turned straight and
the wheel jumps from the course. Connector of motor and servo is unplugged and it is processed slowly by hand
as the operation is too quick and cannot be understood properly. If the sensor state is observed carefully, it
becomes like the following figure.

1 2
●○ ○× × ● ●●
○ ○○× ×●● ●

"0xe0" is detected, When it turns, in the part where the color changes
and turn till it becomes "0x60" completely from white to black, sensor state
becomes "0x60" and it ends large turning immediately.
And the execution moves to pattern 11.
And shifts to pattern 11.
●● ●×× ●●●

3 4
● ●●×× ●● ●

"0x00" is detected and it goes straight 100%. The wheels jump from the course like this
and it is called "the course out".

As shown in figure 2, by the transition of white and black (it is actually white, gray and black but, gray appears
as white), it is understood that the state of the sensor is "0x06". The sensor on the extreme left changes to "0"
because the sensor is not adjusted properly. Though it might be better to increase the sensitivity of the extreme left
sensor, there is a possibility of the wheel jumping from the racing course.Can anything be done in the program?
After some thought, it was noticed that some time was required from the detection of "0xe0" until the final
sensor state of "0x06". After detecting the crossline, here too, it waits for a small progression using the timer as if
the sensor has not detected anything for 0.2 seconds. Check the sensor. Draw and make an image.

- 116 -
Program Explanation Manual “kit05.c”

1 ●●
● 2
×
●×
●●

○○○× ×●●●

"0xe0" is detected, and waited for 0.2 seconds After 0.2 seconds, sensor is confirmed.
after turning with a large angle. "Turning is continued till 0x60".

×× ○ ○

3 4

●●●



×
×


State of "0 x 06", still turning is continued. Detection of "0x60"


End of turn. Go to normal run of pattern 11.

The micom car is at the position where a white and black turning point was exceeded beyond after 0.2 seconds, as
shown in Figure 2. Later, it continues to turn securely till it becomes “0x60”. If this idea is adopted, it seems to be
perfect. Program is done.

323 : case 31:


324 : /* Left crank clear process Wait a little till it becomes stable. */
325 : if( cnt1 > 200 ) {
326 : pattern = 32;
327 : cnt1 = 0;
328 : }
329 : break;
330 :
331 : case 32:
332 : /* Left crank clear process Check of end of turn */
333 : if( sensor_inp(MASK3_3) == 0x60 ) {
334 : led_out( 0x0 );
335 : pattern = 11;
336 : cnt1 = 0;
337 : }
338 : break;

The program checks whether the cnt1 is more than 200 or not on 325th line. If it is more than 200, in other words,
if 0.2 seconds are passed, shifts to pattern 32. Incidentally, cnt1 clear is done on the 289th before the execution
moves to pattern 31.

- 117 -
Program Explanation Manual “kit05.c”

3.33. Pattern 41, 42: Right crank clear processing

When the sensor becomes “0x07" in pattern 24, it is considered as right crank and the crank is cleared by a large
turn to the right.
The next problem that arises is: "Turn to the right up to what extent?"
It is considered to be as shown in the figure below.

●○○×× ●●●


●●
×
×

●×

○○ ×
●●●× ×○○ ○


When sensor state becomes "0x06",
turning is completed and returned to pattern 11.

Though the car turns to the right in case of “0x07”, it takes a large expanding turn due to the high speed. When the
sensor comes near the center-line and becomes "0x06", the turns are complete and the execution returns to pattern
11.
This is described in the program.

case 41:
if( sensor_inp(MASK3_3) == 0x06 ) {
pattern = 11;
}
break;

Program is completed and the micom car is made to run. If this is done, the moment the sensor state has become
“0x07”, the steering is turned to the right. When it was continued to turn like this, immediately it turned straight
and the wheel jumps from the course. Connector of motor and servo is unplugged and the car is processed slowly
by hand as the actual operation is too quick and cannot be understood properly. If the sensor state is observed
carefully, it becomes like the following figure.

- 118 -
Program Explanation Manual “kit05.c”

1 2
●●●××○○●
● ●●× ×○○ ○

Detected "0x07", The moment it turned, the sensor state became


Large turn until "0x06". "0x06" in the part where the white competes with
black. The execution moves to pattern 11
after the momentary large turn.

● ●●× ×●● ●

3 4
●●● ××● ●●

"0x00" is detected, 100% direct advancement. The wheels jump from the course like this
and it is called "the course out".

As shown in figure 2, by the transition of white and black (actually white, gray and black but, gray appears as
white), the state of the sensor is “0x06”. The sensor on the extreme right changes to “0” before it is adjusted
properly. Though it might be better to increase the sensitivity of the extreme right sensor, there is a possibility of
the wheel coming off the axle hence can anything be done in the program?
It was found that some time was required from the detection of "0x07" until the final sensor state of "0x06". After
detecting the crossline, here too it waits for a small progression using the timer as if the sensor has not detected
anything for 0.2 seconds. Can the sensor be checked for that?

- 119 -
Program Explanation Manual “kit05.c”

1 2 ●●
●×
×●
●●

●● ●××○○○

Detection of "0x07", After 0.2 seconds, the sensor is confirmed


Wait for 0.2 seconds after large turn. from here. "Continue turning up to 0x06."
●○○

3 4
×× ●

●●
●●

●×
○○ ×

States of "0x60" Still turning is continued. Detection of "0x06". End of turn.
The execution goes to normal run of pattern 11.

After 0.2 seconds, as shown in Fig 2, the micom car is at the position where a white and black turning point was
exceeded beyond. Later, it continues to turn securely until it becomes “0x06”, if this idea is acceptable, it seem to
be perfect. Program is created.

340 : case 41:


341 : /* Right crank clear processing Waits a little until stabilizing */
342 : if( cnt1 > 200 ) {
343 : pattern = 42;
344 : cnt1 = 0;
345 : }
346 : break;
347 :
348 : case 42:
349 : /* Right crank clear processing Turning completion check */
350 : if( sensor_inp(MASK3_3) == 0x06 ) {
351 : led_out( 0x0 );
352 : pattern = 11;
353 : cnt1 = 0;
354 : }
355 : break;

The program checks that cnt1 is 200 or more in 342nd line. If it is above 200, that is, if 0.2 seconds have passed,
the execution moves to pattern 42. Clear of cnt1 is carried out in 297th line before moving to pattern 41.

- 120 -
Program Explanation Manual “kit05.c”

3.34. None of the above patterns

357 : default:
358 : /* When the pattern is not applied to any case, return to waiting state */
359 : pattern = 0;
360 : break;

Default system is executed, when the pattern is none of the above. The pattern is made to 0 and is set to standby.
Execution of default statement means that some inconsistent pattern was made in the program while changing the
pattern. That part should be searched and corrected.

- 121 -
Program Explanation Manual “kit05.c”

3.35. Method of calculating right and left rotation difference of motor

When the steering is turned, the rotation of the tire is different on the inner and outer sides. The calculation
method is shown below.

Micom car in which steering


is turned by 30°to the left.

Straight facing micom car


θ

W=0.175m W

θ=30°=π/6

T=0.14m r1

r2

r3

T = Tread...Distance of central line of right and left wheel. It is 0.14[m] in kit.


W = Wheelbase...Interval between front wheel and rear wheel. It is 0.175[m] in kit.

According to the figure, the triangular relation of the base r2, height W, and angleθ is as follows.
Tanθ = W/r2

Because W and Angle θ are known, r2 is known.


r2 = W/tanθ = 0.175/tan(π/6) = 0.303[m]

Radius of inner curve is,


r1 = r2 - T/2 = 0.303 - 0.07 = 0.233

Radius of outer curve is,


r3 = r2+T/2 = 0.303 +0.07 = 0.373

Therefore, if the rotation of the outer wheel is 100, the rotation of the inner wheel is,
r1/r3x100 = 0.233/0.373x100 = 62

When steering is turned by 30° to the left, the rotation of left tire becomes 62 as against 100 of right tire.

- 122 -
Program Explanation Manual “kit05.c”

Setting as follows in the program can do the rotation without the loss of inner and outer tires.

handle ( -30 );
speed( 62, 100 );

It is convenient to create a table of angles from 0 to 45 and next is the rotation rate of the left and right tires in
Excel.

Cell Contents Value, example of expression


C1 Input Wheelbase 0.175, if it is a kit
C2 Input Tread 0.14, if it is a kit.
B row Input angle 0 to 45 Direct input
C row Angle ° is converted into rad. C6 cell = B6*3.14/180
D row Calculation of r2 of figure on preceding page D6 cell = $C$1/TAN(C6)
E row Calculation of r1 of figure on preceding page E6 cell = D6-$C$2/2
F row Calculation of r3 of figure on preceding page F6 cell = D6+$C$2/2
G row Calculation of ratio G6 cell = E6/F6*100
Table when W is assumed 0.175[m] and T is assumed 0.14[m] is shown on the next page.
For example, at the time of normal run when the sensor state is “0x06”, the program is as follows.

158 : case 0x06:


159 : /* Slight left inclination->Slight turn to the right */
160 : handle( 10 );
161 : speed( 80 ,?? ); <-?? Is unknown
162 : break;

In order to turn slightly to the right, the steering is turned 30° to the right, left tire 80%. The PWM value of the
right tire on inner side is not known. As from table, it is understood that at 10° the left tire on inner side is 87%.
However, this is a value when the outer tire is 100%. Since here it is 80%,
Right tire = PWM of left tire / ratio 100 = 80 / 87 100 = 69
Therefore, the PWM value of the right tire is set to 69. In kit05.c, the rotation difference between inner and outer
is thus calculated.

- 123 -
Program Explanation Manual “kit05.c”

T Table Relation of inner side


Degrees rad r2 r1 r3 r1/r3*100
0 0 100
1 0.017 10.031 9.961 10.101 99
2 0.035 5.014 4.944 5.084 97
3 0.052 3.341 3.271 3.411 96
4 0.070 2.504 2.434 2.574 95
5 0.087 2.001 1.931 2.071 93
6 0.105 1.666 1.596 1.736 92
7 0.122 1.426 1.356 1.496 91
8 0.140 1.246 1.176 1.316 89
9 0.157 1.105 1.035 1.175 88
10 0.174 0.993 0.923 1.063 87
11 0.192 0.901 0.831 0.971 86
12 0.209 0.824 0.754 0.894 84
13 0.227 0.758 0.688 0.828 83
14 0.244 0.702 0.632 0.772 82
15 0.262 0.653 0.583 0.723 81
16 0.279 0.611 0.541 0.681 79
17 0.297 0.573 0.503 0.643 78
18 0.314 0.539 0.469 0.609 77
19 0.331 0.509 0.439 0.579 76
20 0.349 0.481 0.411 0.551 75
21 0.366 0.456 0.386 0.526 73
22 0.384 0.433 0.363 0.503 72
23 0.401 0.413 0.343 0.483 71
24 0.419 0.393 0.323 0.463 70
25 0.436 0.376 0.306 0.446 69
26 0.454 0.359 0.289 0.429 67
27 0.471 0.344 0.274 0.414 66
28 0.488 0.329 0.259 0.399 65
29 0.506 0.316 0.246 0.386 64
30 0.523 0.303 0.233 0.373 62
31 0.541 0.291 0.221 0.361 61
32 0.558 0.280 0.210 0.350 60
33 0.576 0.270 0.200 0.340 59
34 0.593 0.260 0.190 0.330 58
35 0.611 0.250 0.180 0.320 56
36 0.628 0.241 0.171 0.311 55
37 0.645 0.232 0.162 0.302 54
38 0.663 0.224 0.154 0.294 52
39 0.680 0.216 0.146 0.286 51
40 0.698 0.209 0.139 0.279 50
41 0.715 0.201 0.131 0.271 48
42 0.733 0.195 0.125 0.265 47
43 0.750 0.188 0.118 0.258 46
44 0.768 0.181 0.111 0.251 44
45 0.785 0.175 0.105 0.245 43
When W is 0.175[m] and T is 0.14[m]

When the value of W and T are in the length of one’s own micom car, the rotation rate of the right and left tires is
understood.

- 124 -
Program Explanation Manual “kit05.c”

The “angle calculation.xls” file is an example. The following file opens when this is opened.

If

* L1 cell->Input of steering angle


* L2 cell->Input of outer wheel rim speed

of the cell are set, the angle of the steering and the speed value are input in part automatically.
This is convenient. Actually, this Excel sheet did the right and left rotation difference calculation of kit05.c.
However, please match the wheelbase and the tread carefully.

- 125 -
Program Explanation Manual “kit05.c”

4. Program Explanation “kit05start.src”


4.1. Program List
1 : .CPU 300HA: 20 ; Specification of CPU
2 :
3 : RESERVE: .EQU H'FFFFFFFF ; Address of unused area
4 :
5 : .IMPORT _main ; External reference (main function)
6 : .IMPORT _interrupt_timer0 ; External reference (interrupt_timer0 function)
7 :
8 : .SECTION V ; Start of vector section
9 : .DATA.L RESET_START ; 0 h'000000 reset
10 : .DATA.L RESERVE ; 1 h'000004 System reserved
11 : .DATA.L RESERVE ; 2 h'000008 System reserved
12 : .DATA.L RESERVE ; 3 h'00000c System reserved
13 : .DATA.L RESERVE ; 4 h'000010 System reserved
14 : .DATA.L RESERVE ; 5 h'000014 System reserved
15 : .DATA.L RESERVE ; 6 h'000018 System reserved
16 : .DATA.L RESERVE ; 7 h'00001c External interrupt NMI
17 : .DATA.L RESERVE ; 8 h'000020 Trap instruction
18 : .DATA.L RESERVE ; 9 h'000024 Trap instruction
19 : .DATA.L RESERVE ; 10 h'000028 Trap instruction
20 : .DATA.L RESERVE ; 11 h'00002c Trap instruction
21 : .DATA.L RESERVE ; 12 h'000030 External interrupt IRQ0
22 : .DATA.L RESERVE ; 13 h'000034 External interrupt IRQ1
23 : .DATA.L RESERVE ; 14 h'000038 External interrupt IRQ2
24 : .DATA.L RESERVE ; 15 h'00003c External interrupt IRQ3
25 : .DATA.L RESERVE ; 16 h'000040 External interrupt IRQ4
26 : .DATA.L RESERVE ; 17 h'000044 External interrupt IRQ5
27 : .DATA.L RESERVE ; 18 h'000048 System reserved
28 : .DATA.L RESERVE ; 19 h'00004c System reserved
29 : .DATA.L RESERVE ; 20 h'000050 WDT MOVI
30 : .DATA.L RESERVE ; 21 h'000054 REF CMI
31 : .DATA.L RESERVE ; 22 h'000058 System reserved
32 : .DATA.L RESERVE ; 23 h'00005c System reserved
33 : .DATA.L _interrupt_timer0 ; 24 h'000060 ITU0 IMIA0
34 : .DATA.L RESERVE ; 25 h'000064 ITU0 IMIB0
35 : .DATA.L RESERVE ; 26 h'000068 ITU0 OVI0
36 : .DATA.L RESERVE ; 27 h'00006c System reserved
37 : .DATA.L RESERVE ; 28 h'000070 ITU1 IMIA1
38 : .DATA.L RESERVE ; 29 h'000074 ITU1 IMIB1
39 : .DATA.L RESERVE ; 30 h'000078 ITU1 OVI1
40 : .DATA.L RESERVE ; 31 h'00007c System reserved
41 : .DATA.L RESERVE ; 32 h'000080 ITU2 IMIA2
42 : .DATA.L RESERVE ; 33 h'000084 ITU2 IMIB2
43 : .DATA.L RESERVE ; 34 h'000088 ITU2 OVI2
44 : .DATA.L RESERVE ; 35 h'00008c System reserved
45 : .DATA.L RESERVE ; 36 h'000090 ITU3 IMIA3
46 : .DATA.L RESERVE ; 37 h'000094 ITU3 IMIB3
47 : .DATA.L RESERVE ; 38 h'000098 ITU3 OVI3
48 : .DATA.L RESERVE ; 39 h'00009c System reserved
49 : .DATA.L RESERVE ; 40 h'0000a0 ITU4 IMIA4
50 : .DATA.L RESERVE ; 41 h'0000a4 ITU4 IMIB4
51 : .DATA.L RESERVE ; 42 h'0000a8 ITU4 OVI4
52 : .DATA.L RESERVE ; 43 h'0000ac System reserved
53 : .DATA.L RESERVE ; 44 h'0000b0 DMAC DEND0A
54 : .DATA.L RESERVE ; 45 h'0000b4 DMAC DEND0B
55 : .DATA.L RESERVE ; 46 h'0000b8 DMAC DEND1A
56 : .DATA.L RESERVE ; 47 h'0000bc DMAC DEND1B
57 : .DATA.L RESERVE ; 48 h'0000c0 System reserved
58 : .DATA.L RESERVE ; 49 h'0000c4 System reserved
59 : .DATA.L RESERVE ; 50 h'0000c8 System reserved
60 : .DATA.L RESERVE ; 51 h'0000cc System reserved
61 : .DATA.L RESERVE ; 52 h'0000d0 SCI0 ERI0
62 : .DATA.L RESERVE ; 53 h'0000d4 SCI0 RXI0
63 : .DATA.L RESERVE ; 54 h'0000d8 SCI0 TXI0
64 : .DATA.L RESERVE ; 55 h'0000dc SCI0 TEI0
65 : .DATA.L RESERVE ; 56 h'0000e0 SCI1 ERI1
66 : .DATA.L RESERVE ; 57 h'0000e4 SCI1 RXI1
67 : .DATA.L RESERVE ; 58 h'0000e8 SCI1 TXI1
68 : .DATA.L RESERVE ; 59 h'0000ec SCI1 TEI1
69 : .DATA.L RESERVE ; 60 h'0000f0 A/D ADI
70 :
71 : .SECTION P ; Start of program section
72 : RESET_START:
73 : MOV.L #H'FFFF10,ER7 ; Stack specification
74 : JSR @_main ; Jump to main() function of C language
75 : OWARI:
76 : BRA OWARI
77 :
78 : .END

- 126 -
Program Explanation Manual “kit05.c”

4.2. “.CPU” command

1: .CPU 300HA: 20 ; Specification of CPU

This command specifies the type of CPU. “300HA” specifies H8/300H advanced mode. H8/3048F-ONE uses this
mode. Moreover, “: 20” specifies the bit width of the address space in 20-bit width (address h'00000-h' fffff ).
Since CPU board used with the kit is a single chip mode, which does not use external memory, it is assumed as
20-bit width, though a 24-bit width also is specified.

4.3. Setting of external reference

5: .IMPORT _main ; external reference (main function)


6: .IMPORT _interrupt_timer0 ; external reference (interrupt_timer0 function)

An external reference is defined. External reference means “Not in this file, but there is a label (function) in
another file, so refer to the external file”. External files are the files which are to be united at the time of linking.
The assembler knows that “the label defined with .IMPORT is in another file” and reserves only the name and area.
This reserved address name is allocated when the address of the label is decided by the operation of the link. When
the label defined at the time of link operation is not found, it becomes a link error. The link is described later.
There is “_” (underscore) at the beginning of the label, the reason is that there is regulation of putting “_” at the
beginning, when calling label (function) name of C language file (kit05.c) from assembler file (kit05start.src).

4.4. Declaration of section V

8: .SECTION V ; Start of vector section


9: .DATA.L RESET_START ; 0 h'000000 reset
10 : .DATA.L RESERVE ; 1 h'000004 System reserved
11 : .DATA.L RESERVE ; 2 h'000008 System reserved
12 : .DATA.L RESERVE ; 3 h'00000c System reserved
13 : .DATA.L RESERVE ; 4 h'000010 System reserved
14 : .DATA.L RESERVE ; 5 h'000014 System reserved
15 : .DATA.L RESERVE ; 6 h'000018 System reserved
16 : .DATA.L RESERVE ; 7 h'00001c External interrupt NMI
17 : .DATA.L RESERVE ; 8 h'000020 Trap instruction
18 : .DATA.L RESERVE ; 9 h'000024 Trap instruction
19 : .DATA.L RESERVE ; 10 h'000028 Trap instruction
20 : .DATA.L RESERVE ; 11 h'00002c Trap instruction
21 : .DATA.L RESERVE ; 12 h'000030 External interrupt IRQ0
22 : .DATA.L RESERVE ; 13 h'000034 External interrupt IRQ1
23 : .DATA.L RESERVE ; 14 h'000038 External interrupt IRQ2
24 : .DATA.L RESERVE ; 15 h'00003c External interrupt IRQ3
25 : .DATA.L RESERVE ; 16 h'000040 External interrupt IRQ4
26 : .DATA.L RESERVE ; 17 h'000044 External interrupt IRQ5
27 : .DATA.L RESERVE ; 18 h'000048 System reservedd
28 : .DATA.L RESERVE ; 19 h'00004c System reservedd
29 : .DATA.L RESERVE ; 20 h'000050 WDT MOVI

- 127 -
Program Explanation Manual “kit05.c”

30 : .DATA.L RESERVE ; 21 h'000054 REF CMI


31 : .DATA.L RESERVE ; 22 h'000058 System reserved
32 : .DATA.L RESERVE ; 23 h'00005c System reserved
33 : .DATA.L _interrupt_timer0 ; 24 h'000060 ITU0 IMIA0
34 : .DATA.L RESERVE ; 25 h'000064 ITU0 IMIB0
35 : .DATA.L RESERVE ; 26 h'000068 ITU0 OVI0
36 : .DATA.L RESERVE ; 27 h'00006c System reserved
37 : .DATA.L RESERVE ; 28 h'000070 ITU1 IMIA1
38 : .DATA.L RESERVE ; 29 h'000074 ITU1 IMIB1
39 : .DATA.L RESERVE ; 30 h'000078 ITU1 OVI1
40 : .DATA.L RESERVE ; 31 h'00007c System reserved
41 : .DATA.L RESERVE ; 32 h'000080 ITU2 IMIA2
42 : .DATA.L RESERVE ; 33 h'000084 ITU2 IMIB2
43 : .DATA.L RESERVE ; 34 h'000088 ITU2 OVI2
44 : .DATA.L RESERVE ; 35 h'00008c System reserved
45 : .DATA.L RESERVE ; 36 h'000090 ITU3 IMIA3
46 : .DATA.L RESERVE ; 37 h'000094 ITU3 IMIB3
47 : .DATA.L RESERVE ; 38 h'000098 ITU3 OVI3
48 : .DATA.L RESERVE ; 39 h'00009c System reserved
49 : .DATA.L RESERVE ; 40 h'0000a0 ITU4 IMIA4
50 : .DATA.L RESERVE ; 41 h'0000a4 ITU4 IMIB4
51 : .DATA.L RESERVE ; 42 h'0000a8 ITU4 OVI4
52 : .DATA.L RESERVE ; 43 h'0000ac System reserved
53 : .DATA.L RESERVE ; 44 h'0000b0 DMAC DEND0A
54 : .DATA.L RESERVE ; 45 h'0000b4 DMAC DEND0B
55 : .DATA.L RESERVE ; 46 h'0000b8 DMAC DEND1A
56 : .DATA.L RESERVE ; 47 h'0000bc DMAC DEND1B
57 : .DATA.L RESERVE ; 48 h'0000c0 System reserved
58 : .DATA.L RESERVE ; 49 h'0000c4 System reserved
59 : .DATA.L RESERVE ; 50 h'0000c8 System reserved
60 : .DATA.L RESERVE ; 51 h'0000cc System reserved
61 : .DATA.L RESERVE ; 52 h'0000d0 SCI0 ERI0
62 : .DATA.L RESERVE ; 53 h'0000d4 SCI0 RXI0
63 : .DATA.L RESERVE ; 54 h'0000d8 SCI0 TXI0
64 : .DATA.L RESERVE ; 55 h'0000dc SCI0 TEI0
65 : .DATA.L RESERVE ; 56 h'0000e0 SCI1 ERI1
66 : .DATA.L RESERVE ; 57 h'0000e4 SCI1 RXI1
67 : .DATA.L RESERVE ; 58 h'0000e8 SCI1 TXI1
68 : .DATA.L RESERVE ; 59 h'0000ec SCI1 TEI1
69 : .DATA.L RESERVE ; 60 h'0000f0 A/D ADI

“.SECTION" is a delimiter of the program, and the name “V” is used here. Section V is allocated to the address
that has been described in the sub file. The label which jumps when interrupts which include “reset” occur, is
specified in section V.
“.DATA.L” is a command to allocate data. “L” indicates “long”, “long” means 4 bytes. Since data on 9th line is
“RESET_START”, the value of the address with “RESET_START" is written. If it is h'00100,
“h'00,h'00,h'01,h'00” is written.
In H8/3048F-ONE, the procedure to generate interrupt carries out the following processing,
1. Checks various interrupt flags when the currently executing command ends.
2. If there is an interrupt, moves (shunts) the PC, CCR register to interrupt processing routine.
3. According to the kind of the interrupt generated, the address data of destination to jump is read from the decided
address
4. The interrupt program is executed by jumping to the specified location.
That “decided address” means address h'00000 ~ h'000f. This is a special address and is called a vector address.

- 128 -
Program Explanation Manual “kit05.c”

When the power supply of CPU is turned on, it becomes a ‘power supply turned on’ interrupt. Though there are no
procedures(like 1, 2), the jump destination address data written in the h'00000 address is read and program is
executed by jumping to the specified location. For example, the case when jump destination is address h'00100 is
shown below.

Value h’000100 address


h’00000 h’00 RESET_START:
h’00001 h’00 MOV.L #H'FFFF10,ER7
h’00002 h’01 JSR @_main
h’00003 h’00 OWARI: BRA OWARI

At the time of reset, the data "h'000100" at the address h'00000 is read and
executed by jumping to address h'000100.

The table which summarizes the type of interrupt and vector address is shown below.

Vector Priorit
Interrupt factor Origin Vector Address IPR
No. y level
External
Reset 0 H'0000 - H'0003 −−−
terminal
NMI 7 H'001C - H'001F −−− high
IRQ0 12 H'0030 - H'0033 IPRA7 ↑
IRQ1 13 H'0034 - H'0037 IPRA6
External
IRQ2 14 H'0038 - H'003B
terminal IPRA5
IRQ3 15 H'003C - H'003F
IRQ4 16 H'0040 - H'0043
IRQ5 17 H'0044 - H'0047
IPRA4
18 H'0048 - H'004B
Reserve −−−−−
19 H'004C - H'004F
WOVI (Interval timer) Watch dog timer 20 H'0050 - H'0053
Refresh
CMI (Compare match) 21 H'0054 - H'0057
controller IPRA3
22 H'0058 - H'005B
Reserve −−−−−
23 H'005C - H'005F
IMIA0 (Compare match/input capture A0) 24 H'0060 - H'0063
ITU
IMIB0 (Compare match/input capture B0) 25 H'0064 - H'0067
Channel 0 IPRA2
OVI0 (Overflow 0) 26 H'0068 - H'006B
Reserve ───── 27 H'006C - H'006F
IMIA1 (Compare match/input capture A1) 28 H'0070 - H'0073
ITU
IMIB1 (Compare match/input capture B1) 29 H'0074 - H'0077
Channel 1 IPRA1
OVI1 (Overflow 1) 30 H'0078 - H'007B
Reserve ───── 31 H'007C - H'007F
IMIA2 (Compare match/input capture A2) 32 H'0080 - H'0083
ITU
IMIB2 (Compare match/input capture B2) 33 H'0084 - H'0087
Channel 2 IPRA0
Reserve 34 H'0088 - H'008B
Reserve ───── 35 H'008C - H'008F

- 129 -
Program Explanation Manual “kit05.c”

36 H'0090 - H'0093
ITU
Channel 3 IPRB7
IMIB3 (Compare match/input capture B3) 37 H'0094 - H'0097
OVI3 (Overflow 3) 38 H'0098 - H'009B
Reserve ───── 39 H'009C - H'009F
IMIA4 (Compare match/input capture A4) 40 H'00A0 - H'00A3
ITU
IMIB4 (Compare match/input capture B4) 41 H'00A4 - H'00A7
Channel 4 IPRB6
OVI4 (Overflow 4) 42 H'00A8 - H'00AB
Reserve ───── 43 H'00AC - H'00AF
DEDN0A 44 H'00B0 - H'00B3
DEDN0B DMAC 45 H'00B4 - H'00B7
IPRB5
DEDN1A Group 0 46 H'00B8 - H'00BB
DEDN1B 47 H'00BC - H'00BF
48 H'00C0 - H'00C3
49 H'00C4 - H'00C7
Reserve ───── ──
50 H'00C8 - H'00CB
51 H'00CC - H'00CF
ERI0 (Reception error 0) 52 H'00D0 - H'00D3
RXI0 (Reception completion0) SCI 53 H'00D4 - H'00D7
IPRB3
TXI0 (Transmission data empty 0) Channel 0 54 H'00D8 - H'00DB
TEI0 (Transmission end 0) 55 H'00DC - H'00DF
ERI1 (Reception error 1) 56 H'00E0 - H'00E3
RXI1 (Reception completion1) SCI 57 H'00E4 - H'00E7
IPRB2
TXI1 (Transmission data empty 1) Channel 1 58 H'00E8 - H'00EB ↓
TEI1 (Transmission end 1) 59 H'00EC - H'00EF
ADI (A/D End) A/D 60 H'00F0 - H'00F3 IPRB1 low

As shown in the table the numbers from 0 to 60 are allocated and kit05start.src defines the addresses of all the
numbers. “RESERVE” name is given to the vector that is not a jumping destination and only the location is
secured. Later when using the interrupt, it is possible to change it easily by changing the name "RESERVE" of
corresponding vector address to jump destination address.” RESERVE" is allocated in third line of the program
list (kit05start.src), with h 'FFFFFFFF.

In kit05.c, it is declared as,


Reset of vector number 0 is "RESET_START"
ITU0 IMIA of vector number 24 is declared as "_interrupt_timer0".
There is "_" (underscore) at the beginning of the label. The reason is that there is regulation of putting " _ " at the
beginning when calling label (function) name of C language file (kit05.c) from assembler file (kit05start.src).
Since the section V allocates the vector address, h'00000 is allocated. The address is specified in the sub file. The
specification method is described later.

- 130 -
Program Explanation Manual “kit05.c”

4.5. Declaration of Section P

71 : .SECTION P ; Start of program section


72 : RESET_START:
73 : MOV.L #H'FFFF10,ER7 ; Setting of stuck
74 : JSR @_main ; Jumping to the main() function of C language
75 : OWARI:
76 : BRA OWARI

This is the section named “P”. Here actually are program described while the section ”V” describes address of
jumping destination at interruption. Process in section P is stuck area setting and command to jump into “_main”.
“_main” is a main function of file c, and C language program is executed from the main function.
“BRA OWARI” at the end is process when main function of C language finishes and returns. It is usual that the
main function does not terminates, though, the process jumps into itself to let CPU do nothing in case the main
function terminates. Without this description, process goes to address without program or address with unknown
program, resulting that CPU continues meaningless operation out of control.

4.6. END

78 : .END

“src” file declares always the end of assembly program with “.END”.
Even If some program is written after this command, it is ignored.

- 131 -
Program Explanation Manual “kit05.c”

5. Program Explanation “kit05.sub”

5.1. Program List

It is an information file to link (unite) the files "kit05.obj" and “kit05start.obj".

1: input kit05start,kit05
2: lib c: ¥h8n_win¥3048¥c¥c38hae.lib
3: output kit05
4: print kit05
5: start V(000000)
6: start P,C(000100)
7: start B(0fef10)
8: exit

5.2. input Command

1: input kit05start,kit05

The files that are linked with the "input" command are described. Each “obj” file is described by delimiting with
comma and without extension. Here, “kit05start” and “kit05” are specified.

5.3. lib Command

2: lib c: ¥h8n_win¥3048¥c¥c38hae.lib

The read library is described by “lib” command. The library is a file, which is used when the C language program
is converted into the assembler. The folder where the library is preserved is usually, “c: ¥h8n_win¥3048¥c" and
the library name is “c38hae.lib". On combining, it becomes "c: ¥h8n_win¥3048¥c¥c38hae.lib".

5.4. output Command

3: output kit05

The "output" command describes the file name when the mot file is created. As it is “kit05”, “kitt05.mot” file is
created.

- 132 -
Program Explanation Manual “kit05.c”

5.5. print Command

4: print kit05

The "print" command describes the file name when the map file is created. The “kit05.map" file is created since it
is assumed kit05.

5.6. start Command

5: start V(000000)
6: start P,C(000100)
7: start B(0fef10)

The beginning of the address where the section is to be allocated is described by the "start" command and the
command is specified as "Section name (hexadecimal number address)". The section names that are automatically
assigned by the compiler are as follows.

Section Name Contents

P Section where the program is stored. Allocated in the ROM area.

Section where “const type” data is stored. Allocated in the ROM area. When the variable is
C declared “const”, section C is allocated. Though it is not used in kit05.c, it has been defined
taking into consideration that it is used for adding programs.

Section where the uninitialized data is stored. It is allocated in the RAM area. Section B is
B used when the global variable is allocated. Since cnt0 and cnt1 are allocated as global
variables at this time, it is used.

The addresses for allocation of these sections are decided. Moreover, because section V is originally defined in the
start-up routine, this allocated address is declared.

Section Start address Contents Remarks


V h’00000 Reset vector address Self assigned name
Name automatically given by the
P h’00100 Program address
compiler
Const type data storing Name automatically given by the
C After P section ends
address compiler
Name automatically given by the
B h’FEF10 Global variable address
compiler

When it is compiled or compiled and written in development environment, the following error is generated.

- 133 -
Program Explanation Manual “kit05.c”

H Series LINKAGE EDITOR (Evaluation software) Ver.1.0

: input kit05start,kit05
: lib c: ¥h8n_win¥3048¥c¥c38hae.lib
: output kit05
: print kit05
: start V(000000)
: start P,C(000100)
: start B(0fef10)
: exit
** 121 CANNOT FIND SECTION(C)

This is an error, which says, "There is no section C". As it is explained previously, section C is a section where the
“const type” data is stored. As “const” is not declared in kit05.c, section C is not established, and the error
message is given. Though it is a message, which informs that there is no section C, it can be ignored because there
is no trouble with the execution of the program.

If you consider that there is still a problem, extract the part “,C” from “start P,C(000100)”, so, the command is
such that 6th line of "kit05.sub" as below and the error is not outputted.

6: start P(000100)

However, when the const type is declared after acquisition, section C is allocated after section B.

The section to which address is not allocated is allocated after the last section.

The last section is section B. Section B is allocated in the RAM area. If the position of section C is not specified in
the sub file, it is allocated after the section B, that is, it is allocated in the RAM area. The error that there is no
allocation address of section C, is not outputted. Since section C should originally be allocated in ROM area,its
MOT file does not operate normally.
Therefore, it has been set such that the error is outputted initially and it is better than that the error is not outputted
at the time of usage and ‘no operation if there is no error’.

5.7. exit Command

8: exit

sub file ends by the "exit' command.

- 134 -
Program Explanation Manual “kit05.c”

6. File Conversion Mechanism


6.1. File
In this development environment, 3 files become 1 set and the file finally written in CPU is completed.

It is C source program file written in C language. It is the main part, which controls
kit05.c
the micom car.
It is an assembly source program file which programs the vector address of start-up
kit05start.src routine and interrupt jump destination. File name is "Name of C language file
excluding extension + start.src"
It is a subcommand file where information when files are linked is described. File
kit05.sub
name is "Name of C language file excluding extension + .sub"

The following 6 files are created by assembling, compiling, linking and converting this file.

It is a file called list file for assembly program. When “kit05start.src” file is
kit05start.lis assembled, which type of machine language it has been converted to, can be known
by seeing this file.

It is a file called relocatable object program file. When “kit05start.src” file is


kit05start.obj
assembled, it is output as a file in half executed format.

It is a file called relocatable object program file. When “kit05start.src” file is


kit05.obj
assembled, it becomes the half execution form file.
It is a file called absolute load module file. It is a file after uniting the previous 2
kit05.abs
“obj” files.
It is a file, which is called linkage list file. It is a file, which outputs the address to
kit05.map which function and label are allocated when the “abs” file was created by linking
(uniting) the 2 “obj” files.

It is a file, which is called S type format. This file is output when “abs” file is
kit05.mot converted. The writing software reads this file, and writes the program in the CPU
board via RS-232 of computer.

Though the aim is to create "kit05.mot" file which stores the data to be finally written in the microcomputer from
the files "kit05.c" and "kit05start.src", the MOT file cannot be created if it does not follow the procedure of
assemble -> compile -> link -> convert.

- 135 -
Program Explanation Manual “kit05.c”

6.2. Change of file


The following figures shows how the file is converted.

kit05start.lis
List for assembly program
kit05.map
kit05start.src kit05start.obj Linkage list
Assembly source assemble Relocatable object Link
program program kit05.abs
Absolute load module compile
kit05.c kit05.obj
C source compile Relocatable object
program program
kit05.mot
kit05.sub
S type format
Subcommand link
… File from source information
Write with the writing
… File to be created software

Operation Contents

Assemble The assembly resource program is converted into the object program.

Compile C source program is converted into the object program.

Two or more object files are incorporated by referring to information of the subcommand,
Link
and made in to one absolute load module.
Absolute load module is converted to S type format file, and finally made into a file type
Convert
which can be written in the microcomputer.

Finally, the “kit05.mot” file is written in the CPU board with the writing software via RS-232C of the
computer.

- 136 -
Program Explanation Manual “kit05.c”

7. Bibliography

1. Renesas Technology Corp.


Hardware Manuals of H8/3048 Series, H8/3048F-ZTATTM (H8/3048F, H8/3048F-ONE), Version 7
2. Renesas Technology Corp., Semiconductor Training Center, C Language Elementary Course Text, Version 1
3. Ohmsha, Ltd., Complete Manual of H8 microcomputers, 1st version, published by Fujisawa Yukiho
4. Softbank (Inc), C Language for Elementary and Senior Levels by Hayashi Haruhiko, First Edition.
5. Kyoritsu Shuppan Co., Ltd. ANSI C complete books for programmer, published by L.Ammeraal
Translators:Yoshida Keiichi, Takeuchi Shukuko and Yoshida Emiko, First version
6. Dempa Shimbunsha Co., Ltd. Microcomputer introduction course Published by Takehiko Oosuga, First version

Please see the micom car rally official homepage for detailed information on the micom car rally.
http://www.mcr.gr.jp/

Please see the homepage of Renesas Technology Company Ltd. for detailed information on the H8
microcomputer.
http://japan.renesas.com/

The link is information as of April 2005.

- 137 -

You might also like