a guide to using pic-logicator software and connecting to...

56
A guide to using PIC-Logicator software and connecting to the PIC microcontroller TM Version 2 © Copyright Economatics (Education) Ltd. 2001 No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form by any means, without prior permission of the copyright holder. Copyright is waived in the following circumstances: small number of copies may be made for use only in the purchaser’s school. These copies may not be sold or made available outside the purchaser’s school. ®

Upload: others

Post on 28-Jan-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

A guide to usingPIC-Logicator software

and connecting to the PICmicrocontroller

TM

Version 2 © Copyright Economatics (Education) Ltd. 2001No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form

by any means, without prior permission of the copyright holder.

Copyright is waived in the following circumstances: small number of copies may be made for use only inthe purchaser’s school. These copies may not be sold or made available outside the purchaser’s school.

®

Page 2: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

2

Page 3: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

PIC-Logicator Pack .................................................................................4

Section One:

PIC-Logicator Software

Index of PIC-Logicator Commands ........................................................8

1. How to build, edit and test run a PIC-Logicator flowsheet ................9

Using the Programmer......................................................................13

2. Outputs .............................................................................................14

3. Inputs ................................................................................................18

4. Macros ..............................................................................................26

5. Variables ...........................................................................................32

Section Two:

Connecting to the PIC Microcontroller

1. PIC Microcontrollers .........................................................................41

2. PIC Microcontroller Pin-out Diagrams..............................................45

3. Basic Connections ...........................................................................47

4. Connecting Output Devices.............................................................49

5. Connecting Input Devices ...............................................................55

Contents

3

Page 4: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

PIC-Logicator Pack

The PIC-Logicator Pack contains:

1. PIC-Logicator software for Windows (Site licence).

2. Programmer.

3. Serial lead to connect the Programmer to the computer.

4. Plug-in type power supply for use with the Programmer.

5. This book.

Getting StartedInstall the PIC-Logicator software onto the hard drive of your computer using your normalmethod of installing software. Then see Section One for information on how to use the softwareand the Programmer. See Section Two for information on connection to the PIC microcontrollerchip.

WebsiteThe Economatics website provides the latest PIC-Logicator news and information. It includes:FAQs, projects and programming ideas, PCB designs to download, and an interactive demo ofPIC-Logicator software.

www.economatics.co.uk/education

1

5

2

3

4

4

Page 5: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Section One:

PIC-LogicatorSoftware

Page 6: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

6

Page 7: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

PIC-Logicator provides a graphicalenvironment for designing, testing, editingand downloading control sequences for PICmicrocontrollers.

The range of PIC-Logicator commandsallows you to control output devices, such asmotors and lamps, that are connected to thePIC microcontroller. You can switch deviceson or off in sequences using: timing,counting, repetition, and decisions based onsignals from digital and analogue sensorsthat are connected to the PIC microcontroller.

This section of the book explains how thesoftware is used, giving examples of thevarious commands and techniques in thecontext of possible school projects. It isorganised under the following headings:

1.How to build, edit and testrun a PIC-Logicatorflowsheet

2. OutputsThis section shows:

• how to switch output devices and motorsconnected to outputs of a PICmicrocontroller, using Outputs, MOTOR,SOUND and OUT commands;

• how timing can be built into a controlsystem using WAIT or SLEEP commands;

• how the SEROUT command can be usedto output serial information from the PICmicrocontroller.

3. InputsThis section shows:

• how to check the state of digital sensorsconnected to a PIC microcontroller usingthe Digital Decision command;

• how to use Event and Interrupt commandsfor instant response to digital sensors;

• how to use the Compare Decisioncommand to make use of readings fromanalogue sensors connected to a PICmicrocontroller, in a control system.

4. MacrosThis section shows the important techniqueof building a control system as a number oflinked sub systems.

5. VariablesThis section shows:

• how to create counting systems using INCand DEC commands;

• how timing can be built into a controlsystem using the TIME variable;

• how Expression, IN and RND commandsare used to give a value to a variable;

• how READ and WRITE commands areused to store and access values ofvariables using the PIC microcontroller’sEEPROM memory.

Overview

7

Quick Start

If you are unfamiliar with the Logicator approach to building control systems, it is a good ideato begin by familiarising yourself with the most commonly used commands which are: Outputs,WAIT, MOTOR and Digital Decision (see the Index of Commands page 8). Build and test run theExamples, using section 1 (“How to build, edit and test run a PIC-Logicator flowsheet”) asreference to help you.

Page 8: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

8

Comment...................................................................10

Compare Decision ....................................................24

DEC ...........................................................................33

Digital Decision .........................................................19

Do Macro...................................................................26

END ...........................................................................26

Event .........................................................................22

EVENT .......................................................................22

Expression.................................................................35

IN...............................................................................37

INC ............................................................................32

Interrupt.....................................................................23

INTERRUPT ...............................................................23

Macro ........................................................................26

MOTOR .....................................................................16

OUT ...........................................................................15

Outputs......................................................................14

READ.........................................................................38

RND...........................................................................37

SEROUT ....................................................................17

SLEEP .......................................................................17

SOUND......................................................................15

START........................................................................10

STOP .........................................................................10

WAIT ..........................................................................14

WRITE........................................................................38

Index of PIC-Logicator Commands

Command List

PIC-Logicator commands are accessed from the Command List on the right hand side of thescreen. Scroll the list to see the full range of commands. You can use the Options>SetupCommand List menu to customise the list, by changing the number of commands included, andthe sequence in which they appear.

Page 9: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

In PIC-Logicator, you create your controlsystem in the form of a flowchart bydragging commands from the CommandList and placing them in cells on theflowsheet working area (See fig. 1.1).

Options>PIC Type Before you begin to build a flowsheet, youshould decide which PIC microcontrollerchip you intend to use in your project.

Select this chip from the Options>PIC Typemenu. When you select a chip, thesoftware automatically configures itself todisplay only the input, output and motoroptions available with that chip.

You can then use the commands’ CellDetails boxes to fill in their details asrequired, and complete the flowsheet bydrawing routes to connect the cells.

When the flowsheet runs, the flow ofcontrol follows the route you have drawn,carrying out the command in each cell asit passes through it.

NOTE: The Options>PIC Type menu includesa “None” option. If you select this, you willfind that the software configures itself toshow 8 outputs, 4 motors, 8 digital inputsand 4 analogue inputs. You can build andtest run a flowsheet using this option, but youcannot download it to the Programmer.

1. How to build, edit and test run a PIC-Logicator flowsheet

9

CommandList

Route

Cell

CommandCell Detailsbox

Flowsheetworking area

Fig. 1.1. Pic-Logicator screen

Page 10: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

CommandsNOTE: This chapter deals only with drawingthe flowsheet. Details of how to use thevarious PIC-Logicator commands are givenelsewhere in Section One. See the Index of commands on page 8.

Creating a command cellDrag the required command from theCommands List and place it on anunoccupied cell.

Most commands have their own Cell Detailsdialog box which allows you to enter thecommand details. Double click on thecommand to open its Cell Details dialog box,and set the details of the command asrequired.

When you have set the necessary details,click OK to close the dialog box.

START and STOP commandsThese two commands do not have CellDetails dialog boxes. Simply place them onthe flowsheet working area.

A START command marks the point wherethe flowsheet starts running. When the PICmicrocontroller is reset or powered up, theflowsheet starts at the START command.Every flowsheet must have a STARTcommand.

A flowsheet will stop running whenever aSTOP command is reached.

Labelling a command It can be useful to give a command a labelwhich identifies what it is used for, e.g.“switches on lamp”. When you open a CellDetails box, the text in the yellow “label” boxwill be highlighted, so just type your labeland click OK. This text does not affect theoperation of a command; it is only a label.

Comment Comment commands allow you to add shortexplanatory notes to a flowsheet. Although

you can type up to 34 characters into the textbox in the Cell Details box, the number ofcharacters actually appearing in a Commentcell on the flowsheet will depend on factorssuch as the Zoom setting and screen setting.The default screen setting shows up to 16characters in a Comment cell.

Comments have no effect on the operation ofa flowsheet.

Fig 1.2 Explanatory information can be added to

the flowsheet by using command labels and

Comment commands.

Selecting a block ofcommandsHold down theCtrl key as youclick at the topleft hand cornerof the block anddrag down to theright to enclosethe requiredcommands androutes in theselection frame.

Fig 1.3. A block of commands

in the selection frame.

Selected commands are coloured red. Todeselect commands, click on another part ofthe flowsheet.

Deleting a command Click on the command to select it. Selectedcommands are coloured red.

Press the Delete key to delete the selectedcommand.

To delete a block of commands, select theblock and press the Delete key.

10

Page 11: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Moving commandsTo move a single command, drag it to its newposition.

To move a block of commands, select themand drag them to their new position.

Cutting, Copying and PastingUse the Cut, Copy and Paste options fromthe Edit menu to cut or copy selectedcommands or blocks of commands andpaste them either into another part of thesame flowsheet or into a different flowsheet.

Alternatively, you can copy commands orblocks of commands within a flowsheet byfirst selecting them and then holding downthe Ctrl key as you drag them to their newposition. Remember that copied commandswill retain their existing cell details.

Flowsheet working area Cells are arranged in rows and columns.Each flowsheet has 20 columns and 100rows. The default screen shows just 5columns and 12 rows.

