You are on page 1of 16

Freescale Semiconductor AN3318

Rev. 1.0, 9/2006


Application Note

Closed Loop Software Design for the


KIT 34929
By: Juan Sahagun
RTAC Americas
Mexico

1 Introduction Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
This application note demonstrates how to
implement a first order Proportional-Integral (PI) 2 Closed Loop Block Diagram . . . . . . . . . . . . . 2
closed loop control algorithm using the MC34929
3-phase Brushless DC (BLDC) Motor Driver IC, and 3 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
an 8-bit MCU MC9S08QG8 to process all the
external signals, and provide the correct control to 4 MC9S08QG8 Features. . . . . . . . . . . . . . . . . . . 3
the motor driver. It also gives a guideline to develop
5 PI Control Basic Theory . . . . . . . . . . . . . . . . . 4
the software code implemented on the
KIT34929EPEVME.
6 Software Flowchart Diagram . . . . . . . . . . . . . 5
The PI control specifies the procedure to calculate
the proportional and integrative constants as well 7 MC9S08QG8 Signal Processing . . . . . . . . . . 6
as all the MCU signal processing.
8 Control Process Implementation. . . . . . . . . 11
All of these processes are considered to be
implemented with an MC9S08QG8 8-bit MCU, so 9 Timing Considerations . . . . . . . . . . . . . . . . . 13
calculations and timing processes could change
depending on the selected MCU. 10 Practical Results . . . . . . . . . . . . . . . . . . . . . 14

11 Conclusions. . . . . . . . . . . . . . . . . . . . . . . . . 15

12 References. . . . . . . . . . . . . . . . . . . . . . . . . . 15

© Freescale Semiconductor, Inc., 2006. All rights reserved.


Closed Loop Block Diagram

2 Closed Loop Block Diagram

Reference Signal

MC9SO8QG8
MCU CONTROL

PWM RUN DIR TACH, 3XTACH


3-Hall Effect
Sensors
MC34929 Feedback
BLDC DRIVER
BLDC
3_PHASE MOTOR

Figure 1. Closed Loop Block Diagram

Explanation:
1. The MCU first receives an external reference signal.
2. This signal is compared with the actual motor speed measured by Hall Effect sensors.
3. The error between these two signals is computed by a First Order PI closed loop control
algorithm.
4. Depending on the error, a new pulse width PWM signal is generated to the BLDC Driver.
5. The BLDC driver automatically interprets the signal and drives a 3-phase BLDC Motor.
6. This process repeats.

3 Features
The MC34929 is a complete BLDC motor driver integrated in one chip. Its main features are:
1. Operation Supply Voltage (8V-28V)
2. 3-Phase Hall Effect sensors interface
3. Two tachometer outputs (TACH and 3XTACH)
4. Adjustable Maximum Current Limit
5. Short Circuit Detection and Protection
6. Overtemperature Detection and Thermal Shutdown
7. Undervoltage Detection and Shutdown

Closed Loop Software Design for the KIT34929, Rev. 1.0


2 Freescale Semiconductor
MC9S08QG8 Features

Figure 2. MC34929 Simplified Application Diagram

4 MC9S08QG8 Features
The MC9S08QG8 is a member of the low-cost, high-performance HCS08 Family of 8-bit
microcontroller units (MCUs). All MCUs in the family use the enhanced HCS08 core and are
available with a variety of modules, memory sizes, memory types, and package types.

Figure 3. MC9S08QG8 Features

Closed Loop Software Design for the KIT34929, Rev. 1.0


Freescale Semiconductor 3
PI Control Basic Theory

This application note uses the 16 TSSOP packaged MCU, which is contained on the
KIT34929EPEVME EVB.

5 PI Control Basic Theory


The use of an Proportional controller algorithm will have the effect of decreasing the time
constant of motor speed, in closed loop control, as well as reducing, but not eliminating the
steady state error (desired speed - actual speed feedback). However, the use of the Integral
control algorithm will eliminate the steady state error.
The PI controller transfer function is:

Kp Kp
C ( s) = K p + , τi =
τi ⋅ s Ki

R(z) E(z) M(z) U(z)


