0% found this document useful (0 votes)
26 views

8051architectures

8051 A complete information

Uploaded by

P R Prasad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

8051architectures

8051 A complete information

Uploaded by

P R Prasad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 198

8051 MICROCONTROLLER & ITS APLLICATIONS

By
P RAMANJANEYA PRASAD
Head – Department of Electronics
Avanthi Degree & PG College
Email: [email protected]
What Are Embedded Systems?

“Specialpurpose system which are either used as


standalone or part of a big system”

An Embedded System is one that has computer hardware


with software embedded in it a s one of its important
components.

An embedded product uses a microprocessor (or


microcontroller) to do one task and one task only.

A printer is an example of embedded system since the


processor inside it performs only one task; namely,
getting the data and printing it
WHERE ARE THEY PRESENT

 Integratedsystems in
aircrafts and missiles ?
 Cellular telephones

 Electric and Electronic Motor


controllers

 Engine controllers in
automobiles

 Calculators

 Medical equipment’s

 Videogames
T H E N E C E S S A R Y TOOLS FOR A M ICROPRO C E S S O R /M ICROCONTROLLER

CPU: Central Processing Unit

I/O: Input /Output

Bus: Address bus & Data bus

Memory: RAM & ROM

Timer

Interrupt
Introduction to Microprocessors

“A Microprocessor is a general purpose chip used to create


multifunction device ”

 No RAM, ROM, I/O ports on CPU chip itself

 CPU is stand-alone, Must add RAM, ROM, I/O ports, and


timers externally to make them functional

 Designer can decide on the amount of ROM, RAM and I/O


ports

 Example : Intel’s x86, Motorola’s 680x0


Introduction to Microcontrollers

“A Microcontroller is a computer-on-a-chip optimized to control


Electronic devices”

 CPU + I/O + Timer(s) [+ ROM] [+ RAM] All on single chip

 Limited RAM space, ROM space and I/O pins

 Low chip-count to implement a small system

 Low-cost at large quantities


Microprocessor System Contrasted With
Microcontroller System
Microprocessor vs Microcontroller

Microcontroll Microprocessor
er RAM, ROM, I/O
• CPU,
• CPU is stand-alone, RAM, ROM,
and timer are all on a
I/O, timer are separate.
single chip•
• Designer can decide the amount
• Fix amount of on-chip
• of ROM, RAM and I/O ports
ROM, RAM, I/O ports
• Expensive
• For applications in
• General-purpose
which cost, power and
space are critical
• Expensive
• Single-purpose
8051 Microcontroller Block Diagram


Architecture of
8051
The 8051 Architecture consist of
these specific features
 The 8 bit CPU with Registers A and B
 Internal ROM
 16-bit program counter(PC) and data
pointer(DPTR)
 Internal RAM of 128 bytes
 8-bit Program Status word(PSW)

 Two 16 bit Counter / timers

 4 eight-bit ports

 3 internal interrupts and 2 external interrupts.


 Control register

 Oscillator and clock circuits.


A and B CPU
Register
 The 8051 contains 34 general purpose or working registers.
Two of these Register A and B.

The immediate result is stored in the accumulator


register (Acc) for next operation.

The B register is a register just for multiplication and


division operation which requires more register spaces
for the product of multiplication and the quotient and
the remainder for the division.
Program status
word(PSW)
 The program status word shown in figure.

The PSW contain the math flags, User program flag


F0,and the register select bits that identify which of the
four General-purpose register banks is currently in
use by the program.

The math flags include carry(c),auxiliary carry(AC),


overflow(OV) and parity(p)
Program Status Word
(PSW)
The 8051 oscillator and
clock
The 8051 requires an external oscillator circuit. The oscillator circuit
usually runs around 12MHz. The crystal generates 12M pulses in
one second.

 A machine cycle is minimum amount time must take by simplest


machine instruction

 An 8051 machine cycle consists of 12 crystal pulses (clock cycle).

The first 6 crystal pulses (clock cycle) is used to fetch the Opcode
and the second 6 pulses are used to perform the operation on the
operands in the ALU.

This gives an effective machine cycle rate at 1MIPS (Million


Instructions Per Second).
Oscillator circuit and
timing
Program counter
(PC)
 The program counter points to the address of the next
instruction to be Executed

As the CPU fetches the opcode from the program ROM,
the program counter is increasing to point to the next
instruction.

 The program counter is 16 bits wide

 This means that it can access program addresses 0000


to FFFFH, a total of 64K bytes of code
Data pointer
(DPTR)
 The data pointer is 16 bit register.

It is used to hold the address of the data in the


memory.

The DPTR register can be accessed separately as


lower eight bit(DPL) and higher eight bit (DPH).

It can be used as a 16 bit data register or two


independent data register.
The stack and
The stack pointer
(SP)
 The stack is a section of RAM used by the CPU to store
information temporarily

 This information could be data or an address

‰The register used to access the stack is called the SP


(stack pointer) register

 The stack pointer in the 8051 is only 8 bit wide.


Operation of stack

pop
push

stack pointer

stack
Memory Organization

INTERNAL MEMORY
 128 bytes of RAM.

 Directly addressable range: 00 to 7F hexadecimal.

 Indirectly addressable range: 00 to FF hexadecimal.

 Bit addressable space: 20 to 2F hexadecimal .

 Four register banks: 00 to 1F hexadecimal.


Internal Memory
Internal RAM
The 128 byte internal RAM shown in figure
It is organized into three areas.

1.Working register:
Thirty-two bytes from address 00h to 1Fh that make up 32 working register
organized as Four bank of eight bit each.
 Bits RS0 and RS1 in the PSW determine which bank of register is currently
Is use.
 Bank 0 is selected upon reset

2.Bit addressable:
 A bit addressable area of 16 bytes occupies RAM bytes addresses 20h to
2Fh,forming A total of 128 addressable bits.
 An addressable bit may be specified by its bit address of 00h to 7Fh.

3.General purpose:
 A general-purpose RAM area above the bit area,form 30h to 7Fh,addresable
as bytes.
Internal RAM Organization

Sachin Bhalavat (9409049436)


External memory

External memory is used in cases when the internal ROM


and RAM memory Available On chip is not sufficient. Two
separate are made available by the 16-bit PC and the DPTR
and by different control pins for enabling external ROM and
RAM chips.

If the 128 bytes of internal RAM is insufficient, the


external RAM is accessed by the DPTR. In the 8051 family,
external RAM of upto 64 KB can be added to any chip.
Special Function Register (SFR)

The SFR (Special Function Register) can be accessed


by their names or by their addresses.
‰The SFR registers have addresses between 80H
and FFH.
 Not all the address space of 80 to FF is used by
SFR.
The unused locations 80H to FFH are reserved and
must not be used by the 8051 programmer.
 There are 21 SFRs.
Special Function Register Map
Bit Addressable

F8
B
F0
ACC
E8
PSW
E0

D8 IP
P3
D0 IE
P2
C8 SCON SBUF
P1
C0 TCON TMOD TL0 TL1 TH0 TH1
P0 SP DPL DPH PCON
B8
Special Function Register
(SFR)cont..
Special Function Register
(SFR)cont..
 128 byte address space,
directly addressable as 80 to FF
hex.
 16 addresses are bit
addressable: (those ending in 0
or 8).

 This space contains:

Special purpose CPU registers.


I/O ports.
Interrupt control
Timers
Special Function Register
(SFR)cont..
 CPU registers:
- ACC : Accumulator.
-B : B register.
- PSW : Program
Status Word.
- SP : Stack Pointer.
- DPTR : Data Pointer
 Interrupt control:
(DPH, DPL).
-IE : Interrupt Enable.
-IP : Interrupt Priority.
 I/O Ports:
- P0 : Port 0.
- P1 : Port 1.
- P2 : Port 2.
- P3 : Port 3.
Special Function Register
(SFR)cont..
 Timers:
