Electronic Control Systems Introduction Many electronic devices by bnmbgtrtr52

VIEWS: 19 PAGES: 24

									Electronic Control Systems Introduction




Many electronic devices have been developed to make life easier (for example a microwave), to make life safer
(for example traffic lights), to help with work (for example a computer) and for entertainment purposes (for
example computer games consoles).

Some of these devices are purely electronic devices (for example a digital watch). However, many of these devices
also control mechanisms (for example the eject mechanism in a video recorder) and so can be described as
mechatronic devices.

However, both electronic and mechatronic devices all have one thing in common - an electronic control system.

Any electronic control system can be broken down into three distinct parts. This can be shown as a block
diagram.




Assignment 1.1         (a) List three electronic devices.



(b)    For these three mechatronic devices, explain the 'mechanism' in each of them.




System diagrams




A system diagram is a more detailed block diagram that also shows the real-world input signals (for example light
or heat) and the real-world output signals (for example movement or sound).




Therefore, the system diagram for a warning device for a freezer
in a restaurant would be drawn as below. The buzzer would
sound if the temperature in the freezer passed a certain level.



                                                                                                                1
Case study - an electronic toy




A popular electronic toy is shown above. This is a good example of a mechatronic system, as it uses an electronic
circuit to control a number of mechanisms. It also contains a number of sensors so that it can react to changes
when it is moved (for example being put in a dark place or being turned upside down).



Input transducers are electronic devices that detect changes in the 'real world' and send signals into the process
block of the electronic system. Some of the input transducers for the electronic toy are:
       • push switches on the front and back to detect when the toy is being 'stroked', and a switch in the mouth
         to detect when the toy is being 'fed'
       • a light-dependent resistor (LDR) between the eyes to detect if it is light or dark
       • a microphone to detect noises and speech
       • a tilt switch to detect when the toy is being turned upside down
       • an infrared detector to detect infrared signals from other toys.
Output transducers are electronic devices that can be switched on and off by the process block of the electronic
system. Some of the output transducers of the electronic toy are:
       • a d.c. motor to make the eyes and mouth move
       • a speaker to produce sounds
       • an infrared LED (light-emitting diode) to send signals to other toys.

Assignment 1.2

Draw a systems diagram for the electronic toy described previously.




Assignment 1.3

List the input and output transducers for the following devices.

        a) Washing machine                                     b) Personal stereo




                                                                                                                     2
What is a micro controller?

A micro controller is often described as a 'computer-on-a-chip'. Micro controllers have a controller and memory
all built into a single chip. As they are small and inexpensive they can easily be built into other devices to make
these products more intelligent and easier to use.

Micro controllers are usually programmed for a specific electronic product - for instance, a microwave oven may
use a single micro controller to process information from the keypads, display user information on the seven-
segment display or control the output devices (turntable motor, light, bell and magnetron).

Micro controllers are single-chip 'computers' designed to control specific processes or products. The micro
controller is programmed with a program to complete the desired task. By altering this program, the same 'brand'
of micro controller can be used to complete different tasks. The same micro controller device can therefore be
used in a range of different products by simply programming it with a different program.

One micro controller can often replace a number of separate parts, or even a complete electronic circuit. Some of
the advantages of using micro controllers in a product design are:

       • increased reliability and reduced quantity of stock (as one micro controller replaces several parts)
       • simplified product assembly and smaller end products
       • greater product flexibility and adaptability since features are programmed into the micro controller and
         not built into the electronic hardware
       • rapid product changes or development by changing the program and not the electronic hardware.

Applications that use micro controllers include household appliances (for example a microwave),
alarm systems (for example a fire alarm), medical equipment (for example an incubator for premature babies) and
electronic equipment (for example a computer mouse).

Assignment 1.4 List three devices that may contain a micro controller. Explain why you think it would be
               useful to have a micro controller in these devices.

       a)



       b)



       c)



Micro controller Architecture

The main features of
the micro controller are
shown in the block
diagram.




Micro controllers contain all these features within a single package, as opposed to the microprocessor system
where each block in the diagram above is normally a separate integrated circuit.

                                                                                                                      3
Arithmetic Logic Unit          The processing unit ( ALU ) is the 'brain' of the micro controller. It operates by
reading instructions from the ROM ( read only memory - the permanent program memory ) and then carrying
out the mathematical operations for each instruction.
Clock The speed at which these mathematical operations occur is controlled by the clock circuit. The clock
synchronises all the internal blocks ( ALU, ROM, RAM, etc. ) so that the system remains stable so that the
whole system operates correctly.
Memory (ROM and RAM) Micro controllers contain both ROM (permanent memory) and RAM
(temporary memory). The ROM (Read Only Memory) contains the operating instructions (i.e. the 'program') for
the micro controller.
The ROM is 'programmed' before the micro controller is installed in the target system, and the memory retains
the information even when the power is removed. Most micro controllers are one-time-programmable types
(OTP), which means the ROM can only be programmed once. If you make a mistake, and have to change the
program, the chip has to be thrown away and a new chip programmed with the revised program. To overcome this
problem some micro controllers now use FLASH EEPROM memory instead ( Electronically Erasable
Programmable Read Only Memory).
The RAM (Random Access Memory) is 'temporary' memory used for storing information whilst the program is
running. This memory is 'volatile', which means that as soon as the power is disconnected the contents of the
memory is lost.
Buses Information is carried between the various blocks of the micro controller along 'groups' of wires called
buses sometimes called an information highway. The 'data bus' carries data between the ALU and RAM and
the program bus caries program instructions from ROM to the ALU.
The Stamp Controller           The stamp controller system consists of three main components.
• The 'Basic Stamp' software. This software runs on a computer and allows you to use the computer keyboard to
  type in programs.
