Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CHAPTER-1
IntroductionTo Embedded System
CPU
General Serial
MICROCON RAM ROM I/O Timer COM
TROLLERS Port Port
(MCU)-
Purpose
CHAPTER-2
THE 8051 ARCHITECTURE
Pin 18, 19(X2,X1):Internal oscillator input and output. The 8051 has an on chip oscillator but
requires an external clock to run it. Most often a quartz crystal oscillator is connected to
inputs XTAL1 (pin 19) and XTAL2 (pin 18). The quartz crystal oscillator connected to
XTAL1 and XTAL2 also needs two capacitors of 30 pf value. One side of each capacitor is
connected to the ground. Speed refers to the maximum oscillator frequency connected to
XTAL
unconnected. By applying logic zero to this pin, P2 and P3 are used for data and address
transmission with no regard to whether there is internal memory or not. It means that even
there is a program written to the microcontroller, it will not be executed. Instead, the program
written to external ROM will be executed. By applying logic one to the EA pin, the
microcontroller will use both memories, first internal then external (if exists).
Pin 32-39(Port 0): Similar to P2, if external memory is not used, these pins can be used as
general inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE
pin is driven high (1) or as data output (Data Bus) when the ALE pin is driven low (0).
Pin 40(Vcc):+5V power supply.
2.5PORTS 0,1,2,3:
All the ports upon RESET are configured as input, since P0-P3 have value FFH on them. The
following is a summary of features of P0-P3.
PORT 0:
Port 0 is also designated as AD0-AD7 allowing it to be used for both address and data. When
connecting an 8051/31 to an external memory, port 0 provides both address and data. The
8051 multiplexes address and data through port 0 to save pins. ALE indicates if p0 has
address A0-A7.in the 8051 based systems where there is no external memory connection the
pins of P0 must be connected externally to 10k-ohm pull-up resistor. This is due to the fact
that P0 is an open drain, unlike P1, P2 and P3. Open drain is a term used for MOS chips in
the same way that open collector is used for TTL chips. In many systems using the 8751,
89c51 or DS89c4*0 chips we normally connect P0 to pull up resistors.
PORT 1, PORT 2:
In 8051 based systems with no external memory connection both P1 and P2 are used as
simple I/O. however in 8031/51 based systems with external memory connections P2 must be
used along with P0 to provide the 16-bit address for the external memory. P2 is also
designated as A8-A15 indicating its dual function. Since an 8031/51 is capable of accessing
64k bytes of external memory it needs a path for the 16 bits of address. While P0 provides the
lower 8 bits via A0-a7 it is the job P2 to provide bits A8-A15 of the address. In other words
when the 8031/51 is connected to external memory P2 is used for the upper 8 bits of the 16
bit address and it cannot be used for I/O.
PORT 3:
Port 3 occupies a total of 8 pins 10 through 17. It can be used as input or output. P3 does not
need any pull-up resistors the same as P1 and P2 did not. Although port 3 is configured as
input port upon reset this is not the way it is most commonly used. Port 3 has the additional
function of providing some extremely important signals such as interrupts.
P3 Bit Function Pin
P3.0 RXD 10
P3.1 TXD 11
P3.2 INT0 12
P3.3 INT1 13
P3.4 T0 14
P3.5 T1 15
P3.6 WR 16
P3.7 RD 17
Figure2.4:Accumulator Register
2.8 B Register:
B register is same as that of accumulator of. It is also an 8 bit register and every bit of this is
accessible. This is also a mathematical register B which is used mostly for multiplication and
division.
Figure2.5:B register
PSW register is one of the most important SFRs. It contains several status bits that reflect the
current state of the CPU. Besides, this register contains Carry bit, Auxiliary Carry, two
register bank select bits, Overflow flag, parity bit and user-definable status flag.
RS1 (PSW.4) RS0 (PSW.3)
Bank 0 0 0
Bank 1 0 1
Bank 2 1 0
Bank 3 1 1
models belonging to the 8051 family. Locations available to the user occupy memory space
with addresses 0-7Fh, i.e. first 128 registers. This part of RAM is divided in several blocks.
The first block consists of 4 banks each including 8 registers denoted by R0-R7. Prior to
accessing any of these registers, it is necessary to select the bank containing it. The next
memory block (address 20h-2Fh) is bit- addressable, which means that each bit has its own
address (0-7Fh). Since there are 16 such registers, this block contains in total of 128 bits with
separate addresses (address of bit 0 of the 20h byte is 0, while address of bit 7 of the 2Fh byte
is 7Fh). The third group of registers occupy addresses 2Fh-7Fh, i.e. 80 locations, and does not
have any special functions or features.
The first models of the 8051 microcontroller family did not have internal program memory. It
was added as an external separate chip. These models are recognizable by their label
beginning with 803 (for example 8031 or 8032). All later models have a few Kbyte ROM
embedded. Even though such an amount of memory is sufficient for writing most of the
programs, there are situations when it is necessary to use additional memory as well. A
typical example are so called lookup tables. They are used in cases when equations
describing some processes are too complicated or when there is no time for solving them. In
such cases all necessary estimates and approximates are executed in advance and the final
results are put in the tables (similar to logarithmic tables).EA=0In this case, the
microcontroller completely ignores internal program memory and executes only the program
stored in external memory.EA=1In this case, the microcontroller executes first the program
from built-in ROM, then the program stored in external memory.In both cases, P0 and P2 are
not available for use since being used for data and address transmission. Besides, the ALE
and PSEN pins are also used.
2.12.4 Memory Expansion
In case memory (RAM or ROM) built in the microcontroller is not sufficient, it is possible to
add two external memory chips with capacity of 64Kb each. P2 and P3 I/O ports are used for
their addressing and data transmission.From the users point of view, everything works quite
simply when properly connected because most operations are performed by the
microcontroller itself. The 8051 microcontroller has two pins for data read RD(P3.7) and
PSEN. The first one is used for reading data from external data memory (RAM), while the
other is used for reading data from external program memory (ROM). Both pins are active
low.Even though additional memory is rarely used with the latest versions of the
microcontrollers, we will describe in short what happens when memory chips are connected
according to the previous schematic. The whole process described below is performed
automatically.Similar occurs when it is necessary to read location from external RAM.
Addressing is performed in the same way, while read and write are performed via signals
appearing on the control outputs RD (is short for read) or WR (is short for write).
2.13Special Function Registers (SFRs)
Special Function Registers (SFRs) are a sort of control table used for running and monitoring
the operation of the microcontroller. Each of these registers as well as each bit they include,
has its name, address in the scope of RAM and precisely defined purpose such as timer
control, interrupt control, serial communication control etc. Even though there are 128
memory locations intended to be occupied by them, the basic core, shared by all types of
8051 microcontrollers, has only 21 such registers. Rests of locations are intentionally left
unoccupied in order to enable the manufacturers to further develop microcontrollers keeping
them compatible with the previous versions.
CHAPTER-3
Counters and Timers
The TMOD register selects the operational mode of the timers T0 and T1. As seen in figure
below, the low 4 bits (bit0 - bit3) refer to the timer 0, while the high 4 bits (bit4 - bit7) refer
to the timer 1. There are 4 operational modes and each of them is described herein.
TIMER 1 TIMER 0
Figure3.2-TMOD register
Bits of this register have the following function:
GATE1: enables and disables Timer 1 by means of a signal brought to the INT1 pin (P3.3):
1: Timer 1 operates only if the INT1 bit is set.
0:Timer 1 operates regardless of the logic state of the INT1 bit.
C/T1: selects pulses to be counted up by the timer/counter 1:
1: Timer counts pulses brought to the T1 pin (P3.5).
0 1:Timer counts pulses from internal oscillator.
T1M1, T1M0: These two bits select the operational mode of the Timer 1.
GATE0: enables and disables Timer 1 using a signal brought to the INT0 pin (P3.2).
1 :Timer 0 operates only if the INT0 bit is set.
T1M1 T1M0 Mode Description
0 0 0 13-bit timer
0 1 1 16-bit timer
1 0 2 8-bit auto reload
1 1 3 Split mode
Table 3.1: Timer 1
0: Timer 0 operates regardless of the logic state of the INT0 bit.
C/T0: selects pulses to be counted up by the timer/counter 0:
1: Timer counts pulses brought to the T0 pin (P3.4).
0: Timer counts pulses from internal oscillator.
Tom1 T0m0 Mode Description
0 0 0 13-Bit Timer
0 1 1 16-Bit Timer
1 0 2 8-Bitauto Reload
1 1 3 Split Mode
Table 3.2:Timer 0
T0M1, T0M0: These two bits select the operational mode of the Timer 0.
Timer 0 in mode 0 (13-bit timer)
This is one of the rarities being kept only for the purpose of compatibility with the previuos
versions of microcontrollers. This mode configures timer 0 as a 13-bit timer which consists of
all 8 bits of TH0 and the lower 5 bits of TL0. As a result, the Timer 0 uses only 13 of 16 bits.
How does it operate? Each coming pulse causes the lower register bits to change their states.
After receiving 32 pulses, this register is loaded and automatically cleared, while the higher
byte (TH0) is incremented by 1. This process is repeated until registers count up 8192 pulses.
After that, both registers are cleared and counting starts from 0.
Timer 0 in mode 1 (16-bit timer)
Mode 1 configures timer 0 as a 16-bit timer comprising all the bits of both registers TH0 and
TL0. That's why this is one of the most commonly used modes. Timer operates in the same
way as in mode 0, with difference that the registers count up to 65 536 as allowable by the 16
bits.
Timer 0 in mode 2 (Auto-Reload Timer)
Mode 2 configures timer 0 as an 8-bit timer. Actually, timer 0 uses only one 8-bit register for
counting and never counts from 0, but from an arbitrary value (0-255) stored in another
(TH0) register.If mode 1 or mode 0 is used, It is necessary to write the number 200 to the
timer registers and constantly check whether an overflow has occured, i.e. whether they
reached the value 255. When it happens, it is necessary to rewrite the number 200 and repeat
the whole procedure. The same procedure is automatically performed by the microcontroller
if set in mode 2. In fact, only the TL0 register operates as a timer, while another (TH0)
register stores the value from which the counting starts. When the TL0 register is loaded,
instead of being cleared, the contents of TH0 will be reloaded to it. Referring to the previous
example, in order to register each 55th pulse, the best solution is to write the number 200 to
the TH0 register and configure the timer to operate in mode 2.
Timer 0 in Mode 3 (Split Timer)
Mode 3 configures timer 0 so that registers TL0 and TH0 operate as separate 8-bit timers. In
other words, the 16-bit timer consisting of two registers TH0 and TL0 is split into two
independent 8-bit timers. This mode is provided for applications requiring an additional 8-bit
timer or counter. The TL0 timer turns into timer 0, while the TH0 timer turns into timer 1. In
addition, all the control bits of 16-bit Timer 1 (consisting of the TH1 and TL1 register), now
control the 8-bit Timer 1. Even though the 16-bit Timer 1 can still be configured to operate in
any of modes (mode 1, 2 or 3), it is no longer possible to disable it as there is no control bit to
do it. Thus, its operation is restricted when timer 0 is in mode 3.
3.1.2 Timer Control (TCON) Register:
TCON register is also one of the registers whose bits are directly in control of timer
operation. Only 4 bits of this register are used for this purpose, while rest of them is used for
interrupt control to be discussed later.
Figure 3.5:Timer 1
but writing to the SBUF register, while data receive represents reading the same register. The
microcontroller takes care of not making any error during data transmission. In other words,
CHAPTER-4
8051 Microcontroller Interrupts
2.Timer 0 interrupt
3.External Interrupt INT1
4.Timer 1 interrupt
5.Serial Communication Interrupt
4.2 IP Register (Interrupt Priority)
The IP register bits specify the priority level of each interrupt (high or low priority).
7 6 5 4 3 2 1 0
PT2 PS PT1 PX1 PT0 PX0
Figure 4.3: IP Register (Interrupt Priority)
PS: Serial Port Interrupt priority bit
(Priority 0,Priority 1)
PT1: Timer 1 interrupts priority
(Priority 0,Priority 1)
PX1:External Interrupt INT1 priority
(Priority 0,Priority 1)
PT0: Timer 0 Interrupt Priority
(Priority 0,Priority 1)
PX0: External Interrupt INT0 Priority
(Priority 0,Priority 1)
4.3 Handling Interrupt
When an interrupt request arrives the following occurs:
1.Instruction in progress is ended.
2.The address of the next instruction to execute is pushed on the stack.
3.These addresses store appropriate subroutines processing interrupts. Instead of them, there are
usually jump instructions specifying locations on which these subroutines reside.
4.When an interrupt routine is executed, the address of the next instruction to execute is poped from
the stack to the program counter and interrupted program resumes operation from where it left off.
4.4 Reset
Reset occurs when the RS pin is supplied with a positive pulse in duration of at least 2
machine cycles (24 clock cycles of crystal oscillator). After that, the microcontroller
generates an internal reset signal which clears all SFRs, except SBUF registers, Stack Pointer
and ports (the state of the first two ports is not defined, while FF value is written to the ports
configuring all their pins as inputs). Depending on surrounding and purpose of device, the RS
pin is usually connected to a power-on reset push button or circuit or to both of them. Figure
below illustrates one of the simplest circuits providing safe power-on reset.
CHAPTER-5
Interfacing
Interfacing
5.1 LED interfacing
Like a normal diode, an LED consists of a chip of semiconducting material impregnated, or
doped, with impurities to create a p-n junction. As in other diodes, current flows easily from
the p-side, or anode, to the n-side, or cathode, but not in the reverse direction. Charge-
carrierselectrons and holesflow into the junction from electrodes with different voltages.
When an electron meets a hole, it falls into a lower energy level, and releases energy in the
form of a photon. The wavelength of the light emitted, and therefore its color, depends on the
band gap energy of the materials forming the p-n junction. In silicon or germanium diodes, the
electrons and holes recombine by a non- radiative transition which produces no optical
emission, because these are indirect band gap materials. The materials used for an LED have a
direct band gap with energies corresponding to near-infrared, visible or near-ultraviolet light.
LED development began with infrared and red devices made with gallium arsenide. Advances
in materials science have made possible the production of devices with ever-shorter
wavelengths, producing light in a variety of colors. Conventional LEDs are made from a
variety of inorganic semiconductor materials, producing the following colors:
Aluminium gallium arsenide (AlGaAs) red and infrared
Aluminium gallium phosphide (AlGaP) green
Aluminium gallium indium phosphide (AlGaInP) high-brightness
orange-red, orange, yellow, and green
Gallium arsenide phosphide (GaAsP) red, orange-red, orange, and yellow
Gallium phosphide (GaP) red, yellow and green
Gallium nitride (GaN) green, pure green (or emerald green), and blue
also white (if it has an AlGaN Quantum Barrier)
/* PROGRAM TO GLOW LED*/
#include<reg51.h>
void delay()
{
int a;
for(a=0;a<=30000;a++);
}
void main()
{
while(1)
{
P2=0x00;
delay();
P2=0xFF;
delay();
}
5.2 Seven segment display interfacing
Seven Segment
The seven-segment LED display has four individual digits, each with a decimal point. Each
of the seven segments (and the decimal point) in a given digit contains an individual LED.
When a suitable voltage is applied to a given segment LED, current flows through and
illuminates that segment LED. By choosing which segments to illuminate, any of the nine
digits can be shown. For example, as shown in the figure below, a 2 can be displayed by
illuminating segments a, b, d, e, and g. seven segment displays come in two varieties -
common anode (CA) and common cathode (CC). In a CA display, the anodes for the seven
segments and the decimal point are joined into a single circuit node. To illuminate a segment
in a CA display, the voltage on a cathode must be at a suitably lower voltage (about .7V) than
the anode. In a CC display, the cathodes are joined together, and the segments are illuminated
by bringing the anode voltage higher than the cathode node (again, by about .7V). The Dig
lab board uses CA displays.
The seven LEDs in each digit are labeled a-g. Since the
Digilab board uses CA displays, the anodes for each of the
four digits are connected in a common node, so that four
separate anode circuit nodes exist (one per digit).Similar
cathode leads from each digit have also been tied together to
form seven common circuit nodes, so that one node exists for
each segment type. These four anode and seven cathode
circuit nodes are available at the J2connector pins labeled A1-A4 and CA-CG. With this
scheme, any segment of any digit can be driven individually. For example, to illuminate
segments and c in the second digit, the b and c cathode nodes would be brought to a suitable
low voltage (by connecting the corresponding circuit node available at the J2 connector to
ground), and anode 2 would be brought to a suitablehigh voltage (by connecting the
corresponding circuit node available at theJ2 connector to Vdd).
/* PROGRAM TO SWITCH ON SEVEN SEGMENT DISPLAY MOVING FROM LSB TO
MSB */
#include<reg51.h>
void delay()
{
int a;
for(a=0;a<=30000;a++);
}
void main()
{
P2=0x3F;
delay();
P2=0x30;
delay();
P2=0x5B;
delay();
P2=0x1F;
delay();
P2=0x66;
delay();
P2=0x6D;
delay();
P2=0x7C;
delay();
P2=0x07;
delay();
P2=0x7F;
delay();
P2=0x3F;
delay();
}
delay( );
}
CONCLUSION
The basic architecture and function of a microcontroller is discussed. With examples, the
need for low power microcontroller is illustrated.
Microcontrollers are of prime importance for electronic control and communication of any
modern appliance. Any household appliance e.g. washing machine, refrigerator, air-
conditioner or office appliances e.g. electronic printer, Photostat copier, fax machine contains
one or more microcontrollers. Because of its bulk usage a marginal saving in power for one
8051 results to enormous saving as a whole.
In any device design, when one feature is optimized another feature degrades. When low
power is achieved the speed or performance may go down. Though this microcontroller
consumes negligible power its other features are comparable to the existing microcontrollers
available in the market.
BIBLOGRAPHY
Sources:
[1] Chris and Dawn Schurs Robotics and Artificial Life Forms,
http://www.schursastrophotography.com
[2] Wallace, David N., Line Following Robot http://www.lifekludger.net/category/weekly-
links/page/2/
[3] DenmarksTechniskeUniversitethttp://www.sweeper.org
[4], [9] Mikes Line Following Robot, Central Illinois Robotics Club,
http://www.circ.mtco.com
[5] http://www.leang.com
[6] http://www.cs.umn.edu
[7], [8] Jackson, Ben, http://www.ben.com
[10] http://www.ece.unm.edu
[11] David Cooks Jet http://www.robotroom.com/jet.html
[12] http://blog.makezine.com
[13] The CBA Line Following Module http://www.budgetbot.com
[14] http://www.james.vroman.com/tecbot1a.htm james@vroman.com
[15] http://www.james.vroman.com/javbot1a.htm