Use the View>Zoom menu if you want tochange the number of cells visible on thescreen.

MapThe Map option allows you to view the wholeof the flowsheet at once. The red squaremarks the area currently displayed on the

screen. If you click on any part of the Map,the red square will move to that area, andthat area will be displayed on the screen.

RoutesRoutes can be drawn through the middle of acell, or in either one of the two rails betweencells, as shown in fig 1.4.

Routes must be drawn in the direction thatyou want flow to take when the flowsheetruns.

Drawing routesClick with the right mouse button on thecommand at the start of the route. Thecursor will change to a pencil. Hold downthe right mouse button and draw the route.Release the mouse button when you havecompleted the route.

Deleting routesClick at the beginning of the route to bedeleted, and press the Delete key.

When you draw a new route from acommand, the existing route from thecommand will automatically be deleted.

To delete a route without deleting thecommand in which it starts: first click on thecommand to select it. Then hold down theCtrl key as you press the Delete key.

11

Fig 1.4. Routes

can be drawn

through cells or

between rails.

Page 12: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

How to test run aflowsheetBefore you download a flowsheet to a PICmicrocontroller, it is useful to be able to checkthat it works as you intend it to. PIC-Logicatorhas a number of features that allow you totest run the flowsheet in the software.

1. The Digital PanelAs a flowsheet runs, the Digital Panel showsthe changing state of outputs, motors andinputs as they would be if the flowsheet hadbeen downloaded to a PIC microcontroller.

To display the Digital Panel, select theView>Digital Panel menu. Alternatively, clickthe toolbar icon shown in fig 1.5.

2. Simulating digital inputsThe function keys on the computer keyboardare used to simulate inputs from digital sensorswhile a flowsheet is running.

Function keys F9 to F2 will simulate digitalsensors connected to inputs 0 to 7 on a PICmicrocontroller. Key F9 simulates input 0;key F2 simulates input 7.

Pressing the function key is equivalent to thesensor being “on” (1). When the key is notpressed, it is equivalent to the sensor being“off” (0).

3.Simulating analogue inputsThe Analogue Panel allows you to simulate thechanging reading from analogue sensorswhile a flowsheet is running.

Identify the sensor (A0 to A3) which you wishto simulate, and use the slider in the panel tovary the simulated reading from 0 to 255.

Fig 1.6. Analogue Panel and its toolbar icon

To display the Analogue Panel, select theView>Analogue Panel menu. Alternatively,click the toolbar icon shown in fig 1.6.

4. Run and StopTo test run a flowsheet, either click the Run!menu or the green toolbar icon. To stop aflowsheet running, click the Stop! menu orthe red toolbar icon.

As the flowsheet runs, the flow of control ishighlighted so that you can follow it.

If you want to slow down the speed at whichflow is highlighted, select theOptions>Speed... menu, and use the dialogbox to adjust the speed.

5. Variables and EEPROMdisplay windows

If your flowsheet uses variables, it is useful todisplay the Variables window when you testrun it. The changing values of any of thevariables A to H that are used in theflowsheet will be displayed as the flowsheetruns. To see the changing value of the TIMEvariable, select the TIME window.

Fig 1.7.

The Variables window.

The EEPROM display window shows thevalue in each of the 16 addresses, when theflowsheet uses the READ and WRITEcommands.

Fig 1.8.

The EEPROM window.

All of these display windows are selectedfrom the View menu.

Downloading a flowsheetSee “Using the Programmer” (page 13) forinstructions on downloading a flowsheet to aPIC microcontroller.

12

Fig 1.5. DigitalPanel and itstoolbar icon

Page 13: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Using the Programmer

1. Use the serial lead to connect betweenthe socket on the Programmer marked“TO COMPUTER” and the serial portsocket of your computer.

2. Use only the power supply supplied in thePIC-Logicator Pack.

Plug the power supply lead into the socketon the Programmer marked “POWER 9VDC”. Plug the power supply into aconvenient mains socket. The green LEDmarked “POWER” will come on.

3. Lift the Zero Insert Force (ZIF) socketlever into its upright position.

4. The legend on the Programmer indicatescorrect positioning of 8,18 and 28 pinchips. Carefully insert a PICmicrocontroller chip into the appropriatesockets, and return the lever to its downposition.

5. PIC-Logicator software should be running,and the current flowsheet should be theone that you want to download. Click the“Program PIC” menu option or thetoolbar icon.

6. The yellow LED marked “ACTIVE” willcome on, and the Download bar will bedisplayed in the software. After about 20seconds, the yellow LED will switch offand the Download bar will disappear.Downloading is now complete.

7. Lift the ZIF socket lever into its uprightposition. Carefully remove the PICmicrocontroller chip which is nowprogrammed with your control flowsheet.

To reprogram a PIC microcontroller chip,remove it from the project circuit and followthe same procedure to download the revisedflowsheet. There is no need to erase the chipbefore reprogramming.

ZIF Socket lever

13

Page 14: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Outputs command

Use an Outputs command to switch on or offany output devices that are connected to theoutputs of a PIC microcontroller.

The “Output Port” line of its Cell Details box(fig. 2.1) shows the number of outputsavailable for use.

Fig 2.1. Outputs commandCell Details box for a16F84 chip (eightoutputs).

Each one of the digits in the Output Portrepresents one of the outputs on the PICmicrocontroller. You can click each digit toset it to switch an output device on or off.

This means: switch this output off.

This means: switch this output on.

This means: ignore this output. Leave itin the state in which it was set by theprevious Outputs command.

Click OK.

WAIT command

A WAIT command makes a running flowsheetpause for the number of seconds specifiedbefore the next command is carried out. Youcan use it to keep output devices switchedon or off for a set time.

Use its Cell Details box to enter a number ofseconds (Max 10s. Min 0.1s). Click OK.

Example

A PIC microcontroller has 3 LEDs connectedto outputs 0, 1 and 2. The flowsheet shown infig. 2.2 will switch them on and off in a timedsequence. The sequence will begin as soonas the chip is powered and will stop at theSTOP command, so it will do the sequencejust once.

The flowchart shown in fig. 2.3 will continueto repeat the sequence until power to thechip is switched off. Notice that anotherWAIT command has been added to therepeating sequence. The PIC microcontrolleroperates so quickly that, without WAITcommands, the LEDs would switch on andoff so quickly that you would not see ithappening.

Fig 2.3. Repeating timed sequence.

2. Outputs

14

0

1

0 0 0 0 0 0 0 1

0 0 0 0 0 0 1 •

0 0 0 0 0 1 • •

0 0 0 0 0 0 0 0

Switch on LED connected to output 0.Note that all other outputs areswitched off by this command.

LEDs connected to outputs 0 and1 are on. Notice the use of the “ignore”state to keep output 0 switched on.

LEDs connected to outputs 0,1 and 2 are on.

All LEDs are switched off.

Fig 2.2. Timed sequence, showing how the Output port isset to switch selected outputs on and off.

7 6 5 4 3 2 1 0

7 6 5 4 3 2 1 0

7 6 5 4 3 2 1 0

7 6 5 4 3 2 1 0

Page 15: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

OUT commandWhen flow passes through an OUTcommand, the output port is set to thebinary value of the number entered in thecommand (see fig 2.4). So, for example, ifthe command is set to 255, then all theoutputs will be switched on.

Fig 2.4. The binary number equivalent of each bitin the output port.

If you are familiar with the binary system, thisis a convenient way of switchingcombinations of outputs on and off

Fig 2.5 shows how OUT commands can beused to produce the same sequence as thatshown in fig 2.2.

Fig 2.5. Using the OUT command.

SOUND commandUse the SOUND command to send a pulsedsignal to a piezo sounder connected to anoutput of a PIC microcontroller. You can usea sequence of SOUND commands to play atune.

Fig 2.6 SOUND command Cell Details box

Fig 2.6 shows the SOUND command CellDetails box. Use the “Note:” box to selectthe note you want the command to play. Fig2.7 shows the musical note equivalent ofeach one of the numbers 1 to 49 which youcan select in this box.

Use the “Time” box to select the length oftime (in seconds) that you want the note toplay for.

The piezo transducer will be connected to anoutput of the PIC microcontroller. Enter thenumber of that output in the “Pin” box. Makesure that the correct number is selected inevery SOUND command on the flowsheet.

Fig 2.7.

Musical note equivalent of SOUND numbers.

15

output 7 6 5 4 3 2 1 0

binary value 128 64 32 16 8 4 2 1

C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B

1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 17 18 19 20 21 22 23 2425 26 27 28 29 30 31 32 33 34 35 3637 38 39 40 41 42 43 44 45 46 47 4849

Page 16: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

MOTOR commandThe MOTOR command allows you to use justtwo outputs on a PIC microcontroller toswitch a motor forward, reverse or off.

Use its Cell Details box to set the motor ormotors to drive forward or reverse; or to stop.Remember that the direction in which a motorturns depends on which way current flowsthrough it, and therefore on the way it isconnected to power. For this reason, theterms “On” and “Back” indicate only that thedirections will be different; not the actualdirection in which motors in the project willturn.

Fig. 2.8. MOTOR command Cell Details box.

