You are on page 1of 132

Ten Projects With The MBasic Compiler

INTRODUCTION _____________________________________________________ 3-4


CHAPTER 1: Project_1 GETTING STARTED _____________________________ 5-14
CHAPTER 2: Project_2 LCD ALPHANUMERIC DISPLAY_________________ 15-22
CHAPTER 3: Project_3 MOTOR CONTROL _____________________________ 23-32
CHAPTER 4: Project_4 7-SEGMENT LED DISPLAY _____________________ 33-44
CHAPTER 5: Project_5 RESISTIVE-TYPE SENSORS _____________________ 45-58
CHAPTER 6: Project_6 DISTANCE SENSORS ___________________________ 59-70
CHAPTER 7: Project_7 TEMPERATURE & FLAME SENSORS ____________ 71-80
CHAPTER 8: Project_8 VOICE RECORD/PLAYBACK DEVICE ____________ 81-92
CHAPTER 9: Project_9a &9b REMOTE CONTROL______________________ 93-112
CHAPTER 10: Project_10 MULTI-PURPOSE ROBOT___________________ 113-132

2
Ten Projects With The MBasic Compiler

INTRODUCTION
This book is designed to give the user of the MBasic Compiler a look at some of the
capabilities of the software as well as showing the easy integration with available
hardware. It will illustrate ten individual projects using the two different Development
Boards and the three different PICmicro MCUs that are included in the Basic Micro
Ultimate Combo. The Ultimate Combo Package from Basic Micro includes everything
you need to begin experimenting with the PICmicro MCUs. MBasic is an easy to use
compiler. The compiler combined with the 0818 and 2840 Development Boards and ISP
PRO Programmer make programming any PIC quick to get started and very easy to learn.

The ISP (In Circuit Programming) system allows you to test changes on-the-fly without
unplugging or switching cables around. MBasic is an advanced programming language
modeled after BASIC. The IDE (Integrated Development Environment) is MBasic. The
IDE is used to perform all the tasks associated with using MBasic, such as writing code,
compiling, and programming the target device. Everything is done from within the IDE,
so there is no reason to exit the program or use separate file editors and programming
software for your BASIC code files. The IDE allows all the pieces of MBasic to be in
one place. Instead of multiple programs, it is all integrated in one easy to use Windows
interface.

The 0818 Development Board with the ISP-PRO Programmer can program 8 pin and 18
pin PICmicro MCUs.
The 2840 Development Board with the ISP-PRO Programmer can program 28 pin and 40
pin PICmicro MCUs.
The PIC16F628 is an 18 pin MCU with 13 I/O pins and can be programmed with the 0818
Development Board.
The PIC16F876 is a 28 pin MCU with 22 I/O pins and can be programmed with the 2840
Development Board.
The PIC16F877 is a 40 pin MCU with 33 I/O pins and can be programmed with the 2840
Development Board.

3
Ten Projects With The MBasic Compiler

MBasic IDEs Main Editor provides full syntax highlighting. The Code Explorer allows
you to automatically jump to your files. With the Explorers built in filters, you can view
only the file extensions you want. The Build Window identifies compilation and
assembler errors so they can be easily corrected. It also shows program memory used,
program memory free, warnings and special messages. The Serial Terminal Window
allows you to view serial output from your PICmicro. With the Debug Window you can
send and receive data from your running program on your PICmicro. The Debug data is
passed directly through the ISP-PRO. This allows direct debugging on-the-fly without
the need for two serial ports or any type of cable swapping. With the ICD (In Circuit
Debugger), you can watch your code run line-by-line live as the PICmicro MCU executes
each instruction.

Another great thing about the Ultimate Combo Package is the great support you have
from Basic Micro. If you have any problems or questions, you can get technical support
via e-mail and the discussion forums at www.basicmicro.com So lets get started!

4
Ten Projects With The MBasic Compiler

CHAPTER 1: GETTING STARTED


(Project_1)

INSTALL SOFTWARE

The first step in programming a PICmicro is to insert the Basic Micro CD into your CD-
ROM drive on your computer. Follow the installation directions. Allow the installer to
install MBasic into its default directories. You can later change the default directories
after you completely understand the compiler and how it works. During the installation
process the installer will prompt you for the serial number located on the CD-ROM case.
The serial number must be entered in exactly as it appears. It is case sensitive. Once you
have finished installing the Compiler, restart your computer.

BOARD ENCLOSURES

It is a good idea to either buy or make enclosures for the ISP-Pro and 0818/2840
Development Boards. The enclosures will make working with these boards easier and
will also help in preventing damage to the boards. I attached the boards to some plastic
video tape boxes. It works great. When I am finished programming I can close the boxes
and stack them in a drawer for safe keeping until I need them again.

INSTALL AND CONFIGURE HARDWARE

Install the appropriate PICmicro MCU in the Development Board being used. Let’s start
with the 16F628 and the 0818 Development Board.

Connect the “straight through” serial cable to the ISP-PRO then to the com port of your
computer.

Connect the power supply (wall adapter) to the ISP-PRO.

Connect the ISP-PRO to the 0818 Development Board with the supplied cable.

Start the Basic Micro IDE. Once the IDE is running, configure the ISP-PRO serial port.
This is done from the system setup menu under Tools. Then run the hardware test to see
if the ISP-PRO is functioning correctly.

5
Ten Projects With The MBasic Compiler

Menu Bar: Tools Æ System Setup Æ Choose Com Port the ISP-Pro is attached to Æ
Hardware test.

You should receive the message “ISP-PRO communications are normal”.

WRITING YOUR FIRST PROGRAM

1. Open a new file by selecting: File Æ New Æ PICmicro Basic file Æ OK.
2. Select the PICmicro MCU as 16F628 by using the drop down menu selection.
3. Select the config setup button. Select the oscillator you will be using. Let's start with
the 10Mhz oscillator. Set it for High speed, code protect off. The remaining options
at the bottom of the menu should be unchecked except for the Watchdog timer. Then
click OK.
4. The following configuration settings should be displayed on the Editor window:

CPU = 16F628
MHZ = 10
CONFIG = 16142

5. Enter the following program under the configuration settings:

Temp var byte


Temp1 var word
Temp1 = 0

Main
For temp = 1 to 20
Temp1 = Temp1 + 10
Debug [DEC Temp1, 13]
Next
Debug [“OK”, 13]
Temp1 = 0
Goto Main

6. After the program is entered, save it. Save the file as: File name Æ Test1 / Save as
type Æ Basic file (*.bas). Click on the Debug button. The program will compile,
and then a progress bar will appear. This indicates the target device is being
programmed. Then the bottom Build window will change focus to the Debug
window.

7. Click the connect button. Once the connect button turns to disconnect, a connection
has been established. Then check the auto update button. Next click the Variable
button; another window will appear. This is the Variable Watch window. Next click
Animate.

6
Ten Projects With The MBasic Compiler

8. When the program is running, the Variable Watch window will update the status of
each variable used in the program. It will display the values in HEX, DEC, and
BINARY. The ICD will show a small yellow arrow and a green bar, indicating
where the program is at during execution.

If the program was entered correctly, Temp1 value will be displayed in the Watch
window. After the variable Temp1 equals 200, the text “OK” should appear in the Watch
window. The program should then return to the label Main and start all over again.

Congratulations! You have just written your first program, and successfully programmed
your first PICmicro, and worked with the ICD.

PROJECT_1

It is now time to attach some components on the solderless bread board. Those
components will be a button switch, a potentiometer, 4 resistors, 7 LEDs and a capacitor.
The MBasic commands used in this project are: Low, High, Pause, Goto, Button, Rctime,
If…Then, For…Next, Toggle and Debug (refer to the MBasic manual for the
explanations of these commands).

Nine of the thirteen I/O pins of the PIC16F628 will be used in this project.

Follow the below schematic in wiring the solderless bread board. Use the Jumper Wire
Kit and the components you received in your package. The only thing you need to get on
your own is the .1uf capacitor.

10k ohms Vss

A0 Vdd

A1

A2 Vss
390 ohms
A3
Green LEDs
B0
390 ohms .1uf
Vss Vdd
20k pot
B1

B2
Vss
B3 390 ohms

B5
Bi-Color LEDs

7
Ten Projects With The MBasic Compiler

8
Ten Projects With The MBasic Compiler

After you have wired up the solderless bread board, open a new file and enter the
following program. Follow the same procedure you did in writing your first program.
Once the program is entered, save the file as: Project_1.

Work var byte


Work = 0
Pot var word
Pot = 0
Counter var byte
Counter = 0

Start:
TRISA = $00
PORTA = $00
TRISB = $00
PORTB = $00
Button A0,1,100,10,Work,1,Demo1
Goto Start

Demo1:
Low A3
High A1
For Counter = 1 to 15
High B0
Pause 1
Rctime B0,1,Pot
Pot = Pot * 2
Debug [DEC Pot, 13]
High B1
Low B2
High B3
Low B5
Pause Pot
Debug [DEC Counter, 13]
Low B1
High B2
Low B3
High B5
Pause Pot
Next
Work = 0
Button A0,1,100,10,Work,1,Demo2
Goto Demo1

Demo2:
9
Ten Projects With The MBasic Compiler

Low A1
High A2
Pause 1000
Low B1
Low B2
Low B3
Low B5
Button A0,1,100,10,Work,1,Demo3
Loop:
High B0
Pause 1
Rctime B0,1,Pot
Debug [DEC Pot,13]
Pause 300
If Pot > 600 then Four
If Pot > 400 then Three
If Pot > 200 then Two
If Pot > 10 then One
If Pot < 10 then Demo2
One:
High B1
Low B2
Low B3
Low B5
Debug [DEC 1,13]
Goto Loop
Two:
Low B1
High B2
Low B3
Low B5
Debug [DEC 2,13]
Goto Loop
Three:
Low B1
Low B2
High B3
Low B5
Debug [DEC 4,13]
Goto Loop
Four:
Low B1
Low B2
Low B3
High B5
Debug [DEC 6,13]
Goto Loop

10
Ten Projects With The MBasic Compiler

Demo3:
Low A2
High A3
High B0
Pause 1
Rctime B0,1,Pot
Button A0,1,100,10,Work,1,Start
Debug [DEC Pot,13]
If Pot > 600 then FL
If Pot > 400 then SL
If Pot > 350 then ST
If Pot > 150 then SR
If Pot >= 1 then FR
FL:
Debug ["Fast Left",13]
Toggle B1
Pause 200
toggle B2
Pause 200
Toggle B3
Pause 200
Toggle B5
Pause 200
Goto Demo3

SL:
Debug ["Slow Left",13]
Toggle B1
Pause 500
Toggle B2
Pause 500
Toggle B3
Pause 500
Toggle B5
Pause 500
Goto Demo3

ST:
Debug ["Stop", 13]
Pause 300
Goto Demo3

SR:
Debug ["Slow Right",13]
Toggle B5
Pause 500
Toggle B3
Pause 500
11
Ten Projects With The MBasic Compiler

Toggle B2
Pause 500
Toggle B1
Pause 500
Goto Demo3

FR:
Debug ["Fast Right",13]
Toggle B5
Pause 200
Toggle B3
Pause 200
Toggle B2
Pause 200
Toggle B1
Pause 200
Goto Demo3

End

EXPLANATION OF PROGRAM

Project_1 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does.

Start: This routine makes all of PortA and PortB outputs, and all of the pins are
made low. The program loops forever until the button is pressed and then
the program moves to Demo1.

Demo1: This part of the program turns on the far right green LED. A counter is set
to count from one to fifteen. For each count the bi-color LEDs alternate
their colors. The rate of change of the LEDs is determined by the value of
the potentiometer (RC circuit). Adjust the potentiometer and see the
changes to the variable Pot and the change to the LED display. The RC
circuit value (times two) and the counter value will be displayed in the
Watch window. After the count reaches fifteen the program looks to see if
the button is pressed. If the button is pressed the program moves to
Demo2.

Demo2: The middle green LED turns on. The value of the potentiometer
determines which bi-color LED is activated. When the potentiometer is at
12
Ten Projects With The MBasic Compiler

max position the LEDs are off, and if the button is pressed, the program
goes to Demo3.

Demo3: The far left green LED turns on. The bi-color LEDs display in a
sequential manner to the right or left depending on the value of the
potentiometer. The change rate of the display is also determined by the
potentiometer. When the potentiometer is in mid position the
display is in stop mode. In stop mode, if the button is pressed, the
program will go back to the label Start.

CONCLUSION

Chapter 1 showed how to program the PIC16F628 and how to use some commands of the
MBasic compiler. Project_1 used the 0818 Development Board to blink LEDs in various
ways using a potentiometer and a button switch. The solderless bread board was used to
show how easy it is to connect devices to the PIC without having to solder components
together. The next chapter will be more challenging and will involve the use of an LCD
and Piezo speaker.

13
Ten Projects With The MBasic Compiler

14
Ten Projects With The MBasic Compiler

CHAPTER 2 : LCD ALPHANUMERIC DISPLAY


(Project_2)

LCD DISPLAY

Liquid crystal displays (LCDs) are a passive display technology. That means they do not
emit light; instead, they use the ambient light in the environment. By manipulating that
light, they can display characters using very little power. The LCD consists primarily of
two glass plates in parallel with a dielectric (liquid crystal material) between the plates.
The liquid crystal has two forms, a liquid form and a crystal molecule form. The crystal
molecules are rod shaped and have a definite order or pattern, and an electric field can be
used to manipulate those molecules. Many LCDs use a type of liquid crystal called
twisted nematic (TN). It is naturally twisted. Applying an electric current to these liquid
crystals will untwist them to varying degrees, depending on the current’s voltage. LCDs
use these liquid crystals because they react predictably to electric current in such a way as
to control light passage.

A simple LCD will have a mirror in back, which makes it reflective. The LCD will also
have a piece of glass with a polarizing film on the bottom side, and a transparent common
electrode plane made of indium-tin oxide on top. On top of that is a layer of the liquid
crystal. Above the liquid crystal is another piece of glass with an electrode in the shape
of a rectangle on the bottom and top. The last part is another polarizing film at a right
angle to the first one.

When the LCD is “off”, no voltage is applied to the electrodes, and light passes through
the LCD. When it is “on”, voltage is applied and the liquid crystal molecules align
themselves in the direction of the electric field. This causes the light to be out of phase
with the polarizers and to be blocked, creating a dark area on the LCD. By selectively
applying voltage to the electrodes, a variety of patterns can be achieved.

Having an alphanumeric display for your PIC microcontroller can be very helpful. An
LCD module can display output messages from the PIC, such as numeric values of a
variable or word messages to the user. The LCD module in your kit uses a Hitachi 44780
type controller and has two lines with sixteen characters per line and is organized in
blocks of 5 X 7 dots. It accepts data and command instructions over four or eight bit
parallel interface. For our uses we will use the four bit parallel interface. To operate this
display we will need six I/O lines from the PIC.

LCDWRITE COMMAND

The MBasic command LCDWRITE makes it very easy to use the LCD display. The
control commands that are used with LCDWRITE are in Table 2.1. Before you use the
LCDWRITE command, you must first initialize the LCD screen by adding the following
lines to your program:
15
Ten Projects With The MBasic Compiler

Pause 500
LCDWRITE [INITLCD1,INITLCD2,CLEAR,HOME,SCR]

The internal cursor position is set automatically. If you want to print a character at a
particular location on the screen, position the internal cursor at the specific screen ram
location you want. The screen ram is mapped out as shown in Table 2.2. To display
your message on the second line instead of the first line, you would use the following:

LCDWRITE [SCRRAM+$40]

With the Hitachi 44780 type controller, you have a maximum of 128 bytes of screen ram.
The 2 X 16 display we are using only uses 32 bytes, so the remainder would be off screen
ram. Shifting the display left by using the SCRLEFT command can access this ram.

Command Name: Description:

$133 INITLCD1 Initialize LCD display