• The serial cable. This is the cable that connects the computer to the stamp controller. The cable needs to be
   connected only when downloading programs. It does not have to be connected when the stamp controller is
  running because the program is stored on the stamp controller board - even when the power supply is
  removed!
• The stamp controller
The stamp controller 'runs' programs that have been downloaded
to it. It has indicator LEDs to show which outputs and inputs are
 on or off,and has connectors for the input and output modules.
The 'brain' of the stamp controller board is the 18 pin micro
controller chip in the centre of the board.
The program is stored in the 8 pin EEPROM (Electronically
Erasable Programmable Read Only Memory) chip. This type of
memory can be reprogrammed when desired, but it also keeps the
program when the power supply is removed. This means the stamp
controller will start to run the program currently in the memory
whenever the power supply is connected.
When the power supply is connected to the stamp controller
board, the micro controller 'reads' the program from the EEPROM
memory chip. It then carries out the program as instructed. The program can contain instructions ('commands') to
switch outputs on and off, to react to inputs or to pause for time delays. The stamp controller is extremely fast - it
can process over 1000 instructions in a second!
Summary - programming procedure
1. Draw a flowchart for the control task.
2. Write the program on the computer using the Stamp software.
3. Connect the download cable from the computer to the stamp controller.
4. Connect the power supply to the stamp controller.
5. Use the Stamp software to download the program. The download cable can then be removed
    after the download.
The program will start running on the stamp controller automatically. However, the program can also be restarted
at any time by pressing the reset switch.                                                                      4
Flowcharts

Flowcharts are commonly used to explain how a program works. As flowcharts are drawn graphically they often
make programs easier to understand. A flowchart should be drawn for each program you develop.


The flowchart shown uses three different symbols.
                                                                                          start
Start/stop symbol   The 'Start' or 'Stop' symbol shape is a rectangle with rounded
                    ends. Each flowchart must contain only one 'Start' symbol and,
                    usually, only one 'Stop' symbol.                                   switch all
                                                                                       pins high

Wait symbol         The 'Wait' symbol is a rectangle. The text inside the symbol         wait 5
                    explains how long the time delay is.                                 secs

Outputs symbol      The 'Outputs' symbol is a parallelogram. The text inside the        switch all
                     symbol explains which output pins are switched on or off at        pins low
                    any time.
                                                                                         wait 0.5
                                                                                         secs

                                                                                        switch all
                                                                                        pins high

                                                                                        wait 0.5
                                                                                        secs
Decision Symbol The decision symbol shape is a rhombus. The text inside the
                 rhombus asks whether question. e.g. has an input been                   has this
                 sensed or has a count finished.                                         been
                There are 2 output lines YES and NO. Usually the NO                      done 10         N
                 feeds back to before the rhombus if the answer to                       times
                the question is no.
                                                                                             Y
Continuous loops
                                                                                         switch all
                                                                                         pins low

                                                                                          stop




                                              Sometimes it is necessary to create programs that ‘loop’
                                              forever, as is the case in the following flowchart.

                                              There is no ‘Stop’ symbol because the program never ends.




                                                                                                             5
Assignment 1.5          A set of temporary traffic lights is required for a system of road works.

Draw a flowchart for the continuous lights sequence shown by
one set of traffic lights. Use the times shown in the table for each
stage.




Assignment 1.6      A microwave oven operates with the following sequence. Draw a flowchart for this sequence.




1) Light on
2) Turntable on
3) Magnetron on
4) Wait 30 seconds
5) Magnetron off
6) Wait 10 seconds
7) Turntable off
8) Buzzer on
9) Wait 0.5 second
10) Buzzer off
11) Light off




                                                                                                             6
Number Systems

The counting system used in everyday activities is the
decimal system. This number system uses the ten familiar
digits 0 to 9 to express the magnitude of the number.

However, as the microcontroller only recognises the two
electronic states high and low, it uses the binary number
system. This number system uses just two digits 0 and 1.
An electrical signal which is low is represented by 'logic 0',
 and a signal which is high is 'represented by a 'logic 1'.

The first sixteen numbers in the decimal and binary
systems are shown in the table

A single binary digit is referred to a bit (binary digit).




Bits and Bytes

Eight bits grouped together are described as a byte. The decimal value of a byte is calculated by adding together
the corresponding decimal value of each of the individual bits. The eight bits in a
byte are labelled bits 0 to 7, from right to left. The right most bit is called the Least Significant Bit
( LSB ) and the left most bit is called the Most Significant Bit ( MSB ). The decimal value of each bit
is given in the table below:




e.g.     The binary number 10010111 when converted into decimal would be: =

