Motor Control by uPD78F0714 Hall IC 120 degrees Excitation

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
Motor Control by μPD78F0714
Hall IC 120° Excitation Method
μPD78F0714
Document No. U18774EJ1V0AN00 (1st edition)
Date Published October 2007 N
2007
Printed in Japan
[MEMO]
2
Application Note U18774EJ1V0AN
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 U18774EJ1V0AN
3
• The information in this document is current as of May, 2007. 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 U18774EJ1V0AN
INTRODUCTION
Target Readers
This application note is intended for users who understand the functions of the
μPD78F0714, and who design application systems that use these functions.
The
applicable product is shown below.
• μPD78F0714
Purpose
The purpose of this application note is to help the user understand the system that
drives a brushless DC motor (BLDCM) equipped with Hall ICs via the 120° excitation
method, by using the μPD78F0714.
Organization
How to Read This Manual
This application note is divided into the following sections.
• General information
• System overview
• BLDCM control principle
• Control program
It is assumed that the readers of this application note have general knowledge in the
fields of electrical engineering, logic circuits, and microcontrollers.
For details of hardware functions (especially register functions, setting methods, etc.)
and electrical specifications
→ See the μPD78F0714 User’s Manual (U16928E).
For details of instruction functions
→ See the 78K/0 Series Instructions User’s Manual (U12326E).
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:
G (giga):
230 = 1,0243
Word:
32 bits
Halfword:
16 bits
Byte:
8 bits
Application Note U18774EJ1V0AN
5
Related documents
The related documents indicated in this publication may include preliminary versions.
However, preliminary versions are not marked as such.
Documents related to the device
Document Name
Document No.
μPD78F0714 User’s Manual
U16928E
78K/0 Series Instructions User’s Manual
U12326E
Inverter Control by μPD78F0714 120° Excitation Method Control by
U17297E
Zero-Cross Detection Application Note
Single-Phase Induction Motor Control by μPD78F0714 Two-Phase Sine
U17481E
Wave Inverter Drive via V/f Control Application Note
Motor Control by μPD78F0714 Hall IC 120° Excitation Method
This manual
Application Note
Motor Control by μPD78F0714 Sensorless (BEMF) 120° Excitation
U18051E
Method Application Note
Documents related to development software tools (user’s manuals)
Document Name
RA78K0 Ver. 3.80 Assembler Package
Document No.
Operation
U17199E
Language
U17198E
Structured Assembly
U17197E
Language
CC78K0 Ver. 3.70 C Compiler
SM+ System Simulator
ID78K0-QB Ver. 2.81 Integrated Debugger
Operation
U17201E
Language
U17200E
Operation
U17246E
User Open Interface
U17247E
Operation
U16996E
PM plus Ver.5.20
U16934E
Documents related to development hardware tools (user’s manuals)
Document Name
QB-78K0KX1H In-circuit Emulator
Document No.
U17081E
Documents related to flash memory writing
Document Name
PG-FP3 Flash Memory Programmer User’s Manual
U13502E
PG-FP4 Flash Memory Programmer User’s Manual
U15260E
Caution
The related documents listed above are subject to change without
notice.
Be sure to use the latest version of each document for
designing.
6
Document No.
Application Note U18774EJ1V0AN
Other related documents
Document Name
Document No.
SEMICONDUCTOR SELECTION GUIDE - Products and Packages -
X13769X
Semiconductor Device Mount Manual
Note
Quality Grades on NEC Semiconductor Devices
C11531E
NEC Semiconductor Device Reliability/Quality Control System
C10983E
Guide to Prevent Damage for Semiconductor Devices by Electrostatic
C11892E
Discharge (ESD)
Note See the “Semiconductor Device Mount Manual” website
(http://www.necel.com/pkg/en/mount/index.html)
Caution
The related documents listed above are subject to change without
notice.
Be sure to use the latest version of each document for
designing.
Application Note U18774EJ1V0AN
7
CONTENTS
CHAPTER 1 GENERAL INFORMATION.......................................................................................9
1.1
Operating Environment .......................................................................................................9
1.2
Related Manuals .................................................................................................................9
CHAPTER 2 BLDCM CONTROL PRINCIPLE ..........................................................................10
2.1
Rotation Direction Definition..............................................................................................10
2.2
Rotation Principle ..............................................................................................................10
2.3
Excitation Patterns ............................................................................................................11
2.4
Inverter ..............................................................................................................................11
2.5
120° Excitation Method .....................................................................................................12
2.6
Position Information...........................................................................................................13
2.7
Excitation Pattern Switching..............................................................................................13
2.8
Speed Detection................................................................................................................13
2.9
Voltage Control..................................................................................................................13
2.10
Speed Control ...................................................................................................................13
2.10.1
PID control ..................................................................................................................14
CHAPTER 3 SYSTEM OVERVIEW ............................................................................................15
3.1
Configuration.....................................................................................................................15
3.2
Interface ............................................................................................................................16
3.3
Functions...........................................................................................................................17
3.4
Peripheral I/Os ..................................................................................................................18
3.5
Interrupt.............................................................................................................................18
CHAPTER 4 CONTROL PROGRAM...........................................................................................19
4.1
Excitation Patterns ............................................................................................................19
4.1.1
Low-voltage inverter set ..............................................................................................19
4.2
Excitation Pattern Switching..............................................................................................20
4.3
Speed Detection................................................................................................................20
4.4
Voltage Control..................................................................................................................21
4.5
Speed Control ...................................................................................................................21
4.5.1
PID control ..................................................................................................................21
4.6
Module Configuration ........................................................................................................22
4.7
Control Program Functions and Flowcharts ......................................................................23
4.7.1
Function list.................................................................................................................23
4.7.2
Flowcharts...................................................................................................................25
4.8
Variables and Constants of Control Program ....................................................................45
4.8.1
Values defined by #define of main.h ...........................................................................45
4.8.2
Values defined by #define of lib_eu.h..........................................................................46
4.8.3
Variables .....................................................................................................................48
4.9
Control Program Source File.............................................................................................49
APPENDIX ........................................................................................................................................61
8
Application Note U18774EJ1V0AN
CHAPTER 1
GENERAL INFORMATION
This system uses 120° excitation to drive a brushless DC motor (hereinafter referred to as
“BLDCM”) equipped with Hall ICs.
•
This system (sample program) uses an NEC Electronics motor starter kit (μPD78F0714)Note
and uses 120° excitation to drive a BLDCM equipped with Hall ICs.
•
The control gain is adjusted in accordance with the motor in the operating environment
specified below.
Note
1.1
For the motor starter kit (μPD78F0714), contact an NEC Electronics sales representative.
Operating Environment
This system is created on the assumption that it will be used in the following environment.
•
Motor starter kit (μPD78F0714) board set
•
Low-voltage inverter set
BLDCM PITTMAN (N2311A011)
• Reference voltage [V]:
12
• No-load rotation speed [r/min]: 7197
• Continuous torque [Nm]:
0.11
• Maximum torque [Nm]:
0.23
• Drive coil:
3 phases (Y connection)
• Magnetic pole rotor:
4 poles (2 sets of poles)
• Stator:
6 throttles
• Position sensor:
Hall IC
•
PM plus environment platform V5.20
•
CC78K0 compiler V3.60
•
RA78K0 assembler V3.70
•
DF0714.78K device file V1.10
1.2
Related Manuals
For the development environment and board, see the following manuals.
•
Low-Voltage Motor Starter Kit Manual
•
PM plus Ver. 5.20 User’s Manual
•
Each User’s Manual of CC78K0 Ver. 3.60 C Compiler
•
Each User’s Manual of RA78K0 Ver. 3.70 Assembler Package
Application Note U18774EJ1V0AN
9
CHAPTER 2
BLDCM
CONTROL PRINCIPLE
A BLDCM rotates when its rotor, on which permanent magnets are mounted, rotates by the action
of magnetic fields that are generated by the stator coils.
2.1
Rotation Direction Definition
First, the rotation direction of the motor is defined.
The rotation direction of the motor is either CW (clockwise) or CCW (counterclockwise).
CW or CCW is determined based on the direction in which the object to be rotated by the
motor is rotated. As shown below, the rotation direction is based on when the surface on
which the motor axis is located faces towards the object.
Figure 2-1. Motor Rotation Direction
2.2
Rotation Principle
This section describes the BLDCM rotation principle.
The BLDCM shown in the next figure is a 3-phase bipolar 3-slot Y-connection inner-rotor type
SPM (Surface Permanent Magnet: a structure having permanent magnets placed onto the
surface).
Rotation is caused by the magnetic torque generated by attraction and repulsion of the stator
poles and the poles on the permanent magnets of the rotor.
Figure 2-2. BLDCM Rotation Principle
Flux direction
Excitation U → V
Excitation U
→W
Repulsion
U
Coil
S pole
S
N
N
pole
Rotor
N
N
pole
Stator
W
Rotation direction
S pole
Rotation direction
S
V
Attraction
U
V
W
The polarity of the stator depends on the direction of the coiled winding.
When the magnetic polarity of the rotor is reversed, the rotation direction is reversed.
10
Application Note U18774EJ1V0AN
CHAPTER
2.3
2
BLDCM
CONTROL PRINCIPLE
Excitation Patterns
The following figure shows excitation patterns and the relation between the current flux
direction (stator poles) generated by the coil and magnetic polarity of the rotor.
Figure 2-3. Excitation Patterns and Coil Flux Directions
U
N
V
U
W
V
N
W
V
W
Excitation pattern 4
V+ →U−
Excitation pattern 5
W+ →U−
U
V
U
N
Excitation pattern 6
W+ →V−
N
Excitation pattern 3
U+ →W−
Excitation pattern 2
U+ →W−
Excitation pattern 1
U+ →V−
U
W
V
U
N
N
W
V
W
The polarity of the coils depends on the direction of the coiled winding.
2.4
Inverter
A BLDCM, which does not have brushes or a rectifier, uses an inverter to change the
direction of the current with respect to the coils.
The following figure shows the connections of the 3-phase Y-connection BLDCM and
inverter.
Figure 2-4. Inverter and BLDCM
Inverter
DC power supply
+
U+
V+
W+
V
U−
N
S
V−
W−
−
U
W
BLDCM
The six switching elements control the excitation time and excitation direction.
Application Note U18774EJ1V0AN
11
CHAPTER
2.5
2
BLDCM
CONTROL PRINCIPLE
120° Excitation Method
The following figure shows the status (voltage, current) of phase U when a
rectangular-waveform voltage is applied to each phase in the excitation patterns shown in 2.3.
In phase U, a 120° excitation period and a 60° non-excitation period occur repeatedly.
This method, which performs excitation with the period of each phase being 120 degrees, is
called the 120° excitation method.
Figure 2-5.
120° Excitation Pattern and Current
U phase current
U phase voltage
60
240
120
180
0
300
120
degrees
Excitation pattern
1
Excitation
direction
2
3
U+
V−
4
5
V+
W−
U−
This system uses 120° excitation to drive a BLDCM.
12
6
W+
Application Note U18774EJ1V0AN
V−
Electrical
angle
CHAPTER
2.6
2
BLDCM
CONTROL PRINCIPLE
Position Information
With the 120° excitation method, position information is required for switching the inverter.
The following figure illustrates an example of the output of the magnetic pole position
sensors by using Hall ICs.
Figure 2-6.
H1
θ
N
S
Angle θ
N
H3
H2
Hall IC Output
0
60
120
180
300
240
360
H1
H2
H3
Electromotive force of output pin
The positions of Hall ICs that change every 180 degrees can be detected every 60 degrees,
by placing three Hall ICs every 120 degrees.
The following table shows an example of combining the values of the Hall ICs, excitation
patterns (see 2.3), and position information (see Figure 2-6).
Table 2-1.
Relation Between Hall ICs and Excitation Patterns
Excitation
Pattern
1
2
3
4
5
6
Hall IC
H1
H2
H3
Lo
Lo
Hi
Hi
Lo
Hi
Hi
Lo
Lo
Hi
Hi
Lo
Lo
Hi
Lo
Lo
Hi
Hi
When the rotor has four magnetic poles, the value of a Hall IC changes every 90 degrees
and the excitation pattern is switched every 30 degrees.
Usually, one cycle (six excitation patterns) is defined as 360 degrees of electrical angle and
one rotation of the motor axis is defined as 360 degrees of mechanical angle (all angles in this
document are electrical angles unless otherwise specified).
Mechanical angle:
Electrical angle/Number of pole pairs
Number of pole pairs: Number of poles/2
2.7
Excitation Pattern Switching
The excitation pattern is set by taking into consideration the rotation direction from the
magnetic pole position information of the rotor by the Hall ICs.
2.8
Speed Detection
The rotation speed of the motor is derived from calculating the period of time over which the
values of the Hall ICs change.
2.9
Voltage Control
The voltage to be applied to the motor coil is controlled by PWM (Pulse Width Modulation)
which adjusts the conduction rate (average voltage) through a chopper operation of the
conduction period of any of the switching elements at a high frequency, with a rectangular
waveform of excitation at 120 degrees.
2.10 Speed Control
The speed is controlled by changing the voltage applied to the motor coil (PWM conduction
rate) through PID control.
Application Note U18774EJ1V0AN
13
CHAPTER
2
BLDCM
CONTROL PRINCIPLE
2.10.1 PID control
A PID control action is performed by using a deviation between a specified speed and the
detected rotation speed to change the conduction rate of the PWM voltage control method
(hereinafter referred to as “duty factor”).
A PID control action derives the manipulated variable of the PWM duty factor from a
proportional (P) action that produces an output in proportion to a deviation, an integral (I)
action that produces an output in proportion to the integral of the deviation, and a derivative
(D) action that produces an output in proportion to the derivative of the deviation.
14
Application Note U18774EJ1V0AN
CHAPTER 3
SYSTEM
OVERVIEW
This chapter presents an overview of the system.
3.1
Configuration
The following figure shows the configuration of this system.
Figure 3-1.
System Configuration
Motor starter kit
Motor control
I/O board
Motor drive
module (board)
M-78F0714
(CPU board)
UVW
External
power supply
Hall IC
signal power
supply/GND
DC15 V
BLDCM equipped with Hall ICs
The system consists of a motor drive module that drives the motor, a motor control I/O board
equipped with a switch that controls the motor, and M-78F0714 having a CPU.
A BLDCM has three phases and four poles (two pairs of poles), and is equipped with Hall
ICs.
The block configuration of the motor starter kit is shown below.
Figure 3-2.
Block Configuration
Motor drive module
DC
15 V
Inverter control
signal
+15 V
Overcurrent detection
signal
3
CPU
UPD78F0714
FORWARD
REVERSE
MODE
BLDCM
6
RESET
START/
STOP
U
V
W
Inverter
External
power
supply
Hall IC
signal
GND
+5 V
SPEED
(analog)
M-78F0714
12
8888
7-segment LED
Motor control I/O
board
The motor is controlled by the switch on the motor control I/O board.
Application Note U18774EJ1V0AN
15
CHAPTER
3.2
3
SYSTEM
OVERVIEW
Interface
Table 3-1 lists the user interface functions.
Function
Name
RESET
START/STOP
FORWARD
REVERSE
MODE
SPEED
7-segment
LED
Table 3-1. User Interface
Parts
Function
Number
SW1
Reset
SW2
Start/stop
SW3
Rotation direction (Right rotation,
clockwise, CW)
SW4
Rotation direction (Left rotation,
counterclockwise, CCW)
SW5
Switching speed specification
Switching speed display
R52
Changing specified speed
DISP1
Displaying speed (rpm)Note
DISP2
DISP3
DISP4
Note The specified speed is always displayed while the motor is stopped.
A dot (“.”) is displayed at the lower right while the specified speed is fixed.
The rotation speed is displayed while the motor is rotating.
The specified speed is displayed while the MODE switch is depressed when the motor
rotates.
Reset continues while RESET is depressed and is released when it is released.
“SELF” is displayed for 1 second immediately after reset release.
Table 3-2 lists errors.
Error
Hardware
overcurrent
Hall IC
System
failure
Table 3-2.
LED Indication
0.C.
HALL
FAIL
Errors
Situation
Motor current is abnormal.
Hall IC value is abnormal.
Motor is not rotating.
Table 3-3 lists the interfaces of the μPD78F0714 pin.
Pin Number
8
27 to 32
11
10
9
49 to 52
Table 3-3.
Pin Name
RESET
TW0TO0 to TW0TO5
P01/INTP1
P02/INTP2
P03/INTP3
P64 to P67
56
P73
55
P72
54
P71
53
P70
41 to 48
P40 to P47
12
TW0TOFFP/INTP0
60
ANI4
20
P53/TI000/INTP5
21
P54
Note Shorts out 1-2 of 2JP7.
16
Interface of Pin
Function
RESET (SW1)Note
3-phase PWM inverter selection
Hall IC signal (HALL1)
Hall IC signal (HALL2)
Hall IC signal (HALL3)
7-segment LED selection (LD_LED0 to
LD_LED3)
START/STOP (SW2)
FORWARD (SW3)
REVERSE (SW4)
MODE (SW5)
Output data to 7-segment LED
Overcurrent detection (+5 V → 0 V)
Speed change (R52)
Timer capture trigger
Motor drive module control
Application Note U18774EJ1V0AN
CHAPTER
3.3
3
SYSTEM
OVERVIEW
Functions
Table 3-4 lists the functions and operation overview of this system.
Table 3-4.
Function
Start (power supply)
System Functions and Operation Overview (1/2)
Overview
• “SELF” is displayed with LEDs for 1 second.
• Specified speed (rpm) of SPEED volume is displayed with LEDs.
RESET switch
• System is restarted regardless of the status of motor control.
START/STOP
While motor control is stopped: Motor control is started.
switch
• “0” is displayed with LEDs.
• Motor starts rotating clockwise.
• Motor revolution speed (rpm) is displayed with LEDs.
While motor is controlled: Motor control is stopped.
• Motor revolution speed (rpm) is displayed with LEDs until it
reaches 0.
• Specified speed (rpm) of SPEED volume is displayed with
LEDs.
START and STOP do not toggle even if this switch is depressed.
FORWARD switch
While motor control is stopped: Does not function.
• Does not change.
While motor is controlled: Rotation direction is changed.
• If rotation direction is CCW, it is changed to CW after being
stopped once.
• If rotation direction is CW, it is not changed.
REVERSE switch
While motor control is stopped: Does not function.
• Does not change.
While motor is controlled: Rotation direction is changed.
• If rotation direction is CCW, it is not changed.
• If rotation direction is CW, it is changed to CCW after being
stopped once.
MODE switch
While motor control is stopped: Speed specification is switched.
• Change of specified speed by SPEED volume is enabled or
disabled.
• When disabled, a dot (“.”) is displayed to the lower right of the
LED value.
While motor is controlled: Changes display with LEDs.
• Specified speed (rpm) of SPEED volume is displayed with
LEDs while this switch is depressed.
SPEED volume
While motor control is stopped: Changes specified speed.
• Speed (rpm) displayed with LEDs changes in range of 200 to
3200.
• No change if disabled with MODE switch.
While motor is controlled: Changes specified speed.
• Motor rotates at specified speed (average).
Hardware
• Occurs if permissible current of motor drive module is exceeded.
overcurrent
• Motor control is stopped.
• “0.C.” is displayed with LEDs.
• Functions other than RESET switch are disabled.
No rotation
• If motor does not rotate for 2 seconds or longer from START and if
motor has stopped for 0.05 seconds or longer during rotation.
• Motor control is stopped.
• “FAIL” is displayed with LEDs.
• Functions other than RESET switch are disabled.
Caution The operation is not guaranteed if two or more switches are pressed at the
same time.
Application Note U18774EJ1V0AN
17
CHAPTER
3
SYSTEM
OVERVIEW
Table 3-4.
Function
Hall IC abnormality
System Functions and Operation Overview (2/2)
Overview
• Occurs if value from Hall IC is illegal.
• Motor control is stopped.
• “HALL” is displayed with LEDs.
• Functions other than RESET switch are disabled.
Caution The operation is not guaranteed if two or more switches are pressed at the
same time.
3.4
Peripheral I/Os
This system uses the following peripheral I/Os.
Function
Inverter timer
Real-time output
Wait processing
Reading specified
speed
Capture interrupt
Hardware overcurrent
interrupt
Fail safe
3.5
Table 3-5. Peripheral I/Os
Peripheral I/O Function Name (μPD78F0714)
• For PWM output
• 10-bit inverter control timer (TW0UDC, etc.)
• Carrier (modulation) frequency is 13 kHz (symmetrical
triangular wave).
• Carrier (modulation) synchronization interrupt is generated at
intervals of 76.9 μs (duty factor is calculated by main PID
control action and updated by carrier synchronization
interrupt).
• For changing excitation pattern
• Real-time output port (RTBH01, RTBL01, etc.)
• 16-bit timer capture/compare register 01 (CR01)
(Excitation pattern is changed by carrier synchronization
interrupt.)
• For timing adjustment
• 8-bit timer/event counter 50 (TM50, CR50)
• Converts voltage on variable resistor value into specified
speed.
• A/D converter (ANI4)
• For speed calculation
• Interrupt function (TI000/INTP5)
• Interrupt function (INTP0)
• Occurrence of overcurrent in motor driver module (low-active)
• Watchdog timer
Interrupt
Table 3-6 shows the interrupts used in this system.
Name
INTP0
INTP5
INTTW0UD
RESET
WDT
Remark
18
Table 3-6. Interrupts Used
Function
Detection of overcurrent occurrence
Detection of change in HALL1 (speed
calculation)
Occurrence of carrier synchronization
interrupt
Occurrence of reset
Occurrence of internal reset
Condition of Occurrence
External pin
External pin
Underflow of inverter
timer counter
RESET pin
Watchdog timer overflow
due to program runaway
INTTM50 and INTAD are processed with polling of the interrupt request flag.
Application Note U18774EJ1V0AN
CHAPTER 4
CONTROL PROGRAM
This system uses a basic control program to actually control the speed of a motor.
4.1
Excitation Patterns
The pin information, excitation patterns, and Hall IC values are described with reference to
the BLDCM specifications.
4.1.1 Low-voltage inverter set
Table 4-1.
Color
BROWN
RED
ORANGE
GREY
BLUE
WHITE
VIOLET
BLACK
Table 4-2.
Excitation
(Positive
Rotation)
SENSOR φ 1
SENSOR φ 2
SENSOR φ 3
Excitation
(reverse
rotation)
BLDCM Pin Specifications
Function
Remark
Phase U
MOTOR φ A
Phase V
MOTOR φ B
Phase W
MOTOR φ C
HALL1
SENSOR φ 1
HALL2
SENSOR φ 2
HALL3
SENSOR φ 3
SENSOR Vcc
SENSOR GND
Excitation Patterns and Hall IC Values
+A
−B
+A
−C
+B
−C
+B
−A
+C
−A
+C
−B
L
H
H
L
L
H
H
L
H
H
L
L
H
H
L
L
H
L
+B
−A
+C
−A
+C
−B
+A
−B
+A
−C
+B
−C
Application Note U18774EJ1V0AN
19
CHAPTER
4.2
4
CONTROL PROGRAM
Excitation Pattern Switching
The excitation pattern must be switched in synchronization with inverter timer output (PWM
waveform). This is therefore done by setting to the real-time port an excitation pattern that
corresponds to the Hall IC values, with the carrier synchronization interrupt (76.9 μs period) of
the inverter timer.
Figure 4-1.
Excitation Pattern Switching Timing
Inverter timer output
Real-time port
Output to inverter
U+
U−
V+
V−
W+
W−
U+
U−
V+
V−
W+
W−
• The actual signal has dead time.
• The PWM duty factor is changed with
the PID result synchronizing to the
carrier frequency.
4.3
Switching timing
Speed Detection
High-precision speed detection is performed with speed calculation that is not affected by
delay which occurs when count values are saved in normal processing, by using the timer
capture function to instantaneously save the timer count values at the point when the Hall IC
values change.
In the case of the BLDCM of this system (three phases, four poles), a Hall IC changes four
times with one rotation; however, only the change on the rising side is processed due to the
specifications of the timer function to be used, so the speed is calculated from the timer value
that has changed during half a rotation.
60
Ν= s×n×2 =
2343750
n
N: Number of revolutions per minute (rpm)
s: Resolution of timer (12.8 μs)
n: Value of timer
2: Number of poles facing each other
This system supports a revolution speed range of 200 to 3200 rpm.
20
Application Note U18774EJ1V0AN
CHAPTER
4.4
4
CONTROL PROGRAM
Voltage Control
The voltage to be applied to the motor is controlled with the average voltage, which has been
derived by performing a chopper operation (PWM) on the positive side of the inverter with a 13
kHz carrier frequency.
No complementary operation is performed on the negative side of PWM, so no braking
torque occurs during deceleration (natural deceleration).
4.5
Speed Control
The rotation speed of the motor is controlled by adjusting the PWM duty factor to the 150 ms
period.
The variable of the duty factor to be adjusted is derived by performing a PID control
operation on the difference between the specified speed and motor rotation speed.
4.5.1 PID control
The speed is adjusted by feeding back the difference between the rotation speed and the
specified speed and performing a PID control operation on the PWM duty factor (average
voltage). The duty factor manipulated variable of PWM uses the following speed type PID
algorithm suitable for the sampling method (discrete value).
MVn = MVn-1 + MVn
MVn = Kp(en – en-1) + Ki × en + Kd((en – en-1) – (en-1 – en-2))
MVn:
MVn-1:
MVn:
en :
en-1:
en-2:
Kp:
Ki:
Kd:
Current manipulated variable
Previous manipulated variable
Difference between current and previous manipulated variables
Current deviation (difference between specified speed and actual speed)
Previous deviation
Deviation before previous deviation
Feedback gain (proportional element)
Feedback gain (integral element)
Feedback gain (derivative element)
The optimum values of the feedback gains change depending on the motor characteristics
and on the presence or absence of a load.
The cycle of the PID control action is set to the speed updating interval (150 ms) at the
minimum revolution speed (200 rpm).
Application Note U18774EJ1V0AN
21
CHAPTER
4.6
4
CONTROL PROGRAM
Module Configuration
Figure 4-2 shows status transition of the system.
Figure 4-2.
Status Transition of System
Start/RESET
Operation
stops
(STOP)
Specified-speed
display
Initial setting
SELF display
MODE
START
Stopped
Specified-speed
display
STOP
SPEED
Specified-speed
change
SPEED
Switch
Interrupt
Overcurrent
Operating
Rotation speed
measurement
Rotation speed
display
PID control
Carrier
synchronization
interrupt
Hall IC
interrupt
Excitation mode
change
Duty factor change
FORWARD/
REVERSE
Rotation
direction
change
Timer value save
The carrier synchronization interrupt is generated every 76.9 μs.
22
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
4.7 Control Program Functions and Flowcharts
4.7.1 Function list
The control program consists of many functions. Table 4-5 lists these functions and their
features. For details of processing, see 4.7.2 Flowcharts.
Table 4-5.
Function Name
void main(void)
void int_carrier(void)
void int_faulta(void)
void system_init(void)
void system_start(void)
void system_restart(void)
void system_stop(void)
void INTP0_on(void)
void print_error(char)
void pwm_pid(int)
void get_speed(void)
unsigned char get_sw(void)
void init_PORT(void)
void init_OSC(void)
void init_TW0(void)
void set_TW0(int, int, int, int)
void start_TW0(void)
void stop_TW0(void)
Functions (1/2)
Features
• Main routine
• Starting, initialization, infinite loop waiting for switch input (start,
stop, PID control)
• Carrier synchronization interrupt
• Detects no rotation of motor (error processing).
• Acquires speed information.
• Changes duty factor of PWM.
• Changes excitation pattern.
• Overcurrent interrupt
• Stops control.
• Displays error.
• Initialization
• Initializes internal peripherals.
• Displays “SELF” with LEDs.
• Starts motor control.
• Sets value for starting as variable.
• Starts internal peripheral timer.
• Unmasks interrupt.
• Resumes motor control after stop of reverse rotation.
• Sets value for resumption as variable
• Stops motor control.
• Masks interrupt.
• Sets value for stopping as variable.
• Stops internal peripheral timer.
• Enables overcurrent interrupt.
• Unmasks interrupt.
• Error display
• Displays error on 7-segment LEDs.
• Stops program by STOP instruction.
• PID control of speed
• Calculates speed from number of carrier synchronization
interrupts and timer value.
• Performs PID control for each specified time, based on difference
from specified speed and calculates PWM duty factor.
• Reads specified speed.
• Calculates specified speed from A/D converted voltage of
variable resistor.
• Sets specified speed of 200 to 3200 rpm to variable.
• Reads switch.
• Returns status of control switch.
Initializes I/O port.
Changes CPU clock.
Initializes 10-bit inverter timer.
Changes PWM duty factor.
Starts PWM output.
Stops PWM output.
Application Note U18774EJ1V0AN
23
CHAPTER
4
CONTROL PROGRAM
Table 4-5.
Function Name
void init_TM00(void)
void start_TM00(void)
void stop_TM00(void)
void init_RTPM01(void)
void set_RTPM01(unsigned
char)
void start_RTPM01(void)
void stop_RTPM01(void)
void init_AD(void)
void start_AD(void)
unsigned char get_AD(char)
void init_WDTM(void)
void clear_WDTM(void)
void reset_WDTM(void)
void init_TM50(void)
void start_TM50(void)
void wait_TM50(void)
void speed_print(int)
void INTTW0UD_on(void)
void INTTW0UD_off(void)
void INTP5_on(void)
void INTP5_off(void)
void led_print(int, char)
void led_set(unsigned char,
unsigned char)
void wait(int)
char read_Hall_IC(void)
void int_speed(void)
24
Functions (2/2)
Features
Initializes 16-bit timer (TM00).
Starts 16-bit timer (TM00).
Stops 16-bit timer (TM00).
Initializes real-time port.
• Sets real-time port.
• Changes excitation pattern (port that outputs PWM).
Real-time port function operation
Stops real-time port function operation.
• Initializes A/D function.
• Sets A/D conversion of SPEED volume.
Starts A/D conversion.
Reads A/D conversion value.
Initializes watchdog timer.
Clears watchdog timer counter.
Generates internal reset signal from watchdog timer.
• Initializes 8-bit timer (TM50).
• Used as 1 ms timer.
Starts 8-bit timer (TM50).
Waits for 8-bit timer (TM50).
• Displays speed with LEDs.
• Specified speed is displayed when motor control is stopped.
• Rotation speed of motor is displayed when motor is controlled
and while MODE switch is depressed.
Enables carrier synchronization interrupt.
Disables carrier synchronization interrupt.
Enables INTP5 interrupt.
Disables INTP5 interrupt.
Displays value of 1 to 4 digits with LEDs.
Outputs value to specified LED.
Waits for lapse of specified time (ms).
Reads Hall IC value.
INTP5 interrupt
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
4.7.2 Flowcharts
Figures 4-3 through 4-46 show a flowchart of each function.
<Main routine: main()>
Figure 4-3.
Main Processing (1/2)
main( )
system_init( )
(Initialization)
L
clear_WDTM( )
(Clears counter of
watchdog timer.)
get_speed( )
Reads specified speed.
speed_print( )
Speed display
Motor control in progress?
No
A
Yes
Yes
Waiting for stop?
No
get_sw( )
(Reads switch.)
Value of switch
STOP
FORWARD
REVERSE
Switch is held down.
Rotation is CW.
Yes
No
No
Set flag to wait for motor to stop.
Yes
Change rotation direction flag to CCW.
Set flag to wait for stop of reverse
rotation.
Rotation is CCW.
default
No
Yes
Change rotation direction flag to CW.
Set flag to wait for stop of reverse
rotation.
pwm_pid( )
PID control of speed
B
Application Note U18774EJ1V0AN
25
CHAPTER
Figure 4-3.
B
4
CONTROL PROGRAM
Main Processing (2/2)
A
get_sw( )
Reads switch.
Value of switch
START
Switch is held
down.
No
system_start( )
Starts motor control
MODE
Yes
Switch is held
down.
No
Disabled
Status of speed
change function
Enabled
Set function as invalid.
Set function as valid.
default
Save history of switch.
L
26
Application Note U18774EJ1V0AN
Yes
CHAPTER
4
CONTROL PROGRAM
<Carrier synchronization interrupt>
Figure 4-4.
Carrier Synchronization Interrupt Processing (1/2)
int_carrier( )
Enable interrupt.
Increment number of interrupts.
Increment speed display time
management variable.
No Hall IC interrupt is
generated for given
time.
No
Yes
No
Waiting for system
to stop with STOP
Yes
system_stop( )
Stops motor control.
Waiting for stop of
reverse rotation
RETURN
Yes
system_restart( )
Starts motor control after
stop of reverse rotation.
RETURN
No
No
PWM duty is
exceeded.
Yes
system_stop( )
Stops motor control.
print_error( )
Error display
RETURN
Increment PWM duty factor.
Set number of interrupts to 0.
C
Application Note U18774EJ1V0AN
27
CHAPTER
Figure 4-4.
4
CONTROL PROGRAM
Carrier Synchronization Interrupt Processing (2/2)
C
Increment PID time management variable.
read_Hall_IC( )
Reads Hall IC.
Waiting for stop
Yes
No
Hall IC has
changed.
set_TW0( )
Stops excitation.
No
Hall IC has
changed.
Yes
set_RTPM01( )
Switches excitation pattern.
Yes
Save value of new Hall IC.
Save number of carrier synchronization
interrupts.
Set number of carrier synchronization
interrupts to 0.
Set flag to speed information update.
Duty factor has
changed.
Yes
Save value of new Hall IC.
Save number of carrier
synchronization interrupts.
Set number of carrier synchronization
interrupts to 0.
Set flag to speed information update.
No
Yes
set_TW0( )
Changes duty factor.
Has capturing
been completed?
No
Yes
Read capture value.
RETURN
28
Application Note U18774EJ1V0AN
No
CHAPTER
4
CONTROL PROGRAM
<Overcurrent interrupt>
Figure 4-5.
Overcurrent Interrupt Processing
int_fault( )
system_stop( )
Stops motor control.
print_error( )
Displays error with LEDs.
RETURN
<Initialization>
Figure 4-6.
Initialization Processing
system_init( )
init_AD( )
Initializes A/D.
init_PORT( )
Initializes I/O port.
init_TM50( )
Initializes 8-bit timer.
init_OSC( )
Switches CPU clock.
led_set( )
Displays “SELF” with LEDs.
init_WDTM( )
Initializes WDTM.
init_TW0( )
Initializes inverter timer.
init_TM00( )
Initializes 16-bit timer.
init_RTPM01( )
Initializes real-time port.
wait( )
Waits for one second.
start_AD( )
Starts A/D conversion
processing.
INTP0_on( )
Enables overcurrent
interrupt.
Enable interrupt.
RETURN
Application Note U18774EJ1V0AN
29
CHAPTER
4
CONTROL PROGRAM
<Motor control start>
Figure 4-7.
Motor Control Start Processing
system_start( )
Set 0 to speed variable.
Initialize PID control variable with 0.0.
Initialize number of carrier synchronization
interrupts with 0.
Initialize PID control time management variable
with 0.
Initialize speed display time management
variable with 0.
Set default value to speed calculation variable.
Set flag indicating system control in progress.
Set rotation start flag.
Clear stop wait flag.
Clear flag to wait for stop of reverse rotation.
Clear speed information update flag.
Clear speed variable update flag.
Set default value to PWM control variable.
Set 0 to Hall IC value history variable.
start_TM00( )
Starts 16-bit timer.
INTTW0UD_on( )
Enables carrier
synchronization interrupt.
INTP5_on( )
Enables INTP5 interrupt.
start_RTPM0( )
Enables real-time port
function operation.
start_TW0( )
Starts inverter timer operation.
Enable inverter operation.
RETURN
<Motor control resumption after stop of reverse rotation>
Figure 4-8.
Motor Control Resumption Processing After Stop of Reverse Rotation
system_restart( )
Set 0 to speed variable.
Initialize PID control variable with 0.0.
Initialize PID control time
management variable with 0.
Initialize speed display time
management variable with 0.
Set default value to speed calculation
variable.
Set rotation start flag.
Clear speed information update flag.
Clear speed variable update flag.
Set default value to PWM control
variable.
Set 0 to Hall IC value history variable.
Clear flag to wait for stop of reverse
rotation.
RETURN
30
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
<Motor control stop>
Figure 4-9.
Motor Control Stop Processing
system_stop( )
Stop inverter operation.
INTTW0UD_off( )
Disables carrier synchronization
interrupt.
INTP5_off( )
Disables INTP5 interrupt.
stop_RTPM01( )
Stops real-time port.
stop_TW0( )
Stops inverter timer.
stop_TM00( )
Stops 16-bit timer.
Clear flag indicating system control in
progress.
Clear flag to wait for stop of reverse
rotation.
Clear stop wait flag.
Set rotation direction to CW.
RETURN
Set 0 to speed variable.
RETURN
<Overcurrent interrupt enable>
Figure 4-10.
Overcurrent Interrupt Enable Processing
INTP0_on( )
Set interrupt priority.
Set trigger edge to falling.
Clear interrupt request flag.
Unmask interrupt.
RETURN
Application Note U18774EJ1V0AN
31
CHAPTER
4
CONTROL PROGRAM
<Error display>
Figure 4-11.
Error Display Processing
prent_error( )
Error type
Illegal HALL value
Hardware overcurrent
led_set( )
Displays HALL with LEDs.
led_set( )
Displays 0.C. with LEDs.
Motor failure
led_set( )
Displays FAIL with LEDs.
Other
read_Hall_IC( )
Reads Hall IC value.
No
Abnormal value
Yes
led_set( )
Displays FAIL with LEDs.
led_set( )
Displays HALL with LEDs.
Execute STOP instruction.
32
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
<PID control of speed>
Figure 4-12.
Speed PID Control Processing
pwm_pid( )
Speed information
update flag is set.
No
Yes
Clear speed information update flag.
Rotation start
flag is cleared.
No
Yes
Set flag to speed update.
Clear rotation start flag.
No
PID control
timing
Yes
Set 0 to PID control timing counter.
No
Speed update
Yes
Clear speed update flag.
Is timer calculation
possible?
No
Yes
Calculate rotation speed from
number of carrier synchronization
interrupt generations.
Calculate rotation speed from
timer value.
Yes
Waiting for stop
No
Change PWM duty factor with
PID control.
RETURN
Application Note U18774EJ1V0AN
33
CHAPTER
4
CONTROL PROGRAM
<Specified-speed read>
Figure 4-13.
Specified-Speed Read Processing
get_speed( )
Speed change
function is valid.
No
Yes
get_AD( )
Extracts A/D conversion result.
Convert to value from 200 to 3200.
Set to specified-speed variable.
RETURN
<Switch read>
Figure 4-14.
Switch Read Processing
get_sw( )
Check switch status.
Switch is pressed.
No
Yes
Check switch status.
No
Switch is in same
status for given
time.
Yes
Motor control is
in progress.
No
Yes
Accept STOP, FORWARD,
REVERSE, or MODE switch.
Accept START or MODE switch.
RETURN
34
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
<I/O port initialization>
Figure 4-15.
I/O Port Initialization Processing
init_PORT( )
Set to overcurrent interrupt.
Set switch port to input.
Transmit control signal to inverter board.
Set timer capture trigger to rising edge.
Set LED control port to output.
RETURN
<CPU clock switching>
Figure 4-16.
CPU Clock Switch Processing
init_OSC( )
Change memory size.
clear_WDTM( )
Clears watchdog timer.
Switch clock waiting for oscillation
stabilization.
RETURN
<10-bit inverter timer initialization>
Figure 4-17.
10-bit Inverter Timer Initialization Processing
init_TW0( )
Set count clock to 20 MHz.
Set carrier interrupt frequency to every time.
Set carrier interrupt interval to 76.9 μs.
RETURN
Application Note U18774EJ1V0AN
35
CHAPTER
4
CONTROL PROGRAM
<PWM duty factor change>
Figure 4-18.
PWM Duty Factor Change Processing
set_TW0( )
Set value to inverter register and change
PWM duty factor.
RETURN
<PWM output start>
Figure 4-19.
PWM Output Start Processing
start_TW0( )
Start inverter timer.
RETURN
<PWM output stop>
Figure 4-20.
PWM Output Stop Processing
stop_TW0( )
Stop inverter timer.
RETURN
<16-bit timer (TM00) initialization>
Figure 4-21.
16-bit Timer (TM00) Initialization Processing
init_TM00( )
Set to CR00 capture register operation.
Set to CR01 compare register operation.
Set clock to 78.125 kHz.
RETURN
36
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
<16-bit timer (TM00) start>
Figure 4-22.
16-bit Timer (TM00) Start Processing
start_TM00( )
Start in free-running mode.
RETURN
<16-bit timer (TM00) stop>
Figure 4-23.
16-bit Timer (TM00) Stop Processing
stop_TM00( )
Stop 16-bit timer.
RETURN
<Real-time port initialization>
Figure 4-24.
Real-Time Port Initialization Processing
init_RTPM01( )
Set real-time output mode.
Set with 6 bits and 1 channel.
Select port outputting PWM.
RETURN
Application Note U18774EJ1V0AN
37
CHAPTER
4
CONTROL PROGRAM
<Real-time port setting>
Figure 4-25.
Real-Time Port Setting Processing
set_RTPM01( )
Set excitation pattern.
Write to register with 16-bit timer
interrupt.
RETURN
<Real-time port start>
Figure 4-26.
Real-Time Port Function Operation Processing
start_RTPM01( )
Enable real-time port operation.
RETURN
<Real-time port stop>
Figure 4-27.
Real-Time Port Function Operation Stop Processing
stop_RTPM01( )
Disable real-time port function.
RETURN
38
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
<A/D function initialization>
Figure 4-28.
A/D Function Initialization Processing
init_AD( )
Set to channel 4.
Set conversion speed to 3.6 μs.
RETURN
<A/D conversion start>
Figure 4-29.
A/D Conversion Start Processing
start_AD( )
Start A/D conversion.
Wait until first conversion is completed.
RETURN
<A/D conversion value read>
Figure 4-30.
A/D Conversion Value Read Processing
get_AD( )
Shift A/D conversion value to
right by specified number of bits
and return resulting value.
RETURN
<Watchdog timer initialization>
Figure 4-31.
Watchdog Timer Initialization Processing
init_WDTM( )
clear_WDTM( )
Clears counter.
Set runaway detection time.
RETURN
Application Note U18774EJ1V0AN
39
CHAPTER
4
CONTROL PROGRAM
<Watchdog timer counter clear>
Figure 4-32.
Watchdog Timer Counter Clear Processing
clear_WDTM( )
Set value to clear counter of
watchdog timer.
RETURN
<Internal reset signal generation by watchdog timer>
Figure 4-33.
Internal Reset Signal Generation Processing by Watchdog Timer
reset_WDTM( )
Write illegal value to generate
internal reset.
RETURN
<8-bit timer (TM50) initialization>
Figure 4-34.
8-bit Timer (TM50) Initialization Processing
init_TM50( )
Set 8-bit timer (TM50) interval
time to 1 ms.
RETURN
<8-bit timer (TM50) start>
Figure 4-35.
8-bit Timer (TM50) Start Processing
start_TM50( )
Start 8-bit timer (TM50).
RETURN
40
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
<8-bit timer (TM50) wait>
Figure 4-36.
8-bit Timer (TM50) Wait Processing
wait_TM50( )
No
8-bit timer (TM50) interval is
generated.
Yes
RETURN
<Displaying speed with LEDs>
Figure 4-37.
Processing of Displaying Speed with LEDs
speed_print( )
MODE switch is ON or
motor control is
stopped.
No
Yes
led_print( )
Displays specified speed.
led_print( )
Displays rotation speed.
RETURN
<Carrier synchronization interrupt enable>
Figure 4-38.
Carrier Synchronization Interrupt Enable Processing
INTTW0UD_on( )
Clear interrupt request flag.
Clear interrupt mask flag.
RETURN
Application Note U18774EJ1V0AN
41
CHAPTER
4
CONTROL PROGRAM
<Carrier synchronization interrupt disable>
Figure 4-39.
Carrier Synchronization Interrupt Disable Processing
INTTW0UD_off ( )
Disable carrier synchronization
interrupt.
RETURN
<INTP5 interrupt enable>
Figure 4-40.
INTP5 Interrupt Enable Processing
INTP5_on( )
Clear interrupt request flag.
Clear interrupt mask flag.
RETURN
<INTP5 interrupt disable>
Figure 4-41.
INTP5 Interrupt Disable Processing
INTP5_off ( )
Disable interrupt.
RETURN
42
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
<Displaying 1- to 4-digit value with LEDs>
Figure 4-42.
Processing of Displaying 1- to 4-Digit Value with LEDs
led_print ( )
led_set( )
Displays digit of 1000s.
led_set( )
Displays digit of 100s.
led_set( )
Displays digit of 10s.
led_set( )
Displays digit of 1s.
Displays dot (“.”) when
speed specification change
function is disabled.
RETURN
<Outputting value to specified LED>
Figure 4-43.
Processing of Outputting Value to Specified LED
led_set ( )
Switch output destination.
Output data to LED.
RETURN
Application Note U18774EJ1V0AN
43
CHAPTER
4
CONTROL PROGRAM
<Waiting for lapse of specified time (ms)>
Figure 4-44.
Processing of Waiting for Lapse of Specified Time (ms)
wait( )
start_TM50( )
(Starts 1 ms timer.)
wait_TM50( )
(Waits for 1 ms.)
clear_WDTM( )
(Clears counter of watchdog
timer.)
No
Specified time has
elapsed.
Yes
RETURN
<Reading Hall IC value>
Figure 4-45.
Processing of Reading Hall IC Value
read_Hall_IC ( )
Read Hall IC value.
RETURN
<INTP5 interrupt>
Figure 4-46.
INTP5 Interrupt Processing
int_speed( )
Set capture flag.
RETURN
44
Application Note U18774EJ1V0AN
CHAPTER
4.8
4
CONTROL PROGRAM
Variables and Constants of Control Program
4.8.1 Values defined by #define of main.h
Name
Meaning
FLG_ON
FLG_OFF
CW
CCW
START_TR
STOP_TR
FORWARD_TR
Flag value
Flag value
Rotation direction
Rotation direction
For status setting
For status setting
For status setting
Set
Value
1
0
0
1
0x01
0x02
0x04
REVERSE_TR
For status setting
0x08
MODE_TR
For status setting
0x10
ERROR_HALL
ERROR_OC
ERROR_MOTOR
ERROR_S_OC
P_OFF
P_STOP
P_T1
P_T2
P_T3
P_T4
P_T5
P_T6
Error flag bit
Error flag bit
Error flag bit
Error flag bit
Excitation pattern
Excitation pattern
Excitation pattern
Excitation pattern
Excitation pattern
Excitation pattern
Excitation pattern
Excitation pattern
0x01
0x02
0x04
0x08
0x3f
0x15
0x36
0x1e
0x1b
0x39
0x2d
0x27
Application Note U18774EJ1V0AN
Remark
Indicates whether flag is valid.
Indicates rotation direction of
motor.
Starts control.
Stops control.
Changes rotation direction to
CW.
Changes rotation direction to
CCW.
Status in which MODE switch is
pressed
Hall IC error
Error due to overcurrent
Error of motor failure
Error due to overcurrent
Excitation of real-time port
45
CHAPTER
4
CONTROL PROGRAM
4.8.2 Values defined by #define of lib_eu.h
(1/2)
Name
KP_DEF
KI_DEF
KD_DEF
PWM_LIMIT_H
Meaning
Set
Value
0.020
0.001
0.003
49.0
Remark
Feedback gain
Used for PID control
Feedback gain
Feedback gain
Upper limit of
manipulated variable
of PWM
PWM_LIMIT_L
Lower limit of
−49.0
manipulated variable
of PWM
UNIT_RPM
Speed calculation
2343750 Used for speed calculation
constant
Used for PWM output
PWM_BASE_REF
PWM base
769
PWM_F_REF
PWM default value
0
PWM_F_START
PWM start value
60
PWM_F_MIN
Minimum PWM value
10
PWM_F_MAX
Maximum PWM
769
value
PWM_DTM_REF
Dead time
0
IN
For port setting
1
Used to specify port function
OUT
For port setting
0
Used to access bits of
CLEAR
For register bit
0
registers
setting
SET
For register bit
1
setting
INV_OFF
For inverter control
1/0
Low-voltage inverter
INV_ON
For inverter control
0/1
INVERTER_SW
Inverter control port
P54
Inverter control port
INVERTER_SW_MODE Inverter control port
PM54 Inverter control port
control register
INTP0
Port control register
PM00
Overcurrent detection port
SW2
Port control register
PM73
Port for START/STOP
SW3
Port control register
PM72
Port for FORWARD
SW4
Port control register
PM71
Port for REVERSE
SW5
Port control register
PM70
Port for MODE
LD_LED0
Port control register
PM64
Port for LED selection
LD_LED1
Port control register
PM65
LD_LED2
Port control register
PM66
LD_LED3
Port control register
PM67
LD_DATA
Port control register
PM4
Port that outputs data to LED
46
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
(2/2)
Name
LED_0
LED_1
LED_2
LED_3
LED_4
LED_5
LED_6
LED_7
LED_8
LED_9
LED_O
LED_I
LED_C
LED_H
LED_A
LED_L
LED_
LED_S
LED_E
LED_F
LED_P
LED_dot
IMS_DATA
WDTM_OFF
WDTM_SET
WDTE_CLR
WDTE_RESET
KEY_WAIT
SW
START_SW
STOP_SW
FORWARD_SW
REVERSE_SW
MODE_SW
Meaning
Set
Value
LED display data
0xc0
0xf9
0xa4
0xb0
0x99
0x92
0x82
0xf8
0x80
0x98
0xc0
0xcf
0xc6
0x89
0x88
0xc7
0xff
0x92
0x86
0x8e
0x8c
0x7f
Memory size
0xc8
Stop
0x77
Set
0x67
Clear
0xac
Reset
0x00
Switch observation time
10
Switch
(P7&0xf)
Start
0x7
Stop
0x7
CW
0xb
CCW
0xd
Mode
0xe
Application Note U18774EJ1V0AN
Remark
Displays “0”.
Displays “1”.
Displays “2”.
Displays “3”.
Displays “4”.
Displays “5”.
Displays “6”.
Displays “7”.
Displays “8”.
Displays “9”.
Displays “0” instead of “O”.
Displays “I”.
Displays “C”.
Displays “H”.
Displays “A”.
Displays “L”.
Displays “ ”.
Displays “S”.
Displays “E”.
Displays “F”.
Displays “P”.
Displays “ . ”.
Used to change memory size.
For controlling watchdog timer
Used to eliminate chattering
Switch connection port
Switch status
47
CHAPTER
4
CONTROL PROGRAM
4.8.3 Variables
Variable Name
Type
sys_flag
char
stop_wait
char
Meaning
Control flag
Stop command flag
cw_ccw_flag
cw_ccw_wait
char
char
Rotation direction command flag
Reverse stop command flag
old_hdata
speed_flag
char
char
Hall IC history
Speed information flag
ad_flag
char
Speed change flag
int_cnt
unsigned int Interrupt counter
pid_cnt
unsigned int PID control timing
print_cnt
unsigned int Speed display timing
cnt_data1
cnt_data2
cnt_data3
pwm_base
pwm_ff
old_ff
m_speed
led_data[ ]
unsigned int Data for speed calculation
up_flag
capture_flag
clk_flag
old_clk
new_clk
kp_ref
ki_ref
kd_ref
mvn
en
en_1
en_2
48
int
PWM base clock value
int
PWM command value
int
History of PWM command value
int
Actual speed
unsigned
LED output data
char
char
Actual speed calculation status flag
char
Timer value acquisition flag
char
Timer value calculation enable flag
unsigned int Previous clock value
unsigned int Newest clock value
float
P gain
float
I gain
float
D gain
float
Previous manipulated variable
float
Difference from current value
float
Difference from previous value
float
Difference from value before
previous
Application Note U18774EJ1V0AN
Remark
Control status
Indicates whether stop command is
issued.
Rotation direction status
Indicates whether command to stop
by reversing is issued.
Previous Hall IC value
Indicates whether data required for
calculating speed is present.
Limits specified-speed change
function.
Number of times of carrier
synchronization interrupt
Number of times of carrier
synchronization interrupt
Number of times of carrier
synchronization interrupt
Number of times of carrier
synchronization interrupt
Carrier width of PWM
Distribution rate of PWM
Value of previous pwm_ff
Revolution speed of motor (rpm)
Value displayed with LEDs
For rotation speed calculation
Speed control
CHAPTER
4.9
4
CONTROL PROGRAM
Control Program Source File
Memory size
ROM: 16F0h
RAM: 3B8h
/*
BLDCM 120-degree excitation method with position sensor (Hall IC)
target
: uPD78F0714 motor starter kit
date
: 2006/12/13
filename: main.h
NEC Micro Systems,Ltd
*/
#define FLG_ON
#define FLG_OFF
1
0
#define CW
#define CCW
#define START_TR
#define STOP_TR
#define FORWARD_TR
#define REVERSE_TR
#define MODE_TR
0
1
0x01
0x02
0x04
0x08
0x10
/* Clockwise
/* Counterclockwise
/* Identifies switch pressed
#define ERROR_HALL
#define ERROR_OC
#define ERROR_MOTOR
#define ERROR_S_OC
0x01
0x02
0x04
0x08
/* Hall IC failure
*/
/* Overcurrent
*/
/* Motor failure
*/
/* Overcurrent (software detection) */
#define P_OFF
#define P_STOP
#define P_T1
#define P_T2
#define P_T3
#define P_T4
#define P_T5
#define P_T6
0x3f
0x15
0x36
0x1e
0x1b
0x39
0x2d
0x27
/* Real-time output port OFF
*/
/* Excitation pattern 1 U → V
/* Excitation pattern 2 U → W
/* Excitation pattern 3 V → W
/* Excitation pattern 4 V → U
/* Excitation pattern 5 W → U
/* Excitation pattern 6 W → V
*/
*/
*/
*/
*/
*/
extern char
extern char
extern char
extern char
extern char
extern char
extern char
extern char
extern char
extern unsigned int
extern unsigned int
extern unsigned int
extern unsigned int
extern unsigned int
extern unsigned int
extern unsigned int
extern unsigned int
extern unsigned int
extern int
extern int
extern int
extern int
extern unsigned int
sys_flag;
stop_wait;
cw_ccw_flag;
cw_ccw_wait;
old_hdata;
speed_flag;
ad_flag;
clk_flag;
capture_flag;
new_clk;
old_clk;
int_cnt;
pid_cnt;
print_cnt;
cnt_data;
cnt_data1;
cnt_data2;
cnt_data3;
pwm_ff;
old_ff;
pwm_base;
m_speed;
ad_data;
extern void
extern void
extern void
extern unsigned char
extern void
extern void
extern void
extern void
extern char
extern void
extern void
extern void
extern void
extern void
system_init(void);
get_speed(void);
speed_print(int);
get_sw(void);
pwm_pid(int);
system_start(void);
system_stop(void);
system_restart(void);
read_Hall_IC(void);
set_RTPM01(unsigned char);
set_TW0(int, int, int, int);
print_error(char);
clear_WDTM(void);
reset_WDTM(void);
*/
*/
*/
/*
BLDCM 120-degree excitation method with position sensor (Hall IC)
target
: uPD78F0714 motor starter kit
date
: 2006/12/18
filename: main.c
NEC Micro Systems,Ltd
*/
Application Note U18774EJ1V0AN
49
CHAPTER
#include
4
CONTROL PROGRAM
"main.h"
/*
Main function
*/
void
main(void)
{
unsigned char
unsigned char
sw = 0;
tmp_sw = 0;
system_init();
while(1){
clear_WDTM();
get_speed();
speed_print(200);
if(sys_flag != FLG_OFF){
if((cw_ccw_wait == FLG_OFF) &&
(stop_wait == FLG_OFF)){
sw = get_sw();
switch(sw){
case STOP_TR:
if(sw != tmp_sw){
stop_wait = FLG_ON;
}
break;
case FORWARD_TR:
if(cw_ccw_flag == CCW){
cw_ccw_wait = FLG_ON;
cw_ccw_flag = CW;
}
break;
case REVERSE_TR:
if(cw_ccw_flag == CW){
cw_ccw_wait = FLG_ON;
cw_ccw_flag = CCW;
}
break;
/* Initialization
*/
/* Clears watchdog timer
/* Reads specified speed
/* Displays speed (ms interval)
/* Being started
/* Not waiting for motor to stop reverse rotation
/* Not waiting for stop
/* Checks switch
*/
*/
*/
*/
*/
*/
*/
/* Handles toggle
/* Sets wait for stop
*/
*/
/* Rotating CCW
/* Sets wait for motor to stop reverse rotation
/* Sets rotation direction
*/
*/
*/
/* Rotating CW
/* Sets wait for motor to stop reverse rotation
/* Sets rotation direction
*/
*/
*/
/* PID control: 150 ms period
/* Being stopped
/* Checks switch
*/
*/
*/
/* Starts
/* Handles toggle
/* Starts
*/
*/
*/
/* Speed change function
/* Handles toggle
/* Function being stopped
/* Resumes function
*/
*/
*/
*/
/* Stops function
*/
default:
;
}
}
pwm_pid(150);
}else{
sw = get_sw();
switch(sw){
case START_TR:
if(sw != tmp_sw){
system_start();
}
break;
case MODE_TR:
if(sw != tmp_sw){
if(ad_flag == FLG_ON){
ad_flag = FLG_OFF;
}else{
ad_flag = FLG_ON;
}
}
break;
default:
;
}
}
tmp_sw = sw;
}
}
/*
BLDCM 120-degree excitation method with position sensor (Hall IC)
target
: uPD78F0714 motor starter kit
date
: 2006/12/13
filename: motor.c
NEC Micro Systems,Ltd
*/
#pragma sfr
#pragma INTERRUPT INTP0
#pragma INTERRUPT INTTW0UD
int_faulta
int_carrier
rb1 /* Overcurrent occurrence
*/
rb2 /* Carrier synchronization interrupt: 75 us period */
/* ---------------------------------------------- */
#include "main.h"
/* ---------------------------------------------- */
#define INT_CNT_MAX
1333
/* Identification of non-rotation for 50 ms × 75 us */
#define START_PWM_F_MAX 250
/* Limit value during non-rotation
*/
#define ISHUNT_AD
5
#define ADDATA_MIN
0
50
/* Number of AD channel to which ISHUNT pin is connected */
Application Note U18774EJ1V0AN
CHAPTER
#define ADDATA_100
#define ADDATA_200
#define ADDATA_300
#define ADDATA_400
#define ADDATA_E_2
#define ADDATA_500
#define ADDATA_600
#define ADDATA_700
#define ADDATA_800
#define ADDATA_900
#define ADDATA_1000
#define ADDATA_MAX
#define CLEAR
#define SET
0x1900
0x3200
0x4b00
0x6400
0x7400
0x7d00
0x9600
0xaf00
0xc800
0xe100
0xfa00
0xfcc0
4
CONTROL PROGRAM
/* 464<<6 */
0
1
/* ---------------------------------------------- */
const unsigned char tr_sw[2][8] = {
{P_OFF, P_T4, P_T6, P_T5, P_T2, P_T3, P_T1, P_OFF},
{P_OFF, P_T1, P_T3, P_T2, P_T5, P_T6, P_T4, P_OFF}
};
/* Excitation pattern */
/* CW
*/
/* CCW
*/
/* ====================================================== */
/*
Hardware overcurrent detection
*/
__interrupt void
int_faulta(void)
{
system_stop();
/* Stops system
*/
print_error(ERROR_OC);
/* Processes error */
}
/*
Carrier synchronization interrupt (valley side)
*/
__interrupt void
int_carrier(void)
{
char h_data;
int tmp_pwm;
int_cnt++;
print_cnt++;
if(int_cnt > INT_CNT_MAX){
if(stop_wait != FLG_OFF){
system_stop();
return;
}else if(cw_ccw_wait != FLG_OFF){
system_restart();
return;
}else if(pwm_ff > START_PWM_F_MAX){
system_stop();
print_error(ERROR_MOTOR);
return;
}else{
pwm_ff+=5;
int_cnt = 0;
};
};
/* Number of interrupts
/* Speed display timing
/* No Hall IC change for 50 ms or more
/* Waits for stop with STOP
*/
*/
*/
*/
/* Waits for stop of reverse rotation
*/
/* Being started from START
*/
/* Increments PWM
*/
pid_cnt++;
/* PID control timing
*/
h_data = read_Hall_IC();
if((stop_wait == FLG_OFF) && (cw_ccw_wait == FLG_OFF)){ /* Not waiting for stop
*/
if(h_data != old_hdata){
/* Hall IC has changed
*/
set_RTPM01(tr_sw[cw_ccw_flag][h_data]);
/* Sets excitation pattern
*/
old_hdata = h_data;
/* Saves Hall IC value
*/
cnt_data3 = cnt_data2;
/* Saves interrupt count history
*/
cnt_data2 = cnt_data1;
/* Saves interrupt count history
*/
cnt_data1 = int_cnt;
/* Saves number of interrupts
*/
cnt_data
= cnt_data1 + cnt_data2 + cnt_data3;
int_cnt
= 0;
/* Clears number of interrupts
*/
speed_flag = FLG_ON;
/* Sets speed information as valid
*/
};
if(pwm_ff != old_ff){
/* Duty has changed
*/
tmp_pwm = pwm_base - pwm_ff;
set_TW0(tmp_pwm, tmp_pwm, tmp_pwm, pwm_base); /* Changes duty
*/
old_ff = pwm_ff;
};
}else{
/* Processing for continuing speed display even when waiting for stop */
set_RTPM01(P_OFF);
/* Stops excitation
*/
if(h_data != old_hdata){
/* Hall IC has changed
*/
old_hdata = h_data;
cnt_data3 = cnt_data2;
cnt_data2 = cnt_data1;
cnt_data1 = int_cnt;
cnt_data
= cnt_data1 + cnt_data2 + cnt_data3;
int_cnt
= 0;
speed_flag = FLG_ON;
};
};
if(capture_flag == FLG_ON){
if(CR00 != new_clk){
old_clk
= new_clk;
new_clk
= CR00;
clk_flag
= FLG_ON;
capture_flag = FLG_OFF;
};
};
return;
}
Application Note U18774EJ1V0AN
51
CHAPTER
4
CONTROL PROGRAM
/*
BLDCM 120-degree excitation method with position sensor (Hall IC)
target: uPD78F0714 motor starter kit
date
: 2006/12/13
filename: lib_eu.h
NEC Micro Systems,Ltd
*/
#define KP_DEF
#define KI_DEF
#define KD_DEF
#define PMW_LIMIT_H
#define PMW_LIMIT_L
0.020
0.001
0.003
49.0
-49.0
/* For LowVoltage */
/* Upper limit of PWM manipulated variable
/* Lower limit of PWM manipulated variable
*/
*/
/*
m_speed constant: 400 [rpm] * (0.15 [s] / 12.8 [us]) / 2
--------↑
↑
↑ Number of pole pairs
↑
↑ TM00 count clock: 78.125 [kHz] (1 / 78.125 [kHz])
↑ Period when 400 [rpm] (1 / (400 / 60))
*/
#define UNIT_RPM
2343750
#define MIN_SPEED
#define MAX_SPEED
200
3200
#define PWM_BASE_REF
#define PWM_F_REF
#define PWM_F_START
#define PWM_F_MIN
#define PWM_F_MAX
#define PWM_DTM_REF
769
0
60
10
769
0
/* Half of PWM period
/* Default value of PWM waveform
/* Default value at start
/* Minimum value
/* Maximum value
/* Dead time
#define IN
#define OUT
1
0
/* Input */
/* Output */
#define CLEAR
#define SET
0
1
#define INV_OFF
#define INV_ON
1
0
/* For LowVoltage */
#define INVERTER_SW
#define INVERTER_SW_MODE
#define INTP0
#define SW2
#define SW3
#define SW4
#define SW5
P54
PM54
PM00
PM73
PM72
PM71
PM70
/* Inverter operation control port */
/* Inverter operation control port */
/* Overcurrent detection port
#define LD_LED0
#define LD_LED1
#define LD_LED2
#define LD_LED3
PM64
PM65
PM66
PM67
#define LD_DATA
PM4
#define LED_0
#define LED_1
#define LED_2
#define LED_3
#define LED_4
#define LED_5
#define LED_6
#define LED_7
#define LED_8
#define LED_9
#define LED_O
#define LED_I
#define LED_C
#define LED_H
#define LED_A
#define LED_L
#define LED_
#define LED_S
#define LED_E
#define LED_F
#define LED_P
#define LED_dot
0xc0
0xf9
0xa4
0xb0
0x99
0x92
0x82
0xf8
0x80
0x98
0xc0
0xcf
0xc6
0x89
0x88
0xc7
0xff
0x92
0x86
0x8e
0x8c
0x7f
#define IMS_DATA
0xc8
#define WDTM_OFF
#define WDTM_SET
#define WDTE_CLR
#define WDTE_RESET
/* 2count */
/* LED display data
*/
*/
*/
*/
*/
*/
*/
*/
/* O-C
*/
/* HALL
*/
/* SELF
*/
/* PC
*/
0x77
0x67
0xac
0x00
/* For controlling watchdog timer
*/
#define KEY_WAIT
#define SW
10
(P7&0xf)
/* Chattering elimination ms
/* Switch port
*/
*/
#define START_SW
#define STOP_SW
#define FORWARD_SW
#define REVERSE_SW
#define MODE_SW
0x7
0x7
0xb
0xd
0xe
/* Identifies switch pressed
*/
static void
static void
static void
52
INTP0_on(void);
led_set(unsigned char, unsigned char);
led_print(int, char);
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
static void
wait(int);
static void
INTTW0UD_on(void);
static void
INTTW0UD_off(void);
static void
INTTW0CM3_on(void);
static void
INTTW0CM3_off(void);
static void
init_PORT(void);
static void
init_OSC(void);
static void
init_TW0(void);
static void
start_TW0(void);
static void
stop_TW0(void);
static void
init_TM00(void);
static void
start_TM00(void);
static void
stop_TM00(void);
static void
init_RTPM01(void);
static void
start_RTPM01(void);
static void
stop_RTPM01(void);
static void
init_AD(void);
static void
start_AD(void);
static unsigned int get_AD(char);
static void
init_WDTM(void);
static void
init_TM51(void);
static void
start_TM51(void);
static void
stop_TM51(void);
static void
init_TM50(void);
static void
start_TM50(void);
static void
wait_TM50(void);
static void
INTSR00_on(void);
static void
INTSR00_off(void);
static void
INTP5_on(void);
static void
INTP5_off(void);
/*
BLDCM 120-degree excitation method with position sensor (Hall IC)
target
: uPD78F0714 motor starter kit
date
: 2006/12/13
filename: lib_eu.c
NEC Micro Systems,Ltd
*/
#pragma sfr
#pragma stop
#pragma ei
#pragma di
#pragma INTERRUPT INTP5
int_speed
rb3
#include "main.h"
#include "lib_eu.h"
int
int
char
int
char
int
int
unsigned int
unsigned int
unsigned int
unsigned int
unsigned int
char
char
char
char
char
char
pwm_ff;
old_ff;
old_hdata;
pwm_base = PWM_BASE_REF;
start_flag;
speed_ref = 200;
m_speed;
int_cnt;
pid_cnt;
print_cnt;
cnt_data;
cnt_data1, cnt_data2, cnt_data3;
cw_ccw_flag = CW;
speed_flag;
cw_ccw_wait;
sys_flag;
stop_wait;
ad_flag
= FLG_OFF;
/* Active width: 0 to 750
/* PWM history
/* Hall IC data history
/* Half of PWM period: Fixed to 750
/* Identification immediately after rotation start
/* Specified speed
/* Motor rotation speed
/* Number of times of carrier synchronization interrupt
/* For checking PID control period
/* For checking speed display interval
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
/* History of number of times of carrier synchronization interrupt
/* Rotation direction
/* Presence/absence of speed information
/* Wait for stop of reverse rotation
/* System operation status
/* Indicates whether system is waiting for motor to stop
/* Limitation of specified-speed change function
*/
*/
*/
*/
*/
*/
*/
char
char
unsigned int
clk_flag;
capture_flag;
new_clk, old_clk;
/* Timer value for speed measurement
*/
/*
Static variable
*/
static const unsigned char led_data[10] = {
LED_0, LED_1, LED_2, LED_3, LED_4,
LED_5, LED_6, LED_7, LED_8, LED_9
};
static char
up_flag = FLG_OFF;
static float
kp_ref = KP_DEF;
static float
ki_ref = KI_DEF;
static float
kd_ref = KD_DEF;
static float
mvn;
static float
en, en_1, en_2;
/* For number display */
/* Speed update check flag
/* Kp default value
/* Ki default value
/* Kd default value
/* Manipulated variable
/* Deviation
*/
*/
*/
*/
*/
*/
/*
Initialization
*/
void
system_init(void)
{
init_PORT();
init_OSC();
init_WDTM();
init_TW0();
init_TM00();
init_RTPM01();
init_AD();
init_TM50();
led_set(0, LED_S);
Application Note U18774EJ1V0AN
53
CHAPTER
led_set(1, LED_E);
led_set(2, LED_L);
led_set(3, LED_F);
wait(1000);
start_AD();
INTP0_on();
EI();
4
CONTROL PROGRAM
/* Starts AD conversion
*/
/* Unmasks overcurrent interrupt */
/* Enables interrupt
*/
}
/*
System start
*/
void
system_start(void)
{
m_speed
= 0;
en_1
=
en
=
mvn
= 0.0;
int_cnt
=
pid_cnt
=
print_cnt
= 0;
cnt_data1
=
cnt_data2
=
cnt_data3
= 500;
cnt_data
= 1500;
sys_flag
= FLG_ON;
start_flag
= FLG_ON;
stop_wait
= FLG_OFF;
cw_ccw_wait = FLG_OFF;
speed_flag = FLG_OFF;
up_flag
= FLG_OFF;
clk_flag
= FLG_OFF;
capture_flag = FLG_OFF;
pwm_ff
= PWM_F_START;
old_ff
= 0;
old_hdata
= 0;
start_TM00();
INTTW0UD_on();
/* Unmasks carrier synchronization interrupt */
INTP5_on();
start_RTPM01();
start_TW0();
INVERTER_SW = INV_ON;
/* INVERTER enable
*/
}
/*
Restart from stop of reverse rotation
*/
void
system_restart(void)
{
m_speed
= 0;
mvn
=
en_1
=
en
= 0.0;
pid_cnt
=
print_cnt
= 0;
cnt_data1
=
cnt_data2
=
cnt_data3
= 500;
cnt_data
= 1500;
start_flag
= FLG_ON;
speed_flag = FLG_OFF;
up_flag
= FLG_OFF;
clk_flag
= FLG_OFF;
capture_flag = FLG_OFF;
pwm_ff
= PWM_F_START;
old_ff
= 0;
old_hdata
= 0;
cw_ccw_wait = FLG_OFF;
}
/*
System stop
*/
void
system_stop(void)
{
INVERTER_SW= INV_OFF;
INTTW0UD_off();
INTP5_off();
stop_RTPM01();
stop_TW0();
stop_TM00();
sys_flag
= FLG_OFF;
stop_wait
= FLG_OFF;
cw_ccw_flag = CW;
m_speed
= 0;
}
/* INVERTER disable
/* Speed 0
*/
*/
/*
Overcurrent interrupt enable
*/
static void
INTP0_on(void)
{
PR0L.1 = 0;
EGN.0 = 1;
IF0L.1 = CLEAR;
MK0L.1 = CLEAR;
}
/* Priority
/* Falling edge
/* Clears flag
/* Unmasks
*/
*/
*/
*/
/*
Error display
54
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
*/
void
print_error(char eno)
{
char data;
switch(eno){
case ERROR_HALL:
led_set(0, LED_H);
led_set(1, LED_A);
led_set(2, LED_L);
led_set(3, LED_L);
break;
case ERROR_OC:
led_set(0, LED_);
led_set(1, LED_O & LED_dot);
led_set(2, LED_C & LED_dot);
led_set(3, LED_);
break;
case ERROR_MOTOR:
led_set(0, LED_F);
led_set(1, LED_A);
led_set(2, LED_I);
led_set(3, LED_L);
break;
case ERROR_S_OC:
led_set(0, LED_S & LED_dot);
led_set(1, LED_);
led_set(2, LED_O & LED_dot);
led_set(3, LED_C & LED_dot);
break;
default:
data = read_Hall_IC();
if((0 == data) || (7 == data)){
led_set(0, LED_H);
led_set(1, LED_A);
led_set(2, LED_L);
led_set(3, LED_L);
}else{
led_set(0, LED_F);
led_set(1, LED_A);
led_set(2, LED_I);
led_set(3, LED_L);
}
}
STOP();
}
/*
PID control
*/
void
pwm_pid(int cy_time)
{
unsigned long tmp;
unsigned int new_tmp, old_tmp;
int
pwm_tmp;
if(speed_flag == FLG_ON){
speed_flag = FLG_OFF;
if(start_flag == FLG_OFF){
up_flag = FLG_ON;
}else{
start_flag = FLG_OFF;
}
}
/* Speed is already measured */
/* Speed is already updated */
/* First speed information is not used */
if(pid_cnt >= (unsigned int)(cy_time*13)){ /* cy_time * 1 ms has elapsed */
pid_cnt = 0;
if(up_flag == FLG_ON){
/* Speed is updated */
up_flag = FLG_OFF;
if(clk_flag == FLG_ON){
DI();
clk_flag = FLG_OFF;
new_tmp = new_clk;
old_tmp = old_clk;
EI();
if(old_tmp > new_tmp){
tmp = (unsigned long)((65536 - (long)old_tmp) + (long)new_tmp);
}else{
tmp = new_tmp - old_tmp;
}
m_speed = (int)(UNIT_RPM/tmp);
}else{
m_speed = (int)(200000/(unsigned long)cnt_data);
}
if((sys_flag != FLG_OFF) &&
/* Being started
(stop_wait != FLG_ON) && /* Not waiting for stop
(cw_ccw_wait != FLG_ON)){ /* Not waiting for stop of reverse rotation
up_flag = FLG_OFF;
en_2 = en_1;
en_1 = en;
en
= (float)(speed_ref - m_speed);
mvn = mvn +
kp_ref*(en - en_1) +
ki_ref*en +
kd_ref*((en - en_1) - (en_1 - en_2));
if(mvn > PMW_LIMIT_H){
mvn = (float)PMW_LIMIT_H;
}else if(mvn < PMW_LIMIT_L){
mvn = (float)PMW_LIMIT_L;
*/
*/
*/
Application Note U18774EJ1V0AN
55
CHAPTER
}
pwm_tmp = pwm_ff + (int)mvn;
if(pwm_tmp > PWM_F_MAX){ /* Limiter
pwm_ff = PWM_F_MAX;
}else if(pwm_tmp < PWM_F_MIN){
pwm_ff = PWM_F_MIN;
}else{
pwm_ff = pwm_tmp;
}
4
CONTROL PROGRAM
*/
}
}
}
}
/*
Reading voltage of speed specification variable resistor
Converts into specified speed
*/
void
get_speed(void)
{
unsigned int data;
unsigned long tmp;
if(ad_flag == FLG_OFF){
data = get_AD(2);
data = ((data - 3) * 12) + MIN_SPEED;
if(data > MAX_SPEED){
data = MAX_SPEED;
}else if(data < MIN_SPEED) {
data = MIN_SPEED;
};
tmp = (UNIT_RPM / (unsigned long)data);
speed_ref = (int)(UNIT_RPM / tmp);
}
}
/*
Reading value from switch (P7) and UART
Reads switch value
Returns stable value after checking for given time
*/
unsigned char
get_sw(void)
{
unsigned char data;
int i;
unsigned char tmp;
static unsigned char start_stop_flag = FLG_OFF;
data = SW;
if(data != 0xf){
for(i=0;i<KEY_WAIT;){
tmp = SW;
if(data == tmp){
i++;
}else{
i = 0;
data = tmp;
}
wait(2);
}
if(sys_flag != FLG_OFF){
switch(data){
case STOP_SW:
if(start_stop_flag == FLG_OFF){
data = STOP_TR;
start_stop_flag = FLG_ON;
}
break;
/* Reads switch
/* Eliminates chattering.
/* Re-reads switch
*/
Must be adjusted. */
*/
case FORWARD_SW: data = FORWARD_TR; break;
case REVERSE_SW: data = REVERSE_TR; break;
case MODE_SW:
data = MODE_TR;
break;
default:
;
}
}else{
if((data == START_SW) && (start_stop_flag == FLG_OFF)){
data = START_TR;
start_stop_flag = FLG_ON;
}else if(data == MODE_SW){
data = MODE_TR;
}
}
}
if(data == 0xf){
/* No switch is pressed */
start_stop_flag = FLG_OFF; /* Prevents toggling between START and STOP */
}
return(data);
}
/*
Port settings
*/
static void
init_PORT(void)
{
INTP0 = IN;
SW2 = IN;
SW3 = IN;
56
/* Interrupt of overcurrent notification */
/* START/STOP
*/
/* FORWARD
*/
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
SW4 = IN;
SW5 = IN;
/* REVERSE
/* MODE
INVERTER_SW_MODE = OUT;
INVERTER_SW
= INV_OFF;
/* INVERTER enable/disable */
/* INVERTER disable
*/
EGP5 = SET;
EGN5 = CLEAR;
/* Rising edge is valid */
LD_LED0 = OUT;
LD_LED1 = OUT;
LD_LED2 = OUT;
LD_LED3 = OUT;
/* LED selection: output */
LD_DATA = OUT;
/* LED display data: output */
*/
*/
}
/*
Clock switching
*/
static void
init_OSC(void)
{
IMS = IMS_DATA;
clear_WDTM();
while(OSTC != 0x1f);
PCC
= 0x00;
MCM.0 = 1;
VSWC.1 = 1;
}
/* Switches memory size */
/* Waits for oscillation to stabilize */
/* Sets division ratio */
/* X1 input clock */
/*
Inverter timer
*/
static void
init_TW0(void)
{
TCL02
= 0;
/* Count clock: 20 MHz
*/
TCL01
= 0;
TCL00
= 0;
IDEV02
= 0;
/* Generates interrupt every time */
IDEV01
= 0;
IDEV00
= 0;
TW0M
= 0;
TW0TRGS = 0;
TW0OC
= 0;
TW0CM3
= PWM_BASE_REF;
TW0CM0
= PWM_BASE_REF - PWM_F_REF;
TW0CM1
= PWM_BASE_REF - PWM_F_REF;
TW0CM2
= PWM_BASE_REF - PWM_F_REF;
TW0DTIME = PWM_DTM_REF;
/* Dead time
TW0BFCM3 = PWM_BASE_REF;
TW0BFCM0 = PWM_BASE_REF - PWM_F_REF;
TW0BFCM1 = PWM_BASE_REF - PWM_F_REF;
TW0BFCM2 = PWM_BASE_REF - PWM_F_REF;
TW0TRGS = 1;
}
*/
void
set_TW0(int a, int b, int c, int base)
{
TW0BFCM3 = (unsigned int)base;
TW0BFCM0 = (unsigned int)a;
TW0BFCM1 = (unsigned int)b;
TW0BFCM2 = (unsigned int)c;
}
static void
start_TW0(void)
{
TW0C.7 = SET;
}
static void
stop_TW0(void)
{
TW0C.7 = CLEAR;
}
/* Starts timer */
/* Stops timer */
/*
16-bit timer
CR01
Real-time output port trigger
*/
static void
init_TM00(void)
{
ES000 = 0;
ES001 = 0;
CRC000 = 1;
CRC001 = 1;
CRC002 = 0;
PRM001 = SET;
PRM000 = CLEAR;
}
static void
start_TM00(void)
{
TMIF00 = CLEAR;
TMC00 = 0x04;
}
/* Valid edge of TI000 pin: falling edge
*/
/* CR00 capture register
*/
/* Capture with reverse phase of valid edge of TI000 pin */
/* CR01 compare register
*/
/* Count clock: 78.125 kHz
*/
/* Free-running mode */
static void
Application Note U18774EJ1V0AN
57
CHAPTER
stop_TM00(void)
{
TMC00 = CLEAR;
}
4
CONTROL PROGRAM
/* Stops timer
*/
/* Real-time output mode
/* 6 bits × 1 channel
/* PWM modulation output
/* Output buffer
*/
*/
*/
*/
/* Sets excitation pattern
*/
/*
Real-time port
*/
static void
init_RTPM01(void)
{
RTPM01 = 0x3f;
RTPC01 = 0x20;
DCCTL01 = 0xc0;
RTBL01 = 0x3f;
}
void
set_RTPM01(unsigned char data)
{
RTBL01 = data;
CR01
= TM00 + 1;
}
static void
start_RTPM01(void)
{
RTPC01.7 = SET;
}
static void
stop_RTPM01(void)
{
RTPC01.7 = CLEAR;
}
/* Enables operation */
/* Disables operation */
/*
AD
*/
static void
init_AD(void)
{
ADS
= 4;
ADM
= 0x04;
}
static void
start_AD(void)
{
ADIF = CLEAR;
ADCS = SET;
while(ADIF != SET);
}
/* SPEED */
/* 3.6 us */
/* Clears interrupt notification flag */
/* Enables conversion operation */
/* Waits for interrupt notification */
static unsigned int
get_AD(char s_bit)
{
return((((unsigned int)ADCR)>>(s_bit+6))&0x3ff);
}
/*
Watchdog timer
*/
static void
init_WDTM(void)
{
clear_WDTM();
WDTM = WDTM_SET;
}
void
clear_WDTM(void)
{
WDTE = WDTE_CLR;
}
void
reset_WDTM(void)
{
WDTE = WDTE_RESET;
}
/*
8-bit timer 50
*/
static void
init_TM50(void)
{
TCL50 = 0x06;
CR50 = 78;
}
static void
start_TM50(void)
{
TMIF50 = CLEAR;
TCE50 = SET;
}
static void
wait_TM50(void)
{
while(TMIF50 != SET);
TCE50 = CLEAR;
}
58
/* 1 ms */
/* Clears interrupt notification flag */
/* Starts timer
*/
/* Waits for interrupt notification
/* Stops timer
*/
*/
Application Note U18774EJ1V0AN
CHAPTER
4
CONTROL PROGRAM
/*
Speed display
*/
void
speed_print(int ms)
{
if((MODE_SW == SW) || (sys_flag == FLG_OFF)){
led_print(speed_ref, ad_flag);
}else{
if(print_cnt > (unsigned int)(ms*13)){
led_print(m_speed, FLG_OFF);
print_cnt = 0;
}
}
}
/*
Carrier synchronization interrupt enable
*/
static void
INTTW0UD_on(void)
{
UDIFW0 = CLEAR;
UDMKW0 = CLEAR;
}
/* Clears request flag */
/* Enables interrupt */
/*
Carrier synchronization interrupt disable
*/
static void
INTTW0UD_off(void)
{
UDMKW0 = SET;
UDIFW0 = CLEAR;
}
/* Disables interrupt */
/*
INTP5 interrupt enable
*/
static void
INTP5_on(void)
{
PIF5 = CLEAR;
PMK5 = CLEAR;
}
/*
INTP5 interrupt disable
*/
static void
INTP5_off(void)
{
PMK5 = SET;
}
/*
Displaying value on 7-segment LED
*/
static void
led_print(int data, char flag)
{
unsigned int tmp;
int flg = FLG_OFF;
tmp = (unsigned int)(data / 1000);
if(tmp != 0){
/* Most significant digit is not 0 */
flg = FLG_ON;
/* Numerical value is displayed on most significant digit */
led_set(0, led_data[tmp]);
}else{
led_set(0, LED_);
}
data %= 1000;
tmp = (unsigned int)(data / 100);
if((tmp != 0) || (flg == FLG_ON)){
/* Value is not 0 or number has already been displayed */
flg = FLG_ON;
led_set(1, led_data[tmp]);
}else{
led_set(1, LED_);
}
data %= 100;
tmp = (unsigned int)(data / 10);
if((tmp != 0) || (flg == FLG_ON)){
/* Value is not 0 or number has already been displayed */
led_set(2, led_data[tmp]);
}else{
led_set(2, LED_);
}
data %= 10;
if(flag == FLG_OFF){
led_set(3, led_data[data]);
}else{
led_set(3, (unsigned char)(led_data[data])&LED_dot);
}
}
/*
Displaying data on 7-segment LED
no: Location of LED to be displayed
data: Data to be output
*/
static void
led_set(unsigned char no, unsigned char data)
{
unsigned char p;
Application Note U18774EJ1V0AN
59
CHAPTER
4
CONTROL PROGRAM
P6 = 0x00;
P4 = data;
switch(no){
case 0: p = 0x80; break;
case 1: p = 0x40; break;
case 2: p = 0x20; break;
default: p = 0x10; break;
}
P6 = p;
/* Location to be displayed */
/* Left edge
*/
/* Right edge
*/
}
/*
Time adjustment
*/
static void
wait(int cnt)
{
int i;
ms
for(i=0;i<cnt;i++){
start_TM50();
wait_TM50();
clear_WDTM();
}
return;
/* Starts timer
*/
/* Waits for generation of interrupt request */
/* Clears watchdog timer
*/
}
/*
Reading Hall IC
*/
char
read_Hall_IC(void)
{
return((char)((P0>>1)&0x7));
}
/*
INTP5
*/
__interrupt void
int_speed(void)
{
capture_flag = FLG_ON;
}
60
Application Note U18774EJ1V0AN
APPENDIX
A program example in which this system is controlled by connecting the separately provided motor
operation panel (GUI program) and the RS-232C pin located on the motor control I/O board is
attached.
/*
BLDCM 120-degree excitation method with position sensor (Hall IC)
target: uPD78F0714 motor starter kit
Compiler option GUI: GUI is used
date
: 2006/12/13
filename: lib_eu.h
NEC Micro Systems,Ltd
*/
#define KP_DEF
#define KI_DEF
#define KD_DEF
#define PMW_LIMIT_H
#define PMW_LIMIT_L
0.020
0.001
0.003
49.0
-49.0
/* For LowVoltage */
/* Upper limit of PWM manipulated variable
/* Lower limit of PWM manipulated variable
*/
*/
/*
m_speed constant: 400 [rpm] * (0.15 [s] / 12.8 [us]) / 2
--------↑
↑
↑ Number of pole pairs
↑
↑ TM00 count clock: 78.125 [kHz] (1 / 78.125 [kHz])
↑ Period when 400 [rpm] (1 / (400 / 60))
*/
#define UNIT_RPM
2343750
#define MIN_SPEED
#define MAX_SPEED
200
3200
#define PWM_BASE_REF
#define PWM_F_REF
#define PWM_F_START
#define PWM_F_MIN
#define PWM_F_MAX
#define PWM_DTM_REF
769
0
60
10
769
0
/* Half of PWM period
/* Default value of PWM waveform
/* Default value at start
/* Minimum value
/* Maximum value
/* Dead time
#define IN
#define OUT
1
0
/* Input */
/* Output */
#define CLEAR
#define SET
0
1
#define INV_OFF
#define INV_ON
1
0
/* For LowVoltage */
#define INVERTER_SW
#define INVERTER_SW_MODE
#define INTP0
#define SW2
#define SW3
#define SW4
#define SW5
P54
PM54
PM00
PM73
PM72
PM71
PM70
/* Inverter operation control port */
/* Inverter operation control port */
/* Overcurrent detection port
*/
#define LD_LED0
#define LD_LED1
#define LD_LED2
#define LD_LED3
PM64
PM65
PM66
PM67
#define LD_DATA
PM4
#define LED_0
#define LED_1
#define LED_2
#define LED_3
#define LED_4
#define LED_5
#define LED_6
#define LED_7
#define LED_8
#define LED_9
#define LED_O
#define LED_I
#define LED_C
#define LED_H
#define LED_A
#define LED_L
#define LED_
#define LED_S
#define LED_E
#define LED_F
#define LED_P
#define LED_dot
0xc0
0xf9
0xa4
0xb0
0x99
0x92
0x82
0xf8
0x80
0x98
0xc0
0xcf
0xc6
0x89
0x88
0xc7
0xff
0x92
0x86
0x8e
0x8c
0x7f
/* LED display data
*/
/* O-C
*/
/* HALL
*/
/* SELF
*/
/* PC
*/
#define IMS_DATA
0xc8
#define WDTM_OFF
#define WDTM_SET
#define WDTE_CLR
#define WDTE_RESET
0x77
0x67
0xac
0x00
/* For controlling watchdog timer
*/
#define KEY_WAIT
10
/* Chattering elimination ms
*/
/* 2count */
Application Note U18774EJ1V0AN
*/
*/
*/
*/
*/
*/
61
APPENDIX
#define SW
(P7&0xf)
/* Switch port
*/
#define START_SW
#define STOP_SW
#define FORWARD_SW
#define REVERSE_SW
#define MODE_SW
0x7
0x7
0xb
0xd
0xe
/* Identifies switch pressed
*/
#ifdef GUI
#define MD_ERROR_HALL
#define MD_ERROR_OC
#define MD_ERROR_MOTOR
#define RTS
#define CTS
#define RX_BUFF_SIZE
#define MD_CMD_START
#define MD_CMD_STOP
#define MD_CMD_RESET
#define MD_CMD_GETID
#define MD_CMD_GETVER
#define MD_CMD_SETSSPEED
#define MD_CMD_GETSSPEED
#define MD_CMD_SETPIDPARAM
#define MD_CMD_GETPIDPARAM
#define MD_CMD_GETSPEED
#define MY_ID
#define VER_MAJOR
#define VER_MINOR
#define VER_SEQ
#endif
0xF0
0xF1
0xF2
P11
P10
6
0x20
0x21
0x2f
0x10
0x11
0x30
0x31
0x40
0x41
0x50
0x21
0x01
0x00
0x01
/* Hall IC failure
/* Overcurrent
/* Motor failure
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static unsigned int
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
*/
*/
*/
/* Receive buffer size of UART00 */
/* START
*/
/* STOP
*/
/* RESET
*/
/* ID request
*/
/* Version request
*/
/* Specified-speed change */
/* Specified-speed read
*/
/* PID change
*/
/* PID read
*/
/* Actual-speed read
*/
/* Firm identification ID
*/
/* Version information
*/
INTP0_on(void);
led_set(unsigned char, unsigned char);
led_print(int, char);
wait(int);
INTTW0UD_on(void);
INTTW0UD_off(void);
INTTW0CM3_on(void);
INTTW0CM3_off(void);
init_PORT(void);
init_OSC(void);
init_TW0(void);
start_TW0(void);
stop_TW0(void);
init_TM00(void);
start_TM00(void);
stop_TM00(void);
init_RTPM01(void);
start_RTPM01(void);
stop_RTPM01(void);
init_AD(void);
start_AD(void);
get_AD(char);
init_WDTM(void);
init_TM51(void);
start_TM51(void);
stop_TM51(void);
init_TM50(void);
start_TM50(void);
wait_TM50(void);
INTSR00_on(void);
INTSR00_off(void);
INTP5_on(void);
INTP5_off(void);
#ifdef GUI
static void
init_UART00(void);
static unsigned char get_UART00(void);
static unsigned char wait_UART00(void);
static void
set_UART00(unsigned char);
#endif
/*
BLDCM 120-degree excitation method with position sensor (Hall IC)
target: uPD78F0714 motor starter kit
Compiler option GUI: GUI is used
date
: 2006/12/13
filename: lib_eu.c
NEC Micro Systems,Ltd
*/
#pragma sfr
#pragma stop
#pragma ei
#pragma di
#pragma INTERRUPT INTP5
int_speed
#ifdef GUI
#pragma INTERRUPT INTSR00
#endif
int_uart00
rb3
rb1
/* For receiving UART00 command */
#include "main.h"
#include "lib_eu.h"
int
int
char
int
char
int
62
pwm_ff;
old_ff;
old_hdata;
pwm_base = PWM_BASE_REF;
start_flag;
speed_ref = 200;
/* Active width: 0 to 750
*/
/* PWM history
*/
/* Hall IC data history
*/
/* Half of PWM period: Fixed to 750
*/
/* Identification immediately after rotation start */
/* Specified speed
*/
Application Note U18774EJ1V0AN
APPENDIX
int
unsigned int
unsigned int
unsigned int
unsigned int
unsigned int
char
char
char
char
char
char
m_speed;
int_cnt;
pid_cnt;
print_cnt;
cnt_data;
cnt_data1, cnt_data2, cnt_data3;
cw_ccw_flag = CW;
speed_flag;
cw_ccw_wait;
sys_flag;
stop_wait;
ad_flag
= FLG_OFF;
/* Motor rotation speed
*/
/* Number of times of carrier synchronization interrupt */
/* For checking PID control period
*/
/* For checking speed display interval */
/* History of number of times of carrier synchronization interrupt */
/* Rotation direction
*/
/* Presence/absence of speed information */
/* Wait for stop of reverse rotation
*/
/* System operation status
*/
/* Indicates whether system is waiting for motor to stop*/
/* Limitation of specified-speed change function */
char
char
unsigned int
clk_flag;
capture_flag;
new_clk, old_clk;
/* Timer value for speed measurement */
/*
Static variable
*/
static const unsigned char led_data[10] = {
LED_0, LED_1, LED_2, LED_3, LED_4,
LED_5, LED_6, LED_7, LED_8, LED_9
};
static char
up_flag = FLG_OFF;
static float
kp_ref = KP_DEF;
static float
ki_ref = KI_DEF;
static float
kd_ref = KD_DEF;
static float
mvn;
static float
en, en_1, en_2;
#if 0
static char
i_check_count;
static float
area_high, area_low;
#endif
#ifdef GUI
static unsigned char err_flag = FLG_OFF;
static unsigned char uart00_data[RX_BUFF_SIZE];
static unsigned char read_p, write_p;
static int
kp_tmp, ki_tmp, kd_tmp;
#endif
/* For number display
*/
/* Speed update check flag
/* Kp default value
/* Ki default value
/* Kd default value
/* Manipulated variable
/* Deviation
*/
*/
*/
*/
*/
*/
/* Error notification flag
/* UART00 receive buffer
/* Buffer pointer
/* Kp, Ki, Kd communication data
*/
*/
*/
*/
/* Starts A/D conversion
/* Unmasks overcurrent interrupt
/* Enables interrupt
*/
*/
*/
/*
Initialization
*/
void
system_init(void)
{
init_PORT();
init_OSC();
init_WDTM();
init_TW0();
init_TM00();
init_RTPM01();
init_AD();
init_TM50();
#ifdef GUI
init_UART00();
led_set(0, LED_P);
led_set(1, LED_C);
kp_tmp = (int)(kp_ref * 10000);
ki_tmp = (int)(ki_ref * 10000);
kd_tmp = (int)(kd_ref * 10000);
#else
led_set(0, LED_S);
led_set(1, LED_E);
led_set(2, LED_L);
led_set(3, LED_F);
wait(1000);
#endif
start_AD();
INTP0_on();
EI();
}
/*
System start
*/
void
system_start(void)
{
m_speed
= 0;
en_1
=
en
=
mvn
= 0.0;
int_cnt
=
pid_cnt
=
print_cnt
= 0;
cnt_data1
=
cnt_data2
=
cnt_data3
= 500;
cnt_data
= 1500;
sys_flag
= FLG_ON;
start_flag
= FLG_ON;
stop_wait
= FLG_OFF;
cw_ccw_wait = FLG_OFF;
speed_flag
= FLG_OFF;
up_flag
= FLG_OFF;
clk_flag
= FLG_OFF;
capture_flag = FLG_OFF;
pwm_ff
= PWM_F_START;
old_ff
= 0;
old_hdata
= 0;
start_TM00();
INTTW0UD_on();
/* Unmasks carrier synchronization interrupt */
INTP5_on();
Application Note U18774EJ1V0AN
63
APPENDIX
start_RTPM01();
start_TW0();
INVERTER_SW = INV_ON;
/* INVERTER enable
*/
}
/*
Restart from stop of reverse rotation
*/
void
system_restart(void)
{
m_speed
= 0;
mvn
=
en_1
=
en
= 0.0;
pid_cnt
=
print_cnt
= 0;
cnt_data1
=
cnt_data2
=
cnt_data3
= 500;
cnt_data
= 1500;
start_flag
= FLG_ON;
speed_flag
= FLG_OFF;
up_flag
= FLG_OFF;
clk_flag
= FLG_OFF;
capture_flag = FLG_OFF;
pwm_ff
= PWM_F_START;
old_ff
= 0;
old_hdata
= 0;
cw_ccw_wait = FLG_OFF;
}
/*
System stop
*/
void
system_stop(void)
{
INVERTER_SW = INV_OFF;
INTTW0UD_off();
INTP5_off();
stop_RTPM01();
stop_TW0();
stop_TM00();
sys_flag
= FLG_OFF;
stop_wait
= FLG_OFF;
cw_ccw_flag = CW;
m_speed
= 0;
}
/* INVERTER disable
/* Speed 0
*/
*/
/*
Overcurrent interrupt enable
*/
static void
INTP0_on(void)
{
PR0L.1 = 0;
EGN.0 = 1;
IF0L.1 = CLEAR;
MK0L.1 = CLEAR;
}
/* Priority
/* Falling edge
/* Clears flag
/* Unmasks
*/
*/
*/
*/
/*
Error display
*/
void
print_error(char eno)
{
char data;
switch(eno){
case ERROR_HALL:
#ifdef GUI
err_flag = MD_ERROR_HALL;
#endif
led_set(0, LED_H);
led_set(1, LED_A);
led_set(2, LED_L);
led_set(3, LED_L);
break;
case ERROR_OC:
#ifdef GUI
err_flag = MD_ERROR_OC;
#endif
led_set(0, LED_);
led_set(1, LED_O & LED_dot);
led_set(2, LED_C & LED_dot);
led_set(3, LED_);
break;
case ERROR_MOTOR:
#ifdef GUI
err_flag = MD_ERROR_MOTOR;
#endif
led_set(0, LED_F);
led_set(1, LED_A);
led_set(2, LED_I);
led_set(3, LED_L);
break;
case ERROR_S_OC:
#ifdef GUI
err_flag = MD_ERROR_OC;
#endif
64
Application Note U18774EJ1V0AN
APPENDIX
led_set(0, LED_S & LED_dot);
led_set(1, LED_);
led_set(2, LED_O & LED_dot);
led_set(3, LED_C & LED_dot);
break;
default:
data = read_Hall_IC();
if((0 == data) || (7 == data)){
#ifdef GUI
err_flag = MD_ERROR_HALL;
#endif
led_set(0, LED_H);
led_set(1, LED_A);
led_set(2, LED_L);
led_set(3, LED_L);
}else{
#ifdef GUI
err_flag = MD_ERROR_MOTOR;
#endif
led_set(0, LED_F);
led_set(1, LED_A);
led_set(2, LED_I);
led_set(3, LED_L);
}
}
#ifndef GUI
STOP();
#endif
}
/*
PID control
*/
void
pwm_pid(int cy_time)
{
unsigned long tmp;
unsigned int new_tmp, old_tmp;
int
pwm_tmp;
if(speed_flag == FLG_ON){
speed_flag = FLG_OFF;
if(start_flag == FLG_OFF){
up_flag = FLG_ON;
}else{
start_flag = FLG_OFF;
}
}
/* Speed is already measured
*/
/* Speed is already updated
*/
/* First speed information is not used */
if(pid_cnt >= (unsigned int)(cy_time*13)){ /* cy_time * 1 ms has elapsed */
pid_cnt = 0;
if(up_flag == FLG_ON){
/* Speed is updated
*/
up_flag = FLG_OFF;
if(clk_flag == FLG_ON){
DI();
clk_flag = FLG_OFF;
new_tmp = new_clk;
old_tmp = old_clk;
EI();
if(old_tmp > new_tmp){
tmp = (unsigned long)((65536 - (long)old_tmp) + (long)new_tmp);
}else{
tmp = new_tmp - old_tmp;
}
m_speed = (int)(UNIT_RPM/tmp);
}else{
m_speed = (int)(200000/(unsigned long)cnt_data);
}
if((sys_flag != FLG_OFF) &&
/* Being started
(stop_wait != FLG_ON) &&
/* Not waiting for stop
(cw_ccw_wait != FLG_ON)){
/* Not waiting for stop of reverse rotation
up_flag = FLG_OFF;
en_2 = en_1;
en_1 = en;
en
= (float)(speed_ref - m_speed);
mvn = mvn +
kp_ref*(en - en_1) +
ki_ref*en +
kd_ref*((en - en_1) - (en_1 - en_2));
if(mvn > PMW_LIMIT_H){
mvn = (float)PMW_LIMIT_H;
}else if(mvn < PMW_LIMIT_L){
mvn = (float)PMW_LIMIT_L;
}
pwm_tmp = pwm_ff + (int)mvn;
if(pwm_tmp > PWM_F_MAX){ /* Limiter
*/
pwm_ff = PWM_F_MAX;
}else if(pwm_tmp < PWM_F_MIN){
pwm_ff = PWM_F_MIN;
}else{
pwm_ff = pwm_tmp;
}
}
}
}
*/
*/
*/
}
/*
Reading voltage of speed specification variable resistor
Converts into specified speed
*/
void
get_speed(void)
{
Application Note U18774EJ1V0AN
65
APPENDIX
#ifndef GUI
unsigned int data;
unsigned long tmp;
if(ad_flag == FLG_OFF){
data = get_AD(2);
data = ((data - 3) * 12) + MIN_SPEED;
if(data > MAX_SPEED){
data = MAX_SPEED;
}else if(data < MIN_SPEED) {
data = MIN_SPEED;
};
tmp = (UNIT_RPM / (unsigned long)data);
speed_ref = (int)(UNIT_RPM / tmp);
}
#endif
}
/*
Reading value from switch (P7) and UART
Reads switch value
Returns stable value after checking for given time
*/
unsigned char
get_sw(void)
{
unsigned char data;
#ifndef GUI
int i;
unsigned char tmp;
static unsigned char start_stop_flag = FLG_OFF;
data = SW;
/* Reads switch
if(data != 0xf){
for(i=0;i<KEY_WAIT;){
/* Eliminates chattering.
tmp = SW;
/* Re-reads switch
if(data == tmp){
i++;
}else{
i = 0;
data = tmp;
}
wait(2);
}
if(sys_flag != FLG_OFF){
switch(data){
case STOP_SW:
if(start_stop_flag == FLG_OFF){
data = STOP_TR;
start_stop_flag = FLG_ON;
}
break;
*/
Must be adjusted. */
*/
case FORWARD_SW: data = FORWARD_TR; break;
case REVERSE_SW: data = REVERSE_TR; break;
case MODE_SW:
data = MODE_TR;
break;
default:
;
}
}else{
if((data == START_SW) && (start_stop_flag == FLG_OFF)){
data = START_TR;
start_stop_flag = FLG_ON;
}else if(data == MODE_SW){
data = MODE_TR;
}
}
}
if(data == 0xf){
/* No switch is pressed */
start_stop_flag = FLG_OFF; /* Prevents toggling between START and STOP */
}
#else
int
ss;
unsigned char hi, lo;
data = get_UART00();
switch(data){
case MD_CMD_GETID:
set_UART00(data);
set_UART00(MY_ID);
break;
66
/* Get ID */
case MD_CMD_GETVER:
set_UART00(data);
set_UART00(VER_MAJOR);
set_UART00(VER_MINOR);
set_UART00(VER_SEQ);
break;
/* Get Version */
case MD_CMD_START:
set_UART00(data);
data = START_TR;
break;
/* Start */
case MD_CMD_STOP:
set_UART00(data);
data = STOP_TR;
break;
/* Stop */
case MD_CMD_RESET:
set_UART00(data);
/* Reset */
Application Note U18774EJ1V0AN
APPENDIX
while(STIF00 != SET);
reset_WDTM();
break;
case MD_CMD_SETSSPEED:
lo = wait_UART00();
hi = wait_UART00();
set_UART00(data);
ss = ((int)hi<<8) + lo;
if(hi > 0x80){
ss = ~ss + 1;
data = REVERSE_TR;
if(sys_flag == FLG_OFF){
cw_ccw_flag = CCW;
}
}else{
data = FORWARD_TR;
if(sys_flag == FLG_OFF){
cw_ccw_flag = CW;
}
}
speed_ref = ss;
break;
/* Set Setting Speed */
/* CCW */
case MD_CMD_GETSSPEED:
/* Get Setting Speed */
set_UART00(data);
ss = speed_ref;
if(cw_ccw_flag == CCW){
ss = ~ss + 1;
}
lo = (unsigned char)((unsigned int)(ss)&0xff);
hi = (unsigned char)(((unsigned int)(ss)>>8)&0xff);
set_UART00(lo);
set_UART00(hi);
break;
case MD_CMD_SETPIDPARAM:
lo = wait_UART00();
hi = wait_UART00();
kp_tmp = (((int)(hi))<<8) + lo;
lo = wait_UART00();
hi = wait_UART00();
ki_tmp = (((int)(hi))<<8) + lo;
lo = wait_UART00();
hi = wait_UART00();
kd_tmp = (((int)(hi))<<8) + lo;
set_UART00(data);
kp_ref = ((float)kp_tmp)/10000;
ki_ref = ((float)ki_tmp)/10000;
kd_ref = ((float)kd_tmp)/10000;
break;
/* Sets PID feedback gain */
case MD_CMD_GETPIDPARAM:
/* Reads PID feedback gain */
set_UART00(data);
kp_tmp = ((int)kp_ref)*10000;
set_UART00((unsigned char)((unsigned int)(kp_tmp)&0xff));
set_UART00((unsigned char)(((unsigned int)(kp_tmp)>>8)&0xff));
ki_tmp = ((int)ki_ref)*10000;
set_UART00((unsigned char)((unsigned int)(ki_tmp)&0xff));
set_UART00((unsigned char)(((unsigned int)(ki_tmp)>>8)&0xff));
kd_tmp = ((int)kd_ref)*10000;
set_UART00((unsigned char)((unsigned int)(kd_tmp)&0xff));
set_UART00((unsigned char)((unsigned int)((kd_tmp)>>8)&0xff));
break;
case MD_CMD_GETSPEED:
/* Reads rotation speed */
if(err_flag != FLG_OFF){
set_UART00(err_flag);
err_flag = FLG_OFF;
}else{
set_UART00(data);
if(m_speed == 0){
set_UART00(0);
set_UART00(0);
}else{
ss = m_speed;
if(cw_ccw_wait == FLG_OFF){
if(cw_ccw_flag == CCW){
ss = ~ss + 1;
}
}else{ /* Waits for stop of reverse rotation */
if(cw_ccw_flag == CW){
ss = ~ss + 1;
}
}
set_UART00((unsigned char)((unsigned int)(ss)&0xff));
set_UART00((unsigned char)(((unsigned int)(ss)>>8)&0xff));
}
}
break;
default:
;
}
#endif
return(data);
}
/*
Port settings
*/
static void
init_PORT(void)
{
INTP0 = IN;
/* Interrupt of overcurrent notification */
Application Note U18774EJ1V0AN
67
APPENDIX
SW2 = IN;
SW3 = IN;
SW4 = IN;
SW5 = IN;
/* START/STOP
/* FORWARD
/* REVERSE
/* MODE
*/
*/
*/
*/
INVERTER_SW_MODE = OUT;
INVERTER_SW
= INV_OFF;
/* INVERTER enable/disable */
/* INVERTER disable
*/
EGP5 = SET;
EGN5 = CLEAR;
/* Rising edge is valid */
LD_LED0 = OUT;
LD_LED1 = OUT;
LD_LED2 = OUT;
LD_LED3 = OUT;
/* LED selection: output */
LD_DATA = OUT;
/* LED display data: output */
}
/*
Clock switching
*/
static void
init_OSC(void)
{
IMS = IMS_DATA;
clear_WDTM();
while(OSTC != 0x1f);
PCC
= 0x00;
MCM.0 = 1;
VSWC.1 = 1;
}
/* Switches memory size */
/* Waits for oscillation to stabilize */
/* Sets division ratio */
/* X1 input clock */
/*
Inverter timer
*/
static void
init_TW0(void)
{
TCL02
= 0;
/* Count clock: 20 MHz
*/
TCL01
= 0;
TCL00
= 0;
IDEV02
= 0;
/* Generates interrupt every time */
IDEV01
= 0;
IDEV00
= 0;
TW0M
= 0;
TW0TRGS = 0;
TW0OC
= 0;
TW0CM3
= PWM_BASE_REF;
TW0CM0
= PWM_BASE_REF - PWM_F_REF;
TW0CM1
= PWM_BASE_REF - PWM_F_REF;
TW0CM2
= PWM_BASE_REF - PWM_F_REF;
TW0DTIME = PWM_DTM_REF;
/* Dead time
TW0BFCM3 = PWM_BASE_REF;
TW0BFCM0 = PWM_BASE_REF - PWM_F_REF;
TW0BFCM1 = PWM_BASE_REF - PWM_F_REF;
TW0BFCM2 = PWM_BASE_REF - PWM_F_REF;
TW0TRGS = 1;
}
*/
void
set_TW0(int a, int b, int c, int base)
{
TW0BFCM3 = (unsigned int)base;
TW0BFCM0 = (unsigned int)a;
TW0BFCM1 = (unsigned int)b;
TW0BFCM2 = (unsigned int)c;
}
static void
start_TW0(void)
{
TW0C.7 = SET;
}
static void
stop_TW0(void)
{
TW0C.7 = CLEAR;
}
/* Starts timer
*/
/* Stops timer
*/
/*
16-bit timer
CR01
Real-time output port trigger
*/
static void
init_TM00(void)
{
ES000 = 0;
ES001 = 0;
CRC000 = 1;
CRC001 = 1;
CRC002 = 0;
PRM001 = SET;
PRM000 = CLEAR;
}
static void
start_TM00(void)
{
TMIF00 = CLEAR;
TMC00 = 0x04;
}
68
/* Valid edge of TI000 pin: falling edge
/* CR00 capture register
/* Capture with reverse phase of valid edge of TI000 pin
/* CR01 compare register
/* Count clock: 78.125 kHz */
/* Free-running mode
*/
Application Note U18774EJ1V0AN
*/
*/
*/
*/
APPENDIX
static void
stop_TM00(void)
{
TMC00 = CLEAR;
}
/* Stops timer
*/
/* Real-time output mode
/* 6 bits × 1 channel
/* PWM modulation output
/* Output buffer
*/
*/
*/
*/
/* Sets excitation pattern
*/
/*
Real-time port
*/
static void
init_RTPM01(void)
{
RTPM01 = 0x3f;
RTPC01 = 0x20;
DCCTL01 = 0xc0;
RTBL01 = 0x3f;
}
void
set_RTPM01(unsigned char data)
{
RTBL01 = data;
CR01
= TM00 + 1;
}
static void
start_RTPM01(void)
{
RTPC01.7 = SET;
}
static void
stop_RTPM01(void)
{
RTPC01.7 = CLEAR;
}
/* Enables operation */
/* Disables operation */
/*
AD
*/
static void
init_AD(void)
{
ADS
= 4;
ADM
= 0x04;
}
static void
start_AD(void)
{
ADIF = CLEAR;
ADCS = SET;
while(ADIF != SET);
}
/* SPEED */
/* 3.6 us */
/* Clears interrupt notification flag
/* Enables conversion operation
/* Waits for interrupt notification
*/
*/
*/
static unsigned int
get_AD(char s_bit)
{
return((((unsigned int)ADCR)>>(s_bit+6))&0x3ff);
}
/*
Watchdog timer
*/
static void
init_WDTM(void)
{
clear_WDTM();
WDTM = WDTM_SET;
}
void
clear_WDTM(void)
{
WDTE = WDTE_CLR;
}
void
reset_WDTM(void)
{
WDTE = WDTE_RESET;
}
/*
8-bit timer 50
*/
static void
init_TM50(void)
{
TCL50 = 0x06;
CR50 = 78;
}
static void
start_TM50(void)
{
TMIF50 = CLEAR;
TCE50 = SET;
}
static void
wait_TM50(void)
{
while(TMIF50 != SET);
TCE50 = CLEAR;
/* 1 ms */
/* Clears interrupt notification flag
/* Starts timer
*/
*/
/* Waits for interrupt notification
/* Stops timer
*/
*/
Application Note U18774EJ1V0AN
69
APPENDIX
}
/*
Speed display
*/
void
speed_print(int ms)
{
if((MODE_SW == SW) || (sys_flag == FLG_OFF)){
led_print(speed_ref, ad_flag);
}else{
if(print_cnt > (unsigned int)(ms*13)){
led_print(m_speed, FLG_OFF);
print_cnt = 0;
}
}
}
/*
Carrier synchronization interrupt enable
*/
static void
INTTW0UD_on(void)
{
UDIFW0 = CLEAR;
UDMKW0 = CLEAR;
}
/* Clears request flag
/* Enables interrupt
*/
*/
/*
Carrier synchronization interrupt disable
*/
static void
INTTW0UD_off(void)
{
UDMKW0 = SET;
UDIFW0 = CLEAR;
}
/* Disables interrupt */
/*
INTP5 interrupt enable
*/
static void
INTP5_on(void)
{
PIF5 = CLEAR;
PMK5 = CLEAR;
}
/*
INTP5 interrupt disable
*/
static void
INTP5_off(void)
{
PMK5 = SET;
}
/*
Displaying value on 7-segment LED
*/
static void
led_print(int data, char flag)
{
unsigned int tmp;
int flg = FLG_OFF;
tmp = (unsigned int)(data / 1000);
if(tmp != 0){
/* Most significant digit is not 0 */
flg = FLG_ON;
/* Numerical value is displayed on most significant digit */
led_set(0, led_data[tmp]);
}else{
led_set(0, LED_);
}
data %= 1000;
tmp = (unsigned int)(data / 100);
if((tmp != 0) || (flg == FLG_ON)){
/* Value is not 0 or number has already been displayed */
flg = FLG_ON;
led_set(1, led_data[tmp]);
}else{
led_set(1, LED_);
}
data %= 100;
tmp = (unsigned int)(data / 10);
if((tmp != 0) || (flg == FLG_ON)){
/* Value is not 0 or number has already been displayed */
led_set(2, led_data[tmp]);
}else{
led_set(2, LED_);
}
data %= 10;
if(flag == FLG_OFF){
led_set(3, led_data[data]);
}else{
led_set(3, (unsigned char)(led_data[data])&LED_dot);
}
}
/*
Displaying data on 7-segment LED
no: Location of LED to be displayed
data: Data to be output
*/
static void
led_set(unsigned char no, unsigned char data)
{
unsigned char p;
70
Application Note U18774EJ1V0AN
APPENDIX
P6 = 0x00;
P4 = data;
switch(no){
case 0: p = 0x80; break;
case 1: p = 0x40; break;
case 2: p = 0x20; break;
default: p = 0x10; break;
}
P6 = p;
/* Location to be displayed */
/* Left edge
*/
/* Right edge
*/
/* Starts timer
/* Waits for generation of interrupt request
/* Clears watchdog timer
*/
*/
*/
}
/*
Time adjustment
*/
static void
wait(int cnt)
{
int i;
ms
for(i=0;i<cnt;i++){
start_TM50();
wait_TM50();
clear_WDTM();
}
return;
}
/*
Reading Hall IC
*/
char
read_Hall_IC(void)
{
return((char)((P0>>1)&0x7));
}
/*
INTP5
*/
__interrupt void
int_speed(void)
{
capture_flag = FLG_ON;
}
#ifdef GUI
/*
UART00 settings
*/
static void
init_UART00(void)
{
PM10
= IN;
PM11
= OUT;
PM13
= IN;
PM14
= OUT;
RTS
= 1;
P14
= 1;
BRGC00 = 0x56;
PS001
= CLEAR;
PS000
= CLEAR;
CL00
= SET;
SL00
= CLEAR;
POWER00 = SET;
TXE00
= SET;
RXE00
= SET;
STIF00
= SET;
SRIF00
= CLEAR;
SRMK00 = CLEAR;
RTS
= 0;
read_p
= 0;
write_p
= 0;
}
/* 115200 */
/* 8 bits */
/* Enables receive interrupt */
/*
Returning UART00 receive buffer data
*/
static unsigned char
get_UART00(void)
{
unsigned char data = 0;
if(read_p != write_p){
data = uart00_data[read_p++];
read_p %= 6;
}
return(data);
/* Receive data is present
/* Extracts data
/* Updates read pointer
*/
*/
*/
/* Waits until data is input to buffer
/* Extracts data
/* Updates read pointer
*/
*/
*/
}
/*
Waiting for UART00 reception
*/
static unsigned char
wait_UART00(void)
{
unsigned char data;
while(read_p == write_p);
data = uart00_data[read_p++];
read_p %= 6;
return(data);
}
/*
Application Note U18774EJ1V0AN
71
APPENDIX
Transmission from UART00
*/
static void
set_UART00(unsigned char data)
{
while(STIF00 != SET);
STIF00 = CLEAR;
TXS00 = data;
}
/* Waits for transmission completion
*/
/* Transmits
*/
/* Reads error status
/* Stores status in receive buffer
/* Updates write pointer
*/
*/
*/
/*
For UART00 command reception
*/
__interrupt void
int_uart00(void)
{
unsigned char tmp;
tmp = ASIS00;
uart00_data[write_p++] = RXB00;
write_p %= RX_BUFF_SIZE;
}
#endif
72
Application Note U18774EJ1V0AN
For further information,
please contact:
NEC Electronics Corporation
1753, Shimonumabe, Nakahara-ku,
Kawasaki, Kanagawa 211-8668,
Japan
Tel: 044-435-5111
http://www.necel.com/
[America]
[Europe]
[Asia & Oceania]
NEC Electronics America, Inc.
2880 Scott Blvd.
Santa Clara, CA 95050-2554, U.S.A.
Tel: 408-588-6000
800-366-9782
http://www.am.necel.com/
NEC Electronics (Europe) GmbH
Arcadiastrasse 10
40472 Düsseldorf, Germany
Tel: 0211-65030
http://www.eu.necel.com/
NEC Electronics (China) Co., Ltd
7th Floor, Quantum Plaza, No. 27 ZhiChunLu Haidian
District, Beijing 100083, P.R.China
Tel: 010-8235-1155
http://www.cn.necel.com/
Hanover Office
Podbielskistrasse 166 B
30177 Hannover
Tel: 0 511 33 40 2-0
Munich Office
Werner-Eckert-Strasse 9
81829 München
Tel: 0 89 92 10 03-0
Stuttgart Office
Industriestrasse 3
70565 Stuttgart
Tel: 0 711 99 01 0-0
United Kingdom Branch
Cygnus House, Sunrise Parkway
Linford Wood, Milton Keynes
MK14 6NP, U.K.
Tel: 01908-691-133
Succursale Française
9, rue Paul Dautier, B.P. 52
78142 Velizy-Villacoublay Cédex
France
Tel: 01-3067-5800
Sucursal en España
Juan Esplandiu, 15
28007 Madrid, Spain
Tel: 091-504-2787
Tyskland Filial
Täby Centrum
Entrance S (7th floor)
18322 Täby, Sweden
Tel: 08 638 72 00
Filiale Italiana
Via Fabio Filzi, 25/A
20124 Milano, Italy
Tel: 02-667541
Shanghai Branch
Room 2509-2510, Bank of China Tower,
200 Yincheng Road Central,
Pudong New Area, Shanghai, P.R.China P.C:200120
Tel:021-5888-5400
http://www.cn.necel.com/
Shenzhen Branch
Unit 01, 39/F, Excellence Times Square Building,
No. 4068 Yi Tian Road, Futian District, Shenzhen,
P.R.China P.C:518048
Tel:0755-8282-9800
http://www.cn.necel.com/
NEC Electronics Hong Kong Ltd.
Unit 1601-1613, 16/F., Tower 2, Grand Century Place,
193 Prince Edward Road West, Mongkok, Kowloon, Hong Kong
Tel: 2886-9318
http://www.hk.necel.com/
NEC Electronics Taiwan Ltd.
7F, No. 363 Fu Shing North Road
Taipei, Taiwan, R. O. C.
Tel: 02-8175-9600
http://www.tw.necel.com/
NEC Electronics Singapore Pte. Ltd.
238A Thomson Road,
#12-08 Novena Square,
Singapore 307684
Tel: 6253-8311
http://www.sg.necel.com/
NEC Electronics Korea Ltd.
11F., Samik Lavied’or Bldg., 720-2,
Yeoksam-Dong, Kangnam-Ku,
Seoul, 135-080, Korea
Tel: 02-558-3737
http://www.kr.necel.com/
Branch The Netherlands
Steijgerweg 6
5616 HS Eindhoven
The Netherlands
Tel: 040 265 40 10
G0706