$132 INITLCD2 Initialize LCD display
$101 CLEAR Clear Display
$102 HOME Return Home
$104 INCCUR AutoIncrement Cursor (default)
$105 INCSCR AutoIncrement Display
$106 DECCUR AutoDecrement Cursor
$107 DECSCR AutoDecrement Display
$108 OFF Display,Cursor,and Blink off
$10C SCR Display on,Cursor and Blink off
$10D SCRBLK Display and Blink on Cursor off
$10E SCRCUR Display and Cursor on, Blink off
$10F SCRCURBLK Display, Cursor, and Blink on
$110 CURLEFT Move Cursor left
$114 CURRIGHT Move Cursor right
$118 SCRLEFT Move Display left
$11C SCRRIGHT Move Display right
$120 ONELINE Set display for 1 line LCDs
$128 TWOLINE Set display for 2 line LCDs
$140 CGRAM address Set CGRAM address for R/W
$180 SCRRAM address Set Display ram address for R/W

Table 2.1

0 1 2 3 4 5 6 7 8 9 a b c d e f

40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f

Table 2.2

16
Ten Projects With The MBasic Compiler

LCD SET UP

When we use the LCDWRITE command, MBasic has default pin connections. These
are:

LCDREGSEL CON B0 ;RS LINE


LCDCLK CON B1 ;E LINE
LCDREADWRITE CON B2 ;R/W LINE
LCDPORT CON PORTA ;DB 4-7 out of 0-7

These default settings are the same for all compatible PICmicros. To change the default
settings just assign different values.

ENCLOSURE CONNECTOR

Project_2 requires a larger working space then Project_1, so we will not use the bread
board on the 0818 Development board. It will be necessary to connect wires from the
Development board to a 25-pin DB connector and attach it to the board enclosure. This
will allow us to use a ribbon cable to connect to Project_2. The following shows the pin
out configurations for the connector:

0818 Development Bd. 25-pin DB connector (male)


A0 1
A1 2
A2 3
A3 4
A4 5
B0 6
B1 7
B2 8
B3 9
B4 10
B5 11
B6 12
B7 13
Vss 14
Vdd 25

PROJECT_2

The components used in this project will be an LCD, a potentiometer, 4 button switches,
5 resistors, a capacitor and a speaker. The MBasic commands used in this project are:
Low, High, Pause, Goto, Button, Rctime, If…Then, For…Next, Repeat…Until, Toggle,
Lookup, Freqout, Sound, and Debug (refer to the MBasic manual for the explanations of
these commands).

Eight of the thirteen I/O pins of the PIC16F628 will be used in this project.

17
Ten Projects With The MBasic Compiler

Follow the below schematic in wiring Project_2. Use the components you received in
your kit. The only things you need to get on your own are the resistors, a .1uf capacitor
and speaker.

PIC CONNECTOR

RB3 PIN 9
180 ohms

Demo4 Demo3 Demo2 Demo1

Vss PIN 14
3.9k ohms 2.2k ohms 2.2k ohms 2.2k ohms

0.1uf

Vdd PIN 25

LCD MODULE

Vdd Vss
20k ohms 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Vss Vdd Vo RS R/W E 0 1 2 3 4 5 6 7

Vss PIN 14
Vdd PIN 25
RB0 PIN 6
RB1 PIN 7

RA0 PIN 1
RA1 PIN 2
RA2 PIN 3
RA3 PIN 4

RB5 PIN 11
Vss PIN 14

18
Ten Projects With The MBasic Compiler

After you have wired Project_2, open a new file and enter the following program.
Follow the same procedure you did in writing your first program. Once the program is
entered, save the file as: Project_2.

19
Ten Projects With The MBasic Compiler

CPU = 16F628
MHZ = 10
CONFIG 16142

Pot var word


Sample var byte
Loop var byte
Pause 500
LCDWRITE [INITLCD1,INITLCD2,CLEAR,HOME,SCR]

Start:
Sample = 0
Loop = 0
Repeat
Sample = Sample + 1
High B3
Pause 10
Rctime B3,1,Pot
Until Sample = 5
Pot = Pot * 5
Debug [Dec Pot,13]
If Pot > 1200 Then Demo1
If Pot > 600 Then Demo2
If Pot > 100 Then Demo3
If Pot > 2 Then Demo4
If Pot < 2 Then Start

Demo1:
LCDWRITE ["FIRST LINE"]
Pause 800
LCDWRITE [CLEAR,HOME]
Pause 300
LCDWRITE [SCRRAM+$40,"SECOND LINE"]
Pause 800
LCDWRITE [CLEAR,HOME]
Pause 500
Goto Start

Demo2:
LCDWRITE [SCRRAM+16h,"SCAN LEFT"]
Repeat
Loop = Loop + 1
LCDWRITE [SCRLEFT]
Pause 300
Until Loop = 16
Pause 500
LCDWRITE [CLEAR,HOME]
Goto Start
20
Ten Projects With The MBasic Compiler

Demo3:
LCDWRITE [SCRRAM+58h,"SCAN RIGHT"]
Pause 500
Repeat
Loop = Loop + 1
LCDWRITE [SCRRIGHT]
Pause 300
Until Loop = 16
Pause 500
LCDWRITE [CLEAR,HOME]
Goto Start

Demo4:
AH con 440*2
AS con 466*2
BH con 494*2
CH con 523*2
CS con 554*2
DH con 587*2
DS con 622*2
EH con 659*2
FH con 678*2
FS con 740*2
GH con 784*2
GS con 831*2
Temp var byte
Temp2 var word
For Temp = 0 to 33

Lookup Temp,[CS,BH,AH,BH,CS,0,CS,0,CS,BH,0,|
BH,0,BH,0,CS,EH,0,EH,CS,BH,AH,BH,CS,0,CS,0,|
CS,BH,0,BH,CS,BH,AH],Temp2
If temp2=0 then Finish
Freqout B5,500,Temp2
Finish:
Next
Pause 1000
Sound B5,[500\440,500\466,500\494,500\523,|
500\554,500\587,500\622,500\659,500\698,|
500\740,500\784,500\831]
Pause 1000
Goto Start

End

21
Ten Projects With The MBasic Compiler

EXPLANATION OF PROGRAM

Project_2 has four parts labeled: Start, Demo1, Demo2, Demo3 and Demo4. Following
is a short description of what each part of the program does.

Start: The program loops forever until one of the buttons is pressed and then the
program moves to the Demo specified by the value of the button pressed.

Demo1: This part of the program displays the message “FIRST LINE” on the first
line of the LCD and then displays the message “SECOND LINE” on the
second line of the LCD.

Demo2: This part of the program displays the message “SCAN LEFT” on the far
right side of the LCD display (off screen ram) and then shifts the message
left.

Demo3: This part of the program displays the message “SCAN RIGHT” on the far
left side of the LCD display and then shifts the message right until it goes
to off screen ram.

Demo4: Tones are sent to the speaker.

CONCLUSION

Chapter 2 showed how to program the PIC16F628 and how to use some commands of the
MBasic compiler. Project_2 used the 0818 Development Board to run a LCD in various
ways and send tones to a speaker. In the next chapter we will be experimenting with
three different types of motors.

22
Ten Projects With The MBasic Compiler

CHAPTER 3 : MOTOR CONTROL


(Project_3)
TYPES OF MOTORS

Electric motors convert electrical energy to mechanical energy. Motors come in all
types, shapes and sizes. The motors discussed in this chapter include the DC motor,
servomotor, and stepper motor. These motors use magnets and magnetism with attracting
and repelling forces to create motion. The project for this chapter will include all three of
these motors.

DC MOTOR

DC motors in general have two electrical terminals. Applying a voltage across these two
terminals will cause the motor rotor to spin in one direction, while a reverse polarity
voltage will cause the motor rotor to spin in the other direction. The polarity of the
voltage determines motor direction, while the amplitude of the voltage determines motor
speed. DC motors usually run at too high a speed and too low a torque to be useful. In
order for them to be used they need to be geared down. Connecting the shaft of the
motor to a geartrain causes the output shaft from the geartrain to rotate more slowly and
deliver more torque than the input shaft.

The PIC’s output current is insufficient to power a DC motor directly. But the output of
the PIC can turn on or off a transistor that can control a device that will be able to power
a motor. The device used in this project to control the DC motor is an H-Bridge. With
the H-Bridge the PIC can stop the DC motor, rotate it clockwise, or rotate it counter
clockwise. It’s called an H-Bridge because the transistors (switches) are arranged in an H
pattern. The switches are opened and closed in a manner so as to put a voltage of one
polarity across the motor for one direction or a voltage of opposite polarity for reverse
direction. To control the speed of the motor, the switches are opened and closed at
different rates in order to apply different average voltages across the motor. This is
called Pulse-Width Modulation (PWM). Again, PWM is simply using varying pulse
widths to create different average voltages across the motor to change its speed.

SERVO MOTOR

Servomotors are basically a DC motor with a geartrain, limit stops beyond which the
shaft cannot turn, a potentiometer for position feedback and an integrated circuit for
position control. Servos have three wires, one for power, one for ground, and one for the
position control signal. The potentiometer in the servo is connected to the output shaft.
The pot allows the control circuitry to monitor the current angle of the servo motor. If
the shaft is at the correct angle (the commanded position), then the motor shuts off. If the
circuit finds that the angle is not correct, it will turn the motor until the angle is correct.
If the signal sent is past the limit stops (a mechanical stop built onto the main output
23
Ten Projects With The MBasic Compiler

gear), the motor will be in a “stalled” state and might eventually damage the motor. The
limit stops can be removed to provide continuous rotation; this is called “hacking” the
servo. Information on hacking a servo can be found on many Internet sites on the web.

The position control signal wire communicates the desired angle for the servo to advance
to. The angle is determined by the duration of the pulse sent. The servo expects to see a
pulse every 20 milliseconds. The length of the pulse determines how far the motor turns.
The output shaft is capable of travelling around 180 to 210 degrees. Below is a list of
pulse times for degree positions; actual timings may vary due to different motor
manufacturers.

1.5 ms pulse causes the motor to turn to the 90 degree position.


1.25 ms pulse causes the motor to turn to the 0 degree position.
1.75 ms pulse causes the motor to turn to the 180 degree position.

STEPPER MOTOR

Stepper motors are made with strong permanent magnets and electromagnets. The rotor
consists of the permanent magnet and the windings consist of the electromagnets. Instead
of rotating smoothly, like DC motors, they move incrementally. Each increment is a
fixed angular displacement of the motor’s shaft, typically on the order of one to ten
degrees, depending on the manufacturer. They operate from a pattern of electric pulses
sent to the windings of the motor. The rotor rotates a specific increment with each
electronic pulse. The increments are referred to as “steps”.

There are several types of stepper motors, such as unipolar or bipolar, two phase or four
phase. In this project we will use a four phase unipolar stepper motor. Five interface
connections are required as shown in Figure 3.1.

Common

A C

B D

Figure 3.1

24
Ten Projects With The MBasic Compiler

The common lead is the power connection for the motor coils. Typically, this is
connected to 12 volts. The other four leads are the signal connections. The normal
electrical inputs for the signal wires are a four step switching sequence as shown in Table
3.1.

STEP A B C D

1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

Table 3.1

The speed of rotation will be determined by the time delay between each step. Following
the step sequence backwards (i.e. 4,3,2,1,4 …etc.) will reverse the direction of rotation.

To drive the stepper motor, a ULN2003A darlington array driver chip will be used.

PROJECT_3

The components used in this project will be a geared DC motor, a Futaba S-148 servo,
and a four phase unipolar stepper motor. The integrated circuits used will be a
ULN2003A and a 74HCT245N. Also three button switches, four resistors, four LEDs, a
1N3024 zener diode and a capacitor will be used. The MBasic commands used in this
project, which we have not used before, are: While…Do, Servo, and PWM (refer to the
MBasic manual for the explanations of these commands).

Nine of the thirteen I/O pins of the PIC16F628 will be used in this project.

Follow the below schematic in wiring Project_3. The LEDs and Button switches are in
your kit, the other parts you can get from electronic stores. One good source for the parts
you need is Jameco Electronics (www.jameco.com).

25
Ten Projects With The MBasic Compiler

PIC CONNECTOR

390 ohms
B0 PIN 6

“A” “B”

Vss PIN 14
4.7k ohms
.1mf

Vdd PIN 25

“START”

B1 PIN 7 Vdd

Vss
10k ohms

Vdd
1 20

B2 PIN 8 2 19 Vss
3 18
4 74HCT 17
B3 PIN 9 5 245N 16 DC Motor

15

10
Vss

B5 PIN 11
Vdd SERVO
Vss

26
Ten Projects With The MBasic Compiler

Vdd
LEDs 330 ohms

A0 PIN 1 1 16

A1 PIN 2 2 ULN 15
2003A STEPPER
A2 PIN 3 3 14 MOTOR

A3 PIN 4 4 13

Vss 8 9
1N3024

+9 volts
Battery

27
Ten Projects With The MBasic Compiler

28
Ten Projects With The MBasic Compiler

After you have wired Project_3, open a new file and enter the following program.
Follow the same procedure you did in writing your first program. Once the program is
entered, save the file as: Project_3.

CPU = 16F628
MHZ = 10
CONFIG 16138

High A0
High A1
High A2
High A3
Low B5
Low B2
Low B3

START:
Debug ["START",13]
work var byte
work = 0
pot var word
pot = 0
counter var byte
counter = 0
Pause 1500
Button B1,1,100,10,work,1,DEMO1
Goto START

DEMO1:
Debug ["DEMO1",13]
Pause 1500
Button B1,1,100,10,work,1,DEMO2
Temp1 var byte
Temp1 = 10
High B0
Pause 10
Rctime B0,1,pot
Debug [DEC pot,13]
If pot > 350 then DEMO1
If pot > 300 then Tiltup
If pot > 150 then Tiltdn
Goto DEMO1

Tiltup:
While temp1 <> 250
temp1 = temp1 + 10
29
Ten Projects With The MBasic Compiler

servo B5,temp1
Do
Goto DEMO1

Tiltdn:
Temp1 = 250
While temp1 <> 10
temp1 = temp1 - 10
Servo B5,temp1
Do
Goto DEMO1

DEMO2:
Debug ["DEMO2",13]
Pause 1500
Button B1,1,100,10,work,1,DEMO3
Temp1 = 1
High B0
Pause 10
Rctime B0,1,pot
Debug [DEC pot,13]
If pot > 350 then DEMO2
If pot > 300 then Panrht
If pot > 150 then Panlft
Goto DEMO2

Panrht:
While temp1 <> 3
temp1 = temp1 + 1
PWM B2,240,1000
Do
Low B2
Goto DEMO2

Panlft:
While temp1 <> 3
temp1 = temp1 + 1
PWM B3,240,1000
Do
Low B3
Goto DEMO2

DEMO3:
Debug ["DEMO3",13]
Pause 1500
Button B1,1,100,10,work,1,START
High B0
Pause 10
30
Ten Projects With The MBasic Compiler

Rctime B0,1,pot
Debug [DEC pot,13]
If pot > 350 then DEMO3
If pot > 300 then CW
If pot > 150 then CCW
Goto DEMO3

CW:
counter = 15
Repeat
counter = counter - 1
low A0
Pause 100
high A0
low A1
pause 100
high A1
low A2
pause 100
high A2
low A3
pause 100
high A3
Until counter = 1
Goto DEMO3

CCW:
counter = 15
Repeat
counter = counter - 1
low A3
Pause 100
high A3
low A2
pause 100
high A2
low A1
pause 100
high A1
low A0
pause 100
high A0
Until counter = 1
Goto DEMO3

End

31
Ten Projects With The MBasic Compiler

EXPLANATION OF PROGRAM

Project_3 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does.

Start: The program loops forever until the “START” button is pressed and then
the program goes to Demo1.

Demo1: This part of the program operates the Servo. If the button labeled “A” is
pressed, the servo assembly tilts up until it reaches a certain position. If
the button labeled “B” is pressed, the servo assembly tilts down until it
reaches a certain position. The program loops until the “START” button
is pressed and then goes to Demo2.

Demo2: This part of the program operates the DC motor. If the button “A” is
pressed, the motor assembly will pan right (CCW) for a short distance. If
the button “B” is pressed, the motor assembly will pan left (CW), also for
a short distance. The program loops until the “START” button is pressed
and then goes to Demo3.