Note that when writing binary numbers it is quite common to write all 8 bits, even if the first bits are equal to
zero (unlike the decimal system, where leading zeros are not normally written).

Converting Decimal to Binary

To convert any decimal number into binary repeatedly divide the decimal number by two and record the
remainder after each division. The decimal number 29 is used as an example.

Therefore the decimal number 29 equals the binary number               =

Notation

When using a number of different counting systems it is important to distinguish which counting system you are
using. For instance the number '10' means three different values in the three different counting systems.

Therefore the following notations are used within PBASIC programs:

         Decimal values are written as usual:                      10      ( = 10 in decimal)
         Binary values are preceded by a % symbol:                %10      ( = 2 in decimal)

TUTORIAL - NUMBER SYSTEMS

1      Write the binary equivalent of the following decimal numbers.
        a)       17             b)      23             c)     11             d)      149             e)      38
        f)       254            g)      199            h)     65

2      Convert the following binary numbers to their decimal equivalents.
         a)      %00010001 b)          %00100101        c)    %01101011      d)      %10010011
         e)      %00110110 f)          %11001011         g) %00011010        h)      %00111001                      7
Port Addressing

Micro controllers communicate with the outside world by input / output pins which are grouped together in
'ports '. The Stamp Controller has one input and one output port, which contains a total of eight pins.

Each pin can be addressed individually or all eight in the port can be addressed simultaneously. In the PBASIC
language the pins are labelled 0 - 7 and the whole port address is allocated the label ' pins ' .

 e.g. a) to pin 3 high ( ON) individually the command would be -              high 3
        b) to pin 3 low (OFF ) individually the command would be             - low 3
        c) to switch all pins high the command would be - let pins = 255 ( or let pins = %11111111)
        d) to switch all pins low the command would be - let pins = 0 ( or let pins = %00000000 )
        e) to switch pins 0 - 2 high and pins 3 - 7 low the command would be -
                                                                        MSB                LSB



                                        let pins = 7       ( or let pins = %00000111 )
Notice how the use of the binary system can be used to clearly illustrate which pins are high or low.
Data Direction Register

Each pin can be configured to be an output (to send digital signals) or an input (to receive digital signals). The
Data Direction Register ( DDR ) is used to configure the port and in the PBASIC language the DDR is
allocated the label ' dirs ' .
If any bits in the DDR are set high then all the pins will be set as outputs. If any of the bits are set low then the
pin will be set as an input.

        e.g.    let dirs = 255                          ' set all pins as outputs

                let dirs = 0                            ' set all pins as inputs

                let dirs = %11110000                    ' set 0 - 3 as inputs and 4 - 7 as outputs