+
C(z) G(z)
-

PI Control
Transfer
Function

To apply the Z Transform to this function, it is necessary to use the following conversions:

Obtaining:

Kp Kp
C ( z) = K p + , τi =
τ i ⋅ (1 − z ) −1
Ki

–1
( Kp + Ki ) – Kp • z
C ( z ) = -------------------------------------------------
–1
-
1–z

Closed Loop Software Design for the KIT34929, Rev. 1.0


4 Freescale Semiconductor
Software Flowchart Diagram

6 Software Flowchart Diagram


The software described in this flow chart is contained in the file "mainWithPI.c", discussed in
MC34929EPUG (User Guide).

Port Initialization

TRIM Adjust

PWM Register
Configuration

MTIM Module
Configuration

Keyboard Register
Configuration

Enable Interrupts

Loop Cycle Channel 1 PWM


Interrupt

Keyboard Interrupt MTIM Interrupt

500ms

Set new
PI Control
PWM Calculation

Figure 4. Software Flowchart Diagram

Closed Loop Software Design for the KIT34929, Rev. 1.0


Freescale Semiconductor 5
MC9S08QG8 Signal Processing

The software described below is contained in the file "mainWithPI.c", discussed in


MC34929EPUG (User Guide) and is used with KIT34929EPEVME.
Description:
1. It is first necessary to initialize the pins data direction and give them an initial value.
2. Fix the MCU’s internal oscillator’s Trim Value to increase the internal clock precision.
3. Configure all the registers to be used as PWM, MTIM and KEYBOARD registers.
4. Enable all the interrupts.
5. A loop cycle is started and the PWM output signal is generated depending on the initial
established values.
6. Until reference signal appears, the Keyboard interrupt is activated.
7. At this time the MTIM timer is activated until the reference signal shut down.
8. The pulse width of the reference signal is measured and feedback is given to the PWM.
9. During this process, some TACH feedback values are stored.
10. After 500ms (½ motor turn @ 1 RPS) the stored values begin a PI process control and
set a new pulse width value of the PWM.
11. The loop cycle repeats.

7 MC9S08QG8 Signal Processing


1. Reference Signal Characteristics and decoding process.
The reference signal is generated every 20ms.

Figure 5. Reference Pulse Control

This reference signal consist of a periodical pulse width, which varies


from 0.9ms to 2.1ms with a neutral time of 1.5ms
When the pulse width increases from 1.5ms to 2.1ms, the speed of
the motor increases from STOP to full speed in CW direction.

Closed Loop Software Design for the KIT34929, Rev. 1.0


6 Freescale Semiconductor
MC9S08QG8 Signal Processing

When the pulse width decreases from 1.5ms to 0.9ms, the speed of
the motor increases from STOP to full speed in CCW direction.
2. PWM requirements for the MC34929.
MC34929 BLDC driver can operate with a Maximum input frequency
of 100 kHz. This driver uses the same input frequency to control the
PWM output signal.
It’s important to choose an appropriate frequency, because it can
change the power consumption of the IC. (For more information
regarding this point please refer to the Hardware design application
note AN3319)
3. How to generate the PWM signal.
For the purposes of this application, the PWM signal is generated by
the TPM (Time/Pulse Width Modulator) registers of MC9S08QG8
MCU.
The following TPM registers are used to generate the PWM signal to
control the Speed of the Motor:

Figure 6. TPMSC (8-bit status and control register)

This register controls the Timer Overflow Flag, Timer Overflow


Interrupt Enable, Center-Aligned PWM Select, Clock Source Select
and the Prescaler divisor.
In this application the TPMSC value is 0x08. Setting the bus rate
clock as the TPM clock source for Prescaler input and configuring the
TPM channel to operate as input capture, output compare, or
edge-aligned depends on the MSnB:MSnA values of the TPMCnSC
register.

Closed Loop Software Design for the KIT34929, Rev. 1.0


Freescale Semiconductor 7
MC9S08QG8 Signal Processing