Motors are labelled A,B,C or D. Motor A isthe motor controlled by outputs 0 and 1 ofthe PIC microcontroller. Motor B is the motorcontrolled by outputs 2 and 3, and so on.See “Connecting Motors” (page 53).

NOTE: Outputs and MOTOR commands bothuse the same output lines to switch theoutputs of a PIC microcontroller. The defaultstate of both commands is such that they willautomatically switch off any outputs that arenot set to be on.

So, to aviod inadvertently switching off anoutput device, click the check boxes ofunused motors in a MOTOR command todisable them, and set unused outputs in aOutputs command to their “ignore” state.

Example

A steerable buggy is usually driven by twomotors, one powering each driving wheelwith a free-running jockey wheel to keep itstable. Fig 2.9 shows how a sequence ofMOTOR commands can be used to drive abuggy which has one motor connected tooutputs 0 and 1 (motor A) and the othermotor connected to outputs 2 and 3 (motor B)

Fig 2.9. The MOTOR commands have beengiven labels to show what they do. The tablebeside each one shows how its Cell Details havebeen set.

16

Back Off On

A O O OB O O O

Back Off On

A O O OB O O O

Back Off On

A O O OB O O O

Back Off On

A O O OB O O O

Page 17: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

SLEEP command

This command puts the PIC microcontrollerinto low power mode for a specified numberof seconds. You can use this command tosave battery power in your project.

All output devices will be left in their currentcondition, and signals from input devices willnot be responded to while the chip is insleep mode. The TIME variable will be resetto 0.

Use the Cell Details box to set the number ofseconds of sleep mode you require. Thiscan be any number between 1 and 255. Asetting of 255 would give a sleep time of4.25 minutes. Note that SLEEP times are notquite as accurate as WAIT times.

SEROUT command

This command allows you to outputinformation from the PIC microcontroller to adevice such as a serial printer or a serialLCD screen which is connected to an outputof a PIC microcontroller. The information issent serially at baud rate 2400.

The SEROUT Cell Details box contains twoboxes. Use the first box to enter a number.This number will be the ASCII codeequivalent of the single number, letter orcharacter that you want to output.Remember to use the second box labelled“Pin” to enter the number of the output towhich the device is connected.

Fig 2.10 shows a table of ASCII codeequivalents of the most commonly usedcharacters. Some numbers may be used ascode for specific purposes such as “clearscreen”, by the device you are using.Consult the device’s data sheet for this kindof information.

Fig 2.10. ASCII code equivalents of the mostcommonly used characters.

Example

The flowsheet shown in fig 2.11 will displaythe word HELP on an LCD screen connectedto an output pin of a PIC microcontroller.

Fig 2.11. A sequence to display the word HELP.

17

3233 !34 ‘’35 #36 $37 %38 &39 ’40 (41 )42 *43 +44 ,45 _46 .47 /48 049 150 2

51 352 453 554 655 756 857 958 :59 ;60 <61 =62 >63 ?64 @65 A66 B67 C68 D69 E

70 F71 G72 H73 I74 J75 K76 L77 M78 N79 080 P81 Q82 R83 S84 T85 U86 V87 W88 X