Every PBASIC program listing should always begin with a ' let dirs = ' statement to correctly set-up the DDR.
By default all pins are set to inputs when the Stamp Controller is reset.
Note the 'comments' at the end of each line. A comment starts after an apostrophe ( ' ) and continues to the end
of the line. Although the comments are not needed to make the program work, they are an essential part of the
program as they explain in 'plain language' what the program is doing. You should always add a comment to
every line of your program, particularly if the program is to be studied by someone else at a later date or for a test
or examination.
Time Delay

A time delay can be simply formed by the following line.

                pause x                 'number of milliseconds up to 65535 milliseconds

        e.g.    pause 2000              ‘wait 2 seconds = 2000 ms

                pause 100               ‘wait 1/10 second = 100 ms
GOTO

Some early BASIC language used ' line numbers ' rather than labels for ' goto ' commands. Unfortunately this
line numbering system can be inconvenient to use because if you modify your program by later adding, or
removing, lines of code you then have to modify all the line numbers within the ' goto ' commands accordingly.
The label system overcomes this problem automatically.                                                         8
Downloading a sample program MAC > APPLICATION ALIASES (bottom of screen )
>Virtual PC >OPEN>Choose Cancel>Picaxe Editor on screen> choose PBasic Stamp
                                                                                 OR
Mac HD>Users>Shared>Virtual PC >Windows 98 - Choose Cancel>Picax Editor on screen>
choose PBasic Stamp
Activity 1.1 Start up the Stamp software and key in the program below.


Toolbar short cuts




The following PBASIC program switches output 7 on and off every second. When you download this
program, the red LED 7 on the stamp controller should flash on and off every second.

Converting a flow chart

Once a flow chart has been drawn it is necessary to convert it into the stamp controller programming language
which is called PBASIC.

Activity 1.2                           A PBASIC program which would achieve this control operation is:

                                       let dirs = %11110000            ‘set pins 0-3 inputs, 4-7 outputs

                                       high 7                                 ' set pin 7 high

                                       pause 2000                             ' wait for 2 seconds

                                       high 6                                 ' set pin 6 high

                                       pause 1000                             ‘ wait for 1 second
                                       let pins = %11110000                   ' set pins 4-7 high

                                       pause 3000                             ' wait for 3 seconds

                                       let pins = 0                           ' switch all pins low

                                       end                                    ' end the program



Key in the program listed above, and then download it to the Stamp Controller.
The LED indicator on pin 7 should light first, followed by the indicator on pin 6, and then indicators 4 to 7. To
rerun the program press the reset on the Stamp Controller.

Read through the program carefully, and make sure you understand exactly what each program line achieves.

The following program uses a ‘goto’ statement. Draw the flow chart for this program and write appropriate
comments after each line of program

                               init:           let dirs = %10000000                ‘set up pin 7 as output
                               main:            high 7                        ‘
                                               pause 1000                     ‘
                                               low 7                          ‘
                                               pause 1000                     ‘
                                               goto main                      ‘
                                                                                                                    9
This program uses the 'high' and 'low' commands to control output pin 7, and uses the 'pause' command to make a
delay.
The last 'goto main' command makes the program 'jump' back to the label 'main' at the start of the program. This
means the program loops forever. Note that the first time the label is used it must be followed by the colon ( : )
symbol. This tells the computer that the word is a new label. Save the program and then download it to the
stamp controller by clicking 'Run'.
If you have the stamp controller connected correctly, after a few seconds you should see a 'download successful'
message on the computer screen, and the red LED 7 on the stamp controller should start flashing on / off .

Activity 1.3


                                                A PBASIC program which would achieve this control operation is
                                                listed below.

                                   init :       let dirs = %11110000 ‘set pins 4 - 7 as outputs

                                  main :        high 7                     ‘ set pin 7 high
                                                pause 2000     ‘wait for 2 seconds (= 2000 ms)
                                                high 6                     ‘set pin 6 high
                                                pause 1000                ' wait for 1 second
                                                let pins = %11110000       ' set pins 4-7 high
                                                pause 3000                 ' wait for 3 seconds
                                                let pins = 0               ‘ switch all pins low
                                                pause 1000                 ' wait for 1 second
                                                goto main                  ' loop forever
                                        Key in, download and run the program listed above.


                                        After the first line (which simply sets up the DDR), a label called 'main' has
                                        been added to the listing. Note that all the labels must end with a colon ( : )
                                        when they are first defined. It is also a good programming technique to use
                                        tabs (or spaces) at the start of lines without labels so that all the commands
                                        are neatly aligned. The term 'white space' is used by programmers to
                                        define tabs, spaces and blank lines, and the correct use of white-space can
                                        make the program listing much easier to read and understand.


The last line ' goto main ' causes program flow to ' jump back ' to the line labelled ' main '. This means that this
program will loop ' forever '.
Using symbols
Sometimes it can be hard to remember which pins are connected to which devices. The 'symbol' command can
then be used at the start of a program to rename the inputs and outputs.

symbol red = 7                                          ' rename output 7 'red'
symbol green = 5                                        ' rename output 5 'green'
init : let dirs = %11110000                             ' set pins 4 - 7 as outputs
main:           high red                                'red LED on
                low green                               ' green LED off
                pause 1000                              ' wait 1 second
                low red                                 ' red LED off
                high green                              ' green LED on
                pause 1000                              ' wait 1 second
                goto main                               ' jump back to the start                                       10
Saving Programs

Highlight Program > Edit > COPY > Press Apple key > Cursor bottom of screen> Load Text Editor>Open new >
Edit > Paste>Save into your folder with name

Loading Program
Load text editor with program> Edit> Copy > start pc > paste program in PBasic.

Activity 2. Key in the program listed previously, and then download it to the stamp controller.

The red and green LEDs should flash alternately.

Read through the program carefully, and make sure you understand exactly what each program line achieves.

TUTORIAL

1 What is the function of the term Data Direction Register (DDR) ?

2 What is meant by ' white space ' and why is it important to use white space and comments when writing
  programs ?

3 Draw the flowchart and write the program in PBASIC to switch on each of the following :

                i)     bit 0
               ii)     bit 2 and 4
               iii)   all red LEDs on the controller
               iv)    all green LEDs on the controller

4 Draw the flowchart and write the program in PBASIC to switch on each of the following:

               i) bit 1 for 2 seconds
               ii) bits 1 and 3 for 4.5 seconds
               iii) bits 1,2, 5 and 7 for 10 seconds

5 a) A set of temporary traffic lights are required for a system of road
     works. Draw a flow chart for the light sequence by one set of lights.
     Write a PBASIC program to achieve this operation.
     Use the following pin configuration - red ( 7 ), amber ( 6 )
     and green ( 5 ).

 b) Repeat this example but design a program for traffic lights at crossroads ( use pins 0-2 and 5-7).

6 A fountain in a garden centre is to be used to attract visitors to a new range of plastic ponds. The garden
  centre owner wishes to develop a micro controller-based system that can be programmed to switch the fountain
  pump and an external lighting system on and off at regular intervals.

   The following PBASIC program will switch the pump on and off every 10 seconds. The lights turn on
   10 seconds after the pump has started and turn off 10 seconds before the pump is stopped.

   Draw a flowchart for the control sequence and add the missing comments to the program listing.

       init : let dirs = %11110000                       ' set pins 4 - 7 as outputs

       main:             high 7                          ' switch the pump on
                         pause 10000                     ' wait 10 seconds
                         high 6                          ' switch the lights on
                         pause 4000                      ‘
                         low 6                           '
                         pause 10000                     '
                         low 7                           ‘
                         pause 6000                      '
                         goto main                       '
                                                                                                            11