- TMOD : Timer mode.
- TCON : Timer control.
- TH0 : Timer 0 high byte.
- TL0 : Timer 0 low byte.
- TH1 : Timer 1 high byte.
- TL1 : Timer 1 low byte.

 Serial I/O:
- SCON : Serial port control.
- SBUF : Serial data registers.
 Other:
- PCON : Power control
I/O Ports

-Four 8-bit I/O ports.


Port 0
Port 1
Port 2
Port 3

- Most have alternate functions.

- Quasi-bidirectional:
Port 0

- Port 0 is a dual purpose port, it is located from pin 32 to pin 39


(8 pins).

- To use this port as both input/output ports each pin must be


connected externally to pull-up resistor.
- As an I/O port.
- Alternate functions:
As a multiplexed data bus.
8-bit instruction bus, strobed by PSEN.
Low byte of address bus, strobed by
ALE. 8-bit data bus, strobed by WR and
RD.
Port 1

- Port 1 is a dedicated I/O port from pin 1 to pin 8.


- Upon reset it is configured as outport.
- It is generally used for interfacing to external device
- thus if you need to connect to switches or LEDs, you could make use of
these 8 pins,
- but it doesn’t need any pull- up resistors as it is having internally
- As an I/O port: Standard quasi-bidirectional.
Port 2

- Like port 0, port 2 is a dual-purpose port.(Pins 21 through 28)


-It can be used for general I/O or as the high byte of the address bus for
designs with external code memory.
- Like P1 ,Port2 also doesn’t require any pull-up resistors
- As an I/O port:
Standard quasi-bidirectional.
- Alternate functions:
High byte of address bus for externalprogram and data
memory accesses.
Port 3 – Special Function Register
- Port 3 is also dual purpose but designers generally avoid using
this port unnecessarily for I/O because the pins have alternate
functions which are related to special features of the 8051.
- Indiscriminate use of these pins may interfere with the
normal operation of the 8051.
- As an I/O port:
Standard quasi-bidirectional.
- Alternate functions:
Serial I/O -

TXD, RXD Timer clocks


I/O Port structure

 The internal circuitry for the I/O port is shown in


the figure
 If you want to read in from a pin, you must first give
a logic ‘1’ to the port latch to turn off the FET
otherwise the data read in will always be logic ‘0’.
 When you write to the port you are actually writing
to the latch e.g. a logic 0 given to the latch will be
inverted and turn on the FET which cause the port
pin to be connected to Gnd (logic 0).
I/O Port structure Diagram

Sachin Bhalavat (9409049436)


8051 PIN description
P1.0 1
40 Vcc
P1.1 2
39 P0.0 (AD0)
3 P0.1 (AD1)
38
P1.2
P1.3 4 37 P0.2 (AD2)
5 P0.3 (AD3)
36
P1.4
P1.5 6
35 P0.4 (AD4)
P1.6 7 P0.5 (AD5)
34
8 8051 33 P0.6 (AD6)
P1.7 9
32 P0.7 (AD7)
10 31 EA/VPP
RST
(TXD) P3.1 11
30 ALE/PROG
(INT0) P3.0
(RXD) P3.2 12
29 PSEN
(INT1) 13
28 P2.7 (A15)
P3.3 14
27 P2.6
(T0) P3.4 (A14)
15 26
(T1) P3.5 16 25 P2.5
(WR) P3.6 (A13)
17 24 P2.4
(RD) P3.7 (A12)
18 23
XTAL2
XTAL1 19 22 P2.3
(A11)
GND 20 21
P2.2
(A10)
P2.1 (A9)

P2.0 (A8)
Timer/Counters

 Two 16-bit up counters, named T0 and T1, are provided for the general use
of the programmer.
Each counter may be programmed to count internal clock pulses, acting as a
timer, or programmed to count external pulses as a counter.
The counters are divided into two 8-bit registers called the timer low
(TL0,TL1) and high (TH0, TH1) bytes.
All counter action is controlled by bit states in the timer mode control register
(TMOD), the timer/counter control register (TCON) and certain program
instructions.
TMOD is dedicated to the two timers and can be consider two duplicate 4-bit
registers, each of which controls the action of the timers.
TCON has control bits and flags for the timers in the upper control bits and
flags for the external interrupts in the lower nibble.
Interrupt System
 5 Interrupt Sources (in order of priority):
1 External Interrupt 0 (IE0)
2 Timer 0 (TF0)
3 External Interrupt 1 (IE1)
4 Timer 1 (TF1)
5 Serial Port (RI/TI)
 Each interrupt type has a separate vector
address.
 Each interrupt type can be programmed
to one of two priority levels.
 External interrupts can be
programmed for edge or level
sensitivity.
Interrupt vector Addresses
Source Address
IE0 03H
TF0 0BH
IE1 13H
TF1 1BH
RI&TI 23H

The 8051 starts execution at 0000H after


Reset.
IE : Interrupt Enable Register

EA ---- ---- ES ET1 EX1 ET0 EX0

- EA : Global interrupt enable.


- ES : Enable serial port
interrupt
- ET1 : Timer 1.
- EX1 : External interrupt 1.
- ET0 : Timer 0.
- EX0 : External interrupt 0.

- 0 = Disabled.
- 1 = Enabled.
IP: Interrupt Priority Register
----- ----- ----- PS PT1 PX1 PT0 PX0

- PS : Serial interface.
- PT1 : Timer 1.
- PX1 : External interrupt
1.
- PT0 : Timer 0.
- PX0 : External interrupt
0.

- 0 = Low priority.
- 1= High
priority.
8051 Microcontroller & its Applications

UNIT – 2
INSTRUCTION SETS OF 8051 MICROCONTROLLER
Instruction sets of 8051 microcontroller

Instruction Sets
Data Transfer

Arithmetic

Logical

Control or Jump
General Purpose Data Transfer

Accumulator Specific Data Transfer

Address Object Data Transfer


 .MOV – byte or bit transfer
 PUSH - byte to stack
 . POP - byte from stack
(Flags not effected unless PSW in being modified)
 MOV Rn, # data – Immediate Addressing
 MOV A, # data - Immediate Addressing
 MOV Rn, A - Register Addressing
Note - MOV Ri , Rj – Not allowed
 MOV A, Rn – Register Addressing
 MOV A, Direct – Direct Addressing
 MOV Direct , A - Direct Addressing
 MOV Direct , Rn - Direct Addressing
 MOV Rn , Direct - Direct Addressing
 MOV Direct , Direct - Direct Addressing
 MOV @Ri , A – Indirect Addressing
 MOV A , @Ri -Indirect Addressing
 MOV Direct, @Ri – Direct + Indirect
 MOV @Ri, Direct – Direct + Indirect
(Ri = Ro or R1)
 MOV Direct, # Data - Direct + immediate
 MOV @ Ri , # Data – Indirect + immediate
 PUSH Direct
 POP Direct
 XCH –Exchange data between A reg. and byte source
operand
 XCHD –Exchange lower Nibble in A with Lower Nibble of source
operand (Indirect Addressing).
 MOVX – Data transfer between external data memory and

A register(Indirect Addressing). External

address in DPTR.
 MOVC – Move byte from program memory to A. (Base
Reg + Index reg. indirect addressing)
 XCH A, <byte>
XCH A, Rn – Register Addressing XCH A,
 XCHD A, @Ri (Ri = R0 or R1)
 MOVX < dest. Byte> <src. Byte>
MOVX A, @ Ri
MOVX A , @ DPTR
MOVX @ Ri, A
MOVX @ DPTR , A
 MOVC A, @A + DPTR (Move code byte)
(A) ((DPTR)+(A))
MOVC A, @A + PC
(A) (( PC) +(A))
16-bit Data Transfer

DPTR, # Data16 - Move 16 bit address as immediate


data to DPTR MOV
Arithmetic Instructions
Operations – Add , Subtract , Multiply, Divide, Increment,
Decrement. Only 8 bit operations are permitted.
Flags are affected

ADDITION:-
ADD A, (src. Byte) - Add (A)← (A) + (Src.)

