PROGRAMMING AND CUSTOMIZING THE PICAXE MICROCONTROLLER SECOND EDITION DAVID LINCOLN Mc Grauu Hill New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto
CONTENTS Chapter 1 Introduction 1 What Is a Microcontroller? 1 Where Are Microcontrollers Used? 2 The Advantages of a Programmable Device 2 Why Is It Important to Understand Microcontrollers? 2 What Is a PICAXE Microcontroller? 2 Handling Precautions 3 The Experiments 3 Chapter 2 PICAXE Architecture 5 Chip Architecture 5 Powering the PICAXE 6 Resetting the PICAXE 7 Downloading Programs to the PICAXE 8 Clocking PICAXE Chips 9 PICAXE Memory 10 General-Purpose Variables 10 System Variables 12 Scratchpad 13 Storage Variables 13 Special Function 13 EEPROM 14 Table 16 Pointers 17 Ports 19 Interrupts 20 Chapter 3 Interfacing and Input Output Techniques 21 PICAXE Input and Output 21 Hardware Interfacing 22 Digital Interfacing 24 Port Addressing 26 Setting the Direction of Configurable Pins 26 Analog Interfacing 31 Parallel and Serial Interfacing 34 Asynchronous RS232 35 Personal Computer Connectivity 43 I2C 44 SPI 49 vii
viii CONTENTS UNI/O 52 1-Wire 55 Keyboard 57 Infrared 60 Pulses 62 Servo Motors 66 Chapter 4 Programming 69 What Is a Program? 70 Writing BASIC Code 70 Compiler Directives 71 Data 71 Meaningful Names 74 Commands 75 Labels 75 Comments 76 Reserved Words 76 Assigning Values to Variables 77 Advanced Programming 78 Program Flow 78 Code Structures 79 Selection 79 Iteration 81 Pointers 83 Subroutines 84 Pausing and Stopping Program Execution 56 Power Saving 89 Interrupts 89 Program Testing and Debugging 91 Programming with Flowcharts 93 Chapter 5 PICAXE Arithmetic and Data Conversion 95 Number Systems 96 Binary 96 Octal 96 Hexadecimal 97 Decimal 97 Binary-Coded Decimal 97 ASCII 97 Picaxe Arithmetic 98 Multiplication 99 Division 100 Boolean Arithmetic 101 Data Conversion 705 Converting between Decimal Digits (BCD) and ASCII 105 Converting between Binary and Octal 106 Converting between Binary and Hexadecimal 106 Converting Binary to Decimal and BCD 707 Converting ASCII, BCD, and Decimal to Binary 109
ASCII Integers 110 ASCII Fractions 111 Advanced Arithmetic 112 Decimal Fractions 112 Adding and Subtracting Multiplying Decimal Fractions 112 Dividing Fractions 113 Converting Decimal Fractions to ASCII 113 BCD Arithmetic 113 BCD Addition 113 Subtraction of BCD Digits 114 Decimal Fractions 112 Arithmetic with Signed Numbers 116 Addition and Subtraction of Signed Binary Integers 117 Multiplying and Dividing Signed Binary Numbers 118 Comparing Signed Numbers 119 Converting Signed Binary Numbers to Decimal 119 Arithmetic with Binary Fractions 120 Converting Binary Fractions to Decimal 120 Converting ASCII Fractions to Binary Fractions 122 Floating-Point Arithmetic 122 Lookup 122 Lookdown 124 Chapter 6 Experiments Basic Experiments 126 Constructing the Schools Experimenter Board 127 Testing 128 If It Doesn't Work 129 Using Programming Editor to Download Programs to the Schools Experimenter Board 130 If Program Downloading Fails 131 Connecting External Devices 737 Flash the Red LED 133 Flash the Yellow LED 734 Flash the Red and Green LEDs Alternately 735 Play Sounds through the Piezo Speaker 135 Digital Input 138 Turn the Red LED On If the Switch Is Pressed Turn the Red LED On If Dark 740 Analog Input 141 73S Turn the Red, Green, and Yellow LEDs On Depending on Light Level 141 Turn On the Red, Green, and Yellow LEDs Using a Potentiometer 145 Using a Transistor to Drive a Relay 146 Using a Transistor to Operate a Motor 146 Controlling Motor Speed 147 Controlling Motor Speed with a Potentiometer 148 Operating a Solenoid 149
x CONTENTS Operating an Incandescent Lamp 150 Water Detector 151 Chapter 7 Advanced Experiments 1S3 Bipolar Transistor Output Driver 154 Interfacing Bipolar NPN Transistors 154 Interfacing MOSFETs to a PICAXE 155 N-Channel MOSFETS 155 Transistor Input Driver 156 Interfacing TTL Chips 156 Integrated Driver Circuits 157 H Bridge Motor Drivers L293, SN754410, L298 157 Controlling Motor Speed and Direction with a Potentiometer 159 Stepper Motors 162 Identifying the Windings 162 Identifying the Winding Sequence 163 Circuit Description 163 Servo Motors 167 Input and Output Expansion 169 Input and Output Expansion Using Shift Registers 170 Input and Output Expansion Using Addressable I/O Expansion Chips 172 Using Switches for Input 172 Relays 175 Wireless Links 176 Infrared ) 77 Radio 179 Light-Emitting Diodes 181 Seven-Segment LED Displays 182 Liquid Crystal Displays 189 Connecting an LCD Using a Shift Register 190 Keypad Input 197 Telephone Intercom 199 Testing 203 If It Doesn't Work 204 Connecting Phone Lines 204 Voltmeter 205 Circuit Description 206 1-Wire Serial Number 208 1-Wire Temperature 210 Radio Frequency Identification 214 Simple ASCII Terminal 217 I2C Memory Expansion 221 I2C I/O Expansion 224 I2C Clock/Calendar 225 SPI Memory Expansion 234 SPI I/O Expansion 240 UNI/O Memory Expansion 245
CONTENTS xl Chapter 8 PICAXE M2 Supplement 247 Introduction 248 Powering the PICAXE M2 Series Chips 249 Resetting the M2 Series Chips 250 Downloading Programs to the M2 Series Chips 250 Clocking the M2 Series Chips 250 Memory Arrangement for M2 Series Chips 250 General-Purpose Variables for the M2 Series Chips 251 System Variables for M2 Series Chips 251 Storage Variables for the M2 Series Chips 251 Special Function Variables 252 EEPROM 252 Pointers 253 Byte-Scratchpad Pointer 253 Ports 253 Setting the Direction of Configurable Pins 254 Interrupts 256 Interfacing and Input Output Techniques 259 Programming 260 Compatibility with Existing M and X Series Programs 260 Subroutines 260 PICAXE Arithmetic and Data Conversion 261 Parallel Task Programming (Multitasking) 261 Examples of Parallel Task Processing 263 Touch Sensing 264 Digital to Analog Conversion 266 Fixed-Voltage Reference 268 Elapsed Seconds Timer 268 SR Latch 269 M2 Experiments 270 Touch Sensing 271 Code Description 273 Digital to Analog Conversion 273 Code Description 275 Minute Timer 275 SR Latch Demonstration 276 Appendix Circuit Symbols 277 Index 299