Demo3: This part of the program operates the stepper motor. If the “A” button is
pressed, the motor’s rotor turns (steps) in the CW direction for a certain
distance. If “B” is pressed, the motor’s rotor turns in the CCW direction
for a certain distance. The program loops until the “START” button is
pressed and then goes to the beginning of the program.

CONCLUSION

Chapter 3 showed how to program the PIC16F628 and how to use some commands of the
MBasic compiler. Project_3 used the 0818 Development Board to run three different
types of motors. In the next chapter we will be programming the PIC16F876 with the
2840 Development Board to control two seven segment LED Displays.

32
Ten Projects With The MBasic Compiler

CHAPTER 4: 7-SEGMENT LED DISPLAY


(Project_4)

BOARD ENCLOSURE

In this project the 2840 Development Board will be used. For ease of use, I have put the
2840 Development Board in an enclosure. I have connected wires from the terminal
strips of the Development Board to a 25 pin DB connector and connected it to the side of
the enclosure. A ribbon connector can be connected from the enclosure to Project_4.

INSTALL AND CONFIGURE HARDWARE

Install the PIC16F876 (28 pin), in the 2840 Development Board.

Connect the “straight through” serial cable to the ISP-PRO then to the com port of your
computer.

Connect the power supply (wall adapter) to the ISP-PRO.

Connect the ISP-PRO to the 2840 Development Board with the supplied cable.

Start the Basic Micro IDE. Once the IDE is running, configure the ISP-PRO serial port.
This is done from the system setup menu under Tools. Then run the hardware test to see
if the ISP-PRO is functioning correctly.

Menu Bar: Tools Æ System Setup Æ Choose Com Port the ISP-Pro is attached to Æ
Hardware test.

You should receive the message “ISP-PRO communications are normal”

7-SEGMENT LED DISPLAY

Seven segment displays come in two varieties – common anode (CA) and common
cathode (CC). When a display is referred to as a common anode type, it means that all
the anodes for the individual segment LEDs are connected together internally. The
driving signals are sent to the individual cathode segments as shown in Figure 4.1. With
common cathode types, all the cathodes for the individual segment LEDs are connected
together internally and the driving signals are sent to the individual anode segments. The
segment LEDs are labeled a-g and can display the digits 0 through 9 and the letters
A,b,C,d,E and F. Table 4.1 shows the segments that need to be illuminated to show a
particular digit or letter.

33
Ten Projects With The MBasic Compiler

a b c d e f g

cathode

LED segments
anode

Figure 4.1 common

Digit Illuminated Segment ( 1 = illumination)


Shown a b c d e f g

0 1 1 1 1 1 1 0
1 0 1 1 0 0 0 0
2 1 1 0 1 1 0 1
3 1 1 1 1 0 0 1
4 0 1 1 0 0 1 1
5 1 0 1 1 0 1 1
6 1 0 1 1 1 1 1
7 1 1 1 0 0 0 0
8 1 1 1 1 1 1 1
9 1 1 1 1 0 1 1
A 1 1 1 0 1 1 1
b 0 0 1 1 1 1 1
C 1 0 0 1 1 1 0
d 0 1 1 1 1 0 1
E 1 0 0 1 1 1 1
F 1 0 0 0 1 1 1

f b

e c

d
Table 4.1

34
Ten Projects With The MBasic Compiler

The 7-segment display being used in this project is a common anode type. PORT C of
the 16F876 sends out a binary coded decimal (BCD) value that feeds a couple of 7447
(BCD-to-7 segment decoder/driver) IC chips. The 7447 chips send the appropriate
signals to the common anode 7-segment LED displays. For the display to work, the
common anode needs to be connected to +5 volts, and the 7447 chips need to send out a
low signal to the specified (cathode) segment LEDs.

DIFFERENT PROGRAMMING STYLES

In the following program for Project_4, there will be different styles of programming to
send data to the displays. Different commands will be used, with some being more
efficient than others. Everyone has different styles in writing their programs, so feel free
to change the code and experiment with it.

PROJECT_4

The components used in this project are three button switches, three 10k ohm resistors,
one 100 ohm resistor, two 7447 (BCD-to-7 segment decoder/driver) IC chips and two
common anode 7-segment LED displays. The MBasic commands used in this project
that have not been used in previous projects are: Poke and Gosub…Return (refer to the
MBasic manual for the explanations of these commands).

Eleven of the twenty-two I/O pins of the PIC16F876 will be used in this project.

Follow the below schematic in wiring Project_4.

Vss
10k ohms

B0 Vdd

Vss
10k ohms

B1 Vdd

Vss
10k ohms

B2 Vdd

35
Ten Projects With The MBasic Compiler

C4 C5 C6 C7 C0 C1 C2 C3

7 1 2 6 7 1 2 6
A B C D 8 Vss A B C D 8 Vss
7447 16 Vdd 7447 16 Vdd
a b c d e f g a b c d e f g

13 12 11 10 9 15 14 13 12 11 10 9 15 14

g f a b g f a b

e d c dec. pt. e d c dec. pt.

Vdd
100 ohms

36
Ten Projects With The MBasic Compiler

After you have wired Project_4, open a new file and enter the following program.
Follow the same procedure you did in writing your first program, except select the
PICmicro MCU as 16F876. Once the program is entered, save the file as: Project_4.

CPU = 16F876
MHZ = 10
CONFIG 16254

START:
Debug ["START",13]
TRISC = $00
PORTC = $00
Work var byte
VAL1 var byte
VAL2 var byte
Seg_Val var word
Dig_One var word
Dig_Two var word
Send_Val var byte
Send_ValB var byte
Final var byte
Num var byte
Work = 0
VAL1 = 0
VAL2 = 0
Button B0,1,100,10,Work,1,DEMO1
Goto START

37
Ten Projects With The MBasic Compiler

DEMO1:
Debug ["DEMO1",13]
Pause 1000
Button B1,1,100,10,Work,1,VALUE2
Button B2,1,100,10,Work,1,VALUE1
Button B0,1,100,10,Work,1,DEMO2
Goto DEMO1

VALUE1:
VAL1 = VAL1 + 1
Debug [DEC VAL1,13]
If VAL1 = 1 Then ONE1
If VAL1 = 2 Then ONE2
If VAL1 = 3 Then ONE3
If VAL1 = 4 Then ONE4
If VAL1 = 5 Then ONE5
If VAL1 = 6 Then ONE6
If VAL1 = 7 Then ONE7
If VAL1 = 8 Then ONE8
If VAL1 = 9 Then ONE9
VAL1 = 0
Goto ONE0

VALUE2:
VAL2 = VAL2 + 1
Debug [DEC VAL2,13]
If VAL2 = 1 Then TWO1
If VAL2 = 2 Then TWO2
If VAL2 = 3 Then TWO3
If VAL2 = 4 Then TWO4
If VAL2 = 5 Then TWO5
If VAL2 = 6 Then TWO6
If VAL2 = 7 Then TWO7
If VAL2 = 8 Then TWO8
If VAL2 = 9 Then TWO9
VAL2 = 0
Goto TWO0

DEMO2:
Debug ["DEMO2",13]
Pause 1000
Button B1,1,100,10,Work,1,COUNTDN
Button B2,1,100,10,Work,1,COUNTUP
Button B0,1,100,10,Work,1,DEMO3
Goto DEMO2

COUNTUP:
Pause 500
38
Ten Projects With The MBasic Compiler

Poke 7,0
Pause 500
Poke 7,1
Pause 500
Poke 7,2
Pause 500
Poke 7,3
Pause 500
Poke 7,4
Pause 500
Poke 7,5
Pause 500
Poke 7,6
Pause 500
Poke 7,7
Pause 500
Poke 7,8
Pause 500
Poke 7,9
Pause 500
Poke 7,16
Pause 500
Poke 7,17
Pause 500
Poke 7,18
Pause 500
Poke 7,19
Pause 500
Poke 7,20
Pause 500
Poke 7,21
Pause 1000
Goto DEMO2

COUNTDN:
Pause 500
Poke 7,21
Pause 500
Poke 7,20
Pause 500
Poke 7,19
Pause 500
Poke 7,18
Pause 500
Poke 7,17
Pause 500
Poke 7,16
Pause 500
39
Ten Projects With The MBasic Compiler

Poke 7,9
Pause 500
Poke 7,8
Pause 500
Poke 7,7
Pause 500
Poke 7,6
Pause 500
Poke 7,5
Pause 500
Poke 7,4
Pause 500
Poke 7,3
Pause 500
Poke 7,2
Pause 500
Poke 7,1
Pause 500
Poke 7,0
Pause 1000
Goto DEMO2

DEMO3:
Debug ["DEMO3",13]
Pause 1000
Button B0,1,100,10,Work,1,START
For Num = 99 to 1 step -1
Seg_Val = Num
Gosub SegDisp
pause (150)
Next
Goto DEMO3

SegDisp:
Dig_One = Seg_Val / 10
Send_Val = Dig_One
Send_Val = Send_Val
Dig_Two = Seg_Val // 10
Send_ValB = Dig_Two
Send_ValB = Send_Val << 4
Final = Send_Val | Send_ValB
PORTC = Final
Return

ONE0:
LOW C0
LOW C1
LOW C2
40
Ten Projects With The MBasic Compiler

LOW C3
GOTO DEMO1

ONE1:
HIGH C0
LOW C1
LOW C2
LOW C3
GOTO DEMO1

ONE2:
LOW C0
HIGH C1
LOW C2
LOW C3
GOTO DEMO1

ONE3:
HIGH C0
HIGH C1
LOW C2
LOW C3
GOTO DEMO1

ONE4:
LOW C0
LOW C1
HIGH C2
LOW C3
GOTO DEMO1

ONE5:
HIGH C0
LOW C1
HIGH C2
LOW C3
GOTO DEMO1

ONE6:
LOW C0
HIGH C1
HIGH C2
LOW C3
GOTO DEMO1

ONE7:
HIGH C0
41
Ten Projects With The MBasic Compiler

HIGH C1
HIGH C2
LOW C3
GOTO DEMO1

ONE8:
LOW C0
LOW C1
LOW C2
HIGH C3
GOTO DEMO1

ONE9:
HIGH C0
LOW C1
LOW C2
HIGH C3
GOTO DEMO1

TWO0:
LOW C4
LOW C5
LOW C6
LOW C7
GOTO DEMO1

TWO1:
HIGH C4
LOW C5
LOW C6
LOW C7
GOTO DEMO1

TWO2:
LOW C4
HIGH C5
LOW C6
LOW C7
GOTO DEMO1

TWO3:
HIGH C4
HIGH C5
LOW C6
LOW C7
GOTO DEMO1

42
Ten Projects With The MBasic Compiler

TWO4:
LOW C4
LOW C5
HIGH C6
LOW C7
GOTO DEMO1

TWO5:
HIGH C4
LOW C5
HIGH C6
LOW C7
GOTO DEMO1

TWO6:
LOW C4
HIGH C5
HIGH C6
LOW C7
GOTO DEMO1

TWO7:
HIGH C4
HIGH C5
HIGH C6
LOW C7
GOTO DEMO1

TWO8:
LOW C4
LOW C5
LOW C6
HIGH C7
GOTO DEMO1

TWO9:
HIGH C4
LOW C5
LOW C6
HIGH C7
GOTO DEMO1

END

43
Ten Projects With The MBasic Compiler

EXPLANATION OF PROGRAM

Project_4 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does.

Start: This routine makes all of Port C outputs, and all of Port C’s pins are made
low. The program loops forever until the button switch labeled “Start” is
pressed and then the program moves to Demo1.

Demo1: Pressing the button switches under each individual display will increment
the display by one. The digits zero through nine can be displayed on each
individual display. The program loops until the button switch labeled
“Start” is pressed and then the program moves to Demo2.

Demo2: Depending on which button switch is pressed, the displays will count up
from zero to fifteen or count down from fifteen to zero. The program will
continue to loop until the “Start” button is pressed and then will go to
Demo3.

Demo3: This routine will show the number nine on each display, and decrement
that number until each display reads zero. The program then repeats the
process until the “Start” button is pressed and then the program returns to
the Start routine.

CONCLUSION

Chapter 4 showed how to program the PIC16F876 with the 2840 Development Board.
We were able to display numbers on a couple of common anode 7-segment LED display
in several different ways. In the next chapter we will be adding photocells and two flex
sensors to Project_4.

44
Ten Projects With The MBasic Compiler

CHAPTER 5: RESISTIVE –TYPE SENSORS


(Project_5)

FLEX SENSORS

A Flex sensor is a variable resistor printed on a flexible substrate. As the sensor is bent,
the resistance changes relative to the bend radius. The sensor changes resistance only in
one direction and returns to its nominal value at rest. The sensor at rest provides a low
resistance; when the sensor is bent to 90 degrees the resistance is at maximum.
Typically, the range of resistance is 10k ohms at rest (0 degrees) and 35k ohms at full
bend (90 degrees).

PHOTOCELLS

A photocell is a variable resistor, much like a potentiometer but without the control shaft.
The resistance changes by increasing or decreasing the amount of light the photocell
receives. The more light it receives, the less resistance the sensor provides. In a dark
environment the photocell provides its highest resistance.

ROTARY SWITCH

A rotary switch will be used in Project_5 to test each type of sensor described above.
The rotary switch will act like a variable resistor. It will give a different resistor value for
the four different settings that it has. With each resistance value and the use of the
RCTIME command, the program will be directed to the routine that will test each sensor.

ADIN COMMAND

The PIC16F876 we are using in this chapter has five channels of ADC (Analog to Digital
Converters). Analog to digital conversion allows your program to receive an analog
signal (0 to 5 volts) and convert it into digital values. The analog input charges a sample
and hold capacitor. The output of the sample and hold capacitor is the input into the
converter. The converter then generates a digital result of this analog level via successive
approximation. The A/D conversion of the analog input signal results in a corresponding
10-bit digital number. The ADIN command sets up the hardware A/D converter, takes
the conversion from the chosen channel and places the result into the designated variable.

The PIC powers up with the ADC pins set as analog inputs. If some pins are desired to
be digital, the ADCON1 register needs to be changed. The last four bits (PCFG) of the
ADCON1 register determine which pins are analog or digital. Table 5.1 shows the
different register settings.

45
Ten Projects With The MBasic Compiler

Table 5.1

46
Ten Projects With The MBasic Compiler

The first bit (ADFM), which is the justification bit of ADCON1 is normally set (1). This
bit enables right justification. The ADSETUP in the ADIN command is used to setup the
ADCON1 register. Below is an example.

ADCON1 register
(ADFM) (PCFG)

ADSETUP con %10000010

AN0, AN1, AN2, AN3, AN4 are analog


AN5 is digital

Right justify

Finally, before the ADIN command is used, the designated A/D pins need to be
configured as inputs by setting its TRIS value to one.

PROJECT_5

The components used in this project are: one button switch, one rotary switch, six
photocells, two flex sensors and twelve various resistors in the sensor circuits (refer to
schematic). Also parts from Project_4 that include: three 10k ohm resistors, one 100 ohm
resistor, two 7447 (BCD-to-7 segment decoder/driver) IC chips and two common anode
7-segment LED displays. The MBasic command used in this project that has not been
used in previous projects is: ADIN

Sixteen of the twenty-two I/O pins of the PIC16F876 will be used in this project.

Follow the below schematic in wiring Project_5. (This project is added to Project_4)

47
Ten Projects With The MBasic Compiler

Right Photocells

39k ohms
Vss Vdd

RA0
Left Photocells

39k ohms
Vss Vdd

RA1

Right Flex sensor

39k ohms
Vss Vdd

RA2

Left Flex sensor

39k ohms
Vss Vdd

RA3

1 10k ohms

2 22k ohms

3 33k ohms Vdd


.1 uf
4 39k ohms

RB5 Rotary
switch

1 33k ohms

2 22k ohms

3 10k ohms Vss

4 4.7k ohms

48
Ten Projects With The MBasic Compiler

After you have wired Project_5, open a new file and enter the following program.
Once the program is entered, save the file as: Project_5.

