Gce Electronics Book Chapter 6
Gce Electronics Book Chapter 6
Chapter 6: Microcontrollers
1. Introduction to Microcontrollers
The CPU processes the digital signals, does calculations and logic operations, creates time delays, sets up
sequences of signals, etc., by following a program of instructions stored in part of its electronic memory.
Microcontrollers do exactly what they are told to do by the program, and nothing else!
A program is a list of instructions, along with any data needed to carry them out. Its activities
are synchronised by the clock, which sends a stream of voltage pulses into the CPU to control the
execution of program instructions and the movement of data.
To talk to the outside world, the microcontroller has ‘ports’ that input or output data in the form of binary
numbers. Each port has a number of connections – often referred to as ‘bits’. An 8-bit port handles an 8-bit
(or 1-byte) number.
Information from sensors is fed into the system through the input port(s). The microcontroller processes
this data and uses it to control devices that are connected to the output port(s). The ports themselves are
complex electronic circuits – not simply a bunch of terminals on which to hang components.
Some microcontrollers incorporate analogue-to-digital converters (ADCs) that allow analogue sensors to be
connected directly to an input.
Outputs can be interfaced to a microcontroller with a transistor or MOSFET, so even high-powered output
devices can be accommodated easily.
2. Programming a Microcontroller
A program sets out a sequence of actions which the microcontroller should take.
The design process usually starts with a flowchart. This is a set of program commands which are repre-
sented by icons that describe the program sequence.
The shape of the icon indicates the type of action involved. The actions required are written inside the
icons. The icons are then linked to show the flow of the program by arrowed lines called flow lines.
Terminator
Starting and finishing a flowchart.
Process
Used where calculation or a delay is needed.
3. Programming Languages
• We could write the program in English, or something close to it, and then have the result translated
into numbers.
• We can think through the program design in English and then translate it ourselves into a language
that is similar to numbers, known as ‘assembler’. From there, it is a swift and simple step for an
electronic system to convert it into the numerical code that the microcontroller understands.
These two extremes are known as programming in a high-level language (something close to English) or
in a low-level language (assembler).
Note:
The information given above, in sections 1 and 3, will not be examined in any AS examinations.
4. Graphic-based programming
Learning Objectives:
Converting a flowchart, icon by icon, into a fully fashioned program can be both difficult and time
consuming. For this reason, user-friendly applications have been developed that use a graphics interface
and do not require traditional programming skills.
Flowchart icons are chosen from a menu and ‘dragged’ onto the computer screen. An editing window allows
the contents to be translated into instructions, avoiding syntax errors. The flowchart can then be tested and
edited.
All allow on-screen simulation and download via a USB cable to a microcontroller on a dedicated interface
circuit board.
Note:
The activities in this chapter are written in generic format. Commercial programs vary in the way they are
configured and in the functionality offered by the ‘drag-and-drop’ icons.
The activities are intended to illustrate aspects of programming using flowchart control programs, rather
than to perform a useful function. However, they can be extended and modified to satisfy ‘real-life’
requirements.
Some flowchart control programs:
• use ‘Delay’ rather than ‘Wait’ to represent a time delay;
• use ‘High’ or ‘Low’ to turn a single output on or off and ‘Outputs’ to turn multiple outputs on and off,
whilst others use ‘Outputs’ for both situations;
• Use IN and OUT to read and write to inputs/outputs
Some flowchart control programs can be configured both to examine the state of the switch and execute
the decision in a single icon. Others require that the state of the switch is examined in an Input icon, and
then test the data in a subsequent Decision icon.
Another difference between applications is that some distinguish between digital and analogue signals, or
when comparing signals and use distinctive Decision icons for each.
Investigations
There are no specific investigations suggested in this chapter. Instead, the student should construct and
test each of the following flowcharts, using any available flowchart control program. The student should
ensure that the chosen microprocessor has a sufficient number of bits available on the input and output
ports to satisfy the requirements of the flowchart.
Purpose:
This program:
• switches on an LED, attached to the output port, for five seconds;
• switches it off for two seconds;
• then repeats the process over and over again.
Features:
• Each ‘bit’ of the output port is controlled independently.
Here, the program controls the device attached to bit 0, the LED.
• The task of turning a device on for a period of time must be broken down into three stages:
• turn on the device;
• leave it on for the required time, by adding a delay;
• switch it off.
• This program needs no input from the real world. Once started, it simply runs regardless of external
factors.
• To repeat the sequence indefinitely, the program ‘loops back’ to the beginning of the flowchart.
Note: Some flowchart programs will insert a ‘STOP’ icon by default even if the program loops back and
repeats a sequence indefinitely. In this situation, the STOP will be ignored.
Purpose:
This program:
Features:
Purpose:
This program:
• forms part of the security system for a building;
• reads an entry code, six bits long, entered on a keypad;
• releases the lock – a solenoid – only when the correct code is entered.
Features:
(The program does not penalise the user for an incorrect entry. It could be extended to limit the number of
attempts or impose a time penalty following an incorrect attempt.)
Purpose:
Modern microcontrollers have at least one input port that can convert an analogue signal connected to it
into a binary number, using a built-in analogue-to-digital converter (ADC).
This program:
• monitors the temperature in a workshop using a temperature-sensing unit connected to analogue
input A0 (Port A, bit 0);
• turns on an air-conditioning unit fan when the room temperature is above 35°C.
Features:
• Data from an analogue temperature-sensing unit is sent to an ADC built into the input port of the
microcontroller. It produces a digital number between 0 and 255 which is stored automatically in the
microcontroller memory.
• The Decision icon tests this number to see if it is greater than 35°C. If it is, the program follows the
‘Yes’ route and switches on a fan connected to the output port. When the answer is ‘No’, the fan is
turned off (a necessary step in case previously it had been turned on).
• The program then returns to test the temperature again.
Stage 1:
This program:
• monitors six emergency exits in a theatre by checking switches attached to each;
• identifies the door that was opened by lighting the appropriate LED on a display.
Features:
• Each switch is connected to one of bits 0 to 5 of Port A of the microcontroller and outputs logic 1
when the door is open.
• The status of the switches is stored in a variable called ‘door’ when Port A is ‘read’.
• The LED display is attached to bits 0 to 5 of Port B.
• When a Port B bit is at logic 1, the LED connected to it lights.
Stage 2:
This adds a requirement to check whether any door is opened. If a door is opened, the alarm pulses until it
is closed. The LED display shows which door is open. When the door is closed, the alarm and LED turn off.
Features:
For example:
Purpose:
Microcontrollers are often used to count events, like an object breaking a light-beam on a conveyor belt.
There are complications in this seemingly simple task. In particular, care must be taken to ensure that the
system counts each event only once. This may mean adding a short delay before the program loops back.
Once a specific number of objects have passed through the beam, and been placed in the box, another
action, such as closing the box with the objects inside it, is carried out. The process then repeats.
In this example, there are other tasks to take care of, not tackled in the following flowchart:
• replacing filled boxes with empty ones;
• loading objects onto the conveyor;
• placing objects into the box after being counted, etc.
Features:
Purpose:
Humans are not very good at monotonous tasks like counting the number of vehicles passing down a road
over a long period of time. They fall asleep, lose concentration or forget the current total. Microcontrollers
can be much more reliable.
The aim of this program is to count vehicles by counting electronic pulses received from a pressure pad
placed in the road.
When one hundred vehicles have been counted, the system gives out
a loud ‘beep’ and resets to continue the count. All that the human has
to do is press a switch to acknowledge the ‘beep’ and keep a running
total of how many ‘beeps’ have occurred!
Features:
Purpose:
The menu:
• when switch X is pressed, a buzzer sounds;
• when switch Y is pressed, an LED lights.
Two switches, X and Y are attached to individual bits of the input port.
They are used to select from a menu of tasks.
For example:
Features:
• The numbers inside the input port refer to the numbers identifying the bits – switch Y is attached to
bit 1, etc.
• Other sensing units could be attached to the remaining bits of the input port.
• When the full port is ‘read’, as in the present program, the program receives a binary number, eight
bits long in this case. This is stored in the variable called ‘menu’.
• Pressing different switches changes the number stored in the ‘menu’ variable, as the table shows:
Features:
Purpose:
• switch X – initiates the sequence: ‘Red LED on, Green LED on, Blue LED on’;
• switch Y – initiates the same sequence but then repeats it three times;
• switch Z – initiates the same sequence but repeats it five times.
Without the use of the sub-routine, the flowchart would have been much more cumbersome, as the
flowchart on the next page shows.
Purpose:
a)
b) The program receives the first pulse when the front wheel hits the pressure pad at the beginning
of the practice, followed shortly by the second pulse as the rear wheel hits the pressure pad. Both
events increment the variables so that ‘A’ = 2 and ‘B’ = 1 right at the start. The light will come on
after nine laps are completed, not ten.
c) A bicycle has two wheels and so the pressure pad sends two pulses to the lap counter
each time one bicycle passes over it. Variable ‘A’ counts individual pulses – the number of wheels
hitting the pressure pad. Variable ’B’, having half of the value stored in variable ‘A’, records the
number of times a bicycle crosses the pressure pad.
The picture shows a typical mini servo of the type used in toys or models. It is
an assembly of four parts: a DC motor, a gear reduction unit, a potentiometer
and a control circuit.
The control signal from a microcontroller determines the desired angular position of the servo shaft. Power
applied to its DC motor turns the shaft to that position. The signal is in the form of a digital pulse which is
repeated continuously at a frequency of 50 Hz. The length of the pulse, (the ‘mark’, M) determines how
far the motor turns.The ‘space’, S, is determined in order to keep the period of the signal constant, usually
20 ms.
This would give
For example, when M = 1.5 ms, the motor turns to the 90° position. Shorter pulses move it anti-clockwise,
towards the 0° position, while longer pulses turn it clockwise, toward the 180° position. The following graph
illustrates part of this signal when M = 1.5 ms:
Some flowchart control programs include built-in commands to rotate a servo to a desired
position. For example, the icon shown opposite uses a predefined instruction to turn the
servo to an angle of 60°,when the servo is controlled by output pin Q0.
Converting the required angular position into the servo position parameter
In project work you may need to work out the position parameter, ‘k’, to enter into a program to make a
servo turn to a certain angle.
This can be done by entering the required angle into the equation:
angle
Servo Position parameter, k = + 75
1.2
Examples:
Purpose:
In a model production line, a robot arm moves repeatedly between two positions, the first
where the servo motor moves to an angle of 60°. After three seconds, it then moves to an
angle of 120° and waits there for three seconds. The movement then repeats indefinitely.
Features:
• The ‘Servo_125’ command generates a pulse train in which the pulse has a duration of 0.125 ms
with a period of 20 ms.
• The ‘Servo_175’ command generates a pulse train in which the pulse has a duration of 0.175 ms
with a period of 20 ms.
Exercise 6.1
The lights in set ‘X’ are identified as RedX, AmberX and GreenX. Similarly, those in set Y are called RedY,
AmberY and GreenY.
Both sets of lights, ‘X’ and ‘Y’, are controlled and synchronised by a microcontroller-based system.
b) To make the system more ‘intelligent’, sensors are placed on the approaches to the
junction, at the points labelled ‘S’.
The aim is that the lights change to allow the opposite flow of traffic only if a vehicle is detected
approaching the other side of the junction.
The incomplete flowchart given earlier is shown below. Modify it by adding a suitably labelled
Decision box, and flow line(s) to show how a signal from sensor SX could be used to trigger a
change in the traffic lights.
Reed switches are placed on the track before and after the level crossing. A magnet on the
underside of a train operates the first reed switch when the train passes. This causes the barrier to
be lowered through 90° and a lamp to flash on and off continuously.
When the train has passed the level crossing, the second reed switch is triggered, the barrier
returns to the upright position, and the lamp switches off.
……………………………………………………………..
……………………………………………………………..
……………………………………………………………..
……………………………………………………………
……………………………………………………………..
……………………………………………………………..
……………………………………………………………
……………………………………………………………..