ADDC A, (Src byte) Add with carry (A) ← (A) +(Src) + (C)

Register Addressing-
ADD A, Rn (A) ← (A)+(Rn)
ADDC A, Rn (A) ← (A)+(Rn)+(C)

Direct Addressing-
ADD A, Address (A) ← (A)+(Direct Address)
ADDC A, Address (A) ← (A)+(C)+(Direct Address)
Arithmetic Instructions
Indirect Addressing
ADD A, @Ri (A) ← (A)+Address[Ri]
ADDC A,@Ri (A) ← (A)+(C)+ Address[Ri]
**Ri = R0 or R1
Immediate Addressing
ADD A, #data (A) ← (A)+(Data)
ADDC A, #data (A) ← (A)+(C)+(Data)

Increment
INC Rn Rn ← Rn + 1
INC @Ri, Ri ← [Ri] + 1
INC DPTR DPTR ← DPTR +
1
Arithmetic Instructions
SUBTRACTION
SUBB – Subtract with Borrow

SUBB A, Rn (A) ← (A) – Rn


SUBB A, direct address (A) ← (A) – (direct address)
SUBB A, @Ri (A) ← (A) – Address[Ri]
SUBB A,#data (A) ← (A)+(data)
**Ri = R0 or R1
DEC Rn Rn ← Rn – 1
DEC @Ri Ri ← [Ri] – 1
DEC DPTR DPTR ← DPTR – 1
Arithmetic Instructions
MULTIPLICATION
MUL AB (A) – Operand 1, (B) – Operand 2

After Operation
(A) - lower order byte of the result.
(B) - Higher order byte of the result

DIVIDE
DIV AB (A) – Operand 1 or
Dividend,
(B) – Operand 2 or
Divisor
After Operation
Arithmetic Instructions
DA A (Decimal Adjust Accumulator for addition)

If two BCD numbers (in packed BCD format) are added using ADD or
ADDC instructions then to represent the result in BCD.

DA A instruction is used to get the result in decimal format, Algorithms


is-

(1) If value of lower Nibble in ACC > 9 or if AC Flag is set then 6 is added
to ACC.
(2) Now after (1), if value of higher Nibble is greater than 9 or if CY Flag is
set then 6 is added to higher Nibble of ACC.
Logical Instructions
Logical Instructions

B7 B6 B5 B4 B3 B2 B1 B0

B7 B6 B5 B4 B3 B2 B1 B0

SWAP A (Swap Nibble in ACC)


B7 B6 B5 B4 B3 B2 B1 B0
Logical Instructions

AND Operation
ANL A, Rn (A) ← (A) AND Rn
ANL A, direct address (A) ← (A) AND (direct address)
ANL A, @Ri (A) ← (A) AND Address[Ri]
ANL A,#data (A) ← (A) AND (data)

OR Operation
ORL A, Rn (A) ← (A) OR Rn
ORL A, direct address (A) ← (A) OR (direct address)
ORL A, @Ri (A) ← (A) OR Address[Ri]
ORL A,#data (A) ← (A) OR (data)

**Ri = R0 or R1
Logical Instructions

XOR Operation
XRL A, Rn (A) ← (A) XOR Rn
XRL A, direct address (A) ← (A) XOR (direct address)
XRL A, @Ri (A) ← (A) XOR Address[Ri]
XRL A,#data (A) ← (A) XOR (data)

**Ri = R0 or R1
Control Transfer Instructions

Unconditional calls, Returns and jumps


Conditional Jumps
Unconditional calls, Returns and
jumps
ACALL addr (Absolute Call)
CALL to subroutine located within 2KB memory block
Main program Fact
-------- - 1
-------- 2KB
--------
ACALL
Subroutine
Fact ---------
---------
RET

(1) Program counter is saved in the stack. Address of


Subroutine is loaded to PC.
(2) Program counter value is retrieved from stack
Unconditional calls, Returns and
jumps
 LCALL addr. 16 (Long call)

Call can be made to full 64KB space

LCALL FACT
--------
--------
---------
FACT: --------
--------
--------
RET
Unconditional calls, Returns and
jumps
JUMP

 AJMP addr 11 (Absolute Jump)


Jump to instruction in 2KB block
 LJMP addr16 (Long Jump)
Jump can be any where in memory space
 SJMP relative (Short Jump)
Jump can be 128 bytes before or 127 bytes after
the instruction
Unconditional calls, Returns and
jumps
 Condition
- bit set or bit not set
- result in ACC is zero or not zero
- rel. Jump to 128 byte before 127 after
 JB bit, rel. (Jump if bit set)
JB P0.3 , NEXT

NEXT:
 JNB bit, rel (Jump if bit not set)
 JC rel (Jump if carry set)
 JNC rel (Jump if carry not set)
 JBC bit, rel (Jump if bit is set and clear bit)
 JZ rel , Jump if result in ACC is zero
 JNZ rel , Jump if result in ACC is not zero.
CJNE <dest>, <Src>,address.
If (dest) ≠ (Src) then jump to specified address.
If <dest> less than <Src> CY=1
If <dest> greater than <src> CY =0

CJNE A, direct, address


CJNE A, #data, address
CJNE Rn, #data, address
CJNE @R0, #data, address
DJNZ Rn, address
(Rn) (Rn) – 1
If (Rn) ≠ 0 then jump Else next instruction
The various ways of accessing data are called addressing
modes.

The way by which the source and destination operands


are specified in an instruction are called addressing modes.

The instruction may contain one or more operands.

These addressing modes are determined when it is designed


and cannot be changed by the Programmer.
 Immediate Addressing

 Register Addressing

 Direct Addressing

 Register indirect Addressing

 Base Register plus index register indirect addressing.


Immediate Addressing Mode

 The operand comes immediately after the opcode

 The data is given as part of the instruction.

The immediate data must be preceded by the sign ‘#’

 The source operand is constant.

The information can be loaded into any of the


registers including DPTR
Immediate Addressing Mode

MOV A, #data // Load the immediate data into A

MOV Rn, # data // Load the immediate data in to Rn

MOV DPTR, # data // Load the data into DPTR


Examples
MOV A,# 25H // Load the value 25H into A

MOV R, #63H //Load 63H into register R

MOV DPTR, #4521H // Load 4251H into the


Register Addressing
Mode

 The operands are specified by register names.

 It requires only one byte memory.

Involves the use of registers to hold the data to be


manipulated.
Register Addressing
Mode

MOV A, Rn //Copy the contents of register Rn to A

MOV Rn, A //Copy the contents of A into register Rn

Examples
MOV A,R1 //Copy the contents of register R1 into A

MOV R2, A // Copy the contents of A into register R2


Direct Addressing Mode

The data is stored in RAM memory location


whose address is known and the address is given as
part of the instruction.

The data is accessed directly from the memory.


Direct Addressing Mode

MOV A, Direct // Copy data from address Direct into A


MOV Direct, A //Copy data from A into address Direct

MOV Rn, Direct //Copy data from address Direct into


register Rn

MOV Direct, Rn // Copy data from register Rn into


address Direct
MOV Direct, #data // load immediate data into Direct
address
MOV direct1,direct2 //copy data from address direct2
Direct Addressing Mode

Examples
MOV A, 10H //Copy data from address 10H into A
MOV 10H, //Copy data from A into address 10H
A
MOV R5, 80H //Copy data from address 80H into register
R5
MOV 50H, R3 // Copy data from register R3 into address 50H

MOV 20H, #32H // load immediate data 32H into address 20H

MOV 62H,83H //copy data from address 83H to address 62H


Register Indirect Addressing Mode

A register is used a pointer to the data.

Only the registers R0 and R1 can be used to hold


the address of the data in internal RAM.

These registers are called pointer registers.

When registers are used as pointers they are


preceded by the sign’@’
Register Indirect Addressing Mode

MOV A, @Ri //Copy data from address in Ri to A

MOV @Ri, A //Copy data from A into address in Ri

MOV Direct, @Ri //Copy data from address in Ri to address Direct

