0% found this document useful (0 votes)
10 views109 pages

Unit 1

The document outlines a course on Embedded Systems and IoT, covering topics such as 8-bit microcontroller architecture, embedded C programming, IoT device configurations, communication protocols, and applications development. It includes detailed information on microprocessors and microcontrollers, their architectures, and key components like ALU, registers, and I/O ports. The course aims to equip students with the skills to design and implement embedded and IoT systems using platforms like Arduino and Raspberry Pi.

Uploaded by

953622243011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views109 pages

Unit 1

The document outlines a course on Embedded Systems and IoT, covering topics such as 8-bit microcontroller architecture, embedded C programming, IoT device configurations, communication protocols, and applications development. It includes detailed information on microprocessors and microcontrollers, their architectures, and key components like ALU, registers, and I/O ports. The course aims to equip students with the skills to design and implement embedded and IoT systems using platforms like Arduino and Raspberry Pi.

Uploaded by

953622243011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 109

CS3691 Embedded Systems

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.

UNIT II EMBEDDED C PROGRAMMING


Memory And I/O Devices Interfacing –Programming
Embedded Systems in C – Need For RTOS Multiple
Tasks and Processes – Context Switching – Priority
Based Scheduling Policies.
UNIT III : IOT AND ARDUINO PROGRAMMING
Introduction to the Concept of IoT Devices – IoT Devices Versus
Computers – IoT Configurations – Basic Components – Introduction to
Arduino – Types of Arduino – Arduino Toolchain – Arduino
Programming Structure – Sketches – Pins – Input/Output From Pins
Using Sketches – Introduction to Arduino Shields – Integration of
Sensors and Actuators with
Arduino.

UNIT IV: IOT COMMUNICATION AND OPEN PLATFORMS


IoT Communication Models and APIs – IoT Communication Protocols –
Bluetooth – WiFi – ZigBee – GPS – GSM modules – Open Platform (like
Raspberry Pi) – Architecture – Programming – Interfacing – Accessing
GPIO Pins – Sending and Receiving Signals Using GPIO Pins –
Connecting to the Cloud.
UNIT V: APPLICATIONS DEVELOPMENT
Complete Design of Embedded Systems – Development of IoT
Applications – Home Automation– Smart Agriculture – Smart
Cities – Smart Healthcare.
TEXTBOOK
1. Muhammed Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay,
“The 8051 icrocontroller and Embedded Systems”, Pearson Education,
Second Edition, 2014
2. Robert Barton, Patrick Grossetete, David Hanes, Jerome Henry, Gonzalo
Salgueiro, “IoT Fundamentals: Networking Technologies, Protocols, and
Use Cases for the Internet of Things”, CISCO Press, 2017.
REFERENCES :
1. Michael J. Pont, “Embedded C”, Pearson Education, 2007.
2. Wayne Wolf, “Computers as Components: Principles of Embedded
Computer System Design”, Elsevier, 2006.
3. Andrew N Sloss, D. Symes, C. Wright, “Arm System Developer’s
Guide”, Morgan Kauffman/ Elsevier, 2006.
4. Arshdeep Bahga, Vijay Madisetti, “Internet of Things – A hands-on
approach”, Universities Press, 2015
COURSE OBJECTIVES:
• To learn the internal architecture and programming of
an embedded processor.
• To introduce interfacing I/O devices to the processor.
• To introduce the evolution of the Internet of Things
(IoT).
• To build a small low-cost embedded and IoT system
using Arduino/Raspberry Pi/ open platform.
• To apply the concept of Internet of Things in real world
scenario.
What is a Microprocessor?
• It is a processing device that converts data into
information based on some sets of instructions. It is a
very compact electronic chip due to which it is referred
to as the microprocessor.
OR
• A microprocessor is a computer processor for which the
data processing logic and control is included on a single
integrated circuit (IC), or a small number of Ics

• The microprocessor is used as the CPU (Central


Processing Unit). A typical microprocessor consists of
two major parts namely ALU (Arithmetic Logic Unit)
and CU (Control Unit). Intel 8085 or 8086 processing
What is a Microcontroller?
• A microcontroller is an electronic system which
consists of a processing element, a small memory
(RAM, ROM, EPROM), I/O ports, etc. on a single
chip.

• In electronic systems such washing machines,


