MSP430 Microcontroller Basics

Transcription

MSP430 Microcontroller Basics

This page intentionally left blank

MSP430 Microcontroller BasicsJohn H. DaviesAMSTERDAM BOSTON HEIDELBERG LONDONNEW YORK OXFORD PARIS SAN DIEGOSAN FRANCISCO SINGAPORE SYDNEY TOKYONewnes is an imprint of Elsevier

Newnes is an imprint of Elsevier30 Corporate Drive, Suite 400, Burlington, MA 01803, USALinacre House, Jordan Hill, Oxford OX2 8DP, UKCopyright 2008, Elsevier Ltd. All rights reserved.No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any formor by any means, electronic, mechanical, photocopying, recording, or otherwise, without the priorwritten permission of the publisher.Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford,UK: phone: ( 44) 1865 843830, fax: ( 44) 1865 853333, E-mail: permissions@elsevier.com. You mayalso complete your request online via the Elsevier homepage (http://www.elsevier.com) by selecting“Support & Contact” then “Copyright and Permission” and then “Obtaining Permissions.”Recognizing the importance of preserving what has been written, Elsevier prints itsbooks on acid-free paper whenever possible.Library of Congress Cataloging-in-Publication DataApplication submittedBritish Library Cataloguing-in-Publication DataA catalogue record for this book is available from the British Library.ISBN: 978-0-7506-8276-3For information on all Newnes publications,visit our Web site at: http://www.books.elsevier.com08 09 10 11 12 1310 9 8 7 6 5 4 3 2 1Printed in the United States of America

“To Elizabeth.”

This page intentionally left blank

ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiChapter 1: Embedded Electronic Systems and Microcontrollers . . . . . . . . . . . . . . . . . . . 11.11.21.31.41.51.61.7What (and Where) Are Embedded Systems? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Approaches to Embedded Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Small Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Anatomy of a Typical Small Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Where Does the MSP430 Fit? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Chapter 2: The Texas Instruments MSP430 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.12.22.32.42.52.62.72.8The Outside View—Pin-Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21The Inside View—Functional Block Diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Central Processing Unit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Memory-Mapped Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Clock Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Exceptions: Interrupts and Resets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Where to Find Further Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Chapter 3: Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.13.23.33.43.53.63.7Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44The C Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Assembly Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Access to the Microcontroller for Programming and Debugging . . . . . . . . . . . . . . 57Demonstration Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Equipment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65www.newnespress.com

viiiContentsChapter 4: A Simple Tour of the MSP430 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674.14.24.34.44.54.64.74.8First Program on a Conventional Desktop Computer . . . . . . . . . . . . . . . . . . . . . . . . . . 68Light LEDs in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Light LEDs in Assembly Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Read Input from a Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Automatic Control: Flashing Light by Software Delay . . . . . . . . . . . . . . . . . . . . . . . . 91Automatic Control: Use of Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Automatic Control: Flashing Light by Polling Timer A . . . . . . . . . . . . . . . . . . . . . . 105Header Files and Issues Brushed under the Carpet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Chapter 5: Architecture of the MSP430 Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1195.15.25.35.45.55.65.75.8Central Processing Unit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Constant Generator and Emulated Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146Reflections on the CPU and Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Resets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157Clock System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Chapter 6: Functions, Interrupts, and Low-Power Modes . . . . . . . . . . . . . . . . . . . . . . .1776.16.26.36.46.56.66.76.86.96.10Functions and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178What Happens when a Subroutine Is Called? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Storage for Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Passing Parameters to a Subroutine and Returning a Result . . . . . . . . . . . . . . . . . . 183Mixing C and Assembly Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186What Happens when an Interrupt Is Requested? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188Interrupt Service Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Issues Associated with Interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Low-Power Modes of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Chapter 7: Digital Input, Output, and Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2077.17.27.37.47.57.67.77.87.9Digital Input and Output: Parallel Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Digital Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216Switch Debounce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225Digital Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238Interface between 3V and 5V Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243Driving Heavier Loads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247Liquid Crystal Displays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252Driving an LCD from an MSP430x4xx. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256Simple Applications of the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264www.newnespress.com

ContentsixChapter 8: Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2758.18.28.38.48.58.68.78.88.98.108.11Watchdog Timer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276Basic Timer1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281Timer A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287Measurement in the Capture Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Output in the Continuous Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Output in the Up Mode: Edge-Aligned Pulse-Width Modulation . . . . . . . . . . . . 330Output in the Up/Down Mode: Centered Pulse-Width Modulation . . . . . . . . . . 349Operation of Timer A in the Sampling Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352Timer B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353What Timer Where?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356Setting the Real-Time Clock: State Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Chapter 9: Mixed-Signal Systems: Analog Input and Output . . . . . . . . . . . . . . . . . . . tor A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371Analog-to-Digital Conversion: General Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Analog-to-Digital Conversion: Successive Approximation . . . . . . . . . . . . . . . . . . . 402The ADC10 Successive-Approximation ADC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407Basic Operation of the ADC10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412More Advanced Operation of the ADC10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424The ADC12 Successive-Approximation ADC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432Analog-to-Digital Conversion: Sigma–Delta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438The SD16 A Sigma–Delta ADC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446Operation of SD16 A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459Signal Conditioning and Operational Amplifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475Digital-to-Analog Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485Chapter 10: Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.1210.13Communication Peripherals in the MSP430 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495Serial Peripheral Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497SPI with the USI . . . . .

the MSP430 has a uniform, 16-bit architecture throughout: The address bus, data bus, and registers in the CPU are all 16 bits wide. The CPU has a modern design with plenty of registers, most of which can be used equally for data or addresses. It has a small instruction set with orthogonal addressing and an ingenious constant generator, which is .