41339105 10 Projects With Mikrobasic Pic

132

Transcript of 41339105 10 Projects With Mikrobasic Pic

  • Ten Projects With The MBasic Compiler

    2

    INTRODUCTION _____________________________________________________3-4CHAPTER 1: Project_1 GETTING STARTED_____________________________5-14CHAPTER 2: Project_2 LCD ALPHANUMERIC DISPLAY_________________15-22CHAPTER 3: Project_3 MOTOR CONTROL _____________________________23-32CHAPTER 4: Project_4 7-SEGMENT LED DISPLAY _____________________33-44CHAPTER 5: Project_5 RESISTIVE-TYPE SENSORS_____________________45-58CHAPTER 6: Project_6 DISTANCE SENSORS___________________________59-70CHAPTER 7: Project_7 TEMPERATURE & FLAME SENSORS ____________71-80CHAPTER 8: Project_8 VOICE RECORD/PLAYBACK DEVICE ____________81-92CHAPTER 9: Project_9a &9b REMOTE CONTROL______________________93-112CHAPTER 10: Project_10 MULTI-PURPOSE ROBOT___________________113-132

  • 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.

  • 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!

  • 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. Lets 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.

  • 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.

  • 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,IfThen, ForNext, 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

  • Ten Projects With The MBasic Compiler

    8

  • 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:

  • 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

  • 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

  • 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

  • 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.

  • Ten Projects With The MBasic Compiler

    14

  • 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 currents 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:

  • 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

  • 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, IfThen, ForNext, RepeatUntil, 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.

  • 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

  • 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.

  • 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

  • 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

  • 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.

  • 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 PICs 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. Its 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

  • 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 motors 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

  • 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: WhileDo, 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

  • 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

  • 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

  • Ten Projects With The MBasic Compiler

    28

  • 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

  • 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

  • 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

  • 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 motors rotor turns (steps) in the CW direction for a certaindistance. If B is pressed, the motors 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.

  • 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.

  • 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

  • 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 GosubReturn (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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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 Cs 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 labeledStart 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.

  • 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.

  • Ten Projects With The MBasic Compiler

    46

    Table 5.1

  • 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)

  • 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

  • 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

  • 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]

  • 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

  • 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:

  • 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

  • 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

  • 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

  • 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 switchs 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.

  • 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.

  • Ten Projects With The MBasic Compiler

    58

  • 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 dont 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.

  • 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 -

  • 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

  • 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

  • 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

  • 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

  • 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:

  • 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:

  • 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

  • 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]

  • 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 rotaryswitchs 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.

  • 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 ifthen 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 ifthen 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 byifthen 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 ifthen 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.

  • 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.

  • 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

  • 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

  • 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

  • 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

  • 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:

  • 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

  • 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 sensors output is stored in itsdesignated variable. The pulse output value of the fire detector is alsostored in its designated variable. The rotary switchs 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.

  • 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.

  • Ten Projects With The MBasic Compiler

    80

  • 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

  • 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. Ifyoure first recorded message was five seconds long and started at address 0(dec.) thenyoure 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 Ports A,B and C connected to a 25-pin DBconnector (Connector A). That connector is attached to the board enclosure. Ports 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.

  • 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

  • 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

  • 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

  • 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:

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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.

  • 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 & ANTENNAS

    The receiver being used is the LINX Technologies RXM-433-LC. It is designed torecover data sent by a CPCA transmitter (TXM-433). The receivers output will only

  • 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 ge