This textbook provides practicing scientists and engineers a primer on the Atmel AVR microcontroller, highlight the popular ATmega164 microcontroller and other pin-for-pin controllers in the family with a complement of flash memory up to 128 kbytes. The approach is to provide the fundamental skills to quickly get up and operating with this internationally popular microcontroller. We cover the main subsystems aboard the ATmega164, providing a short theory section followed by a description of the related microcontroller subsystem with accompanying hardware and software to exercise the subsystem. In all examples, we use the C programming language. We include a detailed chapter describing how to interface the microcontroller to a wide variety of input and output devices and conclude with several system level examples. Atmel AVR Microcontroller Primer: Programming and Interfacing 1 Preface 5 Acknowledgments 5 Atmel AVR Architecture Overview 15 ATmega16 ARCHITECTURE OVERVIEW 15 Reduced Instruction Set Computer 15 Assembly Language Instruction Set 16 ATmega16 Architecture Overview 17 NONVOLATILE AND DATA MEMORIES 17 In-System Programmable Flash EEPROM 17 Byte-Addressable EEPROM 19 Static Random Access Memory 19 Programmable Lock Bits 19 PORT SYSTEM 20 PERIPHERAL FEATURES---INTERNAL SUBSYSTEMS 22 Time Base 22 Timing Subsystem 23 Pulse Width Modulation Channels 23 Serial Communications 23 Serial USART 23 Serial Peripheral Interface 24 Two-Wire Serial Interface 24 Analog-to-Digital Converter 24 Interrupts 25 PHYSICAL AND OPERATING PARAMETERS 25 Packaging 25 Power Consumption 25 Speed Grades 27 APPLICATION: ATmega16 TESTBENCH 27 Hardware Configuration 27 Software Configuration 29 PROGRAMMING THE ATmega16 33 Programming Procedure 34 SOFTWARE PORTABILITY 36 SUMMARY 37 REFERENCES AND FURTHER READING 37 CHAPTER PROBLEMS 37 Serial Communication Subsystem 39 SERIAL COMMUNICATIONS 39 SERIAL COMMUNICATION TERMINOLOGY 39 Asynchronous versus Synchronous Serial Transmission 40 Baud Rate 40 Full Duplex 40 Nonreturn to Zero Coding Format 40 The RS-232 Communication Protocol 41 Parity 41 American Standard Code for Information Interchange 41 SERIAL USART 41 System Overview 42 USART Clock Generator 43 USART Transmitter 44 USART Receiver 44 USART Registers 44 System Operation and Programming 46 Serial Peripheral Interface 48 SPI Operation 48 Registers 49 Programming 51 TWO-WIRE SERIAL INTERFACE 52 SUMMARY 52 REFERENCES AND FURTHER READING 52 CHAPTER PROBLEMS 53 Analog-to-Digital Conversion 55 BACKGROUND THEORY 55 Analog versus Digital Signals 56 Sampling, Quantization, and Encoding 58 Resolution and Data Rate 62 ANALOG-TO-DIGITAL CONVERSION PROCESS 64 ADC CONVERSION TECHNOLOGIES 67 Successive Approximation 67 Integration 69 Counter-Based Conversion 69 Parallel Conversion 69 THE ATMEL ATmega16 ADC SYSTEM 69 Block Diagram 70 Registers 72 ADC Multiplexer Selection Register 72 ADC Control and Status Register A 73 ADC Data Registers (ADCH and ADCL) 73 Programming the ADC 73 Digital-to-Analog Conversion 76 SUMMARY 77 REFERENCES AND FURTHER READING 77 CHAPTER PROBLEMS 78 Interrupt Subsystem 79 INTERRUPT THEORY 79 ATmega16 INTERRUPT SYSTEM 79 PROGRAMMING AN INTERRUPT 80 APPLICATION 82 External Interrupts 82 Internal Interrupt 85 SUMMARY 88 REFERENCES AND FURTHER READING 88 CHAPTER PROBLEMS 88 Timing Subsystem 89 OVERVIEW 89 TIMING-RELATED TERMINOLOGY 90 Frequency 90 Period 90 Duty Cycle 90 TIMING SYSTEM OVERVIEW 90 APPLICATIONS 93 Input Capture---Measuring External Timing Event 93 Counting Events 95 Output Compare---Generating Timing Signals to Interface External Devices 95 Industrial Implementation Case Study (PWM) 96 OVERVIEW OF THE ATMEL TIMERS 97 TIMER 0 SYSTEM 98 Modes of Operation 100 Normal Mode 101 Clear Timer on Compare Match 101 Phase Correct PWM Mode 101 Fast PWM 101 Timer 0 Registers 101 Timer/Counter Control Register 0 102 Timer/Counter Register 102 Output Compare Register 102 Timer/Counter Interrupt Mask Register 104 Timer/Counter Interrupt Flag Register 105 TIMER 1 105 Timer 1 Registers 105 TCCR1A and TCCR1B Registers 105 Timer/Counter Register 1 (TCNT1H/TCNT1) 105 Output Compare Register 1 Channel A (OCR1AH/ OCR1AL) 108 Output Compare Register 1 Channel B (OCR1BH/ OCR1BL) 108 Input Capture Register 1 (ICR1H/ICR1L) 108 Timer/Counter Interrupt Mask Register (TIMSK) 108 Timer/Counter Interrupt Flag Register (TIFR) 108 TIMER 2 108 Timer/Counter Control Register 2 108 Timer/Counter Register (TCNT2) 109 Output Compare Register (OCR2) 109 Timer/Counter Interrupt Mask Register (TIMSK) 110 Timer/Counter Interrupt Flag Register 110 PROGRAMMING THE TIMER SYSTEM 110 Precision Delay 112 Pulse Width Modulation 113 Input Capture Mode 115 SUMMARY 117 REFERENCES AND FURTHER READING 117 CHAPTER PROBLEMS 118 Atmel AVR Operating Parameters and Interfacing 119 OPERATING PARAMETERS 120 INPUT DEVICES 121 Switches 123 Switch Debouncing 124 Keypads 125 Sensors 125 Digital Sensors 125 Analog Sensors 128 OUTPUT DEVICES 128 Light-Emitting Diodes 129 Seven-Segment LED Displays 131 Tristate LED Indicator 131 Dot Matrix Display 134 Liquid Crystal Display 134 High-Power DC Devices 138 DC MOTOR SPEED AND DIRECTION CONTROL 139 DC Motor Operating Parameters 140 AC Devices 140 APPLICATION: FLIGHT SIMULATOR PANEL 141 SUMMARY 171 REFERENCES AND FURTHER READING 172 CHAPTER PROBLEMS 172 ATmega16 Register Set 173 ATmega16 Header File 175 Author Biography 191 Index 193 This textbook provides practicing scientists and engineers a primer on the Atmel AVR microcontroller. Our approach is to provide the fundamental skills to quickly get up and operating with this internationally popular microcontroller. The Atmel ATmega16 is used as a representative sample of the AVR line. The knowledge you gain on the ATmega16 can be easily translated to every other microcontroller in the AVR line. We cover the main subsystems aboard the ATmega16, providing a short theory section followed by a description of the related microcontroller subsystem with accompanying hardware and software to exercise the subsytem. In all examples, we use the C programming language. We conclude with a detailed chapter describing how to interface the microcontroller to a wide variety of input and output devices. Table of Contents: Atmel AVR Architecture Overview / Serial Communication Subsystem / Analog-to-Digital Conversion / Interrupt Subsystem / Timing Subsystem / Atmel AVR Operating Parameters and Interfacing / ATmega16 Register Set / ATmega16 Header File "This textbook provides practicing scientists and engineers a primer on the Atmel AVR microcontroller. Our approach is to provide the fundamental skills to quickly get up and operating with this internationally popular microcontroller. The Atmel ATmega16 is used as a representative sample of the AVR line. The knowledge you gain on the ATmega16 can be easily translated to every other microcontroller in the AVR line. We cover the main subsystems aboard the ATmega16, providing a short theory section followed by a description of the related microcontroller subsystem with accompanying hardware and software to exercise the subsystem. In all examples, we use the C programming language. We conclude with a detailed chapter describing how to interface the microcontroller to a wide variety of input and output devices."--Jacket