MOV @Ri, Direct //Copy data from address direct to address Ri

MOV @Ri, #data // Load the immediate value into address in Ri


Register Indirect Addressing Mode

MOV @R0, A //Copy data from A into address in R0

//Copy data from address in R0 to address


MOV Direct, @R0 Direct

//Copy data from address direct to address R1


MOV @R1, Direct

//Load the immediate value data address in


MOV @R1, #data R1
ABSOLUTE ADDRESSING MODE

• This mode is used only with the ACALL & AJMP


instructions
• These 2-byte instructions allow within the current
2K page of code memory.
• This mode has the advantage of short instructions
but has the disadvantage of limiting the range of
destination.
RELATIVE ADDRESSING MODE

• Relative addressing mode is used only with


certain jump instructions.
• A relative address is a 8-bit signed value, which is
added to the PC to form the address of the next
instruction executed.
• The range of jumping is 128 bytes to +127 bytes
• Usually the instructions are SJMP
Indexed Addressing Mode

Two registers are used to form the address of the data.


The contents of either Program Counter(PC) or Data
Pointer(DPTR) are used as base address and Accumulator is used
as index address.
The combination of the contents of these two registers gives

the final address.


This addressing mode is used to access data tables from

program memory and in implementing jump tables


Indexed Addressing Mode

// copy data from program


MOVC memory address formed by
A,@A+PC addition of contents of A
and PC into A

// copy data from program


memory address formed by
MOVC
addition of contents of A
A,@A+DPTR
and DPTR into A
8051 Microcontroller & its Applications

UNIT – 3
PROGRAMMING OF 8051 MICROCONTROLLER
TIMER / COUNTER PROGRAMMING IN 8051
8051 Microcontroller & its Applications

PROGRAMMING OF 8051 MICROCONTROLLER


Basics of a program statement in
assembly language
A statement in assembly language program consists of three
fields-
 Label
 Instructions
 Comments

These fields are arranged as


[Label:] Instructions [//
Comments]

Label
It is used to assign name to the memory location or
program
Basics of a program statement in
assembly language
Comments
Comments are used to describe the operation performed by
an instruction.
They help in understanding the program easily.
Mostly comments start with “//”

Example
MOV R0, #10H
Opcode – MOV //Move 10H into register R0
Operands – R0, 10H
Basics of a program statement in
assembly language
Instructions
Every instruction has two parts

Opcode - It specifies the operation to be performed.


Operands – It is the data on which operation is to be
performed

Operands are of two types.


Source operand – The operands which are inputs for an
operation.
Destination operand – The operands that will store the result of
an operation.
Label

Program Instruction Opcode


s Source
statement Operand operands
s Destina
Comments
ntio
operands
ADDITION PROGRAM

ALP to accept two 8-bit numbers from memory


locations and perform addition(result 8-bit)

Algorithm
1. Initialise the memory location.
2. Get the first number in register A.
3. Add the second number to the contents of Reg A
4. Store the result in the next consecutive memory
location.
5. Halt the program
ADDITION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
ADD A,@R0 //Add the contents in next location
to the contents of Reg A//
INC @R0 //increment R0//
MOV @R0,A //Store the result in next location//
LCALL 0003H //Halt the program//
Data Result
70H – 07H 72H – 0CH
71H – 05H
ADDITION PROGRAM

Start

A ← First Number

A ← A + 2nd Number

Store the result

Stop
ADDITION PROGRAM

ALP to accept two 8-bit numbers from memory


locations and perform addition(result 16-bit)
Algorithm
1. Initialize the memory location
2. Get the first number in register A.
3. Add the second number to the contents of Reg A
4. Check for carry. If carry generated move it to some
location for storage else continue..
5. Store the result in the next consecutive memory
location.
6. Halt the program
ADDITION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV R1,#00H // Initialize R1 with 00H//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
ADD A,@R0 //Add the contents in next location
to the contents of Reg A//
JNC LOOP //Check for carry//
INC R1 // R1 = R1+1//
LOOP: INC R0 //increment R0//
MOV @R0,A //Store the result in next location//
MOV A,R1 //Get carry in Register R1//
INC R0 //R0 = R0+1//
MOV @R0,A //Move carry in next memory location//
LCALL 0003H //Halt the program//
Data Result
70H – 97H 72H – 3CH
71H – 95H 73H – 01H
SUBTRACTION PROGRAM

ALP to accept two 8-bit numbers from memory


locations and perform Subtraction.

Algorithm
1. Initialise the memory location.
2. Get the first number in register A.
3. Subtract the second number to the contents of Reg A
4. Store the result in the next consecutive memory
location.
5. Halt the program
SUBTRACTION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
SUBB A,@R0 //Subtract the contents in next location
to the contents of Reg A//
INC R0 //increment R0//
MOV @R0,A //Store the result in next location//
LCALL 0003H //Halt the program//
Data Result
70H – 07H 72H – 02H
71H – 05H
SUBTRACTION PROGRAM

Start

A ← First Number

A ← A - 2nd Number

Store the result

Stop
ADDITION PROGRAM

ALP to accept two 8-bit numbers from memory


locations and perform decimal addition
Algorithm
1. Initialize the memory location
2. Get the first number in register A.
3. Add the second number to the contents of Reg A
4. Decimal adjust the content of accumulator.
5. Check for carry. If carry generated move it to some
location for storage else continue..
6. Store the result in the next consecutive memory
location.
7. Halt the program
ADDITION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV R1,#00H // Initialize R1 with 00H//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
ADD A,@R0 //Add the contents in next location
to the contents of Reg A//
DA A //Adjust the content of Reg A to decimal number//
JNC LOOP //Check for carry//
INC R1 // R1 = R1+1//
LOOP: INC R0 //increment R0//
MOV @R0,A //Store the result in next location//
MOV A,R1 //Get carry in Register R1//
INC R0 //R0 = R0+1//
MOV @R0,A //Move carry in next memory location//
LCALL 0003H //Halt the program//
Data Result
70H – 97H 72H – 3CH
71H – 95H 73H – 01H
MULTIPLICATION PROGRAM

ALP to accept two 8-bit numbers from memory


locations and perform Multiplication using MUL
instruction.
Algorithm
1. Initialise the memory location.
2. Get the first number in register A.
3. Get the second number in Reg B.
4. Multiply the two numbers.
5. Store the result in next consecutive memory
locations.
6. Halt the program
MULTIPLICATION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
MOV F0H,@R0 //Get the 2nd number in Reg B//
MUL AB //Multiplication//
INC R0 //increment R0//
MOV @R0,A //Store the result in next location//
MOV A,F0H //Move the content of B in A//
INC R0 //R0 = R0 + 1//
MOV @R0,A // Get the next higher byte in memory//
LCALL 0003H //Halt the program//
Data Result
70H – 07H 72H – 23H
71H – 05H 73H – 00H
MULTIPLICATION PROGRAM

ALP to MULTIPLY TWO NUMBERS USING COUNTER


METHOD
Algorithm
1. Initialize the memory location
2. Initialize Reg A = 00H
3. Get the first number in register .
4. Add the second number to Register A.
5. Subtract the first number by 1
6. Check the reg of first number for ‘0’.
7. If ‘0’, Store the result in the next consecutive memory
location.
8. Halt the program
MULTIPLICATION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV R3,#00H //Make R3=00h//
MOV 02H,@R0 //Get first number in R2//
CLR A //Clear the content of Register A//
INC R0 //increment R0//
LOOP: ADD A,@R0 //Add the contents in next location
to the contents of Reg A//
JNC GO
INC R3 //Increment the value of R3//
GO: DJNZ R2, LOOP //Check for carry//
INC R0 //increment R0//
MOV @R0,A //Store the lower order byte//
INC R0 //Increment R0//
MOV A,R3 //Get content of R3 into Register A//
MOV @R0,A //Get higher order byte in next location//
LCALL 0003H //Halt the program//

Data Result
70H – 11H 72H – 32H
71h – 12H 73H – 01H
DIVISION PROGRAM

