Inverter Control by V850 Series Vector Control by Encoder AN

To our customers,
Old Company Name in Catalogs and Other Documents
On April 1st, 2010, NEC Electronics Corporation merged with Renesas Technology
Corporation, and Renesas Electronics Corporation took over all the business of both
companies. Therefore, although the old company name remains in this document, it is a valid
Renesas Electronics document. We appreciate your understanding.
Renesas Electronics website: http://www.renesas.com
April 1st, 2010
Renesas Electronics Corporation
Issued by: Renesas Electronics Corporation (http://www.renesas.com)
Send any inquiries to http://www.renesas.com/inquiry.
Notice
1.
2.
3.
4.
5.
6.
7.
All information included in this document is current as of the date this document is issued. Such information, however, is
subject to change without any prior notice. Before purchasing or using any Renesas Electronics products listed herein, please
confirm the latest product information with a Renesas Electronics sales office. Also, please pay regular and careful attention to
additional and different information to be disclosed by Renesas Electronics such as that disclosed through our website.
Renesas Electronics does not assume any liability for infringement of patents, copyrights, or other intellectual property rights
of third parties by or arising from the use of Renesas Electronics products or technical information described in this document.
No license, express, implied or otherwise, is granted hereby under any patents, copyrights or other intellectual property rights
of Renesas Electronics or others.
You should not alter, modify, copy, or otherwise misappropriate any Renesas Electronics product, whether in whole or in part.
Descriptions of circuits, software and other related information in this document are provided only to illustrate the operation of
semiconductor products and application examples. You are fully responsible for the incorporation of these circuits, software,
and information in the design of your equipment. Renesas Electronics assumes no responsibility for any losses incurred by
you or third parties arising from the use of these circuits, software, or information.
When exporting the products or technology described in this document, you should comply with the applicable export control
laws and regulations and follow the procedures required by such laws and regulations. You should not use Renesas
Electronics products or the technology described in this document for any purpose relating to military applications or use by
the military, including but not limited to the development of weapons of mass destruction. Renesas Electronics products and
technology may not be used for or incorporated into any products or systems whose manufacture, use, or sale is prohibited
under any applicable domestic or foreign laws or regulations.
Renesas Electronics has used reasonable care in preparing the information included in this document, but Renesas Electronics
does not warrant that such information is error free. Renesas Electronics assumes no liability whatsoever for any damages
incurred by you resulting from errors in or omissions from the information included herein.
Renesas Electronics products are classified according to the following three quality grades: “Standard”, “High Quality”, and
“Specific”. The recommended applications for each Renesas Electronics product depends on the product’s quality grade, as
indicated below. You must check the quality grade of each Renesas Electronics product before using it in a particular
application. You may not use any Renesas Electronics product for any application categorized as “Specific” without the prior
written consent of Renesas Electronics. Further, you may not use any Renesas Electronics product for any application for
which it is not intended without the prior written consent of Renesas Electronics. Renesas Electronics shall not be in any way
liable for any damages or losses incurred by you or third parties arising from the use of any Renesas Electronics product for an
application categorized as “Specific” or for which the product is not intended where you have failed to obtain the prior written
consent of Renesas Electronics. The quality grade of each Renesas Electronics product is “Standard” unless otherwise
expressly specified in a Renesas Electronics data sheets or data books, etc.
“Standard”:
8.
9.
10.
11.
12.
Computers; office equipment; communications equipment; test and measurement equipment; audio and visual
equipment; home electronic appliances; machine tools; personal electronic equipment; and industrial robots.
“High Quality”: Transportation equipment (automobiles, trains, ships, etc.); traffic control systems; anti-disaster systems; anticrime systems; safety equipment; and medical equipment not specifically designed for life support.
“Specific”:
Aircraft; aerospace equipment; submersible repeaters; nuclear reactor control systems; medical equipment or
systems for life support (e.g. artificial life support devices or systems), surgical implantations, or healthcare
intervention (e.g. excision, etc.), and any other applications or purposes that pose a direct threat to human life.
You should use the Renesas Electronics products described in this document within the range specified by Renesas Electronics,
especially with respect to the maximum rating, operating supply voltage range, movement power voltage range, heat radiation
characteristics, installation and other product characteristics. Renesas Electronics shall have no liability for malfunctions or
damages arising out of the use of Renesas Electronics products beyond such specified ranges.
Although Renesas Electronics endeavors to improve the quality and reliability of its products, semiconductor products have
specific characteristics such as the occurrence of failure at a certain rate and malfunctions under certain use conditions. Further,
Renesas Electronics products are not subject to radiation resistance design. Please be sure to implement safety measures to
guard them against the possibility of physical injury, and injury or damage caused by fire in the event of the failure of a
Renesas Electronics product, such as safety design for hardware and software including but not limited to redundancy, fire
control and malfunction prevention, appropriate treatment for aging degradation or any other appropriate measures. Because
the evaluation of microcomputer software alone is very difficult, please evaluate the safety of the final products or system
manufactured by you.
Please contact a Renesas Electronics sales office for details as to environmental matters such as the environmental
compatibility of each Renesas Electronics product. Please use Renesas Electronics products in compliance with all applicable
laws and regulations that regulate the inclusion or use of controlled substances, including without limitation, the EU RoHS
Directive. Renesas Electronics assumes no liability for damages or losses occurring as a result of your noncompliance with
applicable laws and regulations.
This document may not be reproduced or duplicated, in any form, in whole or in part, without prior written consent of Renesas
Electronics.
Please contact a Renesas Electronics sales office if you have any questions regarding the information contained in this
document or Renesas Electronics products, or if you have any other inquiries.
(Note 1) “Renesas Electronics” as used in this document means Renesas Electronics Corporation and also includes its majorityowned subsidiaries.
(Note 2) “Renesas Electronics product(s)” means any product developed or manufactured by or for Renesas Electronics.
Application Note
Inverter Control by V850 Series
Vector Control by Encoder
V850E/IA1
V850E/IA2
V850E/IA3
V850E/IA4
Document No. U17324EJ1V0AN00 (1st edition)
Date Published November 2004 N CP(K)
2004
Printed in Japan
[MEMO]
2
Application Note U17324EJ1V0AN
NOTES FOR CMOS DEVICES
1
VOLTAGE APPLICATION WAVEFORM AT INPUT PIN
Waveform distortion due to input noise or a reflected wave may cause malfunction. If the input of the
CMOS device stays in the area between VIL (MAX) and VIH (MIN) due to noise, etc., the device may
malfunction. Take care to prevent chattering noise from entering the device when the input level is fixed,
and also in the transition period when the input level passes through the area between VIL (MAX) and
VIH (MIN).
2
HANDLING OF UNUSED INPUT PINS
Unconnected CMOS device inputs can be cause of malfunction. If an input pin is unconnected, it is
possible that an internal input level may be generated due to noise, etc., causing malfunction. CMOS
devices behave differently than Bipolar or NMOS devices. Input levels of CMOS devices must be fixed
high or low by using pull-up or pull-down circuitry. Each unused pin should be connected to VDD or GND
via a resistor if there is a possibility that it will be an output pin. All handling related to unused pins must
be judged separately for each device and according to related specifications governing the device.
3
PRECAUTION AGAINST ESD
A strong electric field, when exposed to a MOS device, can cause destruction of the gate oxide and
ultimately degrade the device operation. Steps must be taken to stop generation of static electricity as
much as possible, and quickly dissipate it when it has occurred.
Environmental control must be
adequate. When it is dry, a humidifier should be used. It is recommended to avoid using insulators that
easily build up static electricity. Semiconductor devices must be stored and transported in an anti-static
container, static shielding bag or conductive material. All test and measurement tools including work
benches and floors should be grounded.
The operator should be grounded using a wrist strap.
Semiconductor devices must not be touched with bare hands. Similar precautions need to be taken for
PW boards with mounted semiconductor devices.
4
STATUS BEFORE INITIALIZATION
Power-on does not necessarily define the initial status of a MOS device. Immediately after the power
source is turned ON, devices with reset functions have not yet been initialized. Hence, power-on does
not guarantee output pin levels, I/O settings or contents of registers. A device is not initialized until the
reset signal is received. A reset operation must be executed immediately after power-on for devices
with reset functions.
5
POWER ON/OFF SEQUENCE
In the case of a device that uses different power supplies for the internal operation and external
interface, as a rule, switch on the external power supply after switching on the internal power supply.
When switching the power supply off, as a rule, switch off the external power supply and then the
internal power supply. Use of the reverse power on/off sequences may result in the application of an
overvoltage to the internal elements of the device, causing malfunction and degradation of internal
elements due to the passage of an abnormal current.
The correct power on/off sequence must be judged separately for each device and according to related
specifications governing the device.
6
INPUT OF SIGNAL DURING POWER OFF STATE
Do not input signals or an I/O pull-up power supply while the device is not powered. The current
injection that results from input of such a signal or I/O pull-up power supply may cause malfunction and
the abnormal current that passes in the device at this time may cause degradation of internal elements.
Input of signals during the power off state must be judged separately for each device and according to
related specifications governing the device.
Application Note U17324EJ1V0AN
3
These commodities, technology or software, must be exported in accordance
with the export administration regulations of the exporting country.
Diversion contrary to the law of that country is prohibited.
• The information in this document is current as of September, 2004. The information is subject to
change without notice. For actual design-in, refer to the latest publications of NEC Electronics data
sheets or data books, etc., for the most up-to-date specifications of NEC Electronics products. Not
all products and/or types are available in every country. Please check with an NEC Electronics sales
representative for availability and additional information.
• No part of this document may be copied or reproduced in any form or by any means without the prior
written consent of NEC Electronics. NEC Electronics assumes no responsibility for any errors that may
appear in this document.
• NEC Electronics does not assume any liability for infringement of patents, copyrights or other intellectual
property rights of third parties by or arising from the use of NEC Electronics products listed in this document
or any other liability arising from the use of such products. No license, express, implied or otherwise, is
granted under any patents, copyrights or other intellectual property rights of NEC Electronics or others.
• Descriptions of circuits, software and other related information in this document are provided for illustrative
purposes in semiconductor product operation and application examples. The incorporation of these
circuits, software and information in the design of a customer's equipment shall be done under the full
responsibility of the customer. NEC Electronics assumes no responsibility for any losses incurred by
customers or third parties arising from the use of these circuits, software and information.
• While NEC Electronics endeavors to enhance the quality, reliability and safety of NEC Electronics products,
customers agree and acknowledge that the possibility of defects thereof cannot be eliminated entirely. To
minimize risks of damage to property or injury (including death) to persons arising from defects in NEC
Electronics products, customers must incorporate sufficient safety measures in their design, such as
redundancy, fire-containment and anti-failure features.
• NEC Electronics products are classified into the following three quality grades: "Standard", "Special" and
"Specific".
The "Specific" quality grade applies only to NEC Electronics products developed based on a customerdesignated "quality assurance program" for a specific application. The recommended applications of an NEC
Electronics product depend on its quality grade, as indicated below. Customers must check the quality grade of
each NEC Electronics product before using it in a particular application.
"Standard": Computers, office equipment, communications equipment, test and measurement equipment, audio
and visual equipment, home electronic appliances, machine tools, personal electronic equipment
and industrial robots.
"Special": Transportation equipment (automobiles, trains, ships, etc.), traffic control systems, anti-disaster
systems, anti-crime systems, safety equipment and medical equipment (not specifically designed
for life support).
"Specific": Aircraft, aerospace equipment, submersible repeaters, nuclear reactor control systems, life
support systems and medical equipment for life support, etc.
The quality grade of NEC Electronics products is "Standard" unless otherwise expressly specified in NEC
Electronics data sheets or data books, etc. If customers wish to use NEC Electronics products in applications
not intended by NEC Electronics, they must contact an NEC Electronics sales representative in advance to
determine NEC Electronics' willingness to support a given application.
(Note)
(1) "NEC Electronics" as used in this statement means NEC Electronics Corporation and also includes its
majority-owned subsidiaries.
(2) "NEC Electronics products" means any product developed or manufactured by or for NEC Electronics (as
defined above).
M8E 02. 11-1
4
Application Note U17324EJ1V0AN
Regional Information
Some information contained in this document may vary from country to country. Before using any NEC
Electronics product in your application, pIease contact the NEC Electronics office in your country to
obtain a list of authorized representatives and distributors. They will verify:
•
Device availability
•
Ordering information
•
Product release schedule
•
Availability of related technical literature
•
Development environment specifications (for example, specifications for third-party tools and
components, host computers, power plugs, AC supply voltages, and so forth)
•
Network requirements
In addition, trademarks, registered trademarks, export restrictions, and other legal issues may also vary
from country to country.
[GLOBAL SUPPORT]
http://www.necel.com/en/support/support.html
NEC Electronics America, Inc. (U.S.)
NEC Electronics (Europe) GmbH
NEC Electronics Hong Kong Ltd.
Santa Clara, California
Tel: 408-588-6000
800-366-9782
Duesseldorf, Germany
Tel: 0211-65030
Hong Kong
Tel: 2886-9318
• Sucursal en España
Madrid, Spain
Tel: 091-504 27 87
• Succursale Française
Vélizy-Villacoublay, France
Tel: 01-30-67 58 00
• Filiale Italiana
Milano, Italy
Tel: 02-66 75 41
• Branch The Netherlands
Eindhoven, The Netherlands
Tel: 040-244 58 45
• Tyskland Filial
NEC Electronics Hong Kong Ltd.
Seoul Branch
Seoul, Korea
Tel: 02-558-3737
NEC Electronics Shanghai Ltd.
Shanghai, P.R. China
Tel: 021-5888-5400
NEC Electronics Taiwan Ltd.
Taipei, Taiwan
Tel: 02-2719-2377
NEC Electronics Singapore Pte. Ltd.
Novena Square, Singapore
Tel: 6253-8311
Taeby, Sweden
Tel: 08-63 80 820
• United Kingdom Branch
Milton Keynes, UK
Tel: 01908-691-133
J04.1
Application Note U17324EJ1V0AN
5
INTRODUCTION
Target Readers
This application note is intended for users who understand the functions of the
V850E/IA1, V850E/IA2, V850E/IA3, and V850E/IA4, and who design application
systems that use these microcontrollers. The applicable products are shown below.
•
V850E/IA1
Standard products: µPD703116, 70F3116
Special products:
•
µPD703116(A), 703116(A1), 70F3116(A), 70F3116(A1)
V850E/IA2:
Standard products: µPD703114, 70F3114
Special products:
•
V850E/IA3
•
V850E/IA4
µPD703114(A), 70F3114(A)
Standard products: µPD703183, 70F3184
Standard products: µPD703185, 703186, 70F3186
Purpose
The purpose of this application note is to help the user understand how a vector is
controlled by the encoder using a brushless DC motor that uses PWM output and A/D
converter input as a system example of the timer/counter function of the V850E/IA1,
V850E/IA2, V850E/IA3, and V850E/IA4.
Organization
How to Use This Manual
This application note is divided into the following sections.
• Control method
• Software configuration
• Hardware configuration
• Program list
It is assumed that the reader of this application note has general knowledge in the fields
of electrical engineering, logic circuits, and microcontrollers.
Cautions 1. Application examples in this manual are intended for the “standard”
quality models for general-purpose electronic systems. When using
an example in this manual for an application that requires the
“special” quality grade, evaluate each component and circuit to be
actually used to see if they satisfy the required quality standard.
2. To use this manual for special-grade products, read the part
numbers as follows:
µPD703114 → µPD703114(A)
µPD70F3114 → µPD70F3114(A)
µPD703116 → µPD703116(A), 703116(A1)
µPD70F3116 → µPD70F3116(A), 70F3116(A1)
6
Application Note U17324EJ1V0AN
For details of hardware functions (especially register functions, setting methods, etc.)
and electrical specifications
→ See the V850E/IA1 Hardware User’s Manual, V850E/IA2 Hardware User’s
Manual, and V850E/IA3, V850E/IA4 Hardware User’s Manual.
For details of instruction functions
→ See the V850E1 Architecture User’s Manual.
Conventions
Data significance:
Higher digits on the left and lower digits on the right
Active low representation:
xxx (overscore over pin or signal name)
Memory map address:
Higher addresses on the top and lower addresses on
the bottom
Note:
Footnote for item marked with Note in the text
Caution:
Information requiring particular attention
Remark:
Supplementary information
Numeric representation:
Binary ... xxxx or xxxxB
Decimal ... xxxx
Hexadecimal ... xxxxH
Prefix indicating the power
of 2 (address space,
memory capacity):
K (kilo):
210 = 1,024
M (mega): 220 = 1,0242
Data type:
Related Documents
G (giga):
230 = 1,0243
Word:
32 bits
Halfword:
16 bits
Byte:
8 bits
The related documents indicated in this publication may include preliminary versions.
However, preliminary versions are not marked as such.
Documents related to V850E/IA1, V850E/IA2, V850E/IA3, V850E/IA4
Document Name
Document No.
V850E1 Architecture User’s Manual
U14559E
V850E/IA1 Hardware User’s Manual
U14492E
V850E/IA2 Hardware User’s Manual
U15195E
V850E/IA3, V850E/IA4 Hardware User’s Manual
U16543E
V850E/IA1, V850E/IA2 AC Motor Inverter Control Using Vector
U14868E
Operation Application Note
Inverter Control by V850 Series 120° Excitation Method Control by Zero-
U17209E
Cross Detection Application Note
Inverter Control by V850 Series Vector Control by Encoder Application
This manual
Note
Application Note U17324EJ1V0AN
7
Documents related to development tools (user’s manuals)
Document Name
Document No.
IE-V850E-MC (In-Circuit Emulator for V850E/IA1, V850E/IA2)
U14487E
IE-V850E1-CD-NW (PCMCIA Card Type On-Chip Debug Emulator for
U16647E
V850E/IA3, V850E/IA4)
IE-703116-MC-EM1 (In-Circuit Emulator Option Board for V850E/IA1)
U14700E
IE-703114-MC-EM1 (In-Circuit Emulator Option Board for V850E/IA2)
U16533E
CA850 Ver. 3.00 C Compiler Package
Operation
U17293E
C Language
U17291E
Assembly Language
U17292E
Link Directives
U17294E
PM+ Ver. 6.00
U17178E
ID850 Ver. 2.50 Integrated Debugger
Operation
U16217E
ID850NWC Ver. 2.51 Integrated Debugger
Operation
U16525E
Operation
U16973E
Operation
U16218E
SM850 Ver. 2.00 or Later System
External Part User Open
U14873E
Simulator (for V850E/IA1, V850E/IA2)
Interface Specifications
SM+ System Simulator
Operation
U17246E
User Open Interface
U17247E
Basics
U13430E
Installation
U13410E
Technical
U13431E
Basics
U13773E
Installation
U13774E
Technical
U13772E
(for V850E/IA3, V850E/IA4)
ID850QB Ver. 2.80 Integrated Debugger
(for V850E/IA3, V850E/IA4)
SM850 Ver. 2.50 System Simulator
(for V850E/IA1, V850E/IA2)
(for V850E/IA1, V850E/IA2)
RX850 Ver. 3.13 or Later Real-Time OS
RX850 Pro Ver. 3.15 Real-Time OS
8
RD850 Ver. 3.01 Task Debugger
U13737E
RD850 Pro Ver. 3.01 Task Debugger
U13916E
AZ850 Ver. 3.10 System Performance Analyzer
U14410E
PG-FP4 Flash Memory Programmer
U15260E
Application Note U17324EJ1V0AN
CONTENTS
CHAPTER 1 CONTROL METHOD..........................................................................................................11
1.1
Principle of Vector Control ...................................................................................................... 11
CHAPTER 2 HARDWARE CONFIGURATION .......................................................................................14
2.1
Configuration ............................................................................................................................ 14
2.2
Circuit Diagram ......................................................................................................................... 16
CHAPTER 3 SOFTWARE CONFIGURATION........................................................................................25
3.1
Control Block ............................................................................................................................ 25
3.2
Speed Control ........................................................................................................................... 26
3.3
Position Speed Detection by Encoder.................................................................................... 26
3.4
Current Control ......................................................................................................................... 27
3.5
Three-Phase Voltage Conversion ........................................................................................... 27
3.6
PWM Conversion ...................................................................................................................... 28
3.7
Peripheral I/O............................................................................................................................. 28
3.8
Software Processing Structure ............................................................................................... 30
3.9
Flowchart ................................................................................................................................... 32
3.9.1
Main processing ...........................................................................................................................32
3.9.2
LED display ..................................................................................................................................40
3.9.3
Motor control timer interrupt processing .......................................................................................41
3.9.4
Motor control processing ..............................................................................................................41
3.9.5
10 mSEC interval interrupt processing .........................................................................................46
3.9.6
A/D converter channel 0 interrupt processing ..............................................................................47
3.9.7
A/D converter channel 1 interrupt processing ..............................................................................48
3.9.8
Hardware initialization ..................................................................................................................49
3.9.9
Common area initialization ...........................................................................................................50
3.9.10
Revolution start initialization .........................................................................................................50
3.9.11
sin calculation...............................................................................................................................51
3.10 Common Areas ......................................................................................................................... 52
3.11 Tables......................................................................................................................................... 53
3.12 Constant Definitions................................................................................................................. 53
CHAPTER 4 PROGRAM LIST ................................................................................................................54
4.1
Program List (V850E/IA1) ......................................................................................................... 54
4.1.1
Symbol definition ..........................................................................................................................54
4.1.2
Constant definition........................................................................................................................55
4.1.3
Interrupt handler setting ...............................................................................................................57
4.1.4
Startup routine setting ..................................................................................................................59
4.1.5
Main processing function..............................................................................................................61
4.1.6
LED display function.....................................................................................................................65
4.1.7
Motor control interrupt processing function ..................................................................................67
4.1.8
10 mSEC interval interrupt processing function ...........................................................................69
4.1.9
A/D converter interrupt processing function .................................................................................70
4.1.10
Hardware initialization processing function ..................................................................................70
4.1.11
Common area initialization processing function ...........................................................................72
4.1.12
Revolution start initialization processing function .........................................................................72
Application Note U17324EJ1V0AN
9
4.2
4.3
4.4
10
4.1.13
sin calculation processing function .............................................................................................. 73
4.1.14
Link directive file for V850E/IA1................................................................................................... 73
Program List (V850E/IA2) ......................................................................................................... 75
4.2.1
Symbol definition ......................................................................................................................... 75
4.2.2
Constant definition....................................................................................................................... 76
4.2.3
Interrupt handler setting............................................................................................................... 78
4.2.4
Startup routine setting ................................................................................................................. 79
4.2.5
Main processing function ............................................................................................................. 82
4.2.6
LED display function.................................................................................................................... 86
4.2.7
Motor control interrupt processing function.................................................................................. 87
4.2.8
10 mSEC interval interrupt processing function........................................................................... 90
4.2.9
A/D converter interrupt processing function................................................................................. 91
4.2.10
Hardware initialization processing function.................................................................................. 91
4.2.11
Common area initialization processing function........................................................................... 93
4.2.12
Revolution start initialization processing function ........................................................................ 93
4.2.13
sin calculation processing function .............................................................................................. 93
4.2.14
Link directive file for V850E/IA2................................................................................................... 94
Program List (V850E/IA3) ......................................................................................................... 96
4.3.1
Symbol definition ......................................................................................................................... 96
4.3.2
Constant definition....................................................................................................................... 97
4.3.3
Interrupt handler setting............................................................................................................... 99
4.3.4
Startup routine setting ................................................................................................................100
4.3.5
Main processing function ............................................................................................................103
4.3.6
LED display function...................................................................................................................107
4.3.7
Motor control interrupt processing function.................................................................................108
4.3.8
10 mSEC interval interrupt processing function..........................................................................111
4.3.9
A/D converter interrupt processing function................................................................................112
4.3.10
Hardware initialization processing function.................................................................................112
4.3.11
Common area initialization processing function..........................................................................114
4.3.12
Revolution start initialization processing function .......................................................................114
4.3.13
sin calculation processing function .............................................................................................115
4.3.14
Link directive file for V850E/IA3..................................................................................................116
Program List (V850E/IA4) ....................................................................................................... 118
4.4.1
Symbol definition ........................................................................................................................118
4.4.2
Constant definition......................................................................................................................119
4.4.3
Interrupt handler setting..............................................................................................................121
4.4.4
Startup routine setting ................................................................................................................122
4.4.5
Main processing function ............................................................................................................125
4.4.6
LED display function...................................................................................................................129
4.4.7
Motor control interrupt processing function.................................................................................130
4.4.8
10 mSEC interval interrupt processing function..........................................................................133
4.4.9
A/D converter interrupt processing function................................................................................134
4.4.10
Hardware initialization processing function.................................................................................134
4.4.11
Common area initialization processing function..........................................................................136
4.4.12
Revolution start initialization processing function .......................................................................136
4.4.13
sin calculation processing function .............................................................................................137
4.4.14
Link directive file for V850E/IA4..................................................................................................138
Application Note U17324EJ1V0AN
CHAPTER 1 CONTROL METHOD
1.1
Principle of Vector Control
Typically, when controlling a 3-phase motor, voltage and current are indicated as 3-phase AC. However, 2-phase
AC is easier to represent than 3-phase AC. Also, control is even simpler when representing biaxial DC rather than 2phase AC.
When converting biaxial DC (d-q axes), numerous armature coils are connected to the commutator and wound in
the radial direction, similar to a DC motor such as is shown in part (c) of Figure 1-1 below. Two voltage values, vd (daxis voltage) and vq (q-axis voltage), are applied and two currents, id (d-axis current) and iq (q-axis current), flow
across the brush that is situated along the d-q axis which rotates at the same speed as the electromagnetic field.
In the reference system, the current and voltage are controlled along the d-q axis in the same way as in DC motors.
Figure 1-1. Equivalent Circuits (1/3)
(a) Equivalent circuit of 3-phase
(b) Equivalent circuit using 2-phase AC
synchronous motor
vα
vu
θr
θr
N
N
vβ
wr
wr
S
vw
Remark
S
vv
vu: u-phase voltage value
Remark
vα: α axis voltage value
vw: w-phase voltage value
vβ: β axis voltage value
θ r: Angle of revolution
θ r: Angle of revolution
N:
N pole
N: N pole
S:
S pole
S: S pole
wr: Angular velocity
vv: v-phase voltage value
wr: Angular velocity
Application Note U17324EJ1V0AN
11
CHAPTER 1 CONTROL METHOD
Figure 1-1. Equivalent Circuits (2/3)
(c) Equivalent circuit of biaxial DC type,
(d) Relationship between 3-phase and
(d-q) axes
2-phase AC coordinates
d
u θr
id
α
vd
u
N
β
q
S
iq
w
vq
Remark
u:
u phase
v:
v phase
w
v
v
Remark
u:
u phase
v:
v phase
w: w phase
w:
w phase
θ r: Angle of revolution
α:
β:
α axis
β axis
N: N pole
S: S pole
d:
d axis
id: d-axis current value
vd: d-axis voltage value
q:
q axis
iq: q-axis current value
vq: q-axis voltage value
12
Application Note U17324EJ1V0AN
CHAPTER 1 CONTROL METHOD
Figure 1-1. Equivalent Circuits (3/3)
(e) Relationship between 2-phase AC and biaxial DC coordinates
α
d
θr
β
q
Remark
α:
β:
α axis
β axis
d:
d axis
q:
q axis
θ r: Angle of revolution
Application Note U17324EJ1V0AN
13
CHAPTER 2 HARDWARE CONFIGURATION
This chapter describes the hardware configuration.
2.1
Configuration
The reference system’s main functions are described below.
In this reference system, when the revolution
specification switch is pressed after power application, the motor starts revolving in the direction specified.
Figure 2-1. Overall System Configuration
Volume for
speed control
Driver IC
6
M
Encoder
A/D converter
Timer
Current voltage
conversion
RESET
Reset circuit
Microcontroller
Encoder
timer
Current voltage
conversion
Port
Revolution
specification SW
3
LED display
13
14
WDT
Application Note U17324EJ1V0AN
CHAPTER 2 HARDWARE CONFIGURATION
(1) Volume for speed control
Volume for increasing and decreasing the number of revolutions of the motor
(2) Revolution specification SW
CW, CCW, and STOP switches
(3) LED display
LED displaying the number of revolutions, operation time, etc.
(4) WDT
Watchdog timer
(5) Driver IC
Driver for driving motor
(6) Current voltage converter
Converting the motor driving current to voltage, used for detecting overcurrent
(7) Encoder timer
Information of revolution and position from the motor
Application Note U17324EJ1V0AN
15
CHAPTER 2 HARDWARE CONFIGURATION
2.2
Circuit Diagram
Figures 2-2 to 2-5 show diagrams of the sample reference system circuit.
This sample reference system circuit diagram includes the V850E/IA1, V850E/IA2, V850E/IA3, or V850E/IA4, a
reset circuit, oscillator, a pin handling microcontroller peripheral block, operation mode switch block, LED output block,
watchdog timer circuit block, drive circuit block, motor controller, and motor revolution indicator.
(1) Microcontroller and microcontroller peripheral block
The V850E/IA1, V850E/IA2, V850E/IA3, or V850E/IA4 includes a reset circuit, an oscillator that uses a
resonator, and a block for handling the MODE pin and unused pins.
(2) Operation mode switch block
This block includes switches that set the operation mode as CW or CCW operation.
(3) LED output block
This block includes 16 LEDs, which are used to indicate the revolution speed (rpm), errors, etc.
(4) Watchdog timer circuit block
This block is specified to output stop signals when pulse output from the V850E/IA1, V850E/IA2, V850E/IA3, or
V850E/IA4 stops for one ms or longer.
(5) Drive circuit block
The 6-phase outputs from the inverter timer are converted to U-, V-, and W-phase output for the motor driver.
This drive circuit is not shown in detail in this example, since it varies depending on the motor’s specifications.
(6) Motor controller
This block includes the HPS-3-AS, LM324, and other devices that are used to measure the motor’s U and V
drive currents via A/D conversion.
(7) Motor revolution indicator
This block includes a volume adjuster and the LM324 for setting the motor’s revolution speed (rpm).
16
Application Note U17324EJ1V0AN
CHAPTER 2 HARDWARE CONFIGURATION
Figure 2-2. Circuit Diagram of V850E/IA1
1
R1
2.2KΩ
TP2
LC-2-G
[V]
LC-2-G
1
[U]
TP1
TP3
LC-2-G
[W]
1
Velocity
1
U1A
LM324
1
R8 10KΩ
R16 10KΩ
R9 10KΩ
-
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
+
2
R5
R13
R6
R14
R7
R15
3
11
3
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
4
4
VR1A
10KΩ
Speed
Cont.
R10
R2
R11
R3
R12
R4
+7V
+5V
[MOT OUT]
Mot_STOP
U2
Mot_UH
Mot_UL
1
2
Mot_VH
Mot_VL
3
4
Mot_WH
Mot_WL
5
6
1
+5V
OV
HV
LV
OW
8
9
V
10
W
1
2
3
CN 3P
HW
LW
+3.3V
R17
10KΩ
+5V
7
6
5MHz
C5
33pF
CLOCK
5MHz
R34
R36
R38
R40
R42
R43
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
CKSEL
+5V
R48
47Ω
R50
47Ω
47Ω
R55
47Ω
MODE
+5V
R63
R65
R67
R69
10KΩ
10KΩ
10KΩ
10KΩ
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
ANI07
AVDD
AVSS
AVREF1
ANI10
ANI11
ANI12
ANI13
ANI14
ANI15
ANI16
ANI17
TRIG_DBG
AD3_DBG
AD2_DBG
AD1_DBG
AD0_DBG
SYNC
CLK_DBG
RESET
CVDD
CVSS
X1
X2
CKSEL
MODE0
MODE1
MODE2
SI0/P40
SO0/P41
SCK0/P42
SI1/P43
SO1/P44
SCK1/P45
CRXD/P46
CTXD/P47
R71 R72 R73
10KΩ 10KΩ 10KΩ
SW_CW
SW_CCW
SW_STOP
SW2
SW3
SW4
KHz
uSEC
A
B
C
D
E
F
G
DP
DG1
DG2
DG3
DG4
DG5
DG6
DG7
WE
1
2
3
4
5
6
7
8
LED_Seg1
LED_Seg2
LED_Seg3
LED_Seg4
LED_Seg5
LED_Seg6
LED_Seg7
LED_Seg8
10KΩ
10KΩ
10KΩ
LED_Dig1
LED_Dig2
LED_Dig3
LED_Dig4
18
LED_WE
IN+
R18
100KΩ
+12V
AGND
R22
22KΩ
6
5
10KΩ
LM324
U1B
I_V
7
4
+5V
J2
TIUD11/TO11/P13
TCLR10/INTP101/P12
TCUD10/INTP100/P11
TIUD10/TO10/P10
PCM4
HLDRQ/PCM3
HLDAK/PCM2
CLKOUT/PCM1
WAIT/PCM0
PCT7
ASTB/PCT6
PCT5
RD/PCT4
PCT3
PCT2
UWR/PCT1
LWR/PCT0
VDD5
VSS5
VPP
CS7/PCS7
CS6/PCS6
CS5/PCS5
CS4/PCS4
CS3/PCS3
CS2/PCS2
CS1/PCS1
CS0/PCS0
A23/PDH7
A22/PDH6
A21/PDH5
A20/PDH4
A19/PDH3
A18/PDH2
A17/PDH1
A16/PDH0
108
107
106
105
104
103
102
101
100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
P108
P107
P106
P105
P104
P103
P102
P101
P100
P99
P98
P97
P96
P95
P94
P93
P92
P91
P90
P89
P88
P87
P86
P85
P84
P83
P82
P81
P80
P79
P78
P77
P76
P75
P74
P73
R24 10KΩ
Enc_Z
Enc_B
Enc_A
R25 10KΩ
R26 10KΩ
R27 10KΩ
R28
R29
R30
R32
R33
R35
R37
R39
R41
VR2
1
2
3
4
5
1KΩ
AGND
R31
2.2KΩ
+12V
HPS-3-AS
AGND
CT2
6
R45
R46
R47
R49
R51
R53
R56
R57
R58
R60
R61
R62
R64
R66
R68
R70
C4
0.1uF film
CN5P
Rotary Encoder
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
5
ININ+
+12V
NC
OUT
GND
1
2
3
4
R44
AGND
100KΩ
C6
0.1uF film
AGND
+12V
22KΩ
R54
R59
9
10
LM324
C7
0.1uF
U1C
I_U
8
10KΩ
+12V
VR3
C8
0.1uF film
1KΩ
C9
C10 C11 C12 C13 C14 C15
0.1uF 0.1uF 0.1uF 0.1uF 0.1uF 0.1uF 0.1uF
R74
AGND
2.2KΩ
C16 C17 C18
0.1uF 0.1uF 0.1uF
AGND
U6A
+12V +7V
1
CLK
WDO
10
11
12
13
14
15
16
MOT_DRV
0.1uF film
R91 10KΩ
R92 10KΩ
R93 10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
rpm
R75
R76
R77
R78
R79
R80
R81
R82
R83
R84
R85
R86
R87
R88
R89
R90
7SEGLED1
R21
C1
R23
P53
P54
P55
P56
uPD70F3116GJ
R20
+12V
NC
OUT
GND
11
C3
33pF
R19
IN-
1
2
3
4
4
SW1
R52
5
ANI06
ANI05
ANI04
ANI03
ANI02
ANI01
ANI00
AVREF0
AVSS
AVDD
TO015
TO014
TO013
TO012
TO011
TO010
VDD3
VSS3
VSS5
VDD5
TO005
TO004
TO003
TO002
TO001
TO000
INTP6/P07
INTP5/P06
INTP4/P05
ADTRG1/INTP3/P04
ADTRG0/INTP2/P03
ESO1/INTP1/P02
ESO0/INTP0/P01
NMI/P00
TCLR11/INTP111/P15
TCUD11/INTP110/P14
RESET
RXD0/P30
TXD0/P31
RXD1/P32
TXD1/P33
ASCK1/P34
RXD2/P35
TXD2/P36
ASCK2/P37
TI2/INTP20/P20
TO21/INTP21/P21
TO22/INTP22/P22
TO23/INTP23/P23
TO24/INTP24/P24
TCLR2/INTP25/P25
TI3/INTP30/TCLR3/P26
TO3/INTP31/P27
VDD3
VSS3
VSS5
VDD5
AD0/PDL0
AD1/PDL1
AD2/PDL2
AD3/PDL3
AD4/PDL4
AD5/PDL5
AD6/PDL6
AD7/PDL7
AD8/PDL8
AD9/PDL9
AD10/PDL10
AD11/PDL11
AD12/PDL12
AD13/PDL13
AD14/PDL14
AD15/PDL15
C2
47uF
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
RESET
SW
144
143
142
141
140
139
138
137
136
135
134
133
132
131
130
129
128
127
126
125
124
123
122
121
120
119
118
117
116
115
114
113
112
111
110
109
74ACT14
74ACT14
+12V
HPS-3-AS
CT1
ERR
4
-
3
2
+
1
11
+5V
U3B
U3A
-
2
OU
U
+
D1
J1
HU
LU
WD_CLR
2
3
13
W_CLR
ERR
WDT
Watch Dog & Error
+5V
+3.3V
J3
1
2
3
4
5
6
CON6
-7V
uSEC
POWER
7SEG_LEDx8
Application Note U17324EJ1V0AN
17
CHAPTER 2 HARDWARE CONFIGURATION
Figure 2-3. Circuit Diagram of V850E/IA2
1
Velocity
R1
2.2KΩ
TP2
LC-2-G
[V]
LC-2-G
1
[U]
TP1
TP3
LC-2-G
[W]
1
U1A
LM324
1
1
-
10KΩ
10KΩ
10KΩ
10KΩ
+
2
R8
R9
R10
R11
3
11
3
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
VR1A
10KΩ
Speed
Cont.
R2
R3
R4
R5
R6
R7
+7V
4
4
+5V
[MOT OUT]
U2
Mot_VH
Mot_VL
3
4
Mot_WH
Mot_WL
5
6
1
+5V
OW
V
10
W
1
2
3
CN 3P
4
6
74ACT14
74ACT14
RESET
SW
+3.3V
5
MOT_DRV
+5V
C2
47uF
+12V
HPS-3-AS
CT1
ERR
U3B
3
2
9
HW
LW
7
U3A
1
OV
HV
LV
8
ININ+
+12V
NC
OUT
GND
C1
1
2
3
4
+5V
R26
47Ω
MODE0
CKSEL
R35
47Ω
+5V
R42 R43 R44
10KΩ 10KΩ 10KΩ
uPD70F3114GC
SW_CW
SW2
SW_CCW
SW3
SW_STOP
SW4
ESO0/INTP0/P01
NMI/P00
TCLR10/INTP101/P12
TCUD10/INTP100/P11
TIUD10/TO10/P10
PCM1/CLKOUT
PCM0/WAIT
PCT6/ASTB
PCT4/RD
PCT1/UWR
PCT0/LWR
VDD
VSS3
MODE1/VPP
PDH5/A21
PDH4/A20
PDH3/A19
PDH2/A18
PDH1/A17
PDH0/A16
PDL15/AD15
PDL14/AD14
PDL13/AD13
PDL12/AD12
PDL11/AD11
11
+5V
5
I_V
7
4
10KΩ
LM324
U1B
R16
R17
VR2
R18
+5V
10KΩ 10KΩ 10KΩ
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
Mot_Stop
1
2
3
4
5
Enc_Z
Enc_B
Enc_A
R21
R22
R23
R24
R25
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
2.2KΩ
Rotary Encoder
+12V
HPS-3-AS
AGND
CT2
R27
R29
R30
R31
R32
R33
R34
6
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
AGND
R20
CN5P
MOD E1
C4
0.1uF film
1KΩ
J2
R19 10KΩ
5
ININ+
+12V
NC
OUT
GND
1
2
3
4
100KΩ
C6
0.01uF film
AGND
+12V
22KΩ
R36
R37 10KΩ
R38 10KΩ
R39 10KΩ
R28
AGND
11
CLOCK
4MHz
ANI05
AVDD1
AVSS1
ANI10
ANI11
ANI12
ANI13
ANI14
ANI15
ANI16
ANI17
MODE0
VSS3
RVDD
REGOUT
REGIN
X1
X2
RESET
CVSS
CKSEL
SI0/P40
SO0/P41
SCK0/P42
RXD0/P30
6
R40
9
10
LM324
C7
0.1uF
U1C
I_U
8
4
C5
33pF
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
TXD0/P31
SI1/RXD1/P32
SO1/TXD1/P33
SCK1/ASCK1/P34
TI2/INTP20/P20
TO21/INTP21/P21
TO22/INTP22/P22
TO23/INTP23/P23
TO24/INTP24/P24
TCLR2/INTP25/P25
TI3/INTP30/TCLR3/P26
TO3/INTP31/P27
VSS
VDD
PDL0/AD0
PDL1/AD1
PDL2/AD2
PDL3/AD3
PDL4/AD4
PDL5/AD5
PDL6/AD6
PDL7/AD7
PDL8/AD8
PDL9/AD9
PDL10/AD10
4MHz
22KΩ
10KΩ
C8
C9
C10 C11 C12 C13
0.1uF 0.1uF 0.1uF 0.1uF 0.1uF 0.1uF
+12V
VR3
C14
0.1uF film
1KΩ
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
C3
33pF
R14
R15
100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
ANI04
ANI03
ANI02
ANI01
ANI00
AVSS0
AVDD0
TO015
TO014
TO013
TO012
TO011
TO010
VSS
VDD
TO005
TO004
TO003
TO002
TO001
TO000
INTP4/TO3OFF/P05
ADTRG1/INTP3/P04
ADTRG0/INTP2/P03
ESO1/INTP1/P02
U4
100KΩ
R13
AGND
SW1
0.01uF film
+12V
RESET
-
2
R12
10KΩ
OU
U
+
D1
J1
HU
LU
-
1
2
+
Mot_UH
Mot_UL
R45
AGND
C15
0.1uF
2.2KΩ
R41
R46
R47
R48
R49
R50
R51
R52
R53
R54
R55
R56
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
AGND
7SEGLED1
rpm
KHz
uSEC
A
B
C
D
E
F
G
DP
DG1
DG2
DG3
DG4
DG5
DG6
DG7
WE
1
2
3
4
5
6
7
8
LED_Seg1
LED_Seg2
LED_Seg3
LED_Seg4
LED_Seg5
LED_Seg6
LED_Seg7
LED_Seg8
U5A
+12V +7V
1
10
11
12
13
14
15
16
LED_Dig1
LED_Dig2
LED_Dig3
LED_Dig4
CLK
WDO
WD_CLR
2
3
18
13
W_CLR
ERR
WDT
Watch Dog & Error
+5V
+3.3V
J3
1
2
3
4
5
6
CON6
-7V
uSEC
POWER
7SEG_LEDx8
Application Note U17324EJ1V0AN
19
CHAPTER 2 HARDWARE CONFIGURATION
Figure 2-4. Circuit Diagram of V850E/IA3
R2
2.2KΩ
[U]
TP1
TP2
LC-2-G
[V]
LC-2-G
TP3
LC-2-G
[W]
1
1
Velocity
1
-
U1A
LM324
1
1
+
2
11
3
R4 10KΩ
R5 10KΩ
R6 10KΩ
4
VR1A
10KΩ
3
Speed
Cont.
R3 10KΩ
+7V
FLMD0
4
+5V
[MOT OUT]
U2
Mot_UH
Mot_UL
+7V
5
+
6
-
1
3
4
Mot_WH
Mot_WL
5
6
OV
OW
8
U
9
V
10
W
1
2
3
CN 3P
HW
LW
7
R1
2.2KΩ
J1
OU
HV
LV
U1B
LM324
7
11
2
Mot_VH
Mot_VL
HU
LU
+12V
HPS-3-AS
CT1
ERR
LED_Dig4
LED_Dig3
Comp.
Ref.
4
VR1B
10KΩ
LED_WE
5
+5V
1
2
6
5
MOT_DRV
ININ+
+12V
NC
OUT
GND
C1
0.01uF film
1
2
3
4
100KΩ
R7
+12V
+2.5V
22KΩ
R9
9
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
10
10KΩ
I_V
8
U4B
2
3
74ACT14
RESET
SW
C3
47uF
4
74ACT14
R19
R21
RESET
SW1
R22
R23
R25
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
P04/INTP4/ADTRG0
P05/INTP5/ADTRG1
P06/INTP6
P07/INTP7
VDD
VSS
P40/SIB0
P41/SOB0
P42/SCKB0
EVDD
EVSS
CVDD
X2
X1
CVSS
RESET
P43/TOP00/TIP00
P44/TOP01/TIP01
VSS
VDD
R28
R31
10KΩ
10KΩ
+5V
R33 R34 R35
10KΩ 10KΩ 10KΩ
SW_CW
SW_CCW
SW_STOP
uPD70F3184GC
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
LED_Dig2
LED_Dig1
LED_Seg8
LED_Seg7
LED_Seg6
LED_Seg5
LED_Seg4
LED_Seg3
C2
0.1uF film
+5V
R13
R16
R17
AGND
2.2KΩ
R18
+12V
HPS-3-AS
+5V
10KΩ 10KΩ 10KΩ
LED_Seg2
LED_Seg1
R24
R26
R27
R29
1
2
3
4
5
10KΩ
10KΩ
10KΩ
10KΩ
AGND
CT2
J2
CN5P
6
5
ININ+
+12V
NC
OUT
GND
1
2
3
4
100KΩ
R20
AGND
C4
0.01uF film
AGND
+12V
22KΩ
R30
13
Rotary Encoder
R32
12
LM324
C5
0.1uF
U1D
I_U
14
10KΩ
C6
C7
C8
0.1uF 0.1uF 0.1uF
+12V
VR3
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
U4A
1
10KΩ
10KΩ
10KΩ
10KΩ
1KΩ
PDL9
PDL8
PDL7
PDL6
PDL5/FLMD1
PDL4
PDL3
PDL2
VSS
VDD
PDL1
PDL0
P37/TCLR10
P36/TCUD10
P35/TIUD10/TO10
P34/SCKB1
P33/SOB1/TXDA1
P32/SIB1/RXDA1
P31/TXDA0
P30/RXDA0
11
R11
R12
R14
R15
R10
10KΩ
ANI00
ANI01
P70/ANI20
P71/ANI21
P72/ANI22
P73/ANI23
AVDD
AVSS
CMPREF
AVSS
AVDD
P74/ANI24
P75/ANI25
ANI10
ANI11
ANI12
ANI13
P00/INTP0/TOQ0OFF
P02/INTP2/TOP2OFF
P03/INTP3
4
1
D1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
VR2
-
+5V
+
PLLSIN
P17/TOP21/TI P21
P16/TOQ00/TIP20
P15/TOQ0B3/TRGQ0
P14/TOQ0T3/EVTQ0
P13/TOQ0B2/TIQ00
EVDD
EVSS
P12/TOQ0T2/TIQ03/TOQ03
P11/TOQ0B1/TIQ02/TOQ02
P10/TOQ0T1/TIQ01/TOQ01
FLMD0
VDD
VSS
PDL15
PDL14
PDL13
PDL12
PDL11
PDL10
4
U3
LM324
U1C
-
R8
+
AGND
11
+5V
C10 C11 C12
0.1uF 0.1uF 0.1uF
C9
0.1uF film
1KΩ
R36
SW2
AGND
SW3
2.2KΩ
SW4
R37
R38
R39
R40
10KΩ
10KΩ
10KΩ
10KΩ
R41 10KΩ
R42 10KΩ
AGND
7SEGLED1
rpm
KHz
uSEC
A
B
C
D
E
F
G
DP
DG1
DG2
DG3
DG4
DG5
DG6
DG7
WE
1
2
3
4
5
6
7
8
LED_Seg1
LED_Seg2
LED_Seg3
LED_Seg4
LED_Seg5
LED_Seg6
LED_Seg7
LED_Seg8
10
11
12
13
14
15
16
LED_Dig1
LED_Dig2
LED_Dig3
LED_Dig4
18
LED_WE
8MHz
C13
C14
15pF
15pF
U5A
+12V +7V
1
CLK
WDO
WD_CLR
2
3
13
W_CLR
ERR
WDT
Watch Dog & Error
+5V
+3.3V
J3
1
2
3
4
5
6
-7V
CON6
uSEC
POWER
7SEG_LEDx8
Application Note U17324EJ1V0AN
21
CHAPTER 2 HARDWARE CONFIGURATION
Figure 2-5. Circuit Diagram of V850E/IA4
1
2
1
[MOT OUT]
Mot_VH
Mot_VL
3
4
Mot_WH
Mot_WL
5
6
R1
2.2KΩ
OU
HV
LV
OV
OW
LED_WE
8
U
9
V
10
W
1
2
3
CN 3P
HW
LW
7
+5V
J1
HU
LU
6
+2.5V
5
MOT_DRV
ININ+
+12V
NC
OUT
GND
1
+5V
3
RESET
SW
4
74ACT14
RESET
C2
47uF
SW1
R19
R20
R21
R22
R24
R25
R29
R31
R32
R33
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
10KΩ
R36 10KΩ
R38 10KΩ
R18 10KΩ
+5V
R49 R50 R51
10KΩ 10KΩ 10KΩ
uPD70F3186GC
SW_STOP
R16
R17
100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
ANI00
ANI01
ANI02
ANI03
P70/ANI20
P71/ANI21
P72/ANI22
P73/ANI23
AVDD
AVSS
CMPREF
AVSS
AVDD
P74/ANI24
P75/ANI25
P76/ANI26
P77/ANI27
ANI10
ANI11
ANI12
ANI13
P00/INTP0/TOQ0OFF
P01/INTP1/TOQ1OFF
P02/INTP2/TOP2OFF
P03/INTP3/TOP3OFF
P04/INTP4/ADTRG0
P05/INTP5/ADTRG1
P06/INTP6
P07/INTP7
VDD
VSS
P40/SIB0
P41/SOB0
P42/SCKB0
P20/TOQ1T1
P21/TOQ1B1
P22/TOQ1T2
EVDD
EV SS
P23/TOQ1B2
P24/TOQ1T3
P25/TOQ1B3
CVDD
X2
X1
CVSS
RESET
P43/TOP00/TIP00
P44/TOP01/TI P01
VDD
R43 10KΩ
R45 10KΩ
R47 10KΩ
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
100KΩ
22KΩ
9
10
10KΩ
LM324
U1C
I_V
8
4
2
74ACT14
SW_CCW
R14
VR2
PDL9
PLD8
EVDD
EVSS
PDL7
PDL6
PDL5/FLMD1
PDL4
PDL3
PDL2
VSS
VDD
PDL1
PDL0
P27/TOP31
P26/TOQ10
P37/TCLR10
P36/TCUD10
P35/TIUD10/TO10
P34/SCKB1
P33/SOB1/TXDA1
P32/SIB1/RXDA1
P31/TXDA0
P30/RXDA0
VSS
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
LED_Dig2
LED_Dig1
C3
0.1uF film
1KΩ
LED_Seg8
LED_Seg7
LED_Seg6
LED_Seg5
LED_Seg4
LED_Seg3
R23
AGND
+5V
2.2KΩ
HPS-3-AS
R27
R26
R28
LED_Seg2
LED_Seg1
CT2
+5V
10KΩ 10KΩ 10KΩ
R35 10KΩ
R37 10KΩ
6
10KΩ
10KΩ
10KΩ
10KΩ
IN-
J2
1
2
3
4
5
Enc_Z
Enc_B
Enc_A
R39
R40
R42
R44
+12V
5
IN+
AGND
+12V
NC
OUT
GND
1
2
3
4
AGND
R30
100KΩ
C4
0.01uF film
AGND
+12V
22KΩ
R41
13
CN5P
R46
12
Rotary Encoder
R48 10KΩ
LM324
U1D
C5
0.1uF
I_U
14
4
1
U4B
PLLSIN
DRST
DDO
P17/TOP21/TIP21
P16/TOQ00/TIP20
P15/TOQ0B3/TRGQ0
P14/TOQ0T3/EVTQ0
P13/TOQ0B2/TIQ00
EVDD
EVSS
P12/TOQ0T2/TIQ03/TOQ03
P11/TOQ0B1/TIQ02/TOQ02
P10/TOQ0T1/TIQ01/TOQ01
FLMD0
VDD
VSS
P52/DMS/TCLR11
P51/DCK/TCUD11
P50/DDI/TIUD11/TO11
PDL15
PDL14
PDL13
PDL12
PDL11
PDL10
U3
0.01uF film
+12V
AGND
R15
10KΩ
U4A
SW_CW
C1
1
2
3
4
10KΩ
+12V
VR3
C10 C11
C6
C7
C8
C9
0.1uF 0.1uF 0.1uF 0.1uF 0.1uF 0.1uF
C12
0.1uF film
1KΩ
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
2
D1
+12V
HPS-3-AS
CT1
ERR
11
U1B
LM324
7
11
TP3
LC-2-G
[W]
11
-
LC-2-G
U2
Mot_UH
Mot_UL
LED_Dig4
LED_Dig3
+
6
TP2
LC-2-G
[V]
1
R11 10KΩ
R12 10KΩ
R13 10KΩ
R3
R4
R5
R6
4
5
1
2
TP1
1
1
11
FLMOD0
[U]
+7V
VR1B
10KΩ
Comp.
Ref.
R2
2.2KΩ
R8 10KΩ
R9 10KΩ
R10 10KΩ
Velocity
-
+5V
5
U1A
LM324
1
R7 10KΩ
+
2
10KΩ
10KΩ
10KΩ
10KΩ
4
3
-
3
+
VR1A
10KΩ
Speed
Cont.
-
4
+7V
+
+5V
R52
SW2
AGND
SW3
C13
0.1uF
SW4
2.2KΩ
R62 10KΩ
R63 10KΩ
R60 10KΩ
R61 10KΩ
R34 10KΩ
7SEGLED1
R57 10KΩ
R58 10KΩ
R59 10KΩ
R53
R54
R55
R56
10KΩ
10KΩ
10KΩ
10KΩ
AGND
Mot_Stop
rpm
KHz
uSEC
A
B
C
D
E
F
G
DP
DG1
DG2
DG3
DG4
DG5
DG6
DG7
WE
1
2
3
4
5
6
7
8
LED_Seg1
LED_Seg2
LED_Seg3
LED_Seg4
LED_Seg5
LED_Seg6
LED_Seg7
LED_Seg8
10
11
12
13
14
15
16
LED_Dig1
LED_Dig2
LED_Dig3
LED_Dig4
18
LED_WE
8MHz
C14
C15
15pF
15pF
U5A
+12V +7V
1
CLK
WDO
WD_CLR
2
3
13
W_CLR
ERR
WDT
Watch Dog & Error
+5V
+2.5V
J3
1
2
3
4
5
6
-7V
CON6
uSEC
POWER
7SEG_LEDx8
Application Note U17324EJ1V0AN
23
CHAPTER 3 SOFTWARE CONFIGURATION
3.1
Control Block
In the reference system, processing of timer interrupts is used to calculate control along the d-q axis (as is shown
in Figure 3-1) and final output of the U, V, and W phase voltage values is performed by the inverter timer function of
the target microcontroller.
Figure 3-1. Software Control Block Diagram of Reference System
Coordinate
converter
id
iq
vd
Coordinate
converter
u
<2>
+
−
Speed
controller
+
−
Current
controller
vq
v
Driver
Motor
Encoder
w
4× frequency multiplier
−
Position
controller
iv
<3>
<1>
Target
position
Target
speed
A/D
converter
θr
Current
controller
+
iu
PWM
converter
×4
A/D
converter
Σ
<1> If a target position has been set, speed conversion is performed based on the differential between the
target position and the current position.
<2> The target current value is calculated based on the differential between the target speed (previously set or
calculated) and the current speed.
<3> The target voltage for the d-q axis is calculated based on the current return value converted for the d-q
axis and the target current value.
Remark
All processing enclosed in the solid line is internal to the target microcontroller.
Processing enclosed in the broken lines is performed by software.
Application Note U17324EJ1V0AN
25
CHAPTER 3 SOFTWARE CONFIGURATION
3.2
Speed Control
In the reference system, PI (Proportion, Integral) control is used in the speed control block. The equations used for
speed control are shown below.
d_speed = o_speed − now_speed
o_iqp
= ksp × d_speed
o_iqi
= o_iqi (n − 1) + (ksi × d_speed (n − 1))
o_iq
= o_iqp + o_iqi (n)
Remark
3.3
d_speed:
Difference between target speed and current speed
o_speed:
Target speed
now_speed:
Current speed
o_iqp:
Speed proportion component current value
ksp:
Speed proportion constant
o_iqi:
Speed integral component current value
ksi:
Speed integral constant
o_iq:
Target current value
n:
Current component
n−1:
Previous component
Position Speed Detection by Encoder
For ×4 frequency multiplication, the encoder uses a timer for 2-phase encoder input. Absolute positions along the
motor axes must be detected, so encoder values are cleared in the Z phase and absolute positions are detected.
26
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
3.4
Current Control
For current control, the d-axis current (id) and q-axis current (iq) are converted via the following equations to obtain
a target voltage for each axis.
o_vd = ki × (−id)
o_vq = ki × (o_iq − iq)
Remark
o_vd: Target d-axis voltage
ki:
Current proportion gain
id:
d-axis current value
o_vq: Target q-axis voltage
o_iq:
Target current value
iq:
q-axis current value
id and iq are obtained by converting current values for the u and v phases to d-q axis coordinates. The equations
are shown below.
id = iv × sinθ r − iu × sin (θ r − 2π/3)
iq = iv × cosθ r − iu × cos (θ r − 2π/3)
Remark
id: d-axis current value
iq: q-axis current value
iu: u-phase current value
iv: v-phase current value
θ r: Angle of revolution
3.5
Three-Phase Voltage Conversion
The equations used to convert voltage values (vd and vq) calculated for the d-q axis to 3-phase coordinates are
shown below.
o_vu = o_vd × cosθ r − o_vq × sinθ r
o_vv = o_vd × cos (θ r − 2π/3) − o_vq × sin (θ r − 2π/3)
o_vw = −o_vu − o_vv
Remark
o_vu: Target u-phase voltage
o_vv: Target v-phase voltage
o_vw: Target w-phase voltage
o_vd: Target d-axis voltage
o_vq: Target q-axis voltage
θ r:
Angle of revolution
Application Note U17324EJ1V0AN
27
CHAPTER 3 SOFTWARE CONFIGURATION
3.6
PWM Conversion
The calculated target voltage is output by an inverter timer.
3.7
Peripheral I/O
The following types of peripheral I/O functions are used in this reference system.
Table 3-1. List of Peripheral I/O Functions
Function
28
Peripheral I/O
Peripheral I/O
Peripheral I/O
Peripheral I/O
Function Name
Function Name
Function Name
Function Name
(V850E/IA1)
(V850E/IA2)
(V850E/IA3)
(V850E/IA4)
Timer Q0 (TMQ0)
Inverter timer
Timer 00 (TM00)
Timer 00 (TM00)
Timer Q0 (TMQ0)
10 ms timer
Timer 21 (TM21)
Timer 21 (TM21)
Timer P0 (TMP0)
Timer P0 (TMP0)
Motor control timer
Timer 3 (TM3)
Timer 3 (TM3)
Timer P1 (TMP1)
Timer P1 (TMP1)
Time measuring timer
Timer 3 (TM3)
Timer 3 (TM3)
Timer P1 (TMP1)
Timer P1 (TMP1)
U-phase current
ANI00
ANI00
ANI00
ANI00
V-phase current
ANI10
ANI10
ANI10
ANI10
Setting speed (volume)
ANI01
ANI01
ANI01
ANI01
A-phase encoder
TIUD10
TIUD10
TIUD10
TIUD10
B-phase encoder
TCUD10
TCUD10
TCUD10
TCUD10
Z-phase encoder
TCLR10
TCLR10
TCLR10
TCLR10
CW key input
P40
P40
P40
P40
CCW key input
P41
P41
P41
P41
STOP key input
P42
P42
P42
P42
WDT reset output
P43
P30
P30
P30
LED output
PDL0 to PDL11,
PDL0 to PDL11,
PDL0 to PDL11,
PDL0 to PDL11,
PDL15
PDL15
PDL15
PDL15
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
(1) Description of peripheral I/O functions
(a) Inverter timer
Inverter timers are used to output PWM waveforms.
In this reference system, the settings are as shown below.
• 20 kHz symmetrical triangular waveform mode
• Dead time: 6 µs
• Inverter timer output: Low active
• When ESO0 or TOQ0OFF pin input is at high level, PWM output is stopped.
(b) Motor control timer
Motor control timers are used to issue interrupts at a 50 µs interval.
(c) 10 ms timer
10 ms timers are used to issue interrupts at a 10 ms interval.
(d) Speed measuring timer
Used for measuring the revolution speed of the motor.
(e) Current value input
ANI00: U-phase current value (−5 to +5 A)
ANI10: V-phase current value (−5 to +5 A)
(f) Speed specification volume value input
ANI01 is used to input a value from 0 to 1,023.
Application Note U17324EJ1V0AN
29
CHAPTER 3 SOFTWARE CONFIGURATION
3.8
Software Processing Structure
The software processing structure is shown below.
Figure 3-2. Main Processing Structure
CW
revolution
processing
CW
revolution operation
Operation
(STOP CW)
Processing
during
deceleration
Operation
(STOP CCW)
Detection time
processing
STOP processing
30
Application Note U17324EJ1V0AN
CCW
revolution
processing
CCW
revolution operation
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-3. Interrupt Processing Structure
(a) A/D conversion start processing
(b) Capturing A/D conversion data and
motor control processing
(c) Wait timer processing
A/D conversion end
interrupt
Motor control timer
interrupt
A/D conversion
start
processing
Capturing
A/D conversion
data and motor
control
processing
Wait timer
processing
10 ms interval interrupt
The status of the operation mode switch is monitored by the main processing, and processing is transferred to CW,
CCW, and stop status. The motor is controlled in the specified status by using a motor control timer interrupt.
There are the following three motor control statuses.
• Stop status
The motor is not controlled.
• Initial operation status
Revolution control is performed until the position information from the encoder is input.
• Speed control status
Feedback revolution control is performed so that the indication speed is attained.
Application Note U17324EJ1V0AN
31
CHAPTER 3 SOFTWARE CONFIGURATION
3.9
3.9.1
Flowchart
Main processing
Figure 3-4 shows the flowchart of the main processing.
32
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-4. Main Processing
START
B
: Refer to 4.1.5, 4.2.5, 4.3.5, 4.4.5 Main processing function.
hinit
(hardware initialization)
: Refer to Figure 3-19 Hardware Initialization.
ainit
(used area initialization)
: Refer to Figure 3-20 Common Area Initialization.
Enable interrupts
Indication speed calculation
by volume
SW CW pressed?
No
Yes
SW CCW pressed?
SW mode = CW
No
Yes
SW mode = CCW
SW STOP pressed?
No
Yes
SW mode = STOP
Case?
(case 0 to case 6)
0
1
2
3
4
5
6
Application Note U17324EJ1V0AN
33
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-5. Case 0 (Processing During Stoppage)
0
No
SW mode = CW?
Yes
Disable interrupts
Disable interrupts
Initialize target speed and revolution
start
Initialize target speed and revolution
start
Enable interrupts
Enable interrupts
禁
Processing number 1
Processing number 4
A
34
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-6. Case 1 (CW Acceleration Processing)
1
No
Initial operation ends with
acceleration/deceleration counter = 0?
Yes
Re-set acceleration/deceleration counter
No
Target speed < Indication speed?
Yes
Increase target speed
Set speed monitor start timer to 5 seconds
No
Target speed > Indication speed?
Yes
Decrease target speed
Set speed monitor start timer to 5 seconds
Processing number 2
SW mode = CCW or STOP?
No
Yes
Processing number 3
A
Application Note U17324EJ1V0AN
35
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-7. Case 2 (CW Constant-Speed Processing)
2
Indication speed as target speed
No
SW mode = CCW or STOP?
Yes
Processing number 3
A
Figure 3-8. Case 3 (CW Stop Processing)
3
No
Acceleration/deceleration counter = 0?
Yes
Re-set acceleration/deceleration counter
Target speed > Indication speed?
No
Yes
Decrease target speed
Set speed monitor start time to 5 seconds
Set stop flag to on and processing
number to 0
A
36
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-9. Case 4 (CCW Acceleration Processing)
4
No
Initial operation ends with
acceleration/deceleration counter = 0?
Yes
Re-set acceleration/deceleration counter
No
Target speed < Indication speed?
Yes
Target speed > Indication speed?
Increase target speed
No
Yes
Set speed monitor start time to 5 seconds
Decrease target speed
Set speed monitor start time to 5 seconds
Processing number 5
SW mode = CCW or STOP?
No
Yes
Processing number 6
A
Application Note U17324EJ1V0AN
37
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-10. Case 5 (CCW Constant-Speed Processing)
5
Target speed = (– indication speed)
No
SW mode = CW or STOP?
Yes
Processing number 6
A
Figure 3-11. Case 6 (CCW Stop Processing)
6
No
Acceleration/deceleration counter = 0?
Yes
Re-set acceleration/deceleration counter
Target speed < (− indication speed)?
No
Yes
Increase target speed
Set stop flag to on and processing
Set speed monitor start time to 5 seconds
number to 0
A
38
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-12. LED Display Processing
A
No
Display average counter = 0?
Yes
Set display average counter to 100
led_num
(Present average speed
indication)
led_num
(Control period indication)
led_num
(Interrupt processing
time indication)
led_num
(Operation processing
time indication)
Error flag on?
No
Yes
B
7-segment display off
500 ms?
No
Yes
Error number 1?
No
Yes
“1” displayed on 7-segment
No
“2” displayed on 7-segment
500 ms?
Yes
Application Note U17324EJ1V0AN
39
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.2
LED display
Figure 3-13. LED Display
led_num
Area 1?
: Refer to 4.1.6, 4.2.6, 4.3.6, 4.4.6 LED display function.
No
Yes
Display 4 digits of area 1
Revolution
speed
No
Area 2?
Yes
Display 2 digits of area 2
Control
period
Area 3?
No
Yes
Control P processing time
Display 2 digits of area 3
Control operation processing time
EXIT
40
Application Note U17324EJ1V0AN
Display 2 digits of area 4.
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.3
Motor control timer interrupt processing
Figure 3-14. Motor Control Timer Interrupt Processing
: Refer to 4.1.7, 4.2.7, 4.3.7, 4.4.7 Motor
control interrupt processing function.
int_MOTOR
Start conversion for A/D converters 0 and 1
EXIT
3.9.4
Motor control processing
Figure 3-15. Control Interrupt Processing (1/5)
Motor_CONT
: Refer to 4.1.7, 4.2.7, 4.3.7, 4.4.7 Motor
control interrupt processing function.
Read encoder value
Calculate difference with previous
encoder
No
Difference absolute value > 1/2
revolution?
Yes
Difference < 0?
No
Yes
Set difference + 1 revolution as difference
Set difference − 1 revolution as difference
Store current encoder value in previous area
1
Application Note U17324EJ1V0AN
41
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-15. Control Interrupt Processing (2/5)
1
Calculate current speed from difference
Calculate electrical revolution speed
Speed control processing
No
Stop or error?
Yes
Get display timer
4
Convert target speed from rpm value to
radian
Calculate difference between target and
current values
Calculate current value from speed
Update speed integral value
2
Remarks 1. Electrical revolution speed = Speed × Number of poles
2. Current value = (KSP × Speed difference + Speed integral value)/KSIGETA
3. Speed integral value = Previous speed integral value + KSI × Speed difference
4. KSI:
Speed integral constant
KSIGETA: Speed integral constant jack-up constant
KSP:
42
Speed proportion constant
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-15. Control Interrupt Processing (3/5)
2
Current control processing
Convert from U- and V-phase current
values to current values for d and q axes
Calculate current values for d and q axes
Calculate voltage value for d axis
Calculate voltage value for q axis
3-phase voltage conversion processing
No
Initial revolution?
Yes
Update target position to next position
Set voltage values for d and q axes
Target position > 2 turns?
No
Yes
Initial flag off
3
Remarks 1. Voltage value for d axis = KI × (−d-axis current value)/KIGETA
2. Voltage value for q axis = KI × (Target current value for q axis − Current value for q axis)/KIGETA
3. KI:
Current conversion constant
KIGETA: Current conversion constant jack-up constant
Application Note U17324EJ1V0AN
43
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-15. Control Interrupt Processing (4/5)
3
Calculate voltage values for U, V, and W
phases
Calculate vector control operation time
PWM conversion output processing
Reset watchdog timer
All phases active, clear PWM output
prohibition
Convert to PWM output value
PWM output
EXIT
44
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
Figure 3-15. Control Interrupt Processing (5/5)
4
PWM output off
Clear current speed
Clear display time
EXIT
Application Note U17324EJ1V0AN
45
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.5
10 mSEC interval interrupt processing
Figure 3-16. 10 mSEC Interval Interrupt Processing
int_ETC
: Refer to 4.1.8, 4.2.8, 4.3.8, 4.4.8
10 mSEC interval interrupt processing function.
No
Wait counter = 0?
Yes
Wait counter – 1
Acceleration/deceleration counter = 0?
No
Acceleration/deceleration counter – 1
Yes
Display counter = 0?
Yes
No
Display counter – 1
EXIT
46
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.6
A/D converter channel 0 interrupt processing
Figure 3-17. A/D Converter Channel 0 Interrupt Processing
int_AD0
: Refer to 4.1.9, 4.2.9, 4.3.9, 4.4.9 A/D converter interrupt processing
function.
Read U phase current value
No
| Current value | > MAX_I?
Yes
U, V, W phase output off
Set error flag
Read V phase current value
No
I Current value I > MAX_I?
Yes
U, V, W phase output off
Set error flag
EXIT
Remark
MAX_I: Maximum current value
Application Note U17324EJ1V0AN
47
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.7
A/D converter channel 1 interrupt processing
Figure 3-18. A/D Converter Channel 1 Interrupt Processing
: Refer to 4.1.9, 4.2.9, 4.3.9, 4.4.9 A/D converter
interrupt processing function.
int_AD1
Read volume value
EXIT
48
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.8
Hardware initialization
Figure 3-19. Hardware Initialization
hinit
: Refer to 4.1.10, 4.2.10, 4.3.10, 4.4.10 Hardware
initialization processing function.
Initialization of port used
7-segment LED extinguished
Set 10 ms to 10 ms timer
Set to interval interrupt
Set motor control timer to
interrupt of motor sampling interval
Initialization of encoder timer
Initialization of PWM timer
All phases active
A/D initialization
EXIT
Application Note U17324EJ1V0AN
49
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.9
Common area initialization
Figure 3-20. Common Area Initialization
: Refer to 4.1.11, 4.2.11, 4.3.11, 4.4.11 Common area
initialization processing function.
ainit
• Error flag off
• Initial flag off
• Clear display counter
• Stop flag on
• Target speed = 0
• Speed integral value = 0
• Set previous encoder value
EXIT
3.9.10 Revolution start initialization
Figure 3-21. Revolution Start Initialization
: Refer to 4.1.12, 4.2.12, 4.3.12, 4.4.12 Revolution
start initialization processing function.
start_init
Clear average speed buffer
Initialize average speed calculation area
Output initial revolution pattern
Initialize processing area
EXIT
50
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
3.9.11 sin calculation
Figure 3-22. sin Calculation
: Refer to 4.1.13, 4.2.13, 4.3.13, 4.4.13 sin
calculation processing function.
sin
Normalize radian value to 0 or more
Calculate sin in 90° units
EXIT
Application Note U17324EJ1V0AN
51
CHAPTER 3 SOFTWARE CONFIGURATION
3.10 Common Areas
The following table shows the major common areas used by the reference system.
Table 3-2. Common Area List
Symbol
error_flag
Type
unsigned char
Usage
Error flag
Set Value
0:
No error
ERR_NO1: Overcurrent
ERR_NO2: Speed difference error
init_flag
unsigned char
Indicates initial revolution
ON: Initial revolution in progress
OFF: Stop or normal revolution in progress
cont_time
unsigned short
Interrupt processing time
1 µs units
cont_time1
unsigned short
Vector operation time
1 µs units
disp_co
unsigned short
Average speed counter for display
volume
unsigned short
Speed volume value
timer_count
unsigned short
Time wait counter
10 ms units
accel_count
unsigned short
Acceleration/deceleration operation time
counter
10 ms units
stop_flag
unsigned char
Stop flag
ON: Stopped
OFF: Revolving
before_posi[21][2]
signed short
Position buffer
total_sa
signed short
Position total difference
sum_speed
signed int
Total value area for average speed
0, 1, …
calculation
speed_co
signed int
Counter for average speed calculation
0, 1, …
now_speed
signed int
Present speed
rpm
object_speed
signed int
Target speed
rpm
d_speed
unsigned int
Display speed
rpm
iua
signed short
U-phase current
iva
signed short
V-phase current
o_iqai
signed int
Speed integral value
base_position
signed int
Speed estimation value reference point
sa_time
unsigned int
Speed measurement value
timer_count
unsigned short
Time wait counter
accel_count
unsigned short
Acceleration/deceleration operation time
counter
o_trm
signed int
Target position for initialization
before_enc
signed short
Previous encoder value
52
Application Note U17324EJ1V0AN
CHAPTER 3 SOFTWARE CONFIGURATION
3.11 Tables
(1) LED output pattern
Contains display pattern data 0 to 9.
unsigned short led_pat[10] = { 0xfc. 0x60, ~ };
3.12 Constant Definitions
The following table shows the major constants used by the reference system.
Symbol
Note
Usage
Value
PAI
π
3.141592
TH_U
Radian value, jack-up constant
1000
RAD
Radian value of one revolution
2 × PAI × TH_U
OFFSET
Original point OFFSET
1733
RPM_RADS
rpm → radian conversion constant
2 × PAI × TH_U/60
KSP
Speed proportion constant
500
KSI
Speed integral constant
50
KI
Current conversion constant
1
P
Number of motor poles
2
KSPGETA
Speed proportion constant jack-up constant
0
KSIGETA
Speed integral constant jack-up constant
9
KIGETA
Current conversion constant jack-up constant
9
SGETA
sin jack-up constant
14
PWM_TS
PWM cycle
80 µs
PWM_DATA
PWM set value
PWM_TS/0.03125
SPEED_MAX
Maximum speed
500 (3000 rpm)
SPEED_MINI
Minimum speed
100 (600 rpm)
SA_SPEED_MAX
Maximum speed difference
800
IQAMAX
Maximum speed integral value
40000000
MAX_I
Maximum current value
800
TS
Motor control period
80 µs
WATCH_START
Speed monitor start time, 10 ms
500
ACCEL_VAL_1ST
Initial acceleration/deceleration time constant
50
ACCEL_VAL
Acceleration/deceleration time constant
3
ACCEL_SPD
Acceleration/deceleration constant
50
MAXPULSE
Number of pulses of one revolution
10000
Note Value used in the V850E/IA3 and V850E/IA4.
For the value used in the V850E/IA1 and V850E/IA2, check it with the program list.
Application Note U17324EJ1V0AN
53
CHAPTER 4 PROGRAM LIST
4.1
4.1.1
Program List (V850E/IA1)
Symbol definition
/****************************************************************************** /
/*
Common area
*/
/****************************************************************************** /
unsigned char
ram_start ;
unsigned char
error_flag ;
/* Error flag */
unsigned char
init_flag ;
/* Initial flag */
unsigned short cont_time ;
/* Interrupt control time uSEC */
unsigned short cont_time1 ;
/* Vector operation time uSEC */
unsigned short disp_co ;
/* Interrupt control time display timer */
unsigned short volume ;
/* Volume value */
unsigned short timer_count ;
/* Time wait counter */
unsigned short accel_count ;
/* Acceleration/deceleration operation time */
/* counter */
unsigned char
stop_flag ;
/* Stop flag */
signed short
before_posi[21][2] ;
/* Position buffer */
signed short
total_sa ;
/* Position total difference */
signed int
sum_speed ;
signed int
speed_co ;
signed int
now_speed ;
/* Present speed rms */
signed int
object_speed ;
/* Target speed rms */
unsigned int
d_speed ;
/* Display speed rms */
unsigned char
ram_end ;
#pragma section const begin
const unsigned short led_pat[10] = { 0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0,
0xfe, 0xe6 } ;
#pragma section const end
/****************************************************************************** /
/*
Common flags
*/
/****************************************************************************** /
extern unsigned char
ram_start ;
extern unsigned char
error_flag ;
/* Error flag */
extern unsigned char
init_flag ;
/* Initial flag */
extern unsigned short
cont_time ;
/* Interrupt control time uSEC */
extern unsigned short
cont_time1 ;
/* Vector operation time uSEC */
extern unsigned short
disp_co ;
/* Interrupt control time display timer */
extern unsigned short
volume ;
/* Volume value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
extern unsigned char
stop_flag ;
/* time counter */
54
/* Stop flag */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
extern signed short
before_posi[21][2] ; /* Position buffer */
extern signed short
total_sa ;
extern signed int
sum_speed ;
extern signed int
speed_co ;
extern signed int
now_speed ;
/* Present speed rms */
extern signed int
object_speed ;
/* Target speed rms */
extern unsigned int
d_speed ;
/* Display speed rms */
extern unsigned char
ram_end ;
/* Position total difference */
#pragma section const begin
extern const unsigned short led_pat[] ;;
#pragma section const end
/****************************************************************************** /
/*
Motor common definition
*/
/****************************************************************************** /
extern signed short
iua ;
/* U-phase current */
extern signed short
iva ;
/* V-phase current */
extern signed int
o_iqai ;
/* Speed integral value area */
extern signed int
o_trm ;
/* Target position for initialization */
extern signed int
base_position ;
/* Speed estimation value reference point */
extern unsigned int
sa_time ;
/* Speed measurement value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
/* time counter */
extern signed short
4.1.2
before_enc ;
/* Previous encoder value */
Constant definition
/****************************************************************************** /
/*
I/O
*/
/****************************************************************************** /
#define BASE_IO
0xc200000
#define LED11
3
#define LED12
2
#define LED13
1
#define LED14
0
#define LED21
5
#define LED22
4
#define LED31
7
#define LED32
6
#define LED41
9
#define LED42
8
#define DIPSW
0x10
#define SW
0x20
#define DA1
0x30
#define DA2
0x40
#define DA3
0x50
Application Note U17324EJ1V0AN
55
CHAPTER 4 PROGRAM LIST
#define WRESET
0x60
#define MODE
0x70
/****************************************************************************** /
/*
Constant
*/
/****************************************************************************** /
#define ON
1
#define OFF
0
#define CW
1
/* CW operation mode */
#define CCW
2
/* CCW operation mode */
#define STOP
0
/* Operation stop mode */
#define ERR_NO1
1
/* Overcurrent error */
#define ERR_NO2
2
/* Speed difference error */
/****************************************************************************** /
/*
Motor constant
*/
/****************************************************************************** /
#define PAI
3.14159265
/* π */
#define TH_U
1000
/* Radian value jack-up constant */
#define RAD
(int)(2*PAI*TH_U)
/* Radian value of one revolution */
#define OFFSET
1945
/* Original point OFFSET */
#define RPM_RADS
(int)((2*PAI*TH_U)/60) /* rpm -> radian conversion constant */
/* Motor constant */
#define KSP
500
/* Speed proportion constant */
#define KSI
50
/* Speed integral constant */
#define KI
1
/* Current conversion constant */
#define P
2
/* Number of poles */
#define KSPGETA
0
/* KSP jack-up constant */
#define KSIGETA
9
/* KSI jack-up constant */
#define KIGETA
9
/* KI jack-up constant */
#define SGETA
14
/* sin jack-up constant */
#define PWM_TS
50
/* PWM cycle */
#define PWM_DATA
(PWM_TS/0.05)
/* PWM set value */
#define SPEED_MAX
3000
/* Maximum speed rpm */
#define SPEED_MINI
600
/* Minimum speed rpm */
#define SA_SPEED_MAX
800
/* Maximum speed difference rpm */
#define IQAMAX
40000000
/* Maximum speed integral value */
#define MAX_I
800
/* Maximum current value */
#define TS
200
/* Motor control time interval uSEC */
#define WATCH_START
500
/* Speed monitor start time 10 mSEC */
#define ACCEL_VAL_1ST 50
/* Initial acceleration/deceleration */
/* time constant */
#define ACCEL_VAL
3
/* Acceleration/deceleration time */
/* constant */
#define ACCEL_SPD
50
/* Acceleration/deceleration constant */
#define MAXPULSE
10000
/* Number of pulses of one revolution */
/****************************************************************************** /
56
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
/*
Function constant
*/
/****************************************************************************** /
void
fcalcu( signed int *wrm, signed int *trm );
void
OUT_data( unsigned short reg, unsigned short data );
unsigned short IN_data( int reg );
void
led_num( int no, long data ) ;
/****************************************************************************** /
/*
Motor-related common area
*/
/****************************************************************************** /
signed short
iua ;
/* U-phase current */
signed short
iva ;
/* V-phase current */
signed int
o_iqai ;
/* Speed integral value area */
signed int
o_trm ;
/* Target position for initialization */
signed int
base_position ;
/* Speed estimation value reference point */
unsigned int
sa_time ;
/* Speed measurement value */
unsigned short timer_count ;
/* Time wait counter */
unsigned short accel_count ;
/* Acceleration/deceleration operation time counter */
signed short
/* Previous encoder value */
4.1.3
before_enc ;
Interrupt handler setting
/****************************************************************************** /
/*
Interrupt symbol table
*/
/****************************************************************************** /
.extern _ _start
.extern _int_MOTOR
.extern _int_AD0
.extern _int_AD1
.extern _int_ETC
.globl V_RESET
.globl V_ETC
.globl V_MOTOR
.globl V_AD0
.globl V_AD1
#*******************************************************************************
.section ".handler",text
V_RESET:
jr
_start
ld.w
[sp],r1
add
4,sp
jr
_int_ETC
V_ETC:
-- Other timers
V_MOTOR:
ld.w
[sp],r1
add
4,sp
jr
_int_MOTOR
-- Speed control timer
V_AD0:
Application Note U17324EJ1V0AN
57
CHAPTER 4 PROGRAM LIST
ld.w
[sp],r1
add
4,sp
jr
_int_AD0
ld.w
[sp],r1
add
4,sp
jr
_int_AD1
-- A/D converter CH0
V_AD1:
-- A/D converter CH1
.extern V_RESET
.extern V_ETC
.extern V_MOTOR
.extern V_AD0
.extern V_AD1
/****************************************************************************** /
/*
Interrupt jump table
*/
/****************************************************************************** /
.section ".vect_RESET",text
mov
#V_RESET,r1
jmp
[r1]
.section ".id_NO",text
.byte
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
.section ".vect_ETC",text
add
-4,sp
st.w
r1,[r3]
mov
#V_ETC,r1
jmp
[r1]
.section ".vect_MOTOR",text
add
-4,sp
st.w
r1,[r3]
mov
#V_MOTOR,r1
jmp
[r1]
.section ".vect_AD0",text
add
-4,sp
st.w
r1,[r3]
mov
#V_AD0,r1
jmp
[r1]
.section ".vect_AD1",text
58
add
-4,sp
st.w
r1,[r3]
mov
#V_AD1,r1
jmp
[r1]
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
4.1.4
Startup routine setting
#=============================================================================
# DESCRIPTIONS:
#
This assembly program is a sample of start-up module for ca850.
#
If you modified this program, you must assemble this file, and
#
locate a given directory.
#
#
Unless -G is specified, sections are located as the following.
#
#
|
:
|
#
|
:
|
#
tp -> -+-------------- + _ _start
#
| start up
#
|-------------- |
# text section
|
|
#
| user program
|
#
|
|
#
|-------------- |
#
| library
#
|
-+-------------- +
#
|
|
# sdata section
|
|
#
|
|
#
gp -> -+-------------- +
#
|
|
# sbss section
|
|
#
|
|
#
+-------------- + _ _stack
#
| stack area
|
# bss section
|
|
#
| 0x400 bytes
|
#
|
:
|
#
|
:
|
#
|
:
|
_ _esbss
_ _sbss
_ _ebss
ep -> -+-------------- + _ _ep_DATA
# tidata section
#
|
|
-+-------------- +
# sidata section
#
_ _ssbss
sp -> -+-------------- + _ _stack + STACKSIZE
#
#
_ _tp_TEXT
|
|
|
-+-------------- +
#
|
:
|
#
|
:
|
#
#=============================================================================
#----------------------------------------------------------------------------#
special symbols
#----------------------------------------------------------------------------Application Note U17324EJ1V0AN
59
CHAPTER 4 PROGRAM LIST
.extern _ _tp_TEXT, 4
.extern _ _gp_DATA, 4
.extern _ _ep_DATA, 4
.extern _ _ssbss, 4
.extern _ _esbss, 4
.extern _ _sbss, 4
.extern _ _ebss, 4
#----------------------------------------------------------------------------#
C program main function
#----------------------------------------------------------------------------.extern _main
#----------------------------------------------------------------------------#
dummy data declaration for creating sbss section
#----------------------------------------------------------------------------.sbss
.lcomm _ _sbss_dummy, 0, 0
#----------------------------------------------------------------------------#
system stack
#----------------------------------------------------------------------------.set
STACKSIZE, 0x400
.bss
.lcomm _ _stack, STACKSIZE, 4
#----------------------------------------------------------------------------#
start up
#
pointers: tp - text pointer
#
gp - global pointer
#
sp - stack pointer
#
#
ep - element pointer
exit status is set to r10
#----------------------------------------------------------------------------.text
.align 4
.globl _ _start
.globl _exit
.globl _ _exit
_ _start:
60
mov
0x12,r10
st.b
r10,VSWC[r0]
-- Set peripheral I/O wait
mov
0x07,r10
-- ×10
st.b
r0,PHCMD[r0]
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
st.b
r10,CKC[r0]
-- PLL xx multiplication
mov
#_ _tp_TEXT, tp
-- set tp register
mov
#_ _gp_DATA, gp
-- set gp register offset
add
tp, gp
-- set gp register
mov
#_ _stack+STACKSIZE, sp
-- set sp register
mov
#_ _ep_DATA, ep
-- set ep register
mov
#_ _ssbss, r13
-- clear sbss section
nop
nop
nop
nop
nop
#
mov
#_ _esbss, r12
cmp
r12, r13
jnl
.L11
.L12:
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L12
mov
#_ _sbss, r13
mov
#_ _ebss, r12
cmp
r12, r13
jnl
.L14
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L15
jarl
_main, lp
.L11:
#
-- clear bss section
.L15:
.L14:
#
-- call main function
_ _exit:
halt
-- end of program
_ _startend:
nop
#
#
#-------------------------- end of start up module ----------------------------#
#
4.1.5
#
Main processing function
#include
"Common.h"
#include
"Motor.h"
Application Note U17324EJ1V0AN
61
CHAPTER 4 PROGRAM LIST
#pragma
ioreg
/* Peripheral I/O register definition */
static int save_psw;
/****************************************************************************** /
/*
3-phase motor control program
*/
/****************************************************************************** /
void
main()
{
unsigned char
proc_no ;
/* Present processing number */
signed int
speed ;
/* Indication speed rms */
signed int
accel_spd ;
int
sw, sw_mode ;
/* */
hinit() ;
/* Hardware initialization */
ainit() ;
/* Initialization of area used */
proc_no = 0 ;
_ _EI();
while( 1 ) {
accel_spd = ( SPEED_MAX - SPEED_MINI ) / 100;
speed
= ( ( SPEED_MAX - SPEED_MINI ) * volume
+ SPEED_MINI ;
sw =
if
/ 1024 )
/* Indication speed calculation by volume */
~IN_data( SW ) & 0x07 ;
/* Read operation button */
( sw == 1 ) {
sw_mode = CW ;
} else if
( sw == 2 ) {
sw_mode = CCW ;
} else if
( sw == 4 ) {
sw_mode = STOP ;
}
switch( proc_no ) {
/* STOP processing */
case 0 :
if
( sw_mode == CW ) {
_ _DI() ;
object_speed
= SPEED_MINI ;
/* Set target speed to minimum value */
stop_flag = OFF ;
timer_count = WATCH_START ;
/* Set speed monitor start time to 5 SEC */
accel_count = ACCEL_VAL_1ST ;
/* Set acceleration/deceleration counter */
init_flag = 2 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 1 ;
} else if
/* Set next processing number */
( sw_mode == CCW ) {
_ _DI() ;
stop_flag = OFF ;
/* Stop flag off */
object_speed = -SPEED_MINI ;
/* Set target speed to minimum value */
timer_count = WATCH_START ;
/* Set speed monitor start time to */
/* 5 SEC */
62
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
accel_count = ACCEL_VAL_1ST ;
/* Set acceleration/deceleration counter */
init_flag = 3 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 4 ;
/* Set CCW processing number */
}
break ;
/* CW processing, acceleration */
case 1 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< speed ) {
object_speed += accel_spd ;
if
( object_speed > speed ) object_speed = speed;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> speed ) {
object_speed -= accel_spd ;
if
( object_speed < speed ) object_speed = speed;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else {
proc_no = 2 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW processing, constant-speed */
case 2 :
object_speed = speed ;
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW stop processing */
case 3 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
> SPEED_MINI ) {
object_speed -= accel_spd ;
if
( object_speed < SPEED_MINI ) object_speed = SPEED_MINI;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on */
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
Application Note U17324EJ1V0AN
63
CHAPTER 4 PROGRAM LIST
/* CCW processing, acceleration */
case 4 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -speed ) {
object_speed += accel_spd ;
if
( object_speed > -speed ) object_speed = -speed;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> -speed ) {
object_speed -= accel_spd ;
if
( object_speed < -speed ) object_speed = -speed;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else {
proc_no = 5 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW processing, constant-speed */
case 5 :
object_speed = -speed ;
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW stop processing */
case 6 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -SPEED_MINI ) {
object_speed += accel_spd ;
if
( object_speed > -SPEED_MINI ) object_speed = -SPEED_MINI;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on */
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
}
if
( ( proc_no == 2 ) || ( proc_no == 5 ) ) {
if
(
if
timer_count == 0 ) {
( abs( object_speed - now_speed ) > SA_SPEED_MAX ) {
error_flag = ERR_NO2 ;
/* Set error No. */
}
64
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
}
}
if
( disp_co == 0 ) {
led_num(1, d_speed / 100 );
/* Number of revolutions */
d_speed = 0 ;
disp_co = 100 ;
if
( abs(now_speed) == 0 ) {
disp_co = 0;
}
led_num(2, 1000/PWM_TS );
/* Carrier frequency */
led_num(3, cont_time );
/* Overall processing time */
led_num(4, cont_time1 );
/* Vector operation processing time */
}
if
( error_flag ) {
while( 1 ) {
OUT_data( LED41, ~0x00 ) ;
/* LED display off */
OUT_data( LED42, ~0x00 ) ;
timer_count = 50 ;
while( timer_count ) ;
if
( error_flag == ERR_NO1 ) {
OUT_data( LED41, ~0x9e ) ;
/* E1 display */
OUT_data( LED42, ~0x60 ) ;
} else if ( error_flag == ERR_NO2 ) {
OUT_data( LED41, ~0x9e ) ;
/* E2 display */
OUT_data( LED42, ~0xda ) ;
} else {
OUT_data( LED41, ~0x9e ) ;
/* E3 display */
OUT_data( LED42, ~0xf2 ) ;
}
timer_count = 50 ;
while( timer_count ) ;
}
}
}
}
4.1.6
LED display function
/****************************************************************************** /
/*
LED value display subroutine
/*
no
: Display area number (1 to 4)
/*
data: Display data (0 to 99)
*/
*/
*/
/****************************************************************************** /
void led_num( int no, long data )
{
if
( no == 1 ) {
data = data % 10000;
Application Note U17324EJ1V0AN
65
CHAPTER 4 PROGRAM LIST
OUT_data( LED11, ~led_pat[data/1000]&0xff ) ;
OUT_data( LED12, ~led_pat[(data%1000)/100]&0xff ) ;
OUT_data( LED13, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED14, ~led_pat[data%10]&0xff ) ;
} else if
( no == 2 ) {
OUT_data( LED21, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED22, ~led_pat[data%10]&0xff ) ;
} else if
( no == 3 ) {
OUT_data( LED31, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED32, ~led_pat[data%10]&0xff ) ;
} else {
OUT_data( LED41, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED42, ~led_pat[data%10]&0xff ) ;
}
}
/****************************************************************************** /
/*
External I/O output subroutine
*/
/*
reg : Output register number
*/
/*
data: Output data
*/
/****************************************************************************** /
void
OUT_data( unsigned short reg, unsigned short data )
{
if
( reg == WRESET ) {
P4.3 = 0;
data = 1;
/* Dummy step */
P4.3 = 1;
} else {
PDL = data | ( reg << 8 );
PDL = reg | ( reg << 8 ) | 0x8000;
}
}
/****************************************************************************** /
/*
External I/O input subroutine
/*
reg: Input register number
*/
*/
/****************************************************************************** /
unsigned short
IN_data( int reg )
{
unsigned char *po;
/*
*/
if
( reg == SW ) {
return P4;
} else {
return 0;
}
}
66
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
4.1.7
Motor control interrupt processing function
#include
"Common.h"
#include
"Motor.h"
#pragma
ioreg
/* Peripheral I/O register definition */
/****************************************************************************** /
/*
Motor control timer interrupt processing
*/
/****************************************************************************** /
_ _interrupt
void
int_MOTOR(void)
{
ADSCM00 = 0x8001 ;
/* Start AD0 */
ADSCM10 = 0x8000 ;
/* Start AD1 */
}
/****************************************************************************** /
/*
Motor control processing
*/
/****************************************************************************** /
void
Motor_CONT(void)
{
signed int
wrm, wre, trm, tre ;
signed int
o_wre, we, o_iqap, o_iqa ;
signed int
ida, iqa, o_vda1, o_vqa1 ;
signed int
o_vd0, o_vq0, o_vda, o_vqa ;
signed int
o_vua, o_vva, o_vwa, wiua, wiva ;
signed int
s_time, wk ;
signed short
now_enc, sa_enc ;
/*
*/
/****************************************************************************** /
/*
Calculation processing of speed and rotor position
*/
/****************************************************************************** /
now_enc = -TM10 ;
sa_enc = now_enc - before_enc ;
if
( abs( sa_enc ) > ( MAXPULSE / 2 ) ) {
if
( sa_enc < 0 ) {
sa_enc += MAXPULSE ;
} else {
sa_enc -= MAXPULSE ;
}
}
before_enc = now_enc ;
wrm = now_speed = (signed long)sa_enc * RAD / MAXPULSE;
/* Set current speed */
wre = wrm * P ;
tre = ( trm * P + OFFSET ) % RAD ;
/****************************************************************************** /
/*
Speed control processing
*/
/****************************************************************************** /
if
( ( stop_flag == OFF ) && ( error_flag == 0 ) ) {
Application Note U17324EJ1V0AN
67
CHAPTER 4 PROGRAM LIST
s_time = TM3 ;
o_wre = object_speed * RPM_RADS * P / TH_U ;
/* rpm -> radian conversion */
we = o_wre - wre ;
o_iqap = ( ( wre * KSP ) + ( we * KSP ) ) >> KSPGETA ;
o_iqa
if
= o_iqap + ( o_iqai >> KSIGETA ) ;
( o_iqai > IQAMAX ) {
o_iqai = IQAMAX ;
} else if ( o_iqai < -IQAMAX ) {
o_iqai = -IQAMAX ;
} else {
o_iqai += ( KSI * we ) ;
}
/****************************************************************************** /
/*
Current control processing
*/
/****************************************************************************** /
ida = ( ( ( iva * sin( tre ) ) - ( iua * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
iqa = ( ( ( iva * sin( tre + RAD/4 ) ) - ( iua * sin( tre + 11*RAD/12 ) ) ) ) >> SGETA ;
o_vda = ( KI * -ida ) >> KIGETA ;
o_vqa = ( KI * ( o_iqa
- iqa )
) >> KIGETA ;
/****************************************************************************** /
/*
3-phase voltage conversion processing
*/
/****************************************************************************** /
if ( init_flag == 2 ) {
/* CW initial processing */
o_trm += ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm > ( 2 * RAD ) ) {
init_flag = 0;
}
} else if ( init_flag == 3 ) {
/* CCW initial processing */
o_trm -= ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm < -( 2 * RAD ) ) {
init_flag = 0;
}
} else {
o_trm = 0 ;
}
o_vua = ( ( ( o_vda * sin( tre + RAD/4 ) ) - ( o_vqa * sin( tre ) ) ) ) >> SGETA ;
o_vva = ( ( ( o_vda * sin( tre + 11*RAD/12 ) ) - ( o_vqa * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
o_vwa = -o_vua - o_vva ;
cont_time1 = ( TM3 - s_time ) * 10 / 16;
68
/* Convert to uSEC */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
/*
PWM conversion output processing
*/
/****************************************************************************** /
OUT_data( WRESET, 0 ) ;
/* Reset watchdog timer */
POER0 = 0x3f ;
/* All phases active */
TUC00 = 0x02 ;
/* Clear PWM output prohibition */
/* PWM counter value calculation output */
o_vua += ( PWM_DATA
/ 2 ) ;
o_vva += ( PWM_DATA
/ 2 ) ;
o_vwa += ( PWM_DATA
/ 2 ) ;
if
( o_vua <= 0 ) {
o_vua = 1 ;
} else if
( o_vua >= PWM_DATA
o_vua = PWM_DATA
) {
- 1 ;
}
if
( o_vva <= 0 ) {
o_vva = 1 ;
} else if
( o_vva >= PWM_DATA
o_vva = PWM_DATA
) {
- 1 ;
}
if
( o_vwa <= 0 ) {
o_vwa = 1 ;
} else if
( o_vwa >= PWM_DATA
o_vwa = PWM_DATA
) {
- 1 ;
}
BFCM00 = o_vua ;
BFCM01 = o_vva ;
BFCM02 = o_vwa ;
} else {
POER0 = 0 ;
/* PWM output off */
now_speed = 0;
cont_time1 = 0;
}
}
4.1.8
10 mSEC interval interrupt processing function
/****************************************************************************** /
/*
Other timer interrupt processing (10 mSEC interval)
*/
/****************************************************************************** /
_ _multi_interrupt
void
int_ETC(void)
{
unsigned short dummy ;
/* Wait timer processing */
if
( timer_count != 0 ) {
Application Note U17324EJ1V0AN
69
CHAPTER 4 PROGRAM LIST
timer_count -= 1 ;
}
/* Acceleration/deceleration timer processing */
if
( accel_count != 0 ) {
accel_count -= 1 ;
}
/* */
if
( disp_co != 0 ) {
d_speed += abs( now_speed ) ;
disp_co -= 1 ;
}
}
4.1.9
A/D converter interrupt processing function
/****************************************************************************** /
/*
A/D converter interrupt processing for U-phase current and speed volume */
/****************************************************************************** /
_ _multi_interrupt
void
int_AD0(void)
{
iua = (( ADCR00 & 0x3ff ) - 0x200) ;
if
( abs(iua) > MAX_I ) {
POER0 = 0 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
volume = 1023 - ( ADCR01 & 0x3ff ) ;
/* Set volume value */
Motor_CONT() ;
cont_time = TM3 * 10 / 16;
/* Convert to uSEC */
}
/****************************************************************************** /
/*
A/D converter interrupt processing for V-phase current
*/
/****************************************************************************** /
_ _interrupt
void
int_AD1(void)
{
iva = (( ADCR10 & 0x3ff ) - 0x200) ;
if
( abs(iva) > MAX_I ) {
POER0 = 0 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
}
4.1.10 Hardware initialization processing function
/****************************************************************************** /
/*
Hardware (peripheral I/O) initialization
*/
/****************************************************************************** /
70
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
void
hinit( void )
{
short
dummy ;
/* Port mode register initialization */
PMC1 = 0x3f ;
/* ENC select */
PMC2 = 0x07 ;
PM2
= 0x07 ;
PM4 = 0xf7 ;
PMDL = 0x0000 ;
OUT_data( LED11, 0xff ) ;
/* LED OFF */
OUT_data( LED12, 0xff ) ;
OUT_data( LED13, 0xff ) ;
OUT_data( LED14, 0xff ) ;
OUT_data( LED21, 0xff ) ;
OUT_data( LED22, 0xff ) ;
OUT_data( LED31, 0xff ) ;
OUT_data( LED32, 0xff ) ;
OUT_data( LED41, 0xff ) ;
OUT_data( LED42, 0xff ) ;
/* Set 10 mSEC timer TM2 */
STOPTE0 = 0x0000;
PRM02 = 0x01;
/* Select fXX/2 */
CSE0 = 0x0028;
/* Select fCLK/64 (3.2 uSEC) */
TCRE0 = 0x2000;
/* Start timer */
CVSE50 = 1562*2 ;
/* 10 mSEC */
CMSE050 = 0x2400;
CC2IC5 = 0x06;
/* Set motor control interrupt timer TM3 */
PRM03 = 1 ;
/* fCLK = fXX */
TMC30 = 0x51;
/* fXX = 4 MHz*10/64(1.6 uSEC) */
TMC31 = 0x09 ;
CC30
= TS * 10 / 16 ;
/* TS uSEC interval */
TMC30 = 0x53;
/* Start timer */
CC3IC0 = 0x02;
/* Reset interrupt mask */
/* TM10 initialization */
PRM02 = 1 ;
/* fCLK = fXX/2 */
TUM0 = 0x80 ;
/* Select UDC mode */
PRM10 = 0x07 ;
/* Select operation mode 4 */
TMC10 = 0x40 ;
/* Start count */
/* TM00 initialization */
PRM01 = 0 ;
/* fCLK = fXX/2 */
SPEC0 = 0x0000 ;
TOMR0 = 0x80 ;
/* Set output mode */
PSTO0 = 0x00 ;
/* Disable real-time output */
BFCM00 =
PWM_DATA /2 ;
/* Set duty to 50 */
BFCM01 =
PWM_DATA /2 ;
/* Set duty to 50 */
Application Note U17324EJ1V0AN
71
CHAPTER 4 PROGRAM LIST
BFCM02 =
PWM_DATA /2 ;
/* Set duty to 50 */
BFCM03 =
PWM_DATA ;
/* Set PWM cycle */
DTRR0 = 40*3 ;
/* Dead time 6 uSEC */
POER0 = 0x3f ;
/* All phases active */
TMC00 = 0x8018 ;
/* Start TMPWM timer */
/* Set A/D */
ADSCM00 = 0x0001 ;
ADSCM10 = 0x0000 ;
ADIC0
= 0x03 ;
ADIC1
= 0x03 ;
}
4.1.11 Common area initialization processing function
/****************************************************************************** /
/*
Common area initialization
*/
/****************************************************************************** /
void
ainit( void )
{
/* Initialization of flags */
error_flag = 0 ;
/* Clear error flag */
init_flag = OFF ;
/* Initial flag off */
disp_co = 100 ;
d_speed = 0 ;
/* Motor control area initialization */
stop_flag = ON ;
/* Stop flag on */
object_speed = 0 ;
/* Target speed 0 */
o_iqai = 0 ;
/* Speed integral value 0 */
o_trm = 0 ;
before_enc = -TM10 ;
/* Set previous encoder value */
}
4.1.12 Revolution start initialization processing function
/****************************************************************************** /
/*
Revolution start initialization
*/
/****************************************************************************** /
void
start_init( void )
{
int
/*
i;
*/
for ( i = 0 ; i < 21 ; i++ ) before_posi[i][1] = 0;
total_sa = 0 ;
sum_speed = 0 ;
speed_co = 100000 / TS ;
}
72
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
4.1.13 sin calculation processing function
/****************************************************************************** /
/*
sin x
/*
*/
Data
*/
/*
Radian unit
*/
/*
Return value
*/
/*
Sign value*16384
*/
/****************************************************************************** /
int sin( int x )
{
x = x % RAD ;
if
( x < 0 ) x += RAD ;
if
( x < (RAD/4) ){
return sins( x ) ;
} else if ( x < (RAD/2) ) {
return sins( (RAD/2) - x ) ;
} else if ( x < (RAD*3/4) ) {
return -sins( x - (RAD/2) ) ;
} else {
return -sins( RAD - x ) ;
}
}
/*******************************************************************************/
int sins( int x )
{
short z1, z2, z3, z4, z5 ;
/*
*/
if
( x <= (RAD/8) ) {
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
z3 = z1 * z2 >> SGETA ;
z5 = z2 * z3 >> SGETA ;
return ( (12868*z1) - (1322*z3) + (40*z5) ) >> SGETA ;
} else {
x = (RAD/4) - x ;
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
z4 = z2 * z2 >> SGETA ;
return ( (268432772) - (5050*z2) + (252*z4) ) >> SGETA ;
}
}
4.1.14 Link directive file for V850E/IA1
/****************************************************************************** /
/*
Link directive file for V850E/IA1
*/
/****************************************************************************** /
Application Note U17324EJ1V0AN
73
CHAPTER 4 PROGRAM LIST
VECT_RESET: !LOAD ?RX V0x0000000 {
.vect_RESET
= $PROGBITS ?AX .vect_RESET;
};
ID_NO: !LOAD ?RX V0x0000070 {
.id_NO
= $PROGBITS ?AX .id_NO;
};
VECT_ETC: !LOAD ?RX V0x0000240 {
.vect_ETC
= $PROGBITS ?AX .vect_ETC;
};
VECT_MOTOR: !LOAD ?RX V0x0000260 {
.vect_MOTOR
= $PROGBITS ?AX .vect_MOTOR;
};
VECT_AD0: !LOAD ?RX V0x00003a0 {
.vect_AD0
= $PROGBITS ?AX .vect_AD0;
};
VECT_AD1: !LOAD ?RX V0x00003b0 {
.vect_AD1
= $PROGBITS ?AX .vect_AD1;
};
HANDLER: !LOAD ?RX V0x00001000 {
.handler
= $PROGBITS ?AX .handler;
};
TEXT:
!LOAD ?RX {
.text
= $PROGBITS ?AX .text;
};
CONST : !LOAD ?R {
.const = $PROGBITS ?A .const;
};
DATA : !LOAD ?RW V0x0fffc000 {
.data
= $PROGBITS ?AW
;
.sdata = $PROGBITS ?AWG ;
.sbss
= $NOBITS
?AWG ;
.bss
= $NOBITS
?AW
;
};
_ _tp_TEXT @ %TP_SYMBOL;
_ _gp_DATA @ %GP_SYMBOL &_ _tp_TEXT{DATA};
_ _ep_DATA @ %EP_SYMBOL;
74
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
4.2
4.2.1
Program List (V850E/IA2)
Symbol definition
/****************************************************************************** /
/*
Common area
*/
/****************************************************************************** /
unsigned char
ram_start ;
unsigned char
error_flag ;
/* Error flag */
unsigned char
init_flag ;
/* Initial flag */
unsigned short cont_time ;
/* Interrupt control time uSEC */
unsigned short cont_time1 ;
/* Vector operation time uSEC */
unsigned short disp_co ;
/* Interrupt control time display timer */
unsigned short volume ;
/* Volume value */
unsigned short timer_count ;
/* Time wait counter */
unsigned short accel_count ;
/* Acceleration/deceleration operation time */
/* counter */
unsigned char
stop_flag ;
/* Stop flag */
signed short
before_posi[21][2] ;
/* Position buffer */
signed short
total_sa ;
/* Position total difference */
signed int
sum_speed ;
signed int
speed_co ;
signed int
now_speed ;
/* Present speed rms */
signed int
object_speed ;
/* Target speed rms */
unsigned int
d_speed ;
/* Display speed rms */
unsigned char
ram_end ;
#pragma section const begin
const unsigned short led_pat[10] = { 0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0,
0xfe, 0xe6 } ;
#pragma section const end
/****************************************************************************** /
/*
Common flags
*/
/****************************************************************************** /
extern unsigned char
ram_start ;
extern unsigned char
error_flag ;
/* Error flag */
extern unsigned char
init_flag ;
/* Initial flag */
extern unsigned short
cont_time ;
/* Interrupt control time uSEC */
extern unsigned short
cont_time1 ;
/* Vector operation time uSEC */
extern unsigned short
disp_co ;
/* Interrupt control time display timer */
extern unsigned short
volume ;
/* Volume value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
/* time counter */
extern unsigned char
stop_flag ;
extern signed short
before_posi[21][2] ; /* Position buffer */
/* Stop flag */
extern signed short
total_sa ;
extern signed int
sum_speed ;
/* Position total difference */
Application Note U17324EJ1V0AN
75
CHAPTER 4 PROGRAM LIST
extern signed int
speed_co ;
extern signed int
now_speed ;
/* Present speed rms */
extern signed int
object_speed ;
/* Target speed rms */
extern unsigned int
d_speed ;
/* Display speed rms */
extern unsigned char
ram_end ;
#pragma section const begin
extern const unsigned short led_pat[] ;;
#pragma section const end
/****************************************************************************** /
/*
Motor common definition
*/
/****************************************************************************** /
extern signed short
iua ;
/* U-phase current */
extern signed short
iva ;
/* V-phase current */
extern signed int
o_iqai ;
/* Speed integral value area */
extern signed int
o_trm ;
/* Target position for initialization */
extern signed int
base_position ;
/* Speed estimation value reference point */
extern unsigned int
sa_time ;
/* Speed measurement value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
extern signed short
before_enc ;
/* time counter */
4.2.2
/* Previous encoder value */
Constant definition
/****************************************************************************** /
/*
I/O
*/
/****************************************************************************** /
#define BASE_IO
0xc200000
#define LED11
3
#define LED12
2
#define LED13
1
#define LED14
0
#define LED21
5
#define LED22
4
#define LED31
7
#define LED32
6
#define LED41
9
#define LED42
8
#define DIPSW
0x10
#define SW
0x20
#define DA1
0x30
#define DA2
0x40
#define DA3
0x50
#define WRESET
0x60
#define MODE
0x70
/****************************************************************************** /
76
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
/*
Constant
*/
/****************************************************************************** /
#define ON
1
#define OFF
0
#define CW
1
/* CW operation mode */
#define CCW
2
/* CCW operation mode */
#define STOP
0
/* Operation stop mode */
#define ERR_NO1
1
/* Overcurrent error */
#define ERR_NO2
2
/* Speed difference error */
/****************************************************************************** /
/*
Motor constant
*/
/****************************************************************************** /
#define PAI
3.14159265
/* π */
#define TH_U
1000
/* Radian value
#define RAD
(int)(2*PAI*TH_U)
/* Radian value of one revolution */
#define OFFSET
1945
/* Original point OFFSET */
#define RPM_RADS
(int)((2*PAI*TH_U)/60) /* rpm -> radian conversion constant */
jack-up constant */
/* Motor constant */
#define KSP
500
/* Speed proportion constant */
#define KSI
50
/* Speed integral constant */
#define KI
1
/* Current conversion constant */
#define P
2
/* Number of poles */
#define KSPGETA
0
/* KSP jack-up constant */
#define KSIGETA
9
/* KSI jack-up constant */
#define KIGETA
9
/* KI jack-up constant */
#define SGETA
14
/* sin jack-up constant */
#define PWM_TS
50
/* PWM cycle */
#define PWM_DATA
(PWM_TS/0.05)
/* PWM set value */
#define SPEED_MAX
3000
/* Maximum speed rpm */
#define SPEED_MINI
600
/* Minimum speed rpm */
#define SA_SPEED_MAX
800
/* Maximum speed difference rpm */
#define IQAMAX
40000000
/* Maximum speed integral value */
#define MAX_I
800
/* Maximum current value */
#define TS
200
/* Motor control time interval uSEC */
#define WATCH_START
500
/* Speed monitor start time 10 mSEC */
#define ACCEL_VAL_1ST 50
/* Initial acceleration/deceleration */
/* time constant */
#define ACCEL_VAL
3
/* Acceleration/deceleration time */
#define ACCEL_SPD
50
/* Acceleration/deceleration constant */
#define MAXPULSE
10000
/* Number of pulses of one revolution */
/* constant */
/****************************************************************************** /
/*
Function constant
*/
/****************************************************************************** /
void
fcalcu( signed int *wrm, signed int *trm );
void
OUT_data( unsigned short reg, unsigned short data );
Application Note U17324EJ1V0AN
77
CHAPTER 4 PROGRAM LIST
unsigned short
IN_data( int reg );
void
led_num( int no, long data ) ;
/****************************************************************************** /
/*
Motor-related common area
*/
/****************************************************************************** /
signed short
iua ;
/* U-phase current */
signed short
iva ;
/* V-phase current */
signed int
o_iqai ;
/* Speed integral value area */
signed int
o_trm ;
/* Target position for initialization */
signed int
base_position ; /* Speed estimation value reference point */
unsigned int
sa_time ;
/* Speed measurement value */
unsigned short
timer_count ;
/* Time wait counter */
unsigned short
accel_count ;
/* Acceleration/deceleration operation time counter */
signed short
before_enc ;
/* Previous encoder value */
4.2.3
Interrupt handler setting
/****************************************************************************** /
/*
Interrupt symbol table
*/
/****************************************************************************** /
.extern _ _start
.extern _int_MOTOR
.extern _int_AD0
.extern _int_AD1
.extern _int_ETC
.globl V_RESET
.globl V_ETC
.globl V_MOTOR
.globl V_AD0
.globl V_AD1
#*******************************************************************************
.section ".handler",text
V_RESET:
Jr
_ _start
V_ETC:
ld.w
[sp],r1
add
4,sp
jr
_int_ETC
-- Other timers
V_MOTOR:
ld.w
[sp],r1
add
4,sp
jr
_int_MOTOR
-- Speed control timer
V_AD0:
ld.w
[sp],r1
add
4,sp
jr
_int_AD0
-- A/D converter CH0
V_AD1:
78
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
ld.w
[sp],r1
add
4,sp
jr
_int_AD1
-- A/D converter CH1
.extern V_RESET
.extern V_ETC
.extern V_MOTOR
.extern V_AD0
.extern V_AD1
/****************************************************************************** /
/*
Interrupt jump table
*/
/****************************************************************************** /
.section ".vect_RESET",text
mov
#V_RESET,r1
jmp
[r1]
.section ".id_NO",text
.byte
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
.section ".vect_ETC",text
add
-4,sp
st.w
r1,[r3]
mov
#V_ETC,r1
jmp
[r1]
.section ".vect_MOTOR",text
add
-4,sp
st.w
r1,[r3]
mov
#V_MOTOR,r1
jmp
[r1]
.section ".vect_AD0",text
add
-4,sp
st.w
r1,[r3]
mov
#V_AD0,r1
jmp
[r1]
.section ".vect_AD1",text
4.2.4
add
-4,sp
st.w
r1,[r3]
mov
#V_AD1,r1
jmp
[r1]
Startup routine setting
#=============================================================================
# DESCRIPTIONS:
#
This assembly program is a sample of start-up module for ca850.
Application Note U17324EJ1V0AN
79
CHAPTER 4 PROGRAM LIST
#
If you modified this program, you must assemble this file, and
#
locate a given directory.
#
#
Unless -G is specified, sections are located as the following.
#
#
#
#
|
:
|
|
:
|
tp -> -+-------------- + _ _start
#
| start up
#
|-------------- |
# text section
|
|
#
| user program
|
#
|
|
#
|-------------- |
#
| library
#
|
|
-+-------------- +
#
|
|
# sdata section
|
|
#
|
|
#
gp -> -+-------------- +
#
|
|
# sbss section
|
|
#
|
|
#
+-------------- + _ _stack
#
| stack area
|
# bss section
|
|
#
| 0x400 bytes
|
#
|
:
|
#
|
:
|
|
:
|
#
_ _esbss
_ _sbss
_ _ebss
ep -> -+-------------- + _ _ep_DATA
# tidata section
#
|
|
-+-------------- +
# sidata section
#
_ _ssbss
sp -> -+-------------- + _ _stack + STACKSIZE
#
#
_ _tp_TEXT
|
|
-+-------------- +
#
|
:
|
#
|
:
|
#
#=============================================================================
#----------------------------------------------------------------------------#
special symbols
#----------------------------------------------------------------------------.extern _ _tp_TEXT, 4
.extern _ _gp_DATA, 4
.extern _ _ep_DATA, 4
.extern _ _ssbss, 4
.extern _ _esbss, 4
80
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
.extern _ _sbss, 4
.extern _ _ebss, 4
#----------------------------------------------------------------------------#
C program main function
#----------------------------------------------------------------------------.extern _main
#----------------------------------------------------------------------------#
dummy data declaration for creating sbss section
#----------------------------------------------------------------------------.sbss
.lcomm _ _sbss_dummy, 0, 0
#----------------------------------------------------------------------------#
system stack
#----------------------------------------------------------------------------.set
STACKSIZE, 0x400
.bss
.lcomm _ _stack, STACKSIZE, 4
#----------------------------------------------------------------------------#
start up
#
pointers: tp - text pointer
#
gp - global pointer
#
sp - stack pointer
#
ep - element pointer
#
exit status is set to r10
#----------------------------------------------------------------------------.text
.align 4
.globl _ _start
.globl _exit
.globl _ _exit
_ _start:
mov
0x02,r10
st.b
r10,VSWC[r0]
-- Set peripheral I/O wait
mov
0x07,r10
-- ×10
st.b
r0,PHCMD[r0]
st.b
r10,CKC[r0]
-- PLL xx multiplication
nop
nop
nop
nop
Application Note U17324EJ1V0AN
81
CHAPTER 4 PROGRAM LIST
nop
mov
#_ _tp_TEXT, tp
-- set tp register
mov
#_ _gp_DATA, gp
-- set gp register offset
add
tp, gp
-- set gp register
mov
#_ _stack+STACKSIZE, sp
-- set sp register
mov
#_ _ep_DATA, ep
-- set ep register
-- clear sbss section
#
mov
#_ _ssbss, r13
mov
#_ _esbss, r12
cmp
r12, r13
jnl
.L11
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L12
mov
#_ _sbss, r13
mov
#_ _ebss, r12
cmp
r12, r13
jnl
.L14
.L12:
.L11:
#
-- clear bss section
.L15:
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L15
jarl
_main, lp
.L14:
#
-- call main function
_ _exit:
halt
-- end of program
_ _startend:
nop
#
#
#-------------------------- end of start up module ----------------------------#
#
4.2.5
#
Main processing function
#include
"Common.h"
#include
"Motor.h"
#pragma
ioreg
/* Peripheral I/O register definition */
static int save_psw;
/****************************************************************************** /
/*
82
3-phase motor control program
Application Note U17324EJ1V0AN
*/
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
void
main()
{
unsigned char
proc_no ;
/* Present processing number */
signed int
speed ;
/* Indication speed rms */
signed
accel_spd ;
int
int
sw, sw_mode ;
/* */
hinit() ;
/* Hardware initialization */
ainit() ;
/* Initialization of area used */
proc_no = 0 ;
_ _EI();
while( 1 ) {
accel_spd = ( SPEED_MAX - SPEED_MINI ) / 100;
speed
= ( ( SPEED_MAX - SPEED_MINI ) * volume
+ SPEED_MINI ;
sw =
if
/ 1024 )
/* Indication speed calculation by volume */
~IN_data( SW ) & 0x07 ;
/* Read operation button */
( sw == 1 ) {
sw_mode = CW ;
} else if
( sw == 2 ) {
sw_mode = CCW ;
} else if
( sw == 4 ) {
sw_mode = STOP ;
}
switch( proc_no ) {
/* STOP processing */
case 0 :
if
( sw_mode == CW ) {
_ _DI() ;
object_speed
= SPEED_MINI ;
/* Set target speed to minimum value */
stop_flag = OFF ;
timer_count = WATCH_START ;
/* Set speed monitor start time to 5 SEC */
accel_count = ACCEL_VAL_1ST ;
/* Set acceleration/deceleration counter */
init_flag = 2 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 1 ;
} else if
/* Set next processing number */
( sw_mode == CCW ) {
_ _DI() ;
stop_flag = OFF ;
/* Stop flag off */
object_speed = -SPEED_MINI ;
/* Set target speed to minimum value */
timer_count = WATCH_START ;
/* Set speed monitor start time to 5 SEC */
accel_count = ACCEL_VAL_1ST ;
/* Set acceleration/deceleration counter */
init_flag = 3 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 4 ;
/* Set CCW processing number */
}
Application Note U17324EJ1V0AN
83
CHAPTER 4 PROGRAM LIST
break ;
/* CW processing, acceleration */
case 1 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< speed ) {
object_speed += accel_spd ;
if
( object_speed > speed ) object_speed = speed;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> speed ) {
object_speed -= accel_spd ;
if
( object_speed < speed ) object_speed = speed;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else {
proc_no = 2 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW processing, constant-speed */
case 2 :
object_speed = speed ;
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW stop processing */
case 3 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
> SPEED_MINI ) {
object_speed -= accel_spd ;
if
( object_speed < SPEED_MINI ) object_speed = SPEED_MINI;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on */
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
/* CCW processing, acceleration */
case 4 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -speed ) {
object_speed += accel_spd ;
84
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
if
( object_speed > -speed ) object_speed = -speed;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> -speed ) {
object_speed -= accel_spd ;
if
( object_speed < -speed ) object_speed = -speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
proc_no = 5 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW processing, constant-speed */
case 5 :
object_speed = -speed ;
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW stop processing */
case 6 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -SPEED_MINI ) {
object_speed += accel_spd ;
if
( object_speed > -SPEED_MINI ) object_speed = -SPEED_MINI;
timer_count = WATCH_START ; /* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on */
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
}
if
( ( proc_no == 2 ) || ( proc_no == 5 ) ) {
if
(
if
timer_count == 0 ) {
( abs( object_speed - now_speed ) > SA_SPEED_MAX ) {
error_flag = ERR_NO2 ;
/* Set error No.*/
}
}
}
if
( disp_co == 0 ) {
led_num(1, d_speed / 100 );
/* Number of revolutions */
d_speed = 0 ;
Application Note U17324EJ1V0AN
85
CHAPTER 4 PROGRAM LIST
disp_co = 100 ;
if
( abs(now_speed) == 0 ) {
disp_co = 0;
}
led_num(2, 1000/PWM_TS );
/* Carrier frequency */
led_num(3, cont_time );
/* Overall processing time */
led_num(4, cont_time1 );
/* Vector operation processing time */
}
if
( error_flag ) {
while( 1 ) {
OUT_data( LED41, ~0x00 ) ;
/* LED display off */
OUT_data( LED42, ~0x00 ) ;
timer_count = 50 ;
while( timer_count ) ;
if
( error_flag == ERR_NO1 ) {
OUT_data( LED41, ~0x9e ) ;
/* E1 display */
OUT_data( LED42, ~0x60 ) ;
} else if ( error_flag == ERR_NO2 ) {
OUT_data( LED41, ~0x9e ) ;
/* E2 display */
OUT_data( LED42, ~0xda ) ;
} else {
OUT_data( LED41, ~0x9e ) ;
/* E3 display */
OUT_data( LED42, ~0xf2 ) ;
}
timer_count = 50 ;
while( timer_count ) ;
}
}
}
}
4.2.6
LED display function
/****************************************************************************** /
/*
LED value display subroutine
/*
no
: Display area number (1 to 4)
/*
data : Display data (0 to 99)
*/
*/
*/
/****************************************************************************** /
void led_num( int no, long data )
{
if
( no == 1 ) {
data = data % 10000;
OUT_data( LED11, ~led_pat[data/1000]&0xff ) ;
OUT_data( LED12, ~led_pat[(data%1000)/100]&0xff ) ;
OUT_data( LED13, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED14, ~led_pat[data%10]&0xff ) ;
} else if
( no == 2 ) {
OUT_data( LED21, ~led_pat[(data%100)/10]&0xff ) ;
86
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
OUT_data( LED22, ~led_pat[data%10]&0xff ) ;
} else if
( no == 3 ) {
OUT_data( LED31, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED32, ~led_pat[data%10]&0xff ) ;
} else {
OUT_data( LED41, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED42, ~led_pat[data%10]&0xff ) ;
}
}
/****************************************************************************** /
/*
External I/O output subroutine
/*
reg
/*
data : Output data
*/
: Output register number
*/
*/
/****************************************************************************** /
void
OUT_data( unsigned short reg, unsigned short data )
{
if
( reg == WRESET ) {
P3.0 = 0;
data = 1;
/* Dummy step */
P3.0 = 1;
} else {
PDL = data | ( reg << 8 );
PDL = reg | ( reg << 8 ) | 0x8000;
}
}
/****************************************************************************** /
/*
External I/O input subroutine
/*
*/
reg : Input register number
*/
/****************************************************************************** /
unsigned short
IN_data( int reg )
{
unsigned char *po;
/*
*/
if
( reg == SW ) {
return P4;
} else {
return 0;
}
}
4.2.7
Motor control interrupt processing function
#include
"Common.h"
#include
"Motor.h"
#pragma
ioreg
/* Peripheral I/O register definition */
/****************************************************************************** /
/*
Motor control timer interrupt processing
*/
/****************************************************************************** /
Application Note U17324EJ1V0AN
87
CHAPTER 4 PROGRAM LIST
_ _interrupt
void
int_MOTOR(void)
{
ADSCM00 = 0x8001 ;
/* Start AD0 */
ADSCM10 = 0x8000 ;
/* Start AD1 */
}
/****************************************************************************** /
/*
Motor control processing
*/
/****************************************************************************** /
void
Motor_CONT(void)
{
signed int
wrm, wre, trm, tre ;
signed int
o_wre, we, o_iqap, o_iqa ;
signed int
ida, iqa, o_vda1, o_vqa1 ;
signed int
o_vd0, o_vq0, o_vda, o_vqa ;
signed int
o_vua, o_vva, o_vwa, wiua, wiva ;
signed int
s_time, wk ;
signed short now_enc, sa_enc ;
/*
*/
/****************************************************************************** /
/*
Calculation processing of speed and rotor position
*/
/****************************************************************************** /
now_enc = -TM10 ;
sa_enc = now_enc - before_enc ;
if
( abs( sa_enc ) > ( MAXPULSE / 2 ) ) {
if
( sa_enc < 0 ) {
sa_enc += MAXPULSE ;
} else {
sa_enc -= MAXPULSE ;
}
}
before_enc = now_enc ;
wrm = now_speed = (signed long)sa_enc * RAD / MAXPULSE;
/* Set current speed */
wre = wrm * P ;
tre = ( trm * P + OFFSET ) % RAD ;
/****************************************************************************** /
/*
Speed control processing
*/
/****************************************************************************** /
if
( ( stop_flag == OFF ) && ( error_flag == 0 ) ) {
s_time = TM3 ;
o_wre = object_speed * RPM_RADS * P / TH_U ;
/* rpm -> radian conversion */
we = o_wre - wre ;
o_iqap = ( ( wre * KSP ) + ( we * KSP ) ) >> KSPGETA ;
o_iqa = o_iqap + ( o_iqai >> KSIGETA ) ;
if
( o_iqai > IQAMAX ) {
o_iqai = IQAMAX ;
88
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
} else if ( o_iqai < -IQAMAX ) {
o_iqai = -IQAMAX ;
} else {
o_iqai += ( KSI * we ) ;
}
/****************************************************************************** /
/*
Current control processing
*/
/****************************************************************************** /
ida = ( ( ( iva * sin( tre ) ) - ( iua * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
iqa = ( ( ( iva * sin( tre + RAD/4 ) ) - ( iua * sin( tre + 11*RAD/12 ) ) ) ) >> SGETA ;
o_vda = ( KI * -ida ) >> KIGETA ;
o_vqa = ( KI * ( o_iqa
- iqa )
) >> KIGETA ;
/****************************************************************************** /
/*
3-phase voltage conversion processing
*/
/****************************************************************************** /
if ( init_flag == 2 ) {
/* CW initial processing */
o_trm += ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm > ( 2 * RAD ) ) {
init_flag = 0;
}
} else if ( init_flag == 3 ) {
/* CCW initial processing */
o_trm -= ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm < -( 2 * RAD ) ) {
init_flag = 0;
}
} else {
o_trm = 0 ;
}
o_vua = ( ( ( o_vda * sin( tre + RAD/4 ) ) - ( o_vqa * sin( tre ) ) ) ) >> SGETA ;
o_vva = ( ( ( o_vda * sin( tre + 11*RAD/12 ) ) - ( o_vqa * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
o_vwa
= -o_vua - o_vva ;
cont_time1 = ( TM3 - s_time ) * 10 / 16;
/* Convert to uSEC */
/****************************************************************************** /
/*
PWM conversion output processing
*/
/****************************************************************************** /
OUT_data( WRESET, 0 ) ;
/* Reset watchdog timer */
POER0 = 0x3f ;
/* All phases active */
TUC00 = 0x02 ;
/* Clear PWM output prohibition */
/* PWM counter value calculation output */
o_vua += ( PWM_DATA
/ 2 ) ;
Application Note U17324EJ1V0AN
89
CHAPTER 4 PROGRAM LIST
o_vva += ( PWM_DATA
/ 2 ) ;
o_vwa += ( PWM_DATA
/ 2 ) ;
if
( o_vua <= 0 ) {
o_vua = 1 ;
} else if
( o_vua >= PWM_DATA
o_vua = PWM_DATA
) {
- 1 ;
}
if
( o_vva <= 0 ) {
o_vva = 1 ;
} else if
( o_vva >= PWM_DATA
o_vva = PWM_DATA
) {
- 1 ;
}
if
( o_vwa <= 0 ) {
o_vwa = 1 ;
} else if
( o_vwa >= PWM_DATA
o_vwa = PWM_DATA
) {
- 1 ;
}
BFCM00 = o_vua ;
BFCM01 = o_vva ;
BFCM02 = o_vwa ;
} else {
POER0 = 0 ;
/* PWM output off */
now_speed = 0;
cont_time1 = 0;
}
}
4.2.8
10 mSEC interval interrupt processing function
/****************************************************************************** /
/*
Other timer interrupt processing (10 mSEC interval)
*/
/****************************************************************************** /
_ _multi_interrupt
void
int_ETC(void)
{
unsigned short dummy ;
/* Wait timer processing */
if
( timer_count != 0 ) {
timer_count -= 1 ;
}
/* Acceleration/deceleration timer processing */
if
( accel_count != 0 ) {
accel_count -= 1 ;
}
/* */
if
90
( disp_co != 0 ) {
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
d_speed += abs( now_speed ) ;
disp_co -= 1 ;
}
}
4.2.9
A/D converter interrupt processing function
/****************************************************************************** /
/*
A/D converter interrupt processing for U-phase current and speed volume */
/****************************************************************************** /
_ _multi_interrupt
void
int_AD0(void)
{
iua = (( ADCR00 & 0x3ff ) - 0x200) ;
if
( abs(iua) > MAX_I ) {
POER0 = 0 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
volume = 1023 - ( ADCR01 & 0x3ff ) ;
/* Set volume value */
Motor_CONT() ;
cont_time = TM3 * 10 / 16;
/* Convert to uSEC */
}
/****************************************************************************** /
/*
A/D converter interrupt processing for V-phase current
*/
/****************************************************************************** /
_ _interrupt
void
int_AD1(void)
{
iva = (( ADCR10 & 0x3ff ) - 0x200) ;
if
( abs(iva) > MAX_I ) {
POER0 = 0 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
}
4.2.10 Hardware initialization processing function
/****************************************************************************** /
/*
Hardware (peripheral I/O) initialization
*/
/****************************************************************************** /
void
hinit( void )
{
short
dummy ;
/* Port mode register initialization */
PMC1 = 0x3f ;
/* ENC select */
PMC2 = 0x07 ;
PM2 = 0x07 ;
Application Note U17324EJ1V0AN
91
CHAPTER 4 PROGRAM LIST
PM3 = 0xfe ;
PM4 = 0xf7 ;
PMDL = 0x0000 ;
OUT_data( LED11, 0xff ) ;
/* LED OFF */
OUT_data( LED12, 0xff ) ;
OUT_data( LED13, 0xff ) ;
OUT_data( LED14, 0xff ) ;
OUT_data( LED21, 0xff ) ;
OUT_data( LED22, 0xff ) ;
OUT_data( LED31, 0xff ) ;
OUT_data( LED32, 0xff ) ;
OUT_data( LED41, 0xff ) ;
OUT_data( LED42, 0xff ) ;
/* Set 10 mSEC timer TM2 */
STOPTE0 = 0x0000;
PRM02 = 0x01;
/* Select fXX/2 */
CSE0 = 0x0028;
/* Select fCLK/64 (3.2 uSEC) */
TCRE0 = 0x2000;
/* Start timer */
CVSE50 = 1562*2 ;
/* 10 mSEC */
CMSE050 = 0x2400;
CC2IC5 = 0x06;
/* Set motor control interrupt timer TM3 */
PRM03 = 1 ;
/* fCLK = fXX */
TMC30 = 0x51;
/* fXX = 4MHz*10/64(1.6 uSEC) */
TMC31 = 0x09 ;
CC30
= TS * 10 / 16 ;
/* TS uSEC interval */
TMC30 = 0x53;
/* Start timer */
CC3IC0 = 0x02;
/* Reset interrupt mask */
/* TM10 initialization */
PRM02 = 1 ;
/* fCLK = fXX/2 */
TUM0 = 0x80 ;
/* Select UDC mode */
PRM10 = 0x07 ;
/* Select operation mode 4 */
TMC10 = 0x40 ;
/* Start count */
/* TM00 initialization */
PRM01 = 0 ;
/* fCLK = fXX/2 */
SPEC0 = 0x0000 ;
TOMR0 = 0x80 ;
/* Set output mode */
PSTO0 = 0x00 ;
/* Disable real-time output */
BFCM00 =
PWM_DATA /2 ;
/* Set duty to 50 */
BFCM01 =
PWM_DATA /2 ;
/* Set duty to 50 */
BFCM02 =
PWM_DATA /2 ;
/* Set duty to 50 */
BFCM03 =
PWM_DATA
/* Set PWM cycle */
;
DTRR0 = 40*3 ;
/* Dead time 6 uSEC */
POER0 = 0x3f ;
/* All phases active */
TMC00 = 0x8018 ;
/* Start TMPWM timer */
/* Set A/D */
ADSCM00 = 0x0001 ;
92
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
ADSCM10 = 0x0000 ;
ADIC0
= 0x03 ;
ADIC1
= 0x03 ;
}
4.2.11 Common area initialization processing function
/****************************************************************************** /
/*
Common area initialization
*/
/****************************************************************************** /
void
ainit( void )
{
/* Initialization of flags */
error_flag = 0 ;
/* Clear error flag */
init_flag = OFF ;
/* Initial flag off */
disp_co = 100 ;
d_speed = 0 ;
/* Motor control area initialization */
stop_flag = ON ;
/* Stop flag on */
object_speed = 0 ;
/* Target speed 0 */
o_iqai = 0 ;
/* Speed integral value 0 */
o_trm = 0 ;
before_enc = -TM10;
/* Set previous encoder value */
}
4.2.12 Revolution start initialization processing function
/****************************************************************************** /
/*
Revolution start initialization
*/
/****************************************************************************** /
void
start_init( void )
{
int
/*
i;
*/
for ( i = 0 ; i < 21 ; i++ ) before_posi[i][1] = 0;
total_sa = 0 ;
sum_speed = 0 ;
speed_co = 100000 / TS ;
}
4.2.13 sin calculation processing function
/****************************************************************************** /
/*
/*
sin x
*/
Data
*/
/*
Radian unit
*/
/*
Return value
*/
/*
Sign value*16384
*/
Application Note U17324EJ1V0AN
93
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
int sin( int x )
{
x = x % RAD ;
if
( x < 0 ) x += RAD ;
if
( x < (RAD/4) ){
return sins( x ) ;
} else if ( x < (RAD/2) ) {
return sins( (RAD/2) - x ) ;
} else if ( x < (RAD*3/4) ) {
return -sins( x - (RAD/2) ) ;
} else {
return -sins( RAD - x ) ;
}
}
/*******************************************************************************/
int sins( int x )
{
short z1, z2, z3, z4, z5 ;
/*
*/
if
( x <= (RAD/8) ) {
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
z3 = z1 * z2 >> SGETA ;
z5 = z2 * z3 >> SGETA ;
return ( (12868*z1) - (1322*z3) + (40*z5) ) >> SGETA ;
} else {
x = (RAD/4) - x ;
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
z4 = z2 * z2 >> SGETA ;
return ( (268432772) - (5050*z2) + (252*z4) ) >> SGETA ;
}
}
4.2.14 Link directive file for V850E/IA2
/****************************************************************************** /
/*
Link directive file for V850E/IA2
*/
/****************************************************************************** /
VECT_RESET: !LOAD ?RX V0x0000000 {
.vect_RESET
= $PROGBITS ?AX .vect_RESET;
};
ID_NO: !LOAD ?RX V0x0000070 {
.id_NO
= $PROGBITS ?AX .id_NO;
};
VECT_ETC: !LOAD ?RX V0x0000240 {
.vect_ETC
94
= $PROGBITS ?AX .vect_ETC;
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
};
VECT_MOTOR: !LOAD ?RX V0x0000260 {
.vect_MOTOR
= $PROGBITS ?AX .vect_MOTOR;
};
VECT_AD0: !LOAD ?RX V0x00003a0 {
.vect_AD0
= $PROGBITS ?AX .vect_AD0;
};
VECT_AD1: !LOAD ?RX V0x00003b0 {
.vect_AD1
= $PROGBITS ?AX .vect_AD1;
};
HANDLER: !LOAD ?RX V0x00001000 {
.handler
= $PROGBITS ?AX .handler;
};
TEXT:
!LOAD ?RX {
.text
= $PROGBITS ?AX .text;
};
CONST : !LOAD ?R {
.const = $PROGBITS ?A .const;
};
DATA : !LOAD ?RW V0x0fffc000 {
.data
= $PROGBITS ?AW
;
.sdata = $PROGBITS ?AWG ;
.sbss
= $NOBITS
?AWG ;
.bss
= $NOBITS
?AW
;
};
_ _tp_TEXT @ %TP_SYMBOL;
_ _gp_DATA @ %GP_SYMBOL &_ _tp_TEXT{DATA};
_ _ep_DATA @ %EP_SYMBOL;
Application Note U17324EJ1V0AN
95
CHAPTER 4 PROGRAM LIST
4.3
4.3.1
Program List (V850E/IA3)
Symbol definition
/****************************************************************************** /
/*
Common area
*/
/****************************************************************************** /
unsigned char
ram_start ;
unsigned char
error_flag ;
/* Error flag */
unsigned char
init_flag ;
/* Initial flag */
unsigned short cont_time ;
/* Interrupt control time uSEC */
unsigned short cont_time1 ;
/* Vector operation time uSEC */
unsigned short disp_co ;
/* Interrupt control time display timer */
unsigned short volume ;
/* Volume value */
unsigned short timer_count ;
/* Time wait counter */
unsigned short accel_count ;
/* Acceleration/deceleration operation time counter */
unsigned char
stop_flag ;
/* Stop flag */
signed short
before_posi[21][2] ; /* Position buffer */
signed short
total_sa ;
signed int
sum_speed ;
signed int
speed_co ;
signed int
now_speed ;
signed int
object_speed ;
/* Target speed rms */
unsigned int
d_speed ;
/* Display speed rms */
unsigned char
ram_end ;
/* Position total difference */
/* Present speed rms */
#pragma section const begin
const unsigned short led_pat[10] = { 0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe,
0xe6 } ;
#pragma section const end
/****************************************************************************** /
/*
Common flags
*/
/****************************************************************************** /
extern unsigned char
ram_start ;
extern unsigned char
error_flag ;
/* Error flag */
extern unsigned char
init_flag ;
/* Initial flag */
extern unsigned short
cont_time ;
/* Interrupt control time uSEC */
extern unsigned short
cont_time1 ;
/* Vector operation time uSEC */
extern unsigned short
disp_co ;
/* Interrupt control time display timer */
extern unsigned short
volume ;
/* Volume value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
extern unsigned char
stop_flag ;
extern signed short
before_posi[21][2] ; /* Position buffer */
extern signed short
total_sa ;
extern signed int
sum_speed ;
extern signed int
speed_co ;
/* time counter */
96
/* Stop flag */
/* Position total difference */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
extern signed int
now_speed ;
/* Present speed rms */
extern signed int
object_speed ;
/* Target speed rms */
extern unsigned int
d_speed ;
/* Display speed rms */
extern unsigned char
ram_end ;
#pragma section const begin
extern const unsigned short led_pat[] ;;
#pragma section const end
/****************************************************************************** /
/*
Motor common definition
*/
/****************************************************************************** /
extern signed short
iua ;
/* U-phase current */
extern signed short
iva ;
/* V-phase current */
extern signed int
o_iqai ;
/* Speed integral value area */
extern signed int
o_trm ;
/* Target position for initialization */
extern signed int
base_position ;
/* Speed estimation value reference point */
extern unsigned int
sa_time ;
/* Speed measurement value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
/* time counter */
extern signed short
4.3.2
before_enc ;
/* Previous encoder value */
Constant definition
/****************************************************************************** /
/*
I/O
*/
/****************************************************************************** /
#define BASE_IO
0xc200000
#define LED11
0x03
#define LED12
0x02
#define LED13
0x01
#define LED14
0x00
#define LED21
0x05
#define LED22
0x04
#define LED31
0x07
#define LED32
0x06
#define LED41
0x09
#define LED42
0x08
#define DIPSW
0x0f
#define SW
0x0e
#define DA1
0x0a
#define DA2
0x0b
#define DA3
0x0c
#define WRESET
0x0d
#define MODE
0x10
/****************************************************************************** /
/*
Constant
*/
Application Note U17324EJ1V0AN
97
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
#define ON
1
#define OFF
0
#define CW
1
/* CW operation mode */
#define CCW
2
/* CCW operation mode */
#define STOP
0
/* Operation stop mode */
#define ERR_NO1
1
/* Overcurrent error */
#define ERR_NO2
2
/* Speed difference error
*/
/****************************************************************************** /
/*
Motor constant
*/
/****************************************************************************** /
#define PAI
3.14159265
/* π */
#define TH_U
1000
/* Radian value
#define RAD
(int)(2*PAI*TH_U)
/* Radian value of one revolution */
#define OFFSET
1733
/* Original point OFFSET */
#define RPM_RADS
(int)((2*PAI*TH_U)/60) /* rpm -> radian conversion constant */
jack-up constant */
/* Motor constant */
#define KSP
500
/* Speed proportion constant */
#define KSI
50
/* Speed integral constant */
#define KI
1
/* Current conversion constant */
#define P
2
/* Number of poles */
#define KSPGETA
0
/* KSP jack-up constant */
#define KSIGETA
9
/* KSI jack-up constant */
#define KIGETA
9
/* KI jack-up constant */
#define SGETA
14
/* sin jack-up constant */
#define PWM_TS
80
/* PWM cycle */
#define PWM_DATA
(PWM_TS/0.03125)
/* PWM set value */
#define SPEED_MAX
3000
/* Maximum speed rpm */
#define SPEED_MINI
600
/* Minimum speed rpm */
#define SA_SPEED_MAX
800
/* Maximum speed difference rpm */
#define IQAMAX
40000000
/* Maximum speed integral value */
#define MAX_I
800
/* Maximum current value */
#define TS
80
/* Motor control time interval uSEC */
#define WATCH_START
500
/* Speed monitor start time 10 mSEC */
#define ACCEL_VAL_1ST 50
/* Initial acceleration/deceleration */
/* time constant */
#define ACCEL_VAL
3
/* Acceleration/deceleration time constant */
#define ACCEL_SPD
50
/* Acceleration/deceleration constant */
#define MAXPULSE
10000
/* Number of pulses of one revolution */
/****************************************************************************** /
/*
Function constant
*/
/****************************************************************************** /
void
fcalcu( signed int *wrm, signed int *trm );
void
OUT_data( unsigned short reg, unsigned short data );
unsigned short
IN_data( int reg );
void
led_num( int no, long data ) ;
98
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
/*
Motor-related common area
*/
/****************************************************************************** /
signed short
iua ;
/* U-phase current */
signed short
iva ;
/* V-phase current */
signed int
o_iqai ;
/* Speed integral value area */
signed int
o_trm ;
/* Target position for initialization */
signed int
base_position ;
/* Speed estimation value reference point */
unsigned int
sa_time ;
/* Speed measurement value */
unsigned short
timer_count ;
/* Time wait counter */
unsigned short
accel_count ;
/* Acceleration/deceleration operation time counter */
signed short
before_enc ;
/* Previous encoder value */
4.3.3
Interrupt handler setting
/****************************************************************************** /
/*
Interrupt symbol table
*/
/****************************************************************************** /
.extern _ _start
.extern _int_MOTOR
.extern _int_AD0
.extern _int_AD1
.extern _int_ETC
.globl V_RESET
.globl V_ETC
.globl V_MOTOR
.globl V_AD0
.globl V_AD1
#*******************************************************************************
.section ".handler",text
V_RESET:
jr
_ _start
ld.w
[sp],r1
add
4,sp
jr
_int_ETC
V_ETC:
-- Other timers
V_MOTOR:
ld.w
[sp],r1
add
4,sp
jr
_int_MOTOR
ld.w
[sp],r1
add
4,sp
jr
_int_AD0
ld.w
[sp],r1
add
4,sp
-- Speed control timer
V_AD0:
-- A/D converter CH0
V_AD1:
Application Note U17324EJ1V0AN
99
CHAPTER 4 PROGRAM LIST
jr
_int_AD1
-- A/D converter CH1
.extern V_RESET
.extern V_ETC
.extern V_MOTOR
.extern V_AD0
.extern V_AD1
/****************************************************************************** /
/*
Interrupt jump table
*/
/****************************************************************************** /
.section ".vect_RESET",text
mov
#V_RESET,r1
jmp
[r1]
.section ".id_NO",text
.byte
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
.section ".vect_ETC",text
add
-4,sp
st.w
r1,[sp]
mov
#V_ETC,r1
jmp
[r1]
.section ".vect_MOTOR",text
add
-4,sp
st.w
r1,[sp]
mov
#V_MOTOR,r1
jmp
[r1]
.section ".vect_AD0",text
add
-4,sp
st.w
r1,[sp]
mov
#V_AD0,r1
jmp
[r1]
.section ".vect_AD1",text
4.3.4
add
-4,sp
st.w
r1,[sp]
mov
#V_AD1,r1
jmp
[r1]
Startup routine setting
#=============================================================================
# DESCRIPTIONS:
#
This assembly program is a sample of start-up module for ca850.
#
If you modified this program, you must assemble this file, and
#
locate a given directory.
100
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
#
#
Unless -G is specified, sections are located as the following.
#
#
#
#
|
:
|
|
:
|
tp -> -+-------------- + _ _start
#
| start up
#
|-------------- |
# text section
|
|
#
| user program
|
#
|
|
#
|-------------- |
#
| library
#
|
|
-+-------------- +
#
|
|
# sdata section
|
|
#
|
|
#
gp -> -+-------------- +
#
|
|
# sbss section
|
|
#
|
|
#
+-------------- + _ _stack
#
| stack area
|
# bss section
|
|
#
| 0x400 bytes
|
#
|
:
|
#
|
:
|
|
:
|
#
_ _esbss
_ _sbss
_ _ebss
ep -> -+-------------- + _ _ep_DATA
# tidata section
#
|
|
-+-------------- +
# sidata section
#
_ _ssbss
sp -> -+-------------- + _ _stack + STACKSIZE
#
#
_ _tp_TEXT
|
|
-+-------------- +
#
|
:
|
#
|
:
|
#
#=============================================================================
#----------------------------------------------------------------------------#
special symbols
#----------------------------------------------------------------------------.extern _ _tp_TEXT, 4
.extern _ _gp_DATA, 4
.extern _ _ep_DATA, 4
.extern _ _ssbss, 4
.extern _ _esbss, 4
.extern _ _sbss, 4
.extern _ _ebss, 4
Application Note U17324EJ1V0AN
101
CHAPTER 4 PROGRAM LIST
#----------------------------------------------------------------------------#
C program main function
#----------------------------------------------------------------------------.extern _main
#----------------------------------------------------------------------------#
dummy data declaration for creating sbss section
#----------------------------------------------------------------------------.sbss
.lcomm _ _sbss_dummy, 0, 0
#----------------------------------------------------------------------------#
system stack
#----------------------------------------------------------------------------.set
STACKSIZE, 0x400
.bss
.lcomm
_ _stack, STACKSIZE, 4
#----------------------------------------------------------------------------#
start up
#
pointers:
tp - text pointer
#
gp - global pointer
#
sp - stack pointer
#
ep - element pointer
#
exit status is set to r10
#----------------------------------------------------------------------------.text
.align 4
.globl _ _start
.globl _exit
.globl _ _exit
_ _start:
mov
13,r10
-- 500 kHz to 64 MHz
st.b
r10,VSWC[r0]
-- Set peripheral I/O wait
mov
0x03,r10
-- PLL
st.b
r10,PLLCTL[r0]
mov
0x00,r10
st.b
r10,PRCMD[r0]
st.b
r10,PCC[r0]
nop
nop
nop
102
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
nop
nop
mov
#_ _tp_TEXT, tp
-- set tp register
mov
#_ _gp_DATA, gp
-- set gp register offset
add
tp, gp
-- set gp register
mov
#_ _stack+STACKSIZE, sp
-- set sp register
mov
#_ _ep_DATA, ep
-- set ep register
mov
#_ _ssbss, r13
-- clear sbss section
mov
#_ _esbss, r12
cmp
r12, r13
jnl
.L11
#
.L12:
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L12
.L11:
#
mov
#_ _sbss, r13
mov
#_ _ebss, r12
cmp
r12, r13
jnl
.L14
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L15
jarl
_main, lp
-- clear bss section
.L15:
.L14:
#
-- call main function
_ _exit:
halt
-- end of program
_ _startend:
nop
#
#
#-------------------------- end of start up module ----------------------------#
#
4.3.5
#
Main processing function
#include
"Common.h"
#include
"Motor.h"
#pragma
ioreg
/* Peripheral I/O register definition */
static int save_psw;
/****************************************************************************** /
Application Note U17324EJ1V0AN
103
CHAPTER 4 PROGRAM LIST
/*
3-phase motor control program
*/
/****************************************************************************** /
void
main()
{
unsigned char
proc_no ;
/* Present processing number */
signed int
speed ;
/* Indication speed rms */
signed int
accel_spd ;
int
sw, sw_mode ;
/* */
hinit() ;
/* Hardware initialization */
ainit() ;
/* Initialization of area used */
proc_no = 0 ;
_ _EI();
while( 1 ) {
accel_spd = ( SPEED_MAX - SPEED_MINI ) / 100;
speed
= ( ( SPEED_MAX - SPEED_MINI ) * volume
+ SPEED_MINI ;
sw =
if
/ 1024 )
/* Indication speed calculation by volume */
~IN_data( SW ) & 0x07 ;
/* Read operation button */
( sw == 1 ) {
sw_mode = CW ;
} else if
( sw == 2 ) {
sw_mode = CCW ;
} else if
( sw == 4 ) {
sw_mode = STOP ;
}
switch( proc_no ) {
/* STOP processing */
case 0 :
if
( sw_mode == CW ) {
_ _DI() ;
object_speed
= SPEED_MINI ;
/* Set target speed to minimum value */
stop_flag = OFF ;
timer_count = WATCH_START ;
/* Set speed monitor start time to 5 SEC */
accel_count = ACCEL_VAL_1ST ; /* Set acceleration/deceleration counter */
init_flag = 2 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 1 ;
} else if
/*Set next processing number */
( sw_mode == CCW ) {
_ _DI() ;
stop_flag = OFF ;
/* Stop flag off */
object_speed = -SPEED_MINI ;
/* Set target speed to minimum value */
timer_count = WATCH_START ;
/* Set speed monitor start time to 5 SEC */
accel_count = ACCEL_VAL_1ST ; /* Set acceleration/deceleration counter */
init_flag = 3 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 4 ;
104
/* Set CCW processing number */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
}
break ;
/* CW processing, acceleration */
case 1 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< speed ) {
object_speed += accel_spd ;
if
( object_speed > speed ) object_speed = speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> speed ) {
object_speed -= accel_spd ;
if
( object_speed < speed ) object_speed = speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
proc_no = 2 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW processing, constant-speed */
case 2 :
object_speed = speed ;
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW stop processing */
case 3 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
> SPEED_MINI ) {
object_speed -= accel_spd ;
if
( object_speed < SPEED_MINI ) object_speed = SPEED_MINI;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on */
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
/* CCW processing, acceleration */
case 4 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -speed ) {
Application Note U17324EJ1V0AN
105
CHAPTER 4 PROGRAM LIST
object_speed += accel_spd ;
if
( object_speed > -speed ) object_speed = -speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> -speed ) {
object_speed -= accel_spd ;
if
( object_speed < -speed ) object_speed = -speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
proc_no = 5 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW processing, constant-speed */
case 5 :
object_speed = -speed ;
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW stop processing */
case 6 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -SPEED_MINI ) {
object_speed += accel_spd ;
if
( object_speed > -SPEED_MINI ) object_speed = -SPEED_MINI;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on /
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
}
if
( ( proc_no == 2 ) || ( proc_no == 5 ) ) {
if
(
if
timer_count == 0 ) {
( abs( object_speed - now_speed ) > SA_SPEED_MAX ) {
error_flag = ERR_NO2 ;
/* Set error No. */
}
}
}
if
( disp_co == 0 ) {
led_num(1, d_speed / 100 );
106
/* Number of revolutions */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
d_speed = 0 ;
disp_co = 100 ;
if
( abs(now_speed) == 0 ) {
disp_co = 0;
}
led_num(2, 1000/PWM_TS );
/* Carrier frequency */
led_num(3, cont_time );
/* Overall processing time */
led_num(4, cont_time1 );
/* Vector operation processing time */
}
if
( error_flag ) {
while( 1 ) {
OUT_data( LED41, ~0x00 ) ;
/* LED display off */
OUT_data( LED42, ~0x00 ) ;
timer_count = 50 ;
while( timer_count ) ;
if
( error_flag == ERR_NO1 ) {
OUT_data( LED41, ~0x9e ) ; /* E1 display */
OUT_data( LED42, ~0x60 ) ;
} else if ( error_flag == ERR_NO2 ) {
OUT_data( LED41, ~0x9e ) ; /* E2 display */
OUT_data( LED42, ~0xda ) ;
} else {
OUT_data( LED41, ~0x9e ) ; /* E3 display */
OUT_data( LED42, ~0xf2 ) ;
}
timer_count = 50 ;
while( timer_count ) ;
}
}
}
}
4.3.6
LED display function
/****************************************************************************** /
/*
LED value display subroutine
/*
no
/*
data : Display data (0 to 99)
: Display area number (1 to 4)
*/
*/
*/
/****************************************************************************** /
void led_num( int no, long data )
{
if
( no == 1 ) {
data = data % 10000;
OUT_data( LED11, ~led_pat[data/1000]&0xff ) ;
OUT_data( LED12, ~led_pat[(data%1000)/100]&0xff ) ;
OUT_data( LED13, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED14, ~led_pat[data%10]&0xff ) ;
} else if
( no == 2 ) {
Application Note U17324EJ1V0AN
107
CHAPTER 4 PROGRAM LIST
OUT_data( LED21, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED22, ~led_pat[data%10]&0xff ) ;
} else if
( no == 3 ) {
OUT_data( LED31, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED32, ~led_pat[data%10]&0xff ) ;
} else {
OUT_data( LED41, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED42, ~led_pat[data%10]&0xff ) ;
}
}
/****************************************************************************** /
/*
External I/O output subroutine
/*
reg
/*
data : Output data
*/
: Output register number
*/
*/
/****************************************************************************** /
void
OUT_data( unsigned short reg, unsigned short data )
{
if
( reg == WRESET ) {
P3.0 = 0;
data = 1;
/* Dummy step */
P3.0 = 1;
} else {
PDL = data | ( reg << 8 );
PDL = reg | ( reg << 8 ) | 0x8000;
}
}
/****************************************************************************** /
/*
External I/O input subroutine
/*
*/
reg : Input register number
*/
/****************************************************************************** /
unsigned short
IN_data( int reg )
{
unsigned char *po;
/*
*/
if
( reg == SW ) {
return P4;
} else {
return 0;
}
}
4.3.7
Motor control interrupt processing function
#include
"Common.h"
#include
"Motor.h"
#pragma
ioreg
/* Peripheral I/O register definition */
/****************************************************************************** /
/*
108
Motor control timer interrupt processing
Application Note U17324EJ1V0AN
*/
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
_ _interrupt
void
int_MOTOR(void)
{
ADA0M0 = 0xB0 ;
/* Start AD0 */
ADA1M0 = 0xA0 ;
/* Start AD1 */
}
/****************************************************************************** /
/*
Motor control processing
*/
/****************************************************************************** /
void
Motor_CONT(void)
{
signed int
wrm, wre, trm, tre ;
signed int
o_wre, we, o_iqap, o_iqa ;
signed int
ida, iqa, o_vda1, o_vqa1 ;
signed int
o_vd0, o_vq0, o_vda, o_vqa ;
signed int
o_vua, o_vva, o_vwa, wiua, wiva ;
signed int
s_time, wk ;
signed short now_enc, sa_enc ;
/*
*/
/****************************************************************************** /
/*
Calculation processing of speed and rotor position
*/
/****************************************************************************** /
now_enc = -TMENC10 ;
sa_enc = now_enc - before_enc ;
if
( abs( sa_enc ) > ( MAXPULSE / 2 ) ) {
if
( sa_enc < 0 ) {
sa_enc += MAXPULSE ;
} else {
sa_enc -= MAXPULSE ;
}
}
before_enc = now_enc ;
wrm = now_speed = (signed long)sa_enc * RAD / MAXPULSE;
/* Set current speed */
wre = wrm * P ;
tre = ( trm * P + OFFSET ) % RAD ;
/****************************************************************************** /
/*
Speed control processing
*/
/****************************************************************************** /
if
( ( stop_flag == OFF ) && ( error_flag == 0 ) ) {
s_time = TP1CNT ;
o_wre = object_speed * RPM_RADS * P / TH_U ;
we =
/* rpm -> radian conversion */
o_wre - wre ;
o_iqap
= ( ( wre * KSP ) + ( we * KSP ) ) >> KSPGETA ;
o_iqa
= o_iqap + ( o_iqai >> KSIGETA ) ;
if
( o_iqai > IQAMAX ) {
Application Note U17324EJ1V0AN
109
CHAPTER 4 PROGRAM LIST
o_iqai = IQAMAX ;
} else if ( o_iqai < -IQAMAX ) {
o_iqai = -IQAMAX ;
} else {
o_iqai += ( KSI * we ) ;
}
/****************************************************************************** /
/*
Current control processing
*/
/****************************************************************************** /
ida = ( ( ( iva * sin( tre ) ) - ( iua * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
iqa = ( ( ( iva * sin( tre + RAD/4 ) ) - ( iua * sin( tre + 11*RAD/12 ) ) ) ) >> SGETA ;
o_vda = ( KI * -ida ) >> KIGETA ;
o_vqa = ( KI * ( o_iqa
- iqa )
) >> KIGETA ;
/****************************************************************************** /
/*
3-phase voltage conversion processing
*/
/****************************************************************************** /
if ( init_flag == 2 ) {
/* CW initial processing */
o_trm += ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm > ( 2 * RAD ) ) {
init_flag = 0;
}
} else if ( init_flag == 3 ) {
/* CCW initial processing */
o_trm -= ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm < -( 2 * RAD ) ) {
init_flag = 0;
}
} else {
o_trm = 0 ;
}
o_vua = ( ( ( o_vda * sin( tre + RAD/4 ) ) - ( o_vqa * sin( tre ) ) ) ) >> SGETA ;
o_vva = ( ( ( o_vda * sin( tre + 11*RAD/12 ) ) - ( o_vqa * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
o_vwa
= -o_vua - o_vva ;
cont_time1 = ( TP1CNT - s_time ) ;
/* Convert to uSEC */
/****************************************************************************** /
/*
PWM conversion output processing
*/
/****************************************************************************** /
OUT_data( WRESET, 0 ) ;
/* Reset watchdog timer */
HZA0CTL0 |= 0x04;
/* PWM output on */
/* PWM counter value calculation output */
o_vua += ( PWM_DATA
110
/ 2 ) ;
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
o_vva += ( PWM_DATA
/ 2 ) ;
o_vwa += ( PWM_DATA
/ 2 ) ;
if
( o_vua <= 0 ) {
o_vua = 1 ;
} else if
( o_vua >= PWM_DATA
o_vua = PWM_DATA
) {
- 1 ;
}
if
( o_vva <= 0 ) {
o_vva = 1 ;
} else if
( o_vva >= PWM_DATA
o_vva = PWM_DATA
) {
- 1 ;
}
if
( o_vwa <= 0 ) {
o_vwa = 1 ;
} else if
( o_vwa >= PWM_DATA
o_vwa = PWM_DATA
) {
- 1 ;
}
TQ0CCR1 = o_vua ;
TQ0CCR2 = o_vva ;
TQ0CCR3 = o_vwa ;
} else {
HZA0CTL0 |= 0x08;
/* PWM output off */
now_speed = 0;
cont_time1 = 0;
}
}
4.3.8
10 mSEC interval interrupt processing function
/****************************************************************************** /
/*
Other timer interrupt processing (10 mSEC interval)
*/
/****************************************************************************** /
_ _multi_interrupt
void
int_ETC(void)
{
/* Wait timer processing */
if
( timer_count != 0 ) {
timer_count -= 1 ;
}
/* Acceleration/deceleration timer processing */
if
( accel_count != 0 ) {
accel_count -= 1 ;
}
/* */
if
( disp_co != 0 ) {
d_speed += abs( now_speed ) ;
Application Note U17324EJ1V0AN
111
CHAPTER 4 PROGRAM LIST
disp_co -= 1 ;
}
}
4.3.9
A/D converter interrupt processing function
/****************************************************************************** /
/*
A/D converter interrupt processing for U-phase current and speed volume */
/****************************************************************************** /
_ _multi_interrupt
void
int_AD0(void)
{
iua = (( ( ADA0CR0 >> 6 ) & 0x3ff ) - 0x200) ;
if
( abs(iua) > MAX_I ) {
HZA0CTL0 |= 0x08 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
volume = 1023 - ( ( ADA0CR1 >> 6 ) & 0x3ff ) ;
/* Set volume value */
Motor_CONT() ;
cont_time = TP1CNT;
/* Convert to uSEC */
}
/****************************************************************************** /
/*
A/D converter interrupt processing for volume
*/
/****************************************************************************** /
_ _interrupt
void
int_AD1(void)
{
iva = (( ADA1CR0 & 0x3ff ) - 0x200) ;
if
( abs(iva) > MAX_I ) {
HZA0CTL0 |= 0x08 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
}
4.3.10 Hardware initialization processing function
/****************************************************************************** /
/*
Hardware (peripheral I/O) initialization
*/
/****************************************************************************** /
void
hinit( void )
{
/* Port mode register initialization */
PM3 = 0xfe ;
PM4 = 0xff ;
PMDL
= 0x0000 ;
OUT_data( LED11, 0xff ) ;
/* LED OFF */
OUT_data( LED12, 0xff ) ;
112
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
OUT_data( LED13, 0xff ) ;
OUT_data( LED14, 0xff ) ;
OUT_data( LED21, 0xff ) ;
OUT_data( LED22, 0xff ) ;
OUT_data( LED31, 0xff ) ;
OUT_data( LED32, 0xff ) ;
OUT_data( LED41, 0xff ) ;
OUT_data( LED42, 0xff ) ;
/* Set 10 mSEC timer TMP0 */
TP0CTL0 = 0x05 ;
/* Select fXX/64 */
TP0CTL1 = 0x00 ;
/* Select interval timer mode */
TP0CCR0 = 10000 ;
/* 10 mSEC */
TP0CTL0 |= 0x80 ;
/* Start timer */
TP0CCIC0= 0x06;
/* Set motor control interrupt timer TMP1 */
TP1CTL0 = 0x05 ;
/* Select fXX/64 */
TP1CTL1 = 0x00 ;
/* Select interval timer mode */
TP1CCR0 = TS ;
/* TS uSEC */
TP1CTL0 |= 0x80 ;
/* Start timer */
TP1CCIC0= 0x01;
/* TMENC10 initialization */
TUM10 = 0x80 ;
/* Select UDC mode */
PRM10 = 0x07 ;
/* Select operation mode 4 */
TMC10 = 0x40 ;
/* Start count */
/* TMQ0 initialization */
TQ0CTL0 = 0x00;
/* fXX/2 (64 MHz/2 = 32 MHz) */
TQ0CTL1 = 0x07;
/* Select 6-phase PWM output mode */
TQ0IOC0 = 0x55;
/* Positive phase normal output, output enabled */
TQ0IOC1 = 0x00;
/* TIQ00 to TIQ03, EVTQ0, and TRGQ0 pins of TMQ0 */
TQ0IOC2 = 0x00;
/* are not used */
TQ0OPT0 = 0x00;
/* Select comparison mode */
TQ0CCR0 = PWM_DATA ;
/* Carrier wave cycle 20 kHz */
TQ0CCR1 = PWM_DATA /2;
/* Set U-phase duty to 50 */
TQ0CCR2 = PWM_DATA /2;
/* Set V-phase duty to 50 */
TQ0CCR3 = PWM_DATA /2;
/* Set W-phase duty to 50 */
TQ0DTC
/* Dead time 6 uSEC */
= 180;
TQ0OPT1 = 0x00;
TQ0OPT2 = 0x04;
/* No culling, no crest and valley interrupts are used */
/* • No culling between reloads */
/* • Clear and re-count dead time counter */
/* • Output A/D trigger output of INTTP0CC0 */
/*
interrupt during counting up */
/* • A/D trigger output of INTTP0CC0 interrupt enabled */
TQ0IOC3 = 0xfc;
/* Negative phase inverted output, output enabled */
PMC1 = 0x3F;
/* Alternate-function mode */
PFCE1 = 0x00;
/* Select TOQ0T1 to TOQ0T3 or TOQ0B1 to TOQ0B3 output */
PFC1 = 0x00;
Application Note U17324EJ1V0AN
113
CHAPTER 4 PROGRAM LIST
HZA0CTL0 = 0x00;
HZA0CTL0 = 0xD0;
/* High-impedance operation enabled */
/* TOQ0OFF pin rising edge valid */
HZA2CTL0 = 0x00;
/* High-impedance off due to analog input */
TQ0CTL0 |= 0x80;
/* Start 6-phase PWM output mode */
/* Set A/D */
ADA0M0 = 0x30 ;
/* ANI00, ANI01 */
ADA0M1 = 0x01 ;
ADA0S
= 0x01 ;
OP0CTL0 = 0x00 ;
/* Operational amplifier invalid */
OP0CTL1 = 0x00 ;
/* Comparator invalid */
AD0IC
= 0x03 ;
ADA1M0 = 0x20 ;
/* ANI10 */
ADA1M1 = 0x01 ;
ADA1S
= 0x00 ;
OP1CTL0 = 0x00 ;
/* Operational amplifier invalid */
OP1CTL1 = 0x00 ;
/* Comparator invalid */
AD1IC
= 0x03 ;
PIC4
= 0x01 ;
}
4.3.11 Common area initialization processing function
/****************************************************************************** /
/*
Common area initialization
*/
/****************************************************************************** /
void
ainit( void )
{
/* Initialization of flags */
error_flag = 0 ;
/* Clear error flag */
init_flag = OFF ;
/* Initial flag off */
disp_co = 100 ;
d_speed = 0 ;
/* Motor control area initialization */
stop_flag
= ON ;
/* Stop flag on */
object_speed = 0 ;
/* Target speed 0 */
o_iqai = 0 ;
/* Speed integral value 0 */
o_trm = 0 ;
before_enc = -TMENC10 ;
/* Set previous encoder value */
}
4.3.12 Revolution start initialization processing function
/****************************************************************************** /
/*
Revolution start initialization
*/
/****************************************************************************** /
114
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
void
start_init( void )
{
int
/*
i;
*/
for ( i = 0 ; i < 21 ; i++ ) before_posi[i][1] = 0;
total_sa = 0 ;
sum_speed = 0 ;
speed_co = 100000 / TS ;
}
4.3.13 sin calculation processing function
/****************************************************************************** /
/*
sin x
/*
*/
Data
*/
/*
Radian unit
*/
/*
Return value
*/
/*
Sign value*16384
*/
/****************************************************************************** /
int sin( int x )
{
x = x % RAD ;
if
( x < 0 ) x += RAD ;
if
( x < (RAD/4) ){
return sins( x ) ;
} else if ( x < (RAD/2) ) {
return sins( (RAD/2) - x ) ;
} else if ( x < (RAD*3/4) ) {
return -sins( x - (RAD/2) ) ;
} else {
return -sins( RAD - x ) ;
}
}
/*******************************************************************************/
int sins( int x )
{
short z1, z2, z3, z4, z5 ;
/*
*/
if
( x <= (RAD/8) ) {
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
z3 = z1 * z2 >> SGETA ;
z5 = z2 * z3 >> SGETA ;
return ( (12868*z1) - (1322*z3) + (40*z5) ) >> SGETA ;
} else {
x = (RAD/4) - x ;
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
Application Note U17324EJ1V0AN
115
CHAPTER 4 PROGRAM LIST
z4 = z2 * z2 >> SGETA ;
return ( (268432772) - (5050*z2) + (252*z4) ) >> SGETA ;
}
}
4.3.14 Link directive file for V850E/IA3
/****************************************************************************** /
/*
Link directive file for V850E/IA3
*/
/****************************************************************************** /
VECT_RESET: !LOAD ?RX V0x00000000 {
.vect_RESET
= $PROGBITS ?AX .vect_RESET;
};
ID_NO: !LOAD ?RX V0x00000070 {
.id_NO
= $PROGBITS ?AX .id_NO;
};
VECT_ETC: !LOAD ?RX V0x00000250 {
.vect_ETC
= $PROGBITS ?AX .vect_ETC;
};
VECT_MOTOR: !LOAD ?RX V0x00000280 {
.vect_MOTOR
= $PROGBITS ?AX .vect_MOTOR;
};
VECT_AD0: !LOAD ?RX V0x00000400 {
.vect_AD0
= $PROGBITS ?AX .vect_AD0;
};
VECT_AD1: !LOAD ?RX V0x00000410 {
.vect_AD1
= $PROGBITS ?AX .vect_AD1;
};
HANDLER: !LOAD ?RX V0x00001000 {
.handler
= $PROGBITS ?AX .handler;
};
TEXT:
!LOAD ?RX {
.text
= $PROGBITS ?AX .text;
};
CONST : !LOAD ?R {
.const = $PROGBITS ?A .const;
};
DATA : !LOAD ?RW
.data
V0xffffd800 {
= $PROGBITS ?AW
;
.sdata = $PROGBITS ?AWG ;
.sbss
= $NOBITS
?AWG ;
.bss
= $NOBITS
?AW
;
};
_ _tp_TEXT @ %TP_SYMBOL;
116
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
_ _gp_DATA @ %GP_SYMBOL &_ _tp_TEXT{DATA};
_ _ep_DATA @ %EP_SYMBOL;
Application Note U17324EJ1V0AN
117
CHAPTER 4 PROGRAM LIST
4.4
4.4.1
Program List (V850E/IA4)
Symbol definition
/****************************************************************************** /
/*
Common area
*/
/****************************************************************************** /
unsigned char
ram_start ;
unsigned char
error_flag ;
/* Error flag */
unsigned char
init_flag ;
/* Initial flag */
unsigned short cont_time ;
/* Interrupt control time uSEC */
unsigned short cont_time1 ;
/* Vector operation time uSEC */
unsigned short disp_co ;
/* Interrupt control time display timer */
unsigned short volume ;
/* Volume value */
unsigned short timer_count ;
/* Time wait counter */
unsigned short accel_count ;
/* Acceleration/deceleration operation time counter */
unsigned char
stop_flag ;
/* Stop flag */
signed short
before_posi[21][2] ; /* Position buffer */
signed short
total_sa ;
signed int
sum_speed ;
signed int
speed_co ;
signed int
now_speed ;
signed int
object_speed ;
/* Target speed rms */
unsigned int
d_speed ;
/* Display speed rms */
unsigned char
ram_end ;
/* Position total difference */
/* Present speed rms */
#pragma section const begin
const unsigned short led_pat[10] = { 0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0,
0xfe, 0xe6 } ;
#pragma section const end
/****************************************************************************** /
/*
Common flags
*/
/****************************************************************************** /
extern unsigned char
ram_start ;
extern unsigned char
error_flag ;
/* Error flag */
extern unsigned char
init_flag ;
/* Initial flag */
extern unsigned short
cont_time ;
/* Interrupt control time uSEC */
extern unsigned short
cont_time1 ;
/* Vector operation time uSEC */
extern unsigned short
disp_co ;
/* Interrupt control time display timer */
extern unsigned short
volume ;
/* Volume value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
extern unsigned char
stop_flag ;
extern signed short
before_posi[21][2] ; /* Position buffer */
extern signed short
total_sa ;
extern signed int
sum_speed ;
extern signed int
speed_co ;
/* time counter */
118
/* Stop flag */
/* Position total difference */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
extern signed int
now_speed ;
/* Present speed rms */
extern signed int
object_speed ;
/* Target speed rms */
extern unsigned int
d_speed ;
/* Display speed rms */
extern unsigned char
ram_end ;
#pragma section const begin
extern
const unsigned short led_pat[] ;;
#pragma section const end
/****************************************************************************** /
/*
Motor common definition
*/
/****************************************************************************** /
extern signed short
iua ;
/* U-phase current */
extern signed short
iva ;
/* V-phase current */
extern signed int
o_iqai ;
/* Speed integral value area */
extern signed int
o_trm ;
/* Target position for initialization */
extern signed int
base_position ;
/* Speed estimation value reference point */
extern unsigned int
sa_time ;
/* Speed measurement value */
extern unsigned short
timer_count ;
/* Time wait counter */
extern unsigned short
accel_count ;
/* Acceleration/deceleration operation */
/* time counter */
extern signed short
4.4.2
before_enc ;
/* Previous encoder value */
Constant definition
/****************************************************************************** /
/*
I/O
*/
/****************************************************************************** /
#define BASE_IO
0xc200000
#define LED11
0x03
#define LED12
0x02
#define LED13
0x01
#define LED14
0x00
#define LED21
0x05
#define LED22
0x04
#define LED31
0x07
#define LED32
0x06
#define LED41
0x09
#define LED42
0x08
#define DIPSW
0x0f
#define SW
0x0e
#define DA1
0x0a
#define DA2
0x0b
#define DA3
0x0c
#define WRESET
0x0d
#define MODE
0x10
/****************************************************************************** /
/*
Constant
*/
Application Note U17324EJ1V0AN
119
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
#define ON
1
#define OFF
0
#define CW
1
/* CW operation mode */
#define CCW
2
/* CCW operation mode */
#define STOP
0
/* Operation stop mode */
#define ERR_NO1
1
/* Overcurrent error */
#define ERR_NO2
2
/* Speed difference error
*/
/****************************************************************************** /
/*
Motor constant
*/
/****************************************************************************** /
#define PAI
3.14159265
/* π */
#define TH_U
1000
/* Radian value
#define RAD
(int)(2*PAI*TH_U)
/* Radian value of one revolution */
#define OFFSET
1733
/* Original point OFFSET */
#define RPM_RADS
(int)((2*PAI*TH_U)/60) /* rpm -> radian conversion constant */
jack-up constant */
/* Motor constant */
#define KSP
500
/* Speed proportion constant */
#define KSI
50
/* Speed integral constant */
#define KI
1
/* Current conversion constant */
#define P
2
/* Number of poles */
#define KSPGETA
0
/* KSP jack-up constant */
#define KSIGETA
9
/* KSI jack-up constant */
#define KIGETA
9
/* KI jack-up constant */
#define SGETA
14
/* sin jack-up constant */
#define PWM_TS
80
/* PWM cycle */
#define PWM_DATA
(PWM_TS/0.03125)
/* PWM set value */
#define SPEED_MAX
3000
/* Maximum speed rpm */
#define SPEED_MINI
600
/* Minimum speed rpm */
#define SA_SPEED_MAX
800
/* Maximum speed difference rpm */
#define IQAMAX
40000000
/* Maximum speed integral value */
#define MAX_I
800
/* Maximum current value */
#define TS
80
/* Motor control time interval uSEC */
#define WATCH_START
500
/* Speed monitor start time 10 mSEC */
#define ACCEL_VAL_1ST 50
/* Initial acceleration/deceleration */
/* time constant */
#define ACCEL_VAL
3
/* Acceleration/deceleration time constant */
#define ACCEL_SPD
50
/* Acceleration/deceleration constant */
#define MAXPULSE
10000
/* Number of pulses of one revolution */
/****************************************************************************** /
/*
Function constant
*/
/****************************************************************************** /
void
fcalcu( signed int *wrm, signed int *trm );
void
OUT_data( unsigned short reg, unsigned short data );
unsigned short IN_data( int reg );
void
120
led_num( int no, long data ) ;
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
/*
Motor-related common area
*/
/****************************************************************************** /
signed short
iua ;
/* U-phase current */
signed short
iva ;
/* V-phase current */
signed int
o_iqai ;
/* Speed integral value area */
signed int
o_trm ;
/* Target position for initialization */
signed int
base_position ;
/* Speed estimation value reference point */
unsigned int
sa_time ;
/* Speed measurement value */
unsigned short timer_count ;
/* Time wait counter */
unsigned short accel_count ;
/* Acceleration/deceleration operation time counter */
signed short
/* Previous encoder value */
4.4.3
before_enc ;
Interrupt handler setting
/****************************************************************************** /
/*
Interrupt symbol table
*/
/****************************************************************************** /
.extern _ _start
.extern _int_MOTOR
.extern _int_AD0
.extern _int_AD1
.extern _int_ETC
.globl V_RESET
.globl V_ETC
.globl V_MOTOR
.globl V_AD0
.globl V_AD1
#*******************************************************************************
.section ".handler",text
V_RESET:
Jr
_ _start
ld.w
[sp],r1
add
4,sp
jr
_int_ETC
V_ETC:
-- Other timers
V_MOTOR:
ld.w
[sp],r1
add
4,sp
jr
_int_MOTOR
ld.w
[sp],r1
add
4,sp
jr
_int_AD0
ld.w
[sp],r1
add
4,sp
-- Speed control timer
V_AD0:
-- A/D converter CH0
V_AD1:
Application Note U17324EJ1V0AN
121
CHAPTER 4 PROGRAM LIST
jr
_int_AD1
-- A/D converter CH1
.extern V_RESET
.extern V_ETC
.extern V_MOTOR
.extern V_AD0
.extern V_AD1
/****************************************************************************** /
/*
Interrupt jump table
*/
/****************************************************************************** /
.section ".vect_RESET",text
mov
#V_RESET,r1
jmp
[r1]
.section ".id_NO",text
.byte
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
.section ".vect_ETC",text
add
-4,sp
st.w
r1,[sp]
mov
#V_ETC,r1
jmp
[r1]
.section ".vect_MOTOR",text
add
-4,sp
st.w
r1,[sp]
mov
#V_MOTOR,r1
jmp
[r1]
.section ".vect_AD0",text
add
-4,sp
st.w
r1,[sp]
mov
#V_AD0,r1
jmp
[r1]
.section ".vect_AD1",text
4.4.4
add
-4,sp
st.w
r1,[sp]
mov
#V_AD1,r1
jmp
[r1]
Startup routine setting
#=============================================================================
# DESCRIPTIONS:
#
This assembly program is a sample of start-up module for ca850.
#
If you modified this program, you must assemble this file, and
#
locate a given directory.
122
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
#
#
Unless -G is specified, sections are located as the following.
#
#
#
#
|
:
|
|
:
|
tp -> -+-------------- + _ _start
#
| start up
#
|-------------- |
# text section
|
|
#
| user program
|
#
|
|
#
|-------------- |
#
| library
#
|
|
-+-------------- +
#
|
|
# sdata section
|
|
#
|
|
#
gp -> -+-------------- +
#
|
|
# sbss section
|
|
#
|
|
#
+-------------- + _ _stack
#
| stack area
|
# bss section
|
|
#
| 0x400 bytes
|
#
_ _sbss
sp -> -+-------------- + _ _stack + STACKSIZE
_ _ebss
|
:
|
#
|
:
|
|
:
|
#
ep -> -+-------------- + _ _ep_DATA
# tidata section
#
|
|
-+-------------- +
# sidata section
#
_ _ssbss
_ _esbss
#
#
_ _tp_TEXT
|
|
-+-------------- +
#
|
:
|
#
|
:
|
#
#=============================================================================
#----------------------------------------------------------------------------#
special symbols
#----------------------------------------------------------------------------.extern _ _tp_TEXT, 4
.extern _ _gp_DATA, 4
.extern _ _ep_DATA, 4
.extern _ _ssbss, 4
.extern _ _esbss, 4
.extern _ _sbss, 4
.extern _ _ebss, 4
Application Note U17324EJ1V0AN
123
CHAPTER 4 PROGRAM LIST
#----------------------------------------------------------------------------#
C program main function
#----------------------------------------------------------------------------.extern _main
#----------------------------------------------------------------------------#
dummy data declaration for creating sbss section
#----------------------------------------------------------------------------.sbss
.lcomm
_ _sbss_dummy, 0, 0
#----------------------------------------------------------------------------#
system stack
#----------------------------------------------------------------------------.set
STACKSIZE, 0x400
.bss
.lcomm
_ _stack, STACKSIZE, 4
#----------------------------------------------------------------------------#
start up
#
pointers:
tp - text pointer
#
gp - global pointer
#
sp - stack pointer
#
ep - element pointer
#
exit status is set to r10
#----------------------------------------------------------------------------.text
.align 4
.globl _ _start
.globl _exit
.globl _ _exit
_ _start:
mov
13,r10
-- 500 kHz to 64 MHz
st.b
r10,VSWC[r0]
-- Set peripheral I/O wait
mov
0x03,r10
-- PLL
st.b
r10,PLLCTL[r0]
mov
0x00,r10
st.b
r10,PRCMD[r0]
st.b
r10,PCC[r0]
nop
nop
nop
124
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
nop
nop
mov
#_ _tp_TEXT, tp
-- set tp register
mov
#_ _gp_DATA, gp
-- set gp register offset
add
tp, gp
-- set gp register
mov
#_ _stack+STACKSIZE, sp
-- set sp register
mov
#_ _ep_DATA, ep
-- set ep register
mov
#_ _ssbss, r13
-- clear sbss section
#
mov
#_ _esbss, r12
cmp
r12, r13
jnl
.L11
.L12:
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L12
mov
#_ _sbss, r13
mov
#_ _ebss, r12
cmp
r12, r13
jnl
.L14
st.w
r0, [r13]
add
4, r13
cmp
r12, r13
jl
.L15
jarl
_main, lp
.L11:
#
-- clear bss section
.L15:
.L14:
#
-- call main function
_ _exit:
halt
-- end of program
_ _startend:
nop
#
#
#-------------------------- end of start up module ----------------------------#
#
4.4.5
#
Main processing function
#include
"Common.h"
#include
"Motor.h"
#pragma
ioreg
/* Peripheral I/O register definition */
static int save_psw;
/****************************************************************************** /
Application Note U17324EJ1V0AN
125
CHAPTER 4 PROGRAM LIST
/*
3-phase motor control program
*/
/****************************************************************************** /
void
main()
{
unsigned char
proc_no ;
/* Present processing number */
signed int
speed ;
/* Indication speed rms */
signed int
accel_spd ;
int
sw, sw_mode ;
/* */
hinit() ;
/* Hardware initialization */
ainit() ;
/* Initialization of area used */
proc_no = 0 ;
_ _EI();
while( 1 ) {
accel_spd = ( SPEED_MAX - SPEED_MINI ) / 100;
speed
= ( ( SPEED_MAX - SPEED_MINI ) * volume
+ SPEED_MINI ;
sw =
if
/ 1024 )
/* Indication speed calculation by volume */
~IN_data( SW ) & 0x07 ;
/* Read operation button */
( sw == 1 ) {
sw_mode = CW ;
} else if
( sw == 2 ) {
sw_mode = CCW ;
} else if
( sw == 4 ) {
sw_mode = STOP ;
}
switch( proc_no ) {
/* STOP processing */
case 0 :
if
( sw_mode == CW ) {
_ _DI() ;
object_speed
= SPEED_MINI ; /* Set target speed to minimum value */
stop_flag = OFF ;
timer_count = WATCH_START ;
/* Set speed monitor start time to 5 SEC */
accel_count = ACCEL_VAL_1ST ;/* Set acceleration/deceleration counter */
init_flag = 2 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 1 ;
} else if
/* Set next processing number */
( sw_mode == CCW ) {
_ _DI() ;
stop_flag = OFF ;
/* Stop flag off */
object_speed = -SPEED_MINI ; /* Set target speed to minimum value */
timer_count = WATCH_START ;
/* Set speed monitor start time to 5 SEC */
accel_count = ACCEL_VAL_1ST ;/* Set acceleration/deceleration counter */
init_flag = 3 ;
/* CCW initial request */
start_init() ;
/* Initialize revolution start */
_ _EI() ;
proc_no = 4 ;
126
/* Set CCW processing number */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
}
break ;
/* CW processing, acceleration */
case 1 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< speed ) {
object_speed += accel_spd ;
if
( object_speed > speed ) object_speed = speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> speed ) {
object_speed -= accel_spd ;
if
( object_speed < speed ) object_speed = speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
proc_no = 2 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW processing, constant-speed */
case 2 :
object_speed = speed ;
if
( (sw_mode == CCW) || (sw_mode == STOP) ) {
proc_no = 3 ;
/* Deceleration, set processing number */
}
break ;
/* CW stop processing */
case 3 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
> SPEED_MINI ) {
object_speed -= accel_spd ;
if
( object_speed < SPEED_MINI ) object_speed = SPEED_MINI;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on */
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
/* CCW processing, acceleration */
case 4 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -speed ) {
Application Note U17324EJ1V0AN
127
CHAPTER 4 PROGRAM LIST
object_speed += accel_spd ;
if
( object_speed > -speed ) object_speed = -speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else if ( object_speed
> -speed ) {
object_speed -= accel_spd ;
if
( object_speed < -speed ) object_speed = -speed;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
proc_no = 5 ;
/* Constant-speed processing */
}
}
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW processing, constant-speed */
case 5 :
object_speed = -speed ;
if
( (sw_mode == CW) || (sw_mode == STOP) ) {
proc_no = 6 ;
/* Deceleration, set processing number */
}
break ;
/* CCW stop processing */
case 6 :
if
( accel_count == 0 ) {
accel_count = ACCEL_VAL ;
if
( object_speed
/* Set acceleration/deceleration counter */
< -SPEED_MINI ) {
object_speed += accel_spd ;
if
( object_speed > -SPEED_MINI ) object_speed = -SPEED_MINI;
timer_count = WATCH_START ;/* Set speed monitor start time to 5 SEC */
} else {
stop_flag = ON ;
/* Stop flag on */
proc_no = 0 ;
/* Set stop processing number */
}
}
break ;
}
if
( ( proc_no == 2 ) || ( proc_no == 5 ) ) {
if
(
if
timer_count == 0 ) {
( abs( object_speed - now_speed ) > SA_SPEED_MAX ) {
error_flag = ERR_NO2 ;
/* Set error No. */
}
}
}
if
( disp_co == 0 ) {
led_num(1, d_speed / 100 );
128
/* Number of revolutions */
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
d_speed = 0 ;
disp_co = 100 ;
if
( abs(now_speed) == 0 ) {
disp_co = 0;
}
led_num(2, 1000/PWM_TS );
/* Carrier frequency */
led_num(3, cont_time );
/* Overall processing time */
led_num(4, cont_time1 );
/* Vector operation processing time */
}
if
( error_flag ) {
while( 1 ) {
OUT_data( LED41, ~0x00 ) ;
/* LED display off */
OUT_data( LED42, ~0x00 ) ;
timer_count = 50 ;
while( timer_count ) ;
if
( error_flag == ERR_NO1 ) {
OUT_data( LED41, ~0x9e ) ;
/* E1 display */
OUT_data( LED42, ~0x60 ) ;
} else if ( error_flag == ERR_NO2 ) {
OUT_data( LED41, ~0x9e ) ;
/* E2 display */
OUT_data( LED42, ~0xda ) ;
} else {
OUT_data( LED41, ~0x9e ) ;
/* E3 display */
OUT_data( LED42, ~0xf2 ) ;
}
timer_count = 50 ;
while( timer_count ) ;
}
}
}
}
4.4.6
LED display function
/****************************************************************************** /
/*
LED value display subroutine
/*
no
/*
data : Display data (0 to 99)
: Display area number (1 to 4)
*/
*/
*/
/****************************************************************************** /
void led_num( int no, long data )
{
if
( no == 1 ) {
data = data % 10000;
OUT_data( LED11, ~led_pat[data/1000]&0xff ) ;
OUT_data( LED12, ~led_pat[(data%1000)/100]&0xff ) ;
OUT_data( LED13, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED14, ~led_pat[data%10]&0xff ) ;
} else if
( no == 2 ) {
Application Note U17324EJ1V0AN
129
CHAPTER 4 PROGRAM LIST
OUT_data( LED21, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED22, ~led_pat[data%10]&0xff ) ;
} else if
( no == 3 ) {
OUT_data( LED31, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED32, ~led_pat[data%10]&0xff ) ;
} else {
OUT_data( LED41, ~led_pat[(data%100)/10]&0xff ) ;
OUT_data( LED42, ~led_pat[data%10]&0xff ) ;
}
}
/****************************************************************************** /
/*
External I/O output subroutine
/*
reg
/*
data : Output data
*/
: Output register number
*/
*/
/****************************************************************************** /
void
OUT_data( unsigned short reg, unsigned short data )
{
if
( reg == WRESET ) {
P3.0 = 0;
data = 1;
/* Dummy step */
P3.0 = 1;
} else {
PDL = data | ( reg << 8 );
PDL = reg | ( reg << 8 ) | 0x8000;
}
}
/****************************************************************************** /
/*
External I/O input subroutine
/*
*/
reg : Input register number
*/
/****************************************************************************** /
unsigned short
IN_data( int reg )
{
unsigned char *po;
/*
*/
if
( reg == SW ) {
return P4;
} else {
return 0;
}
}
4.4.7
Motor control interrupt processing function
#include
"Common.h"
#include
"Motor.h"
#pragma
ioreg
/* Peripheral I/O register definition */
/****************************************************************************** /
/*
130
Motor control timer interrupt processing
Application Note U17324EJ1V0AN
*/
CHAPTER 4 PROGRAM LIST
/****************************************************************************** /
_ _interrupt
void
int_MOTOR(void)
{
ADA0M0 = 0xB0 ;
/* Start AD0 */
ADA1M0 = 0xA0 ;
/* Start AD1 */
}
/****************************************************************************** /
/*
Motor control processing
*/
/****************************************************************************** /
void
Motor_CONT(void)
{
signed int
wrm, wre, trm, tre ;
signed int
o_wre, we, o_iqap, o_iqa ;
signed int
ida, iqa, o_vda1, o_vqa1 ;
signed int
o_vd0, o_vq0, o_vda, o_vqa ;
signed int
o_vua, o_vva, o_vwa, wiua, wiva ;
signed int
s_time, wk ;
signed short now_enc, sa_enc ;
/*
*/
/****************************************************************************** /
/*
Calculation processing of speed and rotor position
*/
/****************************************************************************** /
now_enc = -TMENC10 ;
sa_enc = now_enc - before_enc ;
if
( abs( sa_enc ) > ( MAXPULSE / 2 ) ) {
if
( sa_enc < 0 ) {
sa_enc += MAXPULSE ;
} else {
sa_enc -= MAXPULSE ;
}
}
before_enc = now_enc ;
wrm = now_speed = (signed long)sa_enc * RAD / MAXPULSE;
/* Set current speed */
wre = wrm * P ;
tre = ( trm * P + OFFSET ) % RAD ;
/****************************************************************************** /
/*
Speed control processing
*/
/****************************************************************************** /
if
( ( stop_flag == OFF ) && ( error_flag == 0 ) ) {
s_time = TP1CNT ;
o_wre = object_speed * RPM_RADS * P / TH_U ;
/* rpm -> radian conversion */
we
=
o_iqap
= ( ( wre * KSP ) + ( we * KSP ) ) >> KSPGETA ;
o_iqa
= o_iqap + ( o_iqai >> KSIGETA ) ;
if
o_wre - wre ;
( o_iqai > IQAMAX ) {
Application Note U17324EJ1V0AN
131
CHAPTER 4 PROGRAM LIST
o_iqai = IQAMAX ;
} else if ( o_iqai < -IQAMAX ) {
o_iqai = -IQAMAX ;
} else {
o_iqai += ( KSI * we ) ;
}
/****************************************************************************** /
/*
Current control processing
*/
/****************************************************************************** /
ida = ( ( ( iva * sin( tre ) ) - ( iua * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
iqa = ( ( ( iva * sin( tre + RAD/4 ) ) - ( iua * sin( tre + 11*RAD/12 ) ) ) ) >> SGETA ;
o_vda = ( KI * -ida ) >> KIGETA ;
o_vqa = ( KI * ( o_iqa
- iqa )
) >> KIGETA ;
/****************************************************************************** /
/*
3-phase voltage conversion processing
*/
/****************************************************************************** /
if ( init_flag == 2 ) {
/* CW initial processing */
o_trm += ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm > ( 2 * RAD ) ) {
init_flag = 0;
}
} else if ( init_flag == 3 ) {
/* CCW initial processing */
o_trm -= ( SPEED_MINI * TS / 20000 ) ;
tre = ( o_trm * P ) % RAD ;
o_vda = 0 ;
o_vqa = 100 ;
if
( o_trm < -( 2 * RAD ) ) {
init_flag = 0;
}
} else {
o_trm = 0 ;
}
o_vua = ( ( ( o_vda * sin( tre + RAD/4 ) ) - ( o_vqa * sin( tre ) ) ) ) >> SGETA ;
o_vva = ( ( ( o_vda * sin( tre + 11*RAD/12 ) ) - ( o_vqa * sin( tre + 2*RAD/3 ) ) ) ) >> SGETA ;
o_vwa
= -o_vua - o_vva ;
cont_time1 = ( TP1CNT - s_time ) ;
/* Convert to uSEC */
/****************************************************************************** /
/*
PWM conversion output processing
*/
/****************************************************************************** /
OUT_data( WRESET, 0 ) ;
/* Reset watchdog timer */
HZA0CTL0 |= 0x04;
/* PWM output on */
/* PWM counter value calculation output */
o_vua += ( PWM_DATA
132
/ 2 ) ;
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
o_vva += ( PWM_DATA
/ 2 ) ;
o_vwa += ( PWM_DATA
/ 2 ) ;
if
( o_vua <= 0 ) {
o_vua = 1 ;
} else if
( o_vua >= PWM_DATA
o_vua = PWM_DATA
) {
- 1 ;
}
if
( o_vva <= 0 ) {
o_vva = 1 ;
} else if
( o_vva >= PWM_DATA
o_vva = PWM_DATA
) {
- 1 ;
}
if
( o_vwa <= 0 ) {
o_vwa = 1 ;
} else if
( o_vwa >= PWM_DATA
o_vwa = PWM_DATA
) {
- 1 ;
}
TQ0CCR1 = o_vua ;
TQ0CCR2 = o_vva ;
TQ0CCR3 = o_vwa ;
} else {
HZA0CTL0 |= 0x08;
/* PWM output off */
now_speed = 0;
cont_time1 = 0;
}
}
4.4.8
10 mSEC interval interrupt processing function
/****************************************************************************** /
/*
Other timer interrupt processing (10 mSEC interval)
*/
/****************************************************************************** /
_ _multi_interrupt
void
int_ETC(void)
{
/* Wait timer processing */
if
( timer_count != 0 ) {
timer_count -= 1 ;
}
/* Acceleration/deceleration timer processing */
if
( accel_count != 0 ) {
accel_count -= 1 ;
}
/* */
if
( disp_co != 0 ) {
d_speed += abs( now_speed ) ;
Application Note U17324EJ1V0AN
133
CHAPTER 4 PROGRAM LIST
disp_co -= 1 ;
}
}
4.4.9
A/D converter interrupt processing function
/****************************************************************************** /
/*
A/D converter interrupt processing for U-phase current and speed volume */
/****************************************************************************** /
_ _multi_interrupt
void
int_AD0(void)
{
iua = (( ( ADA0CR0 >> 6 ) & 0x3ff ) - 0x200) ;
if
( abs(iua) > MAX_I ) {
HZA0CTL0 |= 0x08 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
volume = 1023 - ( ( ADA0CR1 >> 6 ) & 0x3ff ) ;
/* Set volume value */
Motor_CONT() ;
cont_time = TP1CNT;
/* Convert to uSEC */
}
/****************************************************************************** /
/*
A/D converter interrupt processing for volume
*/
/****************************************************************************** /
_ _interrupt
void
int_AD1(void)
{
iva = (( ADA1CR0 & 0x3ff ) - 0x200) ;
if
( abs(iva) > MAX_I ) {
HZA0CTL0 |= 0x08 ;
/* PWM output off */
error_flag = ERR_NO1 ;
/* Set error No. */
}
}
4.4.10 Hardware initialization processing function
/****************************************************************************** /
/*
Hardware (peripheral I/O) initialization
*/
/****************************************************************************** /
void
hinit( void )
{
/* Port mode register initialization */
PM3 = 0xfe ;
PM4 = 0xff ;
PMDL
= 0x0000 ;
OUT_data( LED11, 0xff ) ;
/* LED OFF */
OUT_data( LED12, 0xff ) ;
134
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
OUT_data( LED13, 0xff ) ;
OUT_data( LED14, 0xff ) ;
OUT_data( LED21, 0xff ) ;
OUT_data( LED22, 0xff ) ;
OUT_data( LED31, 0xff ) ;
OUT_data( LED32, 0xff ) ;
OUT_data( LED41, 0xff ) ;
OUT_data( LED42, 0xff ) ;
/* Set 10 mSEC timer TMP0 */
TP0CTL0 = 0x05 ;
/* Select fXX/64 */
TP0CTL1 = 0x00 ;
/* Select interval timer mode */
TP0CCR0 = 10000 ;
/* 10 mSEC */
TP0CTL0 |= 0x80 ;
/* Start timer */
TP0CCIC0= 0x06;
/* Set motor control interrupt timer TMP1 */
TP1CTL0 = 0x05 ;
/* Select fXX/64 */
TP1CTL1 = 0x00 ;
/* Select interval timer mode */
TP1CCR0 = TS ;
/* TS uSEC */
TP1CTL0 |= 0x80 ;
/* Start timer */
TP1CCIC0= 0x01;
/* TMENC10 initialization */
TUM10 = 0x80 ;
/* Select UDC mode */
PRM10 = 0x07 ;
/* Select operation mode 4 */
TMC10 = 0x40 ;
/* Start count */
/* TMQ0 initialization */
TQ0CTL0 = 0x00;
/* fXX/2 (64 MHz/2 = 32 MHz) */
TQ0CTL1 = 0x07;
/* Select 6-phase PWM output mode */
TQ0IOC0 = 0x55;
/* Positive phase normal output, output enabled */
TQ0IOC1 = 0x00;
/* TIQ00 to TIQ03, EVTQ0, and TRGQ0 pins of TMQ0 */
TQ0IOC2 = 0x00;
/* are not used */
TQ0OPT0 = 0x00;
/* Select comparison mode */
TQ0CCR0 = PWM_DATA ;
/* Carrier wave cycle 20 kHz */
TQ0CCR1 = PWM_DATA /2;
/* Set U-phase duty to 50 */
TQ0CCR2 = PWM_DATA /2;
/* Set V-phase duty to 50 */
TQ0CCR3 = PWM_DATA /2;
/* Set W-phase duty to 50 */
TQ0DTC
/* Dead time 6 uSEC */
= 180;
TQ0OPT1 = 0x00;
TQ0OPT2 = 0x04;
/* No culling, no crest and valley interrupts are used */
/* • No culling between reloads */
/* • Clear and re-count dead time counter */
/* • Output A/D trigger output of INTTP0CC0 interrupt */
/*
during counting up */
/* • A/D trigger output of INTTP0CC0 interrupt enabled */
TQ0IOC3 = 0xfc;
/* Negative phase inverted output, output enabled */
PMC1 = 0x3F;
/* Alternate-function mode */
PFCE1 = 0x00;
/* Select TOQ0T1 to TOQ0T3 or TOQ0B1 to TOQ0B3 output */
PFC1 = 0x00;
Application Note U17324EJ1V0AN
135
CHAPTER 4 PROGRAM LIST
HZA0CTL0 = 0x00;
HZA0CTL0 = 0xD0;
/* High-impedance operation enabled */
/* TOQ0OFF pin rising edge valid */
HZA2CTL0 = 0x00;
/* High-impedance off due to analog input */
TQ0CTL0 |= 0x80;
/* Start 6-phase PWM output mode */
/* Set A/D */
ADA0M0 = 0x30 ;
/* ANI00, ANI01 */
ADA0M1 = 0x01 ;
ADA0S
= 0x01 ;
OP0CTL0 = 0x00 ;
/* Operational amplifier invalid */
OP0CTL1 = 0x00 ;
/* Comparator invalid */
AD0IC
= 0x03 ;
ADA1M0 = 0x20 ;
/* ANI10 */
ADA1M1 = 0x01 ;
ADA1S
= 0x00 ;
OP1CTL0 = 0x00 ;
/* Operational amplifier invalid */
OP1CTL1 = 0x00 ;
/* Comparator invalid */
AD1IC
= 0x03 ;
}
4.4.11 Common area initialization processing function
/****************************************************************************** /
/*
Common area initialization
*/
/****************************************************************************** /
void
ainit( void )
{
/* Initialization of flags */
error_flag = 0 ;
/* Clear error flag */
init_flag = OFF ;
/* Initial flag off */
disp_co = 100 ;
d_speed = 0 ;
/* Motor control area initialization */
stop_flag
= ON ;
/* Stop flag on */
object_speed = 0 ;
/* Target speed 0 */
o_iqai = 0 ;
/* Speed integral value 0 */
o_trm = 0 ;
before_enc = -TMENC10 ;
/* Set previous encoder value */
}
4.4.12 Revolution start initialization processing function
/****************************************************************************** /
/*
Revolution start initialization
*/
/****************************************************************************** /
void
136
start_init( void )
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
{
int
/*
i;
*/
for ( i = 0 ; i < 21 ; i++ ) before_posi[i][1] = 0;
total_sa = 0 ;
sum_speed = 0 ;
speed_co = 100000 / TS ;
}
4.4.13 sin calculation processing function
/****************************************************************************** /
/*
sin x
/*
*/
Data
*/
/*
Radian unit
*/
/*
Return value
*/
/*
Sign value*16384
*/
/****************************************************************************** /
int sin( int x )
{
x = x % RAD ;
if
if
( x < 0 ) x += RAD ;
( x < (RAD/4) ){
return sins( x ) ;
} else if ( x < (RAD/2) ) {
return sins( (RAD/2) - x ) ;
} else if ( x < (RAD*3/4) ) {
return -sins( x - (RAD/2) ) ;
} else {
return -sins( RAD - x ) ;
}
}
/*******************************************************************************/
int sins( int x )
{
short z1, z2, z3, z4, z5 ;
/*
*/
if
( x <= (RAD/8) ) {
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
z3 = z1 * z2 >> SGETA ;
z5 = z2 * z3 >> SGETA ;
return ( (12868*z1) - (1322*z3) + (40*z5) ) >> SGETA ;
} else {
x = (RAD/4) - x ;
z1 = (x << SGETA) / (RAD/8) ;
z2 = z1 * z1 >> SGETA ;
z4 = z2 * z2 >> SGETA ;
Application Note U17324EJ1V0AN
137
CHAPTER 4 PROGRAM LIST
return ( (268432772) - (5050*z2) + (252*z4) ) >> SGETA ;
}
}
4.4.14 Link directive file for V850E/IA4
/****************************************************************************** /
/*
Link directive file for V850E/IA4
*/
/****************************************************************************** /
VECT_RESET: !LOAD ?RX V0x00000000 {
.vect_RESET
= $PROGBITS ?AX .vect_RESET;
};
ID_NO: !LOAD ?RX V0x00000070 {
.id_NO
= $PROGBITS ?AX .id_NO;
};
VECT_ETC: !LOAD ?RX V0x00000250 {
.vect_ETC
= $PROGBITS ?AX .vect_ETC;
};
VECT_MOTOR: !LOAD ?RX V0x00000280 {
.vect_MOTOR
= $PROGBITS ?AX .vect_MOTOR;
};
VECT_AD0: !LOAD ?RX V0x00000400 {
.vect_AD0
= $PROGBITS ?AX .vect_AD0;
};
VECT_AD1: !LOAD ?RX V0x00000410 {
.vect_AD1
= $PROGBITS ?AX .vect_AD1;
};
HANDLER: !LOAD ?RX V0x00001000 {
.handler
= $PROGBITS ?AX .handler;
};
TEXT:
!LOAD ?RX {
.text
= $PROGBITS ?AX .text;
};
CONST : !LOAD ?R {
.const = $PROGBITS ?A .const;
};
DATA : !LOAD ?RW
.data
V0xffffd800 {
= $PROGBITS ?AW
;
.sdata = $PROGBITS ?AWG ;
.sbss
= $NOBITS
?AWG ;
.bss
= $NOBITS
?AW
;
};
_ _tp_TEXT @ %TP_SYMBOL;
_ _gp_DATA @ %GP_SYMBOL &_ _tp_TEXT{DATA};
138
Application Note U17324EJ1V0AN
CHAPTER 4 PROGRAM LIST
_ _ep_DATA @ %EP_SYMBOL;
Application Note U17324EJ1V0AN
139