You are on page 1of 19

CSEN601

Spring 2006

Computer Systems Architecture


Lab Manual

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 1/19


Introduction

What is an embedded system?


It is an application that contains at least one programmable computer, e.g. mobile phones, dishwashers,
washing machines, cars, and in some robots.

In this lab the main Micro Controller Unit (MCU) that will be used is PIC 16F877/16F877A.

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 2/19


MCU Architecture under magnifier
The basic components of PIC16F877 is shown in figure 1.1, most of the components here will be used
throughout the labs.

Figure1. 1 16F877 components

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 3/19


Pins Functions

The pin outs of the 16F877 will be discussed in the lab; Figure 1.2 briefly summarizes the function of each
of the port pins. Note that, in many cases, the port pins can serve more than one purpose.

Figure1. 2 16F877 Pins

Pin(s) Function:-

Pin 1: RESET, this is the reset pin of the PIC 16F877, it is ACTIVE LOW, that means whenever you want
to reset the PIC, you should give a LOW signal on this pin to restart your program.

Pins 1–7: Port A. The pins on this port may be used for digital input and output, as well as analog to digital
conversion (except for RA4).

Pin 8-10: Port E. The pins on this port may be used for digital input and output, as well as analog to digital
conversion.

Pin 11 & pin 32: Vcc, connected to the positive +5v.


Pin 12 & pin 31: Ground pin, connected to the ground of your supply. (0v)

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 4/19


Pins 13-14: These pins are used to connect an external crystal or oscillator module to the microcontroller.

Pin 15–18 & 23-26: Port C. The pins on this port may be used for digital input and output. Also RC1 &
RC2 can be used for PWM [Pulse Width Modulation], RC6 & RC7 for serial communication

Pin 19–22 & 27-30: Port D. The pins on this port may be used for digital input and output.

Pins 33–40: Port B. The pins on this port may be used for digital input and output, also has some week pull
up resistances that can be enabled from your program.

Note: Please always refer back to the PIC datasheet from Microchip.

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 5/19


Reset requirements

The process of starting any microcontroller is a non-trivial one. The underlying hardware is complex and a
small, manufacturer-defined, ‘reset routine’ must be run to place this hardware into an appropriate state
before it can begin executing the user program. Running this reset routine takes time, and requires that the
microcontroller’s oscillator is operating.
Where your system is supplied by a robust power supply, which rapidly reaches its specified output voltage
when switched on, rapidly decreases to 0V when switched off, and – while switched on – cannot ‘brown
out’ (drop in voltage), then you can safely use low-cost reset hardware based on a capacitor and a resistor to
ensure that your system will be reset correctly. Figure 1.3 illustrates the basic connection of the PIC16F877
and a simple reset circuit.

Vcc

1K

1 40

2 39

3 38

4 37
5 36

6 35
7 34
8 33
PIC16F877

9 32
Vcc
10 31 Gnd
Vcc 11 30
22pF Gnd 12 29

13 28

14 27
4MHz
15 26

16 25

22pF 17 24

18 23

19 22

20 21

Figure1. 3 Reset and Oscillator circuit

Clock frequency and performance


All digital computer systems are driven by some form of oscillator circuit: the PIC is no exception.
The oscillator circuit is the ‘heartbeat’ of the system and is crucial to correct operation. For example, if the
oscillator fails, the system will not function at all; if the oscillator runs irregularly, any timing calculations
performed by the system will be inaccurate.

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 6/19


Memory Issues
During the runtime, microcontroller uses two different types of memory: one for holding the program being
executed (Program memory), and the other for temporary storage of data and auxiliary variables (RAM
memory). Depending on the particular model of your PIC, this is usually few kilobytes of ROM and
128/256 bytes of RAM. This amount is built-in and is sufficient for common tasks performed
"independently" by the MCU. However, the PIC can address external memory.

Note that: different types of memory are explained in Appendix A

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 7/19


Lab
Experiments

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 8/19


Lab 4
Experiment 1 – LEDs Blinker/output

Abstract:-
Introduce the PIC to the students through practical experience of writing on each pin of one
of the ports of the PIC and write values to them. Students should be able to access each pin and also access
the whole port.

Circuit diagram:-