air conditioners, refrigerators, etc.,
microcontrollers are used to automate the
operation of the device based on user’s
instructions.
S.No Microprocessor Microcontroller
Microprocessor acts as a heart of Microcontroller acts as a heart of
1 computer system. embedded system.
It is a processor in which memory and It is a controlling device in which memory
2 I/O output component is connected and I/O output component is present
externally. internally.
Since memory and I/O output is to be Since on chip memory and I/O output
3 connected externally. Therefore the component is available. Therefore the
circuit is more complex. circuit is less complex.

It cannot be used in compact system. It can be used in compact system.


4 Therefore microprocessor is Therefore microcontroller is more
inefficient. efficient.
Microprocessor has less number of Microcontroller has more number of
5 registers. Therefore most of the registers. Therefore a program is easier to
operations are memory based. write.
A microprocessor having a zero status A microcontroller has no zero flag.
6 flag.
It is mainly used in personal It is mainly used in washing machines, air
7 computers. conditioners etc.
On the basis of architecture the types of
microcontroller are:
Von Neumann architecture:
• In a Von Neumann architecture, instructions and data are
stored in a single memory system. This memory is usually
implemented as random access memory (RAM) and is
connected to the CPU and I/O devices through a common bus.
Harvard Architecture:
•Harvard architecture is a type of computer architecture that has
separate memory spaces for instructions and data.

•In a Harvard architecture system, the CPU accesses instruction


and data memory spaces separately, which can lead to improved
performance.
8051 Microcontroller Architecture:
Key features of the 8051 Microcontroller:

• 4 KB on-chip ROM (Program memory).


• 128 bytes on-chip RAM (Data memory).
• The 8-bit data bus (bidirectional).
• 16-bit address bus (unidirectional).
• Two 16-bit timers.
• Instruction cycle of 1 microsecond with 12 MHz crystal.
• Four 8-bit input/output ports.
• 128 user-defined flags.
• Four register banks of 8 bit each.
• 16-byte bit-addressable RAM.
• The general purpose registers are 32 each is 8-bit.
• 8051 has two external and three internal interrupts.
• 8051 microcontroller specifies some special function features like UARTs,
ADC, Op-amp, etc.
•It has a 16-bit program counter and data pointer.
Arithmetic and Logic Unit (ALU):
• All arithmetic and logical functions are carried out by the ALU.
• Addition, subtraction with carry, and multiplication come under arithmetic
operations.
• Logical AND, OR and EXOR come under logical operations.

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.

Accumulator (A) and B Registers:


• Registers are usually known as data storage devices. 8051 microcontroller has 2
registers, namely Register A and Register B. These registers are used to
store the output of mathematical and logical operations. B register is mainly
used for multiplication and division operation along with A register.
• The Accumulator or Register A: is the most important and
most used 8051 Microcontroller SFRs.
• The Accumulator is used to hold the data for almost all the
ALU Operations.
• Some of the operations where the Accumulator is used are:
• Arithmetic Operations like Addition, Subtraction,
Multiplication etc.
• Logical Operations like AND, OR, NOT etc.
• Data Transfer Operations (between 8051 and External
Memory)
Stack pointer (SP):
The stack pointer in the 8051 holds the 8-bits address at
the top of the stack, it supports the LIFO(Last In First
Out) and it can take a value of 00 to FFH. The stack can
be accessed using PUSH and POP instructions.

DPTR (Data pointer) Register:


DPTR is a 16 bit register used to hold the 16 bit
address of data memory. The 16 bit data pointer can
also be used as two 8 bit data pointer namely DPH,
DPL. 8 bit data pointer used to accessing RAM and
SFR.
Bus :
Bus is a group of wires which uses as a communication canal or
acts as means of data transfer. The different bus configuration
includes 8, 16 or more cables. Therefore, a bus can bear 8 bits, 16
bits all together.
Types of buses in 8051 Microcontroller:
Let's see the two types of bus used in 8051 microcontroller:

Address Bus: 8051 microcontrollers is consisting of 16 bit address


bus. It is generally be used for transferring the data from Central
Processing Unit to Memory.

Data bus: 8051 microcontroller is consisting of 8 bits data bus. It is


