Texas Instruments MSP430 Microcontrollers
Total Page:16
File Type:pdf, Size:1020Kb
Modular Electronics Learning (ModEL) project * SPICE ckt v1 1 0 dc 12 v2 2 1 dc 15 r1 2 3 4700 r2 3 0 7100 .dc v1 12 12 1 .print dc v(2,3) .print dc i(v2) .end V = I R Texas Instruments MSP430 Microcontrollers c 2020-2021 by Tony R. Kuphaldt – under the terms and conditions of the Creative Commons Attribution 4.0 International Public License Last update = 7 September 2021 This is a copyrighted work, but licensed under the Creative Commons Attribution 4.0 International Public License. A copy of this license is found in the last Appendix of this document. Alternatively, you may visit http://creativecommons.org/licenses/by/4.0/ or send a letter to Creative Commons: 171 Second Street, Suite 300, San Francisco, California, 94105, USA. The terms and conditions of this license allow for free copying, distribution, and/or modification of all licensed works by the general public. ii Contents 1 Introduction 3 2 Case Tutorial 5 2.1 Assembly example: adding two numbers ......................... 6 2.2 C example: adding two numbers ............................. 9 2.3 Sketch example: adding two numbers ........................... 11 2.4 Assembly example: subtracting two numbers ...................... 13 2.5 Assembly example: bitwise operations .......................... 15 2.6 C example: bitwise operations ............................... 17 2.7 Assembly example: rotate right instruction ....................... 19 2.8 Assembly example: alternating LED blink ........................ 21 2.9 C example: alternating LED blink ............................ 24 2.10 Sketch example: alternating LED blink ......................... 27 2.11 Assembly example: blink all Port 1 lines ......................... 29 2.12 Assembly example: pushbutton control of LED ..................... 32 2.13 C example: start-stop control ............................... 34 2.14 Assembly example: pushing and popping the stack ................... 38 2.15 Assembly example: driving Port 2 lines with Timer ................... 41 2.16 Assembly example: interrupt triggered by pushbutton ................. 44 2.17 C example: interrupt triggered by pushbutton ...................... 48 2.18 C example: pushbutton-triggered timer ......................... 50 2.19 C example: Boolean SOP expression ........................... 54 2.20 C example: sine wave signal generator .......................... 57 2.21 C example: pulse-width modulation output ....................... 60 2.22 C example: crude analog input .............................. 64 2.23 C example: analog-controlled LEDs ............................ 67 2.24 C example: UART serial text transmission ........................ 71 2.25 C example: UART serial text and number transmission ................ 75 2.26 C example: UART transmission of analog data ..................... 80 2.27 C example: simple datalogger ............................... 89 2.28 Example: interposing MCU to a heavy load ....................... 96 2.29 C example: sine-wave inverter ............................... 103 iii iv CONTENTS 3 Tutorial 107 3.1 Microcontrollers versus microprocessors ......................... 107 3.2 MSP430G2553 pin functions ................................ 111 3.3 Elementary output and input ............................... 113 3.4 Bit-level output instructions ................................ 119 3.5 MSP430G2553 architecture ................................ 122 3.5.1 Registers ...................................... 122 3.5.2 Memory map .................................... 122 3.5.3 I/O ports ...................................... 123 3.5.4 Interrupts ...................................... 126 3.6 MSP430G2553 auxiliary functions ............................ 131 3.6.1 Clocks ........................................ 132 3.6.2 General timers ................................... 133 3.6.3 Watchdog timer .................................. 133 3.6.4 Analog-Digital conversion ............................. 133 3.6.5 Analog comparators ................................ 133 3.6.6 Serial data interfaces ................................ 133 3.7 CCS assembly-language programming .......................... 133 3.8 CCS C-language programming .............................. 136 3.9 Energia programming ................................... 141 3.10 Debugging tools ....................................... 144 4 Derivations and Technical References 161 4.1 Introduction to assembly language programming .................... 162 4.1.1 Machine code to blink an LED .......................... 163 4.1.2 Assembly code to blink an LED .......................... 165 4.1.3 Slowing down the blinking ............................. 167 4.1.4 Simplifying with symbols ............................. 170 4.1.5 Using the stack ................................... 171 5 Questions 175 5.1 Conceptual reasoning .................................... 179 5.1.1 Reading outline and reflections .......................... 180 5.1.2 Foundational concepts ............................... 181 5.1.3 Terminal assignments and limits ......................... 182 5.1.4 Interrupt capabilities ................................ 183 5.1.5 Disabling the watchdog timer ........................... 184 5.2 Quantitative reasoning ................................... 185 5.2.1 Miscellaneous physical constants ......................... 186 5.2.2 Introduction to spreadsheets ........................... 187 5.2.3 Integer conversion table .............................. 190 5.2.4 Setting up Port 1 I/O ............................... 190 5.2.5 Setting up Timer A ................................ 191 5.2.6 Selecting sub-main clock source .......................... 191 5.3 Diagnostic reasoning .................................... 192 5.3.1 Poor interface design ................................ 193 CONTENTS 1 5.3.2 Improving a debug session ............................. 194 5.3.3 Incorrect sum .................................... 195 A Problem-Solving Strategies 197 B Instructional philosophy 199 C Tools used 205 D Creative Commons License 209 E References 217 F Version history 219 Index 221 2 CONTENTS Chapter 1 Introduction The MSP430 series of microcontrollers are an excellent platform for learning microcontroller technology, featuring a clean design and a multiple development tools. This module will discuss some of the basic features of this microcontroller product line with an emphasis on programming. In no way should the Tutorial contained in this module be considered a substitute for the volumes of technical literature published by Texas Instruments on their product, but rather more of a “Getting Started” or “Quick Reference” guide for the student new to the MSP430 and to microcontrollers in general. Important concepts related to microcontrollers in general and to the MSP430 series of microcontrollers in particular include busses, ports, development software, registers, memory maps, binary and hexadecimal numeration, masks, interrupts, oscillators, clocks, timers, analog-digital conversion, comparators, and serial data communication. Here are some good questions to ask of yourself while studying this subject: What purposes do microprocessors serve best, versus microcontrollers? • How does a program get written to the internal memory of a microcontroller? • What does the program counter do in a microprocessor or microcontroller? • What does the stack pointer do in a microprocessor or microcontroller? • What types of information will you find stored within the status register of a microprocessor • or microcontroller? How do we tell the microcontroller to use an I/O pin as an input versus as an output? • What is the sequence of operations when the device receives an interrupt signal? • Why might we want to adjust the clock speed of a microcontroller? • A very important resource for anyone learning to program the MSP430 series of microcontrollers is the Case Tutorial chapter, showing a range of simple programming examples in multiple languages. 3 4 CHAPTER 1. INTRODUCTION Like learning a spoken or written language, it is very helpful to experience practical examples of that language in action. Chapter 2 Case Tutorial The idea behind a Case Tutorial is to explore new concepts by way of example. In this chapter you will read less presentation of theory compared to other Turorial chapters, but by close observation and comparison of the given examples be able to discern patterns and principles much the same way as a scientific experimenter. Hopefully you will find these cases illuminating, and a good supplement to text-based tutorials. These examples also serve well as challenges following your reading of the other Tutorial(s) in this module – can you explain why the circuits behave as they do? 5 6 CHAPTER 2. CASE TUTORIAL 2.1 Assembly example: adding two numbers Schematic diagram +3.3V VCC (+3.3 V) VSS +3.3V P1.0 P2.6 P1.1 MSP430G2553IN20 P2.7 P1.2 TEST P1.3 RST P1.4 P1.7 P1.5 P1.6 P2.0 P2.5 P2.1 P2.4 P2.2 P2.3 On the next page is a listing of the entire assembly-language code, tested and run on a model MSP430G2553IN20 microcontroller using a LaunchPad model MSP-EXP430G2ET development board and version 10 of Code Composer Studio (CCS). Only the code found between the “Code Begin” and “Code End” comment lines are specific to this example program’s purpose, with the rest being assembler directives and other instructions necessary for any simple assembly-language program to run on this microcontroller. All statements beginning with a dot (e.g. .text) are directives telling the assembler how to convert the assembly source code into executable