Xem mẫu
Coin-Cell-Powered Embedded Design
John B. Peatman
Professor of Electrical and Computer Engineering Georgia Institute of Technology
© 2008 by John B. Peatman All rights reserved.
Qwik&Low Books 1418 Iroquois Path, NE Atlanta, GA 30319 (770) 457-6133 jpeatman@comcast.net
Trademark Information: PIC, PICmicro, nanoWatt Technology, PICkit 2, MPLAB, C18, MPASM, MPLINK are trademarks of Microchip Technology Incorporated in the United States and other countries. All other trademarks mentioned in this book are property of their respective companies.
The author makes no warranty of any kind, expressed or implied, with regard to the program code contained in this book. The author shall not be liable in any event arising out of the use of this program code.
ISBN 978-0-9799770-0-8 9 8 7 6 5 4 3 2 1
This book is available as a $15.50 print-on-demand paperback book from
www.lulu.com
or as a free download along with supporting material from www.qwikandlow.com
To six former students who changed the direction of my professional life:
Jim Carreker and Neal Williams 1968–1969
Joe Bazzell and Larry Madar 1989–1990
Rawin Rojvanit and Chris Twigg 2001–2002
CONTENTS Preface 7
Chapter 1 Introduction 13
1.1 Low-Power Designs 13 1.2 The Learning Curve 14
1.3 The PIC18LF4321 Microcontroller 15 1.4 The PIC18LF6390 LCD Controller 15 1.5 QwikBug Development Environment 17
1.6 Programming with the PICkit 2 Programmer 17
5.3 Display Strings 61
5.4 Displayable Characters 62 5.5 Decimal Point 62
5.6 T2.c, A Display Template 63
5.7 Initialization of Two Microcontrollers 68 5.8 SPI Initialization 68
5.9 The Display Function and LCDSTRING 68 5.10 The Time Function 69
5.11 The Pushbutton Function 69 Problems 70
Chapter 2 2.1
2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9
Chapter 3 3.1
3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10
Chapter 4 4.1
4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 4.13 4.14 4.15
Chapter 5 5.1
5.2
Low-Power Operation 18 Overview 18
CR2032 Coin Cell 18
A PIC18LFXXXX Family of Microcontrollers 19 INTOSC and INTRC, the Internal Oscillators 21 Intermittent Sleep Mode Operation 21
Effect of Clock Frequency 25
User Program Steps to Reduce Current Draw 26 The Qwik&Low Board 27
Coding in C 28
Qwik&Low Board 31 Overview 31 Equipment Setup 31
Input/Output Peripheral Power 33
Power Switching and Current Monitoring 35 PICkit 2 Programmer Connection 35
Effect of Coin Cell Aging 37 Watch Crystal Circuitry 38 Qwik&Low LCD 38 Expansion Header 40 Summary of MCU Pin Use 40
A First Template Program (T1.c) 43 Overview 43
A T1.c Template Program 43 Configuration Selections 47 Global Variables 50
Bit Manipulations 50 Function Prototypes 51
A Calibrated Delay Macro 51 Main Function 52
8-bit and 16-bit Registers 53 Clock Rate Choice 53
Analog Pins Versus Digital I/O Pins 54 Digital Inputs Versus Outputs 54 Brownout Module Disabling 54
Main Loop 55 Compilation 56 Problems 57
SPI Bus and the LCD (T2.c) 58 Overview 58
Serial Peripheral Interface 58
Chapter 6 6.1
6.2 6.3 6.4 6.5 6.6 6.7
Chapter 7 7.1
7.2 7.3 7.4 7.5 7.6 7.7
Chapter 8 8.1
8.2 8.3 8.4 8.5
Chapter 9 9.1
9.2 9.3 9.4 9.5 9.6 9.7 9.8
Chapter 10 10.1 10.2 10.3
PC Monitor Use (Measure.c) 71 Overview 71
Waveforms and Baud-rate Accuracy 72 UART’s TX Circuitry and Use 76 UART Initialization 76
TXascii Macro 78
Number-to-ASCII Conversion 78 Measure.c, a Cycle Counting Program 80 Problems 88
Reorganization of Timing Via Interrupts (T3.c) 89 Overview 89
Low- and High-Priority Interrupts 89 Interrupts and the C18 Compiler 91 Timer1 Oscillator 91
Timer1 Counter 94 Timer3 Counter 96
The T3.c Template Program 96 Problems 96
Stepper Motor Control 107 Overview 107
Stepper-Motor Operation 107
Bipolar Versus Unipolar Stepper Motors 111 Stepper-Motor Driver 112
Stepping 115 Problems 116
Analog-To-Digital Converter 119 Overview 119
Qwik&Low Analog Versus Digital Pins 119 ADC Result Alternatives 122
Reference Voltage Choice 123 ADC Timing 123
ADC Input Selection and Conversion 125 ADC Conversion During Sleep 126 AD22103 Temperature Sensor 127 Problems 132
Rotary Pulse Generator (RPG.c) 133 Overview 133
RPG Resolution 133 RPG Functionality 134
4
Contents 5
10.4 10.5
Chapter 11 11.1 11.2 11.3 11.4 11.5 11.6 11.7
Chapter 12 12.1 12.2 12.3 12.4 12.5 12.6 12.7 12.8
Chapter 13 13.1 13.2 13.3 13.4 13.5
Chapter 14 14.1 14.2 14.3 14.4 14.5 14.6
Chapter 15 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8
Chapter 16 16.1 16.2
The RPG Function 136 The Detented RPG 138 Problems 149
Measurements 150 Overview 150 Code Size 150
Code Execution 153
Variable Code Execution 153 Interrupt Timing Measurement 155
Time Spent Doing Useful Work in Main Loop 156 Cleaning Up Surprises 156
Problems 157
Interrupts 159 Overview 159
MCU Interrupt Response 159 Compiler Handling of Interrupts 160 Using One Priority Level Only 163 PIC18LF4321 Interrupt Sources 165
Use of the Interrupt Mechanism + Idle Mode 165 External Interrupts 165
PORTB-Change Interrupts 167 Problems 168
Timing Measurements Revisited (Calibrate.c) 169 Overview 169
Timer0 Operation 170
Internal Oscillator Calibration 173 External Time Measurement 181 Start, Stop, and Send Functions 183 Problems 183
EEPROM (EEtest.c) 187 Overview 187 EEPROM Use 187
EEPROM Registers and Functions 188 Multiple Write Sequences 191 Protecting the Write Sequence 191 EEPROM Life 198
Problems 199
1-Wire Interface (SSN.c) 201 Overview 201
Interface Circuitry 201 Writing Ones and Zeros 203 Message Protocol 204 Reading Ones and Zeros 211
Generalizing from the SSN.c Template 211 Multiple 1-Wire Devices on a Single Bus 213 DS2415 1-Wire Time Chip 215
Problems 217
Starburst Display (LCD.c) 219 Overview 219
Starburst Display Configuration 219
16.3 LCD Controller Circuit 221
16.4 Multiplexed LCD Voltage Waveforms 223 16.5 LCDDATAi Register Use 224
16.6 ASCII Code to LCDDATAi Representation 225 16.7 Awakening vs. Interrupt Vectoring 229
16.8 Reception of SPI Bytes into VSTRING 229 16.9 Decimal Point 230
16.10 Data Structures 230 Problems 239
Chapter 17 SPI for Feature Enhancement 241 17.1 Overview 241
17.2 SPI Output Functionality 242 17.3 SPI Input Functionality 245 17.4 AD5601 DAC Output 246
17.5 MOSI/MISO Terminology 249
17.6 ADT7301 SPI Temperature Sensor 251 Problems 255
APPENDICES
Appendix A1 QwikBug Program Debugger 256 A1.1 Introduction 256
A1.2 Installation 258
A1.3 Graphical User Interface 262 A1.4 Loading a Program 262 A1.5 Program Control 264
A1.6 Breakpoints 266 A1.7 Watch Variables 267
A1.8 Additional Features 273 A1.9 Keyboard Shortcuts 276
Appendix A2 Interpreting .Lst Files 277 A2.1 Overview 277
A2.2 Harvard Architecture 277
A2.3 Instruction Set and Direct Addressing 279 A2.4 F/W Distinction 279
A2.5 Name Replacements for Operand Addresses 283 A2.6 Counting Cycles 283
A2.7 Flag Bits 285
A2.8 Indirect Addressing of RAM Variables 286 A2.9 CPU Registers 287
A2.10 Indirect Addressing of Program Memory 289 A2.11 Special Function Registers 289
Appendix A3 Qwik&Low Board in Detail 293
Appendix A4 Stepper-Motor Board In Detail 300
Index 306
...
- tailieumienphi.vn
nguon tai.lieu . vn