CPU = 16F876
MHZ = 10
CONFIG 16254

49
Ten Projects With The MBasic Compiler

TRISA = $FF
TRISC = $00
PORTC = $FF
work var byte
temp1 var word
temp2 var word
temp3 var word
temp4 var word
temp5 var word

ADSETUP con %10000010

START:
Debug ["START",13]
Button B0,1,100,10,work,1,Demo1
Goto START

Demo1:
Debug ["Demo1",13]
Poke 7,255
ADIN 0,2,ADSETUP,temp1
ADIN 1,2,ADSETUP,temp2
ADIN 2,2,ADSETUP,temp3
ADIN 3,2,ADSETUP,temp4
High B5
Pause 10
RCTIME B5,1,temp5
Debug ["temp1 "]
Debug [dec temp1,13]
Debug ["temp2 "]
Debug [dec temp2,13]
Debug ["temp3 "]
Debug [dec temp3,13]
Debug ["temp4 "]
Debug [dec temp4,13]
Debug ["temp5 "]
Debug [dec temp5,13]
If temp5 > 400 then Demo2
Goto Demo3

Demo2:
Debug ["Demo2",13]
Poke 7,255
If temp5 > 700 then R_Flex
If temp5 > 400 then L_Flex
Goto Demo1
R_Flex:
Debug ["R_Flex",13]
50
Ten Projects With The MBasic Compiler

ADIN 0,2,ADSETUP,temp1
High B5
Pause 10
RCTIME B5,1,temp5
If temp5 < 700 then Demo1
If temp1 > 250 then zero1
If temp1 > 220 then one1
If temp1 > 190 then two1
If temp1 > 160 then three1
If temp1 > 130 then four1
If temp1 > 100 then five1
If temp1 > 90 then six1
If temp1 > 80 then seven1
If temp1 > 70 then eight1
If temp1 > 60 then nine1
Goto R_Flex
L_Flex:
Debug ["L_Flex",13]
ADIN 1,2,ADSETUP,temp2
High B5
Pause 10
RCTIME B5,1,temp5
If temp5 > 700 then Demo1
If temp5 < 400 then Demo1
If temp2 > 330 then zero2
If temp2 > 300 then one2
If temp2 > 250 then two2
If temp2 > 210 then three2
If temp2 > 180 then four2
If temp2 > 150 then five2
If temp2 > 130 then six2
If temp2 > 120 then seven2
If temp2 > 110 then eight2
If temp2 > 100 then nine2
Goto L_Flex

Demo3:
Poke 7,255
If temp5 > 100 then R_Photo
If temp5 < 5 then L_Photo
Goto Demo1
R_Photo:
Debug ["R_Photo",13]
ADIN 2,2,ADSETUP,temp3
High B5
Pause 10
RCTIME B5,1,temp5
If temp5 > 400 then Demo1
51
Ten Projects With The MBasic Compiler

If temp5 < 100 then Demo1


If temp3 > 935 then zero1p
If temp3 > 900 then one1p
If temp3 > 840 then two1p
If temp3 > 790 then three1p
If temp3 > 740 then four1p
If temp3 > 700 then five1p
If temp3 > 650 then six1p
If temp3 > 600 then seven1p
If temp3 > 550 then eight1p
If temp3 > 500 then nine1p
Goto R_Photo
L_Photo:
Debug ["L_Photo",13]
ADIN 3,2,ADSETUP,temp4
High B5
Pause 10
RCTIME B5,1,temp5
If temp5 > 5 then Demo1
If temp4 > 925 then zero2p
If temp4 > 890 then one2p
If temp4 > 840 then two2p
If temp4 > 790 then three2p
If temp4 > 740 then four2p
If temp4 > 700 then five2p
If temp4 > 650 then six2p
If temp4 > 600 then seven2p
If temp4 > 550 then eight2p
If temp4 > 500 then nine2p
Goto L_Photo

zero1:
Poke 7,240
goto R_Flex

one1:
Poke 7,241
goto R_Flex

two1:
Poke 7,242
goto R_Flex

three1:
Poke 7,243
goto R_Flex

four1:
52
Ten Projects With The MBasic Compiler

Poke 7,244
goto R_Flex

five1:
Poke 7,245
Goto R_Flex

six1:
Poke 7,246
Goto R_Flex

seven1:
Poke 7,247
Goto R_Flex

eight1:
Poke 7,248
Goto R_Flex

nine1:
Poke 7,249
Goto R_Flex

zero2:
Poke 7,15
Goto L_Flex

one2:
Poke 7,31
Goto L_Flex

two2:
Poke 7,47
Goto L_Flex

three2:
Poke 7,63
Goto L_Flex

four2:
Poke 7,79
Goto L_Flex

five2:
Poke 7,95
Goto L_Flex
53
Ten Projects With The MBasic Compiler

six2:
Poke 7,111
Goto L_Flex

seven2:
Poke 7,127
Goto L_Flex

eight2:
Poke 7,143
Goto L_Flex

nine2:
Poke 7,159
Goto L_Flex

zero1p:
Poke 7,240
Goto R_Photo

one1p:
Poke 7,241
Goto R_Photo

two1p:
Poke 7,242
Goto R_Photo

three1p:
Poke 7,243
Goto R_Photo

four1p:
Poke 7,244
Goto R_Photo

five1p:
Poke 7,245
Goto R_Photo

six1p:
Poke 7,246
Goto R_Photo

seven1p:
Poke 7,247
Goto R_Photo
54
Ten Projects With The MBasic Compiler

eight1p:
Poke 7,248
Goto R_Photo

nine1p:
Poke 7,249
Goto R_Photo

zero2p:
Poke 7,15
Goto L_Photo

one2p:
Poke 7,31
Goto L_Photo

two2p:
Poke 7,47
Goto L_Photo

three2p:
Poke 7,63
Goto L_Photo

four2p:
Poke 7,79
Goto L_Photo

five2p:
Poke 7,95
Goto L_Photo

six2p:
Poke 7,111
Goto L_Photo

seven2p:
Poke 7,127
Goto L_Photo

eight2p:
Poke 7,143
Goto L_Photo

nine2p:
Poke 7,159
55
Ten Projects With The MBasic Compiler

Goto L_Photo

End

EXPLANATION OF PROGRAM

Project_5 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does.

Start: Program loops until the “START” button is pressed and then goes to
Demo1.

Demo1: The analog to digital conversions of the sensors are stored in their
designated variables. The rotary switch’s four settings determine which
sensor variables are looked at and what values are shown on the 7-segment
LED display. If the switch setting is on the first or second position, then
the program goes to the Demo2 routine. If the switch setting is on the
third or fourth position, then the program goes to the Demo3 routine.

Demo2: If the switch setting is on the first position, then the values represented by
the right flex sensor are displayed on the right 7-segment LED display. If
the switch setting is on the second position, the values represented by the
left flex sensor are displayed on the left 7-segment LED display. If the
switch settings change, the program goes back to Demo1.

Demo3: If the switch setting is on the third position, then the values represented by
the right photocell are displayed on the right 7-segment LED display. If
the switch setting is on the fourth position, the values represented by the
left photocell are displayed on the left 7-segment LED display. If the
switch settings change, the program goes back to Demo1.

56
Ten Projects With The MBasic Compiler

CONCLUSION

Chapter 5 showed how to program the PIC16F876 with the 2840 Development Board.
We were able to display numbers on a couple of common anode 7-segment LED displays
representing changes in value of resistive sensors. In the next chapter we will be adding
a couple of distance sensors and a buzzer to Project_4 & 5.

57
Ten Projects With The MBasic Compiler

58
Ten Projects With The MBasic Compiler

CHAPTER 6: DISTANCE SENSORS


(Project_6)

TYPES OF DISTANCE MEASURING DEVICES

There are many different types of technologies and devices used in measuring distance,
some of them being: Radar, Sonar, Laser, Infrared and Ultrasonic. In this chapter
Infrared and Ultrasonic will be covered. Infrared uses light that is invisible to the human
eye. Also Infrared light bounces off almost everything. Its main disadvantage is that
fluorescent lights generate it and that can cause interference. Ultrasonic uses sound that
is inaudible to the human ear. Its main advantage is that it is not sensitive to objects of
different colors and light reflecting properties. Its disadvantage is that some materials
absorb sound and don’t reflect it.

IR DISTANCE SENSOR

The Sharp GP2D12 Infrared Ranger will be the sensor discussed in this section. This
sensor basically takes a continuous distance reading, and reports the distance as an analog
voltage. With the use of triangulation, distance is detected; and with the use of a small
linear CCD array, distance can be computed.

The basic process works like this: Infrared light is emitted; as the light travels out, it
either hits an object or keeps on going. If the light hits an object, the light reflects back to
the detector and creates a triangle between the point of reflection, the emitter and
detector. As the distance of the object varies, so do the angles in the triangle; and from
those angles, the distance to the object can be calculated. The Sharp GP2D12 is capable
of measuring distances in the range of 4 inches to 2.5 feet.

ULTRASONIC DISTANCE SENSOR

Ultrasonic sensors or sonar sensors basically send out an ultrasonic sound wave that is
emitted by a transmitter. If the sound wave reflects off an object, it returns as an echo to
the receiver. The echo will most likely be of smaller amplitude, but the frequency should
be the same as the transmitted sound wave. With the ultrasonic circuit used in this
chapter, a stream of 40khz pulses are produced by a 555 timer and sent to the transmitter.
The receiver picks up the echo and a 741 Op Amp amplifies the signal. The output of the
circuit produces an analog voltage that varies with the signal strength of the echo. The
Ultrasonic circuit used in this chapter is capable of measuring distance in the range of 3
inches to 1.5 feet.

Buzzer

A buzzer is used in this project to make an audible sound when the sensors detect objects.
The duration of the sound will decrease in length as an object gets closer to the sensor.

59
Ten Projects With The MBasic Compiler

PROJECT_6

The components used in this project are one Sharp GP2D12 Infrared Ranger, one
Ultrasonic circuit, a buzzer, a rotary switch circuit (refer to schematic from Project_5)
also the parts from Project_4.

Fifteen of the twenty-two I/O pins of the PIC16F876 will be used in this project.

Follow the below schematic in wiring Project_6. (This project is added to Project_4 and
part of Project_5)

Sharp GP2D12
Infrared Object Detector

RA0 Analog output

Vdd

Vss

Ultrasonic Circuit

RA3 Analog output

Vdd

Vss

Buzzer

RB3 +

Vss -

60
Ten Projects With The MBasic Compiler

ULTRASONIC CIRCUIT (DETAILED)

Vdd Vdd Vdd


10k pot

1k
220 ohm

Transmitter
8 7 6 5
NE555P
1 2 3 4

Vdd 1k ohm

.02uf

Vss Vss Vss Vss

Receiver
Vdd
Analog output
RA3 22k ohm 1k ohm

8 7 6 5
LM741CN
1 2 3 4
1 Meg
ohm
.1uf Vss

Vss Vss Vss Vss


1k ohm

61
Ten Projects With The MBasic Compiler

After you have wired Project_6, open a new file and enter the following program.
Once the program is entered, save the file as: Project_6.

CPU = 16F876
MHZ = 10
62
Ten Projects With The MBasic Compiler

CONFIG 16254

PORTA = $00
TRISC = $00
PORTC = $FF
work var byte
IR_info var word
U_info var word
Roto_sw var word
work = 0

ADSETUP con %10000010

START:
Debug ["START",13]
Button B0,1,100,10,work,1,Demo1
Goto START

Demo1:
Debug ["Demo1",13]
Poke 7,255
ADIN 0,2,ADSETUP,IR_info
Debug ["IR_info "]
Debug [dec IR_info,13]
ADIN 3,2,ADSETUP,U_info
Debug ["U_info "]
Debug [dec U_info,13]
High B5
Pause 10
RCTIME B5,1,Roto_sw
Debug ["Roto_sw "]
Debug [dec Roto_sw,13]
Button B0,1,100,10,work,1,Contin
Goto Demo1
Contin:
If Roto_sw > 400 then Demo2
Goto Demo3

Demo2:
Debug ["Demo2",13]
Poke 7,255
If Roto_sw > 700 then IR_Display
If Roto_sw > 400 then IR_Sound
Goto Demo1
IR_Display:
Debug ["IR_Display",13]
ADIN 0,2,ADSETUP,IR_info
High B5
63
Ten Projects With The MBasic Compiler

Pause 10
RCTIME B5,1,Roto_sw
If Roto_sw < 700 then Demo1
If IR_info > 650 then zero1
If IR_info > 550 then one1
If IR_info > 500 then two1
If IR_info > 450 then three1
If IR_info > 400 then four1
If IR_info > 350 then five1
If IR_info > 275 then six1
If IR_info > 200 then seven1
If IR_info > 130 then eight1
If IR_info > 100 then nine1
If IR_info > 0 then off1
Goto IR_Display
IR_Sound:
Low B4
Debug ["IR_Sound",13]
ADIN 0,2,ADSETUP,IR_info
High B5
Pause 10
RCTIME B5,1,Roto_sw
If Roto_sw > 700 then Demo1
If Roto_sw < 400 then Demo1
If IR_info > 650 then zero2
If IR_info > 550 then one2
If IR_info > 500 then two2
If IR_info > 450 then three2
If IR_info > 400 then four2
If IR_info > 350 then five2
If IR_info > 275 then six2
If IR_info > 200 then seven2
If IR_info > 130 then eight2
If IR_info > 100 then nine2
If IR_info > 0 then off2
Goto IR_Sound

Demo3:
Debug ["Demo3",13]
Poke 7,255
If Roto_sw > 100 then U_Display
If Roto_sw < 5 then U_Sound
Goto Demo1
U_Display:
Debug ["U_Display",13]
ADIN 3,2,ADSETUP,U_info
High B5
Pause 10
64
Ten Projects With The MBasic Compiler

RCTIME B5,1,Roto_sw
If Roto_sw > 400 then Demo1
If Roto_sw < 100 then Demo1
If U_info > 600 then zero1p
If U_info > 500 then one1p
If U_info > 475 then two1p
If U_info > 450 then three1p
If U_info > 425 then four1p
If U_info > 400 then five1p
If U_info > 375 then six1p
If U_info > 350 then seven1p
If U_info > 335 then eight1p
If U_info > 310 then nine1p
If U_info > 0 then off1p
Goto U_Display
U_Sound:
Debug ["U_Sound",13]
ADIN 3,2,ADSETUP,U_info
High B5
Pause 10
RCTIME B5,1,Roto_sw
If Roto_sw > 5 then Demo1
If U_info > 600 then zero2p
If U_info > 500 then one2p
If U_info > 475 then two2p
If U_info > 450 then three2p
If U_info > 425 then four2p
If U_info > 400 then five2p
If U_info > 375 then six2p
If U_info > 350 then seven2p
If U_info > 335 then eight2p
If U_info > 310 then nine2p
If U_info > 0 then off2p
Goto U_Sound

off1:
Poke 7,255
Goto IR_Display

zero1:
Poke 7,240
Goto IR_Display

one1:
Poke 7,241
Goto IR_Display

two1:
65
Ten Projects With The MBasic Compiler

Poke 7,242
Goto IR_Display

three1:
Poke 7,243
Goto IR_Display

four1:
Poke 7,244
Goto IR_Display

five1:
Poke 7,245
Goto IR_Display

six1:
Poke 7,246
Goto IR_Display

seven1:
Poke 7,247
Goto IR_Display

eight1:
Poke 7,248
Goto IR_Display

nine1:
Poke 7,249
Goto IR_Display

off2:
Sound B4,[500\0]
Goto IR_Sound

zero2:
Sound B4,[10\500]
Goto IR_Sound

one2:
Sound B4,[20\500]
Goto IR_Sound

two2:
Sound B4,[40\500]
Goto IR_Sound

three2:
66
Ten Projects With The MBasic Compiler

Sound B4,[70\500]
Goto IR_Sound

four2:
Sound B4,[100\500]
Goto IR_Sound

five2:
Sound B4,[140\500]
Goto IR_Sound

six2:
Sound B4,[180\500]
Goto IR_Sound

