132

10 Projects With Mikrobasic Pic

Embed Size (px)

Citation preview

Page 1: 10 Projects With Mikrobasic Pic
Page 2: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

2

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

Page 3: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

3

INTRODUCTION

This book is designed to give the user of the MBasic Compiler a look at some of thecapabilities of the software as well as showing the easy integration with availablehardware. It will illustrate ten individual projects using the two different DevelopmentBoards and the three different PICmicro MCUs that are included in the Basic MicroUltimate Combo. The Ultimate Combo Package from Basic Micro includes everythingyou need to begin experimenting with the PICmicro MCUs. MBasic is an easy to usecompiler. The compiler combined with the 0818 and 2840 Development Boards and ISPPRO 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 withoutunplugging or switching cables around. MBasic is an advanced programming languagemodeled after BASIC. The IDE (Integrated Development Environment) is MBasic. TheIDE 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 programmingsoftware for your BASIC code files. The IDE allows all the pieces of MBasic to be inone place. Instead of multiple programs, it is all integrated in one easy to use Windowsinterface.

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

Page 4: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

4

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

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

Page 5: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

5

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 toinstall MBasic into its default directories. You can later change the default directoriesafter you completely understand the compiler and how it works. During the installationprocess 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 youhave 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/2840Development Boards. The enclosures will make working with these boards easier andwill also help in preventing damage to the boards. I attached the boards to some plasticvideo tape boxes. It works great. When I am finished programming I can close the boxesand 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 startwith the 16F628 and the 0818 Development Board.

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

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 seeif the ISP-PRO is functioning correctly.

Page 6: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

6

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 optionsat the bottom of the menu should be unchecked except for the Watchdog timer. Thenclick OK.

4. The following configuration settings should be displayed on the Editor window:

CPU = 16F628MHZ = 10CONFIG = 16142

5. Enter the following program under the configuration settings:

Temp var byte Temp1 var wordTemp1 = 0

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

6. After the program is entered, save it. Save the file as: File name Test1 / Save astype 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 beingprogrammed. Then the bottom Build window will change focus to the Debugwindow.

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

Page 7: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

7

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

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

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

PROJECT_1

It is now time to attach some components on the solderless bread board. Thosecomponents 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 theexplanations 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 WireKit and the components you received in your package. The only thing you need to get onyour 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

B2Vss

B3 390 ohms

B5 Bi-Color LEDs

Page 8: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

8

Page 9: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

9

After you have wired up the solderless bread board, open a new file and enter thefollowing 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 byteWork = 0Pot var wordPot = 0Counter var byteCounter = 0

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

Demo1:Low A3High A1For Counter = 1 to 15

High B0Pause 1Rctime B0,1,PotPot = Pot * 2Debug [DEC Pot, 13]High B1Low B2High B3Low B5Pause PotDebug [DEC Counter, 13]Low B1High B2Low B3High B5Pause Pot

NextWork = 0Button A0,1,100,10,Work,1,Demo2Goto Demo1

Demo2:

Page 10: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

10

Low A1High A2Pause 1000Low B1Low B2Low B3Low B5Button A0,1,100,10,Work,1,Demo3Loop:High B0Pause 1Rctime B0,1,PotDebug [DEC Pot,13]Pause 300If Pot > 600 then FourIf Pot > 400 then ThreeIf Pot > 200 then TwoIf Pot > 10 then OneIf Pot < 10 then Demo2One:

High B1Low B2Low B3Low B5Debug [DEC 1,13]Goto Loop

Two:Low B1High B2Low B3Low B5Debug [DEC 2,13]Goto Loop

Three:Low B1Low B2High B3Low B5Debug [DEC 4,13]Goto Loop

Four:Low B1Low B2Low B3High B5Debug [DEC 6,13]Goto Loop

Page 11: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

11

Demo3:Low A2High A3High B0Pause 1Rctime B0,1,PotButton A0,1,100,10,Work,1,StartDebug [DEC Pot,13]If Pot > 600 then FLIf Pot > 400 then SLIf Pot > 350 then STIf Pot > 150 then SRIf Pot >= 1 then FRFL:

Debug ["Fast Left",13]Toggle B1Pause 200toggle B2Pause 200Toggle B3Pause 200Toggle B5Pause 200Goto Demo3

SL:Debug ["Slow Left",13]Toggle B1Pause 500Toggle B2Pause 500Toggle B3Pause 500Toggle B5Pause 500Goto Demo3

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

SR:Debug ["Slow Right",13]Toggle B5Pause 500Toggle B3Pause 500

Page 12: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

12

Toggle B2Pause 500Toggle B1Pause 500Goto Demo3

FR:Debug ["Fast Right",13]Toggle B5Pause 200Toggle B3Pause 200Toggle B2Pause 200Toggle B1Pause 200Goto Demo3

End

EXPLANATION OF PROGRAM

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

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

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

Demo2: The middle green LED turns on. The value of the potentiometerdetermines which bi-color LED is activated. When the potentiometer is at

Page 13: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

13

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

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

CONCLUSION

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

Page 14: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

14

Page 15: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

15

CHAPTER 2 : LCD ALPHANUMERIC DISPLAY (Project_2)

LCD DISPLAY

Liquid crystal displays (LCDs) are a passive display technology. That means they do notemit light; instead, they use the ambient light in the environment. By manipulating thatlight, they can display characters using very little power. The LCD consists primarily oftwo 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 crystalmolecules are rod shaped and have a definite order or pattern, and an electric field can beused to manipulate those molecules. Many LCDs use a type of liquid crystal calledtwisted nematic (TN). It is naturally twisted. Applying an electric current to these liquidcrystals will untwist them to varying degrees, depending on the current’s voltage. LCDsuse these liquid crystals because they react predictably to electric current in such a way asto control light passage.

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

When the LCD is “off”, no voltage is applied to the electrodes, and light passes throughthe LCD. When it is “on”, voltage is applied and the liquid crystal molecules alignthemselves in the direction of the electric field. This causes the light to be out of phasewith the polarizers and to be blocked, creating a dark area on the LCD. By selectivelyapplying voltage to the electrodes, a variety of patterns can be achieved.

Having an alphanumeric display for your PIC microcontroller can be very helpful. AnLCD module can display output messages from the PIC, such as numeric values of avariable or word messages to the user. The LCD module in your kit uses a Hitachi 44780type controller and has two lines with sixteen characters per line and is organized inblocks of 5 X 7 dots. It accepts data and command instructions over four or eight bitparallel interface. For our uses we will use the four bit parallel interface. To operate thisdisplay 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. Thecontrol commands that are used with LCDWRITE are in Table 2.1. Before you use theLCDWRITE command, you must first initialize the LCD screen by adding the followinglines to your program:

Page 16: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

16

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

The internal cursor position is set automatically. If you want to print a character at aparticular location on the screen, position the internal cursor at the specific screen ramlocation you want. The screen ram is mapped out as shown in Table 2.2. To displayyour 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 screenram. Shifting the display left by using the SCRLEFT command can access this ram.

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

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

Page 17: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

17

LCD SET UP

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

LCDREGSEL CON B0 ;RS LINELCDCLK CON B1 ;E LINELCDREADWRITE CON B2 ;R/W LINELCDPORT CON PORTA ;DB 4-7 out of 0-7

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

ENCLOSURE CONNECTOR

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

0818 Development Bd. 25-pin DB connector (male)A0 1A1 2A2 3A3 4A4 5B0 6B1 7B2 8B3 9B4 10B5 11B6 12B7 13Vss 14Vdd 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 ofthese commands).

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

Page 18: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

18

Follow the below schematic in wiring Project_2. Use the components you received inyour kit. The only things you need to get on your own are the resistors, a .1uf capacitorand 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

Vdd Vss20k ohms

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

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

RB5 PIN 11Vss PIN 14

LCD MODULE

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

Page 19: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

19

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 isentered, save the file as: Project_2.

Page 20: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

20

CPU = 16F628MHZ = 10CONFIG 16142

Pot var wordSample var byteLoop var bytePause 500LCDWRITE [INITLCD1,INITLCD2,CLEAR,HOME,SCR]

Start:Sample = 0Loop = 0RepeatSample = Sample + 1High B3Pause 10Rctime B3,1,PotUntil Sample = 5Pot = Pot * 5Debug [Dec Pot,13]If Pot > 1200 Then Demo1If Pot > 600 Then Demo2If Pot > 100 Then Demo3If Pot > 2 Then Demo4If Pot < 2 Then Start

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

Demo2:LCDWRITE [SCRRAM+16h,"SCAN LEFT"]RepeatLoop = Loop + 1LCDWRITE [SCRLEFT]Pause 300Until Loop = 16Pause 500LCDWRITE [CLEAR,HOME]Goto Start

Page 21: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

21

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

Demo4:AH con 440*2AS con 466*2BH con 494*2CH con 523*2CS con 554*2DH con 587*2DS con 622*2EH con 659*2FH con 678*2FS con 740*2GH con 784*2GS con 831*2Temp var byteTemp2 var wordFor 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],Temp2If temp2=0 then FinishFreqout B5,500,Temp2Finish:NextPause 1000Sound 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 1000Goto Start

End

Page 22: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

22

EXPLANATION OF PROGRAM

Project_2 has four parts labeled: Start, Demo1, Demo2, Demo3 and Demo4. Followingis 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 theprogram 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 firstline of the LCD and then displays the message “SECOND LINE” on thesecond line of the LCD.

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

Demo3: This part of the program displays the message “SCAN RIGHT” on the farleft side of the LCD display and then shifts the message right until it goesto 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 theMBasic compiler. Project_2 used the 0818 Development Board to run a LCD in variousways and send tones to a speaker. In the next chapter we will be experimenting withthree different types of motors.

Page 23: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

