Co2 8051

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 79

Department of

ECE

COURSE:
PROCESSORS AND CONTROLLERS

COURSE CODE:22EC2106
Topic:
CO 2- 8051
The necessary tools for a
microprocessor/controller
Data Bus
CPU
General-
Serial
Purpose RAM ROM I/O Timer COM
Micro- Port Port
processor

Address Bus

General-Purpose Microprocessor System

CPU RAM ROM

Serial
I/O Timer COM Microcontroller
Port
Port

Example : Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X


Block Diagram of 8051
External interrupts
On-chip Timer/Counter

Interrupt ROM for


On-chip Timer 1 Counter
Control program
code RAM Timer 0 Inputs

CPU

Bus Serial
4 I/O Ports
OSC Control Port

P0 P1 P2 P3 TxD RxD
Address/Data
Features of 8051 Microcontroller
• An 8051 microcontroller comes bundled with the following features

• 4KB bytes on-chip program memory (ROM)
• 128 bytes on-chip data memory (RAM)
• Four register banks
• 128 user defined software flags
• 8-bit bidirectional data bus
• 16-bit unidirectional address bus
• 32 general purpose registers each of 8-bit
• 16 bit Timers (usually 2, but may have more or less)
• Three internal and two external Interrupts
• Four 8-bit ports,(short model have two 8-bit ports)
• 16-bit program counter and data pointer
• 8051 may also have a number of special features such as UARTs,
ADC, Op-amp, etc.
Architecture of 8051
Comparison of the 8051 Family Members
Pin Description of the 8051
Port 3 Alternate Functions
RESET Value of Some 8051 Registers:

Register Reset Value


PC 0000
ACC 00
B 00
PSW 00
SP 07
DPTR 0000
RAM are all zero.
Registers
RAM memory space allocation in the 8051

7FH

Scratch pad RAM

30H
2FH
Bit-Addressable RAM

20H
1FH Register Bank 3

18H
17H Register Bank 2