Figure 7. TPMMOD (16-bit module register

This read/write module contains the value of the TPM counter.


After the TPM counter reaches the module value, the counter starts
again and the overflow flag becomes set.
In this application, the TMMOD value is 0x190, setting a PWM period
of 20 kHz.

Figure 8. TPMCnSC (8-bit status and control register)

This register contains the channel interrupt status flag and control
bits that are used to configure the interrupt enable, channel
configuration, and pin function.
For this application, channel 1 is used to generate the PWM signal,
and TPMCnSC becomes TPMC1SC with a value of 0x24. With this
value, the PWM generates as Edge-aligned.

Closed Loop Software Design for the KIT34929, Rev. 1.0


8 Freescale Semiconductor
MC9S08QG8 Signal Processing

During the program, TPMC1SC_CH1IE changes from 0 to 1 allowing


the interrupt vector to do the Feedback count.

Figure 9. PWM Period and Pulse Width

Figure 10. TPMCnV (16-bit channel value register)

The PWM duty cycle depends on the 16-bit value charged into this
register.
This value must be between 0x0000, and the Period value set on
TPMMOD (0x0190).
This value has to be updated for every reference signal or PI control
value.
4. Interpreting the Feedback signal.
The TACH feedback is a square signal which indicates the position
of the motor, one cycle of this square signal indicates one turn of the
motor.
In this application, the feedback signal TACH is polled by the MCU
every 50ms (Frequency 20kHz) by reading bit 2 of the MCU’s parallel
port A. This bit detects every change of state of the pin.

Closed Loop Software Design for the KIT34929, Rev. 1.0


Freescale Semiconductor 9
MC9S08QG8 Signal Processing

With this count, it is possible to obtain the feedback speed and in


accordingly, the error value.
The period of the control process depends on the minimum speed of
the motor. For this case we specified a period of 500ms, which
indicates that if the motor speed is 60 RPM, we can measure one turn
per second or two counts of the MCU port A, bit 2 polling process.
To accurately interpret the TACH signal, it is important to know the
maximum numbers of counts possible for the polling process, which
depends on the maximum speed of the motor.
The program code has a function which makes it necessary to set a
Max Speed in RPM of the motor to be controlled. The following
program code applies:
Function where the RPM of the motor has to be set:
void InitPorts(UINT16 RPM);
The C code to interpret the RPM signal is:
RPS=RPM/60; /*RPS*/
MAXhallcount=RPS/2; /*MAX value of counts every 500ms*/
/*Time of 1 RPS*/
HallFactor=4000/MAXhallcount; /*Frequency hall signal sampling */
Turns = HallCount/2;
Feedback = Turns * HallFactor;
Feedback = Feedback/10;

First, it is necessary to calculate a multiplicative factor (HallFactor) to


get the feedback value and the PWM duty cycle in the same
comparison scale.
To get this value the numbers of turns at max control speed is
needed.
As previously mentioned, the process control period is 500ms. The
maximum number of counts (MAXhallcount) are determined by
dividing the max RPS of the motor by 2.
Following that, divide the max value of the duty cycle (0x0190) by the
maximum number of counts (MAXhallcount) to obtain the
multiplicative factor (HallFactor)
HallCount is the value measured from the TACH feedback signal.
Divide it by 2 to get the number of motor turns, and then multiply the
result and the HallFactor to obtain the scaled Feedback value
(Feedback).

Closed Loop Software Design for the KIT34929, Rev. 1.0


10 Freescale Semiconductor
Control Process Implementation

5. Obtaining Error Value.


Following the last decoding process, compare the Feedback value
and the reference signal value (Speed).
Error = Speed - Feedback; (steady state error)

8 Control Process Implementation


The motor system to be controlled was considered as a first order
system. The actual motor speed is calculated from input capture
channels, and the desired speed is generated in the microcontroller.

R(z) E(z) M(z) U(z)


+
C(z) G(z)
-

System
Transfer
Function

This system has the following transfer function in the continuous time
domain.

Taking the Z transform, and considering the zero order hold of the
PWM module, the system’s transfer function becomes:

–T ⁄ τ –1
(1 – e )⋅z
G ( z ) = -------------------------------------------
–T ⁄ τ –1
1–e ⋅z
As it was shown in section 5, the PI control’s function is:

Closed Loop Software Design for the KIT34929, Rev. 1.0


Freescale Semiconductor 11
Control Process Implementation

If we join the C(z) and G(z) to have just one block that shows the
closed loop system we obtain:

C ( z) ⋅ G( z)
R(z) U(z)
1 + C ( z) ⋅ G( z)
Gd(z)

Where:

– T ⁄ τd –1
(1 – e )⋅z
G d ( z ) = -----------------------------------------------
– T ⁄ τd – 1
1–e ⋅z

Then the controller is:

Gd ( z )
C ( z ) = ----------------------------------------------
G ( z ) ⋅ [ 1 – Gd ( z ) ]

– T ⁄ τd –T ⁄ τ –1 –1
1–e 1–e ⋅z ( Kp + Ki ) – Kp ⋅ z
C ( z ) = ---------------------------- ⋅ --------------------------------------- = ----------------------------------------------------
–T ⁄ τ –1 –1
1–e 1–z 1–z
Solving for Ki and KP:

– T ⁄ τd
Ki = 1 – e

–T ⁄ τ
Ki • e
K p = ----------------------------
–T ⁄ τ
1–e
Where:
T : Controller Period
τ : Time constant of motor speed in open loop.
τd : Desired time constant of motor speed in closed loop.
KP : Proportional Gain of the controller.
Ki : Integral Gain of the controller.

Closed Loop Software Design for the KIT34929, Rev. 1.0


12 Freescale Semiconductor
Timing Considerations

9 Timing Considerations

Graphic 1 Graphic 2
In Graphic 1 and Graphic 2 the following signals are shown (Graphic
2 represents a zoom in of a single yellow/green pulse event):
a) Reference Signal (Yellow) - top signals
b) MTIM Signal (Green) - middle signals
c) PWM (Pink) - bottom signals
The MTIM Timer is configured with a frequency of 40 KHz, counting
the time of the reference design pulse width.
The MTIM is activated when the Keyboard detects a reference
design rising edge and turns off when the Keyboard detects a falling
edge.
During all this time the TPM produces a PWM signal.
All closed loop calculations are processed once the MTIM is turned
off. The time you have available to make this calculation is the result
of the difference between the 20ms of reference period and the lost
time to execute the MTIM counter (reference signal pulse width).
The maximum and the minimum time to execute this calculation are:

