Discover millions of ebooks, audiobooks, and so much more with a free trial

Only €10,99/month after trial. Cancel anytime.

Running Small Motors with PIC Microcontrollers
Running Small Motors with PIC Microcontrollers
Running Small Motors with PIC Microcontrollers
Ebook656 pages4 hours

Running Small Motors with PIC Microcontrollers

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Program PIC microcontrollers to drive small motors

Get your motors running in no time using this easy-to-follow guide. Detailed circuit diagrams and hands-on tutorials show you, step by step, how to program PIC microcontrollers to power a wide variety of small motors. You'll learn how to configure all the hardware and software components and test, troubleshoot, and debug your work. Running Small Motors with PIC Microcontrollers is filled with more than 2,000 lines of PicBasic Pro code you can use right away.

Use PIC microcontrollers to control all kinds of small motors, including:

  • Model aircraft R/C servos
  • Small DC motors
  • Servo DC motors with quadrature encoders
  • Bipolar stepper motors
  • Small AC motors, solenoids, and relays
LanguageEnglish
Release dateAug 24, 2009
ISBN9780071633529
Running Small Motors with PIC Microcontrollers

Read more from Harprit Singh Sandhu

Related to Running Small Motors with PIC Microcontrollers

Related ebooks

Telecommunications For You

View More

Related articles

