ebook img

Programming the Propeller with Spin. Beginner’s Guide to Parallel Processing PDF

336 Pages·2010·2.633 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming the Propeller with Spin. Beginner’s Guide to Parallel Processing

Programming the ProPeller™ with SPin™: a Beginner’S guide to Parallel ProceSSing harprit Singh Sandhu Copyright © 2010 by The McGraw-Hill Companies. ISBN: 978-0-07-171667-3 MHID: 0-07-171667-X The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-171666-6, MHID: 0-07-171666-1. contentS Preface xiii Part i the Propeller/Spin System 1 Introduction for the Beginner 1 chapter 1 a general introduction to the Propeller chip 3 The Propeller Manual 3 Parallax, Inc. 5 Overall System Description 5 The Propeller Tool 7 Instruments Needed to Support Your Experiments 8 chapter 2 the Propeller chip: an overall description 9 Basic Propeller Specifications 10 Voltage and Amperage Requirements 10 The Operation of the Eight Cogs 10 The Cogs 11 The Hub 12 Forty Pins Total, 32 Pins I/O 12 Connecting to the Propeller 13 The System Counter 14 Program Storage and Execution 14 Objects, Methods, and Other Definitions 15 chapter 3 the hardware Setup 19 Setting Up the Hardware 21 A Fundamental Reality We Have to Consider 23 chapter 4 Software Setup: the “Propeller tool” environment 25 Classroom Analogy 27 Getting Ready to Use the Propeller 28 Installing the Software 28 Our First Program 29 The Typical Spin Program 32 Program Structure 34 General Pin Assignments Used in the Book 36 Propeller FAQ* 38 chapter 5 the Various Propeller memories 43 Assigning Memory for a New Cog 45 A New Cog Can Be Started to Run a Private or Public Method 45 chapter 6 the how and why of Shared memory 47 Memory Usage 48 Variable Validity 49 Loops 50 chapter 7 understanding one cog 51 Static Versus Dynamic 53 One Cog 55 Counters 58 Counter: General Description 59 Assignment of the 32 Bits in Each of the Counters 59 Using Counter A for PWM Generation 60 chapter 8 the eight cogs 65 The Cogs 65 The Flags 66 Special Memory Locations 66 The System Clock 66 Programming 67 The ROM 67 chapter 9 Special terms and ideas 69 The Hardware 69 The Software 70 New Hardware-Related Definitions 70 New Software-Related Definitions 71 chapter 10 the Spin language 75 CON 77 VAR 77 OBJ 78 PUB or PRI 78 Creating a Program with Two Cogs 83 chapter 11 tasks Suited to Parallel Processing 85 Parallel Programming Examples 85 Summary 87 Part ii input and output: the Basic techniques to Be mastered—learning by doing 89 chapter 12 general discussion of input/output 91 chapter 13 Binary Pulsing 95 chapter 14 Setting up a 16-character-by-2-line liquid crystal display 101 chapter 15 Binary input and output: reading a Switch and turning on an led if the Switch is closed 109 Discussion 111 The Repeat Command 112 chapter 16 reading a Potentiometer: creating an input we can Vary in real time 113 Analog Inputs 114 Advanced Techniques 118 chapter 17 creating and reading Frequencies 129 Creating Audible Frequencies 130 Reading Frequencies 135 chapter 18 reading and creating Pulses 139 Reading Pulse Widths 139 Determining the Pulse Width 140 Pulse Width Creation 146 Part iii the Projects: using what was learned to Build the Projects 149 chapter 19 Seven-Segment displays: displaying numbers with Seven-Segment led displays 151 chapter 20 the metronomes 159 chapter 21 understanding a 16-character-by-2-line lcd display 163 8-Bit Mode 164 Sophisticated Total LCD Control 171 4-Bit Mode 182 chapter 22 running motors: a Preliminary discussion 189 R/C Hobby Servomotors 190 Stepper Motors (Bipolar) 190 Small Brush-Type DC Motors 191 DC Motors with Attached Encoders 191 Relays and Solenoids 191 Small A/C Motors at 120 Volts, Single Phase 192 Understanding the Concept of the “Response Characteristics” of a Motor 192 So What Does “Compliance” Mean? 192 DC Motor Operation Notes 193 chapter 23 motor amplifiers for Small motors 195 Amplifier Construction Notes (for Homemade Amplifiers) 197 Detailed “Use Information” for the Xavien Two-Axis Amplifier 198 Detailed “Use Information” for the Solarbotics Two-Axis Amplifier 199 chapter 24 controlling r/c hobby Servos 203 Servo Control 204 chapter 25 controlling a Small dc motor 211 The Software 214 chapter 26 running a Stepper motor: Bipolar, Four-wire motors 225 Stepper Motor Power and Speed 226 Details on Bipolar Motors 226 Running the Motor 227 Programming Considerations 229 The Software 231 chapter 27 gravity Sensor Based auto-leveling table 247 Sensor Specifications 248 Discussion 248 chapter 28 running dc motors with attached incremental encoders 257 Not about Motors 258 Discussion 258 DC Servo Motors with Encoders 261 Processor Connections 262 The Goal 262 PID Control in Greater Detail 263 Holding the Motor Position 265 Ramping 294 R/C Signal Use 305 Some Advanced Considerations You Should Be Aware Of 312 chapter 29 running Small ac motors: controlling inductive loads 313 Part iV appendixes 315 appendix a lcdroutines4 and utilities object listings 317 appendix B materials 327 appendix c turning cogs on and off 329 appendix d experiments Board 331 appendix e debugging 335 Debugging and Troubleshooting 335 Dumb Terminal Program 337 Signal Injection Techniques 337 Notes on Solderless Breadboards 338 Debugging at the More Practical Level 339 Writing a Rudimentary Program for Testing the LCD 340 Another List of Simple Checks 341 epilogue 343 index 345 PreFace After I finished my book Running Small Motors with PIC Microcontrollers, I asked my friend David H. at HVW Technologies in Canada if he had in any ideas as to what might be worth covering in my next book. David suggested that a book about the new Propeller chip from Parallax, written in the same vein as my other hands-on books, could be a welcome effort. With this in mind, I contacted Parallax, Inc., in California and they turned me over to Ms. Stephanie Lindsay, their contact person for authors. Ms. Lindsay was good enough to send me a comprehensive authoring package to get me started on this adventure. In this book I share what I have learned about the Propeller chip and parallel processing with you. It is my wish that by the time you have read through it and have done all the experiments, you will have the confidence, skills, and knowledge necessary to start using the Propeller chip in ways that will make your life both more interesting and, hopefully, more productive. My first reaction to opening the authors’ package and starting on the Propeller manual was, How am I ever going to learn to use this processor? The material was not beginner friendly. Although it was at a higher level, it was very interesting. The further I got into reading and understanding the manual, the more fascinated I became with what the very clever engineers at Parallax had created. It is certainly one of the won- ders of the modern world that you can buy eight 32-bit processors and shared memory for less than $8. In this book we will discover what all this, including parallel process- ing, means to us as engineers, technicians, and hobbyists. As always, I will minimize the use of complicated formulas and jargon so that if you are interested in things mechanical and electronic and have a rudimentary knowledge of what a computer program is, you will be able to use these processors to undertake simple tasks and maybe even some fairly complicated projects in a parallel-processing environment. There are, of course, two aspects to learning how to use the Propeller chip. The first is learning how to use each of the identical 32-bit processors in the chip. Parallax calls each of these eight processors a “cog,” and each of these cogs is similar to a typical 32-bit processor, with some special features added and some left off. The second is learning how to make these eight cogs interact with one another in an effective way to explore the fascinating parallel-processing possibilities that are now suddenly within our reach. Because the eight 32-bit processors on the chip are identical, once you have learned to use one of them, you have learned to use all of them. The intellectual discipline that has to be mastered involves setting up the problem in such a way that the eight processors can be used in the most effective way possible, thus creating a viable solution for the task you have in mind. This has to do with really understanding the problem and with learning how to break a problem down into separate tasks, each of which can be assigned to one of the cogs, in an orderly and logical way. We will learn how to do this. Because not every problem lends itself to a parallel-processing solution, we will spend some time on learning how to identify those problems that can be solved within a parallel-processing environment. This book is intended for the novice user. It is for the novice user for two reasons: One, the material that Parallax provides regarding this chip is more advanced than a first-time user can master with ease. Two, I am also a novice as far as this particular discipline (parallel processing) goes. In this book, I share what I have learned with you, in a straightforward and hopefully nonintimidating manner that you will find use- ful. We will learn by doing, which is the best way to learn to do anything. Before we can start, though, we have to understand what the system is capable of and how this book is organized to address the tasks at hand. This book is divided into four parts that compartmentalize what we are interested in: ■ The first part of the book introduces you to the Propeller chip, starting with one cog (the term used by Parallax for each of the eight 32-bit processors in the Propeller chip). First, we learn about just one of the eight processors and how to interact with the I/O provided on the chip. This I/O is in a shared portion of the chip that all the cogs can address. All the features of the one cog are covered in detail. At the end of the first part, you should have a good idea of what the system is all about and be ready to begin using it. ■ In the second part of the book, I cover what you need to know to develop the skills necessary for creating a system that allows the cogs to work together. We will do this by learning how to make the Propeller interact with a number of input and output devices. I have selected the kind of devices an amateur enthusiast, a techni- cian, or an engineer is likely to be interested in interacting with on a day-to-day basis—displays, switches, detectors, motors, and such. The limited memory in the system does not lend itself to the handling of large arrays and related number- crunching applications, so simple control applications like the ones we will con- sider are the most suited for investigation by us beginners. The major objective of this part of the book is to learn how to read and create signals of various types. ■ In the third part of the book, we use the lessons we have learned in Part II to build and program a number of devices using the Propeller chip. The device in each experiment is a real-world application of the parallel-processing environment, and each one uses more than one cog. Not all the projects are completed 100 percent, so you have the challenge to complete them. Appropriate information and hints are provided. ■ The fourth part of the book is composed of appendixes that provide you with sup- plemental information you will find helpful in using the device. This includes the hardware and software resources needed for the experiments we undertake. Where special items are needed, I have made arrangements to provide them on my website at Encodergeek.com. A large part of the information you need to help you use this processor can be found on websites maintained by Parallax and others. I recommend that you get familiar with what is in these websites and get comfortable with using the material these websites provide. The discussion forums are extraordinarily useful and should be made a part of your regular reading and learning experience. The following three online resources provide a good starting point as you progress through this book: ■ The Parallax forums. These forums are your most useful resource. ■ Wikipedia provides useful general information. ■ The support web pages provide specific information. The Propeller chip software is organized such that routines called methods and procedures, created by one person, can be used by others with relative ease. Most of these procedures are well documented, and because all the code is visible, you can modify it to serve your more specific needs should that become necessary. This being the case, becoming familiar with and studying the work done by others is one of the skills you need to develop. I will provide methods with full documentation to support the devices we use so that you can see how these methods are created and then called in subsequent programs. All the experiments in this book can be undertaken with the Propeller Education Kit (32305) provided by Parallax and a minimal amount of additional hardware. Other than the Propeller programming tools provided by Parallax at no charge, con- sisting of Spin (a high-level language) and PASM (the Propeller Assembly language), no other software is needed. The few extra hardware items needed are listed in Appendix B. The work that we will undertake will be designed around the creation of software for running the type of devices you might use in the design of day-to-day projects on the hobby bench, the engineering laboratory, or the industrial research facility. The devices selected are inexpensive and fit within the constraint of working well with beginners who are just learning how to use the Propeller chip. None are hard to use. As mentioned previously, Parallax provides two languages for programming the Propeller chip. The first language is a high-level language called Spin. The Spin lan- guage can be used for almost everything we need to do, except for tasks that need extreme speed and therefore have to be handled with some sort of Assembly language constructs. All the work in this book will be done in the Spin language, with minor calls to Assembly language routines if and when necessary. The goal is to become familiar with the Spin language and to be aware of the capabilities that Assembly language provides. Assembly language routines can be embedded in the Spin lan- guage programs with minimal effort. In that this book addresses the needs of begin- ners, we will not do any programming in the Assembly language PASM. A large part of this book is dedicated to getting an understanding of how the Spin language is used to manage an eight-processor system and its shared memory. A number of simple rules are formulated to allow you to do this from a beginner’s point of view. Starting and stopping cogs and assigning specific tasks to them is covered to give you a feel for how you might use these functions. Because there are only eight cogs on the chip, we have to have some discipline regarding how they are to be used. Because each cog is also capable of performing more than one task, as is any “run of the mill” processor, we also need some understanding of when and why more than one task should be assigned to one processor. This, too, is discussed so that you can assign tasks in a more logical manner when you design the hardware and software for your projects. The Spin language does not support an interrupt capability of any description. Having eight processors in a parallel configuration pretty much eliminates the need for interrupts. However, there are still times when you may need to assign some form of more immediate attention to a task, and techniques that can be used to achieve this are demonstrated. Attention is also given to the special features each cog supports. Of advanced interest is the use of the two counters provided in each cog and the interesting ways in which they can be used. The use of these counters is not obvious to the first-time user, especially so if he is a beginner. These counters provide an important and powerful function within each cog, and using them effectively is an important part of using the Propeller chip. Because there is a total of 16 of these counters in the Propeller, they provide a resource we cannot ignore if we are to consider ourselves as being familiar with the Propeller system. Parallax provides detailed information on using these counters, but most of it is beyond the understanding of beginners. We will use a counter to create a PWM signal when we need one; no other use is covered. Part III of the book is devoted to the projects. I have concentrated on controlling the types of things beginners will be interested in. I have covered some of these tasks in other books I have written, and other authors have written a lot about these topics as well. The difference in this resource is that we concentrate on how to undertake these tasks with the Propeller chip and its parallel-processing environment in a way that’s interesting to the beginning programmer. Controlling things also has a lot do with process control, so in a way this book is a simplified introduction to process control in the parallel environment. The one thing that the Propeller chip does not do well is handle large amounts of data simultaneously. Such data crunching requires three basic resources: ■ A fast processor to perform the work quickly. (This we have.) ■ The implementation of a standardized, verified math package in the software to allow for sophisticated mathematical manipulations. ■ A large amount of memory to store all the data to be crunched. The Propeller chip does not support all three of these capabilities and implemen- tations. This does not mean that we cannot do the day-to-day math we need for our control operations; however, it does means that we are not working with a machine designed for crunching large number arrays. Consequently, there is no discussion of problems that require sophisticated mathematical capabilities in this resource.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.