Unit 1
Unit 1
and IoT
UNIT I 8-BIT EMBEDDED PROCESSOR
8-Bit Microcontroller – Architecture – Instruction Set
and Programming – Programming Parallel
Ports – Timers and Serial Port – Interrupt Handling.
Program Counter(PC):
• A program counter is a 16-bit register .
• The basic function of program counter is to fetch the next instruction to be
executed. The PC increments automatically, holding the address of the next
instruction.
• The I/O ports of the 8051 microcontroller are used to interface with the
outside world. The 8051 microcontroller has four ports, each of which can
be configured as either input or output. The ports are named Port 0, Port 1,
Port 2, and Port 3.
• Port 0 is an 8-bit bidirectional port that can be used to interface with
external devices.
• Port 1 is also an 8-bit bidirectional port that can be used to interface with
external devices.
• Port 2 is an 8-bit port that is used to interface with external memory devices.
• Port 3 is an 8-bit port that is used to interface with external interrupt
sources.
• In addition to the four I/O ports, the 8051 microcontroller also has several
other I/O pins, including the Reset pin, the XTAL1 and XTAL2 pins, and
the ALE (Address Latch Enable) pin. These pins are used for various
purposes, such as system reset, clock input, and address bus control.
Data Memory:
• The data memory of the 8051 microcontroller is used to store data that is
used by the microcontroller during operation. The data memory can be
either internal RAM or external RAM.
• The size of the data memory varies from 128 bytes to 256 bytes for
internal RAM, and up to 64K bytes for external RAM.
• The data memory is divided into two sections: the general-purpose
registers and the SFRs (special function registers).
• The general-purpose registers are used to store data during program
execution, while the SFRs are used to control the operation of the
microcontroller and to interface with external devices.
Program Memory:
The program memory of the 8051 microcontroller is used to store the
program code that the microcontroller executes. The program memory
can be either ROM or Flash memory.
The size of the program memory varies from 2K to 64K bytes,
depending on the specific model of the microcontroller.
SCON (Serial Control) Register:
• SCON Register is an 8 bit register used to select the
programmable mode of 8051.
• The Serial Control or SCON SFR is used to control the 8051
Microcontroller’s Serial Port. It is located as an address of
98H. SCON, control the Operation Modes of the Serial Port.
• SCON Register also consists of bits that are automatically
SET when a byte of data is transmitted or received.
Power Control (PCON) Register:
The PCON register is used for power control and baud rate selection.It also
consists of general purpose user flags.
• 8-bit register used to select timer mode. There are 4 modes in which timer can be
loaded.
1. Mode 0 : 13-bit timer C/T – 0 TIMER
2. Mode 1: 16-bit timer - 1 Counter
3. Mode 2 : 8-bit auto reload
4. Mode 3 : Split timer mode
• TH0/TL0->Timer 0 :16 bit register (byte addressable only) 8 bits can be send at a
time.
• TH1/TL1-> Timer 1: 16 bit register (byte addressable only) 8 bits can be send at a
time.
IE (Interrupt Enable) Register:
• This register is responsible for enabling and disabling the
interrupt. EA register is set to one for enabling interrupts
and set to 0 for disabling the interrupts. Its bit sequence
and their meanings are shown in the following figure.
IP (Interrupt Priority) Register:
• We can change the priority levels of the interrupts by changing
the corresponding bit in the Interrupt Priority (IP) register as
shown in the following figure.
• A low priority interrupt can only be interrupted by the high
priority interrupt, but not interrupted by another low priority
interrupt.
• If two interrupts of different priority levels are received
simultaneously, the request of higher priority level is served.
• If the requests of the same priority levels are received
simultaneously, then the internal polling sequence determines
which request is to be serviced.
Timer and Control Unit:
• This microcontroller includes two timers where each timer is 16-bit where
the system can generate two delays concurrently to produce the suitable
delay.
• The delay can be generated through the timer based on the requirement of
the processor & transmits the signal to the processor whenever the particular
delay gets generated.
Interrupts:
Interrupts is a sub-routine call that given by the microcontroller
when some other program with high priority is request for
acquiring the system buses the n interrupts occur in current
running program.
• Pin 40 (VCC) – This pin provides power supply voltage i.e. +5 Volts to
the circuit.
Addressing modes of 8051
MOV A, #0AFH;
MOV R3, #45H;
MOV DPTR, #FE00H
• MOVC A, @A+PC;
• MOVC A, @A+DPTR;
Implied Addressing Mode:
• In the implied addressing mode, there will be a single operand.
These types of instruction can work on specific registers only.
These types of instructions are also known as register specific
instruction. Here are some examples of Implied Addressing
Mode.
RLA;
SWAP A;
Types of instructions:
• Arithmetic Instructions
• Logical Instructions
• Data exchange
• Bit-oriented Instructions
• Branch Instructions
Data Transfer Instructions:
• Data transfer instructions move the content of one register to
another.
• The register the content of which is moved remains
unchanged.
• Data can be transferred within the internal Memory/to and
from external RAM.
• If they have the suffix “X” (MOVX), the data is exchanged
with external memory.
Data Transfer Instructions:
• MOV <destination><Source>
Used to move data from the source to destination
• MOVX <destination><Source>
To access External data memory (RAM)
• MOVC <destination><Source>
To access External ROM/EPROM
(Program Memory)-Code
Addition Subtraction
• SUBB A, <Source>
• ADD A, <Source>
[A] [A]-[SOURCE]-CY
Example: ADD A,#05H
Example:
[A] [A]+05
SUBB A,R0
ADD A,R1
SUBB A,@R1
• ADDC A, <Source> Add with Carry
SUBB A,#15H
[A] [A]+[SOURCE]+CY
Example: ADDC A,25H
Multiplication Division
• MUL AB
• [A]= Lower Byte of AB • DIV AB
Increment Decrement
• INC A • DEC A
Increment the content of A by 1 Decrement the content of A by 1
[A]= [A]+1 [A]= [A]-1
• ORL <destination>,<source>
• ANL <destination>,<source>
• Example:
• Example:
• ORL A,#15H
• ANL A,#15H
• ORL A,R1
• ANL A,R1
• ORL A,20H
• ORL A,@R1
Clear the Accumulator Complement the Accumulator
• CLR A: • CPL A:
Rotate Instruction
SWAP A
• RL A: Rotate the Accumulator left
• RLC A: Rotate the content of ‘A’ • Swap the nibbles within the
towards left with carry. Accumulator
• RR A: Rotate Right
• RRC A: Rotate Right with Carry
• CLR A: • CPL A:
• RL A: Rotate the Accumulator left
• RR A: Rotate Right
• RRC A: Rotate Right with Carry
This instructions are used to set and reset the particular bit in a Internal Random
Access memory.
• CLR = Clear the Carry Flag/ any bit in a internal data memory.
• SETB = Set the carry Flag/ any bit in a RAM.
• CPL = Complement the bit
• ANLC , <Source - Bit>= Logical AND operation for bit variables
Perform the logical AND operation for the bit variables of this given source bit
and the Carry Flag.
• ORLC, < Source - Bit >= Logical OR operation for bit variables
Perform the logical OR operation for the given source bit and then Carry Flag.
• MOV <Destination Bit ><Source Bit>
Bit Level Logical Instructions:
JUMP: CALL:
It permanently changes the program It temporarily changes the program flow
flow. to allow the subroutine program.
JUMP:
• AJUMP <address 11 bits> = Absolute Jump-Calls a subroutine
located at the specified 11-bit address.
CALL Instructions
• LCALL= Long Call
• ACALL = Absolute Call
Programming Parallel Ports
• The area in which input and output devices are connected
are called I/O ports.
• Serial Port : Bit by Bit
• Parallel Port : Group of bits
• 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”.
Ex: MOV A,#0FFH ; A = FF
MOV P0,A ; make P0 an input port
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 (AD0-AD7),
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.
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
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”.
Ex:
MOV A, #0FFH; A=FF HEX
MOV P1, A; make P1 an input port by writing 1’s to
all of its pins
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.
For Ex:
MOV A, #0FFH ; A=FF hex
MOV P2, A ; make P2 an input port by writing all
1’s to it
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 bits must be made high by sending FF to the
port.
• After the timer is started, it starts count up until it reaches its limit of FFFFH.
When it rolls over from FFFF to 0000H, it sets high a flag bit called TFx
(timer flag).
• This timer flag can be monitored. When this timer
flag is raised, one option would be stop the timer with
the instructions “CLR TR0“ or CLR TR1 for timer 0
and timer 1 respectively. Again, it must be noted that
each timer flag TF0 for timer 0 and TF1 for timer1.
• After THx is loaded with 8 bit value, the 8051 gives a copy of
it to TLx. Then the timer must be started. It is done by the
instruction “SETB TR0” for timer 0 and “SETB TR1” for
timer1. This is like mode 1.
MODE 2…
• After timer is started, it starts to count up by incrementing the TLx
register. It counts up until it reaches its limit of FFH. When it rolls
over from FFH to 00, it sets high the TFx (timer flag).
If we are using timer 0, TF0 goes high; if using TF1 then TF1 is raised.
Given:
Square wave frequency=5 kHz
Clock frequency=11.0592 MHz
Step 1: Calculate the Time delay
T=1/f=1/5 kHz =0.2 ms
T=0.2 ms which is the period of square wave
T/2 =0.2/2=0.1 ms delay for high and low
Step 2: Divide the desired time delay by 1.085 us
Count=0.1ms/1.085 us = 921
Step 3: Perform 65536 – n
TH0-TL0= 65536-921=64615=FC67H
MOV TMOD,#01 ;Timer 0, mode 1, 16-bitmode
AGAIN: MOV TL1,#67H ;TL1=67, low byte of timer
MOV TH1,#0FCH ;TH1=FC, the high byte
9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8
Write a program for the 8051 to transfer letter ‘A’
serially a 4800 baud rate continuously.
• It is just like a call you have when you are busy with some work and depending upon
• 8051 architecture handles 5 interrupt sources, out of which two are internal (Timer
• The highest priority interrupt in 8051 Microcontroller is the Reset, with vector
address 0x0000.
Vector Address: This is the address where the controller jumps after
the interrupt to serve the ISR (interrupt service routine).
Reset
Reset is the highest priority interrupt, upon reset 8051 microcontroller start executing code
8051 mcu has two internal interrupts namely timer0 and timer1. Whenever timer
overflows, timer overflow flags (TF0/TF1) are set. Then the microcontroller jumps to their
vector address to serve the interrupt. For this, global and timer interrupt should be enabled.
8051 mcu has a serial communication port and have related serial interrupt flags (TI/RI).
When the last bit (stop bit) of a byte is transmitted, the TI serial interrupt flag is set, and
when the last bit (stop bit) of the receiving data byte is received, the RI flag gets set.
IE register: Interrupt Enable Register
IE register is used to enable/disable interrupt sources.
In the table, interrupts priorities upon reset are shown. As per 8051
interrupt priorities, the lowest priority interrupts are not served
until the microcontroller is finished with higher priority ones. In a
case when two or more interrupts arrive microcontroller queues
them according to priority.
IP Register: Interrupt priority register
8051 has an interrupt priority register to assign priority to interrupts.