Reviews for Running Small Motors with PIC Microcontrollers

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Running Small Motors with PIC Microcontrollers - Harprit Singh Sandhu

    PREFACE

    How It Happened

    In 1995, I put in the public domain an outline of what would have to be added to the Meccano and Erector set systems to allow aspiring young engineers to make sophisticated automatic machines of all kinds with these systems. These systems provide almost everything imaginable in the way of mechanical components and just about nothing in the way of electronics. Adding electronics would change everything. Everything! I imagined the electronic engineers would take it from there and soon there would be a comprehensive electric/electronic system we could all use. To say I was wrong would be more than an understatement.

    I had lots of correspondence from enthusiasts all over the world telling me what a great thing this would be, but no one seemed interested in providing what was needed. If this was going to happen, it was up to me and I was going to have to learn how to do it! Since I was then employed full time, I did not have the time to create this system. However, I am now retired and have taught myself what is needed to run motors with microcontrollers. In this book I share what I have learned with you. I will be putting motor amplifiers and other components on the market as I develop them. My initial work in this direction is described herein.

    If you want to take a look at what I have to say about the standard I described, it is on the Internet at www.pinecreekbay.com/harpritsan/MeccanICindex.html.

    This tutorial introduces you to the basic techniques used to run small DC, DC servo, stepper, and R/C servo motors with microcontrollers. It concentrates on using the microcontrollers made by the Microchip Corporation, with particular emphasis on the 16F877A and 18F4331 40-pin microcontrollers. It uses microEngineering Labs’ LAB-X1 board to make things easier for the experimenter, but you do not need to have the board to learn to do what needs to be done. Other MCUs and microprocessors made by other manufacturers can also be used. They are similar, and techniques similar to those developed herein are used. (Running larger motors is essentially a matter of using more powerful amplifiers; the techniques described herein for running them are the same.)

    Motor control can take on a number of forms from simple on/off control to carefully managed intricate motion profiles. The language used to control the motor can vary from assembly language and C to PICBASIC PRO. We will go through all the techniques that are suitable for this introductory text with the PICBASIC PRO language. Beginners will find that routines written in PICBASIC are much easier to understand than those written in other more primitive languages. Once you understand the basic routines, they can be written in the language of your choice. Conversion of the routines developed herein to assembly language or the C language can be undertaken by those interested in doing so with relative ease but will not be undertaken in this text.

    PIC Microcontrollers

    I selected the Microchip Technologies family of PIC microprocessors as the focus of these notes for two reasons. First, the Microchip provides the most comprehensive line of microprocessors for the kind of projects we are interested in. Second, the compiler for these processors provides almost the entire line of PICs with comprehensive support. All you have to do is tell the compiler which PIC you are using, and if the features you have been addressing in your project are available on that PIC, the compiler will do the rest. You will never have to buy another compiler if you stay with the very comprehensive Microchip Technologies family of PICs.

    Other Microprocessors

    We will be using a PIC 16F877A and 18F4331 for all experiments, but any number of microcontrollers are suitable for the task. The selection that you make will depend on the availability of suitable software support and other features that you need on the MCU for your particular application.

    THE 16F877A AND THE 18F4331

    The first part of the book concentrates on giving you a basic understanding of how a typical microcontroller works, with focus on the PIC 16F877A. Once you know how the 16F877A works you will be able to use other similar microprocessors with relative ease.

    Enough is covered about the 18F4331 to allow you to use its ability to keep track of what is going on with the standard quadrature encoder interface attached to the motor. This PIC was selected primarily so we can use its ability to keep track of the encoder counts autonomously.

    The second part of the book covers the use of the microcontrollers to run the small motors that we are interested in. (Larger motors need larger motor power amplifiers, but the control techniques are similar.) The following motors are covered:

    Model aircraft R/C servos

    Small, plain DC motors

    Servo DC motors with encoders attached

    Stepper motors (bipolar)

    Small AC motors and solenoids

    All the material is covered in a nonmathematical way so that anyone interested in learning to run motors can learn to do so with a minimal technical background.

    I could not have created this book without the patient help of Charles Leo at micro-Engineering Labs, Inc. Though I never met him, Charles answered countless e-mails from me without protest and with extreme patience (as I discovered, some of my questions were not the most enlightened).

    Should you discover errors in the tutorial, I would appreciate receiving an e-mail description of the error so that I can make the necessary corrections.

    I handle all customer support personally, and you are welcome to e-mail me with relevant questions, comments, and corrections. You can contact me at harprit.sandhu@gmail.com.

    —HARPRIT SINGH SANDHU

    Champaign, Illinois

    Internet support sites: Encodergeek.com and

    www.mhprofessional.com/sandhu

    Part I

    Microcontrollers

    We need to understand what one specific microcontroller can do in some detail so we can use it effectively to control motors.

    1

    INTRODUCTION TO MICROENGINEERING LABS’ LAB-X1 EXPERIMENTAL BOARD

    A vast array of PIC (Peripheral Interface Controller) microcontrollers is manufactured by the Microchip Technology Corporation of Tucson, Arizona. Microchip has shipped over ten billion of their devices all over the world. They are everywhere. Learning to use them is both easy and enjoyable and will serve you well if you are a student, a hobbyist, or an engineer or if your work involves the use of microcontroller-based devices.

    This tutorial is designed to introduce you to these devices as they apply to running motors. I intend to do this in a nonintimidating way for the technically inclined who are not necessarily electronic technicians or electrical engineers.

    We need to have a comprehensive understanding of and familiarity with at least one microcontroller in the rather large family of PIC microcontrollers if we are going to use them for the sophisticated control of all sorts of motors. I picked the PIC 16F877A because it provides almost all of the many features found in microcontrollers that are made by the many suppliers of these small yet comprehensive logic engines.

    As novices, if we want to get familiar with running motors with microcontrollers, we need an easy to use yet sophisticated and versatile board to play with and test our ideas on. Though of course it is possible to design and build a board that would do that, we do not have the expertise to do that at this time. I selected the very popular LAB-X1 and the related PICBASIC PRO compiler software as the basic platforms for the projects and ideas presented in this book. As you go through the book, you will find that the system provides an easy to use and versatile platform for checking out your hardware and software ideas before committing to printed circuits, wire, and solder. microEngineering Labs, Inc., the manufacturers of the LAB-X1 board, maintain a very useful and helpful web site (www.microchip.com) that will be a tremendous aid for you as you learn about your LAB-X1 in particular and the Microchip Technology Corporation PIC microcontrollers in general. Their web site contains a large number of example programs, tutorials, and other technical information that will help you get started with using these microprocessors. There are also a large number of other web sites that are dedicated to the support of PIC microcontrollers.

    This book supplements the information on the Internet from the microEngineering Labs site and from other sources. We will use the sample programs (modified for clarification as may be necessary) and other information that is on the web. The book provides extensive diagrams in a format that you can use to help you design your own devices, with minor modifications, based on what you learn.

    There are two basic aspects of PIC microcontrollers: hardware and software. The LAB-X1 board is designed to provide you with the hardware platform you need to conduct your first software and hardware experiments with PIC microcontrollers, specifically the 40-pin family subset. The PICBASIC PRO (PBP) compiler, provided by the manufacturers of the board, programs this and similar microprocessors and is easy to use and powerful; the code created is fast and efficient.

    If you have a serious budgetary constraint, the software for use with this board is the Basic Compiler from microEngineering Labs. This compiler is available for about $100 (in 2009), but I don’t recommend it for serious work.

    On the other hand, if you have a serious interest in using PIC microcontrollers, especially if you will be using them for a long time, I recommend the PICBASIC PRO compiler because it gives you the comprehensive power and ease of use that you need to rapidly perform useful everyday work. The PRO compiler is available for about $250 (in 2009), and all the software discussed in this workbook was written for the PICBASIC compiler. A listing of instructions and keywords provided with each compiler is provided in Chapter 4.

    You can get a free, limited copy of the PBP (picbasic pro) compiler on the Internet on the microEngineering Labs web site. This copy contains all the instructions in the full version of PBP but is limited to 30 lines of code. Even so, it can be used to effectively try out the powerful command structure of the language. The instructions for the language can also be downloaded from the microEngineering Labs web site at no charge. Before you make a decision about your compiler purchase, try out the free version.

    You will also need a hardware programmer to allow you to transfer the programs you write on your personal computer (PC) to your PIC microcontroller. Programmers are also available from microEngineering Labs for the parallel port, the RS232 serial port, and the USB port of your computer. These programmers make it a one-button click to transfer your program from your computer to the microcontroller and to run it without ever having to remove the MCU (micro controller unit) from the board. I recommend the USB programmer.

    The software needed to write and edit the programs before transferring them to the programmer and onto the microcontroller is a part of the compiler package. Other editors are available at no charge from a number of other suppliers. Programs can also be written in Microsoft Word and then cut and pasted into the programming software.

    The salient hardware features (with some repetition by categories listed) provided on the LAB-X1 are listed next. The following input capabilities are provided:

    A 16-switch keypad, plus a Reset switch

    Three potentiometers

    IR (infrared) detection capability, no detector provided

    Temperature sensing socket, no IC (integrated circuit) provided

    Real time clock socket, no IC provided

    Sockets for experimenting with three basic styles of one-wire memory chips

    Serial interface for RS232, IC provided

    Serial interface for RS485, no IC provided

    PC board holes are provided for other functions. See the microEngineering Labs web site for further details.

    The following output capabilities are provided:

    Ten LED bar graph with eight programmable LEDs

    2-line x 20-character LCD display module

    A piezo speaker/beeper

    DTMF (dual-tone multi-frequency) capability (digital tones used by the phone company)

    PWM (pulse width modulation) for various experiments

    IR (infrared) transmission capability, no LED provided

    Two hobby radio control servo connectors, no servos provided

    As mentioned, sockets for experimenting with:

    Serial memories

    A to D conversion with 12-bit resolution

    Real time clocks

    The following I/O interfaces are provided:

    RS232 interface

    RS485 interface, socket only (the interface chip is inexpensive and easy to obtain)

    You can investigate the use of the following three types of serial EEPROMs:

    I2C

    SPI

    Microwire

    The following miscellaneous devices are also provided:

    Reset button

    5-volt regulator

    40-pin ZIF (zero insertion force) socket for PIC micro MCU (the recommended PIC 16F877A IC is not provided)

    Jumper selectable oscillator from 4 MHz to 20 MHz

    In-circuit programming/debug connector

    Prototyping area for additional circuits

    16-switch keypad

    Socket for RS485 interface (device not included)

    Socket for I2C serial EEPROM (device not included)

    Socket for SPI serial EEPROM (device not included)

    Socket for Microwire serial EEPROM (device not included)

    Socket for real time clock/serial analog to digital converter (devices not included)

    Socket for Dallas 1620/1820 time and temperature ICs (devices not included)

    EPIC (Epic is a trade mark of microEngineering Labs, they give no explanation) in-circuit programming connector for serial, USB or parallel programmer

    A small prototyping area for additional circuits

    All in all, it’s a very comprehensive, well thought out, and useful experimental platform. The board is available assembled, as a kit, or as a bare PCB; see Figure 3.1. The board is 5.5″ × 5.6″.

    As already mentioned, not all the features I mentioned here are completely implemented, but sockets or PC board pin holes are provided for all of them. You may not have to make any soldering additions to the board to use the features you are interested in, but you do have to purchase the additional IC chips if you want to use them. The standard version of the board as shipped to you includes the following:

    The assembled board

    Software diskette, which includes:

    PDF schematic of LAB-X1

    Sample programs

    Editor software

    The 40-pin PIC microcontroller is not included. As received, the board is configured to run a 4 MHz, but it can go up to 20 MHz.

    THE MICROCONTROLLER

    The PIC 16F877A microcontroller (which is a necessary component on the board) is not provided because each of the compatible PIC microprocessors available has varying features, and you need to select a unit that suits the application that you have in mind. We will be using the recommended PIC 16F877A and 18F4331 microcontrollers for all our experiments. If you want to use a different processor, be sure to check for pin-to-pin compatibility on the web. Data sheets can be downloaded for all the microcontrollers at no charge from the Internet. The commonly used 40-pin for pin-compatible MCUs are the 16F873, 16F874, 16F876, 16F877, 18F4331, and 18F4431. They share similar power and pinout layouts but exhibit different capabilities. Other PICs may also be used.

    Figure 1.1 The 40-pin 16F877A PIC microcontroller.

    The following 40-pin PICs will work in the LAB-X1

    SOFTWARE COMPILER

    The PICBASIC PRO BASIC software compiler provided by microEngineering Labs provides the functions needed to control all aspects of the hardware provided by Microchip Technologies as a part of their large PIC offering. All the functions available on the PIC 16F877A microcontroller that we will be using are accessible from the software. The PICBASIC software will write software for almost the entire family of PIC microcontrollers. You will be able to use this compiler for all your future projects; it is a very worthwhile investment.

    ADDITIONAL HARDWARE

    The following hardware can be added to the LAB-X1 without making any modifications to the board. These hardware items fit into sockets or onto pins that are provided on the LAB-X1 as shipped. Not all devices can be mounted simultaneously because some addresses are shared by the sockets provided. In our experiments, we will populate only one of the empty sockets at a time, to make sure that there are no conflicts. (There is no need to use more than one device at one time for any one experiment so this will not be a problem.)

    Memory chips:

    I2C memory chip

    SPI memory chip

    Microwire memory chips:

    12 bit A to D converter chip

    NJU6355

    Real time clock chips:

    DS1202

    DS1302

    LTC1298

    Thermometer chip:

    DS1802

    Serial interface chip:

    RS485

    RC servos:

    Two hobby R/C servos can be controlled simultaneously; not provided.

    The LAB-X1 provides two sets of pins for the R/C servos. All standard model aircraft servos can be used and you can use either one or two of them. (Using these is essentially an exercise in creating pulse width modulated signals and profiles that are used in the R/C industry.)

    40-PIN DEVICES

    All 40-pin MCUs provided by Microchip can be accommodated in the 40-pin ZIF socket provided on the board. Check for compatibility with the pin layout before selecting and buying your MCU. The recommended PIC 16F877A that we are using is an excellent choice for learning if you have no specific use in mind.

    We will also be using the 18F4331 for the experiments needing encoder interfacing with the microprocessor. This chip has the ability to keep track of the encoder position automatically, which is a very useful property for our purposes.

    BREADBOARDING AND EXPANSION

    All 40 pins of the MCU have been provided with extra predrilled PC board holes. These can be used to extend the signals from these pins to an off board location for experimentation. The extensions are easily made with standard 0.1 inch on center pins and matching cables and headers.

    A small breadboard space is provided on the LAB-X1 itself to allow the addition of a limited number of hardware items that you may need to experiment with.

    See the Internet support web site www.encodergeek.com for availability of ready-made headers and cables and so on for use with the LAB-X1.

    SPECIAL PRECAUTIONS AND NOTES OF INTEREST

    The following caveat could have been placed later in the book but is included here to encourage you to select the programmer best suited to your needs.

    Pin B7 on the LAB-X1 is connected to a programming pin on the EPIC parallel programmer at all times, and the programmer forces this pin high. If you are using this pin in your experiment and you need to have it be low, you must disconnect the EPIC programmer to release the pin. The major benefit of using the parallel programmer is that it frees up your computer’s serial port for communications with the LAB-X1, but if you are using a USB programmer, it can be left connected to the LAB-X1 at all times. This is the reason I recommend the USB programmer.

    Resistor R17, which is connected to the keypad, is of no consequence to the operation of the LAB-X1. It is needed for some PIC programming functions and can be ignored for our purposes.

    DATA SHEETS

    The hardest part of using these microcontrollers is understanding the huge data sheets—often 400 pages or so. Since each data sheet is similar but different from every other data sheet, you are advised to select one or two microcontrollers to get familiar with and use them for all your initial projects. In this workbook the three that are discussed are the PIC 16F84A (this chip will not fit in the 40-pin socket provided but is a good alternate choice) for your small projects and the PIC 16F877A for larger, more comprehensive projects. Each of these uses flash memory and can therefore be programmed over and over again with your programmer and a programming socket. The processor you select will be determined by the kind of I/O and internal features that you need and the availability of inexpensive OTP (one-time programmable) equivalents if you are going to go into production. We will use the 18F4331 also but only for the encoded motor experiments.

    A lot of the information in the data sheets is more complicated and detailed than we need to worry about; we can do a lot of useful work without understanding it in every detail. For example, the timing diagrams and other data about the internal workings of the chips are beyond what we need to understand at the level of this book. Our main interest should be in what the various registers are used for and how to use them properly and effectively, as well as being able to set the various registers in the system so that we can activate the features we need for each particular project. Understanding timers and counters is a part of this. The entire interaction of the microcontroller with its environment is determined by the I/O pins and how they are configured, so knowing how to configure the I/O competently is very important.

    The data sheets are available as PDF (portable document format) files on the Internet from the microEngineering Labs web site or from the Microchip web site. Download these onto your computer for immediate access when you need them. Keeping a window open specifically for this data is very handy, but you will also want to print out some of the information to have it in your hands.

    The areas of the data sheet that support our needs are the following:

    1. Understanding and becoming familiar with what has already been defined by the compiler software as it relates to the software

    2. Getting familiar with the addressing and naming conventions used in the data sheet

    3. Understanding the use of the various areas of memory on the MCU

    4. Learning how to assign and use the I/O pins to your best advantage

    5. Understanding how to use the PBP software to its best advantage and writing programs that are as fast as possible

    6. Getting familiar with the register naming conventions and usage.

    ANOTHER INTERESTING BOOK

    David Benson of Square 1 Electronics wrote a very interesting and useful book on the PIC 16F84A called Easy Microcontrol’n (this book used to be called Easy PIC’n) that supports these investigations. It taught me a lot of things I did not know and had not even thought about. In this workbook you will reap some of the benefits of my learning experience. I recommend that you get a copy of Easy Microcontrol’n to support your use of the PIC 16F84A. It has a lot of very useful information in it and will save you a lot of time and headaches. However, the book is comparable to a first course at the community college level, and I found it too dry, with the emphasis on doing things without a BASIC compiler. A BASIC compiler is the easy to use tool of choice in this workbook because of our interest in getting things done in a hurry as opposed to becoming PIC/MCU experts in assembly language. The emphasis here is more in applied results rather than rigorous foundation level learning of assembly language programming. This does not in any way negate the usefulness of Benson’s book to those interested in understanding and using the PIC 16F84A and similar microcontrollers.

    Caution All the programs in Benson’s book are in Assembly Language.

    A FAST INTERNET CONNECTION IS A MUST

    You absolutely must have an Internet connection because so much of the information you need is on the Internet. It is very helpful to have more than a standard phone line connection so get the fastest connection you can afford. A cable modem is strongly recommended. If you and a couple of neighbors can get together and form a local area network (LAN) and share a wireless (Wi-Fi) modem setup, it becomes a really inexpensive way to get fast Internet service. The Wi-Fi signals have no problem reaching all the apartments in a small building and sometimes even the house next door. Amplifiers and repeaters are available to increase signal strength where necessary.

    DOWNLOADING DATA SHEETS

    One of the first things you need to download is the data sheets for the PIC 16F87X. You will in all probability end up using the smaller and less expensive PIC 16F84A for a lot of your initial projects, so it might be a good idea to download the information for that microcontroller while you are at it. As mentioned before, these files are available from the Microchip web site and the information is free.

    Enjoying the preview?
    Page 1 of 1