7 A toy shop has a train set in the window.
  The train set has an electric train, a set of
  red/green signals and a set of moving track
  points that allow the train to move around
  two different loops of track.




   The toy-shop owner wants to develop a program that will carry out the following sequence.

                1.      Switch the green signal on
                2.      Switch the train on for 30 seconds
                3.      Stop the train
                4.      After 2 seconds switch the green signal off and the red signal on
                5.      After 5 seconds switch the points on
                6.      Switch the train on for 30 seconds
                7.      Stop the train
                8.      After 2 seconds switch the points off
                9.      After 5 seconds switch the red signal off and the green signal on
                10.     Jump back to step 1

Draw a flowchart for the control sequence and then use the flowchart to write a PBASIC program.

Connecting output transducers to the stamp controller

The stamp controller can only drive low-power devices,
such as LEDs, directly. It cannot drive devices such as
lamps, buzzers, solenoids or motors directly because
these devices require a higher current to operate.




A common way to drive these devices is with a transistor, as shown in the diagram above. In this case the lamp is
controlled by the transistor switching on and off.



The output driver module provides four transistor
outputs, as in the circuit shown above. Instead of
using four separate transistors, the output driver
uses an integrated circuit called the ULN2803A,
which contains all the transistors in one 18-pin 'chip'.




                                                          2 Motor connections                      Buzzer
                                                        ( pins 4/5 or 6/7                          connection
                                              follow the white lines on the board )              (+V - red / pin
                                                                                                   no. - black)

To use the transistor outputs, the output device should be connected between the screw-terminal numbered output
(4-7) and a V+ connection. The positive (red) wire on polarised devices (for example a buzzer) should be
connected to the V+ connection.                                                                              12
Programs for driving motors

External devices such as motors can be connected via the output driver module. This allows forward and reverse
control of DC motors. Each motor output uses two output pins to control the rotation of the motor. If an Output
Driver board is used the the outputs for the motor are as follows.

motor A ( connected to pins 4 and 5)         motor B ( connected to pins 6 and 7)




Activity 3.1

Connect two motors to the output
driver module as shown in the
diagram. Key in the given
PBASIC program to control the
movement of the motors.                                                             4
                                                                                    5
                                                                                    6
                                                                                    7

init:          let dirs =%11110000                   ‘ set pins 4-7 as outputs

main:          let pins = %10100000                  ‘ motor A fwd , motor B fwd
               pause 1000                            ‘ wait 1 second
               let pins = %00000000                  ‘ motor A off, motor B off
               pause 1000                            ‘ wait 1 second
               let pins = %01010000                  ‘ motor A bck , motor B bck
               pause 1000                            ‘ wait 1 second
               let pins = %00000000                  ‘ motor A off, motor B off
               pause 1000                            ‘ wait 1 second
               goto main
Activity 3.2 Write a program to control the movement of the buggy as
             shown in the flowchart below.




                                                                                                             13
Speed control of d.c. motors

There are two ways to control the speed of a d.c. motor. The simplest is to vary the voltage applied to the motor.
If, for instance, 3 V is applied to a small d.c. motor it will rotate at a lower speed than if 5 V were applied.
Unfortunately the 'turning power' (torque) of the motor will also drop, which means the whole motor system will
be less powerful.

The second way to control the motor is to always apply the full voltage (for example 5 V) across the motor, but
then to switch the power supply on and off rapidly. As the power supply is off some of the time, the motor does
not receive as much power and so the motor turns more slowly. The advantage
of this system is that the torque remains quite high.

This system is called pulse-width modulation (PWM). The time that the power supply is switched
on is called the mark time, and the time that the motor is
switched off is called the space time. By varying the
on (mark)-to-off (space) ratio, the speed of the motor
can be varied.



Connect a motor to the driver board on pin 6 and 7.

Key in, download and run the program below. This program drives the motor at approx. half speed
as the space ( off time ) is the same the length of the mark (on time). Note that you must use pause delays as the
power supply must and off very quickly ( pause delays must be small)

       main:           high 7                                  ‘ output high
                       pause 10                                ‘pause for 10 ms
                       low 7                                    ‘output low
                       pause 10                                ‘pause for 10 ms
                       goto main                               ‘loop back to start
Try out different speeds by experiment by altering the length of the pause delays.

For .... Next Loops

It is often useful to repeat the same part of a program a number
of times, for instance when flashing a LED. In these cases a
‘for ... next ‘ loop can be used.




The number of times the program runs for is set by a variable.
A variable is a number that is stored in the RAM memory of the
Stamp Controller. There are 14 memory locations that byte
variables can be stored in. These locations are labelled b0 to b13,
but also can be ' renamed ' to more appropriate names by use of
the ' symbol ' command.




                                                                                                                14
Ex Key in , download and run the following program.

       symbol counter = b0                             ' define the variable " counter "
       symbol red = 7                                  ' define pin 7 with the name " red "

init : let dirs = %10000000                            ‘set pin 7 as output

main :         for counter = 1 to 5                    ‘start for .. next loop - set counter to 5
                            high red                   ' switch pin 7 high
                            pause 1000                 ' wait for 1 second
                            low red                    ' switch pin 7 low
                            pause 1000                 ' wait for 1 second

               next counter                            ' end of for .. next loop
                     end                               ' end program
