Академический Документы
Профессиональный Документы
Культура Документы
APPLICATIONS
MOON INDER SINGH
Introduction
General-purpose microprocessor
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example:Intel’s x86, Motorola’s 680x0
Microprocessor Microcontroller
• CPU is stand-alone, RAM, • CPU, RAM, ROM, I/O and
ROM, I/O, timer are separate timer are all on a single chip
• designer can decide on the • fix amount of on-chip ROM,
amount of ROM, RAM and RAM, I/O ports
I/O ports. • for applications in which cost,
• expansive power and space are critical
• versatility • single-purpose
• general-purpose
Ex2
FEATURES OF 8051
BLOCK DIAGRAM OF 8051
Comparison of 8051 family members.
Classification of Micro controllers on the basis of
memory.
8751 microcontroller: The PROM used in this processor is
• UV-EPROM.
• It requires a separate PROM burner and an eraser. It possesses a window
on the IC on to which the UV light is beamed.
• UV-EPROM eraser takes 15-20 min to erase.
Disadvantage: On line programming is not possible.
• Sol 1+2+3+4+5
NESTED LOOP
• A LOOP inside a loop is called nested loop
Jump instructions
• There are two types of jump instructions used
in the program
• Conditional and unconditional jumps.
• In the jump instructions the target address
needs to be specified.
• In conditional jumps, the jump occurs if a
particular condition is met
CONDITIONAL JUMP INSTRUCTIONS
• The format of addressing is same in the
instructions JZ,JNZ, JC, JNC.
• Ex. JZ target address, JNC target address
• All conditional jumps are short jumps
• The address of the target must be within -128 to +127 bytes of the
contents of PC
• The unconditional jump is a jump in which control is transferred
unconditionally to the target location
LJMP (long jump)
• 3-byte instruction
• First byte is the opcode
• Second and third bytes represent the 16-bit target address
• The two byte target address allows to jump to any memory location from
0000 to FFFFH
SJMP (short jump)
• 2-byte instruction
• First byte is the opcode
• Second byte is the relative target address 00 to FFH (forward +127 and
backward -128 bytes from the current PC)
Jump target address calculation
• In case of forward jump, the displacement
value is a positive number between 00 to
7FH(0 to 127) where as in case of a backward
jump, the displacement value is a negative
number between -128 to 0
• To calculate the target address of a short jump
(SJMP, JNC, JZ, DJNZ, etc.)
• The second byte(relative target address) is
added to the PC of the instruction
immediately below the jump
• If the target address is more than -128 to +127
bytes from the address below the short jump
instruction the assembler will generate an
error stating the jump is out of range
Ex.
Line PC Opcode Mnemonic Operand
01 0000 ORG 0000
02 0000 7800 MOV R0,#0
03 0002 7455 MOV A,#55H
04 0004 6003 JZ NEXT
05 0006 08 INC R0
06 0007 04 AGAIN: INC A
07 0008 04 INC A
08 0009 2477 NEXT: ADD A,#77H
09 000B 5005 JNC OVER
10 000D E4 CLR A
11 000E F8 MOV R0,A
12 000F F9 MOV R1,A
13 0010 FA MOV R2,A
14 0011 FB MOV R3,A
15 0012 2B OVER: ADD A,R3
16 0013 50F2 JNC AGAIN
17 0015 80FE HERE: SJMP HERE
18 0017 END
Call instructions
• Call instruction is used to call subroutine
• Subroutines are often used to perform tasks that need to be
performed frequently
• This makes a program more structured in addition to saving
memory space
• LCALL (long call)
• 3-byte instruction
• First byte is the opcode
• Second and third bytes are used for address of target subroutine
• Subroutine is located anywhere within 64K byte address space
ACALL (absolute call)
• 2-byte instruction
11 bits are used for address within 2K-byte range
• When a subroutine is called, control is transferred
to that subroutine, the processor Saves on the
stack the address of the instruction immediately
below the LCALL(PC)
• Begins to fetch instructions form the new
location(Target address specified with the call
instruction)
• After finishing execution of the subroutine the
instruction RET transfers control back to the caller
• Every subroutine needs RET as the last instruction
• 001 0000 ORG 0
• 002 0000 7455 BACK: MOV A,#55H ;load A with 55H
• 003 0002 F590 MOV P1,A ;send 55H to p1
• 004 0004 120300 LCALL DELAY ;time delay
• 005 0007 74AA MOV A,#0AAH ;load A with AAH
• 006 0009 F590 MOV P1,A ;send AAH to p1
• 007 000B 120300 LCALL DELAY
• 008 000E 80F0 SJMP BACK ;keep doing this
• 009 0010
• 010 0010 ;-------this is the delay subroutine------
• 011 0300 ORG 300H
• 012 0300 7DFF DELAY: MOV R5,#0FFH ;R5=255
• 013 0302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here
• 015 0304 22 RET ;return to caller
• 016 0305 END ;end of asm file
• The only difference between ACALL and LCALL
is
• The target address for LCALL can be anywhere
within the 64K byte address
• The target address of ACALL must be within a
2K-byte range
• The use of ACALL instead of LCALL can save a
number of bytes of program ROM space
Time delay
• 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 i.e 1machine cycle is 1/921.6
kHz(12/11.0592M) = 1.085μs(M=1000)
• For 8051 system of 11.0592 MHz, find how
long it takes to execute each instruction.
(a) MOV R3,#55 (b) DJNZ R2 target (c) LJMP
(d) SJMP (e) MUL AB
(a) 1 1x1.085μs = 1.085μs
(b) 2 2x1.085μs = 2.17μs
(c) 2 2x1.085μs = 2.17μs
(d) 2 2x1.085μs = 2.17μs
(e) 4 4x1.085μs = 4.34μs
Find the size of the delay in following
program, if the crystal
frequency is 11.0592MHz
DELAY: MOV R3,#200 (1)
HERE: DJNZ R3,HERE (2)
RET (2)
[(200x2)+1+2]x1.085μs = 436.255μs.
• Find the size of the delay in following program, if
the crystal frequency is 11.0592MHz.
DELAY: MOV R3,#250 (1)
HERE: NOP (1)
NOP (1)
NOP (1)
NOP (1)
DJNZ R3,HERE (2)
RET (2)
• Solution:1630.755μs
• Find the size of the delay in following program, if the crystal
• frequency is 11.0592MHz.
• Solution:
AT8051 DS89C4x0
(a) 1X1085ns = 1085ns 2X90ns = 180ns
(b) 1X1085ns = 1085ns 1X90ns = 90ns
(c) 2X1085ns = 2170ns 4X90ns = 360ns
(d) 2X1085ns = 2170ns 3X90ns = 270ns
(e) 2X1085ns = 2170ns 3X90ns = 270ns
(f) 1X1085ns = 1085ns 1X90ns = 90ns
(g) 4X1085ns = 4340ns 9X90ns = 810ns
Write a program to generate a delay of 200ms for AT89C51
and DS89C420/30/40 if crystal frequency =11.0592 MHZ
• Solution
ADDRESSING MODES
• The various ways of accessing the data are
called addressing modes. In 8051 the CPU can
access data in 5 ways,
• Immediate
• Register
• Direct
• Register indirect
• Indexed
Immediate addressing mode
• In this mode the source operand is a constant
and is in the instruction itself
• The immediate data must be preceded by
the pound sign, “#”
• Can load information into any registers,
including 16-bit DPTR register
ex
• MOV A,#25H ;load 25H into A
• MOV R4,#62 ;load 62 into R4
• MOV B,#40H ;load 40H into B
• MOV DPTR,#4521H ;DPTR=4512H
• MOV DPL,#21H ;This is the same
• MOV DPH,#45H ;as above
• MOV DPTR,#68975 ;illegal!! Value > 65535
(FFFFH)
Register addressing mode
• Use registers to hold the data to be
manipulated
• The source and destination registers
must match in size. MOV DPTR,A will give an
error
• The movement of data between Rn registers is
not allowed
• MOV A,R0 ;copy contents of R0 into A
• MOV R2,A ;copy contents of A into R2
• ADD A,R5 ;add contents of R5 to A
• ADD A,R7 ;add contents of R7 to A
• MOV R6,A ;save accumulator in R6
• MOV DPTR,#25F5H
• MOV R7,DPL
• MOV R6,DPH
Direct Addressing Mode
• It is most often used to access RAM locations 30 –
7FH
• The entire 128 bytes of RAM can be accessed
• Contrast this with immediate addressing mode
There is no “#” sign in the operand.
Ex.
• MOV R0,40H ;save content of 40H in R0
• MOV 56H,A ;save content of A in 56H
• MOV A,4 ;is same as
• MOV A,R4 ;which means copy R4 into A
Register
Indirect Addressing Mode
• A register is used as a pointer to the data
• Only register R0 and R1 are used for this
purpose
• R2 – R7 cannot be used to hold the address of
an operand located in RAM
• When R0 and R1 hold the addresses of RAM
locations, they must be preceded by the “@”
sign
Indexed addressing mode
• Indexed addressing mode is widely used in
accessing data elements of look-up table
entries located in the program ROM.
• The instruction used for this purpose is
MOVC A,@A+DPTR “C” means code
• The contents of A are added to the 16-bit
register DPTR to form the 16-bit address of
the needed data i.e A+DPTR points to the
address where the code is stored
• Another way is to use MOVC A,@A+PC
• The DPTR can be initialised( address can be
read into it) by using instructions
• MOV DPTR,#100h
• or MOV DPTR,#label
Write a program to store the word THAPAR in ROM
memory address starting from 90h and copy the code
in RAM starting from 3FH
Special Function Registers
• The Special Function Register or SFR 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 SFRs. The unused locations 80H to FFH are
reserved and must not be used by the 8051
programmer.(NOTE: See the specification
sheets of aduc812)
BIT ADDRESSABLE RAM, SFRs,I/O PORTS
• One unique and powerful feature of the 8051 is
single-bit operation
• Single-bit instructions allow the programmer to set,
clear, move, and complement individual bits of a
port, memory, or register
• It is registers, RAM, and I/O ports that need to be bit-
addressable
• ROM, holding program code for execution, is not bit-
addressable
• The bit-addressable RAM locationS are 20H to
2FH
• These 16 bytes provide 128 bits of RAM bit-
addressability, since 16 × 8 = 128 ie 0 to 127 (in
decimal) or 00 to 7FH
• The first byte of internal RAM location 20H has
bit address 0 to 7H
• The last byte of 2FH has bit address 78H to 7FH
• Internal RAM locations 20-2FH are both byte-
addressable and bit addressable
• Bit address 00-7FH belong to RAM byte
addresses 20-2FH
• The move instruction can be used to alter a bit
in the bit addressable registers
• MOV destbit,source bit
• But in this instruction one of the operands
must be the carry bit.
The last byte
of 2FH has bit
address 78H to
7FH
TB1
Read pin
Port 0 with Pull-Up Resistors
Hardware Structure of I/O Pin
• Each pin of I/O ports has
– Internal CPU bus:communicate with CPU
– A D latch store the value of this pin
• D latch is controlled by “Write to latch”
– Write to latch=1:write data into the D latch
– 2 Tri-state buffer:
• TB1: controlled by “Read pin”
– Read pin=1:really read the data present at the pin
• TB2: controlled by “Read latch”
– Read latch=1:read value from internal latch
– A transistor M1 gate
• Gate=0: open
• Gate=1: close
P1-P3 port structure
• In order to make port 0 an input, the port must
be programmed by writing 1 to all the bits
• To read the input pin a 1 is written to the port bit.
• A 1 written to the port bit is written to the port
latch that results in Q=1 and /Q =0.
• Since /Q connects to M1 transistor, M1 transistor
is off and it blocks any path to the ground for any
signal connected to port pin but directed to
tristate buffer1. Now MOV a,P0 brings the data
present at the pin.
• In reading a port some instructions read the
status of port pins others read the status of an
internal port latch
• Therefore, when reading ports there are two
possibilities:
• Read the status of the input pin
• Read the internal latch of the output port
Reading the status of input pin
• Configure the port/port pin as input port/pin
Read latch Vcc
TB2 2. MOV A,P1
1. write a 1 to the pin (MOV Load(L1)
external
P1,#0FFH) 1 pin=High/low
Load(L1)
Read pin
Writing 0 to the port pin
Load(L1)
0 P1.X
Internal CPU D Q
bus P1.X pin
1 output 0
Write to latch Clk Q M1
Read pin
Read, modify and write instruction
• These instructions read the port latch , perform
an operation(modify its value) then rewrite it
back to the port latch. ex ANL P1,A
1. The read latch activates the tristate buffer TB2
and brings the data from Q latch into the CPU
2 This data is AND ed with the contents of
register A
3. The result is rewritten back to the port latch
4. The port pin data is changed and now has the
• same value as port latch
Arithmetic and logic instructions
• Addition of Unsigned Numbers
• ADD instruction: The instruction ADD is used
to add two operands
• Destination operand is always in register A
• Source operand can be a register, immediate
data, or in memory
• Memory-to-memory arithmetic operations are
never allowed in 8051 Assembly language
• The flags affected by the ADD instruction are
C,AC,P and OV flags.
• Ex(CONSIDERING DIFFERENT ADDRESSING
MODES)
• ADD A,#20H
• ADD A,Rn
• ADD A,20H
• ADD A@Ri i=0 or 1
ADDC A,SOURCE BYTE
It stands for add with carry i.e A=A+Source byte + carry.
8-bit addition between the accumulator, a second
operand and the previous value of the CY flag.
• Useful for 16-bit addition
• Examples considering different addressing modes
• ADDC A,#20H
• ADDC A,Rn
• ADDC A,20H
• ADDC A,@Ri i=0 or 1
Write a programme to add two 32 bit numbers,one no. is stored 40h
onwards and the other is stored 50 h onwards. Store the result 60h
onwards.
• Solution: let the two numbers be 12ED2025H
and 34C121E5H
•
Subtraction of unsigned numbers
• In the 8051 only one instruction SUBB meaning
subtract with borrow is used instead of SUB
instruction.
• The 8051 uses adder circuitry to perform the
subtraction rather than using separate circuitry to
perform exclusive subtract instruction.
SUBB A,source ;A = A – source – CY
#Notice that the CY flag is being used for the borrow
ORG 30H
MAIN:
....
END
• Example 11-2
• Write a program that continuously get 8-bit data from P0
and sends it to P1 while simultaneously creating a square
wave of 200 μs period on pin P2.1. Use timer 0 to create
the square wave. Assume that XTAL = 11.0592 MHz.
• Solution: If we use timer 0 in mode 2 (auto reload). TH0 =
100/1.085 us = 92
upon wake-up go to main, avoid using memory allocated
to Interrupt Vector Table
ORG 0000H
LJMP MAIN ;by-pass interrupt vector table ISR for timer 0
to generate square wave
ORG 000BH ;Timer 0 interrupt vector table
CPL P2.1 ;toggle P2.1 pin
RETI ;return from ISR
ORG 0000H
LJMP MAIN ;by-pass interrupt vector table ISR for timer 0 to
generate square wave
ORG 000BH ;Timer 0 interrupt vector table
CPL P2.1 ;toggle P2.1 pin
RETI ;return from ISR
ORG 0030H ;after vector table space
Solution:
If these three interrupts are activated at the same
time, they are latched and kept internally. Then
the 8051 checks all five interrupts according to
the default sequence . If any is activated, it
services it in sequence. Therefore, when the
above three interrupts are activated, IE0 (external
interrupt 0) is serviced first, then timer 0 (TF0),
and finally IE1 (external interrupt 1).
• The sequence of servicing interrupts can be
altered by assigning a higher priority to the
particular interrupt(s) by programming a register
called IP (interrupt priority). Upon reset ihe bits in
IP register are all cleared resulting in the default
priority polling of interrupts.
• To give a higher priority to any of the interrupts,
set the corresponding bit in the IP register high
• When two or more interrupt bits in the IP register
are set to high, these interrupts have a higher
priority than others, they are serviced according
to the sequence.
INTERFACING
(LCD)
• One of the most common devices attached to an 8051
is an LCD display. Some of the most common LCDs
connected to the 8051 are 16x2 and 20x2 displays. This
means 16 characters per line by 2 lines and 20
characters per line by 2 lines, respectively.
• Fortunately, a very popular standard exists which
allows USERS to communicate with the vast majority of
LCDs regardless of their manufacturer. The standard is
referred to as HD44780U, which refers to the controller
chip which receives data from an external source (in
this case, the 8051) and communicates directly with
the LCD.
• The 44780 standard requires 3 control lines as well as
either 4 or 8 I/O lines for the data bus. The user may select
whether the LCD is to operate with a 4-bit data bus or an 8-
bit data bus. If a 4-bit data bus is used the LCD will require a
total of 7 data lines (3 control lines plus the 4 lines for the
data bus). If an 8-bit data bus is used the LCD will require a
total of 11 data lines (3 control lines plus the 8 lines for the
data bus).
• The three control lines are referred to as EN, RS, and RW.
• The EN line is called "Enable." This control line is used to
tell the LCD that you are sending it data. To send data to the
LCD, your program should make sure this line is low (0) and
then set the other two control lines and/or put data on the
data bus. When the other lines are completely ready, bring
EN high (1) and wait for the minimum amount of time
required by the LCD datasheet (this varies from LCD to
LCD), and end by bringing it low (0) again.
• The RS line is the "Register Select" line. When RS is low (0),
the data is to be treated as a command or special
instruction (such as clear screen, position cursor, etc.).
When RS is high (1), the data being sent is text data which
sould be displayed on the screen. For example, to display
the letter "T" on the screen you would set RS high.
• The RW line is the "Read/Write" control line. When RW is
low (0), the information on the data bus is being written to
the LCD. When RW is high (1), the program is effectively
querying (or reading) the LCD. Only one instruction ("Get
LCD status") is a read command. All others are write
commands. So RW will almost always be low.
• Finally, the data bus consists of 4 or 8 lines (depending on
the mode of operation selected by the user). In the case of
an 8-bit data bus, the lines are referred to as DB0, DB1,
DB2, DB3, DB4, DB5, DB6, and DB7.
Pin description of 14 pin LCD
• To send any of the commands to the LCD, make
pin RS=0. For data,make RS=1. Then send a high-
to-low pulse to the E pin to enable the internal
latch of the LCD. This is shown in the code below.
• ;calls a time delay before sending next
data/command
• ;P1.0-P1.7 are connected to LCD data pins D0-D7
• ;P2.0 is connected to RS pin of LCD
• ;P2.1 is connected to R/W pin of LCD
• ;P2.2 is connected to E pin of LCD
ORG 000h
MOV A,#38H ;INIT. LCD 2 LINES, 5X7 MATRIX
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#0EH ;display on, cursor on
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#01 ;clear LCD
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#06H ;shift cursor right
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#84H ;cursor at line 1, pos. 4
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#’N’ ;display letter N
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#’O’ ;display letter O
ACALL DATAWRT ;call display subroutine
AGAIN: SJMP AGAIN ;stay here
COMNWRT: ;send command to LCD
MOV P1,A ;copy reg A to port 1
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse
CLR P2.2 ;E=0 for H-to-L pulse
RET
• MOV A,#66H
• BACK: MOV P1,A
• RR A
• ACALL DELAY
• SJMP BACK
• DELAY: MOV R1,#0FFH
• MOV R2,#100
• DJNZ ………
• ……………
• RET
To rotate a motor by 64degrees if step size is 2degrees
• Step size 2 deg mean there are total of 180 steps to make
one revolution.
• No of rotor teeth =180/4=45
• That means after four step movement the motor will rotate
by 8 degrees
• So for 64 degrees the four steps should be executed 8
times.
• MOV A,#66H
• MOV R1,#32
BACK:MOV P1,A
• RR A
• ACALL DELAY
• DJNZ R1,BACK