Introduction To Microcontroller Programming For Po
Introduction To Microcontroller Programming For Po
net/publication/354078731
CITATIONS READS
0 323
4 authors, including:
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Nicola Toscani on 12 November 2021.
Mattia Rossi
Nicola Toscani
Marco Mauri
Francesco Castelli Dezza
MATLAB® is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks
does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of
MATLAB® software or related products does not constitute endorsement or sponsorship by The
MathWorks of a particular pedagogical approach or particular use of the MATLAB® software.
Reasonable efforts have been made to publish reliable data and information, but the author and
publisher cannot assume responsibility for the validity of all materials or the consequences of their use.
The authors and publishers have attempted to trace the copyright holders of all material reproduced in
this publication and apologize to copyright holders if permission to publish in this form has not been
obtained. If any copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying, microfilming, and recording, or in any information
storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, access www.copyright.com
or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923,
978-750-8400. For works that are not available on CCC please contact mpkbookspermissions@tandf.
co.uk
Trademark notice: Product or corporate names may be trademarks or registered trademarks, and are
used only for identification and explanation without intent to infringe.
Library of Congress Cataloging‑in‑Publication Data
DOI: 10.1201/9781003196938
Typeset in LM Roman
by KnowledgeWorks Global Ltd.
Foreword xiii
Preface xvii
Acknowledgments xix
Biographies xxi
I Embedded Development:
Hardware Kits and Coding 13
2 Automatic Code Generation through MATLAB® 15
2.1 Model-Based Design and Rapid Prototyping . . . . . . . . . 16
2.2 Workflow for Automatic Code Generation . . . . . . . . . . . 18
2.3 Generate Code for C2000™ Microcontrollers . . . . . . . . . 22
2.4 TI C2000™ Processors Block-set . . . . . . . . . . . . . . . . 24
vii
viii Contents
4 Software Installation 39
4.1 TI Support Packages:
Code Composer™ Studio and ControlSUITE™ . . . . . . . . 39
4.2 MATLAB® Support Package:
Embedded Coder for Texas Instruments C2000 Processors . 41
4.3 Installation Procedure . . . . . . . . . . . . . . . . . . . . . . 41
8 Basic Settings:
Serial Communication COM and Hardware Target 101
8.1 Virtual Serial Communication through COM port . . . . . . 101
Bibliography 423
Index 427
Foreword
xiii
xiv Foreword
Most people go about their day blissfully unaware of the electric motors that
are spinning the world around us. We wake up staring upwards at a ceiling
fan, silently rotating in a circle. We jump into our car and rely on up to
40 motors—pumps, fans, locks, and lifts—to get us to our destination. We
power up our laptop computers and hear the soft whine of fans working to
keep the electronics cool. Motors are everywhere because they are one of the
main ways that an electronic circuit can interact with the real world, i.e. a
power electronic-based system. They are “lectromechanical, turning analog
and digital signals into real and visible mechanical motion. It is estimated
that electric motors consume 45 percent of the total worldwide electricity—
this is a stunning statistic! As we look to reducing energy consumption and
enabling a greener future, electric motors present a huge potential for efficiency
improvements.
Few engineering students are aware of the impact of electric motors on
the world around them, and even less are versed in the design and control of
motor systems. This is a problem! We need engineers growing in competency
in this field to create better and more efficient motor drive systems.
Motor drive and control is an incredibly multidisciplinary field. Real-time
digital processing is implemented in microcontrollers to be the “brain” behind
the motor system; controlling speed, power, and efficiency from the digital
domain. A wealth of analog components from power management (voltage
regulators & gate drivers) and signal chain (amplifiers & sensors) interface
the microcontroller to the motor through a power converter while providing
sensing, safe operation, and support for the system. Texas Instruments has
over 25 years of experience in the field of real-time control and also provides
a comprehensive analog portfolio covering every block of the motor drive and
control system.
This book presents very practical and important lessons to engineers and
engineering students alike on the topics of motor drive and control, covering
not only general concepts but details on how to create a motor drive system.
It provides an excellent resource to encourage the next generation of engineers
to grow and develop skills in the area of electric motors and power electronics,
introducing them the tools they need to make an impact on the world.
Politecnico di Milano is an outstanding academic partner, and the focus
of the Electrical Machines, Drives and Power Electronics Research Group on
cutting-edge power electronic-based technologies helps shape quality engineer-
ing minds. We wish the best of success to this publication and to the continued
collaboration between industry and academia.
Can you write the 100 million lines of code that are needed to build an average
modern car? The answer is pretty obvious: of course you can, it’s just a matter
of time. And how would you compare the complexity of this problem to writing
the 4501 lines of assembly code needed to build the first version of UNIX in
1971? While both tasks appear to be at a similar level of dauntlessness, the half
century separating them has witnessed the emergence of high-level languages
that enable programmers to address highly complex problems on their own
while reusing the legacy of their peers.
At MathWorks Inc., we relentlessly work on providing the best high-level
programming tools to automate the implementation of your ideas into embed-
ded systems. Simulink allows you to design and simulate complex algorithms
that you can translate into thousands of lines of embedded code with a click
of a button via our code generation technology.
The book Introduction to Microcontroller Programming for Power Elec-
tronics Control Applications will teach you how to use these modern techniques
to create control algorithms for systems involving complex physics. The re-
markable work of Mattia Rossi, Nicola Toscani, Marco Mauri and Francesco
Castelli Dezza from Politecnico di Milano, Italy, clearly explains deep concepts
to the reader in the field of embedded programming for power electronics ap-
plications using Model-Based Design.
The copyrighted material included in this book is reprinted with permission of The
MathWorks, Inc.
xvi Foreword
While the shift to digital is now largely dominating the industry of mo-
tor control, this revolution is just starting for power conversion applications.
The material in this book provides state of art techniques to train the many
engineers that the world needs tomorrow in a field that is at the core of the
indispensable transition to clean energy.
In recent conversations with Mattia and Nicola, while they politely thanked
us for our help, it was clear to us that the quality and the amount of effort
in this book deserved much more thanking from our side. With this foreword,
we extend all our gratitude to this outstanding contribution to accelerating
the pace of engineering and science, our core mission.
Power electronics-based systems are the key enabling technology to meet most
of the future sustainable challenges from grid to motor applications.
Standard textbooks and courses about power electronics and electrical ma-
chines deal with analysis in continuous-time, averaged modeling of switched-
mode power converters, and continuous-time control theory. Nevertheless, real
control algorithms and management functions around power converters are
implemented digitally, thus, extending the field of fundamentals studies to
discrete-time modeling and digital control concepts specific to power electron-
ics. The necessary background is achieved by combining specific textbooks
and courses from both power electronics and digital control theory. However,
students who approach the design of digitally controlled power converters for
the first time may not fully understand and successfully practice for a targeted
problem due to such fragmented references.
In this book, we attempt to fill this gap by treating the fundamental as-
pects of digital control implementation for power electronics based systems
in a systematic and rigorous manner. Our objectives are to put the reader
in the position to understand, analyze, model, design, and implement digital
feedback loops around power converters, from system-level transfer function
formulations to understand which coding tool may be used when working
with microcontroller (MCU or µC) platforms. In particular, the latter be-
longs to Texas Instruments™ C2000™ family, which is specifically designed
for real-time closed loop control such as power supplies, industrial drives,
and solar inverters applications. The Simulink® environment is able to au-
tomatically generate ANSI/ISO C/C++ code tailored for specific embedded
targets through a model-based workflow. Given the settings which enable a
background usage of the Code Composer™ Studio IDE, a Simulink® scheme
can be directly compiled and executed on C2000™ MCUs. Such automated
build and execution procedure speed up the control algorithms implementa-
tion, thus, the code generation of software interfaces and MCU peripherals
(e.g. ADC, digital I/O, PWM), which can be tested with execution profiling.
This makes the reader working in a rapid prototyping manner.
This book is oriented to graduate students of electrical and automation
and control engineering pursuing a curriculum in power electronics and drives.
Moreover, it aims to be a reference for engineers and researchers who seek to
expand on the expertize in design-oriented knowledge for the aforementioned
applications. It is assumed that the reader is well acquainted with funda-
mentals of electrical machines and power converters, along with associated
xvii
xviii Preface
Most of the projects shown in this book have been funded by the Department
of Mechanical Engineering of Politecnico di Milano, Italy, with the particular
contribution of its Laboratory of Electrical Drives and Power Electronics.
Special thanks goes to the university rector Prof. Ferrucio Resta and the
department director Prof. Marco Bocciolone for their support to this initiative.
We are grateful to our graduate students Marco Gerosa, Matteo Scandella,
Andrea Polastri, Matteo Sposito, and Luca Grittini for the precious work
in supporting the hardware development, the boards testing and the many
suggestions they made. We also thank all the Ph.D. students and researchers
who contributed to this book. In particular, Dr. Khaled ElShawarby and Dr.
Alberto Bolzoni, who were supporting the project from day zero.
The authors would like to specially thank Prof. Petros Karamanakos from
Tampere University, Finland, and Prof. Ralph Kennel from Technical Univer-
sity Munich, Germany, for their guidance, long discussions and availability to
share their high expertize in this field.
We are grateful to Nora Konopka and CRC Press LLC from Taylor and Fran-
cis Group for publishing this book. Special thanks goes to Prachi Mishra for
her guidance and support.
xix
Biographies
xxi
xxii Biographies
DOI: 10.1201/9781003196938-1 1
2 Advances in Firmware Design for Power Electronics Control Platforms
CPU
A/D
CONVERTER RAM
PROGRAM
CLOCK
MEMORY
MCU
1 From the customer’s point of view the “best” microcontroller is the one which matches
Since this book focuses on electrical power conversion case studies, from
now on the text refers to MCU platforms targeted to power electronics-based
applications. Even if this last statement reduces the candidate list, there still
are several suitable solutions available on the market which share a common
goal of being compact and versatile. Since the definition of a criterion to cat-
egorize each board would not be practical (i.e., there might be even deep
technology differences), it is recommended to follow a system-level approach
like the one presented here:
1. Consider all the components and peripherals that are necessary to
run all of the required features. If the board lacks any of them, it is
important to identify some supported expansions to include them.
2. Consider the supported programming languages and the level of
competence of the final user. Moreover, investigate if any automatic
code generation procedure is available as well as the quality of the
documentation at disposal for the adopted board. Indeed, commu-
nity and support are factors of great importance since they are the
primary resources when designing a project.
3. Evaluate costs versus adopted components for the considered spe-
cific application: is it worth paying for them?
• CPU and clock speed: these two values affect the overall performance of
the board. Namely, how fast it can perform computations. It should be noted
that clock speed comparisons between CPUs coming from different families
may not lead to meaningful considerations. Other factors, such as instruction
cycles, instruction sets, and pipeline depth, also affect MCU performances.
• RAM: the size of this memory affects the number of tasks that can be run
simultaneously. It also impacts how fast data can be processed, as swapping
it from RAM to nonvolatile storage incurs large performance overheads.
• Graphical processing unit (GPU): it allows development board to run
video output (e.g., VGA/HDMI). High-performance GPUs are needed while
processing video/images with the development board.
• Data memory: it affects the size of programs, operating systems, and gen-
erated/downloaded data that can be stored on development boards.
• General purpose input/output pins: these pins are used to connect
external components to the development board in use. Hence, more pins
typically means more possible simultaneous connections. These pins are
usually assigned specific functionalities by the manufacturer being compli-
ant with some standards, such as integrated circuit (I2C), serial peripheral
Selecting a Development Board 5
• Translation
Programs written in Binary code does not need any translation as this lan-
guage is a machine code already. Namely, the hardware is capable of un-
derstanding them without any translation. Instead, Assembly codes need
an Assembler to translate programs to their equivalent counterpart in Ma-
chine Code. High-level languages are always translated by compilers or in-
terpreters. Some of them required both compilers and interpreters to get the
Object/Binary file.
• Support
Low-level languages have less support than high-level ones. There may be
lower number of communities for low-level languages than for high-level ones.
applications in the fields of motor control, digital power supplies, lighting, re-
newable energy, and smart grids. This family is made up of several subfamilies,
from which it is worth mentioning:
In particular, the C28xx chips are from low to high performance MCU.
Piccolo™ (which main features and applications are summarized in Figure 1.3)
and Delfino™ are the families for low and high performance microcontrollers,
respectively. Their main characteristics are reported here in the following:
1. Piccolo™
•MCU with floating-point unit;
•CPU frequency: from 40 to 120 MHz;
•Core: 1xC28x;
•Memory:
from 60 kbit up to 512 kbit flash;
from 12 kbit up to 100 kbit SRAM;
•Main peripherals: ADC, PWM, QEP, DMA, SPI, UART, I2C,
CAN, USB.
2. Delfino™
•MCU with floating-point unit;
•CPU frequency: from 100 to 200 MHz;
•Core: from 1xC28x up to 2xC28x + 2xCLA + ARM Cortex-
M4;
•Memory:
from 512 kB up to 1.5 MB flash;
from 68 kB up to 338 kB SRAM;
•Main peripherals: ADC, PWM, QEP, DMA, SPI, UART, I2C,
CAN, EMIF.
In particular, the following families of C28xx MCUs (which are also available
as LaunchPad™ development kits) are supported with a dedicated library
available in Matlab® Simulink® : F2802x Piccolo™ , F2803x Piccolo™ , F2805x
Piccolo™ , F2807x Piccolo™ , F2806x Piccolo™ , F2837xS Delfino™ , F2837xD
Delfino™ , F28004x, F2823x Delfino™ , F28M3x.
From Chapter 2 on, this book refers to a specific model of development board,
that is Texas Instruments™ LaunchXL F28069M Piccolo™ , which is shown in
Scheme of a Power Electronics Control Problem 9
Figure 1.4. The reason behind this choice is its low-cost, low-power, and simple
development environment of the board. Moreover, it has both a 256 kbit flash
memory and a 96 kbit RAM. Finally, the community and project support for
this device is relatively sparse compared to other boards. TI offers several plug-
in expansion boards to expand the capabilities of the F28069M LaunchPad™
Piccolo™ .
• Reference: the controller must act to let the system follow a reference
signal. The latter can be both internally generated on the MCU or provided
as an external signal.
• Processing: this relates to the control logic implemented on the MCU.
The control structure is the main part of the scheme, since it is aimed at
generating the desired output based on the error between the reference and
the feedback signals. This stage may also comprise scaling routines.
Note that, the C2000™ MCU family includes several parts of this struc-
ture representing a complex ecosystem. Its main features are summarized in
Figure 1.6.
Figure 1.7 Traction converter and motor control for an efficient conversion DC
to AC to drive an electric motor [9].
Embedded Development:
Hardware Kits and Coding
References
IEEE Standard for Binary Floating-Point Arithmetic. ANSI/IEEE Std 754-1985, pages 1–20,
1985.
Paolo Bolzern, Riccardo Scattolini, and Nicola Schiavoni. Fondamenti di controlli automatici.
McGraw-Hill Education, 4th edition, February 2015.
Bimal K. Bose. Modern Power Electronics and AC Drives. Pearson Education, 2001.
Jason Bridgmon and Carolus Andrews. Current Sensing for Inline Motor-Control Applications.
Technical Report SBOA172, Texas Instruments, October 2016. Available at
https://www.ti.com/lit/an/sboa172/sboa172.pdf.
Bill Chou. The Joy of Generating C Code from MATLAB.
https://it.mathworks.com/company/newsletters/articles/the-joy-of-generating-c-code-from-
matlab.html.
Luca Corradini, Dragan Maksimović, Paolo Mattavelli, and Regan Zane. Digital Control of High-
Frequency Switched-Mode Power Converters. John Wiley & Sons, 22 June 2015.
Texas Instruments. BoosterPack Checker. https://dev.ti.com/bpchecker/#/.
Texas Instruments. C2000 Real-Time Control MCUs: Applications.
https://www.ti.com/microcontrollers/c2000-real-time-control-mcus/applications.
Texas Instruments.Inverter & motor control. https://www.ti.com/solution/hev-ev-inverter-motor-
control?variantid=14291subsystemid=17097.
Texas Instruments. Website. https://www.ti.com.
Texas Instruments. Meet the TMS320F28069M - LaunchPad Development Kit. Technical
Report SPRUI02, Texas Instruments, 2014. Available at
https://www.ti.com/lit/ml/sprui02/sprui02.pdf.
Texas Instruments. Lift-Off with the TI LaunchPad Development Ecosystem. Technical Report
SLAT152B, Texas Instruments, 2015.
Texas Instruments. Motor Drive and Control Solutions. Technical Report SLYB165I, Texas
Instruments, 2017.
Texas Instruments. DRV8301 Three-Phase Gate Driver With Dual Current Shunt Amplifiers and
Buck Regulator. Technical Report SLOS719F, Texas Instruments, August 2011 (revised
January 2016). Available at https://www.ti.com/lit/ds/symlink/drv8301.pdf.
Texas Instruments. C2000 Key Technology Guide. Technical Report SPRACN0A, Texas
Instruments, August 2019 (revised March 2020). Available at
https://www.ti.com/lit/an/spracn0a/spracn0a.pdf.
Texas Instruments. User's Guide - LAUNCHXL-F28069M Overview (Rev. B). Technical Report
SPRUI11B, Texas Instruments, January 2015 (revised March 2019). Available at
https://www.ti.com/lit/ug/sprui11b/sprui11b.pdf.
Texas Instruments. TMS320x280x, 2801x, 2804x Serial Communications Interface (SCI).
Technical Report SPRUFK7B, Texas Instruments, March 2009 (revised April 2011). Available at
https://www.ti.com/lit/ug/sprufk7b/sprufk7b.pdf.
Texas Instruments. C2000 MCU JTAG Connectivity Debug. Technical Report SPRACF0, Texas
Instruments, May 2018. Available at https://www.ti.com/lit/an/spracf0/spracf0.pdf.
Texas Instruments. TMS320x280x, 2801x, 2804x DSP Analog-to-Digital Converter (ADC).
Technical Report SPRU716D, Texas Instruments, November 2004 (revised April 2010).
Available at https://www.ti.com/lit/ug/spru716d/spru716d.pdf.
Texas Instruments. TMS320x280x, 2801x, 2804x Enhanced Quadrature Encoder Pulse (eQEP)
Module. Technical Report SPRU790D, Texas Instruments, November 2004 (revised December
2008). Available at https://www.ti.com/lit/ug/spru790d/spru790d.pdf.
Texas Instruments. TMS320x280x, 2801x, 2804x Enhanced Pulse Width Modulator (ePWM)
Module. Technical Report SPRU791F, Texas Instruments, November 2004 (revised July 2009).
Available at https://www.ti.com/lit/ug/spru791f/spru791f.pdf.
Texas Instruments. User's Guide - BOOSTXL-DRV8301 Hardware User's Guide. Technical
Report SLVU974, Texas Instruments, October 2013. Available at
http://www.ti.com/lit/ug/slvu974/slvu974.pdf.
Paul Krause, Oleg Wasynczuk, Scott Sudhoff, and Steven Pekarek. Analysis of Electric
Machinery and Drive Systems. John Wiley & Sons, 3rd edition, 2013.
Werner Leonhard. Control of Electrical Drives. Springer, 3rd edition, 2001.
Renato Manigrasso, Ferdinando L. Mapelli, and Marco Mauri. Azionamenti elettrici vol.1:
Generalità e Macchine Rotanti. Pitagora, 2007.
Renato Manigrasso, Ferdinando L. Mapelli, and Marco Mauri. Azionamenti elettrici vol.2:
Convertitori e Controllo. Pitagora, 2007.
Ferdinando L. Mapelli, Davide Tarsitano, and Marco Mauri. Plug-In Hybrid Electric Vehicle:
Modeling, Prototype Realization, and Inverter Losses Reduction Analysis. IEEE Transactions on
Industrial Electronics, 57(2):598–607, 2010.
MathWorks. Getting Started—Embedded Coder Support Package for Texas Instruments C2000
Processors. Video available at https://www.mathworks.com/videos/getting-started-with-
embedded-coder-support-package-for-ti-c2000-processors-1573540550102.html.
MathWorks. Pid tuner documentation. https://it.mathworks.com/help/control/ref/pidtuner.html.
MathWorks. Specialized power system documentation.
https://it.mathworks.com/help/physmod/sps/specialized-power-systems.html.
MathWorks. TI C2000 Support from Embedded Coder. https://it.mathworks.com/hardware-
support/ti-c2000.html.
MathWorks. White Paper - Hardware-in-the-Loop Testing for Power Electronics Control Design
- Featuring Simulink and Speedgoat real-time target machines. Technical report, MathWorks,
2019. Available at https://it.mathworks.com/campaigns/offers/power-electronics-control-hil-
white-paper.html.
Mathworks. Simulink for Developing Digital Control for Motors, Power Converters, and Battery
Systems. Technical report, MathWorks, 2020. Available at
https://www.mathworks.com/content/dam/mathworks/brochure/speed-digital-control-motor-
power-converters-systems-simulink-handout.pdf.
MathWorks. White Paper - Model-Based Design for Embedded Control Systems. Technical
report, MathWorks, October 2020. Available at
https://it.mathworks.com/campaigns/offers/model-based-design-embedded-control-
systems.confirmation.html?elqsid=1607505922909potential_use=Education.
Marco Mauri, Maria Stefania Carmeli, and Mattia Rossi. Macchine Elettriche. Modelli a regime:
teoria ed esercizi. Società Editrice Esculapio, 2nd edition, 2020.
Robert D. Middlebrook and Slobodan Ćuk. A general unified approach to modelling switching-
converter power stages. In 1976 IEEE Power Electronics Specialists Conference, pages 18–34,
1976.
Ned Mohan, Tore M. Undeland, and William P. Robbins. Power Electronics: Converters,
Applications, and Design. John Wiley & Sons, 3rd edition, 2002.
Mattia Rossi, Francesco Castelli Dezza, Marco Mauri, and Maria Prandini. Explicit Computation
of Indirect Hybrid MPC for Voltage Control in Multilevel DC-DC Converters. In 2018 20th
European Conference on Power Electronics and Applications (EPE’18 ECCE Europe), pages
P.1–P.9, 2018.
Mattia Rossi, Eyke Liegmann, Petros Karamanakos, Francesco Castelli Dezza, and Ralph
Kennel. Direct Model Predictive Power Control of a Series-Connected Modular Rectifier. In
2019IEEE International Symposium on Predictive Control of Electrical Drives and Power
Electronics (PRECEDE), pages 1–6, 2019.
Mattia Rossi, Luigi Piegari, Francesco Castelli Dezza, Marco Mauri, and Maria S. Carmeli.
Voltage Control Comparison for Low-Power DC-DC Converters in EVs: PI and Explicit MPC. In
IECON 2018 - 44th Annual Conference of the IEEE Industrial Electronics Society, pages
5005–5011, 2018.
Sigurd Skogestad and Ian Postlethwaite. Multivariate Feedback Control: Analysis and Design.
John Wiley & Sons, 2nd edition, November 2005.
Stephen Umans. Fitzgerald & Kingsley's Electric Machinery. McGraw-Hill Education, 7th edition,
16 May 2013.
Peter Vas. Sensorless Vector and Direct Torque Control. Oxford University Press, 1998.
J. G. Ziegler and N.B. Nichols. Process lags in automatic control circuits. Transactions of the
A.S.M.E., 65(5):433–444, 1943.