generally be used for transferring the data from one peripherals
position to other peripherals.
Program Status Word Register (PSW):
• Program Status Word Register is also called as Flag Register and
is one of the important Special function Register (SFRs).
• The PSW Register consists of Flag Bits, which help the
programmer in checking the condition of the result and also make
decisions.
• Flags are 1-bit storage elements that store and indicate the nature
of the result that is generated by execution of certain instructions.
The following image shows the contents of the PSW Register.
Program Status Word Register (PSW)…
• Parity Bit (P)
– This parity flag bit is used to show the number of 1s in the accumulator
only. If the accumulator register contains an odd number of 1s, then this
flag set to 1.
– If accumulator contains even number of 1s, then this flag cleared to 0.
• Overflow Flag (OV)
– This flag is set during ALU operations, to indicate overflow in the result. It
is set to 1 if there is a carry out of either the D7 bit or the D6 bit of the
accumulator.
– Overflow flag is set when arithmetic operations such as add and subtract
result in sign conflict.
Register Bank Select Bits (RS1 And RS0)
– These two bits are used to select one of four register banks of RAM. By
setting and clearing these bits, registers R0-R7 are stored in one of four
banks of RAM as follows.
Program Status Word Register (PSW)…

• General-Purpose Flag (F0)


– This is a user-programmable flag; the user can program and
store any bit of his/her choice in this flag, using the bit
address.

• Auxiliary Carry Flag (AC)


– It is used in association with BCD arithmetic. This flag is set
when there is a carry out of the D3 bit of the accumulator.

• Carry Flag (CY)


– This flag is used to indicate the carry generated after arithmetic
operations. It can also be used as an accumulator, to store one
of the data bits for bit-related Boolean instructions.
Input/Output (I/O) Ports:

• 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.

Serial baud rate Modified bit


SMOD=0 at reset
SMOD=1 by the program double the baud rate
IDLE MODE(Stops CPU but keeps timers and serial communication running)
In this mode the clock is cut from the processor only. In this mode 80% of power
is saved. The 20% power is used to get the microcontroller from idle mode to
normal mode. microcontroller resumes operation after an interrupt

POWER DOWN MODE:


In this mode, clock supply is cut from the whole system. In this power saving
mode maximum amount of power is saved. All register values are retained, but
RAM and peripherals are disabled. To wake up, a hardware reset is required.
TCON (Timer Control register):
TCON is an 8-bit register. Its bits are used for generating
interrupts internal or external. The most important bits of the
timer TR and TF are also in it. TR (timer run) and TF (timer
overflow) bits which we use in almost all over timer
applications are in it.
• The TCON (Timer Control Register) is an 8-bit
.
Special Function Register (SFR) in the 8051
microcontroller, responsible for controlling
timer operations and external interrupts
TMOD (Timer Mode register):
The TMOD register is used to select the operating mode and the timer/Counter
operation. The Lower 4 bits used to control timer 0 and the upper two bits used
to control timer 1.

• 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:

The Timer and Control Unit in the 8051 microcontroller


is responsible for managing timing operations, counting
external events, and generating precise delays.

• 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.

Types of interrupt in 8051 Microcontroller:


• Timer 0 overflow interrupt - TF0
• Timer 1 overflow interrupt - TF1
• External hardware interrupt - INT0
• External hardware interrupt - INT1
• Serial communication interrupt - RI/TI
8051 microcontroller Pin Diagram :
8051 microcontroller is a 40
pin Dual Inline Package
(DIP). These 40 pins serve
different functions like read,
write, I/O operations,
interrupts etc.

8051 has four I/O ports


