This article shows how to use the PIC16F84 microcontroller with PicBasic to design a quick & effective, serially controlled, I/O-Expander. Using PicBasic makes developing handy single-chip devices like this extremely simple.
If you're spending all your time programming in assembler, you'll appreciate how simple PicBasic is to use, and how easily you can create single-chip solutions like this one in mere minutes.
This project uses only a few of the instructions that come with PicBasic, but serves to show how easy PicBasic really is. It also shows how PicBasic strongly resembles programming the BASIC Stamp. Here we are using the serin command, and a couple if then statements to design a simple serially controlled I/O-expander that can be used to increase the I/O capabilities of other PIC microcontrollers or the BASIC Stamp.
One feature that PicBasic offers with the serin command is the ability to use a qualifier. Using this technique, we can force our serial I/O-expander to wait for a specific qualifier before accepting other data from the serial-pin. This helps to ensure that our I/O-expander isn't false triggered by line noise, or other interference. It also helps us to synchronize the incoming data and make sure we receive everything in the correct order.
The code for this project waits for the qualifying number 254 to arrive on the serial-pin. Once the qualifier is received, the program will then accept the remaining data, and use this information to control the output-pins.
The code for this project was kept as simple as possible to help beginners understand how it works. If you're already an experienced programmer, you may find the simplicity of the idea to be a starting point for much larger, and more sophisticated designs with a similar purpose for your own applications down the road.
Note: This project can be used with the PC serial port, the BASIC Stamp, and other PIC microcontrollers. The ability to (directly) interface the PIC microcontroller to the PC serial port eliminates the need for expensive RS-232 conversion ICs such as the MAX-232. A single 22K resistor is all that's required to complete the PC interface.
To control our serial I/O-expander, we only need to send it a three-byte string of serial data.
You can use this project as a simple I/O-expander, or to control up to 12 individual relays directly interfaced to your PC, a BASIC Stamp, or just about any type of microcontroller capable of sending asynchronous serial data. It has quite a few uses for many various applications, and it's so incredibly simple to build, it can easily replace much more expensive solutions.
Using the I/O-expander with the BASIC Stamp II is very simple. We use the serout command to send the following data to the I/O-expander:
'' ====== Program to exercise the PIC16F84 serial relay module ====== ' This program sends serial data out pin 0 of the BASIC Stamp II ' or the Wedge prototyping board to exercise the PIC16F84 I/O ' expander/relay control module. Data is sent 2400,N,8,1 through ' a 22K resistor in series with pin RA.4 [the serial input pin] ' of the I/O expander/relay control module. time var byte ' Change byte to "word" for longer delay times. synch con 254 relay var byte stat var byte loops var byte time = 250 start: for relay = 1 to 12 stat = 1 serout 0,16780,[synch,relay,stat] pause time stat = 0 serout 0,16780,[synch,relay,stat] time = time - 1 if time = 0 then reload next goto start reload: time = 250 goto start
The individual outputs are simply addressed as 1 through 12. The logic states are a 1 or 0 depending on the logic state you want the output-pin to go to. The BASIC Stamp II sample code above will exercise the I/O-expander very quickly by sending a series of commands to the I/O-expander causing the output-pins to toggle from 1 to 0 extremely fast.
The pause is only 250mS to start with. The line time = time - 1 quickly decrements the remaining pause time down to 0. This is a fun program to run with this project, and it shows how fast the PIC can operate without missing incoming serial data. The program will cycle through all 12 I/O-pins, and return to the beginning once finished. To test your circuit, hookup 12 LEDs to the output-pins of your I/O-expander through 470 ohm series resistors and watch it blaze through the count.
The PicBasic Code
Download PicBasic .HEX Code
Figure 2 shows how to hookup relays to the PIC16F84. Don't leave out the fly-back protection diode. This diode will save you from damaging your drive transistor when the magnetic field of the relay coil collapses after it's been de-energized.
If you need a total of 12 relays, just hookup one of these relay circuits to each of the 12 output-pins shown in the schematic at the top of the page. If you only need a couple relays, just hookup as many as you need. The project will work just fine with up to 12 relays.
Note: Be sure your power supply has enough current capacity to handle the total load of (all) relays being energized at the same time. If you experience problems or erratic behavior with this circuit, check for power loss when all relays are energized. Be sure your power isn't dropping below the minimum operating requirements of the PIC16F84 and causing the PIC to shut-down during operation.
Until the next project using PicBasic, have fun....... If you don't have PicBasic, what the heck are you waiting for...? Click HERE to get in on the action....!
More Projects using PicBasic:
Copyright © 1999-2008 Reynolds Electronics