Co2 8051
Co2 8051
Co2 8051
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
Serial
I/O Timer COM Microcontroller
Port
Port
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:
7FH
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
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 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
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
JBC: Jump if Bit Set and Clear Bit ORL: Bitwise OR XRL: Bitwise Exclusive OR
TIMER 0
TIMER 1
TMOD Register
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 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
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
• 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
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