ALP to accept two 8-bit numbers from memory


locations and perform DIVISION using MUL
instruction.
Algorithm
1. Initialise the memory location.
2. Get the first number in register A.
3. Get the second number in Reg B.
4. Divide the numbers.
5. Store the result in next consecutive memory
locations.
6. Halt the program
DIVISION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV A,@R0 //Get first number in Reg A//
INC R0 //increment R0//
MOV F0H,@R0 //Get the 2nd number in Reg B//
DIV AB // Division//
INC R0 //increment R0//
MOV @R0,A //Store the result in next location//
MOV A,F0H //Move the content of B in A//
INC R0 //R0 = R0 + 1//
MOV @R0,A // Get the next higher byte in memory//
LCALL 0003H //Halt the program//
Data Result
70H – 07H 72H – 23H
71H – 05H 73H – 00H
DIVISION PROGRAM

ALP TO DIVIDE TWO NUMBERS USING COUNTER


METHOD
Algorithm
1. Initialize the memory location
2. Initialize Register R2 = 00H
3. Get the first number in register A .
4. Increment the Register R2.
5. Subtract the 2nd number from 1st number
6. Check for carry.
7. If no carry continue the subtraction else move the result to
the next memory locations
8. Halt the program
DIVISION PROGRAM
MOV R0,#70H //Initialize the location of data//
MOV R3,#00H //Make R3=00h//
MOV A,@R0 //Get first number in A//
INC R0 //increment R0//
GO: INC R3 //increment R3//
SUBB A,@R0 //Subtract the contents in next location from Reg A//
CJNE A,F0H, LOOP //Compare A and B registers//
LOOP: JNC GO
INC R0 //increment R0//
MOV @R0,A //Store the lower order byte//
INC R0 //Increment R0//
MOV A,R3 //Get content of R3 into Register A//
MOV @R0,A //Get higher order byte in next location//
LCALL 0003H //Halt the program//

Data Result
70H – 09H 72H – 01H
71h – 02H 73H – 04H
LARGEST NUMBER PROGRAM

ALP TO FIND THE LARGEST NUMBER IN A SERIES


Algorithm
1. Initialize the memory location
2. Get the count in one register.
3. Get the first number in register A .
4. Get the second number in register B.
5. Compare and check for carry. If carry, move the higher
number in accumulator
6. If no carry decrement the count and compare for remaining
numbers.
7. Store the result.
8. Halt the program
LARGEST NUMBER PROGRAM
MOV R0,#50H //Initialize the location of data//
MOV R2,#03H //Get count in Register R2//
DEC R2 //Decrement the count//
MOV A,@R0 //Get first number in A//
NEXT: INC R0 //increment R0//
MOV F0H,@R0 //Get next number in register B//
CJNE A,F0H, LOOP //Compare A and B registers for not equal, jump to LOOP//
LOOP: JNC GO //Check for carry? If no, jump to GO//
MOV A,@R0 //Move largest number in Register A //
GO: DJNZ R2, NEXT //R2=R2-1 and check R2//
INC R0 //increment R0//
MOV @R0,A //Get higher order byte in next location//
LCALL 0003H //Halt the program//

Data Result
50H – 86H 53H – 89H
51H – 89H
52H – 88H
SMALLEST NUMBER PROGRAM

ALP TO FIND THE SMALLEST NUMBER IN A SERIES


Algorithm
1. Initialize the memory location
2. Get the count in one register.
3. Get the first number in register A .
4. Get the second number in register B.
5. Compare and check for carry. If no carry, move the smaller
number in accumulator
6. If carry decrement the count and compare for remaining
numbers.
7. Store the result.
8. Halt the program
SMALLEST NUMBER PROGRAM
MOV R0,#50H //Initialize the location of data//
MOV R2,#03H //Get count in Register R2//
DEC R2 //Decrement the count//
MOV A,@R0 //Get first number in A//
NEXT: INC R0 //increment R0//
MOV F0H,@R0 //Get next number in register B//
CJNE A,F0H, LOOP //Compare A and B registers, if not equal jump to LOOP//
LOOP: JC GO //Is Carry present ? – Yes, jump to GO
MOV A,@R0 //Move smallest number in Register A //
GO: DJNZ R2, NEXT //R2=R2-1 and check R2. if not ‘0’ jump to NEXT//
INC R0 //increment R0//
MOV @R0,A //Get higher order byte in next location//
LCALL 0003H //Halt the program//

Data Result
50H – 86H 53H – 89H
51H – 89H
52H – 88H
DESCENDING ORDER PROGRAM

ALP TO ARRANGE A SET OF NUMBERS IN DESCENDING


ORDER
Algorithm
1. Initialize the memory location
2. Get the count in one of the register.
3. Get the first number in register A .
4. Get the second number in Memory.
5. Compare and check for carry. If no carry, move the largest number in
accumulator
6. If carry decrement the count and compare for remaining numbers.
7. Check for the remaining comparison other than the smallest number.
8. Store the result.
9. Halt the program
DESCENDING ORDER PROGRAM
MOV R7,#04H //Get count in Register R7 for number of entries//
GO: MOV R0,#50H //Initialize the memory location//
MOV R6,#04H //Get count in Register R6 for number of comparisons//
MOV A,@R0 //Get first number in A//
INC R0 //increment R0//
NEXT: MOV F0H,@R0 //Get next number in register B//
CJNE A,F0H, LOOP //Compare A and B registers, if not equal jump to LOOP//
SJMP UP //Short jump to the specified location//
LOOP: JNC UP //Is Carry present ? – No, jump to UP//
MOV @R0,A //Move largest number in Memory location //
DEC R0 //Decrement the memory location//
MOV @R0,F0H //Move the smallest number in next location//
UP: DJNZ R6, NEXT //R6=R6-1 and check R6. if not ‘0’ jump to NEXT//
DJNZ R7, GO //R6=R6-1 and check R6. if not ‘0’ jump to GO//
LCALL 0003H //Halt the program//

Data Result
50H – 04H 50H – 04H
51H – 02H 51H – 03H
52H – 01H 52H – 02H
53H – 03H 53H – 01H
ASCENDING ORDER PROGRAM
ALP TO ARRANGE A SET OF GIVEN NUMBERS IN
ASCENDING ORDER
Algorithm
1. Initialize the memory location
2. Get the count in one of the register.
3. Get the first number in register A .
4. Get the second number in Memory.
5. Compare and check for carry. If no carry, move the smaller number in
accumulator
6. If carry decrement the count and compare for remaining numbers.
7. Check for the remaining comparison other than the smallest number.
8. Store the result.
9. Halt the program
ASCENDING ORDER PROGRAM
MOV R7,#04H //Get count in Register R7 for number of entries//
GO: MOV R0,#50H //Initialize the memory location//
MOV R6,#04H //Get count in Register R6 for number of comparisons//
MOV A,@R0 //Get first number in A//
INC R0 //increment R0//
NEXT: MOV F0H,@R0 //Get next number in register B//
CJNE A,F0H, LOOP //Compare A and B registers, if not equal jump to LOOP//
SJMP UP //Short jump to the specified location//
LOOP: JC UP //Is Carry present ? – Yes, jump to UP//
MOV @R0,A //Move smallest number in Memory location //
DEC R0 //Decrement the memory location//
MOV @R0,F0H //Move the largest number in next location//
UP: DJNZ R6, NEXT //R6=R6-1 and check R6. if not ‘0’ jump to NEXT//
DJNZ R7, GO //R6=R6-1 and check R6. if not ‘0’ jump to GO//
LCALL 0003H //Halt the program//

Data Result
50H – 04H 50H – 01H
51H – 02H 51H – 02H
52H – 01H 52H – 03H
53H – 03H 53H – 04H
SUBROUTINES

 A subroutine is program that may be used many times in the


execution of a main program.
 It may be written in the anywhere in the body of main program
when it is needed.
 The programmer writes the main program first.
 Later on he writes the call for subroutine.
 The stack area of the internal RAM automatically stores the