10H
0FH Register Bank 1 )Stack(

08H
07H Register Bank 0
00H
8051 Flag bits and the PSW
register
8051 Flag bits and the PSW register
8051 Flag bits and the PSW
register
8051 PSW BANK SELECTION
8051 PSW BANK SELECTION
Stack in the 8051
• The register used to access 7FH
the stack is called SP (stack
Scratch pad RAM
pointer) register.
30H

• The stack pointer in the 8051 2FH


Bit-Addressable RAM
is only 8 bits wide, which
20H
means that it can take value 1FH Register Bank 3
00 to FFH. When 8051 18H
17H
powered up, the SP register 10H
Register Bank 2

contains value 07. 0FH Register Bank 1 )Stack(


08H
07H
Register Bank 0
00H
0BH 0BH 0BH 0BH

0AH 0AH 0AH 0AH F3

09H 09H 09H 12 09H 12

08H 08H 25 08H 25 08H 25


Example of STACK operation:
MOV R6,#25H
MOV R1,#12H
MOV R4,#0F3H
PUSH 6
PUSH 1
PUSH 4

Start SP=07H SP=08H SP=09H SP=0AH


8051- Addressing Modes
Immediate
Register
Direct
Register Indirect
Indexed
Immediate Addressing Mode
MOV A,#65H

MOV A,#’A’

MOV R6,#65H

MOV DPTR,#2343H

MOV P1,#65H
Register Addressing Mode
MOV Rn, A ;n=0,..,7
ADD A, Rn
MOV DPL, R6

MOV DPTR, A
MOV Rm, Rn
Direct Addressing Mode
Although the entire of 128 bytes of RAM can be accessed using direct addressing mode, it is most often used to access RAM loc. 30 – 7FH.

MOV R0, 40H


MOV 56H, A
MOV A, 4 ; ≡ MOV A, R4
MOV 6, 2 ; copy R2 to R6
; MOV R6,R2 is invalid !

SFR register and their address

MOV 0E0H, #66H ; ≡ MOV A,#66H


MOV 0F0H, R2 ; ≡ MOV B, R2
MOV 80H,A ; ≡ MOV P1,A
Register Indirect Addressing Mode
In this mode, register is used as a pointer to the data.

MOV A,@Ri ; move content of RAM loc. Where address is held by Ri into A
( i=0 or 1 )
MOV @R1,B

In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB instructions.
Example: Write a program to copy a block of 10 bytes from RAM location starting at 40H to RAM location starting at 60H.
Solution:
MOV R0,#40H ; source pointer
MOV R1,#60H ; destination pointer
MOV R2,#10 ; counter
BACK: MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R2,BACK
Indexed Addressing Mode And On-Chip ROM Access
This mode is widely used in accessing data elements of look-up table entries located in the program (code) space ROM at the 8051

MOVC A,@A+DPTR
A= content of address A +DPTR from ROM
Note:
Because the data elements are stored in the program (code ) space ROM of the 8051, it uses the instruction MOVC instead of MOV. The “C” means code.
Example Programs
Example Programs
Example Programs
Example Programs
8051- INSTRUCTION SET
1. Data transfer instructions.
2. Arithmetic instructions.
3. Logical instructions.
4. Logical instructions with bits.
5. Branch instructions.
MOV A,Rn
Data transfer instructions:
MOV Direct, Direct
MOV A, Direct MOV Direct,@Ri
MOV A,@Ri MOV Direct,#Data
MOV A,#Data MOV @Ri,A
MOV Rn,A MOV @Ri,Direct
MOV Rn,Direct MOV @Ri,#Data
MOV Rn,#Data MOV DPTR,#Data16
MOV Direct,A MOVX A,@Ri
MOV Direct, Rn MOVX A,@DPTR
PUSH Direct
Data transfer instructions:

POP Direct
XCH A,Rn
XCH A, Direct
XCH A,@Ri
XCHD A,@Ri
MOVX @Ri,A
MOV @DPTR,A
ADD A,Rn
Arithmetic instructions:
SUBB A, Direct
ADD A,Direct SUBB A,@Ri
ADD A,@Ri SUBB A,#Data
ADD A,#Data INC A
ADDC A,Rn INC Rn
ADDC A, Direct INC Direct
ADDC A,@Ri INC @Ri
ADDC A,#Data DEC A
SUBB A,Rn DEC Rn
DEC Direct
Arithmetic instructions:

DEC @Ri
INC DPTR
MUL AB
DIV AB
DA A
ANL A,Rn
Logical instructions:
ORL A,#Data
ANL A,Direct ORL Direct,A
ANL A,@Ri ORL Direct,#Data
ANL A,#Data XRL A,Rn
ANL Direct,A XRL A,Direct
ANL Direct,#Data XRL A,@Ri
ORL A,Rn XRL A,#Data
ORL A,Direct XRL Direct,A
ORL A,@Ri XRL Direct,#Data
CLR A
Logical instructions:

CPL A
RL A
RLC A
RR A
RRC A
SWAP A
CLR C
Boolean Variable Manipulation instructions:
ORL C,/bit
CLR bit MOV C,bit
SETB C MOV bit,C
SETB bit JC rel
CPL C JNC rel
CPL bit JB bit,rel
ANL C,bit JNB bit,rel
ANL C,/bit JBC bit,rel
ORL C,bit
ACALL addr11
Program branching instructions:
JNZ rel
LCALL addr16 CJNE A,direct,rel
RET CJNE A,#data,rel
RETI CJNE Rn,#data,rel
AJMP addr11 CJNE @Ri,#data,rel
LJMP addr16 DJNZ Rn,rel
SJMP rel DJNZ direct,rel
JMP @A+DPTR NOP
JZ rel
MUL & DIV
MUL AB ;B|A = A*B
MOV A,#25H
MOV B,#65H
MUL AB ;25H*65H=0E99
;B=0EH, A=99H
DIV AB ;A = A/B, B = A mod B
MOV A,#25H
MOV B,#10H
DIV AB ;A=2, B=5
Rotate
EXAMPLE:

RR:

RRC:

RL:

RLC:

C
ACALL: Absolute Call JC: Jump if Carry Set PUSH: Push Value Onto Stack

ADD, ADDC: Add Acc. (With Carry) JMP: Jump to Address RET: Return From Subroutine

AJMP: Absolute Jump JNB: Jump if Bit Not Set RETI: Return From Interrupt

ANL: Bitwise AND JNC: Jump if Carry Not Set RL: Rotate Accumulator Left

CJNE: Compare & Jump if Not Equal JNZ: Jump if Acc. Not Zero RLC: Rotate Acc. Left Through Carry

CLR: Clear Register JZ: Jump if Accumulator Zero RR: Rotate Accumulator Right

CPL: Complement Register


8051 INSTRUCTION SET LCALL: Long Call RRC: Rotate Acc. Right Through Carry

DA: Decimal Adjust LJMP: Long Jump SETB: Set Bit

DEC: Decrement Register MOV: Move Memory SJMP: Short Jump

DIV: Divide Accumulator by B MOVC: Move Code Memory SUBB: Sub. From Acc. With Borrow

DJNZ: Dec. Reg. & Jump if Not Zero MOVX: Move Extended Memory SWAP: Swap Accumulator Nibbles

INC: Increment Register MUL: Multiply Accumulator by B XCH: Exchange Bytes

JB: Jump if Bit Set NOP: No Operation XCHD: Exchange Digits

JBC: Jump if Bit Set and Clear Bit ORL: Bitwise OR XRL: Bitwise Exclusive OR

POP: Pop Value From Stack Undefined: Undefined Instruction


TIMERS
The 8051 has two timers:
1. TIMER 0
2. TIMER 1

TIMER 0

TIMER 1
TMOD Register

• Gate : When set, timer only runs while INT(0,1) is high.


• C/T : Counter/Timer select bit.
• M1 : Mode bit 1.
• M0 : Mode bit 0.
TCON Register (BIT ADRESSABLE REG)

TCON.7 - TF1: Timer 1 overflow flag.


TCON.6 - TR1: Timer 1 run control bit.
TCON.5 - TF0: Timer 0 overflag.
TCON.4 - TR0: Timer 0 run control bit.
TCON.3 - IE1: External interrupt 1 edge flag.
TCON.2 - IT1: External interrupt 1 type flag.
TCON.1 - IE0: External interrupt 0 edge flag.
TCON.0 - IT0: External interrupt 0 type flag.
Concept of timer
Mode 1 Programming
1. Load the TMOD value register indicating which timer (Timer0 or
Timer1) is to be used and which timer mode is selected.
2. Load registers TL & TH with initial count values.
3. Start the timer
4. Keep monitoring the timer flag(TF)
5. Stop the timer.
6. Clear TF for next round .
7. Go back to step 2 to load TH & TL again
NOTE: MODE 0 is exactly like mode-1 except that it is 13-bit timer
instead 16-bit. The 13-bit timer/counter can hold values between 0000H to
1FFFH in TH-TL.
Mode 2 Programming
1. Load the TMOD value register indicating which timer (Timer0 or
Timer1)is to be used, and select the timer mode (mode-2).
2. Load the TH register with the initial count value.
3. Start the timer.
4. Keep monitoring TF timer flag
5. After it becomes “1” then clear the TF flag
6. Go back to step 4 , since mode 2 is auto - reload
Example program for Mode1
ORG 0H
MOV TMOD,#00000001B
BACK: MOV TL0,#0F0H
MOV TH0,#0FFH
CPL P1.0
ACALL DELAY
SJMP BACK

DELAY:SETB TR0
HERE: JNB TF0, HERE
CLR TR0
CLR TF0
RET
END
Example program for Mode2

ORG 0H
MOV TMOD,#00100000B
MOV TH1, #05H
SETB TR1
HERE: JNB TF1, HERE
CPL P2.0
CLR TF1
SJMP HERE
END
Serial Communication
Serial vs. parallel Communication
Synchronous vs. Asynchronous
Serial data communication uses two methods
1. Synchronous
2. Asynchronous

There are special IC chips made by many manufacturers for serial


communications.
3. UART- universal asynchronous receiver-transmitter.
4. USART- universal synchronous - asynchronous receiver-transmitter
Framing (Asynchronous )
SCON register
Doubling the baud rate

There are two ways to increase the baud rate of data transfer in 8051.
1. Use a higher frequency crystal.
2. Change a D7-bit to 1 in the PCON register

PCON Register: ( Byte addressable)


Programming 8051 to transfer data serially

In programming 8051 to transfer character bytes serially, the following steps must
be taken
1. The TMOD register is loaded with the value 20H , indicating the use of Timer 1
in mode 2(8-bit auto reload) to set the baud rate
2. The TH1 is loaded with one of the values to set the baud rate for serial data
transfer( assuming XTAL= 11.0592 MHz).
3. The SCON register is loaded with the value 50H indicating serial mode 1, where
an 8-bit data is framed with start & stop bits.
4. TR1 is SET to 1 to start Timer 1.
5. TI is cleared when transmission is complete.
6. The character byte to be transferred serially is written into SBUF register.
7. TI is monitored for transmission .
8. To transfer the next character go to step 5
Example Program for Serial Communication

ORG 0H
MOV TMOD,#00100000B------(20H)
MOV TH1,#-3
MOV SCON,#01010000B-------(50H)
SETB TR1
AGAIN:MOV A,# ” “
ACALL TRANSFER
MOV A,# ”K“
ACALL TRANSFER
MOV A,# ”L“
ACALL TRANSFER
MOV A,# ”U“ TRANSFER: MOV SBUF,A
ACALL TRANSFER HERE: JNB TI, HERE
MOV A,# ” “ CLR TI
ACALL TRANSFER RET
SJMP AGAIN
END
Interrupts
Concept behind Interrupt
An interrupt is an external or internal event that interrupts the
microcontroller to inform it that a device needs its service
Upon activation of an interrupt, the microcontroller goes through
the following steps
1. It finishes the instruction it is executing and saves the address of
the next instruction (PC) on the stack
2. It also saves the current status of all the interrupts internally (i.e.:
not on the stack)
3. It jumps to a fixed location in memory, called the interrupt vector
table, that holds the address of the ISR
4. The microcontroller gets the address of the ISR from the interrupt
vector table and jumps to it
5. Upon executing the RETI instruction, the microcontroller returns
to the place where it was interrupted
Interrupts & Interrupt vectors:
Interrupt Enable Register
TCON REGISTER
LEVEL TRIGGERED INTERRUPT

In level triggered interrupt to ensure the activation of the


hardware interrupt at the INT ‘n’ pin, make sure that the duration
of the low-level signal is around 4 machine cycles.
In edge-triggered interrupts
• The external source must be held high for at least one machine
cycle, and then held low for at least one machine cycle

• The falling edge of pins INT0 and INT1 are latched by the 8051 and
are held by the TCON.1 and TCON.3 bits of TCON register
Interrupt Priority register
EXAMPLE PROGRAMS
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.

ORG 0000H
LJMP MAIN
ORG 000BH
CPL P2.1
RETI
ORG 0030H
MAIN: MOV TMOD,#02H
MOV P0,#0FFH
MOV TH0,#-92 (TH0=A4H)
MOV IE,#82H
SETB TR0
BACK: MOV A,P0
MOV P1,A
SJMP BACK
END
EXAMPLE PROGRAMS

Write a program to generate a square wave if 50Hz frequency on pin P1.2.


Assume that XTAL=11.0592MHz

ORG 0
LJMP MAIN
ORG 000BH
CPL P1.2
MOV TL0,#00
MOV TH0,#0DCH
RETI
ORG 30H
MAIN: MOV TM0D,#00000001B
MOV TL0,#00
MOV TH0,#0DCH
MOV IE,#82H
SETB TR0
HERE: SJMP HERE
END

You might also like