23

CHAPTER 3 : MOTOR CONTROL (Project_3)

TYPES OF MOTORS

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

DC MOTOR

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

The PIC’s output current is insufficient to power a DC motor directly. But the output ofthe PIC can turn on or off a transistor that can control a device that will be able to powera motor. The device used in this project to control the DC motor is an H-Bridge. Withthe H-Bridge the PIC can stop the DC motor, rotate it clockwise, or rotate it counterclockwise. It’s called an H-Bridge because the transistors (switches) are arranged in an Hpattern. The switches are opened and closed in a manner so as to put a voltage of onepolarity across the motor for one direction or a voltage of opposite polarity for reversedirection. To control the speed of the motor, the switches are opened and closed atdifferent rates in order to apply different average voltages across the motor. This iscalled Pulse-Width Modulation (PWM). Again, PWM is simply using varying pulsewidths 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 theshaft cannot turn, a potentiometer for position feedback and an integrated circuit forposition control. Servos have three wires, one for power, one for ground, and one for theposition 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. Ifthe shaft is at the correct angle (the commanded position), then the motor shuts off. If thecircuit 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

Page 24: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

24

gear), the motor will be in a “stalled” state and might eventually damage the motor. Thelimit stops can be removed to provide continuous rotation; this is called “hacking” theservo. 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 advanceto. The angle is determined by the duration of the pulse sent. The servo expects to see apulse 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 ofpulse times for degree positions; actual timings may vary due to different motormanufacturers.

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 rotorconsists of the permanent magnet and the windings consist of the electromagnets. Insteadof rotating smoothly, like DC motors, they move incrementally. Each increment is afixed angular displacement of the motor’s shaft, typically on the order of one to tendegrees, depending on the manufacturer. They operate from a pattern of electric pulsessent to the windings of the motor. The rotor rotates a specific increment with eachelectronic pulse. The increments are referred to as “steps”.

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

Figure 3.1

Common

A C

B D

Page 25: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

25

The common lead is the power connection for the motor coils. Typically, this isconnected to 12 volts. The other four leads are the signal connections. The normalelectrical inputs for the signal wires are a four step switching sequence as shown in Table3.1.

Table 3.1