seven2:
Sound B4,[220\500]
Goto IR_Sound

eight2:
Sound B4,[260\500]
Goto IR_Sound

nine2:
Sound B4,[300\500]
Goto IR_Sound

off1p:
Poke 7,255
Goto U_Display

zero1p:
Poke 7,15
Goto U_Display

one1p:
Poke 7,31
Goto U_Display

two1p:
Poke 7,47
Goto U_Display

three1p:
Poke 7,63
Goto U_Display
67
Ten Projects With The MBasic Compiler

four1p:
Poke 7,79
Goto U_Display

five1p:
Poke 7,95
Goto U_Display

six1p:
Poke 7,111
Goto U_Display

seven1p:
Poke 7,127
Goto U_Display

eight1p:
Poke 7,143
Goto U_Display

nine1p:
Poke 7,159
Goto U_Display

off2p:
Sound B4,[500\0]
Goto U_Sound

zero2p:
Sound B4,[10\500]
Goto U_Sound

one2p:
Sound B4,[20\500]
Goto U_Sound

two2p:
Sound B4,[40\500]
Goto U_Sound

three2p:
Sound B4,[70\500]
Goto U_Sound

four2p:
Sound B4,[100\500]
68
Ten Projects With The MBasic Compiler

Goto U_Sound

five2p:
Sound B4,[140\500]
Goto U_Sound

six2p:
Sound B4,[180\500]
Goto U_Sound

seven2p:
Sound B4,[220\500]
Goto U_Sound

eight2p:
Sound B4,[260\500]
Goto U_Sound

nine2p:
Sound B4,[300\500]
Goto U_Sound

End

EXPLANATION OF PROGRAM

Project_6 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does.

Start: Program loops until the “START” button is pressed and then goes to
Demo1.

Demo1: The values of each variable are displayed in the debug window. When the
Start button is pressed, the program looks at the rotary switch. The rotary
switch’s four settings determine which sensor variables are looked at and
what numbers are shown on the 7-segment LED display. If the switch
setting is on the first or second position, then the program goes to the
Demo2 routine. If the switch setting is on the third or fourth position, then
the program goes to the Demo3 routine.
69
Ten Projects With The MBasic Compiler

Demo2: The analog to digital conversion of the Sharp IR sensor is stored in its
designated variable. If the switch setting is on the first position, then the
values represented by the IR sensor are translated by if…then statements
and displayed on the right 7-segment LED displays. If the switch setting
is on the second position, the values represented by the IR sensor are
translated by if…then statements and frequencies are sent to the buzzer. If
the rotary switch settings change, the program goes back to Demo1.

Demo3: The analog to digital conversion of the Ultrasonic circuit is stored in its
designated variable. If the switch setting is on the third position, then the
values represented by the Ultrasonic Range Finder are translated by
if…then statements and displayed on the left 7-segment LED displays. If
the switch setting is on the fourth position, the values represented by the
Ultrasonic circuit are translated by if…then statements to frequencies that
are sent to the buzzer. If the rotary switch settings change, the program
goes back to Demo1.

CONCLUSION

Chapter 6 showed how to program the PIC16F876 with the 2840 Development Board.
We were able to display numbers on a couple of common anode 7-segment LED displays
representing changes in value of the IR and Ultrasonic sensors. In the next chapter we
will be adding to Project_4 & 5 a temperature sensor and a fire detector.

70
Ten Projects With The MBasic Compiler

CHAPTER 7: TEMPERATURE & FLAME SENSORS


(Project_7)

TYPES OF TEMPERATURE & FLAME SENSORS

There are many different types of temperature and flame sensors. The use of the sensor
will determine what type you need. For our purposes in this chapter, two inexpensive
sensors will be looked at.

PRECISION TEMPERATURE SENSOR

The LM34CAZ is the temp sensor that will be discussed in this section. The LM34 is a
precision integrated-circuit temperature sensor, whose output voltage is linearly
proportional to the Fahrenheit temperature. This device has a temperature range of –40
to +230 degrees Fahrenheit. The linear output of the LM34 is +10.0 mv/ F scale factor.
If you read the voltage output from the LM34 with your voltage meter, a reading of 0.75
volts indicates a temperature of 75 degrees F. Since the LM34 produces an analog
output, we can use the ADIN command to convert the analog voltage to a digital value.

FLAME DETECTOR

The Hamamatsu C3704 series UV TRON is the flame detector that will be discussed in
this section. This device is known for its high sensitivity to detecting ultraviolet light.
The flame detector can detect a candle flame from 15 feet. It operates in the spectral
range of 185 to 260 nm. Basically the circuitry operates like this: when the UVTron bulb
is exposed to ultraviolet rays (such as from a flame), photoelectrons are emitted from the
cathode and accelerate toward the anode. As the electric field grows stronger, the
molecules of the enclosed gas in the tube eventually ionize. Soon the process causes a
large current between the electrodes, and a discharge takes place. Each time a discharge
occurs, the circuit emits a pulse.

Some environmental conditions can cause false readings from the flame detector. Some
of these are: mercury lamps, halogen lamps, direct or reflected sunlight and electrical
sparks. To help eliminate possible false readings, the flame detector circuit only outputs
a pulse if the UV TRON has detections 3 to 9 times within a minimum of a two-second
interval.

RELAY CIRCUIT

A relay circuit will be used in Project_7 to activate an alarm. When the temp sensor or
flame detector reads above a designated limit, the PIC will activate a relay circuit. The
power lead to a Quorum PAAL alarm is connected when the relay circuit is activated.
The Quorum PAAL is a portable device that creates a very loud alarm when triggered.
71
Ten Projects With The MBasic Compiler

PROJECT_7

The components used in this project are: one LM34 temp sensor, one Hamamatsu UV
TRON Flame Detector, one relay circuit, the rotary switch circuit from Project_5 and the
parts from Project_4.

Fifteen of the twenty-two I/O pins of the PIC16F876 will be used in this project.

Follow the below schematic in wiring Project_7 (This project is added to Project_4 and
part of Project_5).
Precision Temperature Sensor

LM34

Vdd Vss
RA0

Hamamatsu UV TRON
Flame Detector

Vdd Vss

RA3

Vdd

QUORUM
Relay
PAAL

ALARM

10k ohm
RB4 2N4124

Vss

72
Ten Projects With The MBasic Compiler

After you have wired Project_7, open a new file and enter the following program.
Once the program is entered, save the file as: Project_7.

CPU = 16F876
MHZ = 10
CONFIG 16254
73
Ten Projects With The MBasic Compiler

PORTA = $00
TRISC = $00
PORTC = $FF
Low A4
work var byte
Temp_info var word
Fire_info var word
Roto_sw var word

work = 0
ADSETUP con %10001110

START:
Debug ["START",13]
Button B0,1,100,10,work,1,Demo1
Goto START

Demo1:
Debug ["Demo1",13]
Poke 7,255
ADIN 0,2,ADSETUP,Temp_info
Debug ["Temp_info "]
Debug [dec Temp_info,13]
Pulsin A3,1,Fire_info
Debug ["Fire_info "]
Debug [dec Fire_info,13]
High B5
Pause 10
RCTIME B5,1,Roto_sw
Debug ["Roto_sw "]
Debug [dec Roto_sw,13]
Button B0,1,100,10,work,1,Contin
Goto Demo1
Contin:
If Roto_sw > 400 then Demo2
Goto Demo3

Demo2:
Debug ["Demo2",13]
Poke 7,255
If Roto_sw > 700 then Temp_Display
If Roto_sw > 400 then Temp_Relay
Goto Demo1
Temp_Display:
Debug ["Temp_Display",13]
ADIN 0,2,ADSETUP,Temp_info
High B5
74
Ten Projects With The MBasic Compiler

Pause 10
RCTIME B5,1,Roto_sw
If Roto_sw < 700 then Demo1
If Temp_info > 166 then eight6
If Temp_info > 164 then eight4
If Temp_info > 162 then eight2
If Temp_info > 160 then eight0
If Temp_info > 158 then seven8
If Temp_info > 156 then seven6
If Temp_info > 154 then seven4
If Temp_info > 152 then seven2
If Temp_info > 150 then seven0
If Temp_info > 148 then six8
If Temp_info > 146 then six6
If Temp_info > 144 then six4
If Temp_info >= 0 then off1
Goto Temp_Display
Temp_Relay:
Low B4
Debug ["Temp_Relay",13]
ADIN 0,2,ADSETUP,Temp_info
High B5
Pause 10
RCTIME B5,1,Roto_sw
If Roto_sw > 700 then Demo1
If Roto_sw < 400 then Demo1
If Temp_info > 168 then RelayT
If Temp_info > 166 then eight6R
If Temp_info > 164 then eight4R
If Temp_info > 162 then eight2R
If Temp_info > 160 then eight0R
If Temp_info >= 0 then off2
Goto Temp_Relay

Demo3:
Debug ["Demo3",13]
Poke 7,255
If Roto_sw > 100 then Fire_Display
If Roto_sw < 5 then Fire_Relay
Goto Demo1
Fire_Display:
Debug ["Fire_Display",13]
Pulsin A3,1,Fire_info
High B5
Pause 10
RCTIME B5,1,Roto_sw
If Roto_sw > 400 then Demo1
If Roto_sw < 100 then Demo1
75
Ten Projects With The MBasic Compiler

If Fire_info > 2000 then Flash


If Fire_info >= 0 then off3
Goto Fire_Display
Fire_Relay:
Debug ["Fire_Relay",13]
Pulsin A3,1,Fire_info
High B5
Pause 10
RCTIME B5,1,Roto_sw
If Roto_sw > 5 then Demo1
If Fire_info > 2000 then RelayF
If Fire_info >= 0 then off4
Goto Fire_Relay

off1:
Poke 7,255
goto Temp_Display

off2:
Poke 7,255
Low B4
goto Temp_Relay

off3:
Poke 7,255
goto Fire_Display

off4:
Poke 7,255
Low B4
goto Fire_Relay

six4:
Poke 7,100
Goto Temp_Display
six6:
Poke 7,102
Goto Temp_Display
six8:
Poke 7,104
Goto Temp_Display
seven0:
Poke 7,112
Goto Temp_Display
seven2:
Poke 7,114
Goto Temp_Display
seven4:
76
Ten Projects With The MBasic Compiler

Poke 7,116
Goto Temp_Display
seven6:
Poke 7,118
Goto Temp_Display
seven8:
Poke 7,120
Goto Temp_Display
eight0:
Poke 7,128
Goto Temp_Display
eight2:
Poke 7,130
Goto Temp_Display
eight4:
Poke 7,132
Goto Temp_Display
eight6:
Poke 7,134
Goto Temp_Display
eight0R:
Poke 7,128
Low B4
Goto Temp_Relay
eight2R:
Poke 7,130
Low B4
Goto Temp_Relay
eight4R:
Poke 7,132
Low B4
Goto Temp_Relay
eight6R:
Poke 7,134
Low B4
Goto Temp_Relay

RelayT:
High B4
Goto Temp_Relay

RelayF:
High B4
Goto Fire_Relay

Flash:
Poke 7,0
Pause 100
77
Ten Projects With The MBasic Compiler

Poke 7,255
Pause 100
Goto Fire_Display

End

EXPLANATION OF PROGRAM

Project_7 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does.

Start: Program loops until the “START” button is pressed and then goes to
Demo1.

Demo1: The analog to digital conversion of the temp sensor’s output is stored in its
designated variable. The pulse output value of the fire detector is also
stored in its designated variable. The rotary switch’s four settings
determine which sensor variables are looked at and what values are shown
on the 7-segment LED display. If the switch setting is on the first or
second position, then the program goes to the Demo2 routine. If the
switch setting is on the third or fourth position, then the program goes to
the Demo3 routine.

Demo2: If the switch setting is on the first position, then the values represented by
the temp sensor are displayed on the two 7-segment LED displays. If the
switch setting is on the second position, the designated high values
represented by the temp sensor are displayed on the two 7-segment LED
displays. If the variable value is above a certain point, then the relay
circuit is activated. If the switch settings change, the program goes back
to Demo1.

Demo3: If the switch setting is on the third position, then the value represented by
the activation of the fire detector blinks the two 7-segment LED displays.
If the switch setting is on the fourth position, the value represented by the
activation of the fire detector activates the relay circuit. If the switch
settings change, the program goes back to Demo1.

78
Ten Projects With The MBasic Compiler

CONCLUSION

Chapter 7 showed how to program the PIC16F876 with the 2840 Development Board.
We were able to display numbers on a couple of common anode 7-segment LED displays
representing the value output of the temperature sensor. If the value of the temperature
sensor reached a certain point, then the relay circuit was activated. If the flame detector
sensed a flame, then it would blink the LED displays or activate the relay circuit. In the
next chapter we will cover the use of a voice chip and will be programming the
PIC16F877.

79
Ten Projects With The MBasic Compiler

80
Ten Projects With The MBasic Compiler

CHAPTER 8: VOICE RECORD/PLAYBACK DEVICE


(Project_8)

VOICE CHIP

In this chapter we will be using a voice chip. The type being used for Project_8 is the
Information Storage Devices’ ISD25120P Single-Chip Voice Record/Playback Device.
There are several different types of ISD chips such as the ISD2560, ISD2575, ISD2590
and the ISD25120. These chips differ in the length of the recorded message space (60,
75, 90, and 120 seconds). The increase of message space is accomplished by lowering
the audio sampling rate. The lower the sampling rate the lower the quality of the
recording (see Table 8.1). Voice and audio signals are stored directly in nonvolatile
memory cells, providing direct analog storage. This process gives true, natural sounding
reproduction of voice, music, tones and sound effects. The chip is also addressable to
handle multiple messages.

PART DURATION INPUT SAMPLE TYPICAL FILTER PASS


NUMBER (SECONDS) RATE (KHZ) BAND (KHZ)

ISD2560 60 8.0 3.4

ISD2575 75 6.4 2.7

ISD2590 90 5.3 2.3

ISD25120 120 4.0 1.7

Table 8.1

RECORDING MESSAGES

There are two ways to record messages: Operational Mode and Direct Addressing.
Direct Addressing will be used so messages can be recorded at different addresses and
played back in any order. To record an audio segment an address is selected to store the
81
Ten Projects With The MBasic Compiler

beginning of the first message. The P/R pin is taken low and the CE pin is taken low.
Say the message to be recorded. When the message is finished the CE pin is to be taken
high. Repeat this process for each recording. The length of the first message will
determine at what address to start the second message. Each second of the recorded
message (approximately), corresponds to an address length of the decimal value of ten. If
you’re first recorded message was five seconds long and started at address 0(dec.) then
you’re second recorded message would start after address 50(dec.).

PLAYING MESSAGES

To play back a recorded message an address is selected at the beginning of one of the
recorded messages.
The P/R pin is taken high and the CE pin is pulsed low. Repeat this process for each play
back. The play back of the message must be completed before the CE pin can activate
the next message.

AMPLIFICATION CIRCUIT

The audio output of the ISD chip is capable of driving 50mw into a 16 ohm speaker. I
have added an amplification circuit to drive a 2-watt, 8-ohm speaker.

PROJECT_8

The components used in this project are one ISD25120P Voice Record/Playback chip,
one pre-amp chip with circuitry and one amplification chip with circuitry. The 40 pin
PIC16F877 microcontroller needs to be placed in the 2840 Development Board. The
2840 Development Board already has Port’s A,B and C connected to a 25-pin DB
connector (Connector A). That connector is attached to the board enclosure. Port’s D
and E need to be wired to a 15 pin DB connector (Connector B). That connector needs to
be attached to the other side of the enclosure. There will be two ribbon cables going to
Project_8.

Twenty-three of the thirty-three I/O pins of the PIC16F877 will be used in this project.

Follow the below schematic in wiring Project_8.

82
Ten Projects With The MBasic Compiler

PIC CONNECTOR (B)


ISD25120

D0 PIN 1 A0 Vccd Vdd


D1 PIN 2
A1 Vcca

D2 PIN 3 A2
0.1uf
D3 PIN 4 A3 Vssd

A4 Vssa
D4 PIN 5 Vss
D5 PIN 6 A5

