Introduction to Microcontrollers Courses 182.064 & 182.074
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Microcontrollers Courses 182.064 & 182.074 Vienna University of Technology Institute of Computer Engineering Embedded Computing Systems Group February 26, 2007 Version 1.4 Gunther¨ Gridling, Bettina Weiss Contents 1 Microcontroller Basics 1 1.1 Introduction ....................................... 1 1.2 Frequently Used Terms ................................. 6 1.3 Notation ......................................... 7 1.4 Exercises ........................................ 8 2 Microcontroller Components 11 2.1 Processor Core ..................................... 11 2.1.1 Architecture ................................... 11 2.1.2 Instruction Set ................................. 15 2.1.3 Exercises .................................... 21 2.2 Memory ......................................... 22 2.2.1 Volatile Memory ................................ 23 2.2.2 Non-volatile Memory .............................. 27 2.2.3 Accessing Memory ............................... 29 2.2.4 Exercises .................................... 31 2.3 Digital I/O ........................................ 33 2.3.1 Digital Input .................................. 34 2.3.2 Digital Output ................................. 38 2.3.3 Exercises .................................... 39 2.4 Analog I/O ....................................... 40 2.4.1 Digital/Analog Conversion ........................... 40 2.4.2 Analog Comparator ............................... 41 2.4.3 Analog/Digital Conversion ........................... 42 2.4.4 Exercises .................................... 51 2.5 Interrupts ........................................ 52 2.5.1 Interrupt Control ................................ 52 2.5.2 Interrupt Handling ............................... 55 2.5.3 Interrupt Service Routine ............................ 57 2.5.4 Exercises .................................... 59 2.6 Timer .......................................... 60 2.6.1 Counter ..................................... 60 2.6.2 Input Capture .................................. 62 2.6.3 Output Compare ................................ 65 2.6.4 Pulse Width Modulation ............................ 65 2.6.5 Exercises .................................... 66 2.7 Other Features ...................................... 68 2.7.1 Watchdog Timer ................................ 68 i 2.7.2 Power Consumption and Sleep ......................... 69 2.7.3 Reset ...................................... 70 2.7.4 Exercises .................................... 71 3 Communication Interfaces 73 3.1 SCI (UART) ....................................... 75 3.2 SPI ............................................ 82 3.3 IIC (I2C) ........................................ 83 3.3.1 Data Transmission ............................... 84 3.3.2 Speed Control Through Slave ......................... 87 3.3.3 Multi-Master Mode ............................... 87 3.3.4 Extended Addresses .............................. 88 3.4 Exercises ........................................ 88 4 Software Development 89 4.1 Development Cycle ................................... 91 4.1.1 Design Phase .................................. 91 4.1.2 Implementation ................................. 92 4.1.3 Testing & Debugging .............................. 94 4.2 Programming ...................................... 97 4.2.1 Assembly Language Programming ....................... 97 4.3 Download ........................................ 117 4.3.1 Programming Interfaces ............................ 117 4.3.2 Bootloader ................................... 118 4.3.3 File Formats .................................. 118 4.4 Debugging ........................................ 121 4.4.1 No Debugger .................................. 121 4.4.2 ROM Monitor ................................. 124 4.4.3 Instruction Set Simulator ............................ 124 4.4.4 In-Circuit Emulator ............................... 125 4.4.5 Debugging Interfaces .............................. 125 4.5 Exercises ........................................ 127 5 Hardware 129 5.1 Switch/Button ...................................... 129 5.2 Matrix Keypad ..................................... 130 5.3 Potentiometer ...................................... 132 5.4 Phototransistor ..................................... 132 5.5 Position Encoder .................................... 133 5.6 LED ........................................... 134 5.7 Numeric Display .................................... 135 5.8 Multiplexed Display .................................. 136 5.9 Switching Loads .................................... 138 5.10 Motors .......................................... 140 5.10.1 Basic Principles of Operation ......................... 140 5.10.2 DC Motor .................................... 142 5.10.3 Stepper Motor ................................. 146 5.11 Exercises ........................................ 153 ii A Table of Acronyms 155 Index 159 iii iv Preface This text has been developed for the introductory courses on microcontrollers taught by the Institute of Computer Engineering at the Vienna University of Technology. It introduces undergraduate stu- dents to the field of microcontrollers – what they are, how they work, how they interface with their I/O components, and what considerations the programmer has to observe in hardware-based and em- bedded programming. This text is not intended to teach one particular controller architecture in depth, but should rather give an impression of the many possible architectures and solutions one can come across in today’s microcontrollers. We concentrate, however, on small 8-bit controllers and their most basic features, since they already offer enough variety to achieve our goals. Since one of our courses is a lab and uses the ATmega16, we tend to use this Atmel microcontroller in our examples. But we also use other controllers for demonstrations if appropriate. For a few technical terms, we also give their German translations to allow our mainly German- speaking students to learn both the English and the German term. Please help us further improve this text by notifying us of errors. If you have any sugges- tions/wishes like better and/or more thorough explanations, proposals for additional topics, . , feel free to email us at [email protected]. v Chapter 1 Microcontroller Basics 1.1 Introduction Even at a time when Intel presented the first microprocessor with the 4004 there was alrady a demand for microcontrollers: The contemporary TMS1802 from Texas Instruments, designed for usage in cal- culators, was by the end of 1971 advertised for applications in cash registers, watches and measuring instruments. The TMS 1000, which was introduced in 1974, already included RAM, ROM, and I/O on-chip and can be seen as one of the first microcontrollers, even though it was called a microcom- puter. The first controllers to gain really widespread use were the Intel 8048, which was integrated into PC keyboards, and its successor, the Intel 8051, as well as the 68HCxx series of microcontrollers from Motorola. Today, microcontroller production counts are in the billions per year, and the controllers are inte- grated into many appliances we have grown used to, like • household appliances (microwave, washing machine, coffee machine, . ) • telecommunication (mobile phones) • automotive industry (fuel injection, ABS, . ) • aerospace industry • industrial automation • ... But what is this microcontroller we are talking about? What is the difference to a microprocessor? And why do we need microcontrollers in the first place? To answer these questions, let us consider a simple toy project: A heat control system. Assume that we want to • periodically read the temperature (analog value, is digitized by sensor; uses 4-bit interface), • control heating according to the temperature (turn heater on/off; 1 bit), • display the current temperature on a simple 3-digit numeric display (8+3 bits), • allow the user to adjust temperature thresholds (buttons; 4 bits), and • be able to configure/upgrade the system over a serial interface. So we design a printed-circuit board (PCB) using Zilog’s Z80 processor. On the board, we put a Z80 CPU, 2 PIOs (parallel I/O; each chip has 16 I/O lines, we need 20), 1 SIO (serial I/O; for commu- nication to the PC), 1 CTC (Timer; for periodical actions), SRAM (for variables), Flash (for program 1 2 CHAPTER 1. MICROCONTROLLER BASICS memory), and EEPROM (for constants).1 The resulting board layout is depicted in Figure 1.1; as you can see, there are a lot of chips on the board, which take up most of the space (euro format, 10 × 16 cm). Figure 1.1: Z80 board layout for 32 I/O pins and Flash, EEPROM, SRAM. Incidentally, we could also solve the problem with the ATmega16 board we use in the Microcon- troller lab. In Figure 1.2, you can see the corresponding part of this board superposed on the Z80 PCB. The reduction in size is about a factor 5-6, and the ATmega16 board has even more features than the Z80 board (for example an analog converter)! The reason why we do not need much space for the ATmega16 board is that all those chips on the Z80 board are integrated into the ATmega16 microcontroller, resulting in a significant reduction in PCB size. This example clearly demonstrates the difference between microcontroller and microprocessor: A microcontroller is a processor with memory and a whole lot of other components integrated on one chip. The example also illustrates why microcontrollers are useful: The reduction of PCB size saves time, space, and money. The difference