The speed of rotation will be determined by the time delay between each step. Followingthe 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 aULN2003A and a 74HCT245N. Also three button switches, four resistors, four LEDs, a1N3024 zener diode and a capacitor will be used. The MBasic commands used in thisproject, which we have not used before, are: While…Do, Servo, and PWM (refer to theMBasic 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 inyour kit, the other parts you can get from electronic stores. One good source for the partsyou need is Jameco Electronics (www.jameco.com).

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

Page 26: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

26

PIC CONNECTOR

390 ohmsB0 PIN 6

“A” “B”

Vss PIN 14 4.7k ohms

.1mf

Vdd PIN 25

“START”

B1 PIN 7 Vdd

Vss 10k ohms

Vdd1 20

B2 PIN 8 2 19 Vss3 184 17

B3 PIN 9 5 16

15

10Vss

B5 PIN 11VddVss

74HCT 245N

SERVO

DC Motor

Page 27: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

27

Vdd LEDs 330 ohms

A0 PIN 1 1 16

A1 PIN 2 2 15

A2 PIN 3 3 14

A3 PIN 4 4 13

Vss 8 9 1N3024

+9 volts Battery

ULN 2003A STEPPER

MOTOR

Page 28: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

28

Page 29: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

29

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 isentered, save the file as: Project_3.

CPU = 16F628MHZ = 10CONFIG 16138

High A0High A1High A2High A3Low B5Low B2Low B3

START:Debug ["START",13]work var bytework = 0pot var wordpot = 0counter var bytecounter = 0Pause 1500Button B1,1,100,10,work,1,DEMO1Goto START

DEMO1:Debug ["DEMO1",13]Pause 1500Button B1,1,100,10,work,1,DEMO2Temp1 var byteTemp1 = 10High B0Pause 10Rctime B0,1,potDebug [DEC pot,13]If pot > 350 then DEMO1If pot > 300 then TiltupIf pot > 150 then TiltdnGoto DEMO1

Tiltup:While temp1 <> 250

temp1 = temp1 + 10

Page 30: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

30

servo B5,temp1DoGoto DEMO1

Tiltdn:Temp1 = 250While temp1 <> 10

temp1 = temp1 - 10Servo B5,temp1

DoGoto DEMO1

DEMO2:Debug ["DEMO2",13]Pause 1500Button B1,1,100,10,work,1,DEMO3Temp1 = 1High B0Pause 10Rctime B0,1,potDebug [DEC pot,13]If pot > 350 then DEMO2If pot > 300 then PanrhtIf pot > 150 then PanlftGoto DEMO2

Panrht:While temp1 <> 3

temp1 = temp1 + 1 PWM B2,240,1000DoLow B2Goto DEMO2

Panlft:While temp1 <> 3

temp1 = temp1 + 1PWM B3,240,1000

DoLow B3Goto DEMO2

DEMO3:Debug ["DEMO3",13]Pause 1500Button B1,1,100,10,work,1,STARTHigh B0Pause 10

Page 31: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

31

Rctime B0,1,potDebug [DEC pot,13]If pot > 350 then DEMO3If pot > 300 then CWIf pot > 150 then CCWGoto DEMO3

CW:counter = 15Repeat

counter = counter - 1low A0Pause 100high A0low A1pause 100high A1low A2pause 100high A2low A3pause 100high A3

Until counter = 1Goto DEMO3

CCW:counter = 15Repeat

counter = counter - 1low A3Pause 100high A3low A2pause 100high A2low A1pause 100high A1low A0pause 100high A0

Until counter = 1Goto DEMO3

End

Page 32: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

32

EXPLANATION OF PROGRAM

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

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

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

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

Demo3: This part of the program operates the stepper motor. If the “A” button ispressed, the motor’s rotor turns (steps) in the CW direction for a certaindistance. If “B” is pressed, the motor’s rotor turns in the CCW directionfor a certain distance. The program loops until the “START” button ispressed 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 theMBasic compiler. Project_3 used the 0818 Development Board to run three differenttypes of motors. In the next chapter we will be programming the PIC16F876 with the2840 Development Board to control two seven segment LED Displays.

Page 33: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

33

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 the2840 Development Board in an enclosure. I have connected wires from the terminalstrips of the Development Board to a 25 pin DB connector and connected it to the side ofthe 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 yourcomputer.

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 seeif 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 commoncathode (CC). When a display is referred to as a common anode type, it means that allthe anodes for the individual segment LEDs are connected together internally. Thedriving signals are sent to the individual cathode segments as shown in Figure 4.1. Withcommon cathode types, all the cathodes for the individual segment LEDs are connectedtogether internally and the driving signals are sent to the individual anode segments. Thesegment LEDs are labeled a-g and can display the digits 0 through 9 and the lettersA,b,C,d,E and F. Table 4.1 shows the segments that need to be illuminated to show aparticular digit or letter.

Page 34: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

34

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

a

f b

g

e c

dTable 4.1

Page 35: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

35

The 7-segment display being used in this project is a common anode type. PORT C ofthe 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 appropriatesignals to the common anode 7-segment LED displays. For the display to work, thecommon anode needs to be connected to +5 volts, and the 7447 chips need to send out alow signal to the specified (cathode) segment LEDs.

DIFFERENT PROGRAMMING STYLES

In the following program for Project_4, there will be different styles of programming tosend data to the displays. Different commands will be used, with some being moreefficient than others. Everyone has different styles in writing their programs, so feel freeto 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 twocommon anode 7-segment LED displays. The MBasic commands used in this projectthat have not been used in previous projects are: Poke and Gosub…Return (refer to theMBasic 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

Page 36: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

36

C4 C5 C6 C7 C0 C1 C2 C3

7 1 2 6 7 1 2 6

8 Vss 8 Vss

16 Vdd 16 Vdd

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

A B C D

7447a b c d e f g

A B C D

7447a b c d e f g

Page 37: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

37

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 thePICmicro MCU as 16F876. Once the program is entered, save the file as: Project_4.

CPU = 16F876MHZ = 10CONFIG 16254

START:Debug ["START",13]TRISC = $00PORTC = $00Work var byteVAL1 var byteVAL2 var byteSeg_Val var wordDig_One var wordDig_Two var wordSend_Val var byteSend_ValB var byteFinal var byteNum var byteWork = 0VAL1 = 0VAL2 = 0Button B0,1,100,10,Work,1,DEMO1Goto START

Page 38: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

38

DEMO1:Debug ["DEMO1",13]Pause 1000Button B1,1,100,10,Work,1,VALUE2Button B2,1,100,10,Work,1,VALUE1Button B0,1,100,10,Work,1,DEMO2Goto DEMO1

VALUE1:VAL1 = VAL1 + 1Debug [DEC VAL1,13]If VAL1 = 1 Then ONE1If VAL1 = 2 Then ONE2If VAL1 = 3 Then ONE3If VAL1 = 4 Then ONE4If VAL1 = 5 Then ONE5If VAL1 = 6 Then ONE6If VAL1 = 7 Then ONE7If VAL1 = 8 Then ONE8If VAL1 = 9 Then ONE9VAL1 = 0Goto ONE0

VALUE2:VAL2 = VAL2 + 1Debug [DEC VAL2,13]If VAL2 = 1 Then TWO1If VAL2 = 2 Then TWO2If VAL2 = 3 Then TWO3If VAL2 = 4 Then TWO4If VAL2 = 5 Then TWO5If VAL2 = 6 Then TWO6If VAL2 = 7 Then TWO7If VAL2 = 8 Then TWO8If VAL2 = 9 Then TWO9VAL2 = 0Goto TWO0

DEMO2:Debug ["DEMO2",13]Pause 1000Button B1,1,100,10,Work,1,COUNTDNButton B2,1,100,10,Work,1,COUNTUPButton B0,1,100,10,Work,1,DEMO3Goto DEMO2

COUNTUP:Pause 500

Page 39: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

39

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

COUNTDN:Pause 500Poke 7,21Pause 500Poke 7,20Pause 500Poke 7,19Pause 500Poke 7,18Pause 500Poke 7,17Pause 500Poke 7,16Pause 500

Page 40: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

40

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

DEMO3:Debug ["DEMO3",13]Pause 1000Button B0,1,100,10,Work,1,STARTFor Num = 99 to 1 step -1Seg_Val = NumGosub SegDisppause (150)NextGoto DEMO3

SegDisp:Dig_One = Seg_Val / 10Send_Val = Dig_OneSend_Val = Send_ValDig_Two = Seg_Val // 10Send_ValB = Dig_TwoSend_ValB = Send_Val << 4Final = Send_Val | Send_ValBPORTC = FinalReturn

ONE0:LOW C0LOW C1LOW C2

Page 41: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

41

LOW C3GOTO DEMO1

ONE1:HIGH C0LOW C1LOW C2LOW C3GOTO DEMO1

ONE2:LOW C0HIGH C1LOW C2LOW C3GOTO DEMO1

ONE3:HIGH C0HIGH C1LOW C2LOW C3GOTO DEMO1

ONE4:LOW C0LOW C1HIGH C2LOW C3GOTO DEMO1

ONE5:HIGH C0LOW C1HIGH C2LOW C3GOTO DEMO1

ONE6:LOW C0HIGH C1HIGH C2LOW C3GOTO DEMO1

ONE7:HIGH C0

Page 42: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

42

HIGH C1HIGH C2LOW C3GOTO DEMO1

ONE8:LOW C0LOW C1LOW C2HIGH C3GOTO DEMO1

ONE9:HIGH C0LOW C1LOW C2HIGH C3GOTO DEMO1

TWO0:LOW C4LOW C5LOW C6LOW C7GOTO DEMO1

TWO1:HIGH C4LOW C5LOW C6LOW C7GOTO DEMO1

TWO2:LOW C4HIGH C5LOW C6LOW C7GOTO DEMO1

TWO3:HIGH C4HIGH C5LOW C6LOW C7GOTO DEMO1

Page 43: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

43

TWO4:LOW C4LOW C5HIGH C6LOW C7GOTO DEMO1

TWO5:HIGH C4LOW C5HIGH C6LOW C7GOTO DEMO1

TWO6:LOW C4HIGH C5HIGH C6LOW C7GOTO DEMO1

TWO7:HIGH C4HIGH C5HIGH C6LOW C7GOTO DEMO1

TWO8:LOW C4LOW C5LOW C6HIGH C7GOTO DEMO1

TWO9:HIGH C4LOW C5LOW C6HIGH C7GOTO DEMO1

END

Page 44: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

44

EXPLANATION OF PROGRAM

Project_4 has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is ashort 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 madelow. The program loops forever until the button switch labeled “Start” ispressed and then the program moves to Demo1.

Demo1: Pressing the button switches under each individual display will incrementthe display by one. The digits zero through nine can be displayed on eachindividual 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 upfrom zero to fifteen or count down from fifteen to zero. The program willcontinue to loop until the “Start” button is pressed and then will go toDemo3.

Demo3: This routine will show the number nine on each display, and decrementthat number until each display reads zero. The program then repeats theprocess until the “Start” button is pressed and then the program returns tothe 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 displayin several different ways. In the next chapter we will be adding photocells and two flexsensors to Project_4.

Page 45: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

45

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 inone direction and returns to its nominal value at rest. The sensor at rest provides a lowresistance; 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 fullbend (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 photocellreceives. The more light it receives, the less resistance the sensor provides. In a darkenvironment 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 forthe four different settings that it has. With each resistance value and the use of theRCTIME 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 DigitalConverters). Analog to digital conversion allows your program to receive an analogsignal (0 to 5 volts) and convert it into digital values. The analog input charges a sampleand hold capacitor. The output of the sample and hold capacitor is the input into theconverter. The converter then generates a digital result of this analog level via successiveapproximation. The A/D conversion of the analog input signal results in a corresponding10-bit digital number. The ADIN command sets up the hardware A/D converter, takesthe 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 tobe digital, the ADCON1 register needs to be changed. The last four bits (PCFG) of theADCON1 register determine which pins are analog or digital. Table 5.1 shows thedifferent register settings.

Page 46: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

46

Table 5.1

Page 47: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

47

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

ADCON1 register (ADFM) (PCFG)

ADSETUP con %10000010

AN0, AN1, AN2, AN3, AN4 are analogAN5 is digital

Right justify

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

PROJECT_5

The components used in this project are: one button switch, one rotary switch, sixphotocells, two flex sensors and twelve various resistors in the sensor circuits (refer toschematic). Also parts from Project_4 that include: three 10k ohm resistors, one 100 ohmresistor, two 7447 (BCD-to-7 segment decoder/driver) IC chips and two common anode7-segment LED displays. The MBasic command used in this project that has not beenused 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)

Page 48: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

48

Right Photocells

39k ohmsVss Vdd

RA0 Left Photocells

39k ohmsVss Vdd

RA1

Right Flex sensor

39k ohmsVss Vdd

RA2

Left Flex sensor

39k ohmsVss Vdd

RA3

1 10k ohms

2 22k ohms

3 33k ohms Vdd .1 uf

4 39k ohms

RB5 Rotaryswitch

1 33k ohms

2 22k ohms

3 10k ohms Vss

4 4.7k ohms

Page 49: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

49

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 = 16F876MHZ = 10CONFIG 16254

Page 50: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

50

TRISA = $FFTRISC = $00PORTC = $FFwork var bytetemp1 var wordtemp2 var wordtemp3 var wordtemp4 var wordtemp5 var word

ADSETUP con %10000010

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

Demo1:Debug ["Demo1",13]Poke 7,255ADIN 0,2,ADSETUP,temp1ADIN 1,2,ADSETUP,temp2ADIN 2,2,ADSETUP,temp3ADIN 3,2,ADSETUP,temp4High B5Pause 10RCTIME B5,1,temp5Debug ["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 Demo2Goto Demo3

Demo2:Debug ["Demo2",13]Poke 7,255If temp5 > 700 then R_FlexIf temp5 > 400 then L_FlexGoto Demo1R_Flex:Debug ["R_Flex",13]

Page 51: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

51

ADIN 0,2,ADSETUP,temp1High B5Pause 10RCTIME B5,1,temp5If temp5 < 700 then Demo1If temp1 > 250 then zero1If temp1 > 220 then one1If temp1 > 190 then two1If temp1 > 160 then three1If temp1 > 130 then four1If temp1 > 100 then five1If temp1 > 90 then six1If temp1 > 80 then seven1If temp1 > 70 then eight1If temp1 > 60 then nine1Goto R_FlexL_Flex:Debug ["L_Flex",13]ADIN 1,2,ADSETUP,temp2High B5Pause 10RCTIME B5,1,temp5If temp5 > 700 then Demo1If temp5 < 400 then Demo1If temp2 > 330 then zero2If temp2 > 300 then one2If temp2 > 250 then two2If temp2 > 210 then three2If temp2 > 180 then four2If temp2 > 150 then five2If temp2 > 130 then six2If temp2 > 120 then seven2If temp2 > 110 then eight2If temp2 > 100 then nine2Goto L_Flex

Demo3:Poke 7,255If temp5 > 100 then R_PhotoIf temp5 < 5 then L_PhotoGoto Demo1R_Photo:Debug ["R_Photo",13]ADIN 2,2,ADSETUP,temp3High B5Pause 10RCTIME B5,1,temp5If temp5 > 400 then Demo1

Page 52: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

52

If temp5 < 100 then Demo1If temp3 > 935 then zero1pIf temp3 > 900 then one1pIf temp3 > 840 then two1pIf temp3 > 790 then three1pIf temp3 > 740 then four1pIf temp3 > 700 then five1pIf temp3 > 650 then six1pIf temp3 > 600 then seven1pIf temp3 > 550 then eight1pIf temp3 > 500 then nine1pGoto R_PhotoL_Photo:Debug ["L_Photo",13]ADIN 3,2,ADSETUP,temp4High B5Pause 10RCTIME B5,1,temp5If temp5 > 5 then Demo1If temp4 > 925 then zero2pIf temp4 > 890 then one2pIf temp4 > 840 then two2pIf temp4 > 790 then three2pIf temp4 > 740 then four2pIf temp4 > 700 then five2pIf temp4 > 650 then six2pIf temp4 > 600 then seven2pIf temp4 > 550 then eight2pIf temp4 > 500 then nine2pGoto L_Photo

zero1:Poke 7,240goto R_Flex

one1:Poke 7,241goto R_Flex

two1:Poke 7,242goto R_Flex

three1:Poke 7,243goto R_Flex

four1:

Page 53: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

53

Poke 7,244goto R_Flex

five1:Poke 7,245Goto R_Flex

six1:Poke 7,246Goto R_Flex

seven1:Poke 7,247Goto R_Flex

eight1:Poke 7,248Goto R_Flex

nine1:Poke 7,249Goto R_Flex

zero2:Poke 7,15Goto L_Flex

one2:Poke 7,31Goto L_Flex

two2:Poke 7,47Goto L_Flex

three2:Poke 7,63Goto L_Flex

four2:Poke 7,79Goto L_Flex

five2:Poke 7,95Goto L_Flex

Page 54: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

54

six2:Poke 7,111Goto L_Flex

seven2:Poke 7,127Goto L_Flex

eight2:Poke 7,143Goto L_Flex

nine2:Poke 7,159Goto L_Flex

zero1p:Poke 7,240Goto R_Photo

one1p:Poke 7,241Goto R_Photo

two1p:Poke 7,242Goto R_Photo

three1p:Poke 7,243Goto R_Photo

four1p:Poke 7,244Goto R_Photo

five1p:Poke 7,245Goto R_Photo

six1p:Poke 7,246Goto R_Photo

seven1p:Poke 7,247Goto R_Photo

Page 55: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

55

eight1p:Poke 7,248Goto R_Photo

nine1p:Poke 7,249Goto R_Photo

zero2p:Poke 7,15Goto L_Photo

one2p:Poke 7,31Goto L_Photo

two2p:Poke 7,47Goto L_Photo

three2p:Poke 7,63Goto L_Photo

four2p:Poke 7,79Goto L_Photo

five2p:Poke 7,95Goto L_Photo

six2p:Poke 7,111Goto L_Photo

seven2p:Poke 7,127Goto L_Photo

eight2p:Poke 7,143Goto L_Photo

nine2p:Poke 7,159

Page 56: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

56

Goto L_Photo

End

EXPLANATION OF PROGRAM

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

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

Demo1: The analog to digital conversions of the sensors are stored in theirdesignated variables. The rotary switch’s four settings determine whichsensor variables are looked at and what values are shown on the 7-segmentLED display. If the switch setting is on the first or second position, thenthe program goes to the Demo2 routine. If the switch setting is on thethird 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 bythe right flex sensor are displayed on the right 7-segment LED display. Ifthe switch setting is on the second position, the values represented by theleft flex sensor are displayed on the left 7-segment LED display. If theswitch settings change, the program goes back to Demo1.

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

Page 57: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

57

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 displaysrepresenting changes in value of resistive sensors. In the next chapter we will be addinga couple of distance sensors and a buzzer to Project_4 & 5.

Page 58: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

58

Page 59: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

59

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 chapterInfrared and Ultrasonic will be covered. Infrared uses light that is invisible to the humaneye. Also Infrared light bounces off almost everything. Its main disadvantage is thatfluorescent lights generate it and that can cause interference. Ultrasonic uses sound thatis inaudible to the human ear. Its main advantage is that it is not sensitive to objects ofdifferent colors and light reflecting properties. Its disadvantage is that some materialsabsorb sound and don’t reflect it.

IR DISTANCE SENSOR

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

The basic process works like this: Infrared light is emitted; as the light travels out, iteither hits an object or keeps on going. If the light hits an object, the light reflects back tothe detector and creates a triangle between the point of reflection, the emitter anddetector. As the distance of the object varies, so do the angles in the triangle; and fromthose angles, the distance to the object can be calculated. The Sharp GP2D12 is capableof 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 isemitted by a transmitter. If the sound wave reflects off an object, it returns as an echo tothe receiver. The echo will most likely be of smaller amplitude, but the frequency shouldbe the same as the transmitted sound wave. With the ultrasonic circuit used in thischapter, 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 thecircuit produces an analog voltage that varies with the signal strength of the echo. TheUltrasonic circuit used in this chapter is capable of measuring distance in the range of 3inches 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.

Page 60: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

60

PROJECT_6

The components used in this project are one Sharp GP2D12 Infrared Ranger, oneUltrasonic 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 andpart of Project_5)

Sharp GP2D12 Infrared Object Detector

RA0 Analog output

Vdd

Vss

Ultrasonic Circuit

RA3 Analog output

Vdd

Vss

Buzzer

RB3 +

Vss -

Page 61: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

61

ULTRASONIC CIRCUIT (DETAILED)

Vdd Vdd Vdd10k pot

1k 220 ohm

Transmitter

Vdd 1k ohm

.02uf

Vss Vss Vss Vss

Receiver Vdd

Analog outputRA3 22k ohm 1k ohm

1 Meg ohm

.1uf Vss

Vss Vss Vss Vss 1k ohm

8 7 6 5 NE555P1 2 3 4

8 7 6 5 LM741CN1 2 3 4

Page 62: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

62

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 = 16F876MHZ = 10

Page 63: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

63

CONFIG 16254

PORTA = $00TRISC = $00PORTC = $FFwork var byteIR_info var wordU_info var wordRoto_sw var wordwork = 0

ADSETUP con %10000010

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

Demo1:Debug ["Demo1",13]Poke 7,255ADIN 0,2,ADSETUP,IR_infoDebug ["IR_info "]Debug [dec IR_info,13]ADIN 3,2,ADSETUP,U_infoDebug ["U_info "]Debug [dec U_info,13]High B5Pause 10RCTIME B5,1,Roto_swDebug ["Roto_sw "]Debug [dec Roto_sw,13]Button B0,1,100,10,work,1,ContinGoto Demo1Contin:If Roto_sw > 400 then Demo2Goto Demo3

Demo2:Debug ["Demo2",13]Poke 7,255If Roto_sw > 700 then IR_DisplayIf Roto_sw > 400 then IR_SoundGoto Demo1IR_Display:Debug ["IR_Display",13]ADIN 0,2,ADSETUP,IR_infoHigh B5

Page 64: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

64

Pause 10RCTIME B5,1,Roto_swIf Roto_sw < 700 then Demo1If IR_info > 650 then zero1If IR_info > 550 then one1If IR_info > 500 then two1If IR_info > 450 then three1If IR_info > 400 then four1If IR_info > 350 then five1If IR_info > 275 then six1If IR_info > 200 then seven1If IR_info > 130 then eight1If IR_info > 100 then nine1If IR_info > 0 then off1Goto IR_DisplayIR_Sound:Low B4Debug ["IR_Sound",13]ADIN 0,2,ADSETUP,IR_infoHigh B5Pause 10RCTIME B5,1,Roto_swIf Roto_sw > 700 then Demo1If Roto_sw < 400 then Demo1If IR_info > 650 then zero2If IR_info > 550 then one2If IR_info > 500 then two2If IR_info > 450 then three2If IR_info > 400 then four2If IR_info > 350 then five2If IR_info > 275 then six2If IR_info > 200 then seven2If IR_info > 130 then eight2If IR_info > 100 then nine2If IR_info > 0 then off2Goto IR_Sound

Demo3:Debug ["Demo3",13]Poke 7,255If Roto_sw > 100 then U_DisplayIf Roto_sw < 5 then U_SoundGoto Demo1U_Display:Debug ["U_Display",13]ADIN 3,2,ADSETUP,U_infoHigh B5Pause 10

Page 65: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

65

RCTIME B5,1,Roto_swIf Roto_sw > 400 then Demo1If Roto_sw < 100 then Demo1If U_info > 600 then zero1pIf U_info > 500 then one1pIf U_info > 475 then two1pIf U_info > 450 then three1pIf U_info > 425 then four1pIf U_info > 400 then five1pIf U_info > 375 then six1pIf U_info > 350 then seven1pIf U_info > 335 then eight1pIf U_info > 310 then nine1pIf U_info > 0 then off1pGoto U_DisplayU_Sound:Debug ["U_Sound",13]ADIN 3,2,ADSETUP,U_infoHigh B5Pause 10RCTIME B5,1,Roto_swIf Roto_sw > 5 then Demo1If U_info > 600 then zero2pIf U_info > 500 then one2pIf U_info > 475 then two2pIf U_info > 450 then three2pIf U_info > 425 then four2pIf U_info > 400 then five2pIf U_info > 375 then six2pIf U_info > 350 then seven2pIf U_info > 335 then eight2pIf U_info > 310 then nine2pIf U_info > 0 then off2pGoto U_Sound

off1:Poke 7,255Goto IR_Display

zero1:Poke 7,240Goto IR_Display

one1:Poke 7,241Goto IR_Display

two1:

Page 66: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

66

Poke 7,242Goto IR_Display

three1:Poke 7,243Goto IR_Display

four1:Poke 7,244Goto IR_Display

five1:Poke 7,245Goto IR_Display

six1:Poke 7,246Goto IR_Display

seven1:Poke 7,247Goto IR_Display

eight1:Poke 7,248Goto IR_Display

nine1:Poke 7,249Goto 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:

Page 67: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

67

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,255Goto U_Display

zero1p:Poke 7,15Goto U_Display

one1p:Poke 7,31Goto U_Display

two1p:Poke 7,47Goto U_Display

three1p:Poke 7,63Goto U_Display

Page 68: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

68

four1p:Poke 7,79Goto U_Display

five1p:Poke 7,95Goto U_Display

six1p:Poke 7,111Goto U_Display

seven1p:Poke 7,127Goto U_Display

eight1p:Poke 7,143Goto U_Display

nine1p:Poke 7,159Goto 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]