A6 SP +
D6 PIN 7 To amplification ckt.
D7 PIN 8 A7 SP –
Vss
A8 AUX IN 0.1uf
PIC CONNECTOR (A) Vss A9 ANA IN
10k ohm
A0 PIN 1 CE ANA OUT
5.1k ohm
PD

100k ohm P/R MIC REF


0.1uf Mic
EOM MIC
Vss Vdd OVF
0.1uf
XCLK AGC

100k ohm
10k ohm
1k ohm 0.1uf 1k ohm
Vdd Vss Vss

(P) Vss 220uf

(R) Vdd Vss Vss Vdd

83
Ten Projects With The MBasic Compiler

PIC CONNECTOR (A)

10k ohms
Vss

A1 PIN 2 #6 Forward Vdd

Vss

A2 PIN 3 #7 Left Vdd

Vss
#8 Right
A3 PIN 4 Vdd

Vss
#9 Reverse
A4 PIN 5 Vdd

Vss
#1
B0 PIN 6 Vdd

Vss

B1 PIN 7 #2 Vdd

Vss

B2 PIN 8 #3 Vdd

Vss

#4
C5 PIN 18 Vdd

Vss

B5 PIN 11 #5 Vdd

LEDs 330 ohms


C0 PIN 23 #1 Vss

C1 PIN 22 #2 Vss

C2 PIN 21 #3 Vss

C3 PIN 20 #4 Vss

C4 PIN 19 #5 Vss

84
Ten Projects With The MBasic Compiler

100k ohm

1uf 1k ohm 10k ohm


2 1uf 10uf
1 3 5
TL082 LM386
From ISD25120
3
2
1uf 10k ohm

Vss Vss

100k ohm 100k ohm


100uf

Vdd Vss 8 ohm speaker

Vss

85
Ten Projects With The MBasic Compiler

After you have wired Project_8, open a new file and enter the following program.
Once the program is entered, save the file as: Project_8.

CPU = 16F877
MHZ = 10
CONFIG 16382

Work var byte


Work = 0
Poke 133,0
Poke 135,32
Poke 136,0
High A0

Start:
Debug["Press #1 for Demo1",13]
Pause 500
Debug["Press #2 for Demo2",13]
Pause 500
Debug["Press #3 for Demo3",13]
Pause 500
Button B0,1,100,10,Work,1,Demo1
Button B1,1,100,10,Work,1,Demo2
Button B2,1,100,10,Work,1,Demo3
Goto Start

Demo1:
86
Ten Projects With The MBasic Compiler

Poke 7,0
High A0
Debug["Place SW1 in Record",13]
Debug["Press a button to record a message",13]
Pause 1000
Button B0,1,100,10,Work,1,Word1
Button B1,1,100,10,Work,1,Word2
Button B2,1,100,10,Work,1,Word3
Button C5,1,100,10,Work,1,Word4
Button B5,1,100,10,Work,1,Word5
Button A1,1,100,10,Work,1,Word6
Button A2,1,100,10,Work,1,Word7
Button A3,1,100,10,Work,1,Word8
Button A4,1,100,10,Work,1,Word9
Goto Demo1

Word1:
Poke 8,%00000000
High C0
Pause 100
Low A0
Button B0,1,100,10,Work,1,Demo1
Goto Word1

Word2:
Poke 8,%00010100
High C1
Pause 100
Low A0
Button B1,1,100,10,Work,1,Demo1
Goto Word2

Word3:
Poke 8,%00101000
High C2
Pause 100
Low A0
Button B2,1,100,10,Work,1,Demo1
Goto Word3

Word4:
Poke 8,%00111100
High C3
Pause 100
Low A0
Button C5,1,100,10,Work,1,Demo1
Goto Word4

87
Ten Projects With The MBasic Compiler

Word5:
Poke 8,%01010000
High C4
Pause 100
Low A0
Button B5,1,100,10,Work,1,Demo1
Goto Word5

Word6:
Poke 8,%01100100
Pause 100
Low A0
Button A1,1,100,10,Work,1,Demo1
Goto Word6

Word7:
Poke 8,%01110011
Pause 100
Low A0
Button A2,1,100,10,Work,1,Demo1
Goto Word7

Word8:
Poke 8,%10000010
Pause 100
Low A0
Button A3,1,100,10,Work,1,Demo1
Goto Word8

Word9:
Poke 8,%10010001
Pause 100
Low A0
Button A4,1,100,10,Work,1,Start
Goto Word9

Demo2:
High A0
Poke 7,0
Debug["Place SW1 in Playback",13]
Debug["Press a button to play a message",13]
Pause 1000
Button B0,1,100,10,Work,1,Button1
Button B1,1,100,10,Work,1,Button2
Button B2,1,100,10,Work,1,Button3
Button C5,1,100,10,Work,1,Button4
Button B5,1,100,10,Work,1,Button5
Button A1,1,100,10,Work,1,Button6
88
Ten Projects With The MBasic Compiler

Button A2,1,100,10,Work,1,Button7
Button A3,1,100,10,Work,1,Button8
Button A4,1,100,10,Work,1,Button9
Goto Demo2

Button1:
Poke 8,%00000000
High C0
Pause 500
Low A0
Pause 100
Goto Demo2

Button2:
Poke 8,%00010100
High C1
Pause 500
Low A0
Pause 100
Goto Demo2

Button3:
Poke 8,%00101000
High C2
Pause 500
Low A0
Pause 100
Goto Demo2

Button4:
Poke 8,%00111100
High C3
Pause 500
Low A0
Pause 100
Goto Demo2

Button5:
Poke 8,%01010000
High C4
Pause 500
Low A0
Pause 100
Goto Demo2

Button6:
Poke 8,%01100100
Pause 500
89
Ten Projects With The MBasic Compiler

Low A0
Pause 100
Goto Demo2

Button7:
Poke 8,%01110011
Pause 500
Low A0
Pause 100
Goto Demo2

Button8:
Poke 8,%10000010
Pause 500
Low A0
Pause 100
Goto Demo2

Button9:
Poke 8,%10010001
Pause 500
Low A0
Pause 100
Goto Start

Demo3:
High A0
Poke 7,0
Pause 1700
Debug["Playback all messages",13]
Poke 8,%00000000
Low A0
Pause 200
High A0
Pause 3000
Poke 8,%00010100
Low A0
Pause 200
High A0
Pause 3000
Poke 8,%00101000
Low A0
Pause 200
High A0
Pause 3000
Poke 8,%00111100
Low A0
Pause 200
90
Ten Projects With The MBasic Compiler

High A0
Pause 3000
Poke 8,%01010000
Low A0
Pause 200
High A0
Pause 3000
Poke 8,%01100100
Low A0
Pause 200
High A0
Pause 3000
Poke 8,%01110011
Low A0
Pause 200
High A0
Pause 3000
Poke 8,%10000010
Low A0
Pause 200
High A0
Pause 3000
Poke 8,%10010001
Low A0
Pause 200
High A0
Pause 3000
Goto Start

End

EXPLANATION OF PROGRAM

Project_8 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does.