wherein each port has 8 pins
which can be configured as
input or output depending
upon the logic state of the
pins. Therefore, 32 out of
these 40 pins are dedicated to
I/O ports. The rest of the pins
are dedicated to VCC, GND,
XTAL1, XTAL2, RST, ALE,
EA’ and PSEN
•Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other
functions. It is internally pulled up, bi-directional I/O port.
•Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial
values.
•Pins 10 to 17 − These pins are known as Port 3. This port serves some functions
like interrupts, timer input, control signals, serial communication signals RxD and
TxD, etc.
•Pins 18 & 19 − These pins are used for interfacing an external crystal to get the
system clock.
•Pin 20 − This pin provides the power supply to the circuit.
•Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher
order address bus signals are also multiplexed using this port.
•Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to
read a signal from the external program memory.
•Pin 30 − This is EA pin which stands for External Access input. It is used to
enable/disable the external memory interfacing.
•Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to
demultiplex the address-data signal of port.
•Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower
order address and data bus signals are multiplexed using this port.
•Pin 40 − This pin is used to provide power supply to the circuit.
• Port 0 (P0.1 – P0.7) - Pin 32 to Pin 39 : This is an 8-bit
bidirectional input/output pins. The output drives and inputs buffers
of port 0 are used to access external memory.
• Port 1 (P1.0-P0.7) - Pin 1 to Pin 8 – Pin 1 to Pin 8 are assigned to
Port 1 for simple I/O operations. They can be configured as input or
output pins depending on the logic control i.e. if logic zero (0) is
applied to the I/O port it will act as an output pin and if logic one (1)
is applied the pin will act as an input pin. These pins are also
referred to as P1.0 to P1.7
• // (where P1 indicates that it is a pin in port 1 and the number after
‘.’ tells the pin number i.e. 0 indicates first pin of the port. So, P1.0
means first pin of port 1, P1.1 means second pin of the port 1 and so
on). These pins are bidirectional pins.//
• Port 2 (Pin 21 to Pin 28) – Pin 21 to pin 28 are port 2 pins also
referred to as P2.0 to P2.7. When additional external memory is
interfaced with the 8051 microcontroller, pins of port 2 act as
higher-order address bytes. These pins are bidirectional.
Port 3 (Pin 10 to Pin 17)– Pin 10 to pin 17 are port 3 pins which are also referred to
as P3.0 to P3.7. These pins are similar to port 1 and can be used as universal input or
output pins. These pins are bidirectional pins. These pins also have some additional
functions which are as follows:
– P3.0 (RXD) : 10th pin is RXD (serial data receive pin) which is for serial
input. Through this input signal microcontroller receives data for serial
communication.
– P3.1 (TXD) : 11th pin is TXD (serial data transmit pin) which is serial output
pin. Through this output signal microcontroller transmits data for serial
communication.
– P3.2 and P3.3 (INT0′, INT1′ ) : 12th and 13th pins are for External Hardware
Interrupt 0 and Interrupt 1 respectively. When this interrupt is activated(i.e.
when it is low), 8051 gets interrupted in whatever it is doing and jumps to the
vector value of the interrupt (0003H for INT0 and 0013H for INT1) and starts
performing Interrupt Service Routine (ISR) from that vector location.
– P3.4 and P3.5 (T0 and T1) : 14th and 15th pin are for Timer 0 and Timer 1
external input. They can be connected with 16 bit timer/counter.
– P3.6 (WR’) : 16th pin is for external memory write i.e. writing data to the
external memory.
– P3.7 (RD’) : 17th pin is for external memory read i.e. reading data from
external memory.
• Pin 9 (RST) – Reset pin. It is an active-high, input pin. Therefore if
the RST pin is high for a minimum of 2 machine cycles, the
microcontroller will reset i.e. it will close and terminate all
activities. It is often referred as “power-on-reset” pin because it is
used to reset the microcontroller to it’s initial values when power is
on (high).
• Pin 18 and Pin 19 (XTAL2 And XTAL1) – These pins are
connected to an external oscillator which is generally a quartz
crystal oscillator. They are used to provide an external clock
frequency of 4MHz to 30MHz.
• Pin 20 (GND) – This pin is connected to the ground. It has to be
provided with 0V power supply. Hence it is connected to the
negative terminal of the power supply.
• Pin 29 (PSEN) – PSEN stands for Program Store Enable. It is
output, active-low pin. This is used to read external memory. In
8031 based system where external ROM holds the program code,
this pin is connected to the OE pin of the ROM.
• Pin 30 (ALE/ PROG) – ALE stands for Address Latch Enable. It is
input, active-high pin. This pin is used to distinguish between memory
chips when multiple memory chips are used. It is also used to de-
multiplex the multiplexed address and data signals available at port 0.
During flash programming i.e. Programming of EPROM, this pin acts as
program pulse input (PROG).

• Pin 31 (EA/ VPP) – EA stands for External Access input. It is used to


enable/disable external memory interfacing. In 8051, EA is connected to
VCC as it comes with on-chip ROM to store programs.

• Pin 40 (VCC) – This pin provides power supply voltage i.e. +5 Volts to
the circuit.
Addressing modes of 8051

The way in which an operand is given to an instruction is known


as addressing modes. Different addressing modes of the 8051
microcontrollers. In 8051 there are 1-byte, 2-byte instructions
and very few 3-byte instructions are present. The opcodes are 8-
bit long. As the opcodes are 8-bit data, there are 256 possibilities.

• Immediate Addressing Mode