Page 69: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

69

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 ashort description of what each part of the program does.

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

Demo1: The values of each variable are displayed in the debug window. When theStart button is pressed, the program looks at the rotary switch. The rotaryswitch’s four settings determine which sensor variables are looked at andwhat numbers are shown on the 7-segment LED display. If the switchsetting is on the first or second position, then the program goes to theDemo2 routine. If the switch setting is on the third or fourth position, thenthe program goes to the Demo3 routine.

Page 70: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

70

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

Demo3: The analog to digital conversion of the Ultrasonic circuit is stored in itsdesignated variable. If the switch setting is on the third position, then thevalues represented by the Ultrasonic Range Finder are translated byif…then statements and displayed on the left 7-segment LED displays. Ifthe switch setting is on the fourth position, the values represented by theUltrasonic circuit are translated by if…then statements to frequencies thatare sent to the buzzer. If the rotary switch settings change, the programgoes 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 displaysrepresenting changes in value of the IR and Ultrasonic sensors. In the next chapter wewill be adding to Project_4 & 5 a temperature sensor and a fire detector.

Page 71: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

71

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 sensorwill determine what type you need. For our purposes in this chapter, two inexpensivesensors will be looked at.

PRECISION TEMPERATURE SENSOR

The LM34CAZ is the temp sensor that will be discussed in this section. The LM34 is aprecision integrated-circuit temperature sensor, whose output voltage is linearlyproportional to the Fahrenheit temperature. This device has a temperature range of –40to +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.75volts indicates a temperature of 75 degrees F. Since the LM34 produces an analogoutput, 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 inthis 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 spectralrange of 185 to 260 nm. Basically the circuitry operates like this: when the UVTron bulbis exposed to ultraviolet rays (such as from a flame), photoelectrons are emitted from thecathode and accelerate toward the anode. As the electric field grows stronger, themolecules of the enclosed gas in the tube eventually ionize. Soon the process causes alarge current between the electrodes, and a discharge takes place. Each time a dischargeoccurs, the circuit emits a pulse.

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

