Making PIC-based instruments and controllers has to do with learning how to combine the various capabilities that single-chip microcontroller units provide and utilizing a coherent set of interactive functions that serve the purposes we have in mind.
A vast array of PIC microcontrollers is manufactured by the Microchip Technology Corporation of Tucson, Arizona. This tutorial is designed to introduce you to just one of these devices—and do so in a non-intimidating way, if you are technically inclined but not necessarily an electronic technician or electrical engineer. Getting to know this one device will expose you to most of the features provided in this entire family of devices and make you comfortable with selecting and using the device that most closely meets your needs.
The device I have selected is the PIC 16F877A microcontroller. This is a 40-pin device that contains most of the features you are likely to find in the family of microcontrollers that Microchip Technology manufactures. In general, the salient features we need to get comfortable with can be described as dealing with the following:
Input capability
Output capability
Mathematical manipulations within 8/16-bit math
Using timers
Using counters
Communications with digital and analog devices
Dealing with displays
Interacting with personal computers
The PIC 16F877A will let us get familiar with all the preceding items and more, and is an excellent choice for the first-time serious user. This particular MCU (microcontroller unit) is also very popular with robotic enthusiasts. A number of boards that incorporate this device are available in the general marketplace and on the Internet.
As novices, if we want to get familiar with the concept of building instruments and controllers based on PIC microprocessors, we need an easy-to-use yet sophisticated and versatile board to play with and test our nascent ideas on. Though, of course, it would be possible to design and build a board that would do that, we do not have the expertise or time at this juncture to do so. Therefore, I have selected the very popular LAB-X1 and the supporting PICBASIC PRO Compiler software as the basic platforms for the projects and ideas presented in this book. As you read this text, 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 personal computer (PC) boards, wire, and solder.
MicroEngineering Labs, Inc., the manufacturers of the LAB-X1 board and the related PICBASIC PRO Compiler, maintain a very useful and helpful Web site that will be a tremendous aid to 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 to help you get started using their boards. A large number of Internet-based Web sites also exist, which are dedicated to the use of PIC microcontrollers. You should bookmark those pertaining to your area of interest.
This resource supplements the information on the Internet both from the micro-Engineering Labs, Inc. site and from other sources. We will use the sample programs (modified for clarification as may be necessary) and other information on the Web in this book. This book provides extensive diagrams that will help you in designing your own devices based on what you will learn. The diagrams are on the support Web site as AutoCAD files and can be expanded on for your particular designs.
There are two basic aspects in familiarizing yourself with PIC microcontrollers—the hardware aspect and the software aspect. 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. The PICBASIC PRO Compiler, provided by the manufacturers of the board to program the 16F877A and similar microprocessors, is both easy to use and powerful, and the code created is fast and efficient. Other compilers are available but not covered in this book.
If you have serious budget constraints, the software of choice for use with this board is the smaller Basic Compiler from microEngineering Labs. This compiler is available for about $100 (in 2008) and is not recommended by me for serious work. (A free copy of the PBP compiler is available on the microEngineering Labs Web site, which 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 be downloaded from the microEngineering Labs Web site at no charge. Before you make a decision in either direction, be sure to try out the free version of the compiler.)
On the other hand, if you have a serious interest in using PIC microcontrollers, the compiler I recommend is the PICBASIC PRO Compiler because it gives you the comprehensive power and ease of use you need to do useful, everyday, professional work rapidly. The PRO Compiler is available for about $250 (in 2008) and all the software discussed in this workbook was written with the PICBASIC PRO Compiler in mind. A comparative listing of the keywords provided with each compiler is provided in Chapter 4 on software and editors.
You will also need a hardware programmer that lets you transfer the programs you write on your PC to your PIC microcontroller. Programmers are 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 proposition” to transfer your program from your computer to the microcontroller and run it without ever having to remove the MCU from the board. (The USB programmer is recommended by me.)
The editing 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. None is a better choice than the editor provided.
The salient hardware features (with some repetition by categories listed) provided on the LAB-X1 are as follows:
The following input capabilities are provided:
A 16-switch keypad
A reset switch
Three potentiometers
IR (infrared detection capability) (no detector provided)
Temperature sensing socket, (no IC 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 is provided)
Serial interface for RS485 (no IC provided)
PC board holes are provided for other functions. See the microEngineering Labs, Inc. Web site.
The following output capabilities are provided:
Ten-LED bargraphs with eight programmable LEDs
2-line × 20-character LCD display module
A piezo speaker/horn
DTMF 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 hobby servos provided)
Sockets for experimenting with (also mentioned earlier)
Serial memories
A-to-D conversion with 12-bit resolution
Real-time clocks
The following I/O interfaces are provided:
RS232 interface with IC
RS485 interface, socket only (The 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:
A reset button
A 5-volt regulator
A 40-pin ZIF socket for PIC micro MCU (the recommended PIC 16F877A IC is not provided)
A jumper selectable oscillator from 4 MHz to 20 MHz
An in-circuit programming/debug connector
A prototyping area for additional circuitry
A 16-switch keypad
A socket for RS485 interface (device not included)
A socket for I2C serial EEPROM (device not included)
A socket for SPI serial EEPROM (device not included)
A socket for Microwire serial EEPROM (device not included)
A socket for real-time clock/serial analog to digital converter (devices not included)
A socket for Dallas 1620/1820 time and temperature ICs (devices not included)
An EPIC in-circuit programming connector for serial, USB, or parallel programmer
All in all a very comprehensive, well thought out, and useful experimental platform suitable for our investigations.
The board is available assembled, as a kit, or as a bare PCB. The board is 5.5 inch × 5.6 inch.
Not all the features I have mentioned here are completely implemented, but, as I stated, 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 are interested in their use. The standard version of the board as shipped to you includes the following
The assembled board
Software diskette
PDF schematic of LAB-X1
Sample programs
Editor software
Additional support information (on the Web site)
(The 40-pin PIC microcontroller is not included.)
As received, the board is configured to run at 4 MHz.
The PIC 16F877A microcontroller (which is a necessary component on the board) is not provided, because each of the compatible PIC microprocessors available have varying features and you may want to select a unit that suits the application you have in mind. We will be using the recommended PIC 16F877A microcontroller for all our experiments. If you want to use another processor, be sure to check it for pin-to-pin compatibility with the LAB-X1 board on the Web. Datasheets can be downloaded for all the microcontrollers at no charge from the Internet. A large number of PICs can be used in the LAB-X1. See the list at the end of this chapter.
THE SOFTWARE COMPILER
The PICBASIC PRO BASIC software compiler (to be purchased separately), produced by microEngineering Labs, offers the functions needed to control all aspects of the hardware provided by Microchip Technology as a part of their large PIC offering. All the functions available on the PIC 16F877A microcontroller we will be using are accessible from the software. The PICBASIC software will create programs for almost the entire family of PIC microcontrollers. You will be able to use this compiler for all your future projects if you stay with the Microchip MCUs. (It is, all in all, a very worthwhile investment.)
ADDITIONAL HARDWARE
The following hardware can be added 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 in that some addresses are shared by the sockets provided. In our experiments, we will populate only one empty socket at a time in order to make sure no conflicts arise. (There is no need to use more than one device simultaneously for any one experiment, so this will not be a problem.)
Memory chips
I2C memory chip
SPI memory chip
Microwire memory chip
12-bit A-to-D converter chip
NJU6355
Real-time clock chips
DS1202
DS1302
LTC1298
Thermometer chip
DS1802
RS485
RC servos (Two hobby R/C servos can be controlled simultaneously.)
The LAB-X1 provides two sets of pins for the R/C servos. All standard model aircraft servos can be employed and you can use either one or two servos. (Using these is essentially an exercise in creating pulse width modulated signals and profiles that meet the standards used in the radio control hobby industry.)
40-PIN DEVICES
All 40-pin MCUs offered by Microchip can be accommodated in the 40-pin ZIF socket provided on the board. (Check for compatibility with the pin layout before selecting/buying your MCU. See Figure 1.1. The recommended PIC 16F877A we are using is an excellent choice for learning.)
BREADBOARDING AND EXPANSION
All of the MCUs 40 pins (on the LAB-X1) have been provided with extra PC board holes that can be used to extend the signals from these pins to an off-board location for further experimentation. The extensions are easily made with standard 0.1-inch on-center pins with matching cables with headers.
Figure 1.1 Pin out designations for the 40-pin 16F877A PIC microcontroller.
A small breadboard space is provided on the LAB-X1 itself to allow the addition of a limited number of hardware items you may need to experiment with.
See the support Web site regarding the availability of readymade headers and cables and so on, for use with the LAB-X1. (These are the devices I needed and made up to allow me to experiment with the 16F877A when the circuitry I required was not available on the LAB-X1.)
These caveats could have been placed later in this book but are included here to encourage you to think about the programmer best suited to your needs.
Pin B7 on the 16F877A 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 decide it must be low, you must disconnect the EPIC programmer to release this pin. If you are using a serial or USB programmer, it can be left connected to the LAB-X1 at all times. The major benefit of using the USB or parallel programmer is that it frees up your computer’s serial port for communications to the LAB-X1.
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).
The hardest part of learning how to use these microcontrollers is understanding the huge datasheets. Since each datasheet is similar but different from every other datasheet, you are advised to select one or two microcontrollers to get familiar with and then use them for all your initial projects. In this workbook, the two discussed/mentioned are the PIC 16F84A (this chip will not fit in the 40-pin socket provided but is a good alternate choice for the cost-conscious), 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 you need, and the availability of inexpensive OTP (one time programmable) equivalents if you plan to go into production.
A lot of the information in the datasheets is more complicated and detailed than we need to worry about at this time, and we can do a lot of useful work without understanding it in every detail. Our main interest is in what the various registers are used for and how to use them properly and effectively. 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 resource book. Our interest is in being able to set the various registers in the system so 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 is very important.
The datasheets are available as PDF (page description format) files on the Internet from the microEngineering Labs Web site or from the Microchip Technology Web site. These should be downloaded onto your computer for immediate access when you need them. Keeping a window open specifically for this data is very handy. Even so, you will want to print out some of the information to have it in “in your hands.”
Of particular interest are the areas of the datasheets that will support our needs as they apply to the following areas:
Understanding and becoming familiar with what has already been defined by the Compiler software as it relates to the software
Getting familiar with the addressing and naming conventions used in the datasheet
Understanding the use of the various areas of memory on the MCU
Learning how to assign and use the I/O pins to our best advantage
Understanding how to use the PICBASIC PRO software effectively
Getting familiar with the general register usage as it is implemented for the control of the timers and counters
A FAST INTERNET CONNECTION IS PRETTY MUCH A MUST
You absolutely have to have an Internet connection, and it is very helpful to have more than a standard phone line connection, so get the fastest connection you can afford. You need the connection because so much of the information you need is on the Internet. 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 modem set up, 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. Relatively inexpensive 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 datasheets for the PIC16F87XA. 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 best to download the information for that microcontroller while you are at it. As mentioned before, these files are available from the Microchip Technology Web site and the information is free. However, the two documents consist of about 400 pages, so you probably will not want to print it all out. You will, however, want to have some of the more commonly used information printed so you can refer to it whenever necessary. The rest should be stored on your computer so you can call up or search for what you need when necessary.
The Microchip Technology Corporation Web site is www.microchip.com
Click “Support” on their Web site to find what you need. The items are easy to download. Just follow the instructions provided on the site.
Note Much of the information you will need is provided on the Web site that supports this book. But even so, it can’t replace a fast Internet connection.
The following 40-pin PICs will work in the LAB-X1 (as of June 2008). Others may work as well. Check these ICs for the features you need for your particular application, and then select the one that provides the best match.
PIC16C64(A), 16C65(B), 16C662, 16C67, 16C74(AB), 16C765, 16C77, 16C774, 16F74, 16F747, 16F77, 16F777, 16F871, 16F874, 16F874A, 16F877, 16F877A, 16F914, 16F917, 18C442, 18C452, 18F422, 18F4320, 18F4331, 18F4410, 18F442, 18F4420, 18F4431, 18F4439, 18F4455, 18F448, 18F4480, 18F4510, 18F4515, 18F452, 18F4520, 18F4525, 18F4539, 18F4550, 18F458, 18F4580, 18F4585, 18F4610, 18F4620, 18F4680
We are considering the 16F877A.
The 18F4331 family optimizes motion control for encoded motors.
Features provided on each MCU by the manufacturer vary from chip to chip. A chart of comparative features is maintained by microEngineering Labs on their Web site.