Closed Loop Software Design for the KIT34929, Rev. 1.0


Freescale Semiconductor 13
Practical Results

10 Practical Results
The implementation of the PI controller using parallel programming
are shown in the following graphics:

Ki
+
E(z) M(z)

Ki +
1 − z −1

Based on this diagram we obtain the following equations in discrete


time domain:

For each motor it is necessary to calculate its corresponding Kp and


Ki values:

– T ⁄ τd
Ki = 1 – e

–T ⁄ τ
Ki • e
K p = ----------------------------
–T ⁄ τ
1–e
For this application, the values used to calculate Ki and Kp were the
following:
T = 500ms
τ = 5.44ms
τd = 1s.

Closed Loop Software Design for the KIT34929, Rev. 1.0


14 Freescale Semiconductor
Conclusions

Taking these time values and applying the corresponding formulas


for KP and KI we obtained the next results:

–T ⁄ τ
Ki • e – 38
K p = ---------------------------- = 1.22 × 10
–T ⁄ τ
1–e

– T ⁄ τd
Ki = 1 – e = 0.393

These values are scaled by 256 ( KI = 100, KP ˜ 1) to get a better


calculation process. At the end of the operation, the result must be
divided by 256:
MI = ((signed long)(Error*KI)) + ((signed long)(MI));
MK = ((signed int)(Error*KP)) + ((signed long)(MI));
MK = MK>>8;
The MK result can now be added to the output signal to make the
closed loop control.
Speed = ((signed int)(Speed)) + ((signed int)(MK));Recommended
Schematic

11 Conclusions
The MC34929 is not only a 3-phase BLDC motor control IC, but also it can interpret Hall Sensor
inputs and provide TACH signal outputs required to support a PI closed loop control algorithm.
Together with an MCU, the MC34929 provides a complete and intelligent 3-phase BLDC Motor
Control Driver solution.