• Register Addressing Mode
• Direct Addressing Mode
• Register Indirect Addressing Mode
• Indexed Addressing Mode
• Implied Addressing Mode
Immediate addressing mode :
• In this Immediate Addressing Mode, the data is provided in
the instruction itself. The data is provided immediately after
the opcode. These are some examples of Immediate
Addressing Mode.

MOV A, #0AFH;
MOV R3, #45H;
MOV DPTR, #FE00H

• In these instructions, the # symbol is used for immediate


data. In the last instruction, there is DPTR. The DPTR stands
for Data Pointer. Using this, it points the external data
memory location
Register addressing mode :
• In the register addressing mode the source or destination
data should be present in a register (R0 to R7). These are
some examples of Register Addressing Mode.
ADD A, Rn (This is general instruction).
• ADD A, R5 (This instruction will add the contents of register
R5 with the accumulator contents).
• MOV A, R5;
• MOV R2, #45H;
Direct Addressing Mode :
In the Direct Addressing Mode, the source or
destination address is specified by using 8-bit data in
the instruction. Only the internal data memory can be
used in this mode. Here some of the examples of direct
Addressing Mode.

• MOV A, 25H (This instruction will read/move the


data from internal RAM address 25H and store it in
the accumulator.)
• MOV R2, 45H
Register indirect addressing Mode:
• In this mode, the source or destination address is given in the
register. By using register indirect addressing mode, the
internal or external addresses can be accessed. The R0 to R7
are used for 8-bit addresses, and DPTR is used for 16-bit
addresses, no other registers can be used for addressing
purposes. Let us see some examples of this mode.

• MOV A,@R0 This instruction moves the data from the


register whose address is in the R0 register into the
accumulator.
• MOV 0E5H,@R0;
• MOV @R1,80H
• In the instructions, the @ symbol is used for register indirect
addressing.
Indexed addressing mode:
• In the indexed addressing mode, the source memory can
only be accessed from program memory only. The
destination operand is always the register A. These are
some examples of Indexed addressing mode.

• 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:

Depending on operation they perform, all instructions are divided in several


groups:

• Data Transfer Instructions

• Arithmetic Instructions

• Logical Instructions

• Data exchange

• Push and Pop Instructions

• Rotate and Swap Instructions

• 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

Data Transfer with Stack


• PUSH<Source> Push onto the stack
• POP< destination > POP from the Stack

Data Exchange Instruction:


• XCH Example: XCH A,R0
• XCH D
Data Transfer Instructions…
Instruction to Access External Data Memory:
Arithmetic Instructions:

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

• [B]= Higher Byte of AB • [A]= Quotient of A/B


• [B]= Reminder of A/B

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

Decimal Arithmetic Instruction


• DA A: Decimal Adjust Accumulator for BCD addition
Byte Logical Instructions :
Logical OR Instruction
Logical AND Instruction

• 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

Clear the Accumulator Complement the Accumulator

• CLR A: • CPL A:
• RL A: Rotate the Accumulator left

• RR A: Rotate Right
• RRC A: Rotate Right with Carry

• RLC A: Rotate the content of ‘A’ towards left with carry.


SWAP A
Logical Instructions :
Bit Level Logical Instructions:

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:

Push and Pop Instructions:


Data Exchange instructions:
Branch instructions:
These instructions are used to exchange the flow of the program by changing
the content of PC.

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.

• LJMP < address 16 bits > = Long Jump-Calls a subroutine


located at the specified 16-bit address. The address range

• SJMP <address> = Short Jump


These are Jump instruction without any condition
Jump With Conditions

• JZ Address: Jump if Accumulator is Zero.


• JNZ Address: Jump if Accumulator is not Zero.
• JC Address: Jump if Carry Flag is set.
• JNC Address: Jump if CY=0.
• JB bit, rel: Jump if the direct bit is set.
• CJNE <destination>,<source> = Compare and Jump if not equal.
• DJNZ = Decrement and Jump if not zero.
• NOP= No Operation.

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.

• MOV A, #0FFH ; A= FF hex


