Final
Final
Multi-Tasking Environment
Generic algorithms had been implemented for
performing parallel tasks. They are meant for
faster processing.
Complex operations like 32 bit and 64-bit addition
and subtraction can be done.
Cannot respond and interact with the outside
analog world.
The cost is high due to memory (RAM, ROM)
availability.
What is Embedded System?
As the name implies, it may be hardware or
software integrated to perform a particular
function.
It uses a Microcontroller/Microprocessor to
perform a single job.
It is a stand-alone device with no operating
system.
Examples may be a washing machine, Music
player, ATM, Vending machine, Data Logger etc.
Nowadays, Most of the devices run on the OS
(Operating System). So, what is the need for an
The user needs smarter devices capable of
doing multiple jobs in less time.
The 8051 has a 8-bit PSW register which is also known as Flag register.
In the 8-bit register only 6-bits are used by 8051.
The two unused bits are user definable bits.
In the 6-bits four of them are conditional flags .
They are Carry –CY,
Auxiliary Carry-AC,
Parity-P,
and Overflow-OV .
These flag bits indicate some conditions that resulted after an
instruction was executed.
The bits PSW3 and PSW4 are denoted as RS0
and RS1 and these bits are used to select the
bank registers of the RAM location.
The meaning of various bits of PSW register is
shown.
CY
»?
STACK in 8051 Microcontroller
The stack is a part of RAM used by the CPU to
store information temporarily. This information
may be either data or an address .
The CPU needs this storage area as there are
only limited number of registers.
The register used to access the stack is called the
Stack pointer which is an 8-bit register..
So,it can take values of 00 to FF H.
When the 8051 is powered up ,the SP register
contains the value 07.i.e the RAM location value
08 is the first location being used for the stack
There are two important instructions to handle this
stack.
One is the PUSH and the Other is the POP.
The loading of data from CPU registers to the
stack is done by PUSH and the loading of the
contents of the stack back into a CPU register is
done by POP.
Example
MOV R6 ,#35 H
MOV R1 ,#21 H
PUSH 6
PUSH 1
Memory organization
The 8051 microcontroller has 128 bytes of
Internal RAM and 4kB of on chip ROM .
The RAM is also known as Data memory and the
ROM is known as program
memory.
The program memory is also known as Code
memory .This Code memory holds the actual
8051 program that is to be executed.
In 8051 this memory is limited to 64K .
Code memory may be found on-chip, as ROM or
Data Pointer Register(DPTR)
It is a 16-bit register which is the only user-
accessible.
DPTR, as the name suggests, is used to point to
data. It is used by a number of commands which
allow the 8051 to access external memory.
When the 8051 accesses external memory it will
access external memory at the address
indicated by DPTR.
This DPTR can also be used as two 8-registers
DPH and DPL.
SPECIAL FUNCTION REGISTERS
(SFRs)
In 8051 microcontroller there certain registers
which uses the RAM addresses from 80h to FFh
and they are meant for certain specific
operations .
These registers are called Special function
registers (SFRs).Some of these registers are bit
addressable also.
The list of SFRs and their functional names are
given below.
In these SFRs some of them are related to I/O
ports (P0,P1,P2 and P3) and some of them are
meant for control operations (TCON,SCON,
PCON..)
Remaining are the auxillary SFRs, in the sense
that they don't directly configure the 8051.
S.No Symbol Name of SFR
Address (Hex)
1 ACC* Accumulator
0E0
2 B* B-Register
0F0
3 PSW* Program Status
word register
0DO
4 SP Stack Pointer
S.No Symbol Name of SFR
Address (Hex)
6 P0* Port 0
80
7 P1* Port 1
90
8 P2* Port 2
0A
9 P3* Port 3
0B
S.No Symbol Name of SFR
Address (Hex)
15 TL0 Timer 0 Lower
byte
8A
16 TH1 Timer 1Higher
byte
8D
17 TL1 Timer 1 lower
Byte
PARALLEL I /O PORTS
The 8051 microcontroller has four parallel I/O
ports , each of 8-bits .
So, it provides the user 32 I/O lines for connecting
the microcontroller to the peripherals.
The four ports are P0 (Port 0), P1(Port1) ,P2(Port
2) and P3 (Port3).
Upon reset all the ports are output ports.
In order to make them input, all the ports must be
set i.e a high bit must be sent to all the port pins.
This is normally done by the instruction “SETB”.
PORT 0
Port 0 is an 8-bit I/O port with dual purpose. If
external memory is used, these port pins are
used for the lower address byte address/data
(AD 0 -AD 7 ), otherwise all bits of the port are
either input or output.
Unlike other ports, Port 0 is not provided with pull-
up resistors internally ,so for PORT0 pull-up
resistors of nearly 10k are to be connected
externally as shown
Dual role of port 0
Port 0 can also be used as address/data
bus(AD0-AD7), allowing it to be used for both
address and data.
When connecting the 8051 to an external
memory, port 0 provides both address and data.
The 8051 multiplexes address and data through
port 0 to save the pins.
ALE indicates whether P0 has address or data.
When ALE = 0, it provides data D0-D7, and
when ALE =1 it provides address and data with
the help of a 74LS373 latch.
Port 1
Port 1 occupies a total of 8 pins (pins 1 through
8). It has no dual application and acts only as
input or output port.
In contrast to port 0, this port does not need any
pull-up resistors since pull-up resistors
connected internally.
Upon reset, Port 1 is configured as an output port.
To configure it as an input port , port bits must be
set i.e a high bit must be sent to all the port pins.
This is normally done by the instruction “SETB”.
Port 2
Port 2 is also an eight bit parallel port. (pins 21-
28). It can be used as input or output port.
As this port is provided with internal pull-up
resistors it does not need any external pull-up
resistors.
Upon reset, Port 2 is configured as an output port.
If the port is to be used as input port,all the port
bits must be made high by sending FF to the
port.
Dual role of port 2
Port2 lines are also associated with the higher
order address lines A8-A15.
In systems based on the 8751, 8951, and
DS5000, Port2 is used as simple I/O port.. But,
in 8051-based systems, port 2 is used along
with P0 to provide the 16-bit address for the
external memory.
Since an 8051 is capable of accessing 64K bytes
of external memory, it needs a path for the 16
bits of the address.
While P0 provides the lower 8 bits via A0-A7, it is
PORT 3
Port3 is also an 8-bit parallel port with dual
function.( pins 10 to 17). The port pins can be
used for I/O operations as well as for control
operations.
The details of these additional operations are
given below in the table.
Port 3 also do not need any external pull-up
resistors as they are provided internally similar
to the case of Port2 & Port 1.
Upon reset port 3 is configured as an output port .
If the port is to be used as input port, all the port
Alternate Functions of Port 3
P3.0 and P3.1 are used for the RxD (Receive
Data) and TxD (Transmit Data) serial
communications signals.
Bits P3.2 and P3.3 are meant for external
interrupts.
Bits P3.4 and P3.5 are used for Timers 0 and 1
and P3.6 and P3.7 are used to provide the write
and read signals of external memories
connected in 8051 based systems
Interrupt Structure
An interrupt is an external or internal event that
disturbs the microcontroller to inform it that a
device needs its service.
The program which is associated with the
interrupt is called the interrupt service routine
(ISR) or interrupt handler.
Upon receiving the interrupt signal the
Microcontroller , finish current instruction and
saves the PC on stack.
Jumps to a fixed location in memory depending
on type of interrupt Starts to execute the
The 8051 microcontroller has FIVE
interrupts in addition to Reset. They
are
• Timer 0 overflow Interrupt
• Timer 1 overflow Interrupt
• External Interrupt 0(INT0)
• External Interrupt 1(INT1)
• Serial Port events (buffer full, buffer empty, etc)
Interrupt
Each interrupt has a specific place in code memory where
program execution (interrupt service
routine) begins.
IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timser 0 interrupt priority bit
IP.0: External interrupt 0 priority bit
TIMERS in 8051 Microcontrollers
The 8051 microcontroller has two 16-bit timers
Timer 0 (T0) and Timer 1(T1) which can be used
either to generate accurate time delays or as
event counters.
These timers are accessed as two 8-bit registers
TLO, THO & TL1 ,TH1 because the 8051
microcontroller has 8-bit architecture.
TIMER 0
The Timer 0 is a 16-bit register and can be treated
as two 8-bit registers (TL0 & TH0) and these
registers can be accessed similar to any other
registers like A,B or R1,R2,R3 etc...
There are two external interrupts EX0 and EX1 to serve external
devices. Both these interrupts are active low. In 8051, P3.2 (INT0)
and P3.3 (INT1) pins are available for external interrupts 0 and 1
respectively.
TF1: Over flow flag for Timer1.
TF1 = 1, Set when timer rolls from all 1s to 0
TF1 = 0, Cleared to execute interrupt service
routine
TR1: Run control bit for timer1.
TR1 =1 Timer1 Turn On
TR1 =0Timer1 Turn Off
TF0: Over flow flag for timer0, same as TF1.
TR0: Runcontrol bit for Timer0, same as TR1.
ADDRESSING MODES OF 8051
The way in which the data operands are accessed
by different instructions is known as the
addressing modes. There are various methods
of denoting the data operands in the instruction.
The 8051 microcontroller supports mainly 5
addressing modes. They are
1.Immediate addressing mode
2.Direct Addressing mode
3.Register addressing mode
4. Register Indirect addressing mode
Immediate addressing mode
The addressing mode in which the data operand
is a constant and it is a part of the instruction
itself is known as Immediate addressing mode.
Normally the data must be preceded by a #
sign. This addressing mode can be used to
transfer the data into any of the registers
including DPTR.
MOV DPTR ,# 8245H :Move the data 8245 into
the data pointer register.
MOV P1,#21 H
Direct addressing mode
The addressing mode in which the data operand
is in the RAM location (00 -7FH) and the
address of the data operand is given in the
instruction is known as Direct addressing mode.
The direct addressing mode uses the lower 128
bytes of Internal RAM and the SFRs
MOV R1, 42H : Move the contents of RAM
location 42 into R1 register
Register addressing mode
The addressing mode in which the data operand
to be manipulated lies in one of the registers is
known as register addressing mode.
MOV A,R0 : Move the contents of the register R0
to the accumulator
Register Indirect addressing mode
The addressing mode in which a register is used
as a pointer to the data memory block is known
as Register indirect addressing mode.
MOV A,@ R0 :Move the contents of RAM location
whose address is in R0 into A
Indexed addressing mode
This addressing mode is used in accessing the
data elements of lookup table entries located in
program ROM space of 8051.
Ex : MOVC A,@ A+DPTR
The 16-bit register DPTR and register A are used
to form the address of the data element stored
in on-chip ROM.
Here C denotes code .In this instruction the
contents of A are added to the 16-bit DPTR
register to form the 16-bit address of the data
operand.
COMMUNICATION
Data transfer between two electronic devices (Ex
Between a computer and microcontroller or a
peripheral device) is generally done in two ways
Disadvantages
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
Time management
Task management
Task creation: create a newTCB
Task termination: remove the TCB
Change Priority: modify the TCB
...
State-inquiry: read the TCB
Task management
Challenges for an RTOS
Creating an RT task, it has to get the memory without
delay: this is difficult because memory has to be
allocated and a lot of data structures, code segment
must be copied/initialized
The memory blocks for RT tasks must be locked in
main memory to avoid access latencies due to
swapping
Changing run-time priorities is dangerous: it may
change the run-time behaviour and predictability of
the whole system
Basic functions of RT OS
Task management
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
Time management
Interrupts
Interrupt: environmental event that demands
attention
Example: “byte arrived” interrupt on serial channel