89 Y90 Z91 [92 \93 ]94 ^95 -96 `97 a98 b99 c100 d101 e102 f103 g104 h105 i106 j107 k

108 l109 m110 n111 o112 p113 q114 r115 s116 t117 u118 v119 w120 x121 y122 z123 124 :125

Page 18: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Input devices such as switches and sensorssend information from the outside world intothe control system. Output devices areswitched on or off in response to theinformation provided by input devices.

Example one

A buggy is often fitted with microswitches sothat if it approaches an obstacle, amicroswitch will be pressed.

The information that the switch has beenpressed can be used in the system to switchoff the motors driving the buggy, and start asequence of movements to move around theobstacle.

A microswitch is a digital sensor. It has onlytwo states - “on” (or “closed”) and “off” (or“open”). These states are often labelled bythe digits 1 and 0, which is why the sensorsare called digital sensors.

Example two

A controlled hot water system includes atemperature sensor which constantlymonitors the water temperature. The waterheater is switched on and off in response tothe information provided by the sensor. If thewater temperature falls below a set level, theheater is switched on until it reaches thatlevel again. Then the heater is switched off.

A temperature sensor is an analogue sensor.It provides a reading which changes in linewith the changing level of whatever it issensing.

3. Inputs

18

Out

In

ControlSystem

Water Tank

Heater

TemperatureSensor

Temperature

Sensor Reading

No obstacles - sensor off

Sensors

Obstacle - sensor on

Page 19: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Digital InputsDigital Decision commandUse this command to test the state of adigital sensor connected to a digital input ofa PIC microcontroller.

When flow reaches a Decision cell, itcontinues in either the “Y” (yes) or “N” (no)direction depending on the result of thedecision test. See fig 3.1.

Fig 3.1. This Digital Decision command is testingthe state of a microswitch. If the switch ispressed, flow will go in the Y route; if it is notpressed, flow will go in the N route.

The Cell Details box of the Digital Decisioncommand is shown in fig 3.2. The Input Portline shows the number of digital inputsavailable for use on the PIC microcontrolleryou have selected.

Fig 3.2. Digital Decision Cell Details box for a16F84 chip (five digital inputs).

Each one of the digits in the Input Portrepresents one of the digital inputs on thePIC microcontroller. You can click each digitto set it to one of three states:

This means: is this sensor “on” (1)?

This means: is this sensor “off” (0)?

This means: ignore this sensor.

When you have done this, click OK.

Drawing routes from a Decision commandThe first line that you draw from a Decisioncommand is the “Y” direction, and thesecond line is the “N” direction.

You can swap the “Y” and “N” routes bydouble-clicking on the Decision commandwith the right mouse button.

Example one

A PIC microcontroller is being used to controla security system. A buzzer is connected toone of the outputs. A pressure pad isconnected to input 0, and a push switch isconnected to input 1.

Fig 3.3 is a flowsheet for the control system,showing how the two Decision commandsare set. When the chip is powered, thepressure pad is tested. If it is not pressed,flow will go in the N route and will continue togo round this loop until the pad is pressed.When the pad is pressed, flow will go in theY route and the buzzer will be switched on.The buzzer will stay on until the push switchis pressed. When it is pressed, the buzzerwill switch off and flow will return to testingthe pressure pad.

7 6 5 4 3 2 1 0Is the pressure padpressed?

7 6 5 4 3 2 1 0Is the push switchpressed?

Fig 3.3. Security system.

19

1

0

• • • • 1

• • • 1 •

Page 20: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

A similar flowsheet could be used to control asecurity system for a drawer. In this case, thesensor could be a micro-switch which is keptclosed (on) as long as the drawer is shut. Ifsomeone opens the drawer, the microswitchwill be open (off).

Fig 3.4 shows two different ways of using aDecision command to test the micro-switch inthis system.

7 6 5 4 3 2 1 0

Is the sensor off? (drawer open?)

7 6 5 4 3 2 1 0

Is the sensor on?(drawer shut?)

Example two

Home security systems often have a numberof sensors in different parts of the house. Ifany one of them is activated, the alarm isswitched on. Fig. 3.5 shows a securitysystem which has three sensors and a resetswitch.

Fig 3.5. Security system with three sensors (OR

function).

Two of the sensors are the magnetic type forwindows which have the magnet fixed to thewindow frame and the reed switch fixed tothe window. As long as the window is shut,the magnet keeps the reed switch contactsclosed (“sensor on”). When the window isopened and the magnet is moved away fromthe switch, the contacts are open (“sensoroff”). Therefore, the two Decision commandshave been set to go in the Y route if thesensor is off (0).

The system shown in fig 3.5 is an ORfunction. Some security systems have twoseparate reset switches arranged in an ANDfunction so that the system is reset only ifboth switches are pressed together. Fig 3.6shows how you can set a Decision commandto test two switches in this way.

7 6 5 4 3 2 1 0

Fig 3.6. Decision command set to check if two switchesare pressed at the same time (AND function).

20

• • • • 0

• • • • 1

Fig 3.4. Notice that the direction of flow depends on howthe command is set.

1 1 • • •

Page 21: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Example three

In the flowsheet shown in fig 3.7, the outputis switched on when a push switch ispressed. When you stop pressing the switchthe output switches off. In other words: IF the input is on, THEN switch the output on,ELSE switch the output off.

Fig 3.7 “Normally open” switch effect.

This is the equivalent of a simple electricalcircuit containing a normally open pushswitch and an output device. The bigdifference is that you can change the waythe system works in software, by simplychanging over the Y and N on the Decisioncommand as shown in fig 3.8.

Fig 3.8 “Normally closed” switch effect.

Example four

A monostable device has only one stablestate. It changes state when it is triggeredby an input, and stays in that state for acertain time. It then goes back to its originalstate. Fig 3.9 shows how this function canbe produced in PIC-Logicator.

Example five

A bistable device has two stable states. Itchanges state when it is triggered (set) by aninput, and stays in that state until it istriggered (reset) by a second input. It thengoes back to its original state. Fig 3.10shows how this function can be produced inPIC-Logicator.

Fig 3.11 shows how you can use just oneswitch for both set and reset. In this casethe Decision commands are used in pairs.The first one checks to see if the switch ispressed, and the second one checks for it tobe unpressed before the output is switched.The program is processed so fast that, if youdidn’t include this feature, it would switch theoutput and start checking the switch againwhile you were still pressing it for the firsttime.

Fig 3.11 “Bistable”function using one switchfor both set and reset

21

Fig 3.9 “Monostable”

function.

Fig 3.10 “Bistable”function using twoswitches for set andreset.

Page 22: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Events and Interrupts Event and Interrupt commands instantlycapture the flow of control whenever a pre-set digital input condition occurs to trigger it,e.g. when a switch is pressed.

EventsWhen an Event is triggered, flow jumpsimmediately to the Event command. Thecommands following the Event command arecarried out and flow is routed back into themain routine at some chosen point.

The following activity is designed to show thedifference between using a Digital Decisioncommand and an Event command. Makesure that you are familiar with test running aflowsheet (see page 12) before you begin.

1. Build the flowsheet shown in fig 3.12. Set the first Outputs command to switch on aselection of outputs. Use the second one toswitch them off.

Fig. 3.12

This system is designed

to start flashing the

outputs on and off as

soon as you press

function key F8 (input 1),

and go on flashing them

until you press function

key F9 (input 0) when it

will stop flashing and wait

for key F8 to be pressed

again.

2. Run the flowsheet. When it is running,press function key F8 to start the outputsflashing. While they are flashing, pressfunction key F9 and release it immediately.You will find that the flowsheet responds onlyif the key is pressed when flow is actually onthe Decision command. If it is anywhereelse, it will not respond.

3. Now edit your flowsheet to look like fig3.13. Use the information below to help you.

Drag an Event command onto the flowsheet.Double click on it to open its Cell Details box.It has the same Input Port line as a DigitalDecision command. Set it to respond tofunction key F9 (input 0). Click OK. Thecommand will automatically label itself 1: Event.

An Event will be triggered only if it has beenenabled by an EVENT command.

Drag an EVENT command onto the flow-sheet. Double click on it to open its CellDetails box.

Select number 1 from the drop-down listcalled “Number”. Select ON from the drop-down list called “State”. Click OK.

When an Event is triggered, it must be re-enabled before it can be triggered again.So, flow is routed back through the EVENT 1,ON command to re-enable the Event.

Test run the flowsheet. This time it shouldrespond immediately, whenever you pressfunction key F9.

22

Fig. 3.13

Using the EVENT

Command

Page 23: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

InterruptsInterrupts work in a similar way to Events.The difference between them is that when theInterrupt is triggered, flow jumpsimmediately to the Interrupt command andthen carries out any commands which followuntil it reaches an END command. It thenreturns to the point which it was at when theInterrupt occurred.

The following activity illustrates the use ofInterrupts. Make sure that you are familiarwith test running a flowsheet (see page 12)before you begin.

1. Build the flowsheet shown in fig 3.14.

Use the following information to help you.

The Cell Details box of an Interrupt commandhas the same Input Port line as a DigitalDecision or an Event command. Set eachone of the three Interrupt commands on theflowsheet to respond to a different functionkey. Note that Interrupts automaticallynumber themselves from 5 to 8.

Set the Outputs commands in the mainroutine so that all the outputs flash on and offevery two seconds.

Set the Outputs command in Interrupt 5 toswitch off all outputs.

Set the Outputs commands in Interrupt 6 toflash output 7.

Set the Outputs commands in Interrupt 7 toflash ouput 0.

Unlike Events, Interrupts have to be enabledonly once. This is done with an INTERRUPTcommand. Drag an INTERRUPT commandonto the flowsheet. Double click on it toopen its Cell Details box.

Select ALL from the drop-down list called“Number”. Select ON from the drop-down listcalled “State”. Click OK.

2. Test run the flowsheet. Press each one ofthe three function keys separately as theflowsheet runs, and watch the result in theOutput line in the Digital Panel. Also followthe flow of control on the screen as theflowsheet runs to see how an Interruptcaptures the flow. If you want to slow downthe speed at which flow is highlighted, selectthe Options>Speed... menu.

You can use up to a maximum of four Eventsand four Interrupts in one PIC-Logicatorflowsheet. Events are numbered 1 to 4;Interrupts are numbered 5 to 8.

Further examples of using Events andInterrupts can be found on pages: 27, 33, 36.

23

Fig. 3.14

Using the INTERRUPT command

Page 24: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Analogue InputsIf you want to use analogue sensors in aproject, you must use a PIC microcontrollerthat has analogue inputs (see page 46).

Calibrating sensorsAll analogue sensors connected to a PICmicrocontroller provide information on theconditions they are sensing, simply as anumber between 0 and 255. So, when youhave made a sensor as shown on page 56,you will need to calibrate it.

For example, if you are using a temperaturesensor, you will need to know the equivalentin degrees centigrade of the numbers (0 to255) that it provides. If you are using a lightsensor in a system in which output devicesare switched on and off at different lightlevels, you will need to know the number thatthe sensor provides when it is actually placedin each one of those light levels.

The PIC-Logicator Analogue CalibrationBoard allows you to calibrate your sensors inthis way. Connect the sensor to the board asshown in fig 3.15, and make a note of thenumber between 0 and 255 that is displayedwhen the sensor is placed in differentconditions.

Fig 3.15 PIC-Logicator Analogue CalibrationBoard with light sensor connected.

Compare Decision command

Use this command to check the reading froman analogue sensor connected to an analogueinput of a PIC microcontroller.

The most common use of an analogue sensorin a control system is to switch output deviceson or off when the reading from the sensorreaches a particular level. This level issometimes called the “threshold”.

When flow reaches a Compare Decision cell,the software checks the current reading fromthe specified sensor, and compares it withthe threshold that you have set. Flow willcontinue in either the “Y” (yes) or “N” (no)direction depending on the result of thecomparison.

The Cell Details box of the Compare Decisioncommand is shown in fig 3.16.

Fig 3.16. Cell Details box of the CompareDecision command.

1. Use box one to select the sensor that youwant the command to check. Analoguesensors are labelled A0 to A3 according towhich pin on the chip they are connected to.Type in the number of the sensor you wantthe command to check, or select it from thedrop-down box.

2. Use boxes two and three to complete thecomparison. The drop-down list in box twocontains a list of operators such as “greaterthan” (>), “less than” (<), and “equals” (=).Select the one that you require.

NOTE: It is usually better to use an operatorsuch as “greater than or equals” (>=) insteadof “equals”, because analogue sensorreadings can fluctuate rapidly, and you mayfind that the checking of the sensor readingnever actually coincides with the exactthreshold level.

Use box three to set the threshold level.Type in a number between 0 and 255, orselect it from the drop-down list.

24

Page 25: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Example one

A PIC microcontroller is being used to controla lamp. A light sensor is connected toanalogue input 0. The system will switch onthe lamp automatically in dark conditions. Fig3.17 shows a flowsheet for the system.

Fig 3.17 System to switch on a lamp automatically

in dark conditions.

The Compare Decision command ischecking the reading from the light sensor. Ifthe reading is less than or equal to 50, flowwill go in the Y route and switch on the lamp;if the reading is greater than 50, flow will goin the N route and switch off the lamp.

The system could be extended as shown infig 3.18. This system controls three separatelamps. It automatically switches them on oneby one as darkness falls, and switches themoff in the same way as conditions growlighter.

Fig 3.18. System to switch on three lamps inresponse to changing light levels.

Example two

A PIC microcontroller is used to make a lightmeter for use by cricket or tennis umpires todecide when to abandon play because ofbad light. A light sensor is connected toanalogue input 0. An LED is connected toeach one of the eight outputs. In brightsunlight, all the LEDs will be lit. As the lightlevel falls, the LEDs will switch off one byone. FIg 3.19 shows the flowsheet for thesystem. Notice the use of the OUTcommand to switch on combinations ofoutputs.

Fig 3.19. Light meter system.

25

Page 26: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

PIC-Logicator software provides a clear,step-by-step method of building a complexcontrol system, by creating a number oflinked sub-systems called “macros”.

How to build a macro

1. Use a Macro command to begin themacro. Drag the command onto the flow-sheet and place it separately from the STARTcommand as shown in fig 4.1. Double clickon the command to open its Cell Details box.Type in any appropriate name, and click OK.The software automatically puts the nameinto capitals.

Fig 4.1. Placing the Macro command.

2. Use other commands as normal to createthe macro. Place an END command at theend of the macro as shown in fig 4.2.

This command does not have a Cell Detailsbox; simply place it on the flowsheet.

Fig 4.2. This macro, called FLASH will switch onselected lamps for 3 seconds and then switchthem off.

3. When you have created a macro, you cantest run it. Click on the Macro command toselect it, and click Run!

How to use a macroOnce you have built a macro, you can call itinto use whenever you like in the flowsheetby using the Do Macro command, as shownin fig 4.3.

Fig 4.3. The Do Macro command calls the macrointo use.

Drag a Do Macro command onto theflowsheet. Place it at the point where youwant the macro to be called into use. Doubleclick on the command to open its Cell Detailsbox. Type in the name of the macro or selectit from the drop-down list. Click OK.

Note that all the macros that have been built ina flowsheet are automatically listed in thedrop-down box.

When flow reaches a Do Macro command, itjumps to the Macro command with the samename. When the flow of control reaches anEND command, the flow jumps back to the DoMacro command that called the macro.

Notice that after a macro has been test run,the Macro command is highlighted so that itcan be run again if necessary. To test run thewhole flowsheet, click on the STARTcommand to highlight it, and click Run!

4. Macros

26

Page 27: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Example one

A PIC microcontroller is used to to control asystem in a child’s toy which plays a tunewhen it is hugged. A piezo transducer isconnected to an output pin, and a pushswitch is used to sense when the toy ishugged. The flowsheet for the system isshown in fig 4.4. The tune is created as amacro which can be tested and editedseparately from the main routine.

Fig 4.4. This is also a good example of the use ofEvent and SOUND commands.

Example two

The flowsheet shown in fig 4.5 is a controlsystem for a sliding door. When a switch ispressed, the door opens. It stays open forten seconds and then closes again. Thesystem uses limit switches to sense when thedoor is fully open and fully closed. The motoris halted in response to the feedback fromthese microswitches.

Fig 4.5. Sliding door control system usingmacros.

27

Page 28: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Example three

A keypad is a useful input device. Thisexample shows how the PIC-Logicatorsoftware can be used to scan a keypad in aproject in which a three digit number has tobe entered to open a solenoid-operated lock.

Connect the keypad to a PIC microcontrollerusing inputs and outputs as shown in fig. 4.6.The flowsheet in fig. 4.7 shows how thescanning is done.

In this case, the code number uses a digitfrom each one of the first three rows (e.g.357 or 268). Each row is scanned in turnusing a macro.

To begin with, the row is made “live” byswitching on the output to which it isconnected. Then a Digital Decisioncommand checks for the appropriate key inthat row to be pressed, by testing for thatinput to be on. When the correct key ispressed, flow passes on to the next macro.When all three digits have been enteredcorrectly, the solenoid is switched to unlockthe door.

Fig 4.7. Flowsheet to scan the keypad.

Fig 4.6. Keypad connections.

28

1 2 3

4 5 6

7 8 9

* 0 #

output 0

output 1

output 2

output 3

input 0 input 1 input 2

Page 29: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Designing systemswith macrosUsing macros, you can design and testsystems either “top-down” or “bottom-up”

Example one

The “top-down” approach.

This approach begins with an overall view ofthe system (the main routine), and thencreates each part of it separately as a macro.The following sequence shows how it can beused to develop a control system for a buggywhich is fitted with microswitches that arepressed if the buggy comes into contact withan obstacle. When this happens, the buggysounds an alarm and moves round theobstacle.

1. The main routine is created as a series ofDo Macro commands as shown in fig 4.8.

2. Then each part of the system is built as aseparate macro as shown in fig 4.9. Eachmacro can be test run independently.

Fig 4.8. Main routine.

Fig 4.9. Notice that theAVOID macro uses the top-down approach, so theflowsheet is incomplete atthis stage. Fig 4.10 on page30 shows the completeflowsheet.

29

Page 30: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

3. The AVOID macro shown in fig 4.9 hasbeen built by using the top-down approach.To clarify the avoiding procedure, eachmovement is simply listed as a Do Macrocommand. Then the details required for thebuggy to make each movement can be dealtwith separately as shown in fig 4.10.

Fig 4.10. This flowsheetillustrates the way in whichmacros may be called fromwithin other macrodefinitions.

30

Page 31: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Example two

The “bottom-up” approach.

This approach develops each part of thesystem separately as a macro, and thenwrites the main routine to link them. Thefollowing sequence shows how it can beused to develop a control system for ananimated clown’s head on which the eyesand nose light up and the hat rotates.

1. A separate macro is built and tested foreach one of the three elements, as shown infig 4.11.

2. A main routine is then written to call themacros into use in the required sequencewhenever a switch is pressed (Fig 4.12).

This flowsheet shows some of theadvantages of using this approach. Once amacro has been created, it can be called intouse as many times as you like within theflowsheet. Editing the sequence is easy.

The Do Macro commands can be movedaround, deleted or copied to change thesequence as required. Macros can be cut,copied and pasted between flowsheets.Remember that copied commands will retaintheir existing cell details.

31

Fig 4.11. In thisapproach, the macrosare created first.

Fig 4.12. The complete system.

Page 32: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

In PIC-Logicator a variable is a single letter ora keyword that can be given a value. Thevariables that can be used are: any one of thesingle letters A to H, and the keywords TIMEand PORT. This section explains how theycan be used for a variety of mainly countingand timing purposes.

Counting The INC commandEach time flow passes through an INCcommand, one is added to the value of aselected variable (INC is short for increment).When you open the Cell Details box, simplyselect which variable you want to use, andclick OK.

The flowsheet shown in fig 5.1 shows how itcan be used to repeat a sequence threetimes.

Each time that flow goes round the loop, theFLASH macro is done, and one is added tothe value of variable A.

A Compare Decision is used to check thevalue of A. When this value reaches 3, flowwill go in the Y direction and stop theflowsheet.

Fig 5.2. Cell

Details box of

the Compare

Decision

command.

1. Use box one to select the variable that youwant the command to check.

2. Use boxes two and three to complete thecomparison. The drop-down list in box twocontains a list of operators such as “greaterthan” (>), “less than” (<), and “equals” (=).Select the one that you require.

Use box three to set the number of times thesequence will repeat. Type in a numberbetween 0 and 255, or select it from the drop-down list.

Another use of the INC command is to countthe number of times something happens - thenumber of people passing through a gate orturnstile for example. This is often done byusing a digital sensor such as a micro switchor a reed switch placed so that the sensor is“on” when a person passes. Fig 5.3 shows thethree commands needed to do this. Noticethat two Digital Decision commands are usedto check the switch. The first commandresponds when the sensor is on. Then thesensor is immediately checked again to seethat it is off before anything else happens.This ensures a clean signal for the INCcommand to count.

5. Variables

32

Fig 5.1. Repeating a sequence three times.Fig 5.3. Ensuring aclean signal from adigital sensor.

Page 33: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

You may well find that once it is downloadedinto the chip, the flowsheet runs so quicklythat even using the two Digital Decisioncommands does not give a clean count. Ifthis is the case, you should include a shortWAIT before the INC command, as shown infig. 5.4. This flowsheet is for a system tocount the number of people passing througha turnstile and to display the number in binaryform, using LEDs connected to each one ofthe eight outputs on a PIC microcontroller.

Example one

A PIC microcontroller is used to control asystem for counting cars entering andleaving a car park using two digital sensors.The outputs of the system are a red lamplighting a “Full” sign, and a green lamplighting a “Spaces” sign. The flowsheet forthe system is shown in fig 5.5. When youtest run this flowsheet, display the Variableswindow to see the changing value of A.

This system uses the DEC command whichworks in a very similar way to the INCcommand. The difference is that when flowpasses through a DEC command, one issubtracted from the selected variable.

33

Fig 5.5. Car park counting system

Fig 5.4.

Flowsheet for making and displaying a count.

Page 34: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Example two

A seven-segment display is a useful outputdevice for displaying counting and timing.The flowsheet in fig. 5.6 is designed tocontrol the kind of supermarket delicatessen-counter system in which customers take aticket and then wait for their turn to be servedwhen their number is displayed. When theassistant has served a customer, he or shepresses a switch to display the next number.

The main routine uses an INC command toincrement (add one to) the value of thevariable A each time the assistant pressesthe switch. The DISPLAY macro makes anefficient way of translating the current valueof A into an Outputs command which is setto switch on the appropriate number ofoutputs to display the number.

A similar approach could be used with anLCD screen. In this case, the DISPLAYmacro would use a series of SEROUTcommands as shown in fig. 5.7.

Fig 5.7. Part of an equivalent system that uses anLCD screen to display numbers.

Fig 5.6. A “Now Serving....” display system.

34

Page 35: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

TimingThe variable TIME allows you to repeat asequence for a set period of time. Theflowsheet shown in fig 5.8 shows how it canbe used to repeat a sequence for 10seconds.

A Compare Decision is used to check thevalue of TIME. When this value reaches 10,flow will go in the Y direction and stop theflowsheet. Use the three boxes in theCompare Decision Cell Details box to enterthe expression TIME > 10.

The maximum value that can be entered inthe variable TIME is 255 (seconds).

Setting the value of avariable1: the Expression command

The Expression command is used to give avalue to a variable as a flowsheet runs. Thevariable is given its value as flow passesthrough the command. The followingexamples show how it can be used.

Example one

The TIME variable works like a timer. It is resetto zero automatically at the start of a flowsheetrun, and then continues to count up inseconds until the flowsheet stops.

There may be times when you want to resetthe timer to zero during the flowsheet run. Thesystem shown in fig 5.9 is designed to flashlights for 8 seconds and then play a tune for 6seconds. If will do this only if the value ofTIME is reset to zero as shown. Use the firsttwo boxes in the Expression Cell Details boxto enter the expression TIME = 0.

35

Fig 5.9. Two consecutive timed sequences.

Fig 5.8.

Repeating a sequence for 10 seconds.

Page 36: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Example two

A container in a warehouse is designed tohold ten packs of components. A system isneeded to indicate the changing contents ofthe container as packs are removed. Theflowsheet shown in fig 5.10 is designed to dothis.

A digital sensor is used to indicate each timea pack is removed (notice the use of twoDigital Decision commands to ensure a cleancount). The number of packs in thecontainer is displayed as a binary countusing 8 LEDs connected to outputs of thePIC microcontroller.

The DEC command counts down, so anExpression command is used to set the valueof variable A to 10 at the start of thecountdown when the container is full. TheExpression command Cell Details box isshown in fig. 5.11. Use the first two boxes toenter the expression A = 10.

Mathematical expressionsA value can also be given to a variable in theform of a mathematical expression as shownin the flowsheet in fig 5.12. This systemcounts the number of times that two separateswitches are pressed, and displays thecombined total. Use all four boxes in theExpression Cell Details box to enter theexpression C=A + B.

NOTE: the third box in the Expression CellDetails box contains a range of mathematicaloperators. These include SHL and SHRwhich are used to multiply and divide thevalues of variables in a limited way:

B = A SHR 1 means divide the value of A by2, and put the result into variable B. SHR 2means divide the value of A by 4, and so on.Use SHL in the same way to multiply thevalue of a variable.

36

Fig 5.10. Counting down.

Fig 5.12. Displaying a combined count.

Fig 5.11. Expression command Cell Details box

Page 37: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Setting the value of avariable 2: IN,RND and PORT

The IN command sets the value of aspecified variable to the current binary valueof the input port. So, for example, if switchesconnected to inputs 0 and 1 are pressed,then the value of the variable will be 3.

The flowsheet in fig 5.13 shows how this canbe used to make a simple security system.

When switches connected to inputs 0 and 2are pressed at the same time the binaryvalue of the input port equals 5 (4+1), flowfrom the Decision command goes in the Ydirection and a solenoid-operated lock isopened. If any other combination ofswitches is pressed, flow goes in the Ndirection.

The variable PORT can be used in a similarway to the IN command. The flowsheet in fig5.14 serves the same purpose as that shownin fig 5.13. The Expression commandA=PORT puts the binary value of the inputport into the variable A.

Note that It is possible to transfer the binaryvalue of the input port directly to the outputport by using an Expression command PORT = PORT.

The RND commandEach time flow passes through thiscommand, the selected variable is given arandom value between 0 and 255. In theexample shown in fig 5.15, a set of displaylights for a small christmas tree areconnected to 8 outputs of a PICmicrocontroller. Every second the display willchange at random.

37

Fig. 5.13. Simple security system that responds to pressingtwo switches.

Fig 5.15. Using the RND command to create a randomdisplay of lights.

Fig 5.14 Using the variable PORT.

Page 38: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

READ and WRITE

When a flowsheet run is started, all variablevalues automatically reset to zero. So,whenthe PIC microcontroller is reset or poweredup, all variable values are reset to zero.

If you want to retain variable values when thePIC microcontroller is powered up or reset,you can use the WRITE command to storevalues in the chip’s EEPROM memory. TheREAD command is used to retrieve thevalues from the chip’s memory.

The flowsheet in fig. 5.16 shows an exampleof how the commands can be used. Thefollowing information explains how this works.

1. The READ command takes the valuewhich is currently stored in a selectedaddress (in this case address 0), and puts itinto the selected variable (in this casevariable A). Use the READ command CellDetails box (fig 5.17) to enter the variableand the address from which the value is tobe read.

Fig 5.17. READ command Cell Details box.

The PIC microcontroller’s EEPROM memoryhas 16 separate addresses. Each one canstore a number between 0 and 255. TheEEPROM window displays the contents of thememory when you test run a flowsheet.

Fig 5.18

EEPROM window.

2. The OUT A command in the flowsheetdisplays the current value of A using 8 LEDsconnected to outputs of the PICmicrocontroller.

3. The INC A command increments (addsone to) the value of A each time a switch ispressed.

4. The new value of A is immediately storedin address 0 of the EEPROM memory by theWRITE command. The Cell Details box ofthis command is used in the same way as forthe READ command.

When the PIC microcontroller is powereddown, the value of A is stored in the chip’smemory. When it is powered up, the firstthing that happens is that the READ A,0command retrieves the value of A which hasbeen stored in address 0.

The EEPROM window gives an accuratesimulation of the way these commands workwhen the flowsheet is downloaded. Anyvalues shown will be saved when you savethe flowsheet. They will also be downloadedwith the flowsheet.

Therefore, when you have test run theflowsheet, make sure that you edit the valuesshown in this window back to zero before youdownload the flowsheet - unless, of course,you want to download the current values forsome reason.

38

Fig 5.16. Using READ and WRITEcommands tostore a count.

Page 39: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Section Two:

Connecting to thePIC

Microcontroller

Page 40: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

40

Page 41: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Note: definitions of terms used in connectionwith PICs are in the “Definitions” section onpage 44.

A PIC microcontroller is a single chip thatcan be programmed to switch output deviceson and off in sequences and in response toinput from sensors.

A microcontroller contains all the elements ofa microprocessor system. This system,which is the basis of a computer, consists ofa number of separate elements: CPU, RAM,ROM and I/O. In a microprocessor system,each of these elements will be in the form ofone or more individual chips. However, thePIC microcontroller combines all of theseelements in just one chip, that processesinstructions as well as controlling devices.

Fig 1.1 The difference between a PICmicrocontroller chip and a microprocessorsystem.

A PIC microcontroller is a programmabledevice, which means that it is able to storesets of instructions in the form of a program,and carry out the instructions whenever theprogram is run. The code that the chip usesinternally to do this, is called “machinecode”. It is written in hexadecimal and isdifficult for anyone other than a trainedprogrammer to use. So, a programminglanguage is used to allow designers tocreate, edit and download instructions to thechip. The machine code is generatedautomatically from the programminglanguage.

Programming languagesPIC programming languages come in manydifferent formats. High level languages suchas PIC-Logicator and BASIC require verylittle knowledge of how PICs work and usecommands that are easy to understandbecause they tend to use recognisable,everyday English words. The disadvantageof high level languages is that programsrequire more memory space and run moreslowly than low level ones.

Low level languages (for example, assemblercode) require a much deeper level ofunderstanding of how the chips actuallywork. Their commands are complex andsometimes obscurely titled. The lower thelevel of the language, the nearer it is to themachine code and the harder it is tounderstand. On the other hand, the benefitof using a low level language is that thefinished code is normally very concise andtherefore runs very efficiently and faster thana higher level language can.

The panel on page 42 shows how the samesimple program would appear in machinecode and in three different programminglanguages.

1. PIC Microcontrollers

41

CPU

RAM

I/O

ROM

CPU

RAM ROM

I/O

Microprocessor System

PICmicro

Page 42: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Code examplesThese sections of code simply blink an LED connected to output 4 once a second.

Assembler Code Example (.ASM)

PBCX equ 1

include

_loop movlw 004hcall high@movlw 001hmovwf R0+1movlw 0F4hcall pause@XWmovlw 004hcall low@movlw 001hmovwf R0+1movlw 0F4hcall pause@XW@GOTO _loopcall end@

Machine Code Example (.HEX)

:10000000031E080086015F308C000D3084008001E3:10001000840A8C0B07282C282120840007398406A9:100020000310840C840C840C041A841704128A01B3:10003000820701340234043408341034203440344C:10004000803407398A018207303431343234333412:100050003434353436343734043041200130A30091:10006000F430482004303D200130A300F430482013:100070002C283A20073055203A280C20FF3A8005DA:1000800045280C2080044428FF3A84178005632803:10009000A2005D2064002308220403190800F73041:1000A000FF3E031D50285F204A2883160739F83881:1000B00081006300FF3081006328A309A209A20A1E:0C00C0000319A30A080083126400080062:084000007F007F007F007F00BC:04400E00F53F010079:00000001FF

BASIC Example (.BAS)

loop: High4Pause 500

low 4Pause 500

Goto loopEnd

42

Different types of PICPIC chips are available in many differentsizes to suit different applications. Chips insizes from 8 pins to 40 pins are available toprovide different numbers of outputs andinputs. Some provide digital inputs only.Some have ADC (analogue to digitalconversion) built into them so that analoguesensors can be connected directly to them.Obviously the larger, higher specificationPICs are more expensive, so it is importantfor designers to select the most appropriatechips for their purposes.

PICs also vary in terms of how they areprogrammed and erased. Some PICs areerasable by UV, some electrically and somenot at all. UV light erasable chips are notwidely used because they need a special UVlight source to erase them and because ofthe health and safety problems associatedwith using UV light. Most PIC chips areFLASH reprogrammable which allows themto be overwritten electrically as many timesas required. These chips have an “F” in theirreference number. The third type of chip iscalled OTP (one time programmable) and, asthe name suggests, can only beprogrammed once. These chips have a “C”in their reference number.

PIC-Logicator Example (.PFL)

Page 43: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

ApplicationsMillions of PIC chips are used each year in:electronic consumer goods, mobiletelephones, medical equipment, computerproducts, and industrial applications. Therewill be, on average, 35 programmable chipsin every car, used in a range of sub-systemsfrom engine management to remote locking.

Up to 225 chips can be found in every home,where a typical use would be in a securitysystem in which a PIC might be used tomonitor the sensors placed around the houseand switch an alarm; as well as enablingfeatures such as the use of a code, input bya keypad, to set and reset the system. Amicrowave oven may use a single PIC toprocess information from the keypad, displayuser information on a seven segment display,and control the output devices (turntablemotor, light, bell, magnetron).

The complex control system for an automaticwashing machine is likely to use PICs. Thesystem takes in information from devicesused to select different washing programs,and also from sensors that monitortemperature and water levels. It isprogrammed to make use of this informationand switch output devices such as motors,pumps and heaters on and off in appropriatetimed sequences.

Advantages anddisadvantagesOne PIC chip can replace a wide variety oftraditional discrete electronic componentssuch as transistors, logic circuits, 555 timerchips. This means that products based onPICs can be smaller and cheaper. They willhave fewer separate parts so they are likelyto be more reliable. The companymanufacturing the products will have reduced stock levels. Product-assembly willbe simpler and therefore quicker andcheaper.

Using PICs can make products more flexible.Their features are programmed into the chip,not built into electronic hardware, so they canbe developed and changed quickly andeasily. If the manufacturer of a productwants to change a feature of the product, asimple change to the PIC program canachieve what is required without the need toalter any of the components on the PCB.

The main disadvantage of PICs is that theyhave only a very low power output, of a fewmilliamps. They therefore require interfacingcircuitry to drive higher current loads.

Interfacing to a PICA PIC chip does not provide the completecontrol system. It should be seen as the“process” section of the system. It isprogrammed with instructions to useinformation from the devices in the “input”section, and switch on and off the devices inthe “output” section.

INPUT PROCESS OUTPUTPICmicro

Once the chip has been programmed it isuseless until it is interfaced to the real world.Interfacing involves providing power to thecircuit, and using standard interfacingcircuits for input and output devices. Theseinclude potential dividers for sensors, andtransistor drivers for output devices. Thissection of the book shows the basicinterfacing circuits that are likely to berequired for most school projects.

43

Page 44: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Definitions

PIC The letters PIC stand for“peripheral interface controller”.The full name of this type of chipis “PIC microcontroller”, but this isoften shortened to “PICmicro” orjust “PIC”.

CPU Central Processing Unit

ALU Arithmetic Logic UnitA separate processor inside thechip to specifically handlemathematical and logicoperations. This is more efficientthan having the main processorperform these operations.

RAM Random Access Memory Memory that can be accessed asrequired, with ability to write andread as necessary. Data storedin RAM is not held if the power tothe chip is turned off.

ROM Read Only Memory Memory that can only be readfrom and is programmed onlyonce.

I/O ports Input / Output portsConnections on the PIC chip thateither connect to input devicessuch as switches or outputdevices such as bulbs through anappropriate driver. The I/O portson PICs can be configured aseither inputs or outputs, but inPIC-Logicator these have beenpreset to simplify programming.

PICs commonly have two I/Oports referred to as PORTA andPORTB. Within each port thereare a number of bits (designatedRAx or RBx (where x is thenumber of the bit)) which are ineffect the physical pins on thePIC.

CLOCK All microprocessors operate at afixed speed which is referred toas speed or clock frequency andis quoted in Hertz (Hz). PICchips normally run at 4Mhz(Mega Hertz) and in some casesrequire an external resonator toregulate this speed. Some newertype PIC chips have theseresonators built in.

EEPROM Electronically ErasableProgrammable Read OnlyMemoryThis is memory in the chipssometimes called FLASH memorythat can be programmed withdata and read back. Thismemory is still stored even ifpower to the device is switchedoff and is what makes up most ofthe data memory in PIC chips.

ADC Analogue to Digital ConverterAnalogue sensors provideinformation in the form of varyingvoltages, usually between 0 and 5volts. Analogue to DigitalConverter chips sample thesevoltages at regular intervals andconvert them to a digitalinformation which is then sent outfrom the chip using serial datatransmission.

Some PICs contain the ADCfunction. For example, thePIC16F872 has 4 analogue inputswhich, after conversion, eachgive a digital value of between 0and 255. So, a temperaturesensor connected to the chipwould give a range of readings inwhich 0˚C (0 volts) is convertedto 0, and 100˚C (5 volts) isconverted to 255. So, there are2.5 ‘steps’ per degree change.The range is 0 to 255 because 8bits are used to make theconversion (255 decimal =11111111 binary). The higher thenumber of bits used for sampling,the higher the accuracy of theconversion.

44

Page 45: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

NOTE: When you select a chip from the Options>PIC Type menu in PIC-Logicator software, the softwareautomatically configures itself to display only the input, output and motor options available withthat chip. This is illustrated in the table beside each pin-out diagram.

18 pin PIC microcontrollers16F84, 16F84A

16F627

2. PIC Microcontroller Pin-out Diagrams

45

Input:

Output:

Motors:

7 6 5 4 3 2 1 0

0 00 0 0 0 0 0

0 0 0 0 0 0 0 0D C B A

This chip has aninternal resonator.

Page 46: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

16F628

28 pin PIC microcontrollers

16F872, 16F873, 16F876

46

1 28

14 15

Input:

Output:

Motors:

7 6 5 4 3 2 1 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0D C B A

A0

A1

A N A L O G U E

A2

A3

Input:

Output:

Motors:

7 6 5 4 3 2 1 0

0 00 0 0 0

0 0 0 0 0 0 0 0D C B A

A0

A1

A N A L O G U E

This chip has aninternal resonator.

Page 47: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Power The PIC microcontroller requires a 6V DC supply. This can be provided by 4 x AA cells. Thefollowing diagrams show connection to 18 and 28 pin chips. The 100nF polyester capacitor isused to decouple the PIC microcontroller power supply for reliable operation.

3. Basic Connections

47

100nFpolyester

6V

0V

100nFpolyester

6V

0V

Using a low-voltage powersupplyIn some situations it might be moreconvenient to power your project from a lowvoltage DC power supply, rather than abattery. If you wish to do this, you shoulduse a 7805 voltage regulator as shown in fig. 3.1.

Fig 3.1 Using a 7805 voltage regulator

Using two power suppliesIf your project includes an output devicewhich requires a voltage greater than the 6Vthat you are using to power the PICmicrocontroller circuit, use two separatepower supplies as shown in fig. 3.2. This ismore efficient and economical than using arelay.

Fig 3.2 Using separate power supplies to powerthe process and output sections of a project.

6V

0V

12V

10kΩoutputpin

outputpin

PIC

microcontroller

BCX38B

IN4001

IN4001

7V - 12V

DC

0V

100µF

+ +

7805

0V

100µF

6V

+

Input(7V-12V)

Output(6V)

Ground(0V)

outputdevice

Page 48: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Resonator The PIC microcontroller provides a clock pulse internally. The resonator regulates the speed ofthe clock pulse, or in other words, sets the speed at which the PIC microcontroller works (4MHz).Some PIC microcontrollers have an internal resonator. Others need to have a 4MHz ceramicresonator connected as shown in fig. 3.3.

ResetThis pin must also be connected to the 6V rail through a 4k7 resistor as shown in fig. 3.4. Youcan also include a reset switch as shown in fig. 3.5, if you like. When you press this switch, theflowsheet programmed into the chip will restart at the START command.

48

6V

0V

4MHzceramicresonator

6V

0V

4MHzceramicresonator

6V

0V

4.7kΩ

6V

0V

4.7kΩ

6V

0V

4.7kΩ6V

0V

4.7kΩ

Fig. 3.3. Connecting a resonator to 18 pin and 28 pin microcontrollers

Fig. 3.4. Connecting the reset pin

Fig. 3.5. Connecting the reset pin to include a reset switch

4Mhz ceramic resonator

Page 49: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Note that diagrams shown in this section show how to connect the output device only. Forsimplicity, the PIC microcontroller is simply shown as a block. In each case, the PICmicrocontroller must be connected to power as shown in “3. Basic Connections” (page 47).

The position of the output pins on the block diagram does not necessarily indicate the position ofan output pin on a PIC microcontroller chip. Consult the pin-out diagrams in “2. PICMicrocontroller Pin-out Diagrams” (page 45) to identify the correct pin to use.

A: Components that can be connected directly to an output pin

The following low current output devices can be switched directly by the PIC microcontroller.

A1. Light Emitting Diode (LED)An LED can be connected directly to anoutput pin of the PIC microcontroller, asshown in fig. 4.1. The resistor isrequired to limit the current through theLED.

Fig 4.1 Connection of an LED

A2. Seven Segment DisplayA seven segment display is made up of seven LED bars, which can be connected directly tooutput pins of the PIC microcontroller, using series resistors, as with individual LEDs.

The bars of the seven segment display can be lit in different combinations to show the ten digits0 to 9, as well as some letters of the alphabet.

A number of different types of seven segment display are available. You should use a commoncathode type. The common pin connects to 0V.

Fig 4.2 Connection of a sevensegment display. Each LED barhas an identifying letter. Consultthe pin-out diagram of the sevensegment display that you areusing, to identify the appropriatepins. This may be supplied withthe component or it may be inthe supplier’s catalogue.

4. Connecting Output Devices

49

0V

330Ωoutputpin

e

d

c

b

a

g

f

PIC

microcontroller

330Ω

0V

PIC

microcontroller

outputpin

Page 50: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

A3. Piezo SounderA piezo sounder can be used to produce a range of different sounds. The sound is created bya pulsed signal. This signal can be sent from any one of the output pins on the PICmicrocontroller. Use the SOUND command in PIC-Logicator software to select the sound andthe pin to be used. Because the software is generating the pulsed signal, use a piezo sounder(sometimes called a “piezo transducer”) without drive circuit.

Fig 4.3 Connection of a piezo sounder

A4. Counter ModuleA counter module is an LCD numerical display which can be used to show a changing countvalue. It is powered by its own internal 1.5V battery.

The counter is incremented by a pulse. Use two Outputs commands to create a pulse - one toswitch the output on; the other to switch it off.

Connect the counter module as shown in fig. 4.4. A potential divider, formed from two resistors,is used to reduce the PIC microcontroller output signal to the 1.5V required by the countermodule.

Fig 4.4 Connection of a counter module

50

0V

3.3kΩ

PIC

microcontroller

1kΩ

counter

1 3

outputpin

0V

PIC

microcontroller

outputpin

Page 51: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

B: Components that require a transistorswitching circuit

Higher current output devices cannot be switched directly by the PIC microcontroller chip. Theyrequire a transistor switching circuit. A device that is commonly used for this purpose is aBCX38B Darlington driver, which is actually two transistors in a single package. This device canswitch currents up to 800mA. If you want to switch higher currents than this, you should use aMOSFET as shown on page 52.

B1. Figs 4.5 and 4.6 show how a BCX38B Darlington driver is used to connect a signal lampand a buzzer to a PIC microcontroller.

Fig 4.5 Connection of a signal lamp

Fig 4.6 Connection of a buzzer

B2. Output devices such as relays, solenoids and solenoid valves create a back emf whenpower is switched off. When you are using one of these devices, it is essential that you connecta back emf suppression diode across the output device as shown in fig. 4.7.

Fig. 4.7 Connection of an output device such asa relay, solenoid or solenoid valve

51

6V

0V

10kΩoutputpinP

ICm

icrocontroller

BCX38B

6V

0V

10kΩoutputpin

BCX38B

PIC

microcontroller

IN4001

6V

0V

10kΩoutputpin

outputdevice

BCX38B

PIC

microcontroller

IN4001

Page 52: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

B3. If you need to connect a number of output devices to the PIC microcontroller, it will be moreconvenient to use a ULN2003A Darlington driver IC as shown in fig. 4.8. This chip containsseven Darlington transistors similar in value to the BCX38B. It also contains internal back emfsuppression diodes, so no external 1N4001 diodes are needed.

Fig. 4.8 Connection of a number of output devices using a ULN2003A Darlington driver IC

B4. The BCX38B can switch currents up to 800mA. If you want to switch higher currents thanthis, you should use a MOSFET as shown in fig. 4.9. The IRF530 is a suitable power MOSFET touse for this purpose. Note that when using two separate power supplies, it is necessary to linkthe 0V rails as shown.

Fig. 4.9 Standard MOSFET circuit

52

6V

0V

1kΩ

outputpin

PIC

microcontroller

1kΩ

1 16

8 9

ULN

2003A

12V

0V

outputpin

outputdevice

IRF530

PIC

microcontroller

6V

Page 53: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

C: Motors

C1. Low-voltage DC motors

The higher quality low voltage DC motors often called “solar motors” are recommended.

The PIC-Logicator system simplifies the control of motors by using the MOTOR command (seepage 16).

The dialog box of this command allows you to select the motor or motors you want to switch,and set them to drive forward or reverse.

Motors are labelled A,B,C or D. Motor A is the motor controlled by outputs 0 and 1 of the PICmicrocontroller. Motor B is the motor controlled by outputs 2 and 3, and so on. See the “PICMicrocontroller Pin-out Diagrams” section (page 45) for information on how this works with thedifferent chip options.

The simplest way to connect motors to the PIC microcontroller is to use a motor driver IC suchas the L293D. This allows you to control either one or two motors. Fig 4.10 shows how toconnect one motor. If you use the two PIC microcontroller outputs 0 and 1 as shown, then youwould use Motor A in the software MOTOR command to control it.

Fig 4.10 Using the L293D motor driver with a PIC microcontroller

NOTE: Pin 8 of the L293D should be connected to 6V if this is the appropriate voltage for themotor that you are using. If you are using a higher voltage motor e.g. 12V, then this pin shouldbe connected to a power supply of this voltage. Maximum current is 1A.

You could connect another motor to pins “Out 3” and “Out 4” of the L293D. It would becontrolled by connecting pins “In 3” and “In 4” to another pair of output pins on the PICmicrocontroller. If you were to use PIC microcontroller outputs 2 and 3, then you would useMotor B in the software MOTOR command to control it.

53

6V

0V

1kΩ

a pair ofoutput pinse.g. 0 and 1

1kΩ

1 16

8 9

Out 1

In 1 In 4

Out 4

In 2

Out 2 Out 3

In 3

L2

93

D

M

PIC

microcontroller

Page 54: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

C2. Stepper motorsStepper motors are very accurate motors thatare used in devices such as printers andcomputer disk drives. Unlike DC motors whichspin freely, a stepper motor moves round inaccurate steps. Each step is usually 7.5degrees, so that 48 steps make one completerevolution.

There are two main types of stepper motor -unipolar and bipolar. The unipolar is mostcommonly used in school projects and so thisis the type shown here.

Unipolar motors usually have four coils whichmust be switched on and off in a particularsequence to make the motor turn. The tablein fig. 4.11 shows the four-step sequence.

Fig 4.11 Four step sequence to drive a stepper

motor

Use an Outputs command in PIC-Logicatorsoftware for each step. Set each commandto switch on or off the appropriate outputsfrom the PIC microcontroller. The timebetween each Outputs command will

determine the speed at which the motorturns. The minimum WAIT time is 0.1s, so ifyou use this command the fastest speedpossible is only about 5s per revolution. Abetter solution is shown in fig. 4.12.

The TIMER macro in this flowsheet providesa very brief pause between each Outputscommand. It will take the PIC about 0.5ms toprocess the macro. The Expressioncommand that sets the value of A willtherefore set the speed of the motor. Seepages 32 and 35 for more information on thecommands used.

Fig. 4.12.

Accurate speed control of a stepper motor.

54

12V

0V

6V

PIC

microcontroller

steppermotor

ULN

2003

A

8 9

1

2

3

4

16

15

14

13

Step Coil 1 Coil 2 Coil 3 Coil 4

1 1 0 1 0

2 1 0 0 1

3 0 1 0 1

4 0 1 1 0

1 1 0 1 0

Fig 4.13 A ULN2003A Darlington Driver is used to drive the stepper motor.

Page 55: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

Note that diagrams shown in this section show how to connect the input device only. Forsimplicity, the PIC microcontroller is simply shown as a block. In each case, the PICmicrocontroller must be connected to power as shown in “3. Basic Connections” (page 47).

The position of the input pins on the block diagram does not necessarily indicate the position ofan input pin on a PIC microcontroller chip. Consult the pin-out diagrams in “2. PICMicrocontroller Pin-out Diagrams” (page 45) to identify the correct pin to use.

A. Digital SensorsThe most-commonly used digital sensor is a switch, such as a microswitch, push switch or reed(magnetic) switch. All of these devices have two contacts which are either open (0) or closed(1). Fig 5.1 shows how to connect a digital sensor such as a switch to a PIC microcontroller

Fig 5.1 Connection of a switch

B. Analogue SensorsB1. PotentiometerConnect the wiper directly to an analogue input pin on the PIC microcontroller as shown in fig. 5.2.

Fig 5.2 Connection of

a potentiometer.

5. Connecting Input Devices

55

0V

6V

PIC

microcontroller

1kΩ

10kΩ

inputpin

Microswitch Push Switch

Reed (Magnetic) Switch

0V

6V

PIC

microcontroller

inputpin

1kΩ

10kΩ

Rotary potentiometer

Linear potentiometer

Page 56: A guide to using PIC-Logicator software and connecting to ...resources.hwb.wales.gov.uk/VTC/ngfl/dandt/75/Systems/Economatics/Pic... · The range of PIC-Logicator commands allows

B2. Light sensorFig 5.3 shows how a light sensor can be made by connecting a light dependent resistor (LDR)and a fixed resistor to a PIC microcontroller. For this circuit, ambient light (inside) will be aroundthe 80 level on the scale, with dark being 0 and bright sunlight taking the reading up to 255.

B3. Temperature sensorA temperature sensor is made by connecting a thermistor and fixed resistor to a PICmicrocontroller as shown in fig 5.4. In this circuit, a temperature of about 20˚C will give areading of about 30 on the PIC-Logicator scale and will drop to nearly zero at 0˚C.

To avoid possible interference from sensors to other analogue inputs, it is a good idea, whendesigning your circuit, to connect any unused analogue inputs to 0 volts (ground).

In this type of simple circuit, the sensors do not respond linearly. Some experimentation isrequired for your own application to ensure that your PIC microcontroller is responding at thecorrect levels. To be sure of the actual level that the PIC microcontroller is reading from theinput, use the PIC-Logicator Analogue Calibration Board (see page 24) which is a valuabledevelopment tool because it displays the actual reading that is being obtained from the sensor.

56

0V

6V

PIC

microcontroller

analogueinputpin

Thermistor

220Ω

1kΩ

Thermistor

0V

6V

PIC

microcontroller

analogueinputpin

Lightdependentresistor

ORP12

220Ω

Lightdependentresistor

Fig 5.3 Connection of

a light sensor

Fig 5.4 Connection of a

temperature sensor