• MOV P3, A ; make P3 an input port by writing all 1’s to it
TIMER’S CLOCK FREQUENCY AND ITS PERIOD
• In 8051-based system, the crystal oscillator has a frequency of
11.0592 MHz when C/T bit of TMOD is 0. Each machine
cycle is made up of 12 clock cycles.
• Hence for a single machine cycle, the frequency becomes
1/12 × 11.0529 MHz = 921.6 KHz.
• For a single machine cycle, the time taken is
T = 1/921.6 KHz = 1.085 us
• so the oscillator takes 1.085us for completing a single machine
cycle.
Timers and Serial Port
MODE 0 :
• Mode 0 is exactly same like mode 1 except that it is a
13-bit timer instead of 16-bit. The 13- bit counter can
hold values between 0000 to 1FFFH in TH-TL.
• Therefore, when the timer reaches its maximum of
1FFH, it rolls over to 0000, and TF is raised.
MODES OF OPERATION:
MODE 1:
• It is a 16-bit timer; therefore it allows values from 0000 to FFFFH to be loaded
into the timer’s registers TL and TH as shown in Fig.
• After TH and TL are loaded with a 16-bit initial value, the timer must be
started. We can do it by “SETB TR0” for timer 0 and “SETB TR1” for timer 1.

• 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 the timer reaches its limit and rolls over, in


order to repeat the process the registers TH and TL
must be reloaded with the original value and TF must
be reset to 0.
MODE 2:
• It is an 8 bit timer that allows only values of 00 to FFH to be
loaded into the timer’s register TH as shown in Figure.

• 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.

• When TLx register rolls from FFH to 00 and TF is set to 1, TLx is


reloaded automatically with the original value kept by the THx
register.

• To repeat the process, we must simply clear TFx and let it go


without any need by the programmer to reload the original value.
This makes mode 2 auto reload, in contrast in mode 1 in which
programmer has to reload THx and TLx.
MODE 3:
• Mode 3 is also known as a split timer mode. Timer 0
and 1 may be programmed to be in mode 0, 1 and 2
independently of similar mode for other timer. This is
not true for mode 3.

• Timers do not operate independently if mode 3 is


chosen for timer 0. Placing timer 1 in mode 3 causes
it to stop counting; the control bit TR1 and the timer
1 flag TF1 are then used by timer 0.
Example:
Generate Delay =10ms with Clock frequency= 11.0592 MHz, using
Timer 0 in mode1.
Solution:
Time delay=10ms; Clock frequency=11.0592 MHz
Step 1: Divide the desired time delay by 1.085 us
Count =10 ms/1.085 us= 9216
Step 2: Perform 65536 – n
65536-9216=56320= DC00H
Step 3: Set TL = xx and TH = yy

Here xx=DC and yy=00, Hence, TH0=DC and TL0=00.


The assembly code program to generate a delay
of 10ms

MOV TMOD, #01 ;Timer 0, mode 1, 16-bitmode


HERE: MOV TL0, #00 ;TL0=0, the low byte
MOV TH0,#0DCH ;TH0=DC, the high byte
SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
Program to generate a square wave of 5 kHz
frequency on pin P1.0, clock frequency =11.0592 MHz

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

SETB TR1 ;Start timer


BACK: JNB TF1,BACK ;until timer rolls over
CPL P1.0 ; compliment P1.0
CLR TR1 ;Stop the timer 1
CLR TF1 ;Clear timer 1 flag
SJMP AGAIN ;Reload timer
Toggle LED connected at P1.0 with 5
microsec delay using timer1 and mode 2
MOV TMOD,#20 ;Timer 1 mode 2, 8-bit auto reload
AGAIN: MOV TH1,#-5 ;TL1=256-5, low byte of timer
SETB TR1 ;Start timer 1
BACK: JNB TF1, BACK ;until timer rolls over
CPL P1.0 ; compliment P1.0 toggle LED
CLR TF1 ;Stop the timer 1
SJMP BACK
Serial Ports in 8051 Microcontroller

• Microcontrollers can communicate data in either parallel form


or serial form.
• In parallel communication, data is transferred over more than
one wire for example if 8 wires of one microcontroller are
connected to any other peripheral device or another
microcontroller then at a particular time 8 data bits are
transferred.
• On the other hand, in serial communication, data is transferred
in bit by bit manner over a single wire.
• Serial communication is preferred when the distance between
transmitter and receiver is large and it is required to save the
cabling cost and reduce hardware complexity but definitely this
comes at the cost of reduced speed of data transfer.
Fig. shows serial data transmission
Fig. shows parallel data transmission

In 8051 in built UART