address called the return address of the instruction found
immediately after the CALL.
SUBROUTINES

 A Call instruction is used to activate the subroutine


 On executing the call instruction, the address after the call
instruction is stored in the stack using PUSH operation.
 The subroutine program is then executed.
 When a RET instruction is executed, the subroutine returns back
to the main program.
 The stack loads the address which is stored using POP
instruction.
SUBROUTINES

8000 MOV R0,#70H Program Counter


M
A 8002 MOV A,@R0 PUSH 80 07H
I ……………….
N ……………….
………………. 80H 41H
P 8006 ACALL 8020H 07H 40H
R 8007………………… POP
Stack Area↑
O
…………………..
G
…………………….. SP
R
A …………………….. Internal RAM
M 8010 LCALL 0003H
8020 MOV R1,FFH
8021 DEC R1
……………….
SUBROUTINE
……………….
8025 RET
I/O PROGRAMMING
Configuring a port as output port
FFH - 11111111
MOV A,#FFH
MOV P1,A 1 1 1 1 1 1 1 1
ACALL DELAY
B7 B6 B5 B4 B3 B2 B1 B0
SJMP BACK

Configuring a port as input port


00H - 00000000
MOV A,#00H
0 0 0 0 0 0 0 0
MOV P1,A
ACALL DELAY B7 B6 B5 B4 B3 B2 B1 B0
SJMP BACK
I/O PROGRAMMING

Configuring a port as input/output port


AAH - 10101010
MOV A,#AAH
MOV P1,A 1 0 1 0 1 0 1 0
ACALL DELAY B7 B6 B5 B4 B3 B2 B1 B0
SJMP BACK
BIT MANIPULATION

Configuring a particular bit of a port as input/output port

CLR P0.2
ACALL DELAY X X 1 X X 0 X X
SETB P0.5 B7 B6 B5 B4 B3 B2 B1 B0
ACALL DELAY
SJMP BACK
GENERATION OF A WAVEFORM BY
ACCESSING A PORT
Create a square wave of 50% duty cycle on bit 0 of port 1.
Solution:
The 50% duty cycle means that the “on” and “off” state (or the high & low
portion of the pulse) have the same length. Therefore, we toggle P1.0 with a time
delay in between each state. Science

HERE: SETB P1.0 //set to high bit 0 of port 1//


LCALL DELAY //call the delay subroutine//
CLR P1.0 //Clear to low bit 0 of port 1//
LCALL DELAY //call the delay subroutine//
SJMP HERE //Continue the execution of the program//
8051

P1.0
8051 Microcontroller & its Applications

TIMER / COUNTER PROGRAMMING IN 8051


Timer/Counters

 Two 16-bit up counters, named T0 and T1, are provided for the general use
of the programmer.
Each counter may be programmed to count internal clock pulses, acting as a
timer, or programmed to count external pulses as a counter.
The counters are divided into two 8-bit registers called the timer low
(TL0,TL1) and high (TH0, TH1) bytes.
All counter action is controlled by bit states in the timer mode control register
(TMOD), the timer/counter control register (TCON) and certain program
instructions.
TMOD is dedicated to the two timers and can be consider two duplicate 4-bit
registers, each of which controls the action of the timers.
TCON has control bits and flags for the timers in the upper control bits and
flags for the external interrupts in the lower nibble.
Timer/Counters(cont..)
 These timers exist in the SFR area as pairs of 8- bit registers.
– TL0 (8AH) and TH0 (8CH) for Timer0.
– TL1 (8BH) and TH1 (8DH) for Timer1. (LSB is bit 0 ; MSB is bit 7)

When used as timers, the registers are incremented once per


machine cycle. – Each machine cycle is 12 clock cycles.

Count frequency = (system clock frequency) / 12

When used as counters, the registers will be incremented once on


every 1-0 (negative edge) on the appropriate input pin.
• T0 – P3.4
• T1 – P3.5

The pins must be held high for one complete machine cycle and then
low for one complete machine cycle.
Timer/Counters: Block Schematic
Timer/Counters: Application

The timers can be used for:


1. Interval timing
The timer is programmed to overflow at a regular interval
and set the timer overflow flag. Overflow means
reaching maximum count of FFFFH.

2. Event counting
Determine the number of occurrences of an event.
An event is any external stimulus that provides a 1-
to-0 transition on a pin of the µC.
TCON (Counter/Timer Control Register)

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

- TF1, TF0 : Overflow flags for Timer 1 and Timer 0.

- TR1, TR0 : Run control bits for Timer 1 and Timer 0.


Set to run, reset to hold.

- IE1, IE0 : Edge flag for external interrupts 1 and 0. *


Set by interrupt edge, cleared when interrupt is processed.

- IT1, IT0 : Type bit for external interrupts. *


Set for falling edge interrupts, reset for 0 level interrupts.

* = not related to counter/timer operation.


TMOD
GAT C/T M1 M0 C/T M1 M0
E GATE
Timer 1 Timer 0

- GATE : Permits INTx pin to enable/disable counter.


- C/T : Set for counter operation, reset for timer operation.
M1, M0 : Operating Mode select Bit 1/0. Set/Cleared by
program to select Mode

M1 M0 Mode
0 0 0
0 1 1
1 0 2
1 1 3
TIME DELAY CALCULATIONS
 The clock frequency of 8051 is 11.0592 MHz.
 CPU executing an instruction takes a certain number of
clock cycles.
 These are referred as to as machine cycles.
 The length of machine cycle depends on the frequency
of the crystal oscillator connected to 8051.
 In original 8051, one machine cycle lasts 12 oscillator
periods
TIME DELAY CALCULATIONS
Find the delay caused in the following program

Instructions Machine Cycles No. of times instruction executed


MOV R3,#200 1 1x1=1
HERE: DJNZ R3,HERE 2 200 x 2 = 400
RET 2 1x2=2
Total machine cycles = (1+400+2=403 cycles)
Hence the time delay is 403 x 1.085 µs = 437µs or 0.43ms
TIME DELAY CALCULATIONS
Find the delay caused in the following program

Instructions Machine Cycles No. of times instruction executed


MOV R3,#250 1 1 x 1= 1
HERE: NOP 1 1 x 250 = 250
NOP 1 1 x 250 = 250
NOP 1 1 x 250 = 250
NOP 1 1 x 250 = 250
DJNZ R3,HERE 2 2 x 250 = 500
RET 2 2x1=2
Total machine cycles = (1+250+250+250+250+500+2=1503 cycles)
Hence the time delay is 1503 x 1.085 µs = 1630µs or 1.6ms
8051 Microcontroller & its Applications

UNIT – 4
SERIAL COMMUNICATION
APPLICATIONS OF MICROCONTROLLER
8051 Microcontroller & its Applications

SERIAL COMMUNICATIONS
Basics of serial communication
Basics of serial communication
• To transfer to a device located many meters away,
the serial method is used.

• The data is sent one bit at a time.

• At the transmitting end, the byte of data must be


converted to serial bits using parallel-in-serial-out
shift register.

• At the receiving end, there is a serial-in-parallel-out


shift register to receive the serial data and pack
them into byte.
Basics of serial communication
• When the distance is short, the digital signal can be
transferred as it is on a simple wire and requires no
modulation.

• If data is to be transferred on the telephone line, it


must be converted from 0s and 1s to audio tones.

• A protocol is a set of rules agreed by both the sender


and receiver on
 How the data is packed
 How many bits constitute a character
 When the data begins and ends
Types of Serial communications
METHODS OR MODES OF SERIAL
COMMUNICATION

Modes of operation

 Synchronous Mode – Block of data is


transferred.
 Asynchronous Mode-Data transmitted bit
by bit.
ASYNCHRONOUS MODE
 Asynchronous serial data communication is widely
used for character-oriented transmissions.
 Each character is placed in between start and stop
bits, this is called framing.
 Block-oriented data transfers use the synchronous