Vcc

1K

1 40

2 39

3 38

4 37
5 36

6 35
7 34
8 33
PIC16F877

9 32
Vcc
10 31 Gnd
Vcc 11 30
22pF Gnd 12 29

13 28

14 27
4MHz
15 26

16 25

22pF 17 24

18 23

19 22

20 21

Figure2.1 16F877 connected to 8 LEDs

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 9/19


Program pseudo code:-

Initialize

!"#

$ % &
'!
(

% ) * &
*+

, --
$ % &
, --
$ % &
(
(

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 10/19


Lab 5
Experiment 2 – LEDs control/Input

Abstract:-
Experiment how to let the PIC microcontroller respond to input and observe the result by connecting
LEDs on another port.

Circuit diagram:-

Vcc

1K

1 40

2 39

3 38 PORTB
4 37
5 36

6 35
Vcc 1K 7 34
1K 8 33
PIC16F877

PORTE 1K 9 32
Vcc
10 31 Gnd
Vcc 11 30
22pF Gnd 12 29

13 28

14 27
4MHz
15 26

16 25

22pF 17 24

18 23

19 22

20 21

Figure2.2 16F877 connected to push buttons and LEDs

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 11/19


Program pseudo code:-

*%+
% )) %
. , #

. ,
. ,
. ! ,
(
. #

. , ,
.
. ! ,
(
. ! #

. , ,
. ,
. !
(
(

Notes:

#What happens if the program sets the working bit only and not the other PORTB
pins? Example:
/
. , # . , (
. # . (
. ! # . ! (
/

!#What’s wrong with this code? What happens if you press more that one button at
the same time?

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 12/19


Lab 6
Experiment 3 – Motor Control
Abstract:-
Controlling a motor with the PIC microcontroller using a relay and a transistor. The PIC should
respond to the push button turning the motor on.
The PIC can only sink or source 25mA only, while motors usually draw a lot more current than this,
this is why a relay is used. Yet, driving a relay on and off directly from the PIC is a bad idea, because the
relay contains a coil, the sudden change of turning the relay on or off produces back-EMF (Electro Magnetic
Force) that can destroy the PIC, this is why a transistor is used for protection. [Also to protect the transistor
itself it’s a good practice to use a diode in parallel with the relay’s coil]

Circuit diagram:-

+5v
Vcc
M +9v
1K Relay

330ohm
1 40

2 39
2N2222
3 38

4 37
Vcc 5 36

6 35
1K 7 34
8 33
PIC16F877

9 32
Vcc
10 31 Gnd
Vcc 11 30
22pF Gnd 12 29

13 28

14 27
4MHz
15 26

16 25

22pF 17 24

18 23

19 22

20 21

Figure2.3 16F877 controlling a motor

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 13/19


Program pseudo code:-

#
#

0 % )1
. , #

. 2
(

. ,
(

Notes:

1) What if you want to control the direction of the motor? What would you do?
What component(s) will be added to the circuit and how will it/they be
connected? Hint: check the relay terminals and connections.
2) What happens if we removed the ‘else’ from the program? How will this
affect the behavior of the circuit?

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 14/19


Lab 7
Experiment 4 – Sensors/Motors

Abstract:-

Introduce the usage of sensors with the PIC microcontroller. While sometimes sensors give an
analog output that has to be used with an analog to digital converter, here we’ll learn a simple trick of using
a simple transistor and a potentiometer (variable resistance) to output a digital output to the PIC from the
sensor. Adjusting the potentiometer controls the “sensitivity” of the sensor that’s connected to the transistor
based on the simple rule of voltage divider.

Circuit diagram:-
+5v
Vcc
M +9v
10K Relay

330ohm
1 40

2
2N2222
39

3 38

Vcc Vcc 4 37
5 36

6
LDR 10K
35
7 34

330 8 33
PIC16F877

10k 9 32
Vcc
2N2222
pot 10 31 Gnd
Vcc 11 30

22pF Gnd 12 29

13 28

14 27
4MHz
15 26

16 25
22pF 17 24

18 23

19 22

20 21

Figure2.4 16F877 controlling a motor with sensor

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 15/19


Further explanation:-
Since the LDR (Light Dependent Resistor) changes its resistance according to the light subjected to
it, it can be used to trigger the transistor on or off depending on the light intensity of the environment, and
since the transistor is connected to the PIC it will drive the PIC pin high or low. The potentiometer in series
with the LDR is for adjusting the sensitivity of the LDR to light, when it reaches a certain value (threshold
value) it will give enough current to the ‘Base’ of the transistor to turn it on, otherwise it will not have
enough current and the transistor will stay off.

Program pseudo code:-

#
#

0 % + 1
. , #

. 2
(

. ,
(
(

Notes:

How about using two sensors and mounting your circuit at the bottom of a
robot, two sensors each on one side of a drawn white line on a black floor (or vice
versa, black line on a white floor), and by implementing the right code, your robot
will be able to follow this line.

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 16/19


Appendix A
Types of memory
Since RAM was first introduced, new forms of memory devices have appeared, including various forms of
ROM (read-only memory). Since these ROM devices are also ‘random access’ in nature, the acronym RAM
is now best translated as ‘Read-Write Memory’.
23 Introducing the 8051 microcontroller family

Dynamic RAM (DRAM)


Dynamic RAM is a read-write memory technology that uses a small capacitor to store information. As the
capacitor will discharge quite rapidly, it must be frequently refreshed to maintain the required information:
circuitry on the chip takes care of this refresh activity. Like most current forms of RAM, the information is
lost when power is removed from the chip.

Static RAM (SRAM)


Static RAM is a read-write memory technology that uses a form of electronic flipflop to store the
information. No refreshing is required, but the circuitry is more complex and costs can be several times that
of the corresponding size of DRAM. However, access times may be one-third those of DRAM.

Mask Read-Only Memory (ROM)


Mask ROM is – from the software developer’s perspective – read only: however, the manufacturer is able to
write to the memory, at the time the chip is created, according to a ‘mask’ provided by the company for
which the chips are being produced. Such devices are therefore sometimes referred to as
‘factoryprogrammed ROM’. Mask programming is not cheap, and is not a low-volume option: mistakes can
be very expensive, and providing code for your first mask can be a character-building process. Access times
are often slower than RAM: roughly 1.5 times that of DRAM.
Many members of the 8051 family are available with on-chip, maskprogrammed, ROM.

Programmable Read-Only Memory (PROM)


PROM is a form of Write-Once, Read-Many (WORM) or ‘One-Time Programmable’ (OTP) memory.
Basically, we use a PROM programmer to blow tiny ‘fuses’ in the device. Once blown, these fuses cannot
be repaired; however, the devices themselves are cheap. Many modern members of the 8051 family are
available with OTP ROM.

UV Erasable Programmable Read-Only Memory (UV EPROM)


Like PROMs, UV EPROMs are programmed electrically. Unlike PROMs, they also have a quartz window
which allows the memory to be erased by exposing the internals of the device to UV light. The erasure
process can take several minutes and, after erasure, the quartz window will be covered with a UV-opaque
label. This form of EPROM can withstand thousands of program / erase cycles.

More flexible than PROMs and once very common, UV EPROMs now seem rather primitive compared
with EEPROMs (see below). They can be useful for prototyping but are prohibitively expensive for use in
production.
Many older members of the 8051 family are available with on-board UV EPROM.

EEPROM and Flash ROM


Electrically-Erasable Programmable Read-Only Memory (EEPROMs) and ‘Flash’

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 17/19


ROMs are a more user-friendly form of ROM that can be both programmed and erased electrically.
EEPROM and Flash ROM are very similar. EEPROMs can usually be reprogrammed on a byte-by-byte
basis, and are often used to store passwords or other ‘persistent’ user data. Flash ROMs generally require a
block-sized ‘erase’ operation before they can be programmed: often the size of the block will be several
kilobytes: such ROMs are often used for the storage of program code.
Many members of the 8051 family are available with on-board EEPROM or flash ROM, and some devices
contain both types of memory.

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 18/19


References
[1] Michael J. Pont. “Embedded C” Addison Wesley.
[2] 16F877, 16F877A Datasheet from www.microchip.com

Prepared By: Engy Foda, Khaled Wagdy, Dr.Gamal Fahmy 19/19

You might also like