(Universal Asynchronous
Receiver Transmitter)
module performs the job of
serial communication of
data.
In 8051 microcontroller serial communication of data is
performed with the help of following special purpose
registers:
• SBUF (Serial buffer register)
• SCON (serial control register)
• TMOD (Timer mode register)
• TCON (Timer control register)
• TH1 (Timer1 register higher byte)
SBUF (Serial buffer register) :
• It is an 8-bit register and is used for serial communication of
data in 8051 microcontroller.
• Whatever data is required to be transmitted via TXD line must be
placed in the SBUF register.
• Similarly, the received data via RXD line is saved in SBUF
register.
• When data is written to SBUF register then it is framed in
between start and stop bit before it is transmitted via TXD line
and similarly during reception of data start and stop bits are
removed and actual data bits are extracted from the received
frame and then it is placed in the SBUF register.
SCON (Serial Control) Register:
• 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.
Example: set baud rate at 9600
MOV TMOD, #20H ; timer 1,mode 2(auto reload)
MOV TH1, #-3 ; To set 9600 baud rate
SETB TR1 ; start timer 1

For the crystal oscillator frequency of 11.0592MHz the below given


table can be used as a reference for getting the value that is required
to be loaded into TH1 register for the desired baud rate generation.
Baud Rate TH1 (Decimal) TH1 (Hex)

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.

MOV TMOD, #20H ; Timer 1, Mode 2 (Auto reload)


MOV TH1, #-6 ; 4800 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; Star Timer 1
AGAIN: MOV SBUF, #”A” ; Letter A to be transferred
HERE: JNB TI ; Clear TI for next char
SJMP AGAIN
Write a program to transfer the message “YES” serially at 9600 baud rate, 8-bit data, 1 stop
bit. Do this continuously.

MOV TMOD, #20H ;Timer 1, Mode 2 (Auto reload)


MOV TH1, #FD ; 9600 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; Star Timer 1
AGAIN: MOV A, #”Y” ; transfer Y
ACALL TRANS
MOV A, #”E” ; transfer E
ACALL TRANS
MOV A, #”S” ; transfer S
ACALL TRANS
SJMP AGAIN

TRANS: MOV SBUF, A ; Load SBUF


HERE: JNB TI, HERE ; wait for last bit to transfer
RET
Write a program for the 8051 to receive byte of data serially, and
put them in P1. Set the 4800 baud rate, 8-bit data and 1 sop bit.

MOV TMOD, #20H ;Timer 1, Mode 2 (Auto reload)


MOV TH1, #-6 ; 4800 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; Star Timer 1
HERE: JNB RI, HERE ; wait for char to come
MOV A, SBUF ;save incoming byte in A
MOV P1, A ;sent to port 1
CLR RI ;get ready to receive next byte
SJMP HERE
INTERRUPT
• An interrupt is an event that occurs randomly in the flow of continuity.

• It is just like a call you have when you are busy with some work and depending upon

call priority you decide whether to attend or neglect it.

• The same thing happens in microcontrollers.

• 8051 architecture handles 5 interrupt sources, out of which two are internal (Timer

Interrupts), two are external and one is a serial interrupt.

• Each of these interrupts has its interrupt vector address.

• 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

from 0x0000 address.

8051 Internal interrupt (Timer Interrupt)

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 Serial interrupt

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.

Bit 2 – EX1: Enable External1


Bit 7 – EA: Enable All Bit Interrupt Bit
1 = Enable all interrupts 1 = Enable External1 interrupt
0 = Disable all interrupts 0 = Disable External1 interrupt
Bit 6,5 – Reserved bits Bit 1 – ET0: Enable Timer0 Interrupt
Bit 4 – ES: Enable Serial Interrupt Bit Bit
1 = Enable serial interrupt 1 = Enable Timer0 interrupt
0 = Disable serial interrupt 0 = Disable Timer0 interrupt
Bit 3 – ET1: Enable Timer1 Interrupt Bit Bit 0 – EX0: Enable External0
1 = Enable Timer1 interrupt Interrupt Bit
0 = Disable Timer1 interrupt 1 = Enable External0 interrupt
0 = Disable External0 interrupt
8051 MCU Interrupt priority
Priority to the interrupt can be assigned by using the interrupt priority
register (IP) Interrupt priority after Reset:

Priority Interrupt source Intr. bit / flag

1 External Interrupt 0 INT0


2 Timer Interrupt 0 TF0
3 External Interrupt 1 INT1
4 Timer Interrupt 1 TF1
5 Serial interrupt (TI/RI)

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.