method.
ASYNCHRONOUS MODE
ASYNCHRONOUS MODE
 The start bit is always one bit, but the stop bit can be one or
two bits.
 The start bit is always a 0 (low) and the stop bit(s) is 1 (high).
 Assuming that we are transferring a text file of ASCII
characters using 1 stop bit, we have a total of 10 bits for each
character.
 This gives 25% overhead, i.e. each 8-bit character with an
extra 2 bits
ASYNCHRONOUS MODE
 The rate of data transfer in serial data
communication is stated in bps (bits per second)

 Another widely used terminology for bps is baud


rate. It is modem terminology and is defined as the
number of signal changes per second
RS 232 STANDARD OR PORT
 In telecommunications, RS-232, Recommended
Standard 232 is a standard originally introduced by
Electronic Standard Association (EIA) in 1960 serial
communication of data.

 The standard defines the electrical characteristics


and timings of signals, meaning of the signals, size
etc.

 Personal computer not only use this to connect


modems but also printers, Mouse, data storage
devices
RS 232 STANDARD OR PORT
RS 232 STANDARD OR PORT
Current terminology classifies data communication equipment as
 DTE (data terminal equipment) refers to terminal and
computers that send and receive data.

 DCE (data communication equipment) refers to


communication equipment, such as modems.

 The simplest connection between a PC and microcontroller


requires a minimum of three pins, TxD, RxD, and ground
RS 232 STANDARD OR PORT
DTR (data terminal ready). When terminal is turned on,
it sends out signal DTR to indicate that it is ready for
Communication

DSR (data set ready) : When DCE is turned on and has


gone through the self-test, it assert DSR to indicate that
it is ready to communicate.

RTS (request to send) : When the DTE device has byte


to transmit, it assert RTS to signal the modem that it
has a byte of data to transmit.
RS 232 STANDARD OR PORT
CTS (clear to send) : When the modem has room for
storing the data it is to receive, it sends out signal CTS
to DTE to indicate that it can receive the data now.

DCD (data carrier detect) : The modem asserts signal DCD


to inform the DTE that a valid carrier has been
detected and that contact between it and the other
modem is established

RI (ring indicator) : An output from the modem and an


input to a PC indicates that the telephone is ringing. It
goes on and off in synchronous with the ringing sound
RxD and TxD pins in the 8051

 The 8051 has two pins for transferring and


receiving data by serial communication. These
two pins are part of the Port3(P3.0 &P3.1)
 These pins are TTL compatible and hence they
require a line driver to make them RS232
compatible
 Serial communication is controlled by an 8-bit
register called SCON register, it is a bit
addressable register.
SCON : Serial Control Register
SMO SM1 SM2 REN TB8 RB8 TI RI

- SM0, SM1 = Serial Mode:


00 = Mode 0 : Shift register I/O expansion.
01 = Mode 1 : 8-bit UART with variable baud rate.
10 = Mode 2 : 9-bit UART with fixed baud rate.
11 = Mode 3 : 9-bit UART with variable baud rate.
- SM2 : It enables the multiprocessor communication feature in
Mode 2 & Mode 3
- REN = Enables receiver.
- TB8 = Ninth bit transmitted (in modes 2 and 3).
- RB8 = Ninth bit received:
Mode 0 : Not used.
Mode 1 : Stop bit.
Mode 2,3 : Ninth data bit.
- TI = Transmit interrupt flag.
- RI = Receive interrupt flag.
SM0 , SM1
 These two bits of SCON register determine the
framing of data by specifying the number of bits per
character and start bit and stop bits. There are 4
serial modes.
SM0 SM1
0 0 Serial Mode 0
0 1 Serial Mode 1, 8 bit data,
1 stop bit, 1 start bit
1 0 Serial Mode 2
1 1 Serial Mode 3
REN
• REN (Receive Enable) also referred as SCON.4.
When it is high,it allows the 8051 to receive
data on the RxD pin. So to receive and transfer
data REN must be set to 1.When REN=0,the
receiver is disabled. This is achieved as below
SETB
SCON.4 & CLR
SCON.4
TI , RI
• TI (Transmit interrupt) is the D1 bit of SCON register.
When 8051 finishes the transfer of 8-bit character, it
raises the TI flag to indicate that it is ready to transfer
another byte. The TI bit is raised at the beginning of
the stop bit.
• RI (Receive interrupt) is the D0 bit of the SCON
register. When the 8051 receives data serially ,via
RxD, it gets rid of the start and stop bits and places
the byte in the SBUF register. Then it raises the RI flag
bit to indicate that a byte has been received and
should be picked up before it is lost. RI is raised
halfway through the stop bit.
8051 Microcontroller & its Applications

APPLICATION OF MICROCONTROLLER
8051 Microcontroller & its Applications

APPLICATIONS

Temperature Measurement

LCD Interfacing

DAC & ADC Interfacing

Stepper Motor

Keyboard Interfacing
8051 Microcontroller & its Applications
8051 Interfacing LCD (Liquid Crystal Display)

LCD is finding widespread use replacing LEDs


 The declining prices of LCD.
 The ability to display numbers, characters, and
graphics.
 Incorporation of a refreshing controller into the LCD,
thereby relieving the CPU of the task of refreshing
the LCD.
 Ease of programming for characters and graphics
8051 Interfacing LCD (Liquid Crystal
Display)
8051 Interfacing LCD (Liquid Crystal
Display)
8051 Interfacing LCD (Liquid Crystal
Display)
8051 Interfacing LCD (Liquid Crystal
Display)
P0 D0 +5V
VCC

VEE 10K
P7 D7 LCD
8051 VSS POT
RS R/W E
P3.2

P3.3
LCD Program
• org 0000h
• mov a,#38h ; INITIALIZE 2 line 5x7
• acall comm ;CALL COMMAND SUBROUTINE
• acall delay ;GIVE LCD SOME TIME

• mov a,#0eh ;DISPLAY ON,CURSOR ON


• acall comm ;CALL COMMAND SUBROUTINE
• acall delay

• mov a,#01h ;clear LCD


• acall comm
• acall delay

• mov a,#06h ;shift cursor right


• acall comm
• acall delay

• mov a,#80h ;FORCE CURSOR TO BEGINNING OF FIRST LINE


• acall comm
• acall delay
LCD Program
• mov a,#„S'
• acall data1 ;CALL DISPLAY
ROUTINE
• acall delay

• mov a,#'R'
• acall data1
• acall delay

• mov a,#„E'
• acall data1
• acall delay

• mov a,#'S'
• acall data1
• acall delay
LCD Program
• comm: ;SEND COMMAND TO LCD
• mov p0,a ;COPY REG A TO PORT0
• clr p2.2 ;RS=0 FOR COMMAND
• clr p2.1 ;R/W=0 FOR WRITE
• setb p2.0 ;E=1
• clr p2.0 ;E=0
ret
• data1: ;WRITE DATA TO LCD
• mov p0,a ;COPY REG A TO PORT0
• setb p2.2 ;RS=1 FOR DATA
• clr p2.1 ;R/W=0 FOR WRITE
• setb p2.0 ;E=1
• clr p2.0 ;E=0
ret
• delay: mov r1,#255
• here2: mov r2,#255
• here: djnz r2,here
• djnz r1,here2
• Ret
• end
8051 Interfacing ADC

• Digital computers use binary We need Analog-to-Digital

values but in physical world converter (ADC) to translate


Analog signals to Digital number
everything is Analog
so that Microcontroller can read
(continuous) eg. Temperature,
and process them.
Pressure
• A physical quantity is converted
ADC0808 allows us to monitor up
into electrical (voltage, current) to 8 different analog input channels
signals using a device called using single chip

as Transducer (also referred


as Sensors).
ADC 0808
Address Lines (A,B,C)

Selected ADC • ADC0808 has 8-bit


C B A
channel
data output
IN0 0 0 0
IN1 0 0 1
• 8 analog input
IN2 0 1 0 channels are
IN3 0 1 1 multiplexed and
IN4 1 0 0
selected using three
IN5 1 0 1
IN6 1 1 0
address pins A, B & C.
IN7 1 1 1
Signals for A/D Conversion