Note again how the white space has been used to clearly illustrate all the commands that are contained between
the for and next commands. The ' symbol ' command is also used to label pins and variables to make them
easier to use.


Assignment 3. 3

Connect the washing machine model to the output driver module.
The motor on the washing machine is controlled by outputs 6 and 7
(motor B). Switching output 6 on will make the motor turn one way;
switching output 7 on will make the motor turn the other way.

(a) Write a PBASIC program that will make the motor rotate at full
    speed in each direction for five seconds.

(b) Write a second PBASIC program that will make the motor rotate at half speed in each direction for five
    seconds. Use PWM speed control for the motor.

Assignment 3.4

Connect two motors to the output driver module as shown in the diagram. The two motors could be connected to
a buggy which could follow the movement path as shown below.

Draw a flowchart for the movement of the buggy, making use of a for ... next command.

Write a high level program in PBASIC to control the movement of the buggy.




                                                   4
                                                   5
                                                   6
                                                   7




                                                                                                             15
Assignment 3.5          The following PBasic program will carry out the instructions shown in the
                        flowchart. Add the missing comments to complete the program listing.
        start

     switch all                symbol counter = b0                   ' define the variable 'counter'
     pins high
                               init: let dirs = %11110000            ‘
       wait 5
       secs                    main: let pins = %11110000            ‘
      switch all
      pins low                         pause 5000                    '

       wait 0.5                        for counter = 1 to 10         '
       secs

       switch all                             let pins = %00000000             ‘
       pins high
                                              pause 500                        '
       wait 0.5
       secs
                                              let pins = %11110000             ‘

       has this                               pause 500                        ‘
       been         N
       done 10
       times                           next counter                  '

           Y                           let pins =%00000000           ‘
       switch all
       pins low                        end                           ‘ stop the program
         stop

Sub-procedures         A sub-procedure is a separate 'mini-program' that can be called from the main program.
Once the sub-procedure has been carried out, the main program continues. Sub-procedures are often used to
separate the program into small sections to make it easier to understand. The following program uses two sub-
procedures to separate the two main sections of the program ('flash' and 'noise').

Activity 3.6 Key in, download and run the above program. At the end of each sub-procedure a 'return'
command is used to return the program flow back to the main program loop.

symbol red = 7                                ' rename output 7 'red'
symbol buzzer = 6                             ' rename output 6 'buzzer'
symbol counter = b0                           ' define a counter using variable b0

main: gosub flash                             ' call the sub-procedure flash
      gosub noise                             ' call the sub-procedure noise
      goto main                               ' loop back
end                                           ' end of the main program

flash: for counter = 1 to 25                  ‘start a for ... next loop
               high red                       ' red LED on
              pause 50                        ‘wait 0.05 second
              low red                         ' red LED off
              pause 50                        'wait 0.05 second
       next counter                           'next loop
       return                                 ' return from the sub-procedure

noise: high buzzer                             ' buzzer on
        pause 2000                             ' wait 2 seconds
         low buzzer                            ' buzzer off
        return                                 ‘return from the sub-procedure
Note that at the end of the main program (but before the sub-procedures) an 'end' command has been added. This
is a good idea as it stops the main program accidentally 'falling' into a sub-procedure at the end of the main
program.                                                                                                       16
Assignment 3.7         A washing machine has both wash and spin cycles. Add the missing comments to this
                       PBASIC program, which will control the washing machine.

symbol motor = 7                              ' rename output 7 'motor'
symbol counter = b0                           ' name a counter variable
main:             gosub wash                  ‘
                  gosub spin                  ‘
                  gosub wash                  ‘
                  gosub spin                  ‘
                  gosub wash                  ‘
                  gosub spin                  ‘
end                                           ‘
wash:                                         ‘
      for counter = 1 to 250                  '
             high motor                       '
             pause 10                         ‘
              low motor                       '
             pause 5                          '
      next counter                            '
      return                                  '
spin: high motor                              '
      pause 5000                              '
       low motor                              '
      return                                  '
Using Inputs

A digital sensor is a simple sensor which can be ‘on’ or ‘off’. ( e.g. micro switches, reed switch, LDR )

We can connect an input board on to the Stamp board or even connect external switches to the board direct.




If ... Then ...


The 'if ... then' programming structure allows
the computer to make a decision based on
information received from an input pin.




                                                                                                             17
Activity 4.1 Key in , download and run the program listed below.

For the above program pin 7 switches high and then waits for an input connected to pin 3 to go high. When this
happens the output pin is switched low. Connect the input switch unit to the Stamp Controller.

                init : let dirs = %10000000                        ' set DDR - pin 7 as output
                main : let pins = %10000000                        ' switch pin 7 high
                       if pin3 = 1 then skip                      ‘jump to label skip if input 3 is high
                       goto main
                skip :        let pins = 0                        ' switch all pins off
                              end                                 ' end program
NB The then command must be followed by a label to jump to. You cannot add extra commands on the
    same line within the PBASIC language.

Activity 4.2 Key in , download and run the program listed below. The program makes the pin 5 flash every time
the input pin 0 is pushed.

