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 :
[email protected]
- 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 Sub board
B
C PortA Port7
CPU board
To Servo
A
Left gear
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 board
A
Sensor B CPU board C H8/3048F-ONE sub board
Motor drive board
Left motor servo
Sensor board
Black and white of course is read, and it is converted into digital signal of “0" or “1" and output. Output signal is white: “0", black: “1". 8 sensors are attached.
Sensor sub board
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 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.
CPU board
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 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.
Motor drive board
Weak electric signal from CPU board is converted into strong electric signal to operate the motor. The drive of the servo also uses the power supply for the motor. 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.
Battery
* 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 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 A P
CPU board
Motor drive board
7 P Left motor
Sensor sub board
5V It is 5V to the servo. 4 Nos. AA batteries for
Sensor board
control system power supply
4 Nos. AA batteries for
5V
driving system power
such as CPU, sensor, and
supply such as right and
control system circuit part of
left motor, and servo.
5V
motor drive board
Flow of power supply system is shown as follows. Motor drive board
Sensor board
5V
Sensor sub
5V
board
CPU board
Control system power supply Driving system power supply
5V
5V
Control
Driving
Right motor
system
system
Left motor
circuit
circuits
Servo
LM2940-5
LM350
5V
Control system power supply 4 Nos. AA batteries
-3-
Driving system power supply 4 Nos. AA batteries
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 A P
CPU board
Motor drive board
+
7 P
LM350 addition set Left motor
9.6V
Sensor sub board 6V to the servo
9.6V is for the secondary battery
9.6V 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
Sensor Board
5.0V
Sensor sub
5.0V
board
CPU board
5.0V
Control
Driving
system
system
circuit
circuits
5.0V
Control system power supply
LM350
9.6V Power supply 8 nos. AA batteries
-4-
9.6V
6V
LM2940-5
Driving system power supply
9.6V
Right motor Left motor
Servo
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. The sensitivity can be adjusted with the blue quadrangular part called a volume. Black 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 INPUT
OUTPUT LEVEL
Light ON
LOW
Light OFF
HIGH
-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
T o di gital va lu e
S en so r b o a rd
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 b o ard
Rig ht an d left re placem ent
7
6
5
4
3
2
1
0
0
1
1
1
0
0
0
0
(Hexadeci mal no : 0x70
P ortB
This value is used in program!!
P o rt7 Po rtA
B: Black W: White
-7-
4
Program Explanation Manual “kit05.c”
2.5. Motor Control CPU Board Drive syste m B attery Box
Control syste m B attery Box PortB
Port7 PortA
LED 0 LED 1 SW
Servo
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.
CPU board
M
×
CPU board
The motor does not turn because the signal is weak.
Motor Drive Board
M
○
Convert to large current, motor is 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 OFF
Motor
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.
1 cycle 5V 0V 5V 0V 5V 0V 5V 0V 5V 0V
Voltage conversion
Duty ratio 25%
=
1.25V
50%
=
2.5V
75%
=
3.75V
100% =
5V
0%
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 Normal rotation Reverse Brake
1
Terminal 1 GND connection +connection GND connection
2
M
1
Normal rotation 正転
M
2
Reverse 逆転
Terminal 2 +connection GND connection GND connection
1
M
2
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
M Normal rotation 正転
10V
10V
M Reverse 逆転
10V
10V
M 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. Normal rotation
Reverse
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 0V 0V
B 0V 10V
10V
0V
0V
0V
Operation Brake Reverse Normal rotation 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. 実際の波形 Actual waveform
理想的な波形 Ideal waveform
Normal
Normal
モータ Motor
Brake ブレーキ
rotation 正転
Brake ブレーキ
モータ Motor Gate ゲート
Gate ゲート
200ns PchFET Operation 動作
NchFET 動作 Operation
Brake ブレーキ
rotation Brake ブレーキ 正転
ON OFF
Operation 動作
ON OFF
動作 Operation
87ns
PchFET 120ns
225ns
ON OFF ON OFF
NchFET
ショート 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
2
C (PchIN) D (NchIN)
Brake
Normal rotation
3
1
10V 0V 4
10V 0V
PchFET Operation
ON OFF
NchFET Operation
ON 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]
Input (1 pin of U2)
50us 3 pin of U2 3.5V
- 13 -
74HC series is considered to be “1" 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" figure, though there is a gap/lapse of only 225ns, delay time of 50us is created in the integrating circuit. 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.
Program Explanation Manual “kit05.c” The circuit which combines integrating circuit to FET is as follows. E A C B D
Integrating Circuit
F
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 Motor
Brake
A,B,....E,F.
Normal rotation
Brake
A
"1"(5V) "0" 50us
Integrating Circuit
Voltage Conversion
50us
B
"1"(5V) "0"
C =AandB
"1"(5V) "0"
D =AorB
"1"(5V) "0" 10V
E (PchIN)
0V 10V
F (NchIN)
0V
PchFET 動作
ON OFF
NchFET 動作
ON OFF
FET
Motor
出力 電圧
0V
10V Free Free Both are not turned ON.
- 14 -
0V
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 A Integrating circuit B
“0": Brake “1": Operate (To the direction of PB2)
C “0": normal rotation “1": reverse rotation
Digital relay
Gate of FET1
Gate of FET2
Gate of FET3
Gate of FET4
CN4 2 pin
CN4 1 pin
Motor operation
0
10V (OFF)
10V (ON)
10V (OFF)
10V (ON)
0V
0V
Brake
0
1
10V (OFF)
0V (OFF)
10V (OFF)
10V (ON)
Free (Open)
0V
Free
1
1
0V (ON)
0V (OFF)
10V (OFF)
10V (ON)
10V
0V
Normal Rotation
0
1
10V (OFF)
0V (OFF)
10V (OFF)
10V (ON)
Free (Open)
0V
Free
0
0
10V (OFF)
10V (ON)
10V (OFF)
10V (ON)
0V
0V
Brake
A
B
Gate of FET1
Gate of FET2
Gate of FET3
Gate of FET4
CN4 2 pin
CN4 1 pin
Motor operation
0
0
10V (OFF)
10V (ON)
10V (OFF)
10V (ON)
0V
0V
Brake
0
1
10V (OFF)
10V (ON)
10V (OFF)
0V (OFF)
0V
Free (Open)
Free
1
1
10V (OFF)
10V (ON)
0V (ON)
0V (OFF)
0V
10V
Reverse
0
1
10V (OFF)
10V (ON)
10V (OFF)
0V (OFF)
0V
Free (Open)
Free
0
0
10V (OFF)
10V (ON)
10V (OFF)
10V (ON)
0V
0V
Brake
A
B
0
C
0
C
1
Ú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
Left 90 左90度
90 右Right 90度
0.7m s
1.5m s
16m s
0
2.3m 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]
OFF
ON! No current flow!! 5V
0V
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
"0"
"1"
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. resistor. B.
- 19 -
Program Explanation Manual “kit05.c”
3. Program Explanation “kit05.c” 3.1. Program list 1 2 3 4 5 6 7 8 9 10
: : : : : : : : : :
/****************************************************************************/ /* Micom Car Rally Trace Program 2005 Version */ /* 2005.04 Micom Car Rally Executive Committee */ /****************************************************************************/ /* This kit05.c program connects to motor drive board (Vol.3). The following contents have been changed with respect to kit04.c. - The port connected to CPU board has been changed from J2 (Port A) to J3 (Port B). - Motor mode function has been removed (scrapped) (always brake) - 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
speed( 0, 0 ); pattern = 0; cnt1 = 0; while( 1 ) { switch( pattern ) { /***************************************************************** About pattern 0: Switch input wait 1: Wait for 1 second after the switch is pressed. 11: Usual trace 12: Check for the end of long turn to the right 13: Check for the end of long turn to the left 21: Processing when first crossline is detected 22: Skip second crossline 23: Trace 1 after crossline 24: Trace 2 after crossline, crank detection 31: Left crank Clear process Waits till it stabilizes 32: Left crank clear process Check for end of turn 41: Right crank clear process Waits till it stabilizes 42: Right crank clear process Check for end of turn *****************************************************************/ case 0: /* Switch input waiting */ if( pushsw_get() ) { pattern = 1; cnt1 = 0; break; } if( cnt1 < 100 ) { led_out( 0x1 ); } else if( cnt1 < 200 ) { led_out( 0x2 ); } else { cnt1 = 0; } break;
/* LED blinking process */
case 1: /* 1 second waiting after the switch is pushed */ if( cnt1 < 500 ) { /* 1.0 seconds starting ago LED1:"OFF" , LED0:"ON" */ led_out( 0x1 ); } else if( cnt1 < 1000 ) { /* 0.5 seconds starting ago LED1:"ON" , LED0:"OFF" */ led_out( 0x2 ); } else { /* Start!! */ led_out( 0x0 ); pattern = 11; cnt1 = 0; } break; case 11: /* Usual trace */ if( check_crossline() ) { /* Crossline Check */ pattern = 21; break; } switch( sensor_inp(MASK3_3) ) { case 0x00: /* Center -> Straight */ handle( 0 ); speed( 100 ,100 ); break; case 0x04: /* Slightly left inclined -> Slight turn to the right */ handle( 5 ); speed( 100 ,100 ); break; case 0x06: /* Little left inclined -> Small turn to the right */ handle( 10 ); speed( 80 ,69 ); break; case 0x07: /* Left inclined from the middle -> Middle turn to the right */ handle( 15 ); speed( 50 ,40 ); break; case 0x03: /* Large inclined to the left -> Large turn to the right */ handle( 25 ); speed( 30 ,21 );
- 21 -
Program Explanation Manual “kit05.c” 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
pattern = 12; break; case 0x20: /* Slightly inclined to the right -> Slight turn to the left */ handle( -5 ); speed( 100 ,100 ); break; case 0x60: /* A little inclined to the right -> Small turn to the left */ handle( -10 ); speed( 69 ,80 ); break; case 0xe0: /* Right inclined from the middle veering -> Middle turn to the left */ handle( -15 ); speed( 40 ,50 ); break; case 0xc0: /* Large inclined to the right -> Large turn to the left */ handle( -25 ); speed( 21 ,30 ); pattern = 13; break; default: break;
} break;
case 12: /* Check of large turning to the right completion */ if( check_crossline() ) { /* Crossline check even during turning */ pattern = 21; break; } if( sensor_inp(MASK3_3) == 0x06 ) { pattern = 11; } break; case 13: /* Check of large turning completion to the left */ if( check_crossline() ) { /* Crossline check even during large turn */ pattern = 21; break; } if( sensor_inp(MASK3_3) == 0x60 ) { pattern = 11; } break; case 21: /* Process when first crossline is detected */ led_out( 0x3 ); handle( 0 ); speed( 0 ,0 ); pattern = 22; cnt1 = 0; break; case 22: /* Second is skipped. */ if( cnt1 > 100 ) { cnt1 = 0; pattern = 23; } break; case 23: /* 1 of trace after crossline */ if( cnt1 > 300 ) { cnt1 = 0; pattern = 24; break; } switch( sensor_inp(MASK3_3) ) { case 0x00: /* Center -> Straight */ handle( 0 ); speed( 40 ,40 ); break; case 0x04: case 0x06: case 0x07: case 0x03: /* Tending to the left -> Turn to the right */ handle( 8 );
- 22 -
Program Explanation Manual “kit05.c” 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
speed( 40 ,36 ); break; case 0x20: case 0x60: case 0xe0: case 0xc0: /* Tending to the right -> Turn to the left */ handle( -8 ); speed( 36 ,40 ); break; default: break;
} break;
case 24: /* 2 of trace after crossline and crank detection */ switch( sensor_inp(MASK3_3) ) { case 0xe0: /* Judgment of left crank -> To the left crank clearing process */ led_out( 0x1 ); handle( -38 ); speed( 10 ,50 ); pattern = 31; cnt1 = 0; break; case 0x07: /* Judgment of right crank -> The process for getting over right crank */ led_out( 0x2 ); handle( 38 ); speed( 50 ,10 ); pattern = 41; cnt1 = 0; break; case 0x00: /* Center -> Straight */ handle( 0 ); speed( 40 ,40 ); break; case 0x04: case 0x06: case 0x03: /* Tending to the left -> Turn to the right */ handle( 8 ); speed( 40 ,36 ); break; case 0x20: case 0x60: case 0xc0: /* Tending to the right -> Turn to the left */ handle( -8 ); speed( 36 ,40 ); break; default: break; } break; case 31: /* Left crank clear process Wait a little till it becomes stable. */ if( cnt1 > 200 ) { pattern = 32; cnt1 = 0; } break; case 32: /* Left crank clear process Check of end of turn */ if( sensor_inp(MASK3_3) == 0x60 ) { led_out( 0x0 ); pattern = 11; cnt1 = 0; } break; case 41: /* Right crank clear processing Waits a little until stabilizing */ if( cnt1 > 200 ) { pattern = 42; cnt1 = 0; } break; case 42: /* Right crank clear processing Turning completion check */ if( sensor_inp(MASK3_3) == 0x06 ) { led_out( 0x0 ); pattern = 11; cnt1 = 0; } break;
- 23 -
Program Explanation Manual “kit05.c” 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
default: /* When the pattern is not applied to any case, return to waiting state */ pattern = 0; break; } } } /************************************************************************/ /* H8/3048F-ONE Built in Peripheral Function Initialization */ /************************************************************************/ void init( void ) { /* I/O port Setting */ P1DDR = 0xff; P2DDR = 0xff; P3DDR = 0xff; P4DDR = 0xff; P5DDR = 0xff; P6DDR = 0xf0; /* DIP SW on CPU board */ P8DDR = 0xff; P9DDR = 0xf7; /* Communication Port */ PADDR = 0xff; PBDR = 0xc0; PBDDR = 0xfe; /* Motor Drive Board Vol.3 */ /* As P7 of the sensor board is an exclusive input, there are no input output settings. */ /* ITU0 Interrupt at every 1ms */ ITU0_TCR = 0x23; ITU0_GRA = TIMER_CYCLE; ITU0_IER = 0x01; /* ITU3, ITU3_TCR ITU_FCR ITU3_GRA ITU3_GRB ITU4_GRA ITU4_GRB ITU_TOER
4 = = = = = = =
reset-synchronized PWM mode 0x23; 0x3e; PWM_CYCLE; ITU3_BRB = 0; ITU4_BRA = 0; ITU4_BRB = SERVO_CENTER; 0x38;
for right-left motor and servo */ /* /* /* /*
Setting of cycle */ PWM Setting of left motor*/ PWM Setting of right motor*/ PWM Setting of servo */
/* Count start of ITU */ ITU_STR = 0x09; } /************************************************************************/ /* ITU0 Interrupt process */ /************************************************************************/ #pragma interrupt( interrupt_timer0 ) void interrupt_timer0( void ) { ITU0_TSR &= 0xfe; /* Flag clear */ cnt0++; cnt1++; } /************************************************************************/ /* Timer main unit */ /* Argument Timer value 1=1ms */ /************************************************************************/ void timer( unsigned long timer_set ) { cnt0 = 0; while( cnt0 < timer_set ); } /************************************************************************/ /* Sensor state detection */ /* Argument Mask value */ /* Return value Sensor value */ /************************************************************************/ unsigned char sensor_inp( unsigned char mask ) { unsigned char sensor; sensor = P7DR; /* Since bit0 is for left and bit7 is for right in the new sensor board which is reversed */ /* to the previous sensor board, the bit has been replaced to maintain compatibility. */ sensor = bit_change( sensor ); /* Bit replacement */ sensor &= mask; }
return sensor;
/************************************************************************/ /* Crossline detection processing */ /* Return value 0: no crossline 1: crossline exists */ /************************************************************************/ int check_crossline( void )
- 24 -
Program Explanation Manual “kit05.c” 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
{ unsigned char b; int ret; ret = 0; b = sensor_inp(MASK2_2); if( b==0x66 || b==0x64 || b==0x26 || b==0x62 || b==0x46 ) { ret = 1; } return ret; } /************************************************************************/ /* Reading of DIP switch value */ /* Return value Switch value 0 - 15 */ /************************************************************************/ unsigned char dipsw_get( void ) { unsigned char sw; sw = ~P6DR; sw &= 0x0f; }
/* Reading of DIP switch */
return sw;
/************************************************************************/ /* Push switch value reading */ /* Return value Switch value ON: "1" and OFF: "0" */ /************************************************************************/ unsigned char pushsw_get( void ) { unsigned char sw; sw = ~PBDR; sw &= 0x01;
/* Reading of port having a push switch */
return sw; } /************************************************************************/ /* LED control */ /* Argument Switch value (LED1,LED0)=(bit1,bit0) "0":OFF , "1":ON */ /* Example 0x3->(LED1,LED0)=(ON,ON) : 0x2->(LED1,LED0)=(ON,OFF) */ /************************************************************************/ void led_out( unsigned char led ) { unsigned char data; led = ~led; led <<= 6; data = PBDR & 0x3f; PBDR = data | led; } /************************************************************************/ /* Speed Control */ /* Argument Left motor: -100 - 100 , Right motor: -100 - 100 */ /* 0:Stop,100:normal rotation 100%,-100:Reverse 100% */ /************************************************************************/ void speed( int accele_l, int accele_r ) { unsigned char sw_data; unsigned long speed_max; sw_data = dipsw_get() + 5; /* DIP switch read */ speed_max = (unsigned long)(PWM_CYCLE-1) * sw_data / 20; /* Left motor */ if( accele_l >= 0 ) { PBDR &= 0xfb; ITU3_BRB = speed_max * accele_l / 100; } else { PBDR |= 0x04; accele_l = -accele_l; ITU3_BRB = speed_max * accele_l / 100; }
}
/* Right motor */ if( accele_r >= 0 ) { PBDR &= 0xf7; ITU4_BRA = speed_max * accele_r / 100; } else { PBDR |= 0x08; accele_r = -accele_r; ITU4_BRA = speed_max * accele_r / 100; }
/************************************************************************/ /* Servo steering operation */
- 25 -
Program Explanation Manual “kit05.c” 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
: : : : : : : : : : : : : : : : : : : : : : : : : : : : :
/* Argument Servo operation angle: -90 ~ 90 */ /* -90:90 to the left turn , 0:straight ,90:90 to the right turn */ /************************************************************************/ void handle( int angle ) { ITU4_BRB = SERVO_CENTER - angle * HANDLE_STEP; } /************************************************************************/ /* Bit change */ /* Argument Value to be changed */ /* RReturn value Changed value */ /************************************************************************/ char unsigned bit_change( char unsigned in ) { unsigned char ret; int i; for( i=0; i<8; i++ ) { ret >>= 1; ret |= in & 0x80; in <<= 1; } return ret;
}
/* Right shift of return value */ /* Ret bit7 = in bit7 */ /* Left shift of argument */
/************************************************************************/ /* end of file */ /************************************************************************/
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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
: /**************************************************************************************/ :/*Definition of I/O register of built-in peripheral function for H8/3048F-ONE Ver1.02 */ : /* 2005.04 Micom car Rally Executive Committee */ : /**************************************************************************************/ : : #define P1DDR (*(unsigned char*)0xfffc0) : #define P1DR (*(unsigned char*)0xfffc2) : #define P2DDR (*(unsigned char*)0xfffc1) : #define P2DR (*(unsigned char*)0xfffc3) : #define P3DDR (*(unsigned char*)0xfffc4) : #define P3DR (*(unsigned char*)0xfffc6) : #define P4DDR (*(unsigned char*)0xfffc5) : #define P4DR (*(unsigned char*)0xfffc7) : #define P5DDR (*(unsigned char*)0xfffc8) : #define P5DR (*(unsigned char*)0xfffca) : #define P6DDR (*(unsigned char*)0xfffc9) : #define P6DR (*(unsigned char*)0xfffcb) : #define P7DR (*(unsigned char*)0xfffce) : #define P8DDR (*(unsigned char*)0xfffcd) : #define P8DR (*(unsigned char*)0xfffcf) : #define P9DDR (*(unsigned char*)0xfffd0) : #define P9DR (*(unsigned char*)0xfffd2) : #define PADDR (*(unsigned char*)0xfffd1) : #define PADR (*(unsigned char*)0xfffd3) : #define PBDDR (*(unsigned char*)0xfffd4) : #define PBDR (*(unsigned char*)0xfffd6) : #define P2PCR (*(unsigned char*)0xfffd8) : #define P4PCR (*(unsigned char*)0xfffda) : #define P5PCR (*(unsigned char*)0xfffdb) : : #define ITU_STR (*(unsigned char*)0xfff60) : #define ITU_SNC (*(unsigned char*)0xfff61) : #define ITU_MDR (*(unsigned char*)0xfff62) : #define ITU_FCR (*(unsigned char*)0xfff63) : #define ITU_TOER (*(unsigned char*)0xfff90) : #define ITU_TOCR (*(unsigned char*)0xfff91) : : #define ITU0_CNT (*(unsigned int *)0xfff68) : #define ITU0_TCR (*(unsigned char*)0xfff64) : #define ITU0_GRA (*(unsigned int *)0xfff6a) : #define ITU0_GRB (*(unsigned int *)0xfff6c) : #define ITU0_IER (*(unsigned char*)0xfff66) : #define ITU0_TSR (*(unsigned char*)0xfff67) : #define ITU0_TIOR (*(unsigned char*)0xfff65) : : #define ITU1_CNT (*(unsigned int *)0xfff72) : #define ITU1_TCR (*(unsigned char*)0xfff6e) : #define ITU1_GRA (*(unsigned int *)0xfff74) : #define ITU1_GRB (*(unsigned int *)0xfff76) : #define ITU1_IER (*(unsigned char*)0xfff70) : #define ITU1_TSR (*(unsigned char*)0xfff71) : #define ITU1_TIOR (*(unsigned char*)0xfff6f) : : #define ITU2_CNT (*(unsigned int *)0xfff7c) : #define ITU2_TCR (*(unsigned char*)0xfff78) : #define ITU2_GRA (*(unsigned int *)0xfff7e) : #define ITU2_GRB (*(unsigned int *)0xfff80) : #define ITU2_IER (*(unsigned char*)0xfff7a) : #define ITU2_TSR (*(unsigned char*)0xfff7b) : #define ITU2_TIOR (*(unsigned char*)0xfff79) : : #define ITU3_CNT (*(unsigned int *)0xfff86) : #define ITU3_TCR (*(unsigned char*)0xfff82) : #define ITU3_GRA (*(unsigned int *)0xfff88) : #define ITU3_GRB (*(unsigned int *)0xfff8a) : #define ITU3_BRA (*(unsigned int *)0xfff8c) : #define ITU3_BRB (*(unsigned int *)0xfff8e) : #define ITU3_IER (*(unsigned char*)0xfff84) : #define ITU3_TSR (*(unsigned char*)0xfff85) : #define ITU3_TIOR (*(unsigned char*)0xfff83)
71 :
- 27 -
Program Explanation Manual “kit05.c” 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
#define #define #define #define #define #define #define #define #define
ITU4_CNT ITU4_TCR ITU4_GRA ITU4_GRB ITU4_BRA ITU4_BRB ITU4_IER ITU4_TSR ITU4_TIOR
(*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned
int *)0xfff96) char*)0xfff92) int *)0xfff98) int *)0xfff9a) int *)0xfff9c) int *)0xfff9e) char*)0xfff94) char*)0xfff95) char*)0xfff93)
#define #define #define #define #define #define #define
SCI0_SMR SCI0_BRR SCI0_SCR SCI0_TDR SCI0_SSR SCI0_RDR SCI0_SCMR
(*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned
char*)0xfffb0) char*)0xfffb1) char*)0xfffb2) char*)0xfffb3) char*)0xfffb4) char*)0xfffb5) char*)0xfffb6)
#define #define #define #define #define #define
SCI1_SMR SCI1_BRR SCI1_SCR SCI1_TDR SCI1_SSR SCI1_RDR
(*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned
char*)0xfffb8) char*)0xfffb9) char*)0xfffba) char*)0xfffbb) char*)0xfffbc) char*)0xfffbd)
#define #define #define #define #define #define
AD_DRA AD_DRB AD_DRC AD_DRD AD_CSR AD_CR
(*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned (*(unsigned
int *)0xfffe0) int *)0xfffe2) int *)0xfffe4) int *)0xfffe6) char*)0xfffe8) 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. 1ffff
0
fef10
Flash ROM (128kB)
Memory
fff0f
fff1c
fffff
RAM (4kB)
I/O 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 ・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 from the terminal of port A.
…
0
…
fffd3
1
0
0
1
fffff
0
1
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.
#define PADR
(unsigned char*)0xfffd3
PADR is 8-bit width 0xfffd3 block number without code (unsigned char).
#define PADR
*(unsigned char*)0xfffd3
PADR is value of 8-bit width 0xfffd3 block number without 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
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]. 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
PWM cycle is added to Right motor, Left motor and servo. The PWM cycle is of 16[ms]. And becomes (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.
HANDLE_STEP
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 (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.
MASK2_2
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. 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”.
MASK2_0
As “Mask2_0” is defined as “0x60”, bit6, 5, are maintained as they are in “0110 0000” binary numbers and the others are forcibly set to “0”.
MASK0_2
As “Mask0_2” is defined as “0x06”, bit2, 1 are maintained as they are in “0000 0110” binary numbers and the others are forcibly set to “0”.
MASK3_3
As “Mask3_3” is defined as “0xe7”, bit7, 6, 5, 2, 1, 0 are maintained as they are in “1110 0111” binary numbers and the others are forcibly set to “0”.
MASK0_3
As “Mask0_3” is defined as “0x07”, bit2, 1, 0, are maintained as they are in “0000 1110” binary numbers and the others are forcibly set to “0”.
MASK3_0
As “Mask3_0” is defined as “0xe0”, bit7, 6, 5, are maintained as they are in “1110 0000” binary numbers and the others are forcibly set to “0”.
MASK4_0
As “Mask4_0” is defined as “0xf0”, bit7, 6, 5, 4 are maintained as they are in “1111 0000” binary numbers and the others are forcibly set to “0”.
MASK0_4
As “Mask0_4” is defined as “0x0f”, bit3, 2, 1, 0 are maintained as they are in “0000 1111” binary numbers and the others are forcibly set to “0”.
MASK1_1
As “Mask1_1” is defined as “0x81”, bit7, 0 are maintained as they are in “1000 0001” binary 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 main( void ) { int i; i = 10; test( i ); }
Compiled though there is an error
void test( int *a ) { printf(“%d¥n”,*a ); }
void test( int *a ); <- Prototype Declaration void main( void ) { int i; Compile i = 10; test( i ); error! } void test( int *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 ); a(); printf(“%d¥n”,timer ); printf(“%d¥n”,i );
<- 0 is displayed <-Value 20 set in the function a is displayed as timer is a global variable <-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
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
V cc
P83
P81
P66
P64
*RES
P53
P51
P27
P25
P23
P21
P17
P15
P13
P11
P37
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 3 4 5 6
8 8 8 4 7
8 9 A B
5 6 8 8
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 -
Ex, P84 indicate bit 4 of the port 8 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
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: PBDDR:
7
6
5
4
3
2
1
0
Input-Output Setting of port bit 7
Input-Output Setting of port bit 6
Input-Output Setting of port bit 5
Input-Output Setting of port bit 4
Input-Output Setting of port bit 3
Input-Output Setting of port bit 2
Input-Output Setting of port bit 1
Input-Output Setting of port bit 0
Motor drive board is connected to port B. Confirm the connective conditions for each bit. Pin Signal, Details “0” “1” Input-output No. Direction 1 + 5V − 2 Board <-PB7 LED1 ON OFF Output 3 Board<-PB6 LED0 ON OFF Output 4 Board<-PB5 Servo Signal PWM Signal Output 5 Board<-PB4 Right Motor PWM Stop PWM Motion Output Right Motor Rotational Normal Reverse 6 Board<-PB3 Output Direction Rotation Rotation Left Motor Rotational Normal Reverse 7 Board<-PB2 Output Direction Rotation Rotation 8 Board<-PB1 Left Motor PWM Stop PWM Motion Output 9 Board->PB0 Push Button Pushed Not Pushed Input 10 GND −
1
7
1
6
1
5
1
4
1
3
1
2
1
1
0
0
R D D B P 1
7
1
6
1
5
1
4
1
3
1
2
1
1
0
0
DDR Value 1 1 1 1 1 1 1 0
Bit 0 is input setting and others are output setting. As input is set to ‘0’ and output is set to ’1’ thus PBDDR = 1111 1110 = 0xfe.
Earlier it was explained that address was assigned to the I/O register and block number is top address of the I/Oregister. As PBDDR has block number 0xfffd4, 0xfffd4 directly relates to the part that decides the input-output of port B and input-output direction is decided by the value written in the block number. - 36 -
4 d f f f x 0
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
PBDR:
Output data setting to bit 7 /Input data read from bit 7
Output data setting to bit 6 /Input data read from bit 6
Output data setting to bit 5 /Input data read from bit 5
Output data setting to bit 4 /Input data read from bit 4
Output data setting to bit 3 /Input data read from bit 3
Output data setting to bit 2 /Input data read from bit 2
Output data setting to bit 1 /Input data read from bit 1
Output data setting to bit 0 /Input data read 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
External Voltage
2.0V 0.8V 0V
Input voltage range recognized as“1”
“1”
Input voltage range recognized as“0”
“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). “1”
Microcomputer “0”
5.0V
External
Voltage
3.5V
Output “1”
0.4V 0V
Output “0”
Voltage
- 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 “1”
5.0V
5.0V 4.9V
“1”
“1”
5.0V “1”
3.5V
2.7V 2.0V 1.5 V “0”
0.4V Outpu 0V
0.8 V 0V
“0”
Input
“0”
0.1V Outpu 0V
TTL Levels
“0”
Input
0V
CMOS Levels
About output currents The I/O permissible output current of H8/3048F-ONE is as follows. Item Permissible output low level current
(Per pin)
Permissible output low level current
(total)
Permissible output high level current
(Per pin)
Ports 1, 2, 5, and B Other output pins Total of 28pins in ports 1, 2, 5 and B Total of all output pins , including the above All output pins
Permissible output high level current
(total)
Total of all output pins
Conditoions:
Permissible Current 10mA 2.0mA 80mA 120mA 2.0mA 40mA
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 Number 1 2 3 4 5
Signal, Direction − Board <-PB7 Board<-PB6 Board<-PB5 Board<-PB4
6
Board<-PB3
7
Board<-PB2
8 9 10
Board<-PB1 Board->PB0 −
1
7
5V
1
6
5V
0
5
0V
0
4
0V
0
3
0V
0
2
0V
Details + 5V LED1 LED0 Servo signal Right Motor PWM Right Motor Rotational Direction Left Motor Rotational Direction Left Motor PWM Push Button Switch GND
“0”
“1”
DR Value
ON
OFF OFF
1 1 0 0
ON
PWM Signal Stop PWM Motion Normal Reverse Rotation Rotation Normal Reverse Rotation Rotation Stop PWM Motion Pushed Not Pushed
0 0 0 Both are acceptable.
To turn off LED, “1” is output first. To stop the motor, “0” is output and the temporary “0” is put in the servo as both are acceptable. Since PB0 is for input, even if write anything, the value does not change. However, “0” is written as there may be a doubt that something happens if “1” is written. PBDR = 1100 0000 = 0xc0.
R D B P
In the program 0xc0 is set in PBDR first and then 0xfe is set in PBDDR. Why should we 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 0
1
0
0
0V
. - 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 Direction
Present output value
1
7
1
0
6
0
1
5
1
1
4
1
0
3
0
0
2
0
1
1
1
1
0
Input value
1
R D B P
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
P1DDR P2DDR P3DDR P4DDR P5DDR
Setup value 0xff 0xff 0xff 0xff 0xff
P6DDR
0xf0
Port
P7 P8DDR
0xff
P9DDR
0xf7
PADDR PBDDR
0xff 0xfe
No connection No connection No connection No connection No connection Bit 3 ~ 0 are DIPswitch inputs on the CPU board and others are not connected. Sensor board is connected. There is no P7DDR as port 7 is an exclusive input port. No connection Bit 3 is communication data output and bit 1 is communication data input, others are not connected. No connection 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?
Start Switch Check Process Processing Time 10us Process1 Processing Time 1us
Repeats at High speed
The program performs various processes. There is no problem if 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 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, and each process is carried out properly. Terefore, in the normal process, time of process 1 and 2 are the impeccable time.
Process 2 Processing Time 1us
Start Switch Check Process Processing Time 10us Process1 Processing Time 100ms Process 2 Processing Time 200ms
Repeats with Low speed
Then, what should be done if processing takes time? 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 300[ms]. And so, the swich may be turned off during 300[ms], namely there may be a possibility of omission in detection of switch at the end of 300 [ms]. If this is a sensor check of micom car, it is omitted by detecting omission. It is serious problem. Actually, though the process that takes time of 100 [ms] and 200 [ms] is problem, it has been explained here as an example.
The “Interrupt” appears here for the first time to solve the Start problem above.When program When switch is started, it is set as “if the pressed during Process1 Process switch pin changes from “1” to process 1 or 2 Processing Time 100ms “0”; interruption is generated” (Actually, it is necessary to set Process2 Interrupt 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. Interrupt Program Start
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 : 385 : 386 : 387 : Omitted 400 :
/* ITU0 Interrupt for every 1 ms */ ITU0_TCR = 0x23; ITU0_GRA = TIMER_CYCLE; ITU0_IER = 0x01; ITU_STR = 0x09;
ITU0 Register Meaning of the registers that will be used hereafter Register
Name
Meaning
ITU0_CNT
Timer counter
ITU0_TCR
Timer control register
It acts like a second hand of the clock. The counter value is incremented by one after a fixed interval. This becomes a standard value. Decides the timing when ITU0_CNT is made 0 or incremented by 1.
ITU0_GRA
General Purpose register A
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 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.
ITU0_GRB
General Purpose register B
It is the second register named as B which is similar to the register A.
ITU0_IER
Timer Interrupt Register
Interrupt means a break in the line (disturb) and enable means to activate the operation. It is a register that decides whether to enable operation or not. -> permit interrupt or not.
ITU_STR
Timer Start Register
ITU0_CNT stops first. This register starts ITU0_CNT. It acts like a switch.
ITU0_TSR
Timer Status Register
Though status means the position of human being in society it means a state or condition in Computers. Using this it is possible to get the state of the ITU0.
Enable
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: ITU0_TCR: Setup value: Hexadecimal :
7
6
5
4
3
2
1
0
−
CCLR1
CCLR0
CKEG1
CKEG0
TPSC2
TPSC1
TPSC0
0
0
1
0
0
0
1
1
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
Crystal
This setting sets the time for incrementing ITU0_CNT. Internal clock φ is the crystal value. Crystal is the standard for all 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
0
0
0
Internal Clock: Count using φ Because it is reciprocal when counting a timing with 24.576MHz, a period of one count = 1 / (24.576 106) = 40.69 ns Maximum value is 40.69 65535=2.666ms.
0
0
1
Internal Clock: Count using φ/2 a period of one count = 1 / (24.576 106/2) = 81.38[ns] Maximum value is 81.38[ns] 65535 = 5.333[ms]
0
1
0
Internal Clock: Count using φ/4 a period of one count = 1 / (24.576 106/4) = 162.76[ns] Maximum value is 162.76[ns] 65535 = 10.67[ms]
0
1
1
Internal Clock: Count using φ/8 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: ITU0_IER: Setup value: Hexadecimal:
Bit2:
7
6
5
4
3
2
1
0
−
−
−
−
−
OVIE
IMIEB
IMIEA
0
0
0
0
0
0
0
1
0
1
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: ITU0_TSR:
7
6
5
4
3
2
1
0
−
−
−
−
−
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] 0 Read IMFB when IMFB = 1, then write 0 in IMFB 1
( Initial value)
[Setting condition] (1) CNT=GRB when GRB functions as an output compare register. (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] 0 Read IMFA when IMFA = 1, then write 0 in IMFA 1
( Initial value)
[Setting condition] (1) CNT=GRA when GRA functions as an output compare register. (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
ITU_STR:
−
−
−
Start counting ITU4_CNT
Start counting ITU3_CNT
Start counting ITU2_CNT
Start counting ITU1_CNT
Start counting ITU0_CNT
Setup value: Value to be ORed in program:
0
0
0
0
1
0
0
1
0
Bit4-0: Counter State 4 ~ 0 Selects ITUx_CNT counter start/stop. STR4-0 0 Stops counting CNT of ITUx. 1 Starts counting CNT of ITUx.
9
Explanation
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
GRA+1 (3072)
CNT value
1ms till becoming 3072
When becoming 3072, it is cleared to 0.
GRA+1 (3072)
0
CNT value
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 alarm. (1 10-3) / (325.52 10-9)=3072 Therefore, 3071 of 1 small value is set in the ITU0_GRA.
The ordinary clock goes through a rotation, but on the way through a rotation, ITU can forcefully adjust the second hand to 0. Since it is set as "CNT is cleared in compare match/ input capture of GRA “ by ITU0_TCR, if it becomes ITU0_CNT=(ITU0_GRA + 1) then 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 each 1[ms].
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" Bell
Battery is put in the alarm (Clock is stared) ITU STR = 0x01
In order to ring the alarm bell the Bit 0 of ITU0_IER is set to "1", and an interrupt is generated. When the bell rings,the microcomputer shifts to the interrupt process.
Setting completed! But, actually, there is no battery in the clock. The clock in which battery is put is moved. This is the setting 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 generation
Jump to the read address
Reads value, written in adress 0x0060
The summarized table for interrupt types and vector address is displayed below.
Interrupt source Reset
Generating origin
Vector no.
External pin
0
- 49 -
Vector address H'0000
~ H'0003
IPR −−−
Priority
Program Explanation Manual “kit05.c” NMI IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5
External pins
Reserve
−−−−−−−−−
WOVI (Interval timer) CMI (Compare match)
Watch dog timer Refresh controller −−−−−−−−−
Reserve IMIA0 (Compare match/input capture A0) IMIB0 (Compare match/input capture B0) OVI0 (Overflow 0) Reserve IMIA1 (Compare match/input capture A1) IMIB1 (Compare match/input capture B1) OVI1 (Overflow 1) Reserve IMIA2 (Compare match/input capture A2) IMIB2 (Compare match/input capture B2) OVI2 (Overflow 2) Reserve IMIA3 (Compare match/input A3) IMIB3 (Compare match/input B3) OVI3 (Overflow 3) Reserve IMIA4 (Compare match/input A4) IMIB4 (Compare match/input B4) OVI4 (Overflow 4) Reserve DEDN0A DEDN0B DEDN1A DEDN1B
Reserve
ERI0 (Recieve error 0) RXI0 (Receive data full 0)
~ H'001F ~ H'0033 ~ H'0037 ~ H’003B ~ H’003F ~ H’0043 ~ H’0047 ~ H’004B ~ H’004F ~ H’0053 ~ H’0057 ~ H’005B ~ H’005F
24
H'0060
~ H’0063
25
H'0064
~ H’0067
−−−−−−−−−
26 27
H'0068 ~ H’006B H'006C ~ H’006F
28
H'0070
~ H’0073
ITU Channel 1
29
H'0074
~ H’0077
−−−−−−−−−
30 31
H'0078 ~ H’007B H'007C ~ H’007F
32
H'0080
~ H’0083
33
H'0084
~ H’0087
34
H'0088
~ H’008B
35
H'008C
~ H’008F
36
H'0090
~ H’0093
ITU Channel 3
37
H'0094
~ H’0097
−−−−−−−−−
38 39
H'0098 ~ H’009B H'009C ~ H’009F
40
H'00A0
~ H’00A3
41
H'00A4
~ H’00A7
42 43 44 45 46 47
H'00A8 H'00AC H'00B0 H'00B4 H'00B8 H'00BC
~ H’00AB ~ H’00AF ~ H’00B3 ~ H'00B7 ~ H'00BB ~ H'00BF
−−−−−−−−−−
48 49 50 51
H'00C0 ~ H'00C3 H'00C4 ~ H'00C7 H'00C8 ~ H'00CB H'00CC ~ H'00CF
SCI Channel 0
52 53
H'00D0 H'00D4
ITU Channel 2 −−−−−−−−−
capture capture
H'001C H'0030 H'0034 H'0038 H'003C H'0040 H'0044 H'0048 H'004C H'0050 H'0054 H'0058 H'005C
ITU Channel 0
capture capture
7 12 13 14 15 16 17 18 19 20 21 22 23
ITU Channel 4 −−−−−−−−− DMAC Group 0
- 50 -
~ H'00D3 ~ H'00D7
−−− IPRA7 IPRA6 IPRA5
IPRA4
IPRA3
IPRA2
IPRA1
IPRA0
IPRB7
IPRB6
IPRB5
−−−− IPRB3
High ↑
Program Explanation Manual “kit05.c” TXI0 (Tansmit data empty 0 ) TEI0 (Transmit end 0) ERI1 (Recieve error 1) RXI1 (Receive data full 1) TXI1 (Tansmit data empty 1) TEI1 (Transmit end 1) ADI (A/D end)
54 55 56 57 58 59 60
SCI Channel 1 A/D
H'00D8 H'00DC H'00E0 H'00E4 H'00E8 H'00EC H'00F0
~ H'00DB ~ H'00DF ~ H'00E3 ~ H'00E7 ~ H'00EB ~ H'00EF ~ H'00F3
IPRB2 ↓ 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 kit05start.src
Main function execution 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: 9: 10 : 11 : (Omitted) 31 : 32 : 33 : 34 : 35 : (Omitted) 68 : 69 :
.SECTION V .DATA.L RESET_START .DATA.L RESERVE .DATA.L RESERVE
; Beginning of vector section ; 0 h'000000 Reset ; 1 h'000004 System reservation ; 2 h'000008 System reservation
.DATA.L RESERVE .DATA.L RESERVE .DATA.L _interrupt_timer0 .DATA.L RESERVE .DATA.L RESERVE
; 22 h'000058 ; 23 h'00005c ; 24 h'000060 ; 25 h'000064 ; 26 h'000068
System reservation System reservation ITU0 IMIA0 ITU0 IMIB0 ITU0 OVI0
.DATA.L RESERVE .DATA.L RESERVE
; 59 h'0000ec ; 60 h'0000f0
SCI1 TEI1 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 0x0061 0x0062 0x0063
0x00 0x00 0x05 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; 410 : cnt0 ++ ; 411 : cnt1 ++ ; 412 : }
/* Flag clear
*/
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 : ITU0_TSR:
7
6
5
4
3
2
1
0
−
−
−
−
−
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] 0 Read IMFA when IMFA = 1, then write 0 in IMFA 1
( Initial value)
[Setting condition] (1) CNT=GRA when GRA functions as an output compare register. (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 1 1 1 1 1 1 1 0 which AND 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”
mov.b
#h'00,r1
Checking of interrupt flag For details refer to Process flow
cmp.b
r1,r2
Checking of interrupt flag For details refer to sub.b
Flow of interrupt check Checking of 7 flags of external interrupt source and 30 flags of internal interrupt source. If flag is raised, the address corresponding with the flag and stored in the interrupt vector table, is read out and jumps to the address in which the interrupt program.is stored. Flow of processing returns to place from which was formerly jumped when interrupt program is completed and flag is checked. If all interrupt flags are "0" then flow of processing shifts to next program.
#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 Address reading from vector address 24 Jump to that address Process flow
#pragma interrupt( interrupt_timer0 ) void interrupt_timer0 (void) { ITU0_TSR &= 0xfe; cnt0++; cnt1++; }
When returning, the interrupt flag is checked again. If all are "0", it is as follows cmp.b
r1,r2
Same process #h'56,r3 …
sub.b
- 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 ) void abc( void ) {
_abc:
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. 389 : 390 : 391 : 392 : 393 : 394 : 395 : 396 : 397 : 398 : 399 :
Setting of reset-synchronized PWM mode /* ITU3, 4 reset-synchronized PWM mode for right-left motor and servo */ ITU3_TCR = 0x23; ITU_FCR = 0x3e; ITU3_GRA = PWM_CYCLE; /* Setting of cycle */ ITU3_GRB = ITU3_BRB = 0; /* PWM Setting of left motor */ ITU4_GRA = ITU4_BRA = 0; /* PWM Setting of right motor */ ITU4_GRB = ITU4_BRB = SERVO_CENTER; /* PWM Setting of servo */ ITU_TOER = 0x38; /* Count start of ITU*/ 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.
ITU3 PWM mode
ITU4 PWM mode
B0 B0 B1 ITU3 and ITU4
B2
B2 Reset synchronized
B4 B3
※ B0 means bit #0 of port B single PWM mode Output of 2 PWM by 2 ITU is possible
B5 Reset
synchronized
PWM
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 CCLR1 − ITU3_TCR: Set value: 0 0 Hexadecimal:
5
4
3
2
1
0
CCLR0
CKEG1
CKEG0
TPSC2
TPSC1
TPSC0
1
0
0
0
1
1
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 CNT is cleared by compare match/input capture of GRA 0 1 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 be reciprocal number 1 / (24.576 106) = 40.69[ns] Maximum value is 40.69[ns] 65535 = 2.666[ms]
0
0
0
0
0
1
Internal clock: It is counted by φ / 2 1 / (24.576 106/2) = 81.38[ns] Maximum value is 1.38[ns] 65535 = 5.333[ms]
0
1
0
Internal clock: It is counted by φ / 4 1 / (24.576 106/4) = 162.76[ns] Maximum value is 162.76[ns] 65535 = 10.67[ms]
0
1
1
Internal clock: It is counted by φ / 8 1 / (24.576 106/8) = 325.52[ns] Maximum value is 325.52[ns] 65535 = 21.33[ms]
1 1 1 1
0 0 1 1
0 1 0 1
External clock A: It is counted by TCLKA pin (PA0). External clock B: It is counted by TCLKB pin (PA1). External clock C: It is counted by TCLKC pin (PA2). 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: ITU_FCR: Set value: Hexadecimal:
7
6
5
4
3
2
1
0
−
−
CMD1
CMD0
BFB4
BFA4
BFB3
BFA3
0
0
1
1
1
1
1
0
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. Channels 3 and 4 operate together in reset-synchronized PWM mode. 1 1 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: ITU_TOER: Set value: Hexadecimal
7
6
5
4
3
2
1
0
−
−
EXB4
EXA3
EB3
EB4
EA4
EA3
0
0
1
1
1
0
0
0
3
8
Bit 5: Master Enable TOCXB4 (EXB4) Enables or disables ITU output at pin TOCXB4. EXB4 Explanation 0
TOCXB4 output is disabled regardless of TFCR settings ( 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 0
TOCXA4 output is disabled regardless of TFCR settings ( 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 0
TIOCB3 output is disabled regardless of TIOR3 and TFCR settings (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 0
TIOCB4 output is disabled regardless of TIOR4 and TFCR settings (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 0
TIOCA4 output is disabled regardless of TIOR4, TMDR, and TFCR settings (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 0
TIOCA3 output is disabled regardless of TIOR3, TMDR, and TFCR settings (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
PB4 is a normal input-output port.
PB1 is a normal input-output port.
PB3 is a normal input-output port.
PB2 is a normal input-output port.
PB0 is a normal input-output port.
When "0"
−
−
PB5 is a normal input-output port
When "1"
−
−
PB5 is a PWM output
PB4 is a PWM output
PB1 is a PWM output
PB3 is a PWM output
PB2 is a PWM output
PB0 is a PWM output
Setting at this time
0
0
1
1
1
0
0
0
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.
ITU3_GRB
Setting of PWM waveform , which is output, from TIOCA3 and TIOCB3 pins. 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.
ITU4_GRA
Setting of PWM waveform , which is output, from TIOCA4 and TIOCXA4 pins. 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.
ITU4_GRB
Setting of PWM waveform , which is output, from TIOCB4 and TIOCX B4 pins. 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
GRA3 +1
GRB3 +1
"0"
ITU_TOER
Current output signal
PB0 PB0
CNT3 PB1 PB1 0
GRA3 +1
"0"
PB2 PB2
CNT3 PB4
0
GRA3 +1
PB4
GRA4 +1
"0"
PB3 PB3
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
GRA3 +1
GRB3 +1
"1"
ITU_TOER
Current output signal
PB0 PB0
CNT3 PB1 PB1 0
GRA3 +1
"0"
PB2 PB2
CNT3 PB4
0
GRA3 +1
PB4
GRA4 +1
"0"
PB3 PB3
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
GRA3 +1
GRB3 +1
"1"
ITU_TOER
Current output signal
PB0 PB0
CNT3 PB1 PB1 0
GRA3 +1
"1"
PB2 PB2
CNT3 PB4
0
GRA3 +1
PB4
GRA4 +1
"0"
PB3 PB3
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
GRA3 +1
GRB3 +1
"1"
Current output signal
ITU_TOER PB0 PB0
CNT3 PB1 PB1 0
GRA3 +1
"1"
PB2 PB2
CNT3 PB4
0
GRA3 +1
PB4
GRA4 +1
"1"
PB3 PB3
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
GRA3 +1
GRB3 +1
"0"
Current output signal
ITU_TOER PB0 PB0
CNT3 PB1 PB1 0
GRA3 +1
"0"
PB2 PB2
CNT3 PB4
0
GRA3 +1
PB4
GRA4 +1
"0"
PB3 PB3
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
GRA3 +1
GRB3 +1
"0"
ITU_TOER
Current output signal
PB0 PB0
CNT3 PB1 PB1 0
GRA3 +1
"0"
PB2 PB2
CNT3 PB4
0
GRA3 +1
PB4
GRA4 +1
"0"
PB3 PB3
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 reversed here.
Shape was reversed 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 ) { init(); while( 1 ) { ITU3_GRB = 4999; … … } }
0
CNT3
GRA3 +1
PB0 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 ITU3_BRA ITU3_BRB ITU4_BRA ITU4_BRB
Explanation It is a buffer of ITU3_GRA (ITU3 buffer register A). This time, ITU3_GRA does not go with 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 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 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 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 ) { init(); while( 1 ) { ITU3_BRB = 4999; … … } }
0
CNT3
GRA3 +1
PB0 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 CMD1 CMD0 − − ITU_FCR: Setup value: 0 0 1 1 Hexadecimal :
3
3
2
1
0
BFB4
BFA4
BFB3
BFA3
1
1
1
0
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. 1 Cycle
1Cycle Output waveform Value of ITU3_CNT
1000→0
0 1
It becomes "1"→"0" in CNT=(GRB+1)=(0+1)=1. It becomes "0"→"1" in CNT=(GRA+1)=(999+1)=1000. (”1” is reversed to “0”.) 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. 1 Cycle
1Cycle 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
1000→0
0 999
It becomes "1"-> "0" CNT=(GRB+1)=(998+1)=999 (reverse)
in It becomes “0”->”1” in CNT=(GRA+1)=(999+1)=1000 (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 : 415 : 416 : 417 : 418 : 419 : 420 : 421 :
/************************************************************************/ /* Timer main unit */ /* Argument Timer value 1=1ms */ /************************************************************************/ void timer( unsigned long timer_set ) { cnt0 = 0; while( cnt0 < timer_set ); }
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 : 425 : 426 : 427 : 428 : 429 : 430 : 431 : 432 : 433 : 434 : 435 : 436 : 437 : 438 : 439 : 440 : 430 :
/************************************************************************/ /* Sensor state detection */ /* Argument Mask value */ /* Return value Sensor value */ /************************************************************************/ unsigned char sensor_inp( unsigned char mask ) { unsigned char sensor; sensor
= P7DR;
/* Since bit0 is for left and bit7 is for right in the new sensor board which is reversed */ /* to the previous sensor board, the bit has been replaced to maintain compatibility. */ sensor = bit_change( sensor ); /* Bit replacement */ sensor &= mask; return sensor; } 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) 0 1
B 0 0
A and B 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 Mask value
7
6
5
4
3
2
1
0
0
0
0
0
0
1
1
0
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 Mask value
00011111 00000110
Result
00000110 Return value
- 72 -
)AND
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. limited in case of creating program of the micom car. It is defined in advance.
38 : 39 : 40 : 41 : 42 : 43 : 44 : 45 : 46 : 47 :
/* Mask value setting ×: There is mask(Invalid) #define MASK2_2 0x66 #define MASK2_0 0x60 #define MASK0_2 0x06 #define MASK3_3 0xe7 #define MASK0_3 0x07 #define MASK3_0 0xe0 #define MASK4_0 0xf0 #define MASK0_4 0x0f #define MASK1_1 0x81
: No mask(Valid) /* × ×× × /* × ××××× /* ××××× × /* ×× /* ××××× /* ××××× /* ×××× /* ×××× /* ××××××
"MASK _ " means that the number of “1” on the left side are bits and the remaining are made compulsorily “0”.
Actually, the mask value is
*/ */ */ */ */ */ */ */ */ */
bits, and the number of “1” on the right side are
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 : 548 : 549 : 550 : 551 : 552 : 553 : 554 : 555 : 556 : 557 : 558 : 559 : 560 : 561 : 562 :
/************************************************************************/ /* Bit change */ /* Argument Value to be changed */ /* Return value Changed value */ /************************************************************************/ char unsigned bit_change( char unsigned in ) { unsigned char ret; int i; for( i = 0; i < 8; i++ ) { ret >>= 1; ret |= in & 0x80; in <<= 1; } return ret; }
/* Right shift of return value */ /* Ret bit7 = in bit7 */ /* Left shift of argument */ 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
0 11111000
ret
00000000 →
Though “ret” is shifted to right, there is no change because all bits are “0” at the beginning.
10000000
Bit7 of “in” and bit7 of “ret” are operated in logical OR. It shifts one bit to the left, “0” is entered in bit0.
11110000 ←
1 11110000
01000000 →
“ret” shifts to the right, the prior “1” moves to bit6 and “0” is inserted in bit7.
11000000
Bit7 of “in” and bit7 of “ret” are operated in logical OR. “in” shifts one bit to the left, “0” is entered in bit0.
11100000 ←
00000000
…
…
… 7
00011111 →
“ret” shifts to the right, prior “0” moves to the bit6, “0” is inserted in bit7.
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 ←
8
Explanation
“in” shifts one bit to the left, “0” is entered in bit0. 00011111
End, the bit change value is assumed to be a return 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 : 444 : 445 : 446 : 447 : 448 : 449 : 450 : 451 : 452 : 453 : 454 : 455 : 456 : 457 :
/************************************************************************/ /* Crossline detection processing */ /* return value 0: no crossline 1: crossline exists */ /************************************************************************/ int check_crossline( void ) { unsigned char b; int ret; ret = 0; b = sensor_inp(MASK2_2); if( b==0x66 || b==0x64 || b==0x26 || b==0x62 || b==0x46 ) { ret = 1; } return ret; }
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 : 461 : 462 : 463 : 464 : 465 : 466 : 467 : 468 : 469 : 470 : 471 :
/************************************************************************/ /* Reading of DIP switch value */ /* Return value Switch value 0 ~15 */ /************************************************************************/ unsigned char dipsw_get( void ) { unsigned char sw; sw = ~P6DR; sw &= 0x0f; return
/* Reading of DIP switch */
sw;
}
- 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
P60 P61 P62 P63
Inversion (by “tilde”) OFF
sw
????0011 Mask
ON State of switch
????1100
sw
00000011 Return value
sw
3.16.
00000011
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 : 475 : 476 : 477 : 478 : 479 : 480 : 481 : 482 : 483 : 484 : 485 :
481 :
/************************************************************************/ /* Push switch value reading */ /* Return value Switch value ON: “1” and OFF: “0” */ /************************************************************************/ unsigned char pushsw_get( void ) { unsigned char sw; sw = ~PBDR; sw &= 0x01; return
/* Reading of port having a push switch */
sw;
}
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
?
4
?
3
?
PBDR
Inversion sw
ON
2
?
1
?
0
0
? ? ? ? ? ? ? 0
? ? ? ? ? ? ? 1
Mask Return Value
Push switch
0 0 0 0 0 0 0 1
? something unknown
- 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 1 2 3
00 01 10 11
Switch off Switch off Switch on Switch on
Switch off Switch on Switch off 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 : 489 : 490 : 491 : 492 : 493 : 494 : 495 : 496 : 497 : 498 : 499 : 500 :
496 :
/************************************************************************/ /* LED control */ /* Argument Switch value (LED1, LED0) = (bit1 : bit0) :"0": OFF, "1": ON */ /* Example 0x3->(LED1, LED0)=(ON, ON) : 0x2->(LED1, LED0)=(ON, OFF) */ /************************************************************************/ void led_out( unsigned char led ) { unsigned char data; led = ~led; led <<= 6; data = PBDR & 0x3f; PBDR = data | led; }
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
Read
Inversion by “~” 1 1 1 1 1 1 0 1
0
7
1
6
?
5
?
4
?
3
?
2
?
1
?
0
? ? ? ? ? ? ? ?
Mask of 0x3f
6 bit shift 0 1 0 0 0 0 0 0
LED1 LED0
PBDR
0 0 ? ? ? ? ? ?
Logical OR
0 1 ? ? ? ? ? ?
?is no change
- 81 -
ON!
OFF
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.
1 2 3
Signal, direction board<-PB7 board<-PB6
4
board<-PB5
Servo signal
5
board<-PB4
Right motor PWM
Stop
Operate
6
board<-PB3
7
board<-PB2
Direction of right motor rotation Direction of left motor rotation
Normal rotation Normal rotation
Reverse rotation Reverse rotation
8
board<-PB1
Left motor PWM
Stop
Operate
9
board->PB0
Push switch
Pushed
Not pushed
10
-
GND
Pin no.
Details
“0”
“1”
+5V LED1 LED0
ON ON
OFF OFF PWM signal
Details
Duty ratio setting by ITU4_BRB Duty ratio setting by ITU4_BRA
Duty ratio setting by ITU3_BRB
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
GRA3 +1
GRB3 +1
"0"
ITU_TOER PB0 PB0
Usual I/O port
CNT3 PB1 PB1
To left motor
0
GRA3 +1
"0"
PB2 PB2
Usual I/O port
CNT3 PB4
0
GRA3 +1
To right motor
PB4
GRA4 +1
Buffer is ITU4_BRA
"0"
PB3 PB3
Usual I/O port
CNT3 PB5 PB5
interlock
GRB4+1
- 82 -
To Servo
Program Explanation Manual “kit05.c”
502 : 503 : 504 : 505 : 506 : 507 : 508 : 509 : 510 : 511 : 512 : 513 : 514 : 515 : 516 : 517 : 518 : 519 : 520 : 521 : 522 : 523 : 524 : 525 : 526 : 527 : 528 : 529 : 530 : 531 : 532 : 533 : 534 :
/************************************************************************/ /* Speed Control */ /* Argument Left motor: -100 ~ 100 , Right motor: -100 ~ 100 */ /* 0:Stop,100:normal rotation 100%,-100:Reverse 100% */ /************************************************************************/ void speed( int accele_l, int accele_r ) { unsigned char sw_data; unsigned long speed_max; sw_data = dipsw_get() + 5; /* DIP switch read */ speed_max = (unsigned long)(PWM_CYCLE-1) * sw_data / 20; /* Left motor */ if( accele_l >= 0 ) { PBDR &= 0xfb; ITU3_BRB = speed_max * accele_l / 100; } else { PBDR |= 0x04; accele_l = -accele_l; ITU3_BRB = speed_max * accele_l / 100; } /* Right motor */ if( accele_r >= 0 ) { PBDR &= 0xf7; ITU4_BRA = speed_max * accele_r / 100; } else { PBDR |= 0x08; accele_r = -accele_r; ITU4_BRA = speed_max * accele_r / 100; } }
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
speed_max =
(unsigned long)
2
Value when 100%
3
sw_data(5 ~ 20) 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 P63
P62
P61
P60
Decimal number
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Calculation 5/20 6/20 7/20 8/20 9/20 10/20 11/20 12/20 13/20 14/20 15/20 16/20 17/20 18/20 19/20 20/20
Ratio of The maximum speed 25% 30% 35% 40% 45% 50% 55% 60% 65% 70% 75% 80% 85% 90% 95% 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 : 516 : 517 : 518 : 519 :
/* Left motor */ if( accele_l >= 0 ) { PBDR &= 0xfb; ITU3_BRB = speed_max * accele_l / 100; } 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 PBDR before change AND value PBDR after change
7
6
5
4
3
2
1
0
?
?
?
?
?
?
?
1
1
1
1
1
1
0
1
1
->0 xfb
0
= 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 speed function" is output to the motor.
- 84 -
ratio set by
Program Explanation Manual “kit05.c” 516 : Omission 519 : 520 : 521 : 522 : 523 :
if( accele_l >= 0 ) { } else { PBDR |= 0x04; accele_l = -accele_l; ITU3_BRB = speed_max * accele_l / 100; }
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 -> 0x04 OR value 0 0 0 0 0 1 0 0 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 : 526 : 527 : 528 : 529 :
/* Right motor */ if( accele_r >= 0 ) { PBDR &= 0xf7; ITU4_BRA = speed_max * accele_r / 100; } 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 PBDR before change AND value PBDR after value
7
6
5
4
3
2
1
0
?
?
?
?
?
?
?
1
1
1
1
1
0
1
1
1
-> 0xf7
0
= 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 speed function" is output to the motor.
- 85 -
ratio set by
Program Explanation Manual “kit05.c” 526 : Omission 529 : 530 : 531 : 532 : 533 :
if( accele_r >= 0 ) { } else { PBDR |= 0x08; accele_r = -accele_r; ITU4_BRA = speed_max * accele_r / 100; }
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 PBDR before change OR value PBDR after change
7
6
5
4
3
2
1
0
?
?
?
?
?
?
?
1
0
0
0
0
1
0
0
0
-> 0x08
1
= 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 );
0 0 1 1 P60
P61
P62 P63
OFF ON State of switch
/* Left 70% ratio
Right 100% ratio
*/
When the DIP switch is as shown in the figure, since the value read from the switch is 3, Left motor PWM value = (3+5)/20 70/100 = 0.4 0.7 = 0.28 = 28% 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 : 538 : 539 : 540 : 541 : 542 : 543 : 544 :
/************************************************************************/ /* Servo steering operation */ /* Argument Servo operation angle: -90 ~ 90 */ /* -90: 90o to the left turn ,0: straight ,90: 90o to the right turn */ /************************************************************************/ void handle( int angle ) { ITU4_BRB = SERVO_CENTER - angle * HANDLE_STEP; }
The connection of the motor drive board is confirmed again.
1 2 3
Signal, direction board<-PB7 board<-PB6
4
board<-PB5
Servo signal
5
board<-PB4
Right motor PWM
Stop
Operation
6
board<-PB3
7
board<-PB2
Right motor rotation direction Left motor rotation direction
Normal rotation Normal rotation
Reverse rotation Reverse rotation
8
board<-PB1
Left motor PWM
Stop
Operation
9 10
board->PB0 -
Push switch GND
Pushed
Not pushed
Pin no.
Details
“0”
“1”
+5V LED1 LED0
ON ON
OFF OFF PWM signal
Details
Duty ratio is set with ITU4_BRB Duty ratio is set with ITU4_BRA
Duty ratio is set with ITU3_BRB.
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 of servo
angle
Increment of 1 degree
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 : 71 : 72 : 73 : 74 : 75 : 76 : 77 : 78 : 79 : 80 : 81 : 82 : 83 : 84 : 85 :
/************************************************************************/ /* Main Program */ /************************************************************************/ void main( void ) { int i; int pattern; /* Initialization of microcomputer function */ init(); /* Initialization */ set_ccr( 0x00 ); /* Whole interrupt enable */ /* State initialization of micom car*/ handle( 0 ); speed( 0, 0 ); pattern = 0; 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: CCR: Set value: Hexadecimal number:
7
6
5
4
3
2
1
0
I
UI
H
U
N
Z
V
C
0
-
-
-
-
-
-
-
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”
I 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 : 82 : 83 : 84 : 85 :
/* State initialization of micom car */ handle( 0 ); speed( 0, 0 ); pattern = 0; 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 : 88 : 107 : 121 : 122 : 123 : 135 :
while( 1 ) { switch( pattern ) { case 0: Process at pattern 0 break; case 1: Process at pattern 1 break; Pair
Pair
Each pattern process 357 : 358 : 359 : 360 : 361 : 362 :
default: /* When it is not any pattern, return to standby state. */ pattern = 0; break; } }
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. switch( pattern ) { case 0: /* If it is pattern =0, this line is executed. */ break; case 1: /* If it is pattern =1, this line is executed. */ break; default: /* If it is not any of above, then this line is executed. */ break; }
Constant number of pattern and each case statement is compared and if it matches then process is done by jumping to that case position. This process ends when meets with break statement or end terminal “}” of switch statement. The default statement is executed when inapplicable even to the constant number of 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 0 1 11 12 13 21 22 23 24 31 32 41 42
Process contents Switch input waiting Wait for one second after the switch is pushed. Common trace Checking of end of large turn to the right. Checking of end of large turn to the left. Process at the time of first crossline detection. Second crossline is skipped. No.1 process of trace after crossline. Crank detection and No.2 process of trace after crossline. Wait a little till doing left crank clearing process stability. Check of left crank clear process turning end. Wait a little till doing right crank clearing process stability. Check of right crank clear process turning end.
Remarks 0~10 is assumed as process before program run. 11~20 is assumed as process during common program run.
After the crossline is found, 21~30 is assumed as process up to the right angle.
31~40 is assumed as left crank process.
41~50 is assumed as right crank process.
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
Left large bend Pattern 11 Common trace process Right large bend
Crossline detection
Pattern 12 Check of large right bending end
Large bend end.
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 0.2 seconds waiting
Pattern 41 0.2 seconds waiting
Pattern 32 Check of left crank bending end
Pattern 42 Check of right crank bending end
- 92 -
Pattern 13 Check of large left bending end
Large bend end
Crossline detection
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 ) { led_out( 0x1 ); } else if( cnt1 < 200 ) { led_out( 0x2 ); } else { cnt1 = 0; }
<-Is cnt1 0~99? <-Then, only LED0 lights <-Is cnt1 100~199? <-Then, only LED1 lights <-If other than this (more than 200) <-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 : 108 : 109 : 110 : 111 : 112 : 113 : 114 : 115 : 116 : 117 : 118 : 119 : 120 : 121 :
case 0: /* Switch input waiting */ if( pushsw_get() ) { pattern = 1; cnt1 = 0; break; } if( cnt1 < 100 ) { led_out( 0x1 ); } else if( cnt1 < 200 ) { led_out( 0x2 ); } else { cnt1 = 0; } break;
/* LED blinking process
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 : 122 : 123 : 124 : 125 : 126 : 127 : 128 : 129 : 130 : 131 : 132 : 133 : 134 : 135 :
case 1: /* 1 second waiting after the switch is pushed */ if( cnt1 < 500 ) { /* 1.0 seconds starting ago LED1: "OFF",LED0: "ON" */ led_out( 0x1 ); } else if( cnt1 < 1000 ) { /* 0.5 seconds starting ago LED1: "ON",LED0: "OFF" */ led_out( 0x2 ); } else { /* Start!! */ led_out( 0x0 ); pattern = 11; cnt1 = 0; } 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.
1
2
3
4
5
6
7
8
9
State of course and sensor
Value when sensor is read
Hexadecimal number
Steering angle
Left motor PWM
Right motor PWM
●●●××●●●
00000000
0x00
0
100
100
00000100
0x04
5
100
100
00000110
0x06
10
80
69
00000111
0x07
15
50
40
00000011
0x03
25
30
21
00100000
0x20
-5
100
100
01100000
0x60
-10
69
80
11100000
0xe0
-15
40
50
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.
State of course and sensor
10
×○○××○○×
Value when sensor is read
Hexadecimal number
01100110
0x66
Steering angle
Left motor PWM
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 : 147 : 148 : 149 : 150 :
case 0x00: /* Center -> Straight */ handle( 0 ); speed( 100 ,100 ); 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 -
Right motor PWM
Program Explanation Manual “kit05.c” ●It is slightly left inclined. 152 : 153 : 154 : 155 : 156 :
case 0x04: /* Slightly left inclined->Slight turn to the right. handle( 5 ); speed( 100 ,100 ); 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 : 159 : 160 : 161 : 162 :
case 0x06: /* Little left inclined->Small turn to the right. handle( 10 ); speed( 80 ,69 ); 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 1 1 0 0 0 0
- 97 -
Program Explanation Manual “kit05.c” ●Left inclined from the middle veering 164 : 165 : 166 : 167 : 168 :
case 0x07: /* Left inclined from the middle->Middle turn to the right */ handle( 15 ); speed( 50 ,40 ); 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.
1 1 1 0 0 0 0 0
●More inclined to the left 170 : 171 : 172 : 173 : 175 :
case 0x03: /* Large inclined to the left->Large turn to the right */ handle( 25 ); speed( 30 ,21 ); 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.
1 1 0 0 0 0 0 0
- 98 -
Program Explanation Manual “kit05.c” ●Slightly inclined to the right 177 : 178 : 179 : 180 : 181 :
case 0x20: /* Slightly inclined to the right->Slight turn to the left */ handle( -5 ); speed( 100 ,100 ); 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 : 184 : 185 : 186 : 187 :
case 0x60: /* A little inclined to the right->Small turn to the left */ handle( -10 ); speed( 69 ,80 ); 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 : 190 : 191 : 192 : 193 :
case 0xe0: /* Right inclined from the middle veering->Middle turn to the left */ handle( -15 ); speed( 40 ,50 ); 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 : 196 : 197 : 198 : 200 :
case 0xc0: /* Large inclined to the right->Large turn to the left */ handle( -25 ); speed( 21 ,30 ); 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 : 142 : 143 : 144 :
if( check_crossline() ) { pattern = 21; break; }
/* Crossline Check
*/
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 : 203 :
default: 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 0 0 0 0 0
0 1 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:
1
1
0
0
0
0
0
196 :
/* Large inclined to the right - >
197 :
handle(- 25 );
Large turn to the left */
0
198 :
speed( 21 ,30 );
199 :
pattern = 13;
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 : 171 : 172 : 173 : 174 : 175 :
case 0x03: /* Large incline to the left->Large turn to the right */ handle( 25 ); speed( 30 ,21 ); pattern = 12; <-Move to pattern 12 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 0 1 1 0
1 1 0 0 0 0 0
To pattern 12 since 0x03
Continue turning in pattern 12
0 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 : 214 : 218 : 219 : 220 : 221 :
case 12: /* Right turn completion check */ if( sensor_inp(MASK3_3) == 0x06 ) { pattern = 11; } 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
0
0 0
0
1
1
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 : 208 : 209 : 210 : 211 : 212 : 213 : 214 : 215 : 216 :
case 12: /* Check of large turning to the right completion */ if( check_crossline() ) { /* Crossline check even during turning */ pattern = 21; break; } if( sensor_inp(MASK3_3) == 0x06 ) { pattern = 11; } 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
State 0xc0 at the time of large turn
0 0 0 0 0 1
Inclined more to the right than 0xc0. Though still turning to the left.
170 : case 0x03:
0
171 :
/* Large incline to the left- >
172 :
handle( 25 );
173 :
speed( 30 ,21 );
174 :
pattern = 12;
175 :
break;
l arge turn to the right */
0
0
0
0
0
1
1
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 the left according to the program...
Since it is actually inclining to the right, when the servo 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
1 1 0 0 0 0 0 0
0 0
0
0 0
1
1
Continue turning in pattern 13
To pattern 13 since 0xc0 is attained
0 0 1 0
0
0 0
0
1 1
Still turning
0
1 1
0 0
0
0 0
0
Continue turning even though about to fall
0 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 : 219 : 224 : 225 : 226 : 227 :
case 13: /* Check of large turning completion to the left */ if( sensor_inp(MASK3_3) == 0x60 ) { pattern = 11; } 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
1 0
0
0 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 : 219 : 220 : 221 : 222 : 223 : 224 : 225 : 226 : 227 :
case 13: /* Check of large turning completion to the left */ if( check_crossline() ) { /* Crossline check even during large turn */ pattern = 21; break; } if( sensor_inp(MASK3_3) == 0x60 ) { pattern = 11; } 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
Left large bend Pattern 11 Common trace process Right large bend
Crossline detection
Pattern 12 Check of large right bending end
Large bend end.
Crossline detection
Pattern 13 Check of large left bending end
Large bend end
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 0.2 seconds waiting
Pattern 41 0.2 seconds waiting
Pattern 32 Check of left crank bending end
Pattern 42 Check of right crank 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 ? from pattern 11 to pattern 21, it was a program like below. 141 : 142 : 143 : 144 :
if( check_crossline() ) { pattern = 21; break; }
/* crossline check
If you recall the part that moved
*/
5 0 ~1 0 0 c m
When the sensor board has detected the crossline, the flow becomes to pattern 21 as a figure below.
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 : 230 : 231 : 232 : 233 : 234 : 235 : 236 : 237 : 238 : 239 : 240 : 241 : 242 : 243 : 244 :
case 21: /* Process when first crossline is detected*/ led_out( 0x3 ); handle( 0 ); speed( 0 ,0 ); pattern = 22; cnt1 = 0; break; case 22: /* Second is skipped. if( cnt1 > 100 ) { cnt1 = 0; pattern = 23; } 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”
○○○× ×●● ●
-> 0xe0
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 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
●● ●××●●●
●●●××○●●
●●●××○○●
●●●××○○○
●●●××●○○
-> 0x00
-> 0x04
-> 0x06
-> 0x07
-> 0x03
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 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 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 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 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 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”
●● ○××●●●
●○○××●●●
○ ○○× ×● ●●
○○●××●●●
-> 0x20
-> 0x60
-> 0xe0
-> 0xc0
The situation when micom car is inclined to right side is considered. The 4 states are shown in left figure. It is inclined 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 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 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 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.
5 0 ~1 0 0 c m
Part processed by pattern 24
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 : 247 : 248 : 249 : 250 : 251 : 252 : 253 : 254 : 255 : 256 : 257 : 258 : 259 : 260 : 261 : 262 : 263 : 264 : 265 : 266 : 267 : 268 : 269 : 270 : 271 : 272 : 273 : 274 : 275 : 276 : 277 : 278 : 279 : 280 : 281 : 282 : 283 : 284 : 285 : 286 : 287 : 288 : 289 : 290 :
case 23: /* 1 of trace after crossline */ if( cnt1 > 300 ) { cnt1 = 0; Moves to pattern 24 after 0.3 seconds. pattern = 24; break; } switch( sensor_inp(MASK3_3) ) { case 0x00: /* Center->Straight */ handle( 0 ); speed( 40 ,40 ); break; case 0x04: If written by continuing the case case 0x06: It means that time of 0x04 or 0x06 or 0x07 or case 0x07: case 0x03: 0x03. /* Tending to the left->Turn to the right */ handle( 8 ); speed( 40 ,36 ); break; case 0x20: If written by continuing the case case 0x60: It means that time of 0x20 or 0x60 or 0xe0 or 0xc0. case 0xe0: case 0xc0: /* Tending to the right->Turn to the left */ handle( -8 ); speed( 36 ,40 ); break; default: break; } break; case 24: /* 2 of trace after crossline and crank detection */ switch( sensor_inp(MASK3_3) ) { case 0xe0: /* Judgment of left crank->To the left crank clearing process */ led_out( 0x1 ); handle( -38 ); speed( 10 ,50 ); pattern = 31; cnt1 = 0; break;
- 114 -
Program Explanation Manual “kit05.c” 291 : 292 : 293 : 294 : 295 : 296 : 297 : 298 : 299 : 300 : 301 : 302 : 303 : 304 : 305 : 306 : 307 : 308 : 309 : 310 : 311 : 312 : 313 : 314 : 315 : 316 : 317 : 318 : 319 : 320 : 321 :
case 0x07: /* Judgment of right crank->The process for getting over right crank */ led_out( 0x2 ); handle( 38 ); speed( 50 ,10 ); pattern = 41; cnt1 = 0; break; case 0x00: /* Center->Straight */ handle( 0 ); speed( 40 ,40 ); break; case 0x04: case 0x06: case 0x03: /* Tending to the left->Turn to the right */ handle( 8 ); speed( 40 ,36 ); break; case 0x20: case 0x60: case 0xc0: /* Tending to the right->Turn to the left */ handle( -8 ); speed( 36 ,40 ); break; default: break; } 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.
2
1 ●○ ○× × ● ●● ○ ○○× ×●● ●
When it turns, in the part where the color changes 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.
"0xe0" is detected, and turn till it becomes "0x60"
●● ●×× ●●●
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
○○○× ×●●●
After 0.2 seconds, sensor is confirmed. "Turning is continued till 0x60".
4
●
●
●
3
●
○
○
×
×
●●●
×× ○ ○
●
"0xe0" is detected, and waited for 0.2 seconds after turning with a large angle.
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 : 324 : 325 : 326 : 327 : 328 : 329 : 330 : 331 : 332 : 333 : 334 : 335 : 336 : 337 : 338 :
case 31: /* Left crank clear process Wait a little till it becomes stable. */ if( cnt1 > 200 ) { pattern = 32; cnt1 = 0; } break; case 32: /* Left crank clear process Check of end of turn */ if( sensor_inp(MASK3_3) == 0x60 ) { led_out( 0x0 ); pattern = 11; cnt1 = 0; } 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”
2
1
●●●××○○● ● ●●× ×○○ ○
Detected "0x07", Large turn until "0x06".
The moment it turned, the sensor state became "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”
2
1
●●
●×
×●
●●
●● ●××○○○
Detection of "0x07", Wait for 0.2 seconds after large turn. ●○○
4
●●
×× ●
3
After 0.2 seconds, the sensor is confirmed from here. "Continue turning up to 0x06."
●× ×
●●
○○ ●
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 : 341 : 342 : 343 : 344 : 345 : 346 : 347 : 348 : 349 : 350 : 351 : 352 : 353 : 354 : 355 :
case 41: /* Right crank clear processing Waits a little until stabilizing */ if( cnt1 > 200 ) { pattern = 42; cnt1 = 0; } break; case 42: /* Right crank clear processing Turning completion check */ if( sensor_inp(MASK3_3) == 0x06 ) { led_out( 0x0 ); pattern = 11; cnt1 = 0; } 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 : 358 : 359 : 360 :
default: /* When the pattern is not applied to any case, return to waiting state */ pattern = 0; 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 : 159 : 160 : 161 : 162 :
case 0x06: /* Slight left inclination->Slight turn to the right */ handle( 10 ); speed( 80 ,?? ); <-?? Is unknown 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
0 1
0 0.017
r2 10.031
2 3
0.035 0.052
5.014 3.341
4 5
0.070 0.087
6 7
r1
r3
r1/r3*100
9.961
10.101
100 99
4.944 3.271
5.084 3.411
97 96
2.504 2.001
2.434 1.931
2.574 2.071
95 93
0.105 0.122
1.666 1.426
1.596 1.356
1.736 1.496
92 91
8 9
0.140 0.157
1.246 1.105
1.176 1.035
1.316 1.175
89 88
10 11
0.174 0.192
0.993 0.901
0.923 0.831
1.063 0.971
87 86
12
0.209
0.824
0.754
0.894
84
13 14
0.227 0.244
0.758 0.702
0.688 0.632
0.828 0.772
83 82
15 16
0.262 0.279
0.653 0.611
0.583 0.541
0.723 0.681
81 79
17 18
0.297 0.314
0.573 0.539
0.503 0.469
0.643 0.609
78 77
19
0.331
0.509
0.439
0.579
76
20 21
0.349 0.366
0.481 0.456
0.411 0.386
0.551 0.526
75 73
22 23
0.384 0.401
0.433 0.413
0.363 0.343
0.503 0.483
72 71
24 25
0.419 0.436
0.393 0.376
0.323 0.306
0.463 0.446
70 69
26
0.454
0.359
0.289
0.429
67
27 28
0.471 0.488
0.344 0.329
0.274 0.259
0.414 0.399
66 65
29 30
0.506 0.523
0.316 0.303
0.246 0.233
0.386 0.373
64 62
31 32
0.541 0.558
0.291 0.280
0.221 0.210
0.361 0.350
61 60
33
0.576
0.270
0.200
0.340
59
34 35
0.593 0.611
0.260 0.250
0.190 0.180
0.330 0.320
58 56
36 37
0.628 0.645
0.241 0.232
0.171 0.162
0.311 0.302
55 54
38 39
0.663 0.680
0.224 0.216
0.154 0.146
0.294 0.286
52 51
40
0.698
0.209
0.139
0.279
50
41 42
0.715 0.733
0.201 0.195
0.131 0.125
0.271 0.265
48 47
43 44
0.750 0.768
0.188 0.181
0.118 0.111
0.258 0.251
46 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
: .CPU 300HA: 20 : : RESERVE: .EQU H'FFFFFFFF : : .IMPORT _main : .IMPORT _interrupt_timer0 : : .SECTION V : .DATA.L RESET_START : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L _interrupt_timer0 : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : .DATA.L RESERVE : : .SECTION P : RESET_START: : MOV.L #H'FFFF10,ER7 : JSR @_main : OWARI: : BRA OWARI : : .END
; Specification of CPU ; Address of unused area ; External reference (main function) ; External reference (interrupt_timer0 function) ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
Start of vector section 0 h'000000 reset 1 h'000004 System reserved 2 h'000008 System reserved 3 h'00000c System reserved 4 h'000010 System reserved 5 h'000014 System reserved 6 h'000018 System reserved 7 h'00001c External interrupt 8 h'000020 Trap instruction 9 h'000024 Trap instruction 10 h'000028 Trap instruction 11 h'00002c Trap instruction 12 h'000030 External interrupt 13 h'000034 External interrupt 14 h'000038 External interrupt 15 h'00003c External interrupt 16 h'000040 External interrupt 17 h'000044 External interrupt 18 h'000048 System reserved 19 h'00004c System reserved 20 h'000050 WDT MOVI 21 h'000054 REF CMI 22 h'000058 System reserved 23 h'00005c System reserved 24 h'000060 ITU0 IMIA0 25 h'000064 ITU0 IMIB0 26 h'000068 ITU0 OVI0 27 h'00006c System reserved 28 h'000070 ITU1 IMIA1 29 h'000074 ITU1 IMIB1 30 h'000078 ITU1 OVI1 31 h'00007c System reserved 32 h'000080 ITU2 IMIA2 33 h'000084 ITU2 IMIB2 34 h'000088 ITU2 OVI2 35 h'00008c System reserved 36 h'000090 ITU3 IMIA3 37 h'000094 ITU3 IMIB3 38 h'000098 ITU3 OVI3 39 h'00009c System reserved 40 h'0000a0 ITU4 IMIA4 41 h'0000a4 ITU4 IMIB4 42 h'0000a8 ITU4 OVI4 43 h'0000ac System reserved 44 h'0000b0 DMAC DEND0A 45 h'0000b4 DMAC DEND0B 46 h'0000b8 DMAC DEND1A 47 h'0000bc DMAC DEND1B 48 h'0000c0 System reserved 49 h'0000c4 System reserved 50 h'0000c8 System reserved 51 h'0000cc System reserved 52 h'0000d0 SCI0 ERI0 53 h'0000d4 SCI0 RXI0 54 h'0000d8 SCI0 TXI0 55 h'0000dc SCI0 TEI0 56 h'0000e0 SCI1 ERI1 57 h'0000e4 SCI1 RXI1 58 h'0000e8 SCI1 TXI1 59 h'0000ec SCI1 TEI1 60 h'0000f0 A/D ADI
NMI
IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5
; Start of program section ; Stack specification ; Jump to main() function of C language
- 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: 6:
.IMPORT _main .IMPORT _interrupt_timer0
; external reference (main function) ; 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: 9: 10 : 11 : 12 : 13 : 14 : 15 : 16 : 17 : 18 : 19 : 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 :
.SECTION V .DATA.L RESET_START .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE
; Start of vector section ; 0 h'000000 reset ; 1 h'000004 System reserved ; 2 h'000008 System reserved ; 3 h'00000c System reserved ; 4 h'000010 System reserved ; 5 h'000014 System reserved ; 6 h'000018 System reserved ; 7 h'00001c External interrupt NMI ; 8 h'000020 Trap instruction ; 9 h'000024 Trap instruction ; 10 h'000028 Trap instruction ; 11 h'00002c Trap instruction ; 12 h'000030 External interrupt IRQ0 ; 13 h'000034 External interrupt IRQ1 ; 14 h'000038 External interrupt IRQ2 ; 15 h'00003c External interrupt IRQ3 ; 16 h'000040 External interrupt IRQ4 ; 17 h'000044 External interrupt IRQ5 ; 18 h'000048 System reservedd ; 19 h'00004c System reservedd ; 20 h'000050 WDT MOVI
- 127 -
Program Explanation Manual “kit05.c” 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : 38 : 39 : 40 : 41 : 42 : 43 : 44 : 45 : 46 : 47 : 48 : 49 : 50 : 51 : 52 : 53 : 54 : 55 : 56 : 57 : 58 : 59 : 60 : 61 : 62 : 63 : 64 : 65 : 66 : 67 : 68 : 69 :
.DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L _interrupt_timer0 .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE .DATA.L RESERVE
; 21 h'000054 ; 22 h'000058 ; 23 h'00005c ; 24 h'000060 ; 25 h'000064 ; 26 h'000068 ; 27 h'00006c ; 28 h'000070 ; 29 h'000074 ; 30 h'000078 ; 31 h'00007c ; 32 h'000080 ; 33 h'000084 ; 34 h'000088 ; 35 h'00008c ; 36 h'000090 ; 37 h'000094 ; 38 h'000098 ; 39 h'00009c ; 40 h'0000a0 ; 41 h'0000a4 ; 42 h'0000a8 ; 43 h'0000ac ; 44 h'0000b0 ; 45 h'0000b4 ; 46 h'0000b8 ; 47 h'0000bc ; 48 h'0000c0 ; 49 h'0000c4 ; 50 h'0000c8 ; 51 h'0000cc ; 52 h'0000d0 ; 53 h'0000d4 ; 54 h'0000d8 ; 55 h'0000dc ; 56 h'0000e0 ; 57 h'0000e4 ; 58 h'0000e8 ; 59 h'0000ec ; 60 h'0000f0
REF CMI System reserved System reserved ITU0 IMIA0 ITU0 IMIB0 ITU0 OVI0 System reserved ITU1 IMIA1 ITU1 IMIB1 ITU1 OVI1 System reserved ITU2 IMIA2 ITU2 IMIB2 ITU2 OVI2 System reserved ITU3 IMIA3 ITU3 IMIB3 ITU3 OVI3 System reserved ITU4 IMIA4 ITU4 IMIB4 ITU4 OVI4 System reserved DMAC DEND0A DMAC DEND0B DMAC DEND1A DMAC DEND1B System reserved System reserved System reserved System reserved SCI0 ERI0 SCI0 RXI0 SCI0 TXI0 SCI0 TEI0 SCI1 ERI1 SCI1 RXI1 SCI1 TXI1 SCI1 TEI1 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.
h’00000 h’00001 h’00002 h’00003
Value h’00 h’00 h’01 h’00
h’000100 address RESET_START: MOV.L #H'FFFF10,ER7 JSR @_main 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.
Interrupt factor Reset NMI IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5
Vector No.
Vector Address
IPR
External terminal
0
H'0000 - H'0003
−−−
7 12 13 14 15 16 17 18 19 20
H'001C - H'001F H'0030 - H'0033 H'0034 - H'0037 H'0038 - H'003B H'003C - H'003F H'0040 - H'0043 H'0044 - H'0047 H'0048 - H'004B H'004C - H'004F H'0050 - H'0053
−−− IPRA7 IPRA6
21
H'0054 - H'0057
22 23 24 25 26 27 28 29 30 31 32 33 34 35
H'0058 - H'005B H'005C - H'005F H'0060 - H'0063 H'0064 - H'0067 H'0068 - H'006B H'006C - H'006F H'0070 - H'0073 H'0074 - H'0077 H'0078 - H'007B H'007C - H'007F H'0080 - H'0083 H'0084 - H'0087 H'0088 - H'008B H'008C - H'008F
External terminal
Reserve WOVI (Interval timer) CMI
Origin
(Compare match)
Reserve IMIA0 (Compare match/input capture A0) IMIB0 (Compare match/input capture B0) OVI0 (Overflow 0) Reserve IMIA1 (Compare match/input capture A1) IMIB1 (Compare match/input capture B1) OVI1 (Overflow 1) Reserve IMIA2 (Compare match/input capture A2) IMIB2 (Compare match/input capture B2) Reserve Reserve
−−−−− Watch dog timer Refresh controller −−−−−
ITU Channel 0 ───── ITU Channel 1 ───── ITU Channel 2 ─────
- 129 -
IPRA5
IPRA4
IPRA3
IPRA2
IPRA1
IPRA0
Priorit y level
high ↑
Program Explanation Manual “kit05.c”
IMIB3 (Compare match/input capture B3) OVI3 (Overflow 3) Reserve IMIA4 (Compare match/input capture A4) IMIB4 (Compare match/input capture B4) OVI4 (Overflow 4) Reserve DEDN0A DEDN0B DEDN1A DEDN1B
Reserve
ERI0 (Reception error 0) RXI0 (Reception completion0) TXI0 (Transmission data empty 0) TEI0 (Transmission end 0) ERI1 (Reception error 1) RXI1 (Reception completion1) TXI1 (Transmission data empty 1) TEI1 (Transmission end 1) ADI (A/D End)
ITU Channel 3 ───── ITU Channel 4 ───── DMAC Group 0
─────
SCI Channel 0
SCI Channel 1 A/D
36
H'0090 - H'0093
37 38 39 40 41 42
H'0094 - H'0097 H'0098 - H'009B H'009C - H'009F H'00A0 - H'00A3 H'00A4 - H'00A7 H'00A8 - H'00AB
43 44 45 46 47
H'00AC - H'00AF H'00B0 - H'00B3 H'00B4 - H'00B7 H'00B8 - H'00BB H'00BC - H'00BF
48 49 50 51
H'00C0 - H'00C3 H'00C4 - H'00C7 H'00C8 - H'00CB H'00CC - H'00CF
52 53 54 55 56 57 58 59 60
H'00D0 - H'00D3 H'00D4 - H'00D7 H'00D8 - H'00DB H'00DC - H'00DF H'00E0 - H'00E3 H'00E4 - H'00E7 H'00E8 - H'00EB H'00EC - H'00EF H'00F0 - H'00F3
IPRB7
IPRB6
IPRB5
──
IPRB3
IPRB2 ↓ 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 72 : RESET_START: 73 : MOV.L #H'FFFF10,ER7 74 : JSR @_main 75 : OWARI: 76 : BRA OWARI
; Start of program section ; Setting of stuck ; Jumping to the main() function of C language
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: 2: 3: 4: 5: 6: 7: 8:
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
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: 6: 7:
start V(000000) start P,C(000100) 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.
C
Section where “const type” data is stored. Allocated in the ROM area. When the variable is 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.
B
Section where the uninitialized data is stored. It is allocated in the RAM area. Section B is 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 V
Start address h’00000
P
h’00100
C
After P section ends
B
h’FEF10
Contents Reset vector address
Remarks Self assigned name Name automatically given by the Program address compiler Const type data storing Name automatically given by the address compiler Name automatically given by the 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.
kit05.c
It is C source program file written in C language. It is the main part, which controls the micom car.
kit05start.src
It is an assembly source program file which programs the vector address of start-up routine and interrupt jump destination. File name is "Name of C language file excluding extension + start.src"
kit05.sub
It is a subcommand file where information when files are linked is described. File name is "Name of C language file excluding extension + .sub"
The following 6 files are created by assembling, compiling, linking and converting this file.
kit05start.lis
It is a file called list file for assembly program. When “kit05start.src” file is assembled, which type of machine language it has been converted to, can be known by seeing this file.
kit05start.obj
It is a file called relocatable object program file. When “kit05start.src” file is assembled, it is output as a file in half executed format.
kit05.obj
It is a file called relocatable object program file. When “kit05start.src” file is assembled, it becomes the half execution form file.
kit05.abs
It is a file called absolute load module file. It is a file after uniting the previous 2 “obj” files.
kit05.map
It is a file, which is called linkage list file. It is a file, which outputs the address to which function and label are allocated when the “abs” file was created by linking (uniting) the 2 “obj” files.
kit05.mot
It is a file, which is called S type format. This file is output when “abs” file is 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 Assembly source program
kit05.c C source program
assemble
compile
kit05start.obj
Linkage list
Relocatable object program
Link
kit05.abs Absolute load module
kit05.obj Relocatable object program
kit05.mot
kit05.sub Subcommand
… File from source
compile
S type format
link
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.
Link
Two or more object files are incorporated by referring to information of the subcommand, and made in to one absolute load module.
Convert
Absolute load module is converted to S type format file, and finally made into a file type 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 -