BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

Using PicBasic with the PIC16F84 PIC Microcontroller

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.


Figure 1:  Using the PIC16F84 and PicBasic to create a serial I/O-expander, single-chip solution.

To control our serial I/O-expander, we only need to send it a three-byte string of serial data.

bullet

The qualifier number 254.

bullet

The relay number we want to address & control.

bullet

The logic state we want the relay output-pin to go to.

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:

The BASIC Stamp II Code

 '' ====== 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

'**************************************************************** '* Name : PC-RELAY2.BAS * '* Author : Bruce Reynolds * '* Notice : Copyright (c) 2002 Reynolds Electronics * '* : All Rights Reserved * '* Date : 7/31/2002 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** ' Using the PIC16F84 PIC For I/O Expansion with ' the BASIC Stamp, or PC serial port INCLUDE "bs2defs.bas" relay VAR b3 'relay number storage variable stat VAR b4 'relay status ON/OFF variable serpin VAR porta.4 'serial input pin trisa = %00010000 trisb = %00000000 loop: SERIN serpin,N2400,[254],relay,stat 'serial data in on PortA.4 IF relay = 1 THEN outr1 ' if request is for relay#1 then goto relay#1 routine IF relay = 2 THEN outr2 ' if request is for relay#2 then goto relay#2 routine IF relay = 3 THEN outr3 ' if request is for relay#3 then goto relay#3 routine IF relay = 4 THEN outr4 ' if request is for relay#4 then goto relay#4 routine IF relay = 5 THEN outr5 IF relay = 6 THEN outr6 IF relay = 7 THEN outr7 IF relay = 8 THEN outr8 IF relay = 9 THEN outr9 IF relay = 10 THEN outr10 IF relay = 11 THEN outr11 IF relay = 12 THEN outr12 GOTO loop outr1: IF stat = 1 THEN high1 ' If status request is I/O pin#0 logic 1 [high] LOW 0: GOTO loop ' then make I/O pin#0 high, else make it [low] high1: HIGH 0: GOTO loop ' Make I/O pin#0 logic 1 [high] outr2: IF stat = 1 THEN high2 LOW 1: GOTO loop high2: HIGH 1: GOTO loop outr3: IF stat = 1 THEN high3 LOW 2: GOTO loop high3: HIGH 2: GOTO loop outr4: IF stat = 1 THEN high4 LOW 3: GOTO loop high4: HIGH 3: GOTO loop outr5: IF stat = 1 THEN high5 LOW 4: GOTO loop high5: HIGH 4: GOTO loop outr6: IF stat = 1 THEN high6 LOW 5: GOTO loop high6: HIGH 5: GOTO loop outr7: IF stat = 1 THEN high7 LOW 6: GOTO loop high7: HIGH 6: GOTO loop outr8: IF stat = 1 THEN high8 LOW 7: GOTO loop high8: HIGH 7: GOTO loop outr9: IF stat = 1 THEN high9 porta.0 = 0: GOTO loop high9: porta.0 = 1: GOTO loop outr10: IF stat = 1 THEN high10 porta.1 = 0: GOTO loop high10: porta.1 = 1: GOTO loop outr11: IF stat = 1 THEN high11 porta.2 = 0: GOTO loop high11: porta.2 = 1: GOTO loop outr12: IF stat = 1 THEN high12 porta.3 = 0: GOTO loop high12: porta.3 = 1: GOTO loop 
Download PicBasic .HEX Code
bullet
Compiled for PIC16F84-04/P: 
PCRELAY2.txt Save the file then rename it PCRELAY.HEX before programming the PIC.


Figure 2:  Example Relay Hookup Diagram

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.

Get PicBasic:

bullet

Click HERE to purchase PicBasic Compilers - and hardware.

bullet

Click HERE to learn more about the PicBasic compiler.

bullet

Click HERE to return to the PicBasic projects page.

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:

bullet Check out our new PicBasic Project Section for a large variety of complete projects.

Copyright © 1999-2008 Reynolds Electronics

| Contact Information |