RELAY CIRCUIT

A relay circuit will be used in Project_7 to activate an alarm. When the temp sensor orflame detector reads above a designated limit, the PIC will activate a relay circuit. Thepower 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.

Page 72: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

72

PROJECT_7

The components used in this project are: one LM34 temp sensor, one Hamamatsu UVTRON Flame Detector, one relay circuit, the rotary switch circuit from Project_5 and theparts 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 andpart of Project_5).

Precision Temperature Sensor

Vdd VssRA0

Hamamatsu UV TRON Flame Detector

Vdd Vss

RA3

Vdd

10k ohmRB4 2N4124

Vss

LM34

Relay

QUORUM

PAAL

ALARM

Page 73: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

73

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 = 16F876MHZ = 10CONFIG 16254

Page 74: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

74

PORTA = $00TRISC = $00PORTC = $FFLow A4work var byteTemp_info var wordFire_info var wordRoto_sw var word

work = 0ADSETUP con %10001110

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

Demo1:Debug ["Demo1",13]Poke 7,255ADIN 0,2,ADSETUP,Temp_infoDebug ["Temp_info "]Debug [dec Temp_info,13]Pulsin A3,1,Fire_infoDebug ["Fire_info "]Debug [dec Fire_info,13]High B5Pause 10RCTIME B5,1,Roto_swDebug ["Roto_sw "]Debug [dec Roto_sw,13]Button B0,1,100,10,work,1,ContinGoto Demo1Contin:If Roto_sw > 400 then Demo2Goto Demo3

Demo2:Debug ["Demo2",13]Poke 7,255If Roto_sw > 700 then Temp_DisplayIf Roto_sw > 400 then Temp_RelayGoto Demo1Temp_Display:Debug ["Temp_Display",13]ADIN 0,2,ADSETUP,Temp_infoHigh B5

Page 75: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

75

Pause 10RCTIME B5,1,Roto_swIf Roto_sw < 700 then Demo1If Temp_info > 166 then eight6If Temp_info > 164 then eight4If Temp_info > 162 then eight2If Temp_info > 160 then eight0If Temp_info > 158 then seven8If Temp_info > 156 then seven6If Temp_info > 154 then seven4If Temp_info > 152 then seven2If Temp_info > 150 then seven0If Temp_info > 148 then six8If Temp_info > 146 then six6If Temp_info > 144 then six4If Temp_info >= 0 then off1Goto Temp_DisplayTemp_Relay:Low B4Debug ["Temp_Relay",13]ADIN 0,2,ADSETUP,Temp_infoHigh B5Pause 10RCTIME B5,1,Roto_swIf Roto_sw > 700 then Demo1If Roto_sw < 400 then Demo1If Temp_info > 168 then RelayTIf Temp_info > 166 then eight6RIf Temp_info > 164 then eight4RIf Temp_info > 162 then eight2RIf Temp_info > 160 then eight0RIf Temp_info >= 0 then off2Goto Temp_Relay

Demo3:Debug ["Demo3",13]Poke 7,255If Roto_sw > 100 then Fire_DisplayIf Roto_sw < 5 then Fire_RelayGoto Demo1Fire_Display:Debug ["Fire_Display",13]Pulsin A3,1,Fire_infoHigh B5Pause 10RCTIME B5,1,Roto_swIf Roto_sw > 400 then Demo1If Roto_sw < 100 then Demo1

Page 76: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

76

If Fire_info > 2000 then FlashIf Fire_info >= 0 then off3Goto Fire_DisplayFire_Relay:Debug ["Fire_Relay",13]Pulsin A3,1,Fire_infoHigh B5Pause 10RCTIME B5,1,Roto_swIf Roto_sw > 5 then Demo1If Fire_info > 2000 then RelayFIf Fire_info >= 0 then off4Goto Fire_Relay

off1:Poke 7,255goto Temp_Display

off2:Poke 7,255Low B4goto Temp_Relay

off3:Poke 7,255goto Fire_Display

off4:Poke 7,255Low B4goto Fire_Relay

six4:Poke 7,100Goto Temp_Displaysix6:Poke 7,102Goto Temp_Displaysix8:Poke 7,104Goto Temp_Displayseven0:Poke 7,112Goto Temp_Displayseven2:Poke 7,114Goto Temp_Displayseven4:

Page 77: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

77

Poke 7,116Goto Temp_Displayseven6:Poke 7,118Goto Temp_Displayseven8:Poke 7,120Goto Temp_Displayeight0:Poke 7,128Goto Temp_Displayeight2:Poke 7,130Goto Temp_Displayeight4:Poke 7,132Goto Temp_Displayeight6:Poke 7,134Goto Temp_Displayeight0R:Poke 7,128Low B4Goto Temp_Relayeight2R:Poke 7,130Low B4Goto Temp_Relayeight4R:Poke 7,132Low B4Goto Temp_Relayeight6R:Poke 7,134Low B4Goto Temp_Relay

RelayT:High B4Goto Temp_Relay

RelayF:High B4Goto Fire_Relay

Flash:Poke 7,0Pause 100

Page 78: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

78

Poke 7,255Pause 100Goto Fire_Display

End

EXPLANATION OF PROGRAM

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

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

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

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

Demo3: If the switch setting is on the third position, then the value represented bythe 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 theactivation of the fire detector activates the relay circuit. If the switchsettings change, the program goes back to Demo1.

Page 79: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

79

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 displaysrepresenting the value output of the temperature sensor. If the value of the temperaturesensor reached a certain point, then the relay circuit was activated. If the flame detectorsensed a flame, then it would blink the LED displays or activate the relay circuit. In thenext chapter we will cover the use of a voice chip and will be programming thePIC16F877.

Page 80: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

80

Page 81: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

81

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 theInformation Storage Devices’ ISD25120P Single-Chip Voice Record/Playback Device.There are several different types of ISD chips such as the ISD2560, ISD2575, ISD2590and 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 loweringthe audio sampling rate. The lower the sampling rate the lower the quality of therecording (see Table 8.1). Voice and audio signals are stored directly in nonvolatilememory cells, providing direct analog storage. This process gives true, natural soundingreproduction of voice, music, tones and sound effects. The chip is also addressable tohandle multiple messages.

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 andplayed back in any order. To record an audio segment an address is selected to store the

