Freescale Semiconductor Application Note

AN3318 Rev. 1.0, 9/2006

Closed Loop Software Design for the KIT 34929
By: Juan Sahagun RTAC Americas Mexico



1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

This application note demonstrates how to implement a first order Proportional-Integral (PI) closed loop control algorithm using the MC34929 3-phase Brushless DC (BLDC) Motor Driver IC, and an 8-bit MCU MC9S08QG8 to process all the external signals, and provide the correct control to the motor driver. It also gives a guideline to develop the software code implemented on the KIT34929EPEVME. The PI control specifies the procedure to calculate the proportional and integrative constants as well as all the MCU signal processing. All of these processes are considered to be implemented with an MC9S08QG8 8-bit MCU, so calculations and timing processes could change depending on the selected MCU.

2 Closed Loop Block Diagram . . . . . . . . . . . . . 2 3 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 4 MC9S08QG8 Features. . . . . . . . . . . . . . . . . . . 3 5 PI Control Basic Theory . . . . . . . . . . . . . . . . . 4 6 Software Flowchart Diagram . . . . . . . . . . . . . 5 7 MC9S08QG8 Signal Processing . . . . . . . . . . 6 8 Control Process Implementation. . . . . . . . . 11 9 Timing Considerations . . . . . . . . . . . . . . . . . 13 10 Practical Results . . . . . . . . . . . . . . . . . . . . . 14 11 Conclusions. . . . . . . . . . . . . . . . . . . . . . . . . 15 12 References. . . . . . . . . . . . . . . . . . . . . . . . . . 15

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

Closed Loop Block Diagram


Closed Loop Block Diagram
Reference Signal




3-Hall Effect Sensors Feedback

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.



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


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.


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:

C ( s) = K p +


τi ⋅ s

, τi =

Kp Ki


+ -






PI Control Transfer Function

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


C ( z) = K p +


τ i ⋅ (1 − z )

, τi =

Kp Ki

( 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


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 500ms Set new PWM PI Control Calculation

MTIM Interrupt

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.


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; MAXhallcount=RPS/2; HallFactor=4000/MAXhallcount; Turns = HallCount/2; Feedback = Turns * HallFactor; Feedback = Feedback/10; /*RPS*/ /*MAX value of counts every 500ms*/ /*Time of 1 RPS*/ /*Frequency hall signal sampling */

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)


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.


+ -






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) 1 + C ( z) ⋅ G( 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 ) ] –1 –T ⁄ τ –1 – T ⁄ τd ( Kp + Ki ) – Kp ⋅ z 1–e ⋅z 1–e C ( z ) = ---------------------------- ⋅ -------------------------------------- = --------------------------------------------------–1 –1 –T ⁄ τ 1–z 1–z 1–e Solving for Ki and KP: Ki = 1 – e – T ⁄ τd

–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


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) Ki 1 − z −1 + M(z)

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: Ki = 1 – e – T ⁄ τd

–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


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 Ki = 1 – e – T ⁄ τd = 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, PrenticeHall, 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:
Home Page: E-mail: 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 Europe, Middle East, and Africa: Freescale Halbleiter Deutschland GmbH Technical Information Center Schatzbogen 7 81829 Muenchen, Germany +44 1296 380 456 (English) +46 8 52200080 (English) +49 89 92103 559 (German) +33 1 69 35 48 48 (French) Japan: Freescale Semiconductor Japan Ltd. Headquarters ARCO Tower 15F 1-8-1, Shimo-Meguro, Meguro-ku, Tokyo 153-0064 Japan 0120 191014 or +81 3 5437 9125 Asia/Pacific: Freescale Semiconductor Hong Kong Ltd. Technical Information Center 2 Dai King Street Tai Po Industrial Estate Tai Po, N.T., Hong Kong +800 2666 8080 For Literature Requests Only: Freescale Semiconductor Literature Distribution Center P.O. Box 5405 Denver, Colorado 80217 1-800-441-2447 or 303-675-2140 Fax: 303-675-2150

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 counterparts. For further information, see or contact your Freescale sales representative. For information on Freescale’s Environmental Products program, go to

Information in this document is provided solely to enable system and software implementers to use Freescale Semiconductor products. There are no express or implied copyright licenses granted hereunder to design or fabricate any integrated circuits or integrated circuits based on the information in this document. Freescale Semiconductor reserves the right to make changes without further notice to any products herein. Freescale Semiconductor makes no warranty, representation or guarantee regarding the suitability of its products for any particular purpose, nor does Freescale Semiconductor assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation consequential or incidental damages. “Typical” parameters that may be provided in Freescale Semiconductor data sheets and/or specifications can and do vary in different applications and actual performance may vary over time. All operating parameters, including “Typicals”, must be validated for each customer application by customer’s technical experts. Freescale Semiconductor does not convey any license under its patent rights nor the rights of others. Freescale Semiconductor products are not designed, intended, or authorized for use as components in systems intended for 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 could create a situation where personal injury or death may occur. Should Buyer purchase or use Freescale Semiconductor products for any such unintended or unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges that Freescale Semiconductor was negligent regarding the design or manufacture of the part. Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc., 2006. All rights reserved.

AN3318 Rev. 1.0 9/2006

Sign up to vote on this title
UsefulNot useful