Table Of ContentProgrammable Microcontrollers with
Applications
Programmable Microcontrollers with
Applications
MSP430 LaunchPad with CCS and Grace
Cem Ünsalan
H. Deniz Gürhan
New York Chicago San Francisco
Athens London Madrid Mexico City Milan
New Delhi Singapore Sydney Toronto
Copyright © 2014 by McGraw-Hill Education. All rights reserved. Except as permitted under the United States
Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or
stored in a database or retrieval system, without the prior written permission of the publisher.
ISBN: 978-0-07-183004-1
MHID: 0-07-183004-9
®
e-Book conversion by Cenveo Publisher Services
Version 1.0
The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-183003-4, MHID: 0-07-
183003-0.
McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions, or
for use in corporate training programs. To contact a representative, please visit the Contact Us page at
www.mhprofessional.com.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of
a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no
intention of infringement of the trademark. Where such designations appear in this book, they have been printed with
initial caps.
Information has been obtained by McGraw-Hill Education from sources believed to be reliable. However, because of the
possibility of human or mechanical error by our sources, McGraw-Hill Education, or others, McGraw-Hill Education
does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or
omissions or the results obtained from the use of such information.
TERMS OF USE
This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this
work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve
one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works
based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill
Education’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the
work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO
GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR
RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE
ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY
WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill Education and its licensors do
not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will
be uninterrupted or error free. Neither McGraw-Hill Education nor its licensors shall be liable to you or anyone else for
any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill
Education has no responsibility for the content of any information accessed through the work. Under no circumstances
shall McGraw-Hill Education and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or
similar damages that result from the use of or inability to use the work, even if any of them has been advised of the
possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim
or cause arises in contract, tort or otherwise.
About the Authors
Cem Ünsalan, Ph.D., has worked in signal and image processing for 15 years. After
getting a Ph.D. from The Ohio State University in 2003, he started working at Yeditepe
University, Istanbul, Turkey, where he established the DSP Laboratory and has been
teaching microprocessor and digital signal processing courses for 7 years. Dr. Ünsalan has
published 16 journal articles and 4 international books. He holds one patent.
H. Deniz Gürhan received his B.Sc. from Yeditepe University. He is pursuing a Ph.D. in
digital signal processing and embedded systems at the same university and works in the
DSP Laboratory.
Contents
Preface
Acknowledgments
1 Introduction
1.1 The TI MSP430 LaunchPad
1.2 Topics to Be Covered in This Book
2 Review of Digital Circuits
2.1 Transistor as a Switch
2.2 Logic Gates from Transistors
2.3 Combinational Circuits from Gates
2.4 Sequential Circuits from Gates
2.5 Summary
2.6 Problems
3 Data Types
3.1 Number Representations
3.2 Negative Numbers
3.3 Fixed- and Floating-Point Representations
3.4 The Word Size and Overflow
3.5 Little and Big Endian Representations
3.6 ASCII Characters
3.7 Summary
3.8 Problems
4 MSP430 Architecture
4.1 General Layout
4.2 Central Processing Unit
4.3 Memory
4.4 Input and Output Ports
4.5 Clocks, the Timer, and Watchdog Timer Modules
4.6 ADC and Comparator Modules
4.7 The Digital Communication Module
4.8 Other Modules
4.9 The Pin Layout of the MSP430G2553
4.10 Summary
4.11 Problems
5 Code Composer Studio
5.1 Setup
5.2 Creating a C Project
5.3 Creating an Assembly Project
5.4 Program Execution
5.5 Observing Hardware under CCS
5.6 Terminating the Debug Session and Closing the Project
5.7 Graphical Peripheral Configuration Tool (Grace)
5.8 The Terminal Window
5.9 Summary
5.10 Problems
6 MSP430 Programming with C
6.1 Memory Management
6.2 C Data Types
6.3 Arithmetic and Logic Operations
6.4 Control Structures
6.5 Arrays and Pointers
6.6 Miscellaneous Issues
6.7 Summary
6.8 Problems
7 MSP430 Instruction Set
7.1 Introduction
7.2 Anatomy of an Instruction
7.3 MSP430 Addressing Modes
7.4 The Stack
7.5 Assembly Program Structure
7.6 Sample Programs on Instruction Set Usage
7.7 Summary
7.8 Problems
8 Digital Input and Output
8.1 Pin Layout for Digital I/O
8.2 Digital I/O Registers
8.3 Digital I/O Hardware Issues
8.4 Coding Practices for Digital I/O
8.5 Digital I/O in Grace
8.6 Digital Safe Application
8.7 Summary
8.8 Problems
9 Interrupts
9.1 What Happens When an Interrupt Occurs?
9.2 Types of Interrupts
9.3 Interrupt Flags
9.4 Interrupt Vectors
9.5 Interrupt Service Routines
9.6 Port Interrupts
9.7 Coding Practices for Interrupts
9.8 Interrupts in Grace
9.9 Washing Machine Application
9.10 Summary
9.11 Problems
10 Oscillators, Clocks, and Timers
10.1 Oscillators
10.2 Clocks
10.3 BCM+ in Grace
10.4 Low-Power Modes
10.5 The Watchdog Timer
10.6 WDT+ in Grace
10.7 Timers
10.8 The Pin Layout for the BCM+ and Timer_A Modules
10.9 Timer_A in Grace
10.10 Chronometer Application
10.11 Summary
10.12 Problems
11 Mixed Signal Systems
11.1 Analog and Digital Signals
11.2 The Comparator
11.3 Comparator A+ in Grace
11.4 Analog-to-Digital Conversion
11.5 Digital-to-Analog Conversion
11.6 ADC10 in Grace
11.7 Non-Touch Paper Towel Dispenser Application
11.8 Summary
11.9 Problems
12 Digital Communication
12.1 Universal Serial Communication Interface
12.2 Universal Asynchronous Receiver/Transmitter
12.3 UART in Grace
12.4 Serial Peripheral Interface
12.5 SPI in Grace
12.6 Inter Integrated Circuit
12.7 I2C in Grace
12.8 Digital Communication Application
12.9 Summary
12.10 Problems
13 Flash Memory
13.1 MSP430 Flash Memory
13.2 Flash Memory Programming
13.3 Coding Practices for Flash Memory
13.4 Flash Memory in Grace
13.5 Summary
13.6 Problems
14 Applications
14.1 Car Door Alarm
14.2 Car Window Control
14.3 Car Park Tollgate
14.4 Digital Lock System
14.5 Air Freshener Dispenser
14.6 Traffic Lights
14.7 Sound Detector
14.8 Obstacle-Avoiding Tank
14.9 Car Parking Sensor System
14.10 Fire Alarm
14.11 Wave Generator
14.12 Sports Watch
15 Appendix
15.1 MSP430 Intrinsic Functions
15.2 MSP430G2553 Header File
References
Index