init:                   let dirs = %11110000                     ‘ set pins 0-3 inputs
main:                   if pin0 =1 then flash                    ' jump if the input is on
                        goto main                                ' else loop back around
flash:                                                           ' make a label called 'flash'
                        let pins = %00100000                     ' switch output 5 on
                        pause 2000                               ' wait 2 seconds
                        let pins = %00000000                     ' switch output 5 off
                        pause 2000                               ' wait 2 seconds
                        goto main                                ' jump back to start
In this program the first three lines make up a continuous loop. If the input is off the program just loops around
time and time again.

If the switch is then pushed, the program jumps to the label called ‘flash'. The program then switches output 5 on
for two seconds before returning to the main loop.

Note carefully the spelling in the 'if ... then ' line - 'pin0' is all one word (without a space). Note also that only
the label is placed after the command 'then' - no other words apart from a label are allowed.

Assignment 4.2

A burglar alarm must sound a buzzer and light a warning signal for 20 seconds when any of the four windows in
a house are opened. Each window contains a reed switch that is connected to the alarm.

Draw a flowchart and write a PBASIC program that will operate the burglar alarm correctly. Use the following
input and output connections and connect a buzzer to pin 6 ( red wire to +V and black to pin 6). Check to see if
the system operates fully.


                    Input connection                  Pin        Output connection
                                                      7            red light
                                                      6            buzzer
                                                      5
                                                      4
                    switch 3                          3
                    switch 2                          2
                    switch 1                          1
                    switch 0                          0

                                                                                                                         17
Assignment 4.3                                                           Input            Pin Output
                                                                         connection           connection
As part of a Christmas decoration in a shop, a lighting sequence
is to be controlled by a microcontroller. The output connections                          7 red light
are shown.                                                                                6 yellow light
When a visitor treads on a pressure mat under the carpet, the                             5 green light
lights should flash on and off in sequence three times.
The following PBASIC program will carry out the instructions                              4
shown in the table above. Draw the flow chart for this program                            3
and add the missing comments to complete the program listing.                             2
                                                                                          1
init: let dirs = %11100000            ‘ set pins 7,6.5 as outputs
      symbol counter = b0             ‘ set counter                      pressure pad     0

main:          if pin0 =1 then flash                         '
               goto main                                     '

flash: for counter = 1 to 3                                  '
               let pins = %10000000                          '
               pause 500                                     '
               let pins = %01000000                          ‘
               pause 500                                     '
               let pins = %00100000                          '
               pause 500                                     '
        next counter                                         '
        let pins = %00000000                                 ‘
        goto main                                            '
TUTORIAL

1 Connect a two motor buggy to the output driver module. Connect the micro switch bumpers to pins 0 and 1
  on the Stamp Controller via the screw terminals as shown. The motors should continue in a forward direction
  until either of the two micro switch bumpers is activated. At this point the motors should reverse for three
  seconds, rotate 90 degrees for 2 seconds and then continues in a forwards direction. Draw a flowchart and
  write a PBASIC program to control the movement of the buggy.




                                                                                                            19
2 A motor is connected to pin 4 and 5 is to run when a start switch connected to pin 0 is momentarily pressed.
 The motor continues to run until a stop switch connected to pin 1 is pressed. At this point the motor switches
 off. The system should reset itself to wait for another start signal. Draw a flowchart and write a PBASIC
  program for the sequence.

3 From the following flowchart develop a PBASIC program that
  will carry out the instructions. Use the following pin
  configuration.




          Input          Output
          connection Pin connection
                       7    red light
                       6 amber light
                       5 green light
                       4
          start switch 3
                                                                            AMBER
                       2
                       1
                       0




4 Connect the washing machine model to the inputs            Input              Pin Output
  module and output drivers module. The washing machine      connection             connection
  model has the following connections.                                          7      red light
                                                                                    6
                                                                                    5   Motor CW
                                                                                    4   Motor ACW
                                                                                    3
                                                                                    2
The washing machine operates as follows.                     Door interlock         1
                                                              Power switch     0
1) Wait until the power switch is on.
2) Wait until the door interlock switch is on.
3) Wash cycle: repeated 20 times - motor forwards for five seconds, motor backwards for five
   seconds.
4) Wait 5 seconds
5) Spin cycle. repeated 10 times - motor forwards for two seconds, motor backwards for two
   seconds.
6) Switch off and red light on

Draw a flowchart and write a PBASIC program to control the movement of the washing machine as described
above.
                                                                                                             20
Stepper Motors

Stepper motors are very accurate motors that are commonly used in
computer disk drives, printers, XY plotters and clocks. Unlike d.c. motors,
which spin round freely when power is applied, stepper motors
require their power supply to be continuously pulsed in specific patterns.

For each pulse the stepper motor moves around one
' step ', typically 7.5 degrees ( giving 48 steps for one
full revolution ).




Stepper motors do have some limitations. First, the power consumption is greatest when the stepper motor is
stopped ( as all coils are still energised ). Secondly the output torque is not very high and so some type of
gearbox is normally required. Unless this gearbox is very carefully designed the ' backlash ' of the gears can
disrupt the accuracy of the final system. Finally the speed of rotation is limited to approx. 100 steps per second,
which provides a rotational speed of 2 revs/s ( 120 rev/min ) only.
The stepper motor contains magnets which are
fixed to a central armature. Four electronic coils are
located round the casing. When a current is passed
through these coils they generate a magnetic field,
which attracts / repels the permanent magnets on
the armature and so the armature spins one ' step '
 until the magnetic fields align. The coils are then
