Ron Hackett
Contents at a Glance
PART ONE PICAXE Basics1 Introduction to PICAXE Programming and Projects
2 Introduction to Stripboard Circuits
3 Designing and Building a +5V Regulated Power Supply
4 Hardware Overview of the PICAXE M2-Class Processors
5 The Ins and Outs of PICAXE Interfacing
6 Introduction to ADC Inputs on M2-Class Processors
PART TWO PICAXE Peripheral Projects
7 Introduction to the PICAXE-20X2 Processor
8 Infrared Input from a TV Remote Control
9 Interfacing Parallel LCDs
10 Serializing a Parallel LCD
11 Interfacing Keypads
12 SPI Communication
13 Background Timing on the 20X2 Processor
14 Constructing a Programmable Multifunction Peripheral Device
15 Developing Software for the Evil Genius MPD
PART THREE Octavius: An Advanced Robotics
Experimentation Platform
16 Birthing Octavius
17 Driving Octavius
18 Programming Octavius
Epilogue: What’s Next for Octavius?
Index
About the Author
Ron Hackett has had more than 30 years experience in the fields of education andpsychology. He has taught mathematics, psychology, and computer science courses on both
the high school and college levels, and in-service courses for teachers in the use of
microcomputers in the classroom setting. Ron has published numerous PICAXE-related
articles for Nuts and Volts and SERVO magazines. He also designed the “Brain-Alpha” PC
board used in the popular SERVO TankBot robot. To assist his readers in obtaining
PICAXE information and parts, Ron has also established a website (www.JRHackett.net)
dedicated to “spreading the word” about the PICAXE line of microcontrollers.
When he isn’t ensconced in his basement workspace, writing, or tinkering with Octavius,
Ron enjoys grilling and barbecuing in the outdoor kitchen he built in his backyard.
Currently, he’s working on a PICAXE-based temperature controller for his ceramic smoker.
Foreword
I’VE JUST PUT DOWN THE TELEPHONE after a call from a lovely gentleman, who, at the
age of 81, has decided to start electronics as a hobby. He phones every so often and
always ends the conversation with a jovial “Thanks for your help. Got to get this
project finished soon, as I don’t know how long I’ve got left!” I hope it’s a long time!
When we launched the PICAXE system over ten years ago, it was designed as a
method of allowing schoolchildren to use all the power of Microchip PICs within their
school projects without any of the technical difficulties of complicated hardware or
complex programming languages. In the intervening years, the PICAXE system has
been adopted by hundreds of thousands of other users—industrial, hobbyist, and
educational—due to its ease of use. It’s a joy for the team at Revolution Education to
see all the wonderful projects created by users, both young and old, around the world.
In this book, Ron has worked hard to explain how the PICAXE system operates
through simple examples, and I’m sure his easy-to-read style will help many people
progress with their PICAXE projects. With the recent launch of the new M2 series of
PICAXE chips described in this book, we hope you can achieve even more than
before—and if in need of further help (or proud of a project to share!), why not join
the ever-growing PICAXE community at www.picaxeforum.co.uk?
Enjoy!
Clive Seager
Technical Director
Revolution Education, Ltd.
Introduction
Prologue
THE FIRST MICROCOMPUTER I ever purchased was
the Synertek Systems SYM Model 1 computer (see
Figure 1). I just couldn’t resist the advertising
blurb: “The [SYM-1] is one of the most versatile
and sophisticated single-board computers
available… It’s an ideal introduction to the
expanding world of microprocessor technology as
well as a powerful development tool for design of
microcomputer-based systems.” In its day (1978),
the SYM-1 was state-of-the-art technology: It ran at
a blazing 1MHz and had 1KB of memory and two
8-bit I/O ports for expansion—all that for only
$239.95! (According to the U.S. Bureau of Labor
Statistics, that’s more than $800 in today’s money.)
Out of the box, the SYM-1 could only be
programmed in machine language—a very tedious
process, to say the least. However, it certainly
provided me with a thorough introduction to the
world of microcontrollers. Using it, I was able to
develop a simple home control system that enabled
me to use any telephone in the house to turn lights
on and off, adjust the temperature, set timers and
alarms, etc. I was having a grand time until the day
the telephone repairman (who came to fix an
unrelated problem) ordered me to cease and desist
or he would confiscate my SYM-1. Back then, Ma
Bell had the legal authority to do exactly that with
anything it didn’t want connected to the phone
and then develop several intelligent peripherals
based on PICAXE processors in the Educational
line (especially the 08M2). For example, in
Chapter 11 we’re going to use the PICAXE-08M2
to develop a serially interfaced keypad that can be
connected easily with our 20X2 master processor.
Of course, the 20X2 (or any PICAXE processor,
for that matter) could also be connected directly
to a keypad without the help of a peripheral
processor, but the approach we are going to take
has two major advantages. First, once we have
developed an “intelligent” peripheral (e.g., our
keypad), it becomes a stand-alone device that can
simply be connected to any future project we
tackle. If we have dozens of projects, we don’t
need dozens of keypads; we can simply move the
keypad from project to project as the need arises.
Second, our multiprocessor approach greatly
simplifies the process of software development as
we move into more complex projects because
many of the programming details of the I/O
interfaces will already have been thoroughly
developed and debugged before interfacing the
peripheral device with the master processor.
Essentially, we’re using a “divide and conquer”
approach to both the hardware and software
development of complex projects.
In Part One of this book, we’re going to start
at the very beginning. Our first project is supersimple
and doesn’t require any previous knowledge
of PICAXE programming. Throughout the
remainder of Part One we’ll focus primarily on the
08M2 and 20M2 processors, and cover some of the
essential elements of PICAXE programming and
I/O interfacing. By the time you have completed
the projects in Part One, you will be ready to
tackle Part Two, in which we will implement our
master/peripheral paradigm.
In Part Two, we will begin by developing a
master processor circuit based on the PICAXE-
20X2 and then move on to the development of
several stand-alone peripherals for our master
processor, including:
■ Infrared remote receiver
■ Sixteen-character-by-two-line LCD
■ 4 by 4 matrix keypad
■ Four-digit LED display
In Part Three, we’ll apply our master/peripheral
paradigm to the development of Octavius, a
sophisticated robotics experimentation platform
that includes a 40X2 master processor and eight
breadboard stations, which span its perimeter and
provide us with the necessary workspace to
develop a range of sensory/motor peripherals to
augment Octavius’ functioning. Octavius also
includes a unique time-slice arrangement that
enables him to communicate with his numerous
sensory/motor peripheral processors and greatly
simplifies the software necessary for monitoring
his various processes and successfully navigating
his way through the environment.
A Brief but Important Note
Concerning All the Projects in the Book
When I began writing this book, the new M2
class of PICAXE processors had not yet been
announced. All the projects in the book that use a
smaller chip were originally developed using the
older M-class processors (08M, 14M, and 20M).
When RevEd announced the new M2 processors,
I decided to modify all the relevant projects so that
the programs and information in the book would
be as up to date as possible. However, since many
people (including myself) still have the older
M-class processors on hand, the relevant projects
can also be implemented on the corresponding
M-class processor with only minor modifications
to the software presented in the book. When you
visit my website (www.JRHackett.net) to
download the software for the projects, you will
see that I have included an “M” version and an
“M2” version for every relevant program; simply
download the version that will work with the
processor you have available.
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Prologue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
PART ONE PICAXE Basics . . . . . . . . . . . . . . . . . . . 1
1 Introduction to PICAXE Programming and Projects. . . . . . . 3
Choosing a PICAXE Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Interfacing a Project with Your Mac or PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Using RevEd’s Free Programming Editor or AXEpad Software . . . . . . . . . . . . . . 5
Programming in PICAXE BASIC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Breadboards, Stripboards, and PC Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Project 1 “Hello World”. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Debugging a PICAXE Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2 Introduction to Stripboard Circuits. . . . . . . . . . . . . . . . . . . . . 15
Designing Stripboard Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Tools for Stripboard Circuit Construction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Project 2 The USBS-PA3 PICAXE Programming Adapter . . . . . . . . . . . . . . . . . 21
Hello Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 Designing and Building a +5V Regulated Power Supply . . . 27
Designing a +5V Regulated Power Supply for Breadboard Circuits . . . . . . . . . . . 28
Project 3 More Power, Scotty!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4 Hardware Overview of the PICAXE M2-Class Processors . . 39
General-Purpose Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Storage Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Special-Function Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Project 4 Cylon Eye. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5 The Ins and Outs of PICAXE Interfacing . . . . . . . . . . . . . . . . 51
PICAXE I/O Interfacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Setting Up an Interrupt Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Project 5 Mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6 Introduction to ADC Inputs on M2-Class Processors . . . . . . 65
Voltage Dividers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Project 6 A Three-State Digital Logic Probe . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
PART TWO PICAXE Peripheral Projects . . . . . . . 81
7 Introduction to the PICAXE-20X2 Processor. . . . . . . . . . . . . 83
Advanced Features of the 20X2 Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Project 7 Implementing the 20X2 Master Processor Circuit . . . . . . . . . . . . . . . . 86
8 Infrared Input from a TV Remote Control . . . . . . . . . . . . . . . 93
Reception and Transmission of Standard TV IR Signals . . . . . . . . . . . . . . . . . . . . 93
IR-Based Serial Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Simple IR Object-Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Experiment 1: A Simple TV-IR Input Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Experiment 2: Interfacing the IR Circuit with the Master Processor . . . . . . . . . . . 98
Project 8 Constructing the TV-IR Input Module. . . . . . . . . . . . . . . . . . . . . . . . . . 101
9 Interfacing Parallel LCDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Understanding the Basics of HD44780-based LCDs . . . . . . . . . . . . . . . . . . . . . . . 108
Experiment 1: Interfacing an HD44780-based Parallel LCD . . . . . . . . . . . . . . . . . 110
Project 9 Constructing an Eight-bit Parallel 16 x 2 LCD Board. . . . . . . . . . . . . . 114
Programming Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
10 Serializing a Parallel LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Receiving Serial Data in the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Project 10 Constructing a Serialized 16 x 2 LCD. . . . . . . . . . . . . . . . . . . . . . . . . 121
11 Interfacing Keypads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Decoding Matrix Keypads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Project 11 Constructing a Serialized 4 by 4 Matrix Keypad. . . . . . . . . . . . . . . . . 145
12 SPI Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The MAX7219 8-Digit LED Display Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Project 12 Constructing an SPI 4-Digit LED Display . . . . . . . . . . . . . . . . . . . . . 158
Learning to Count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
13 Background Timing on the 20X2 Processor . . . . . . . . . . . . . 173
Using Timer1 on the 20X2 Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
“Deconstructing” a Matrix Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Testing the “New and Improved” Keypad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Project 13 Constructing a Countdown Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
14 Constructing a Programmable Multifunction
Peripheral Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Project 14 The Evil Genius Multifunction Peripheral Device . . . . . . . . . . . . . . . 187
15 Developing Software for the Evil Genius MPD . . . . . . . . . . . 203
Understanding the 20X2’s Built-in Comparator Hardware. . . . . . . . . . . . . . . . . . . 203
Testing Our Comparator 1 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
“We Interrupt This Program to Bring You a Keypress!”. . . . . . . . . . . . . . . . . . . . . 206
Project 15 A Simple MPD Operating System. . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
PART THREE Octavius: An Advanced Robotics
Experimentation Platform . . . . . . . 213
16 Birthing Octavius. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Understanding Octavius. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Project 16 Building Octavius. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
17 Driving Octavius . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
H-Bridge Motor Control Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The L298 Dual H-Bridge Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Project 17 Constructing an L298 Dual DC Motor Controller Board . . . . . . . . . . 228
18 Programming Octavius . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
The MaxBotix LV-MaxSonar Ultrasonic Range Finders . . . . . . . . . . . . . . . . . . . . 239
Who’s in Charge Here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Project 18 Hail, Octavius! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Epilogue: What’s Next for Octavius? . . . . . . . . . . . . . . . . . . . 253
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255