8051 Microcontroller-Day2
8051 Microcontroller-Day2
8051 Microcontroller-Day2
TO
TRAINING PROGRAMME
ON
8051 MICROCONTOLLER
&
APPLICATIONS
September 1-2,2008
DEPARTMENT OF ECE
MVGR COLLEGE OF ENGINEERING
July 4,CHINTALAVALASA,VIZIANAGARAM
2015
DAY 2
SESSION 1
July 4, 2015
Contents
More Instructions
Ports
Timers & Counters
Serial port communication
Interrupts
July 4, 2015
July 4, 2015
Logical Operations
ANL - ORL - XRL
EXAMPLE:
ANL
MOV
R5,#89H;
10001001
ANL
R5,#08H;
00001000
therefore R5=00001000(08H)
July 4, 2015
Logical Operations.
CPL
Example:
MOV
L01: CPL
MOV
ACALL
SJMP
July 4, 2015
;1s complement
A,#55H
A
P1,A
DELAY
L01
;A=01010101 B
Logical Operations.
RR RL RRC RLC A
EXAMPLE:
RR A
RR:
RRC:
RL:
RLC:
July 4, 2015
C
7
A,#0;
R2,#10
A,#03
R2,AGAING ;repeat until R2=0 (10 times)
R5,A
July 4, 2015
Jump if A=0
JNZ
Jump if A/=0
DJNZ
CJNE A,byte
Jump if A/=byte
CJNE reg,#data
Jump if byte/=#data
JC
Jump if CY=1
JNC
Jump if CY=0
JB
Jump if bit=1
JNB
Jump if bit=0
JBC
July 4, 2015
10
CJNE , JNC
Exercise:
Write a program that compare R0,R1.
If R0>R1 then send 1 to port 2,
else if R0<R1 then send 0FFh to port 2,
else send 0 to port 2.
July 4, 2015
11
CALL Instructions
Another control transfer instruction is the CALL
instruction, which is used to call a subroutine.
LCALL(long call)
In this 3-byte instruction, the first byte is the opcode
an the second and third bytes are used for the address
of target subroutine. Therefore, LCALL can be used
to call subroutines located anywhere within the 64K
byte address space of the 8051.
July 4, 2015
12
13
I/O PROGRAMMING
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
RST
(RXD)P3.0
(TXD)P3.1
(INT0)P3.2
(INT1)P3.3
(T0)P3.4
(T1)P3.5
(WR)P3.6
(RD)P3.7
XTAL2
XTAL1
GND
July 4, 2015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
8051
(8031)
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
Vcc
P0.0(AD0)
P0.1(AD1)
P0.2(AD2)
P0.3(AD3)
P0.4(AD4)
P0.5(AD5)
P0.6(AD6)
P0.7(AD7)
EA/VPP
ALE/PROG
PSEN
P2.7(A15)
P2.6(A14)
P2.5(A13)
P2.4(A12)
P2.3(A11)
P2.2(A10)
P2.1(A9)
P2.0(A8)
14
July 4, 2015
15
Other Pins
P1, P2, and P3 have internal pull-up resisters.
P1, P2, and P3 are not open drain.
P0 has no internal pull-up resistors and does not connects to
Vcc inside the 8051.
P0 is open drain.
Compare the figures of P1.X and P0.X.
However, for a programmer, it is the same to program P0, P1,
P2 and P3.
All the ports upon RESET are configured as output.
July 4, 2015
16
A Pin of Port 0
Read latch
TB2
Internal CPU
bus
Write to latch
Clk
P1.X
Q
TB1
Read pin
July 4, 2015
P0.X
pin
8051 IC
M1
P1.x
17
Tri-state Buffer
Output
Input
Tri-state control
(active high)
July 4, 2015
Low
Highimpedance
(open-circuit)
18
July 4, 2015
19
July 4, 2015
Port 0
P0.0
DS5000 P0.1
P0.2
8751
P0.3
P0.4
8951
P0.5
P0.6
P0.7
10 K
20
July 4, 2015
21
74LS373
PSEN
ALE
P0.0
P0.7
74LS373
G
D
OE
OC
A0
A7
D0
D7
EA
P2.0
A8
P2.7
A15
8051
July
4, 2015
ROM
22
P0.0
P0.7
A7
PSEN
ALE
1. Send address to
ROM
Address
D0
D7
EA
P2.0
A8
P2.7
A12
8051
July 4, 2015
ROM
23
74LS373
G
D
Address
OE
OC
A0
A7
D0
D7
EA
A8
P2.7
A12
8051
July
4, 2015
ROM
24
ALE Pin
The ALE pin is used for de-multiplexing the
address and data by connecting to the G pin of
the 74LS373 latch.
When ALE=0, P0 provides data D0-D7.
When ALE=1, P0 provides address A0-A7.
The reason is to allow P0 to multiplex address and
data.
July 4, 2015
25
July 4, 2015
26
July 4, 2015
27
Function
Pin
P3.0
RxD
10
P3.1
TxD
11
P3.2
INT0
12
P3.3
INT1
13
P3.4
T0
14
P3.5
T1
15
P3.6
WR
16
P3.7
RD
17
July 4, 2015
28
A Pin of Port 1
Read latch
TB2
Vcc
Load(L1)
Internal CPU
bus
Write to latch
Clk
P1.X
pin
P1.X
Q
M1
TB1
P0.x
Read pin
July 4, 2015
8051 IC
29
30
Vcc
TB2
Vcc
Write to latch
Clk
P1.X
pin
P1.X
Q
M1
output 1
TB1
Read pin
July 4, 2015
8051 IC
31
Vcc
TB2
ground
Write to latch
Clk
P1.X
pin
P1.X
Q
M1
output 0
TB1
Read pin
July 4, 2015
8051 IC
32
BACK:
MOV
A,#55H
MOV
P1,A
ACALL
DELAY
CPL A
SJMP BACK
Let P1 toggle.
You can write to P1 directly.
July 4, 2015
33
TB2
2. MOV A,P1
Vcc
external pin=High
Load(L1)
P1.X pin
P1.X
Write to latch
Clk
M1
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
July 4, 2015
8051 IC
34
Vcc
2. MOV A,P1
TB2
Load(L1)
external pin=Low
MOV P1,#0FFH
Internal CPU bus
P1.X pin
P1.X
Write to latch
Clk
M1
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
July 4, 2015
8051 IC
35
BACK:
MOV
MOV
MOV
MOV
SJMP
A,#0FFH
P1,A
A,P1
P2,A
BACK
;A=11111111B
;make P1 an input port
;get data from P0
;send data to P2
July 4, 2015
36
Mnemonics
Examples
Description
MOV A,PX
MOV A,P2
JNB PX.Y,..
JNB P2.1,TARGET
JB PX.Y,..
JB P1.3,TARGET
MOV C,PX.Y
MOV C,P2.4
July 4, 2015
37
Read-Modify-Write Instructions
Mnemonics
Example
ANL
ANL P1,A
ORL
ORL P1,A
XRL
XRL P1,A
CPL
CPL P1.2
INC
INC
DEC
DEC P1
DJNZ P1,TARGET
MOV PX.Y,C
MOV P1.2,C
CLR PX.Y
CLR P1.3
SETB PX.Y
SETB P1.4
July 4, 2015
P1
38
Timer/Counter Logic
July 4, 2015
39
July 4, 2015
40
Timer 1 registers
TL1 ( timer 1 low byte )
TH1 ( timer 1 high byte )
July 4, 2015
41
July 4, 2015
42
July 4, 2015
43
July 4, 2015
44
Operation of Timer on
Mode-0
July 4, 2015
45
Operation of Timer in
Mode 1
July 4, 2015
46
Operation of Timer in
Mode 2
July 4, 2015
47
Operation of Timer in
Mode 3
July 4, 2015
48
8051 SERIAL
COMMUNICATION
July 4, 2015
49
Basics of serial
communication
July 4, 2015
50
July 4, 2015
51
July 4, 2015
52
July 4, 2015
53
SM0,SM1
SM0 and SM1 are D7 and D6 of the SCON
SM0
0
0
1
1
SM1
0
Serial Mode 0
1
Serial Mode 1,8 bit data,
1 stop bit, 1 start bit
0
Serial Mode 2
1
Serial Mode 3
July 4, 2015
54
D0
SMO
D
--
--
MOV A,PCON
SETB ACC.7
MOV PCON,A
July 4, 2015
--
GF1
GF0
PD
IDL
July 4, 2015
56
July 4, 2015
57
INTERRUPTS PROGRAMMING
July 4, 2015
58
July 4, 2015
59
Step in enabling an
interrupt
July 4, 2015
60
Example:
Writing the ISR for Timer0 interrupt
ORG 0000H
;reset
LJMP MAIN
ORG 000BH
;Timer0 entry point
T0ISR: .
;Timer0 ISR begins
.
RETI
;return to main program
MAIN: .
;main program
.
.
END
July 4, 2015
61
July 4, 2015
62
July 4, 2015
63
July 4, 2015
64
NOP: No Operation
ORL: Bitwise OR
July 4, 2015
65
DAY 1
End of session 1
July 4, 2015
66