energised in a different pattern to create a different
magnetic field and the armature spins another step.



To make the armature rotate continuously, the four coils must be switched on and off in a certain order. Many
microcontroller systems use four output lines to control the stepper motor, each line controlling the power to one
of the coils.

The table below shows the four different steps required to make the stepper motor turn.




To make the motor spin the other way, the steps are reversed ( 4 - 3 - 2 - 1 - 4 )

Note : An incorrect coil arrangement will result in the stepper motor oscillating rather than rotating.
Examples of stepper motors -- Printer , X - Y Plotter , Clock , One arm bandit

Advantages of stepper motors over dc motors -- accurate , positionally , more torque
Disadvantages         "        "            -- more expensive , draws a current when stationary, less speed


                                                                                                                 21
Ex Build the circuit as shown.




a) Key in, download and run the program listed below. The program demonstrates how to continually
   spin the stepper motor.

Remember not to connect the stepper motor to the voltage supply for long periods of time ( max 10
secs)

b) Try changing the speed by altering the value of the delay.

               symbol delay = b0

init : let dirs = %11110000                          ' make pin 4 - 7 outputs
       let delay = 100                               ' set delay to 100 ms

main :          let pins = %1010000                  ' first step
               pause delay                           ' pause for delay
               let pins = %1001000                   ' next step
               pause delay                           ' pause for delay
               let pins = %0101000                   ' next step
               pause delay                           ' pause for delay
               let pins = %0110000                   ' next step
               pause delay                           ' pause for delay

               goto main                             ' loop forever
TUTORIAL

   Connect a stepper motor to the output driver module.

       Develop a PBASIC program that will rotate the stepper motor 48 steps in one direction
       and then 48 steps in the other direction.

Analogue Sensors

An analogue sensor measures a continuous property such as
light, temperature or position. The analogue sensor provides a
varying voltage signal. This voltage signal can be represented
by a number in the range 1 to 240 (for example very dark = 1,
bright light= 240).

Common examples of a digital sensor are:

• LDR (light-dependent resistor)

• Thermistor

• Variable resistor (potentiometer)                                                                 22
Light-dependent resistor (LDR)

The LDR is a component whose
resistance depends on the amount of
light falling on it. Its resistance changes
with light level. In bright light the LDR's
resistance is low (typically around 1 kΩ).
 In darkness its resistance is high (typically
 around 1 MΩ).

The circuit symbol and a graph showing
the resistance at various light levels are
shown below.

The analogue sensors are connected to the inputs module in a
potential divider arrangement.




Thermistor

The thermistor is a component whose resistance depends on its temperature. The circuit symbol and a graph
showing the resistance at various temperatures.




Variable resistor (potentiometer)




A variable resistor is used to measure position.

The inputs module


The inputs module provides the interfacing circuits required
to connect switches and sensors to the stamp controller.

When the slide switch is 'down' the inputs module provides
two digital (on/off) and two analogue sensor connections.

The two analogue sensor connections allow the connection of
analogue sensors (for example an LDR light sensor or a thermistor
temperature sensor). The analogue sensors provide a reading in the
range of 1-240 with a change in value (of, for example, the light level or temperature).                    23
PROJECTS ( at least 4 project briefs must be attempted)

For the following practical assignments students are expected to:

1. Draw a control diagram of the system.

2. Select suitable input and output transducers, explaining the reason a particular transducer was selected.

3. Build a model of the system, correctly connecting the transducers via appropriate interfacing circuits.

4. Develop a flowchart of the control sequence.

5. Use the flowchart to develop and test a Pbasic program for the control sequence.

Project Briefs

1. Design and build a warning light for shipping that will come on automatically as sunset and will remain on
   until dawn. The light must flash on and off and revolve around 270°.

2. Design and build a lift suitable for a two story warehouse. The lift will run unmanned but must not be able to
   operate unless a safety grill has been closed first. The lift should be able to be controlled from both floors.

3. A heavy duty, industrial tumble dryer is required by a local cleaning contractor. The drum should be given by
   gears due to the load it will be expected to cope with. The system should also include a fan to circulate hot air
   and a method the ensure that the door has been closed before the machine starts. The drum should also rotate
   in both directions during the process.

4. Design and build a mechanism and control system to model a microwave oven. Your design should also
   include a method of preventing the microwave form being started unless the door has been closed first. It
   should also include a fan and a rotating turntable.

5. Car theft has become a major problem. After careful consideration, design and build a possible burglar alarm
   for cars left unattended.

6. Design and build a small three-wheeled remotely controlled, electrically powered buggy which can be
   controlled to follow a figure of eight course.

7. Design and build a machine that will revolve to face a light source.

8. Design and build a model of an automatic railway level crossing.

9. Design and build an automatic curtain control which operates at dusk and dawn. There should also be
   provision for manual override.




                                                                                                                   24

								
To top