Start: Program loops until a specified (#1 or #2 or #3) button is pressed and then
goes to the specific Demo designated.

Demo1: Records messages. The Debug Window will instruct for SW1 to be
placed in the Record (R) position. When a button (#1-9) is pressed the
91
Ten Projects With The MBasic Compiler

LED next to the button will turn on and the yellow (CE) LED will turn on.
Say the message. When finished, press the button again to stop the
recording (the yellow LED will turn off). After recording Word9
(Reverse), the program will go to the “Start” routine.

Demo2: Plays back messages. The Debug Window will instruct for SW1 to be
placed in the Playback (P) position. When a button (#1-9) is pressed the
LED next to the button will flash on and the yellow (CE) LED will flash
on. The message will be played. After playing Word9 (Reverse), the
program will go to the “Start” routine.

Demo3: Plays all recorded messages (#1-9). After the last message is played the
program will go to the “Start” routine.

CONCLUSION

Chapter 8 showed how to program the PIC16F877 with the 2840 Development Board.
Nine messages were recorded and played back by the ISD25120 chip. In the next chapter
RF communication will be used for Project_8 to communicate to Project_9 and control
two DC motors.

92
Ten Projects With The MBasic Compiler

CHAPTER 9: REMOTE CONTROL


(Project_9a & 9b)

PROJECT_8 CHANGES

This chapter will use RF to have commands sent from Project_8 (Project_9a) to
Project_9b. Several changes to Project_8 need to happen for this to take place.

1. RF transmitter with antenna connected.


2. Encoder chip installed (HT-640)
3. 40-pin socket wired.
4. Project_8 program changed
5. 16F877 programmed and placed in 40-pin socket.
6. Battery power supply connected.

These changes are illustrated in the schematic section of this chapter.


The program changes made to Project_8 are in the program labeled Project_9a.

RF TRANSMITTER

The transmitter being used is the LINX Technologies TXM-433-LC. It is capable of


transferring serial data at distances in excess of 300 feet. No external RF components,
except an antenna are required. The TXM transmits data using CPCA (carrier-present
carrier-absent) modulation. This type of AM modulation is often referred to by other
designations including CW and OOK. This type of modulation represents a logic “0” by
the absence of a carrier and a logic “1” by the presence of a carrier.

The TXM-433 data input (TTL & CMOS compatible) is provided on Pin 8 (Dout) of the
Encoder chip. When the input has a logic low, the carrier is fully suppressed and the
transmitter consumes less than 2uA of current. When the input has a logic high the
transmitter generates a carrier to indicate to the receiver the presence of a logic 1.

RF RECEIVER & ANTENNA’S

The receiver being used is the LINX Technologies RXM-433-LC. It is designed to


recover data sent by a CPCA transmitter (TXM-433). The receiver’s output will only

93
Ten Projects With The MBasic Compiler

transition when valid data is present. When there is no carrier present the output will
remain low.

The transmitting and receiving antennas being used here is a basic quarter-wave whip. A
whip-style monopole antenna is inexpensive and can be easily fabricated from a wire or
rod. The proper length for a quarter-wave antenna can be easily found using the formula
below.

L= 234 / Fmhz L= Length in feet of quarter-wave length


F= Operating frequency in megahertz

The transmitter and receiver are operating at 433 Mhz. The length of the quarter-wave
antenna is calculated to be 6.5 inches.

MOTOR ASSEMBLY

Two DC geared 12V motors are attached on a (Tape Reel) platform. Wheels are attached
to the motor rotors. An H-Bridge chip will drive the two motors. A separate power
supply will be used to power the motors.

PROJECT_9a & 9b

A RF Transmitter, Encoder and battery power supply is added to Project_8 (Project_9a).


Also added is a 40-pin socket that the programmed 16F877 can be placed in. The
components of Project_9b are: A RF Receiver, Decoder, two 12VDC geared motors with
a battery power supply attached to a platform, a motor driver (SN754410), a 74LS04
chip, an LCD display and nine button switches.

Twenty-four of the thirty-three I/O pins of the PIC16F877 are used in Project_9a.
Sixteen of the thirty-three I/O lines are used in Project_9b (more pins will be used in the
next and final project).

Follow the below schematic in the wiring changes to Project_8. Project_8 will now be
called Project_9a.

94
Ten Projects With The MBasic Compiler

Antenna

TXM-433

1 2 3 4

Vss Encoder chip pin 8 (Dout)

10uf 10 430
(tantalum) ohm ohm

Vss Vdd Gnd

Vdd

LED

5 Volt
Regulator 1.5 V 1.5 V 1.5 V 1.5 V 330
Out Gnd In ohm
Power switch
Vdd

Disconnect power (Vdd) from 2840 Development Bd.

95
Ten Projects With The MBasic Compiler

Connector (A) Connector (A)


PIC PIN 4.7k ohm
1 16F877 40
Vdd B7 PIN 13 Encoder chip
socket
pin 9 (TE)
2 39
A0 PIN 1 Voice chip (CE)
3 38
A1 PIN 2 Button #6 (Forward) B5 PIN 11 Button #5
4 37
A2 PIN 3 Button #7 (Left)
5 36
A3 PIN 4 Button #8 (Right)
6 35
A4 PIN 5 Button #9 (Reverse) B2 PIN 8 Button #3
7 34
B1 PIN 7 Button #2
8 33
B0 PIN 6 Button #1
9 32
Vdd
Vss
10 31
Vdd Connector (B)
11 30
D7 PIN 8 Voice (A7)*
Vss
12 29
Resonator D6 PIN 7 Voice (A6)*

10 13 28
Vss D5 PIN 6 Voice (A5)*
Mhz
14 27
D4 PIN 5 Voice (A4)*
15 26
C0 PIN 23 LED #1
16 25
C1 PIN 22 LED #2
Connector (A)
17 24
C2 PIN 21 LED #3 C5 PIN 18 Button #4
18 23
C3 PIN 20 LED #4 C4 PIN 19 LED #5
Connector (B) Connector (B)
19 22
D0 PIN 1 Voice chip (A0)* D3 PIN 4 Voice (A3)*
20 21
D1 PIN 2 Voice chip (A1)* D2 PIN 3 Voice (A2)*

* Add a 5 k ohm resistor tied to ground.

96
Ten Projects With The MBasic Compiler

8-BIT ENCODER

1
B2 (Button #3) D1 Vcc Vdd
2
C5 (Button #4) D2 D0 B1 (Button #2)
3
B5 (Button #5) D3 A9
4
A1 (Button #6) D4 A8
5
A2 (Button #7) D5 A7
6
A3 (Button #8) D6 A6
7
A4 (Button #9) D7 A5
Transmitter (TXM-433) 8
Dout A4
Transmit enable
B7 TE A3

390k ohm OSC1 A2

OSC2 A1

Vss GND A0

HT-640

97
Ten Projects With The MBasic Compiler

Follow the below schematic in wiring Project_9b.


Antenna

RXM-433

1 2 3 4

Decoder chip pin 9 (Din) Vss

10uf 10 200
(tantalum) ohm ohm

Vss Vdd

LCD MODULE

Vdd Vss
20k ohms 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Vss Vdd Vo RS R/W E 0 1 2 3 4 5 6 7

Connector (A)

Vss PIN 14
Vdd PIN 25
B0 PIN 6
B1 PIN 7

A0 PIN 1
A1 PIN 2
A2 PIN 3
A3 PIN 4

10k ohm
Vss
#1
C4 PIN 19 Vdd

Vss

C3 PIN 20 #2 Vdd

98
Ten Projects With The MBasic Compiler

Vss

C2 PIN 21 #3 Vdd

Vss

#4
C1 PIN 22 Vdd

Vss

C0 PIN 23 #5 Vdd

Vss

C7 PIN 16 #6 Vdd

Connector (B) Vss

E0 PIN 9 #7 Vdd

Vss

#8
E1 PIN 10 Vdd

Vss

E2 PIN 11 #9 Vdd

12V motor power Vdd


Connector (A)
8 16

B3 PIN 9 1 3
M1 enable
C5 PIN 18 2
M1 input A R motor
74LS04
pins 1,2 7 M1 input B 6

B2 PIN 8 9 14
M2 enable
C6 PIN 17 10
M2 input A L motor
74LS04
pins 3,4 15 M2 input B 11

4,5,12,13

Vss

99
Ten Projects With The MBasic Compiler

8-BIT DECODER
De-activate Decoder switch *
1
C2 (Button #3) D1 Vcc Vdd
2
C1 (Button #4) D2 D0 C3 (Button #2)
3
C0 (Button #5) D3 A9
4
C7 (Button #6) D4 A8
5
E1 (Button #7) D5 A7
6
E2 (Button #8) D6 A6
7
E3 (Button #9) D7 A5
8 Vdd
VT A4
Data input from RF RCVR 9
Din A3
LED
390k ohm OSC1 A2

OSC2 A1
470 ohms
Vss GND A0

HT-648L 2.2k ohm


Valid transmit output
2N3904

Vss

* When switch is open, buttons #2-9 can be used on Project_9b.

100
Ten Projects With The MBasic Compiler

After you have modified Project_8 (now Project_9a), open a new file and enter the
following program. Once the program is entered, save the file as: Project_9a. Program
the 16F877 and install it in the socket on Project_9a.

CPU = 16F877
MHZ = 10
CONFIG 16382
101
Ten Projects With The MBasic Compiler

Work var byte


Work = 0
Poke 133,0
Poke 135,32
Poke 136,0

Start:
High A0
Debug["Press #1 for Demo1",13]
Pause 500
Debug["Press #3 for Demo3",13]
Pause 500
Button B0,1,100,10,Work,1,Demo1
Button B2,1,100,10,Work,1,Demo3
Debug["Jump to Demo2",13]
Goto Demo2

Demo1:
Poke 7,0
High A0
Debug["Place SW1 in Record",13]
Debug["Press a button to record a message",13]
Pause 1000
Button B0,1,100,10,Work,1,Word1
Button B1,1,100,10,Work,1,Word2
Button B2,1,100,10,Work,1,Word3
Button C5,1,100,10,Work,1,Word4
Button B5,1,100,10,Work,1,Word5
Button A1,1,100,10,Work,1,Word6
Button A2,1,100,10,Work,1,Word7
Button A3,1,100,10,Work,1,Word8
Button A4,1,100,10,Work,1,Word9
Goto Demo1

Word1:
Poke 8,%00000000
High C0
Pause 100
Low A0
Button B0,1,100,10,Work,1,Demo1
Goto Word1

Word2:
Poke 8,%00010100
High C1
Pause 100
Low A0
102
Ten Projects With The MBasic Compiler

Button B1,1,100,10,Work,1,Demo1
Goto Word2

Word3:
Poke 8,%00101000
High C2
Pause 100
Low A0
Button B2,1,100,10,Work,1,Demo1
Goto Word3

Word4:
Poke 8,%00111100
High C3
Pause 100
Low A0
Button C5,1,100,10,Work,1,Demo1
Goto Word4

Word5:
Poke 8,%01010000
High C4
Pause 100
Low A0
Button B5,1,100,10,Work,1,Demo1
Goto Word5

Word6:
Poke 8,%01100100
Pause 100
Low A0
Button A1,1,100,10,Work,1,Demo1
Goto Word6

Word7:
Poke 8,%01110011
Pause 100
Low A0
Button A2,1,100,10,Work,1,Demo1
Goto Word7

Word8:
Poke 8,%10000010
Pause 100
Low A0
Button A3,1,100,10,Work,1,Demo1
Goto Word8

103
Ten Projects With The MBasic Compiler

Word9:
Poke 8,%10010001
Pause 100
Low A0
Button A4,1,100,10,Work,1,Start
Goto Word9

Demo2:
High A0
Poke 7,0
Debug["Place SW1 in Playback",13]
Debug["Press a button to play a message",13]
Pause 1000
Button B0,1,100,10,Work,1,Button1
Button B1,1,100,10,Work,1,Button2
Button B2,1,100,10,Work,1,Button3
Button C5,1,100,10,Work,1,Button4
Button B5,1,100,10,Work,1,Button5
Goto Demo2

Button1:
Poke 8,%00000000
High C0
Low A0
High B7
Pause 700
Low B7
Loop:
High A0
High B7
Pause 100
Low B7
Button A1,1,100,10,Work,1,Button6
Button A2,1,100,10,Work,1,Button7
Button A3,1,100,10,Work,1,Button8
Button A4,1,100,10,Work,1,Button9
Button B1,1,100,10,Work,1,Button2
Button B2,1,100,10,Work,1,Button3
Button C5,1,100,10,Work,1,Button4
Button B5,1,100,10,Work,1,Button5
Goto Loop

Button2:
Poke 8,%00010100
Low C0
High C1
Low A0
High B7
104
Ten Projects With The MBasic Compiler

Pause 700
Low B7
Goto Demo2

Button3:
Poke 8,%00101000
Low C0
High C2
Low A0
High B7
Pause 700
Low B7
Goto Demo2

Button4:
Poke 8,%00111100
Low C0
High C3
Low A0
High B7
Pause 700
Low B7
Goto Demo2

Button5:
Poke 8,%01010000
Low C0
High C4
Low A0
High B7
Pause 700
Low B7
Goto Demo2

Button6:
Poke 8,%01100100
Low A0
High B7
Pause 700
Low B7
Goto Loop

Button7:
Poke 8,%01110011
Low A0
High B7
Pause 700
Low B7
105
Ten Projects With The MBasic Compiler

Goto Loop

Button8:
Poke 8,%10000010
Low A0
High B7
Pause 700
Low B7
Goto Loop

Button9:
Poke 8,%10010001
Low A0
High B7
Pause 700
Low B7
Goto Loop

Demo3:
High A0
Poke 7,0
Pause 1700
Debug["Playback all messages",13]
Poke 8,%00000000
Low A0
Pause 200
High A0
Pause 3000

Poke 8,%00010100
Low A0
Pause 200
High A0
Pause 3000

Poke 8,%00101000
Low A0
Pause 200
High A0
Pause 3000

Poke 8,%00111100
Low A0
Pause 200
High A0
Pause 3000

Poke 8,%01010000
106
Ten Projects With The MBasic Compiler

Low A0
Pause 200
High A0
Pause 3000

Poke 8,%01100100
Low A0
Pause 200
High A0
Pause 3000

Poke 8,%01110011
Low A0
Pause 200
High A0
Pause 3000

Poke 8,%10000010
Low A0
Pause 200
High A0
Pause 3000

Poke 8,%10010001
Low A0
Pause 200
High A0
Pause 3000
Goto Start

End

After you have wired Project_9b, open a new file and enter the following program. Once
the program is entered, save the file as: Project_9b.

CPU = 16F877
MHZ = 10
CONFIG 16382

Work var byte


Work = 0
107
Ten Projects With The MBasic Compiler

LCDWRITE[INITLCD1,INITLCD2,CLEAR,HOME,SCR]
pause 1500

Start:
Low B2
Low A4
Button C3,1,100,10,Work,1,Demo2
Button C2,1,100,10,Work,1,Demo3
Button C1,1,100,10,Work,1,Demo4
Button C0,1,100,10,Work,1,Demo5

Demo1:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["REMOTE CONTROL"]
PAUSE 100
Low B2
Low A4
Debug["Demo1 "]
Button C7,1,100,10,Work,1,Forward
Button E0,1,100,10,Work,1,Left
Button E1,1,100,10,Work,1,Right
Button E2,1,100,10,Work,1,Revers
Button C3,1,100,10,Work,1,Demo2
Button C2,1,100,10,Work,1,Demo3
Button C1,1,100,10,Work,1,Demo4
Button C0,1,100,10,Work,1,Demo5
Goto Demo1

Forward:
Low C5
Low C6
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["FORWARD"]
High B2
High A4
Pause 1000
Goto Demo1

Left:
Low C6
High B2
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["LEFT"]
Pause 1000
Goto Demo1

Right:
Low C5
108
Ten Projects With The MBasic Compiler

High A4
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["RIGHT"]
Pause 1000
Goto Demo1

Revers:
High C5
High C6
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["REVERSE"]
High B2
High A4
Pause 1000
Goto Demo1

Demo2:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["OBJECT DETECT"]
PAUSE 1500
GOTO Start

Demo3:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["EDGE AVOIDANCE"]
PAUSE 1500
GOTO Start

Demo4:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["FIRE/TEMP READ"]
PAUSE 1500
GOTO Start

Demo5:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["LINE FOLLOWING"]
PAUSE 1500
GOTO Start

END

109
Ten Projects With The MBasic Compiler

EXPLANATION OF PROGRAMS

Project_9a has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is a
short description of what each part of the program does. The program is basically the
same as Project_8 except for minor changes to the Start and Demo2 routines.

Start: At the beginning of the program if #1 button is pressed, then the program
will go to Demo1 (recording messages). If #3 button is pressed at the
beginning of the program, the program will go to Demo3 (playback all
recorded messages). If no buttons are pressed at the beginning of the
program, then the program goes to Demo2 (playback message for each
button pressed).

Demo1: Records messages. SW1 needs to be placed in the Record (R) position.
When a button (1-9) is pressed the LED next to the button will turn on and
the yellow (CE) LED will turn on. Say the message. When finished,
press the button again to stop the recording (the yellow LED will turn off).
After recording Word9, the program will go to the “Start” routine.

Demo2: Plays back messages. SW1 needs to be placed in Playback (P) position.
When a button (#1-9) is pressed the LED next to the button will flash on
and the yellow (CE) LED will flash on. A specified message will be
played for each button pressed. The button information also goes to the
Encoder. The output of the Encoder goes to the transmitter. Buttons #6-9
will play their messages only when button #1 (remote control) is pressed.

Demo3: Plays all recorded messages (#1-9). After the last message is played the
program will go to the “Start” routine.

Project_9b has six parts labeled: Start, Demo1, Demo2, Demo3, Demo4 and Demo5.
Following is a short description of what each part of the program does.

Start: At the beginning of the program if any of the button switches are pressed
on Project_9a
110
Ten Projects With The MBasic Compiler

the program will go to the Demo routines specified by the button pressed.
If now buttons are pressed the program will go to Demo1.

Demo1: Displays “REMOTE CONTROL” on the LCD and waits for an RF signal
from Project_9a. If buttons (#6-9) are pressed the program will go to the
routines to activate the motors (Forward, Left, Right, Reverse). If other
buttons (#1-5) are pressed the program will go to those specified routines.

Demo2: Displays “OBJECT DETECT” on the LCD and returns to the “Start”
routine. Demo2 will have more functions in Project_10.

Demo3: Displays “EDGE AVOIDANCE” on the LCD and returns to the “Start”
routine. Demo3 will have more functions in Project_10.

Demo4: Displays “FIRE/TEMP READ” on the LCD and returns to the “Start”
routine. Demo4 will have more functions in Project_10.

Demo5: Displays “LINE FOLLOWING” on the LCD and returns to the “Start”
routine. Demo5 will have more functions in Project_10.

CONCLUSION

Chapter 9 showed how to program the PIC16F877 with the 2840 Development Board.
RF communication was used to have communication between Project_9a and Project_9b.
An LCD (on Project_9b) was used to display what buttons were pressed. Two DC
motors were installed with an H-bridge for Project_9b to have locomotion.

111
Ten Projects With The MBasic Compiler

112
Ten Projects With The MBasic Compiler

CHAPTER 10: MULTI-PURPOSE ROBOT


(Project_10)

PROJECT_9b CHANGES

This chapter will assemble the sensors from previous chapters and implement them in
making a multi-purpose robot. Several changes to Project_9b need to happen for this to
take place.

1. Connectors installed
2. Quad bilateral switches installed
3. 40-pin socket wired (for 16F877)
4. Project_9b program changed
5. 16F877 programmed and placed in 40-pin socket
6. Battery power supply connected

These changes are illustrated in the schematic section of this chapter. The program
changes made to Project_9b are in the new program written for Project_10.

SENSORS

The sensors being used in this chapter are described in previous chapters. The ones being
used here are:

• Flex sensors and Photocells (described in chapter 5)


• Ultrasonic sensor and IR sensors (described in chapter 6)
• Fire and Temperature sensor (described in chapter 7)

The Flex sensors will be used for preventing the robot from falling off a platform or
tabletop. The Photocells will be used to find a black line for a line following routine.
The Ultrasonic sensor and IR sensors will detect objects for the robot to avoid. The
Temperature sensor will have the temperature displayed on an LCD. The Fire sensor will
detect if there is a fire and display that message on an LCD.

QUAD BILATERAL SWITCHES

Three 4066 (quad bilateral switch) chips will be used in this project, they are labeled A,
B, and C. The enable control of each chip will be used to determine which sensor
information is sent to the 16F877 D/A inputs. The switches assigned to the sensors are:

• Chip (A) will send the Flex and Temperature data


• Chip (B) will send the IR and Ultrasonic data
• Chip (C) will send the Photocell data
113
Ten Projects With The MBasic Compiler

CONNECTORS

Each sensor will have a cable connected to the main circuit board. Nine connectors are
needed. The following list describes the connectors used.

IR1 connector * Pin 1 Vss to left infrared sensor


* Pin 2 Analog signal from left infrared sensor
* Pin 3 Vdd to left infrared sensor

IR2 connector * Pin 1 Vss to right infrared sensor


* Pin 2 Analog signal from right infrared sensor
* Pin 3 Vdd to right infrared sensor

Line connector* Pin 1 Vdd to line following photocell circuit


* Pin 2 Analog signal from left photocell group
* Pin 3 Analog signal from center photocell group
* Pin 4 Analog signal from right photocell group
* Pin 5 Vss to line following photocell circuit

LED connector* Pin 1Negative lead of 9V battery to LED circuit


* Pin 2 Return to LED on main circuit board
* Pin 3 Positive lead of 9V battery through relay to LED circuit

Ultrasonic
Connector * Pin 1 Vss to ultrasonic sensor
* Pin 2 Analog signal from ultrasonic circuit
* Pin 3 Vdd to ultrasonic sensor

Motor
Connector * Pin 1 Output of H-bridge to positive lead of right motor
* Pin 2 Output of H-bridge to negative lead of right motor
* Pin 3 Output of H-bridge to positive lead of left motor
* Pin 4 Output of H-bridge to negative lead of left motor

Motor Pwr
Connector * Pin 1 Negative lead of 12 Volt battery to H-bridge
* Pin 2 Positive lead of 12 Volt battery to H-bridge

Flex connector* Pin 1 Vss to flex sensors


* Pin 2 Analog signal from left flex sensor
* Pin 3 Analog signal from right flex sensor
* Pin 4 Vdd to flex sensors
114
Ten Projects With The MBasic Compiler

Battery
Connector * Pin 1 Negative lead of 12 Volt battery supply to 5V regulator (Vss)
* Pin 2 Positive lead of 12 Volt battery supply to 5V regulator (Vdd)

PROJECT_10

4066 switches, connectors and a battery supply are added to Project_9b. Also added is a
40-pin socket that the programmed 16F877 can be place in. The components of
Project_10 are: three 4066 chips, a 5 Volt relay, a NPN transistor, a diode, two resistors,
LEDs, a 9 Volt battery, sensors from previous projects and nine connectors.

Twenty-six of the thirty-three I/O pins of the PIC16F877 are used in Project_10.

Follow the below schematic in wiring Project_10.


Connector (A)
2840 Development Board 25-pin DB connector (male)

A0 1 4066 chips pin 1


A1 2 4066 chips pin 4
A2 3 4066 chips pin 8
A3 4
A4 5 Motor (M1 Enable)
B0 6 LCD (RS)
B1 7 LCD (E)
B2 8 Motor (M2 Enable)
B3 9
B4 10
B5 11
B6 12
B7 13
Vdd 14
15
C7 16 Decoder (Button #6 - Forward)
C6 17 Motor (M2 input A)
C5 18 Motor (M1 input A)
C4 19 Decoder (Button #1 – Remote Control)
C3 20 Decoder (Button #2 – Object Detection)
C2 21 Decoder (Button #3 – Edge Avoidance)
C1 22 Decoder (Button #4 – Fire/Temp Reading)
C0 23 Decoder (Button #5 – Line Following)
24
Vss 25

115
Ten Projects With The MBasic Compiler

Connector (B)
15-pin DB connector (male)

D0 1 LCD pin 11
D1 2 LCD pin 12
D2 3 LCD pin 13
D3 4 LCD pin 14
D4 5 Switch Control (A)
D5 6 Switch Control (B)
D6 7 Switch Control (C)
D7 8 Fire detection
E0 9 Decoder (Button #7 – Left)
E1 10 Decoder (Button #8 – Right)
E2 11 Decoder (Button #9 – Reverse)
12
13
14
15

Connector (A) Quad Bilateral Switch


PIC PIN 4066 Demo3 & Demo4

1 2
A0 PIN 1 A/D input Flex sensor 1
13

4 3
A1 PIN 2 A/D input Flex sensor 2
5

A2 PIN 3 A/D input 8 9 Temp sensor

Connector (B) 11 10 Vdd

D4 PIN 5 Switch control (A) 12

Vss 7 14 Vdd

(A)

116
Ten Projects With The MBasic Compiler

Connector (A) Demo2


1 2
A0 PIN 1 A/D input IR1 sensor
13

4 3
A1 PIN 2 A/D input IR2 sensor
5

A2 PIN 3 A/D input 8 9 Ultra sonic sensor

Connector (B) 11 10 Vdd

D5 PIN 6 Switch control (B) 12

Vss 7 14 Vdd

(B)

Connector (A) Demo5


1 2
A0 PIN 1 A/D input Line 1 photocell
13

4 3
A1 PIN 2 A/D input Line 3 photocell
5

A2 PIN 3 A/D input 8 9 Line 2 photocell

Connector (B) 11 10 Vdd

D6 PIN 7 Switch control (C) 12

Vss 7 14 Vdd

(C) 9 Volts
Vdd + _

diode Relay --- LEDs


coil
**

10k ohm etc.

Vss
Light to illuminate path for line following

117
Ten Projects With The MBasic Compiler

Switch (C) pin 2 Switch (C) pin 9 Switch (C) pin 3

**

Left Photocells Center Photocells Right Photocells

Vdd Vss Vdd Vss

** Components located on line following board (placed below robot, close to floor)

Connector (A)
PIC PIN 4.7k ohm
1 16F877 40
Vdd
socket
21 39
A0 PIN 1 Switches pin 1
22 38
A1 PIN 2 Switches pin 4
23 37
A2 PIN 3 Switches pin 8
24 36
Connector (A)
25 35
A4 PIN 5 Motor (M1 Enable) B2 PIN 8 Motor (M2 Enable)
26 34
Connector (B) B1 PIN 7 LCD (E)
E0 PIN 9 Decoder (Left)
27 33
B0 PIN 6 LCD (RS)
E1 PIN 10 Decoder (Right)
28 32
Vdd
E2 PIN 11 Decoder (REV) Vss
29 31
Vdd Connector (B)
30 30
D7 PIN 8 Fire detect
Vss
31 29
Resonator D6 PIN 7 Switch C control

10 32 28
Vss D5 PIN 6 Switch B control
Mhz
33 27
D4 PIN 5 Switch A control
Connector (A)
34 26
C0 PIN 23 Button #5 C7 PIN 16 Decoder (FWD)
35 25
C1 PIN 22 Button #4 C6 PIN 17 Motor (M2 A)
36 24
C2 PIN 21 Button #3 C5 PIN 18 Motor (M1 A)
37 23
C3 PIN 20 Button #2 C4 PIN 19 Button #1
Connector (B) Connector (B)
38 22
D0 PIN 1 LCD pin 11 D3 PIN 4 LCD pin 14
39 21
D1 PIN 2 LCD pin 12 D2 PIN 3 LCD pin 13

118
Ten Projects With The MBasic Compiler

Vdd

LED

5 Volt
Regulator 1.5 V 1.5 V 1.5 V 1.5 V 330
Out Gnd In ohm
Power switch
Vdd

Disconnect power (Vdd) from 2840 Development Bd. 1.5 V 1.5 V 1.5 V 1.5 V

119
Ten Projects With The MBasic Compiler

120
Ten Projects With The MBasic Compiler

121
Ten Projects With The MBasic Compiler

After you have wired Project_10, open a new file and enter the following program.
Once the program is entered, save the file as: Project_10. Program the 16F877 and install
it in the socket on Project_10.

CPU = 16F877
MHZ = 10
CONFIG 16382

LCDPORT con PORTD


Pause 500
LCDWRITE[INITLCD1,INITLCD2,CLEAR,HOME,SCR]
TRISA = $FF
ADSETUP con %10000010
IR1_info var word
IR2_info var word
U_info var word
IR1_init var word
IR2_init var word
U_init var word
FIRE_info var word
TEMP_info var word
FLEX1_info var word
FLEX2_info var word
FLEX1_init var word
FLEX2_init var word
122
Ten Projects With The MBasic Compiler

LINE1_info var word


LINE2_info var word
LINE3_info var word
LINE1_init var word
LINE2_init var word
LINE3_init var word
Work var byte
Work = 0
Low B5
Low D4
Low D5
Low D6

Start:
LCDWRITE["INITIALIZING"]
Low B2
Low A4
High D5
Pause 1000
ADIN 0,2,ADSETUP,IR1_init
ADIN 1,2,ADSETUP,IR2_init
ADIN 2,2,ADSETUP,U_init
Low D5
High D4
Pause 1000
ADIN 0,2,ADSETUP,FLEX1_init
ADIN 1,2,ADSETUP,FLEX2_init
Low D4
High D6
Pause 1000
ADIN 0,2,ADSETUP,LINE1_init
ADIN 2,2,ADSETUP,LINE2_init
ADIN 1,2,ADSETUP,LINE3_init
Low D6
IR1_init = IR1_init + 70
IR2_init = IR2_init + 70
U_init = U_init + 25
FLEX1_init = FLEX1_init - 35
FLEX2_init = FLEX2_init - 35
LINE1_init = LINE1_init + 50
LINE2_init = LINE2_init + 50
LINE3_init = LINE3_init + 50

Demo1:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["REMOTE CONTROL"]
PAUSE 100
Low D4
123
Ten Projects With The MBasic Compiler

Low D5
Low D6
Low B2
Low A4
Button C3,1,100,10,Work,1,Demo2
Button C2,1,100,10,Work,1,Demo3
Button C1,1,100,10,Work,1,Demo4
Button C0,1,100,10,Work,1,Demo5
Button C7,1,100,10,Work,1,Forward
Button E0,1,100,10,Work,1,Left
Button E1,1,100,10,Work,1,Right
Button E2,1,100,10,Work,1,Revers
Goto Demo1

Demo2:
High D5
Low D4
Low D6
Low B2
Low A4
LCDWRITE[CLEAR,HOME]
LCDWRITE["OBJECT DETECT"]
Pause 500
Button C4,1,100,10,Work,1,Demo1
Button C3,1,100,10,Work,1,Demo2a
Button C2,1,100,10,Work,1,Demo3
Button C1,1,100,10,Work,1,Demo4
Button C0,1,100,10,Work,1,Demo5
Goto Demo1
Demo2a:
ADIN 0,2,ADSETUP,IR1_info
DEBUG["IR1_INFO "]
DEBUG[dec IR1_info,13]
DEBUG["IR1_INIT "]
DEBUG[dec IR1_init,13]

ADIN 1,2,ADSETUP,IR2_info
DEBUG["IR2_INFO "]
DEBUG[dec IR2_info,13]
DEBUG["IR2_INIT "]
DEBUG[dec IR2_init,13]

ADIN 2,2,ADSETUP,U_info
DEBUG["U_INFO "]
DEBUG[dec U_info,13]
DEBUG["U_INIT "]
DEBUG[dec U_init,13]

124
Ten Projects With The MBasic Compiler

IF IR1_info > IR1_init then DetR


IF IR2_info > IR2_init then DetL
IF U_info > U_init then DetF
Goto Demo2

DetR:
LCDWRITE[SCRRAM+$40,"RIGHT SIDE"]
Pause 500
Gosub R_Rotate
Goto Demo2

DetL:
LCDWRITE[SCRRAM+$40,"LEFT SIDE"]
Pause 500
Gosub L_Rotate
Goto Demo2

DetF:
LCDWRITE[SCRRAM+$40,"IN FRONT"]
Pause 500
Gosub Rev_RL
Goto Demo2

Demo3:
High D4
Low D5
Low D6
Low B2
Low A4
LCDWRITE[CLEAR,HOME]
LCDWRITE["EDGE AVOIDANCE"]
Pause 500
Button C4,1,100,10,Work,1,Demo1
Button C3,1,100,10,Work,1,Demo2
Button C2,1,100,10,Work,1,Demo3a
Button C1,1,100,10,Work,1,Demo4
Button C0,1,100,10,Work,1,Demo5
Goto Demo1
Demo3a:
ADIN 0,2,ADSETUP,FLEX1_info
DEBUG["FLEX1_INFO "]
DEBUG[dec FLEX1_info,13]
DEBUG["FLEX1_INIT "]
DEBUG[dec FLEX1_init,13]

ADIN 1,2,ADSETUP,FLEX2_info
DEBUG["FLEX2_INFO "]
125
Ten Projects With The MBasic Compiler

DEBUG[dec FLEX2_info,13]
DEBUG["FLEX2_INIT "]
DEBUG[dec FLEX2_init,13]

If FLEX1_info < FLEX1_init AND FLEX2_info < FLEX2_init then MovB


IF FLEX1_info < FLEX1_init then MovR
IF FLEX2_info < FLEX1_init then MovL
Gosub FWD
Goto Demo3

MovR:
LCDWRITE[SCRRAM+$40,"MOVE RIGHT"]
Pause 500
Gosub R_Rotate
Goto Demo3

MovL:
LCDWRITE[SCRRAM+$40,"MOVE LEFT"]
Pause 500
Gosub L_Rotate
Goto Demo3

MovB:
LCDWRITE[SCRRAM+$40,"MOVE BACK"]
Pause 500
Gosub Rev_RL
Gosub R_Rotate
Goto Demo3

Demo4:
High D4
Low D5
Low D6
LCDWRITE[CLEAR,HOME]
LCDWRITE["FIRE/TEMP READ"]
Pause 500
Button C4,1,100,10,Work,1,Demo1
Button C3,1,100,10,Work,1,Demo2
Button C2,1,100,10,Work,1,Demo3
Button C1,1,100,10,Work,1,Demo4a
Button C0,1,100,10,Work,1,Demo5
Goto Demo1
Demo4a:
Pulsin D7,1,FIRE_info
DEBUG["FIRE_INFO "]
DEBUG[dec FIRE_info,13]
If FIRE_info > 2000 then Fire

126
Ten Projects With The MBasic Compiler

ADIN 2,2,ADSETUP,TEMP_info
DEBUG["TEMP_INFO "]
DEBUG[dec TEMP_info,13]
IF TEMP_info > 166 then Temp5
IF TEMP_info > 163 then Temp3
IF TEMP_info > 160 then Temp1
IF TEMP_info > 95 then Temp9
Goto Demo4

Fire:
LCDWRITE[SCRRAM+$40,"FIRE!FIRE!FIRE!"]
Pause 800
Goto Demo4

Temp5:
LCDWRITE[SCRRAM+$40,"TEMP ABOVE 75"]
Pause 800
Goto Demo4

Temp3:
LCDWRITE[SCRRAM+$40,"TEMP 73"]
Pause 800
Goto Demo4

Temp1:
LCDWRITE[SCRRAM+$40,"TEMP 71"]
Pause 800
Goto Demo4

Temp9:
LCDWRITE[SCRRAM+$40,"TEMP BELOW 69"]
Pause 800
Goto Demo4

Demo5:
High D6
Low D4
Low D5
Low B2
Low A4
LCDWRITE[CLEAR,HOME]
LCDWRITE["LINE FOLLOWING"]
Pause 500
Button C4,1,100,10,Work,1,Demo1
Button C3,1,100,10,Work,1,Demo2
Button C2,1,100,10,Work,1,Demo3
Button C1,1,100,10,Work,1,Demo4
Button C0,1,100,10,Work,1,Demo5a
127
Ten Projects With The MBasic Compiler

Goto Demo1
Demo5a:
ADIN 0,2,ADSETUP,LINE1_info
DEBUG["LINE1_INFO "]
DEBUG[dec LINE1_info,13]
DEBUG["LINE1_INIT "]
DEBUG[dec LINE1_init,13]
IF LINE1_info > LINE1_init then L_R

ADIN 1,2,ADSETUP,LINE3_info
DEBUG["LINE3_INFO "]
DEBUG[dec LINE3_info,13]
DEBUG["LINE3_INIT "]
DEBUG[dec LINE3_init,13]
IF LINE3_info > LINE3_init then L_L

ADIN 2,2,ADSETUP,LINE2_info
DEBUG["LINE2_INFO "]
DEBUG[dec LINE2_info,13]
DEBUG["LINE2_INIT "]
DEBUG[dec LINE2_init,13]
IF LINE2_info > LINE2_init then L_C
LCDWRITE[SCRRAM+$40,"ROBOT ON LINE?"]
Pause 3000
Goto Demo5

L_R:
LCDWRITE[SCRRAM+$40,"TO FAR RIGHT"]
Pause 500
Low C6
High B2
Pause 100
Goto Demo5

L_L:
LCDWRITE[SCRRAM+$40,"TO FAR LEFT"]
Pause 500
Low C5
High A4
Pause 100
Goto Demo5

L_C:
LCDWRITE[SCRRAM+$40,"IN CENTER"]
Pause 500
Low C5
Low C6
High B2
128
Ten Projects With The MBasic Compiler

High A4
Pause 100
Goto Demo5

Forward:
Low C5
Low C6
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["GO FORWARD"]
High B2
High A4
Pause 500
Goto Demo1

Left:
Low C6
High B2
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["TURN LEFT"]
Pause 500
Goto Demo1

Right:
Low C5
High A4
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["TURN RIGHT"]
Pause 500
Goto Demo1

Revers:
High C5
High C6
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["GO REVERSE"]
High B2
High A4
Pause 500
Goto Demo1

R_Rotate:
Low C5
High C6
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["ROTATE RIGHT"]
High A4
High B2
129
Ten Projects With The MBasic Compiler

Pause 500
Return

L_Rotate:
High C5
Low C6
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["ROTATE LEFT"]
High A4
High B2
Pause 500
Return

Rev_RL:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["REVERSE"]
High C5
High C6
High B2
High A4
Pause 500
Return

FWD:
LCDWRITE [Clear,Home,SCR]
LCDWRITE ["FORWARD"]
Low C5
Low C6
High B2
High A4
Pause 100
Return

End

EXPLANATION OF PROGRAM

Project_10 has six parts labeled: Start, Demo1, Demo2, Demo3, Demo4 and Demo5.
Following is a short description of what each part of the program does.

Start: At the beginning of the program the sensors for Demo2, Demo3 and
Demo5 have their initial readings recorded. The program then goes to
Demo1.
130
Ten Projects With The MBasic Compiler

Demo1: Displays “REMOTE CONTROL” on the LCD and looks for an RF signal
from Project_9a. If buttons (#6-9) are pressed the program will go to the
routines to activate the motors (Forward, Left, Right, Reverse). If other
buttons (#1-5) are pressed the program will go to those specified routines.

Demo2: Displays “OBJECT DETECT” on the LCD and then takes A/D readings
of the Ultrasonic sensor and IR sensors. If those readings exceed the
initial readings of those sensors in the “Start” routine then the program
goes to the routine that will display what side the object has been detected.
That routine will also tell the motors to move in a certain direction. The
program continues to take A/D readings until a different Demo button is
pressed.

Demo3: Displays “EDGE AVOIDANCE” on the LCD and takes A/D readings of
the Flex sensors. If those readings exceed the initial readings of those
sensors in the “Start” routine then the program goes to the routine that will
display what action is necessary. The routine will tell the motors to move
in a certain direction. The program continues to take A/D readings until a
different Demo button is pressed.

Demo4: Displays “FIRE/TEMP READ” on the LCD. An A/D reading of the


Temperature sensor is taken. A reading of the fire sensor is taken by the
pulsin command on Port D pin 7. The readings determine what
information is displayed on the LCD. The program continues to take
readings until a different Demo button is pressed.

Demo5: Displays “LINE FOLLOWING” on the LCD and takes A/D readings of
the photocells in the line following circuit board. If those readings exceed
the initial readings of those photocells in the “Start” routine then the
program goes to the routine that will display what position the robot is in
compared to where the line is. The routine will tell the motors to move in
a certain direction to stay on the line. The program continues to take A/D
readings until a different Demo button is pressed.

CONCLUSION

Chapter 10 showed how to program the PIC16F877 with the 2840 Development Board.
A RF transmitter and receiver is used to have communication between Project_9a and
Project_10 (the Robot). An LCD on the Robot is used to display what buttons are
pressed. The sensors in conjunction with the Demo routines determine what directions
the motors will move the Robot.
131
Ten Projects With The MBasic Compiler

132

You might also like