Bit 1 – PT0: Timer0 Interrupt


Bit 7,6,5 – Reserved bits. Priority Bit
Bit 4 – PS: Serial Interrupt Priority Bit 1 = Assign high priority to
1 = Assign a high priority to serial interrupt. Timer0 interrupt.
0 = Assign low priority to serial interrupt. 0 = Assign low priority to
Bit 3 – PT1: Timer1 Interrupt Priority Bit Timer0 interrupt.
1 = Assign high priority to Timer1 interrupt. Bit 0 – PX0: External 0 Interrupt
0 = Assign low priority to Timer1 interrupt. Priority Bit
Bit 2 – PX1: External Interrupt 1 Priority Bit 1 = Assign high priority to
1 = Assign high priority to External1 External0 interrupt.
interrupt. 0 = Assign low priority to
0 = Assign low priority to External1 External0 interrupt.
interrupt.
External interrupts in 8051
• 8051 has two external interrupt INT0 and INT1.
• 8051 controller can be interrupted by external Interrupt, by
providing level or edge on external interrupt pins PORT3.2,
PORT3.3.
• External peripherals can interrupt the microcontroller through
these external interrupts if global and external interrupts are
enabled.
• Then the microcontroller will execute current instruction and
jump to the Interrupt Service Routine (ISR) to serve to interrupt.
• In the polling, method the microcontroller has to continuously
check for a pulse by monitoring pin, whereas, in the interrupt
method, the microcontroller does not need to poll. Whenever an
interrupt occurs microcontroller serves the interrupt request.
8051 mcu has two types of external interrupts of activation level
• Edge triggered (Interrupt occur on rising/falling edge detection)
• Level triggered (Interrupt occur on high/low-level detection)
In 8051 mcu, two types of activation levels are used. These are,
Low level triggered
Whenever a low level is detected on the INT0/INT1 pin
while global and external interrupts are enabled, the controller
jumps to interrupt service routine (ISR) to serve interrupt.
Falling edge triggered
Whenever falling edge is detected on the INT0/INT1 pin
while global and ext. interrupts are enabled, the controller jumps
to interrupt service routine (ISR) to serve interrupt.
There are lower four flag bits in the TCON register required to
select and monitor the external interrupt type and ISR status.
TCON: Timer/ counter Register
Bit 3- IE1:
External Interrupt 1 edge flag, set by hardware when interrupt
on INT1 pin occurred and cleared by hardware when interrupt get
processed.
Bit 2- IT1:
This bit selects the external interrupt event type on INT1 pin,
1= sets interrupt on falling edge
0= sets interrupt on low level
Bit 1- IE0:
Interrupt0 edge flag, set by hardware when interrupt on INT0
pin occurred and cleared by hardware when an interrupt is
processed.
Bit 0 - IT0:
This bit selects the external interrupt event type on the INT0 pin.
1= sets interrupt on falling edge
0= sets interrupt on low level
Handling Interrupts of 8051

• Enabling and disabling of Interrupts


• Storing the Interrupt Subroutine program in Memory
• Initializing the Stack pointer

Enabling and disabling of Interrupts


• The 8051 interrupts are disabled upon power ON or after a reset.
• The desired interrupts has to be enabled by programming the appropriate bits in the
interrupt enable register.
• The interrupts has a global enable which can be programmed to enable/disable all
the interrupt.
• Also, every interrupt has an individual enable that can be programmed to
enable/disable a particular interrupt.
• In order to enable a particular interrupt, both global enable and particular enable bit
has to be set to high.
• For disabling an interrupt, it is enough if the particular bit of an interrupt is reset to
low.
Storing the Interrupt Subroutine program in Memory
• For every interrupt, a subroutine program has to be developed and stored in the
code memory starting from the vector address of that interrupt.
• Normally, very few memory locations are reserved for storing the interrupt
subroutine .
• If the interrupt subroutine requires larger memory space then it is stored in any
available code memory space.
• A branch instruction with starting address of interrupt subroutine as branch
address is stored in vector address of the interrupt.

Initializing the Stack pointer


• The stack pointer is initialized with the data memory address 07H upon power ON
or after a reset.
• The content of memory addressed by the stack pointer is considered occupied stack
(or top of the stack).
• Then for every push operation, the stack pointer is incremented and using the
incremented address the content of memory/register is stored in the stack.

You might also like