12 References
1. Katsuhiko Ogata, MODERN CONTROL ENGINEERING, Third Edition, Prentice- Hall,
1998.
2. W. Bolton, Control Engineering, 2nd Edition, Alfaomega, 2001.
3. Katsuhiko Ogata, DISCRETE TIME CONTROL SYSTEMS, Second Edition, Prentice-
Hall, 1996.
4. MC34929 Data Sheet
5. AN3319 - Hardware Design Using the MC34929
6. KIT34929 Software Programming Quickstart User Guide - MC34929EPUG

Closed Loop Software Design for the KIT34929, Rev. 1.0


Freescale Semiconductor 15
How to Reach Us: RoHS-compliant and/or Pb-free versions of Freescale products have the functionality
and electrical characteristics of their non-RoHS-compliant and/or non-Pb-free
Home Page: counterparts. For further information, see http://www.freescale.com or contact your
www.freescale.com Freescale sales representative.

E-mail:
support@freescale.com For information on Freescale’s Environmental Products program, go to
http://www.freescale.com/epp.
USA/Europe or Locations Not Listed:
Freescale Semiconductor
Technical Information Center, CH370
1300 N. Alma School Road
Chandler, Arizona 85224
+1-800-521-6274 or +1-480-768-2130
support@freescale.com
Information in this document is provided solely to enable system and software
Europe, Middle East, and Africa: implementers to use Freescale Semiconductor products. There are no express or
Freescale Halbleiter Deutschland GmbH implied copyright licenses granted hereunder to design or fabricate any integrated
Technical Information Center circuits or integrated circuits based on the information in this document.
Schatzbogen 7
81829 Muenchen, Germany
+44 1296 380 456 (English) Freescale Semiconductor reserves the right to make changes without further notice to
+46 8 52200080 (English) any products herein. Freescale Semiconductor makes no warranty, representation or
+49 89 92103 559 (German) guarantee regarding the suitability of its products for any particular purpose, nor does
+33 1 69 35 48 48 (French) Freescale Semiconductor assume any liability arising out of the application or use of any
support@freescale.com
product or circuit, and specifically disclaims any and all liability, including without
Japan: limitation consequential or incidental damages. “Typical” parameters that may be
Freescale Semiconductor Japan Ltd. provided in Freescale Semiconductor data sheets and/or specifications can and do vary
Headquarters in different applications and actual performance may vary over time. All operating
ARCO Tower 15F parameters, including “Typicals”, must be validated for each customer application by
1-8-1, Shimo-Meguro, Meguro-ku, customer’s technical experts. Freescale Semiconductor does not convey any license
Tokyo 153-0064 under its patent rights nor the rights of others. Freescale Semiconductor products are
Japan not designed, intended, or authorized for use as components in systems intended for
0120 191014 or +81 3 5437 9125
support.japan@freescale.com surgical implant into the body, or other applications intended to support or sustain life,
or for any other application in which the failure of the Freescale Semiconductor product
Asia/Pacific: could create a situation where personal injury or death may occur. Should Buyer
Freescale Semiconductor Hong Kong Ltd. purchase or use Freescale Semiconductor products for any such unintended or
Technical Information Center unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor and
2 Dai King Street its officers, employees, subsidiaries, affiliates, and distributors harmless against all
Tai Po Industrial Estate claims, costs, damages, and expenses, and reasonable attorney fees arising out of,
Tai Po, N.T., Hong Kong directly or indirectly, any claim of personal injury or death associated with such
+800 2666 8080
support.asia@freescale.com unintended or unauthorized use, even if such claim alleges that Freescale
Semiconductor was negligent regarding the design or manufacture of the part.
For Literature Requests Only:
Freescale Semiconductor Literature Distribution Center Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc.
P.O. Box 5405
Denver, Colorado 80217 All other product or service names are the property of their respective owners.
1-800-441-2447 or 303-675-2140 © Freescale Semiconductor, Inc., 2006. All rights reserved.
Fax: 303-675-2150
LDCForFreescaleSemiconductor@hibbertgroup.com

AN3318
Rev. 1.0
9/2006

You might also like