• Address Latch Enable (ALE): A LOW-TO-HIGH signal at this

pin will latch the above-selected address and selected the


respective channel for ADC conversion.
• START Conversion (SC): The A/D converter‟s successive

approximation register (SAR) is reset on the positive edge of the


start conversion (SC) pulse. Thus we need to generate a LOW-
HIGH pulse for starting the ADC conversion.
• End of Conversion (EOC): Once the conversion is over, this

pin is pulled HIGH by ADC0808. This pin needs to be monitored


for the conversion to complete and then read the data.
Signals for A/D Conversion

• Output Enable(OE): ADC0808 does the A/D conversion and

holds the data in the internal registers. A HIGH signal on this pin
will bring the data on the output lines.
• D0-D7 are the digital Data output lines.
8051 Interfacing ADC
TEMPERATURE MEASUREMENT

What is Data Acquisition or Signal Conditioning ?


 Data acquisition begins with the physical phenomenon or
physical property to be measured.
 Examples of this include temperature, light intensity, gas
pressure, fluid flow, and force.
 Regardless of the type of physical property to be measured,
the physical state that is to be measured must first be
transformed into a unified form that can be sampled by a
data acquisition system.
TEMPERATURE MEASUREMENT

 The task of performing such transformations falls on devices


called sensors.
 A data acquisition system is a collection of software and
hardware that allows one to measure or control physical
characteristics of something in the real world.
 Data acquisition is the process of sampling signals that
measure real world physical conditions and converting the
resulting samples into digital numeric values that can be
manipulated by a computer
8051 Interfacing Temperature Sensor (LM34/LM35)

• Temperature is converted to electrical signals using a

transducer called as Thermistor.


• Simple and widely used Temperature sensors include LM34 &

LM35 from Semiconductor Corp


• LM34 does not require external calibration since it is

internally calibrated.
• LM35 are precision integrated circuit temperature sensors whose

output voltage is linearly proportional to Celsius temp. It does not


require external calibration since it is internally calibrated. It outputs
10mV for each degree of centigrade temperature.
Signal Conditioning
• Sensors produce the output
in the form of voltage, Analog World
(temperature, pressure)
current, charge, resistance &
capacitance. Transducer (Sensor)
• But, we need to convert these

signals to voltage in order to


Signal Conditioning
send input to ADC.
• This conversion is called as
ADC
Signal conditioning.
• Signal conditioning can be
Microcontroller
current to voltage conversion
or signal amplification.
8051 Interfacing Temperature Sensor (LM34/LM35)
Connecting LM35 to ADC
• Since ADC has 8 bit
Temperature Vs Vout for ADC resolution with a max
of 256 steps and LM35
Temp (C) Vin (mV) Vout produces 10mV for
(D7- every degree of
D0) temperature change,
0 0 0000 0000 we can condition Vin of
1 10 0000 0001 the ADC to produce a
2 20 0000 0010 Vout of 2.56V for full
3 30 0000 0011 scale output.
10 100 0000 1010 • So Vref= 2.56
30 300 0001 1110
8051 Interfacing DAC

• Microcontroller are used in wide variety of applications like for

measuring and control of physical quantity like temperature,

pressure, speed, distance, etc.

• In these systems microcontroller generates output which is in

digital form but the controlling system requires analog signal as

they don't accept digital data thus making it necessary to use

DAC which converts digital data into equivalent analog voltage


8051 Interfacing DAC

• Digital to Analog Converter is a device used to convert digital

pulses to analog signals.

• In the figure shown, we use 8-bit DAC 0808. This IC converts

digital data into equivalent analog Current. Hence we require an


I to V converter to convert this current into equivalent voltage.

• DAC0808 provides 256 discrete voltage (or current) levels of

output.
DAC 0808

• In the MC1408 (DAC0808), the digital inputs are converted to

current (Iout), and by connecting a resistor to the Iout pin, we convert


the result to voltage.

• The total current provided by the Iout pin is a function of the binary

numbers at the D0 – D7 inputs of the DAC0808 and the reference


current (Iref), and is as follows:

• Where D0 is the LSB, D7 is the MSB for the inputs, and Iref is the

input current that must be applied to pin 14. The Iref current is
generally set to 2.0 mA.
DAC 0808
Interfacing Diagram
8051 Interfacing Stepper Motor
 Stepper motor is a widely
used device that
translates electrical pulses
into
mechanical movement
 Stepper motor is used in
applications for position
control such as
 disk drives
 dot matrix printer
 robotics etc.
 Stepper motors commonly
have a permanent
magnet rotor (shaft)
surrounded by a stator
8051 Interfacing Stepper Motor
 Commonly used stepper motors  There are 8 main poles on
have four stator windings that the stator, each having 5
are paired with a center–tapped tooth in the pole face
commonly.

 Such motors are called as four-


phase or unipolar stepper
motor. It has a permanent
magnet rotor (shaft) which is
surrounded by a stator.

 A practical PM stepper motor


will have 1.8 degrees step angle
and 50 tooth on its rotor.
8051 Interfacing Stepper Motor
4 step Sequence of rotation

Step Winding A Winding B Winding C Winding D Data in


HexaD A
n
C t
1 1 0 0 1 l i
09h
o
c
k C
2 1 1 0 0 0Ch
l
W o
i c
3 0 1 1 0 06h s k
e
W
4 0 0 1 1 03h i
s
e
Step Angle
• Step angle is defined as the minimum degree of rotation
associated with a single step.

• Total number of steps needed to rotate 360 degrees


is
Steps per Revolution.

No of steps per revolution = 360° / step angle

• Steps per second = (rpm x steps per revolution) / 60

• Example: step angle = 2°

• No of steps per revolution = 180


Interfacing Diagram
8051 Interfacing Keyboard
• Keyboards are organized in a matrix of rows and columns.

• The CPU processes both rows and columns through ports

• So, with two 8-bit ports, an 8X8 matrix of keys can be


connected to the controller.

• When a key is pressed, a row and a column make a contact.


Otherwise there is no connection

• In microcontroller, programs stored in EPROM scan the keys


continuously.

• In the following figure, 4 X 4 matrix connected to two ports. rows are


connected to output ports and columns are connected to an input
port.
Matrix Keyboard connection to Ports
Scanning & Identifying Key
• It is a function of microcontroller to scan the keyboard
continuously to detect and identify key pressed

• To detect Pressed key microcontroller grounds all rows by providing 0


to output latch, then it reads the columns

• If the data read from columns is D3-D0 = 1111 , no key is pressed and
the process continues till key press is detected.

• If one of the column bits has a zero, means key press has occurred.

• If D3-D0 = 1101 this means that key in the column D1 has been
pressed.

• After detecting a key press, microcontroller will go through the


process of identifying key.
Scanning & Identifying Key
• Starting with the top Row, the microcontroller grounds it
by providing a low to Row D0 only
• If reads a column if data read is all 1s, no key in that row is
activated and the process is moved to the next row.

• It grounds the next row, reads the columns, and checks


for any zero
• This process continues until row is identified

• After identification of the Row in which Key has


been pressed, find out column the key press
belongs to
TEMPERATURE MEASUREMENT

What is Data Acquisition or Signal Conditioning ?


 Data acquisition begins with the physical phenomenon or
physical property to be measured.
 Examples of this include temperature, light intensity, gas
pressure, fluid flow, and force.
 Regardless of the type of physical property to be measured,
the physical state that is to be measured must first be
transformed into a unified form that can be sampled by a
data acquisition system.
TEMPERATURE MEASUREMENT

 The task of performing such transformations falls on devices


called sensors.
 A data acquisition system is a collection of software and
hardware that allows one to measure or control physical
characteristics of something in the real world.
 Data acquisition is the process of sampling signals that
measure real world physical conditions and converting the
resulting samples into digital numeric values that can be
manipulated by a computer

You might also like