PART DURATION INPUT SAMPLE TYPICAL FILTER PASSNUMBER (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

Page 82: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

82

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 takenhigh. Repeat this process for each recording. The length of the first message willdetermine at what address to start the second message. Each second of the recordedmessage (approximately), corresponds to an address length of the decimal value of ten. Ifyou’re first recorded message was five seconds long and started at address 0(dec.) thenyou’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 therecorded messages.The P/R pin is taken high and the CE pin is pulsed low. Repeat this process for each playback. The play back of the message must be completed before the CE pin can activatethe next message.

AMPLIFICATION CIRCUIT

The audio output of the ISD chip is capable of driving 50mw into a 16 ohm speaker. Ihave 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 pinPIC16F877 microcontroller needs to be placed in the 2840 Development Board. The2840 Development Board already has Port’s A,B and C connected to a 25-pin DBconnector (Connector A). That connector is attached to the board enclosure. Port’s Dand E need to be wired to a 15 pin DB connector (Connector B). That connector needs tobe attached to the other side of the enclosure. There will be two ribbon cables going toProject_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.

Page 83: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

83

PIC CONNECTOR (B)ISD25120

D0 PIN 1 VddD1 PIN 2

D2 PIN 3 0.1uf

D3 PIN 4

D4 PIN 5 VssD5 PIN 6

D6 PIN 7 To amplification ckt.D7 PIN 8

Vss 0.1uf

PIC CONNECTOR (A) Vss10k ohm

A0 PIN 1 5.1k ohm

100k ohm0.1uf Mic

Vss Vdd 0.1uf

100k ohm 10k ohm

1k ohm 0.1uf 1k ohm Vdd Vss Vss

(P) Vss 220uf

(R) Vdd Vss Vss Vdd

A0 Vccd

A1 Vcca

A2

A3 Vssd

A4 Vssa

A5

A6 SP +

A7 SP –

A8 AUX IN

A9 ANA IN

CE ANA OUT

PD

P/R MIC REF

EOM MIC

OVF

XCLK AGC

Page 84: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

84

PIC CONNECTOR (A)

10k ohmsVss

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

#4C5 PIN 18 Vdd

Vss

B5 PIN 11 #5 Vdd

LEDs 330 ohmsC0 PIN 23 #1 Vss

C1 PIN 22 #2 Vss

C2 PIN 21 #3 Vss

C3 PIN 20 #4 Vss

C4 PIN 19 #5 Vss

Page 85: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

85

100k ohm

1uf 1k ohm 10k ohm 2 1uf 10uf

1 3 5From ISD25120

3 2

1uf 10k ohm

Vss Vss

100k ohm 100k ohm 100uf

Vdd Vss 8 ohm speaker

Vss

TL082 LM386

Page 86: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

86

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 = 16F877MHZ = 10CONFIG 16382

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

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

Demo1:

Page 87: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

87

Poke 7,0High A0Debug["Place SW1 in Record",13]Debug["Press a button to record a message",13]Pause 1000Button B0,1,100,10,Work,1,Word1Button B1,1,100,10,Work,1,Word2Button B2,1,100,10,Work,1,Word3Button C5,1,100,10,Work,1,Word4Button B5,1,100,10,Work,1,Word5Button A1,1,100,10,Work,1,Word6Button A2,1,100,10,Work,1,Word7Button A3,1,100,10,Work,1,Word8Button A4,1,100,10,Work,1,Word9Goto Demo1

Word1:Poke 8,%00000000High C0Pause 100Low A0Button B0,1,100,10,Work,1,Demo1Goto Word1

Word2:Poke 8,%00010100High C1Pause 100Low A0Button B1,1,100,10,Work,1,Demo1Goto Word2

Word3:Poke 8,%00101000High C2Pause 100Low A0Button B2,1,100,10,Work,1,Demo1Goto Word3

Word4:Poke 8,%00111100High C3Pause 100Low A0Button C5,1,100,10,Work,1,Demo1Goto Word4

Page 88: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

88

Word5:Poke 8,%01010000High C4Pause 100Low A0Button B5,1,100,10,Work,1,Demo1Goto Word5

Word6:Poke 8,%01100100Pause 100Low A0Button A1,1,100,10,Work,1,Demo1Goto Word6

Word7:Poke 8,%01110011Pause 100Low A0Button A2,1,100,10,Work,1,Demo1Goto Word7

Word8:Poke 8,%10000010Pause 100Low A0Button A3,1,100,10,Work,1,Demo1Goto Word8

Word9:Poke 8,%10010001Pause 100Low A0Button A4,1,100,10,Work,1,StartGoto Word9

Demo2:High A0Poke 7,0Debug["Place SW1 in Playback",13]Debug["Press a button to play a message",13]Pause 1000Button B0,1,100,10,Work,1,Button1Button B1,1,100,10,Work,1,Button2Button B2,1,100,10,Work,1,Button3Button C5,1,100,10,Work,1,Button4Button B5,1,100,10,Work,1,Button5Button A1,1,100,10,Work,1,Button6

Page 89: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

89

Button A2,1,100,10,Work,1,Button7Button A3,1,100,10,Work,1,Button8Button A4,1,100,10,Work,1,Button9Goto Demo2

Button1:Poke 8,%00000000High C0Pause 500Low A0Pause 100Goto Demo2

Button2:Poke 8,%00010100High C1Pause 500Low A0Pause 100Goto Demo2

Button3:Poke 8,%00101000High C2Pause 500Low A0Pause 100Goto Demo2

Button4:Poke 8,%00111100High C3Pause 500Low A0Pause 100Goto Demo2

Button5:Poke 8,%01010000High C4Pause 500Low A0Pause 100Goto Demo2

Button6:Poke 8,%01100100Pause 500

Page 90: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

90

Low A0Pause 100Goto Demo2

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

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

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

Demo3:High A0Poke 7,0Pause 1700Debug["Playback all messages",13]Poke 8,%00000000Low A0Pause 200High A0Pause 3000Poke 8,%00010100Low A0Pause 200High A0Pause 3000Poke 8,%00101000Low A0Pause 200High A0Pause 3000Poke 8,%00111100Low A0Pause 200

Page 91: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

91

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

End

EXPLANATION OF PROGRAM

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

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

Demo1: Records messages. The Debug Window will instruct for SW1 to beplaced in the Record (R) position. When a button (#1-9) is pressed the

Page 92: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

92

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 therecording (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 beplaced in the Playback (P) position. When a button (#1-9) is pressed theLED next to the button will flash on and the yellow (CE) LED will flashon. The message will be played. After playing Word9 (Reverse), theprogram will go to the “Start” routine.

Demo3: Plays all recorded messages (#1-9). After the last message is played theprogram 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 chapterRF communication will be used for Project_8 to communicate to Project_9 and controltwo DC motors.

Page 93: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

93

CHAPTER 9: REMOTE CONTROL (Project_9a & 9b)

PROJECT_8 CHANGES

This chapter will use RF to have commands sent from Project_8 (Project_9a) toProject_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 changed5. 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 oftransferring 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-presentcarrier-absent) modulation. This type of AM modulation is often referred to by otherdesignations including CW and OOK. This type of modulation represents a logic “0” bythe 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 theEncoder chip. When the input has a logic low, the carrier is fully suppressed and thetransmitter consumes less than 2uA of current. When the input has a logic high thetransmitter 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 torecover data sent by a CPCA transmitter (TXM-433). The receiver’s output will only

Page 94: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

94

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

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

L= 234 / Fmhz L= Length in feet of quarter-wave lengthF= Operating frequency in megahertz

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

MOTOR ASSEMBLY

Two DC geared 12V motors are attached on a (Tape Reel) platform. Wheels are attachedto the motor rotors. An H-Bridge chip will drive the two motors. A separate powersupply 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. Thecomponents of Project_9b are: A RF Receiver, Decoder, two 12VDC geared motors witha battery power supply attached to a platform, a motor driver (SN754410), a 74LS04chip, 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 thenext and final project).

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

Page 95: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

95

Antenna

Vss Encoder chip pin 8 (Dout)

10uf 10 430 (tantalum) ohm ohm

Vss Vdd Gnd

Vdd

LED

1.5 V 1.5 V 1.5 V 1.5 V 330 ohm

Power switchVdd

Disconnect power (Vdd) from 2840 Development Bd.

TXM-433

1 2 3 4

5 Volt RegulatorOut Gnd In

Page 96: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

96

Connector (A) Connector (A)PIC PIN 4.7k ohm

Vdd B7 PIN 13 Encoder chip pin 9 (TE)

A0 PIN 1 Voice chip (CE)

A1 PIN 2 Button #6 (Forward) B5 PIN 11 Button #5

A2 PIN 3 Button #7 (Left)

A3 PIN 4 Button #8 (Right)

A4 PIN 5 Button #9 (Reverse) B2 PIN 8 Button #3

B1 PIN 7 Button #2

B0 PIN 6 Button #1

VddVss

Vdd Connector (B)D7 PIN 8 Voice (A7)*

Vss Resonator D6 PIN 7 Voice (A6)*

Vss D5 PIN 6 Voice (A5)*

D4 PIN 5 Voice (A4)*

C0 PIN 23 LED #1

C1 PIN 22 LED #2 Connector (A)

C2 PIN 21 LED #3 C5 PIN 18 Button #4

C3 PIN 20 LED #4 C4 PIN 19 LED #5Connector (B) Connector (B)D0 PIN 1 Voice chip (A0)* D3 PIN 4 Voice (A3)*

D1 PIN 2 Voice chip (A1)* D2 PIN 3 Voice (A2)*

* Add a 5 k ohm resistor tied to ground.

1 16F877 40socket

2 39

3 38

4 37

5 36

6 35

7 34

8 33

9 32

10 31

11 30

12 29

13 28

14 27

15 26

16 25

17 24

18 23

19 22

20 21

10Mhz

Page 97: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

97

8-BIT ENCODER

1B2 (Button #3) Vdd

2C5 (Button #4) B1 (Button #2)

3B5 (Button #5)

4A1 (Button #6)

5A2 (Button #7)

6A3 (Button #8)

7A4 (Button #9)

Transmitter (TXM-433) 8

Transmit enableB7

390k ohm

Vss

HT-640

D1 Vcc

D2 D0

D3 A9

D4 A8

D5 A7

D6 A6

D7 A5

Dout A4

TE A3

OSC1 A2

OSC2 A1

GND A0

Page 98: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

98

Follow the below schematic in wiring Project_9b.

Antenna

Decoder chip pin 9 (Din) Vss

10uf 10 200 (tantalum) ohm ohm

Vss Vdd

Vdd Vss20k ohms

Connector (A)

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

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

10k ohmVss

#1C4 PIN 19 Vdd

Vss

C3 PIN 20 #2 Vdd

RXM-433

1 2 3 4

LCD MODULE

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

Page 99: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

99

Vss

C2 PIN 21 #3 Vdd

Vss

#4C1 PIN 22 Vdd

Vss

C0 PIN 23 #5 Vdd

Vss

C7 PIN 16 #6 Vdd

Connector (B) Vss

E0 PIN 9 #7 Vdd

Vss

#8E1 PIN 10 Vdd

Vss

E2 PIN 11 #9 Vdd

12V motor power VddConnector (A)

8 16

B3 PIN 9 1 3

C5 PIN 18 2

74LS04pins 1,2 7 6

B2 PIN 8 9 14

C6 PIN 17 10

74LS04pins 3,4 15 11

4,5,12,13

Vss

M1 enable

M1 input A

M1 input B

M2 enable

M2 input A

M2 input B

R motor

L motor

Page 100: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

100

8-BIT DECODER De-activate Decoder switch *

1C2 (Button #3) Vdd

2C1 (Button #4) C3 (Button #2)

3C0 (Button #5)

4C7 (Button #6)

5E1 (Button #7)

6E2 (Button #8)

7E3 (Button #9)

8 Vdd

Data input from RF RCVR 9

LED390k ohm

470 ohms Vss

HT-648L 2.2k ohmValid transmit output

2N3904

Vss

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

D1 Vcc

D2 D0

D3 A9

D4 A8

D5 A7

D6 A6

D7 A5

VT A4

Din A3

OSC1 A2

OSC2 A1

GND A0

Page 101: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

101

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

CPU = 16F877MHZ = 10CONFIG 16382

Page 102: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

102

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

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

Demo1:Poke 7,0High A0Debug["Place SW1 in Record",13]Debug["Press a button to record a message",13]Pause 1000Button B0,1,100,10,Work,1,Word1Button B1,1,100,10,Work,1,Word2Button B2,1,100,10,Work,1,Word3Button C5,1,100,10,Work,1,Word4Button B5,1,100,10,Work,1,Word5Button A1,1,100,10,Work,1,Word6Button A2,1,100,10,Work,1,Word7Button A3,1,100,10,Work,1,Word8Button A4,1,100,10,Work,1,Word9Goto Demo1

Word1:Poke 8,%00000000High C0Pause 100Low A0Button B0,1,100,10,Work,1,Demo1Goto Word1

Word2:Poke 8,%00010100High C1Pause 100Low A0

Page 103: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

103

Button B1,1,100,10,Work,1,Demo1Goto Word2

Word3:Poke 8,%00101000High C2Pause 100Low A0Button B2,1,100,10,Work,1,Demo1Goto Word3

Word4:Poke 8,%00111100High C3Pause 100Low A0Button C5,1,100,10,Work,1,Demo1Goto Word4

Word5:Poke 8,%01010000High C4Pause 100Low A0Button B5,1,100,10,Work,1,Demo1Goto Word5

Word6:Poke 8,%01100100Pause 100Low A0Button A1,1,100,10,Work,1,Demo1Goto Word6

Word7:Poke 8,%01110011Pause 100Low A0Button A2,1,100,10,Work,1,Demo1Goto Word7

Word8:Poke 8,%10000010Pause 100Low A0Button A3,1,100,10,Work,1,Demo1Goto Word8

Page 104: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

104

Word9:Poke 8,%10010001Pause 100Low A0Button A4,1,100,10,Work,1,StartGoto Word9

Demo2:High A0Poke 7,0Debug["Place SW1 in Playback",13]Debug["Press a button to play a message",13]Pause 1000Button B0,1,100,10,Work,1,Button1Button B1,1,100,10,Work,1,Button2Button B2,1,100,10,Work,1,Button3Button C5,1,100,10,Work,1,Button4Button B5,1,100,10,Work,1,Button5Goto Demo2

Button1:Poke 8,%00000000High C0Low A0High B7Pause 700Low B7Loop:High A0High B7Pause 100Low B7Button A1,1,100,10,Work,1,Button6Button A2,1,100,10,Work,1,Button7Button A3,1,100,10,Work,1,Button8Button A4,1,100,10,Work,1,Button9Button B1,1,100,10,Work,1,Button2Button B2,1,100,10,Work,1,Button3Button C5,1,100,10,Work,1,Button4Button B5,1,100,10,Work,1,Button5Goto Loop

Button2:Poke 8,%00010100Low C0High C1Low A0High B7

Page 105: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

105

Pause 700Low B7Goto Demo2

Button3:Poke 8,%00101000Low C0High C2Low A0High B7Pause 700Low B7Goto Demo2

Button4:Poke 8,%00111100Low C0High C3Low A0High B7Pause 700Low B7Goto Demo2

Button5:Poke 8,%01010000Low C0High C4Low A0High B7Pause 700Low B7Goto Demo2

Button6:Poke 8,%01100100Low A0High B7Pause 700Low B7Goto Loop

Button7:Poke 8,%01110011Low A0High B7Pause 700Low B7

Page 106: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

106

Goto Loop

Button8:Poke 8,%10000010Low A0High B7Pause 700Low B7Goto Loop

Button9:Poke 8,%10010001Low A0High B7Pause 700Low B7Goto Loop

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

Poke 8,%00010100Low A0Pause 200High A0Pause 3000

Poke 8,%00101000Low A0Pause 200High A0Pause 3000

Poke 8,%00111100Low A0Pause 200High A0Pause 3000

Poke 8,%01010000

Page 107: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

107

Low A0Pause 200High A0Pause 3000

Poke 8,%01100100Low A0Pause 200High A0Pause 3000

Poke 8,%01110011Low A0Pause 200High A0Pause 3000

Poke 8,%10000010Low A0Pause 200High A0Pause 3000

Poke 8,%10010001Low A0Pause 200High A0Pause 3000Goto Start

End

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

CPU = 16F877MHZ = 10CONFIG 16382

Work var byteWork = 0

Page 108: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

108

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

Start:Low B2Low A4Button C3,1,100,10,Work,1,Demo2Button C2,1,100,10,Work,1,Demo3Button C1,1,100,10,Work,1,Demo4Button C0,1,100,10,Work,1,Demo5

Demo1:LCDWRITE [Clear,Home,SCR]LCDWRITE ["REMOTE CONTROL"]PAUSE 100Low B2Low A4Debug["Demo1 "]Button C7,1,100,10,Work,1,ForwardButton E0,1,100,10,Work,1,LeftButton E1,1,100,10,Work,1,RightButton E2,1,100,10,Work,1,ReversButton C3,1,100,10,Work,1,Demo2Button C2,1,100,10,Work,1,Demo3Button C1,1,100,10,Work,1,Demo4Button C0,1,100,10,Work,1,Demo5Goto Demo1

Forward:Low C5Low C6LCDWRITE [Clear,Home,SCR]LCDWRITE ["FORWARD"]High B2High A4Pause 1000Goto Demo1

Left:Low C6High B2LCDWRITE [Clear,Home,SCR]LCDWRITE ["LEFT"]Pause 1000Goto Demo1

Right:Low C5

Page 109: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

109

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

Revers:High C5High C6LCDWRITE [Clear,Home,SCR]LCDWRITE ["REVERSE"]High B2High A4Pause 1000Goto Demo1

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

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

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

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

END

Page 110: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

110

EXPLANATION OF PROGRAMS

Project_9a has four parts labeled: Start, Demo1, Demo2, and Demo3. Following is ashort description of what each part of the program does. The program is basically thesame 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 programwill go to Demo1 (recording messages). If #3 button is pressed at thebeginning of the program, the program will go to Demo3 (playback allrecorded messages). If no buttons are pressed at the beginning of theprogram, then the program goes to Demo2 (playback message for eachbutton 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 andthe 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 onand the yellow (CE) LED will flash on. A specified message will beplayed for each button pressed. The button information also goes to theEncoder. The output of the Encoder goes to the transmitter. Buttons #6-9will play their messages only when button #1 (remote control) is pressed.

Demo3: Plays all recorded messages (#1-9). After the last message is played theprogram 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 pressedon Project_9a

Page 111: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

111

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 signalfrom Project_9a. If buttons (#6-9) are pressed the program will go to theroutines to activate the motors (Forward, Left, Right, Reverse). If otherbuttons (#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 DCmotors were installed with an H-bridge for Project_9b to have locomotion.

Page 112: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

112

Page 113: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

113

CHAPTER 10: MULTI-PURPOSE ROBOT (Project_10)

PROJECT_9b CHANGES

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

1. Connectors installed2. Quad bilateral switches installed3. 40-pin socket wired (for 16F877)4. Project_9b program changed5. 16F877 programmed and placed in 40-pin socket6. Battery power supply connected

These changes are illustrated in the schematic section of this chapter. The programchanges 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 beingused 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 ortabletop. 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. TheTemperature sensor will have the temperature displayed on an LCD. The Fire sensor willdetect 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 sensorinformation 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

Page 114: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

114

CONNECTORS

Each sensor will have a cable connected to the main circuit board. Nine connectors areneeded. 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

UltrasonicConnector * Pin 1 Vss to ultrasonic sensor

* Pin 2 Analog signal from ultrasonic circuit* Pin 3 Vdd to ultrasonic sensor

MotorConnector * 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 PwrConnector * 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

Page 115: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

115

BatteryConnector * 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 a40-pin socket that the programmed 16F877 can be place in. The components ofProject_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 1A1 2 4066 chips pin 4A2 3 4066 chips pin 8A3 4A4 5 Motor (M1 Enable)B0 6 LCD (RS)B1 7 LCD (E)B2 8 Motor (M2 Enable)B3 9B4 10B5 11B6 12B7 13Vdd 14

15C7 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)

24Vss 25

Page 116: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

116

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

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

12131415

Connector (A) Quad Bilateral SwitchPIC PIN 4066 Demo3 & Demo4

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

13

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

5

A2 PIN 3 A/D input 8 9 Temp sensor

6

Connector (B) 11 10 Vdd

D4 PIN 5 Switch control (A) 12

Vss 7 14 Vdd

(A)

Page 117: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

117

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

6

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

6

Connector (B) 11 10 Vdd

D6 PIN 7 Switch control (C) 12

Vss 7 14 Vdd

(C) 9 Volts Vdd + _

diode - - - LEDs

**

10k ohm etc.

VssLight to illuminate path for line following

Relay coil

Page 118: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

118

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

Vdd

A0 PIN 1 Switches pin 1

A1 PIN 2 Switches pin 4

A2 PIN 3 Switches pin 8

Connector (A)

A4 PIN 5 Motor (M1 Enable) B2 PIN 8 Motor (M2 Enable)

Connector (B) B1 PIN 7 LCD (E)E0 PIN 9 Decoder (Left)

B0 PIN 6 LCD (RS)E1 PIN 10 Decoder (Right)

VddE2 PIN 11 Decoder (REV) Vss

Vdd Connector (B)D7 PIN 8 Fire detect

Vss Resonator D6 PIN 7 Switch C control

Vss D5 PIN 6 Switch B control

D4 PIN 5 Switch A control Connector (A)

C0 PIN 23 Button #5 C7 PIN 16 Decoder (FWD)

C1 PIN 22 Button #4 C6 PIN 17 Motor (M2 A)

C2 PIN 21 Button #3 C5 PIN 18 Motor (M1 A)

C3 PIN 20 Button #2 C4 PIN 19 Button #1Connector (B) Connector (B)D0 PIN 1 LCD pin 11 D3 PIN 4 LCD pin 14

D1 PIN 2 LCD pin 12 D2 PIN 3 LCD pin 13

1 16F877 40socket

21 39

22 38

23 37

24 36

25 35

26 34

27 33

28 32

29 31

30 30

31 29

32 28

33 27

34 26

35 25

36 24

37 23

38 22

39 21

10Mhz

Page 119: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

119

Vdd

LED

1.5 V 1.5 V 1.5 V 1.5 V 330 ohm

Power switchVdd

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

5 Volt RegulatorOut Gnd In

Page 120: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

120

Page 121: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

121

Page 122: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

122

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 installit in the socket on Project_10.

CPU = 16F877MHZ = 10CONFIG 16382

LCDPORT con PORTDPause 500LCDWRITE[INITLCD1,INITLCD2,CLEAR,HOME,SCR]TRISA = $FFADSETUP con %10000010IR1_info var wordIR2_info var wordU_info var wordIR1_init var wordIR2_init var wordU_init var wordFIRE_info var wordTEMP_info var wordFLEX1_info var wordFLEX2_info var wordFLEX1_init var wordFLEX2_init var word

Page 123: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

123

LINE1_info var wordLINE2_info var wordLINE3_info var wordLINE1_init var wordLINE2_init var wordLINE3_init var wordWork var byteWork = 0Low B5Low D4Low D5Low D6

Start:LCDWRITE["INITIALIZING"]Low B2Low A4High D5Pause 1000ADIN 0,2,ADSETUP,IR1_initADIN 1,2,ADSETUP,IR2_initADIN 2,2,ADSETUP,U_initLow D5High D4Pause 1000ADIN 0,2,ADSETUP,FLEX1_initADIN 1,2,ADSETUP,FLEX2_initLow D4High D6Pause 1000ADIN 0,2,ADSETUP,LINE1_initADIN 2,2,ADSETUP,LINE2_initADIN 1,2,ADSETUP,LINE3_initLow D6IR1_init = IR1_init + 70IR2_init = IR2_init + 70U_init = U_init + 25FLEX1_init = FLEX1_init - 35FLEX2_init = FLEX2_init - 35LINE1_init = LINE1_init + 50LINE2_init = LINE2_init + 50LINE3_init = LINE3_init + 50

Demo1:LCDWRITE [Clear,Home,SCR]LCDWRITE ["REMOTE CONTROL"]PAUSE 100Low D4

Page 124: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

124

Low D5Low D6Low B2Low A4Button C3,1,100,10,Work,1,Demo2Button C2,1,100,10,Work,1,Demo3Button C1,1,100,10,Work,1,Demo4Button C0,1,100,10,Work,1,Demo5Button C7,1,100,10,Work,1,ForwardButton E0,1,100,10,Work,1,LeftButton E1,1,100,10,Work,1,RightButton E2,1,100,10,Work,1,ReversGoto Demo1

Demo2:High D5Low D4Low D6Low B2Low A4LCDWRITE[CLEAR,HOME]LCDWRITE["OBJECT DETECT"]Pause 500Button C4,1,100,10,Work,1,Demo1Button C3,1,100,10,Work,1,Demo2aButton C2,1,100,10,Work,1,Demo3Button C1,1,100,10,Work,1,Demo4Button C0,1,100,10,Work,1,Demo5Goto Demo1Demo2a:ADIN 0,2,ADSETUP,IR1_infoDEBUG["IR1_INFO "]DEBUG[dec IR1_info,13]DEBUG["IR1_INIT "]DEBUG[dec IR1_init,13]

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

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

Page 125: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

125

IF IR1_info > IR1_init then DetRIF IR2_info > IR2_init then DetLIF U_info > U_init then DetFGoto Demo2

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

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

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

Demo3:High D4Low D5Low D6Low B2Low A4LCDWRITE[CLEAR,HOME]LCDWRITE["EDGE AVOIDANCE"]Pause 500Button C4,1,100,10,Work,1,Demo1Button C3,1,100,10,Work,1,Demo2Button C2,1,100,10,Work,1,Demo3aButton C1,1,100,10,Work,1,Demo4Button C0,1,100,10,Work,1,Demo5Goto Demo1Demo3a:ADIN 0,2,ADSETUP,FLEX1_infoDEBUG["FLEX1_INFO "]DEBUG[dec FLEX1_info,13]DEBUG["FLEX1_INIT "]DEBUG[dec FLEX1_init,13]

ADIN 1,2,ADSETUP,FLEX2_infoDEBUG["FLEX2_INFO "]

Page 126: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

126

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

If FLEX1_info < FLEX1_init AND FLEX2_info < FLEX2_init then MovBIF FLEX1_info < FLEX1_init then MovRIF FLEX2_info < FLEX1_init then MovLGosub FWDGoto Demo3

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

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

MovB:LCDWRITE[SCRRAM+$40,"MOVE BACK"]Pause 500Gosub Rev_RLGosub R_RotateGoto Demo3

Demo4:High D4Low D5Low D6LCDWRITE[CLEAR,HOME]LCDWRITE["FIRE/TEMP READ"]Pause 500Button C4,1,100,10,Work,1,Demo1Button C3,1,100,10,Work,1,Demo2Button C2,1,100,10,Work,1,Demo3Button C1,1,100,10,Work,1,Demo4aButton C0,1,100,10,Work,1,Demo5Goto Demo1Demo4a:Pulsin D7,1,FIRE_infoDEBUG["FIRE_INFO "]DEBUG[dec FIRE_info,13]If FIRE_info > 2000 then Fire

Page 127: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

127

ADIN 2,2,ADSETUP,TEMP_infoDEBUG["TEMP_INFO "]DEBUG[dec TEMP_info,13]IF TEMP_info > 166 then Temp5IF TEMP_info > 163 then Temp3IF TEMP_info > 160 then Temp1IF TEMP_info > 95 then Temp9Goto Demo4

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

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

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

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

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

Demo5:High D6Low D4Low D5Low B2Low A4LCDWRITE[CLEAR,HOME]LCDWRITE["LINE FOLLOWING"]Pause 500Button C4,1,100,10,Work,1,Demo1Button C3,1,100,10,Work,1,Demo2Button C2,1,100,10,Work,1,Demo3Button C1,1,100,10,Work,1,Demo4Button C0,1,100,10,Work,1,Demo5a

Page 128: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

128

Goto Demo1Demo5a:ADIN 0,2,ADSETUP,LINE1_infoDEBUG["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_infoDEBUG["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_infoDEBUG["LINE2_INFO "]DEBUG[dec LINE2_info,13]DEBUG["LINE2_INIT "]DEBUG[dec LINE2_init,13]IF LINE2_info > LINE2_init then L_CLCDWRITE[SCRRAM+$40,"ROBOT ON LINE?"]Pause 3000Goto Demo5

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

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

L_C:LCDWRITE[SCRRAM+$40,"IN CENTER"]Pause 500Low C5Low C6High B2

Page 129: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

129

High A4Pause 100Goto Demo5

Forward:Low C5Low C6LCDWRITE [Clear,Home,SCR]LCDWRITE ["GO FORWARD"]High B2High A4Pause 500Goto Demo1

Left:Low C6High B2LCDWRITE [Clear,Home,SCR]LCDWRITE ["TURN LEFT"]Pause 500Goto Demo1

Right:Low C5High A4LCDWRITE [Clear,Home,SCR]LCDWRITE ["TURN RIGHT"]Pause 500Goto Demo1

Revers:High C5High C6LCDWRITE [Clear,Home,SCR]LCDWRITE ["GO REVERSE"]High B2High A4Pause 500Goto Demo1

R_Rotate:Low C5High C6LCDWRITE [Clear,Home,SCR]LCDWRITE ["ROTATE RIGHT"]High A4High B2

Page 130: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

130

Pause 500Return

L_Rotate:High C5Low C6LCDWRITE [Clear,Home,SCR]LCDWRITE ["ROTATE LEFT"]High A4High B2Pause 500Return

Rev_RL:LCDWRITE [Clear,Home,SCR]LCDWRITE ["REVERSE"]High C5High C6High B2High A4Pause 500Return

FWD:LCDWRITE [Clear,Home,SCR]LCDWRITE ["FORWARD"]Low C5Low C6High B2High A4Pause 100Return

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 andDemo5 have their initial readings recorded. The program then goes toDemo1.

Page 131: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

131

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

Demo2: Displays “OBJECT DETECT” on the LCD and then takes A/D readingsof the Ultrasonic sensor and IR sensors. If those readings exceed theinitial readings of those sensors in the “Start” routine then the programgoes 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. Theprogram continues to take A/D readings until a different Demo button ispressed.

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

Demo4: Displays “FIRE/TEMP READ” on the LCD. An A/D reading of theTemperature sensor is taken. A reading of the fire sensor is taken by thepulsin command on Port D pin 7. The readings determine whatinformation is displayed on the LCD. The program continues to takereadings until a different Demo button is pressed.

Demo5: Displays “LINE FOLLOWING” on the LCD and takes A/D readings ofthe photocells in the line following circuit board. If those readings exceedthe initial readings of those photocells in the “Start” routine then theprogram goes to the routine that will display what position the robot is incompared to where the line is. The routine will tell the motors to move ina certain direction to stay on the line. The program continues to take A/Dreadings 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 andProject_10 (the Robot). An LCD on the Robot is used to display what buttons arepressed. The sensors in conjunction with the Demo routines determine what directionsthe motors will move the Robot.

Page 132: 10 Projects With Mikrobasic Pic

Ten Projects With The MBasic Compiler

132