Micro Controler
Micro Controler
15/03/2023
PART 1
ADDRESS BUS
CPU
RAM ROM Printer Disk Monitor Keyboard
DATA BUS
Read/Write
CONTROL BUS
CPU CPU RAM ROM
General-
Purpose Serial
RAM ROM I/O Timer COM
PORT Port
Microproc-
I/O Timer Serial
essor COM
Port
ADDRESS
DATA Memory
(program)
Memory DATA
(program CPU ADDRESS
CPU
+data)
Memory
ADDRESS (data)
DATA
DPTR Latch
Port 1 I/O
PC DPH
ROM Special
DPL
Function
16-Bit Address Bus Register
IE
EA Byte/Bit Latch I/O
ALE System IP Port 2
Address A8-A15
PSEN Timing PCON
XTAL 1 Register SBUF
XTAL 2 System Bank 3
SCON I/O
RESET Interrupts
Register Interrupt
TCON Latch
Bank 2 Port 3 Counter
Timer TMOD Serial Data
VCC Data Buffers Register TLO RD-WR
GND Memory Bank 1
THO
Controls
Register TL1
Bank 0 TH1
Internal RAM Structure
8051 ARCHITECTURE
Features of the 8051
Feature Quantity
ROM 4K bytes
RAM 128 bytes
Timer 2
I/O pins 32
Serial port 1
Interrupt sources 6
Comparison of 8051 Family Members
Table 3:- Versions of 8051 From Dallas Semiconductor’s Soft Micro controller
Part ROM RAM I/O pins Timer Interrupt Vcc Packaging
Number
DS5000-8 8K 128 32 2 6 5V 40
DS5000-32 32K 128 32 2 6 5V 40
DS5000-8 8K 128 32 2 6 5V 20
DS5000T-8 32K 128 32 2 6 3V 40
PIN DESCRIPTION
P1.0 1 40 VCC
P1.1 2 39 P0.0 (AD0)
P1.2 3 38 P0.1 (AD1)
P1.3 4 37 P0.2 (AD2)
P1.4 5 36 P0.3 (AD3)
P1.5 6 35 P0.4 (AD4)
P1.6 34 P0.5 (AD5)
7
P1.7 8 33 P0.6 (AD6)
RST 9 32 P0.7 (AD7)
31 EA/VPP
(RXD) P3.0
(TXD) P3.1
(INT0) P3.2
10
11
12
8051 30
29
28
ALE/PROG
PSEN
P2.7 (A15)
(INT1) P3.3 13
27 P2.6 (A14)
(T0) P3.4 14
(T1) P3.5 15 26 P2.5 (A13)
7 6 5 4 3 2 1 0
CY AC F0 RS1 RS0 OV ---- P
FLAGS Contd.
• Flags are 1 bit register which are grouped inside the
PSW and PCON.
• Flags are provided to store the result of certain program
instruction.
• 8051 has 4 math flags which include Carry flag (C),
Auxiliary Carry (AC), Overflow (OV) and Parity (P), and 3
user flags are named F0,GF0, and GF1.
• GF0 and GF1 are store in PCON register.
• Carry flag is set when there is a carry in the 7th bit of
accumulator, this flag is used in arithmetic, jump, rotate,
and Boolean instruction.
FLAGS Contd.
• Auxiliary Carry flag is set when there is a carry form 3rd
bit to 4th bit of accumulator. This flag is used in BCD
arithmetic operation.
• Overflow flag is set when there is any overflow in math
operation.
• Parity flag is set to 1 when the number of 1’s in A
register is odd.
• The three user flags are general purpose flags
programmers can be set or reset by the programmer at
will.
• RS0 and RS1 are two register bank select bits which are
used to select the specific register bank of internal RAM.
FLAGS Contd.
Problem2:
Show the status of the CY, AC, and P flags after the
addition of 9CH and 64H in the following instructions.
MOV A, #9CH
ADDC A, #64H
INTERNAL RAM
• There are 128 bytes of RAM in 8051 and their
assigned address are 00H to 7FH.
• A total of 32 bytes from location 00 to 1FH are
assigned for register banks.
• A total of 16 bytes from location 20H to 2FH are
assigned for bit addressable read/ write
memory.
• A total of 80 bytes form location 30H to 7FH are
used for normal read/ write storage memory,
which is called Scratch Pad.
INTERNAL RAM Contd.
INTERNAL RAM Contd.
INTERNAL RAM Contd.
BANK 0 BANK 1 BANK 2 BANK 3
7 R7 F R7 17 R7 1F R7
6 R6 E R6 16 R6 1E R6
5 R5 D R5 15 R5 1D R5
4 R4 C R4 14 R4 1C R4
3 R3 B R3 13 R3 1B R3
2 R2 A R2 12 R2 1A R2
1 R1 9 R1 11 R1 19 R1
0 R0 8 R0 10 R0 18 R0
WORKING REGISTER
INTERNAL RAM Contd.
BYTE ADD BIT ADDRESS BYTE ADD
78 79 7A 7B 7C 7D 7E 7F B
2F I
7F
2E 70 71 72 73 74 75 76 77 T
2D 68 69 6A 6B 6C 6D 6E 6F
A
2C 60 61 62 63 64 65 66 67 D
D
2B 58 59 5A 5B 5C 5D 5E 5F
R
2A 50 51 52 53 54 55 56 57 E
S
29 48 49 4A 4B 4C 4D 4E 4F
S
28 40 41 42 43 44 45 46 47 A
B
27 38 39 3A 3B 3C 3D 3E 3F L
26 30 31 32 33 34 35 36 37 E
25 28 29 2A 2B 2C 2D 2E 2F R
24 E
20 21 22 23 24 25 26 27
G
23 18 19 1A 1B 1C 1D 1E 1F I
S
22 10 11 12 13 14 15 16 17 T 30
21 08 09 0A 0B 0C 0D 0E 0F E
R
20 00 01 02 03 04 05 06 07 GENERAL PURPOSE
PSW Bits Bank Selection
RS1 (PSW.4) RS0 (PSW.3)
Bank 0 0 0
Bank 1 0 1
Bank 2 1 0
Bank 3 1 1
Example: State the contents of the RAM locations after the following program:
SETB PSW.4 ; select bank 2
MOV R0,#99H ; load R0 with value 99H
MOV R1,#85H ; load R1 with value 85H
MOV R2,#3FH ; load R2 with value 3FH
MOV R7,#63H ; load R7 with value 63H
MOV R5,#12H ; load R5 with value 12H
Solution:
By default, PSW.3=0 and PSW.4; therefore, the instruction “SETB PSW.4” sets RS1=1
and RS0=0, thereby selecting register bank 2. Register bank 2 uses RAM location 10H-
17H. After the execution of the above program we have the following:
RAM location 10H has value 99H RAM location 11H has value 85H
RAM location 12H has value 3FH RAM location 17H has value 63H
RAM location 15H has value 12H
SPECIAL FUNCTION REGISTER
• Special Function Register (SFR) are areas of
memory that control specific functionality of the
8051 microcontroller.
• The SFRs allow the user to access the ports,
serial communication, control and access timers,
configure the 8051’s interrupt system and power
control system.
• Some SFRs can be accessed with bit operations
also, i.e they are bit addressable Special
Function Registers.
SPECIAL FUNCTION REGISTER Contd..
A Accumulator 0E0
B Arithemetic 0F0
DPH Addressing external moeory 83
DPL Addressing external moeory 82
D7
D7 D0
A0
A0 – A11
4K*8
A11
A12
A13
CS
A14
A15
RD WR
MEMR
Logic gate as Decoder MEMW
• Using the 74LS138 3-8 decoder:
D0
D7
D7 D0
A0
A0 – A11
Y0 4K*8
A12 A Y1 A11
A13 B Y2
A14
C Y3
A15
G2A Y4 CE
GND Y5
G2B
Vcc G1 Y6
OE Vpp
Y7
MEMR
74LS138 as Decoder Vcc
CONNECTION WITH EXTERNAL PROGRAM ROM
8031
Vcc
EA P3.7 RD
P3.6 WR
PSEN OE Vpp
C
P2.7 E
A12 A12
P2.0 A8 A8 2864
(2764)
ALE G
8K*8
D Q A7 A7 PROGRAM
P0.7 AD7
ROM
74LS373
P0.0 AD0 A0 A0
OC D7 D0
D7
D0
Connection to External program ROM
On chip-off chip ROM
74LS373
P0.0 AD0 OC A0 A0
D7 D0
D7
D0
A8 A8 A8 2864
P2.0 G 8K*8 (2764)
ALE DATA 8K*8
D Q A7 A7 ROM A7 PROGRAM
AD7 ROM
P0.7
74LS373
A0
P0.0 AD0 OC A0 A0
D7 D0 D0
D7 D0
D7
D0
P3.7 RD
P3.6 WR
PSEN WE OE
A15
A14 C
P2.7 A13
A13 E
A12 A12
P2.0 A8 A8
G 8K*8
ALE
DATA
D Q A7 A7 RAM
P0.7 AD7
74LS373
P0.0 AD0 A0 A0
OC D7 D0
D7
D0
PART 2
MOV Rr,#n Copy the immediate data byte n MOV R0,#0A0H 2 1/12
to the Rr register
MOV DPTR,#nn Copy the immediate 16 bit MOV DPTR,#1234h 3 2/24
number nn to the DPTR register
REGISTER ADDRESSING MODE
• The data are transferred through register to register
using register addressing mode occur between
register A and R0 to R7. It is not possible to move the
data in between R0 to R7 using register addressing
mode.
MOV Rr,add Copy the data from direct address MOV R0, 07H (R7) 2 2/24
add. to the Rr register
MOV add,Rr Copy the data from register Rr to MOV 0FH,R2 2 2/24
direct address add. (OFH is R7 of Bank 1)
MOV add1,add2 Copy the data from direct address MOV 5CH,77H 3 2/24
add1 to the direct address add2 (location of internal RAM)
MOV add,#n Copy the immediate data byte n to the MOV 5CH,# 77H 3 2/24
direct address add
INDIRECT ADDRESSING MODE
• The Indirect addressing mode uses a register to hold the
actual address that will finally be used in data moves, the
register itself is not the address, but rather the number in the
register. In this mode only R0 and R1 register is used, often
called a data pointer.
Mnemonics Operation Example Bytes Cycles/T-
state
MOV @RP,#n Copy the immediate byte n MOV @R0,#80H 2 1/12
to the address in Rp.
MOV @Rp,add Copy the contents of add to MOV @R1,80H 2 2/24
the address in Rp
MOVC A,@A,+DPTR Copy the code byte, found MOV DPTR,#1234H 1 2/24
at the ROM address MOV A,#56H
formed by adding A and MOV C A,@A+DPTR
the DPTR, to A.
MOVC A,@A,+PC Copy the code byte, found MOV A,#56H 1 2/24
at the ROM address MOV C A,@A+PC
formed by adding A and
the PC, to A.
EXTERNAL DATA MOVES
• The 8051 always access the external memory using
Indirect addressing modes. Where we uses MOVX
instruction, which specify the external memory.
Mnemonics Operation Example Bytes Cycles/
T-state
MOVX A,@DPTR Copy the content of the MOV DPTR,#8000H 1 2/24
external address in DPTR to A MOVX A,@DPTR
MOVX @DPTR,A Copy data from A register to MOV DPTR,#8000H 1 2/24
the external address in DPTR MOVX @DPTR,A
PUSH add Increment sp, copy the data in Mov 81h,#30h 2 2/24
add to the internal RAM Mov Ro,#0ACh
address contained in SP Push 00h
POP add Copy the data from the internal Pop 01h 2 2/24
RAM address contained in SP
to add, and decrement the SP.
STACK IN THE 8051: When the 8051 is powered up , the SP register
contains value 07 i.e RAM location 08 is the first location being used for the stack by
the 8051.
0B 0B 0B 0B
0A 0A 0A 0A
F3
09 09 09 09
12 12
08 08 08 08
25 25 25
0B 0B 0B 0B
54
0A 0A 0A 0A
F9 F9
09 09 09 09
76 76 76
08 08 08 08
6C 6C 6C 6C
ORL A, #n OR each bit of A with the same bit of ORL A, #60h 2 1/12
immediate date n, put the result in A.
ORL A,add OR each bit of A with the same bit of direct ORL A,80h 2 1/12
RAM address, put the result in A.
ORL A,Rr OR each bit of A with the same bit of ORL A,R0 1 1/12
register Rr, put the result in A.
ORL A,@Rp OR each bit of A with the same bit of ORL A,@R0 1 1/12
register pointer Rp, put the result in A.
ORL add,A OR each bit of A with the same bit of direct ORL 90h,A 2 1/12
RAM address, put the result in RAM.
ORL add,#n OR each bit of RAM address with the ORL 7fh,#n 3 2/24
same bit of immediate data n , put the
result in RAM
LOGICAL XOR OPERATION
Mnemonics Operation Example Bytes Cycles/ t-
state
XRL A, #n XOR each bit of A with the same bit of XRL A, #60h 2 1/12
immediate date n, put the result in A.
XRL A,add XOR each bit of A with the same bit of XRL A,80h 2 1/12
direct RAM address, put the result in A.
XRL A,Rr XOR each bit of A with the same bit of XRL A,R0 1 1/12
register Rr, put the result in A.
XRL A,@Rp XOR each bit of A with the same bit of XRL A,@R0 1 1/12
register pointer Rp, put the result in A.
XRL add,A XOR each bit of A with the same bit of XRL 90h,A 2 1/12
direct RAM address, put the result in RAM.
XRL add,#n XOR each bit of RAM address with the XRL 7fh,#n 3 2/24
same bit of immediate data n , put the
result in RAM
COMPLEMENT OPERATION
Mnemonics Operation Example Bytes Cycles/ t-
state
CLR A Clear each bit of A register to 0. CLR A 1 1/12
NOTE: No flags are affected by the byte level logical operations unless
the direct RAM address is the PSW.
BIT LEVEL LOGICAL OPERATION
• Bit level logical operation also includes AND, OR and
Complement operation.
Mnemonics Operation Example Bytes Cycles/
t-state
ANL C,b AND C and the address bit, ANL C,70H 2 2/24
put the result in C
ANL C,/b AND C and complement of the ANL C,/80H 2 2/24
address bit, put the result in C
ORL C,b OR C and the address bit, ORL C,70H 2 2/24
put the result in C
ORL C,/b OR C and complement of the ORL C,/80H 2 2/24
address bit, put the result in C
CPL C Complement the Carry Flag CPL C 1 1/12
CPL b Complement the address bit CPL 81H 2 1/12
CLR C Clear the carry flag to 0 CLR C 1 1/12
CLR b Clear the address bit to 0 CLR 81H 2 1/12
SETB C Set the carry flag to 1 SETB C 1 1/12
SETB b Set the address bit to 1 SETB 20H 2 1/12
ROTATE AND SWAP
INSTRUCTION
MNEMONICS OPERATION Example Bytes Cycles/ t-
state
INC add Add a 1 to the content of the direct INC 80h 2 1/12
memory address.
INC @Rp Add a 1 to the content of memory INC @R0 1 1/12
address in Rp
INC DPTR Add a 1 to the 16 bit DPTR register. INC DPTR 1 2/24
DECREMENTING OPERATION
MNEMONIC OPERATION Example Bytes Cycles/ t-
S state
Mnemonics Operation
CJNE A,add,radd Compare the A with the address, if they are not equal then
jump to the relative address
CJNE A,#n,radd Compare the A with the immediate data, if they are not equal
then jump to the relative address
CJNE Rr,#n,radd Compare the Rr with the immediate data, if they are not
equal then jump to the relative address
CJNE @Rp,#n,radd Compare the contents of the address in Rp with the
immediate data, if they are not equal then jump to the
relative address
DJNZ Rn,radd Decrement the Rn by 1 and jump to the relative address if
the result is not 0.
DJNZ add,radd Decrement thedirect address by 1 and jump to the relative
address if the result is not 0.
Mnemonics Operation
JMP @A+DPTR Jump to the address formed by adding A with DPTR
NOP No operation
CALL INSTRUCTION
MNEMONICS OPERATION
A A A 0 0 0 0 1 A A A A A A A A
10 9 8 7 6 5 4 3 2 1 0
A A A 1 0 0 0 1 A A A A A A A A
10 9 8 7 6 5 4 3 2 1 0
Example: Using the following list file, verify the jump forward address
calculation.
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
First notice that the JZ and JNC instructions both jump forward. The target
address for a forward jump is calculated by adding the PC of the following
instruction to the second byte of the short jump instruction, which is called
the relative address. In line 4 the instruction “JZ NEXT” has opcode of 60 and
operand of 03 at the address of 0004 and 0005. The 03 is the relative
address, relative to the address of the next instruction INC R0, which is 0006.
By adding 0006 to 3, the target address of the label NEXT, which is 0009, is
generated. In the same way for line 9, the “JNC OVER” instruction has opcode
and operand of 50 and 05 where 50 is the opcode and 05 the relative address.
Therefore, 05 is added to 000D, the address of instruction “CLR A”, giving
12H, the address of label OVER.
(b) Solution:
In that program list, “JNC AGAIN” has opcode 50 and relative address F2H.
When the relative address of F2H is added to 15H, the address of the
instruction below the jump, we have 15H +F2H= 07 (the carry is dropped).
Notice that 07 is the address of label AGAIN. Look also at “SJMP HERE”,
which has 80 FE for the opcode and relative address, respectively. The PC of
the following instruction, 0017H, is added to FEH, the relative address, to get
0015H, address of the HERE label (17H+FEH). Notice that FEH is –2 and
17H+(-2)= 15H.
Write a program in 8051 assembly language to
transfer 10 numbers in the internal RAM starting
from 30H from external RAM, data are stored from
memory location 8050H.
• MOV DPTR,#8050H
• MOV R5,#0AH
• MOV R1,#30H
• BACK: MOVX A,@DPTR
• MOV @R1,A
• INC DPTR
• INC R1
• DJNZ R5,BACK
• HERE:SJMP HERE
ASSEMBLER DIRECTIVES
• ORG:The ORG directives is used to indicate
the beginning of the address.
• EQU (equate):This is used to define a
constant without occupying a memory location.
Ex. COUNT EQU 25
……….. ………….
MOV R3,#COUNT
• END:This indicates to the assembler the end
of the source file.
DATA TYPES & DIRECTIVES
• The 8051 micro controller has only one data
type of 8 bits & the size of each register is 8
bits. Data may be unsigned or signed.
• MOV R0,#255
HERE1: MOV R1,#200
HERE: DJNZ R1,HERE
DJNZ R0,HERE1
• MOV R0,#FFH
HERE2: MOV R1,#FFH
HERE1: MOV R2,#FFH
HERE: DJNZ R2,HERE
DJNZ R1,HERE1
DJNZ R0,HERE2
Example1: Find the size of the delay in the following program, if the crystal
frequency is 11.0592 MHz.
MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;……………..Time delay
DELAY: MOV R3,#200
HERE: DJNZ R3,HERE
RET
Solution:
From Table A-1 in Appendix A, the following machine cycles for each
instruction of the DELAY subroutine.
Machine cycle
DELAY: MOV R3,#200 1
HERE: DJNZ R3,HERE 2
RET 1
For the HERE loop, (4*250)1.085 s= 1085 s. The AGAIN loop
repeats the HERE loop 200 times; therefore, 200*1085 s= 217000, if we do
not include the overhead. However, the instructions “MOV R3,#250” and
“DJNZ R2,AGAIN” at the beginning and end of the AGAIN loop add
(3*200*1.085 s)= 651 s to the time delay. As a result we have
217000+651= 217651 s= 217.651 milliseconds for total time delay
associated with the above DELAY subroutine.
1. Write a program to get the x value from P1 and send x2 to P2, continuously
Solution:
ORG 0
MOV DPTR, #300H ; LOAD LOOK-UP TABLE ADDRESS
MOV A, #0FFH ; A=FF
MOV P1, A ; CONFIGURE P1 AS INPUT PORT
BACK: MOV A, P1 ; GRT X
MOVC A, @A+DPTR ; GET X SQUARE FROM TABLE
MOV P2, A ; ISSUE IT TO P2
SJMP BACK ; KEEP DOING IT
ORG 300H
XSQR_TABLE:
DB 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
END
Solution:
(a) All values are in hex.
300 = (00) 301 = (01) 302 = (04) 303 = (09)
304 = (10) 4 * 4 = 16 = 10 in hex
305 = (19) 5 * 5 = 25 = 19 in hex
306 = (24) 6 * 6 = 36 = 24H
307 = (31) 308 = (40) 309 = (51)
Timer 1register:Timer 1 is same as timer 0 but the high and low bytes referred to as TH1 & TL1.
TMOD register:Both timer 0 and timer 1 use the same register,called TMOD, to set the various timer
operation modes. (MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
GATE:Gating control when set.Timer/counter is enabled only while the INTx pin is high and the TRx
control pin is set.When cleared ,the timer is enabled whenever the TRx control bit is set.
C/T: Cleared for timer operation and set for counter operation.
M1: Mode bit 1.
M0: Mode bit 0.
M1 M0 Mode Operating Mode
0 0 0 13-bit timer mode
0 1 1 16-bit timer mode
1 0 2 8-bit auto reload
1 1 3 Split timer mode
EX.:Find the value for TMOD if we want to program timer 0 in mode 2, use 8051 XTAL
for the clock source, and use instructions to start and stop the timer.
When GATE=1 in TMOD
XTAL
12
oscillator
C/T=0
T0 pin C/T=1
Pin 3.4
TR0
Gate
INT0 pin
Pin 3.2 Timer/counter 0
TIMER CONTROL REGISTER (TCON)
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
C/T=0 TR
XTAL
oscillator
12 THX TLX TF
C/T=0 TR
EX. Creating a square wave of 50% duty cycle (with equal position high and low) on
the P1.5 bit.Timer 0 is used to generate the time delay.
MODE1:
(a) In hex:
(FFFF – YYXX+1) * 1microsec, where YYXX are TH, TL initial value
respectively (for 12 MHz crystal oscillator).
(b) In decimal:
(65536 – nnnnn) * 1microsec, nnnnn will be converted into hex value.
Problem 3: What is the possible largest time delay excluding overhead due
to the instruction in the loop.
XTAL Overflow
oscillator
12 TL TF flag
reload
C/T=0 TR
TF goes high
Delay=(256-TLX)X12/ Fosc TH When FF 0
PULSE INTERRUPT
TL0 (8BIT) TF0
INPUT
TR1 BIT IN
TCON
CLR P2 . 3 ; clear P2 . 3
MOV TMOD, #01 ; Timer 0, mode 1 (16 – bit mode)
HERE: MOV TL0, #3EH ; TL0 = 3E, low byte
MOV T01, #0B8H ; TH0 = B8, high byte
SETB P2 . 3 ; set high P2 . 3
SETB TR0 ; start the timer 0
AGAIN: JNB TF0, AGAIN ; monitor the timer flag 0
CLR TR0 ; stop the timer 0
CLR TF0 ; clear timer 0 flag for next round
CLR P2 . 3
Solution:
(a) (FFFF – B83E + 1) = 47C2H = 18370 in decimal and 18370*1.085 s = 19.93145 ms.
(b) Since TH – TL = B83EH = 47166 (in decimal) we have 65536 – 47166 = 18370. This means that the timer
counts from B83EH to FFFF. This plus rolling over to 0 goes through a total of 18370 clock cycles, where each clock
is 1. 085 s in duration. Therefore, we have 18370*1.085 s =19.93145 ms as the width of the pulse.
Modify TL and TH in Example above to get the largest time delay possible. Find the delay in ms. In your calculation,
exclude the overhead due to the instructions in the loop.
Solution:
To get the largest delay we make TL and TH both 0. This will count up from 0000 to FFFFH and then roll over to zero.
CLR P2 . 3 ; clear P2 . 3
MOV TMOD, #01 ; Timer 0, mode 1 (16 – bit mode)
HERE: MOV TL0, # 0 ; TL0 = 0, the low byte
MOV TH0, # 0 ; TH0 =0, the high byte
SETB P2 . 3 ; set high P2 . 3
SETB TR0 ; start timer 0
AGAIN: JNB TF0, AGAIN ; monitor the timer flag 0
CLR TR0 ; stop timer 0
CLR TF0 ; clear timer 0 flag
CLR P2 . 3
Making TH and TL both zero means that the timer will count from 0000 to FFFF, and then roll over to raise the TF flag. As a
result, it goes through a total of 65536 states. Therefore, we have delay = (65536-0) x 1. 085 s = 71. 1065 ms.
Assuming that XTAL = 11.0592 MHz, write a program to generate a square wave of 2 kHz frequency on pin P1.5.
Solution:
Here we must toggle the bit to generate the square wave. Look at the following steps.
(a) T = 1 / f = 1 / 2 kHz = 500 s the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 250 s.
(c) 250 s / 1.085 s = 230 and 65536 – 230 = 65306 which in hex is FF1AH.
(d) TL = 1A and TH = FF, all in hex. The program is as follows:
EX.
Assuming that clock pulses are fed into pin T1, write a program for counter 1
in mode 2 to count the pulses and display the state of the TL1 count on P2.
MOV TMOD,#01100000B
MOV TH1,#0
SETB P3.5
AGAIN SETB TR1
BACK MOV A,TL1
MOV P2,A
JNB TF1,BACK
CLR TR1
CLR TF1
SJMP AGAIN
Overflow
Timer 0 flag
External
Input TH0 TL0 TF0
Pin 3.4
C/T=1
TF0 goes high
TR0 When FFFF 0
Notice in the above program the role of the instruction “SETB P3 . 5”. Since ports are set up for output
when the 8051 is powered up, we make P3 . 5 an input port by making it high. In other words. We must
configured (set high) the T1 pin (pin P3 . 5) to allow pulses to be fed into it.
8051
P2 To LEDs
P2 is connected to 8 LEDs
And input T0 to pulses. P3.5
T1
8051 SERIAL COMMUNICATION
1. Serial communication is used for transferring data between two systems
located at distances of feet to millions of miles apart.
2. Data must be converted in serial bits.
3. To send data using parallel-in serial-out register.
4. To receive data using serial-in parallel-out register.
5. Protocol has to be maintained between sender and receiver.
Types of serial communication data:
1. Synchronous:Transfer a block of data.
2. Asynchronous:Transfer a single byte at a time.
Start
space Stop 0 1 0 0 0 0 0 1 mark
bit
bit
d7 d0
Goes out first
Goes out last
Data transfer rate:The rate of data transfer in serial communication is stated in bps.
Simplex Transmitter Receiver
Transmitter Receiver
Half Duplex
Receiver Transmitter
Transmitter Receiver
Full Duplex
Receiver Transmitter
1. Synchronous &
2. Asynchronous
+
C3 8051
+ 16 2 MAX232
C1 1
6 11 11
3 P3.1 5
14 2
+ C4 TXD
4 +
C2
5 13 3
T1IN 10 12
11 14 P3.0
R1OUT RXD DB-9
12 13
T2IN
10 7
R2OUT
9 8
9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8
SBUF register:SBUF is an 8-bit register used solely for serial
communication in the 8051.For a byte of data to be transferred via the TXD
line, it must be placed in the SBUF register.Similarly it holds the byte of data
received by 8051’s RXD line.
SCON register:The SCON register is an 8-bit register used to program the
start bit,stop bit,and data bits of data framing.
REN: receive enable bit (SCON.4) When it is high, it allows the 8051 to
receive data on the RXD pin,
TI: Transmit interrupt (SCON.1) When 8051 finishes the transfer of 8 bit
character, TI flag will be high to indicate that it is ready to transfer another byte.
RI: Receive interrupt (SCON.0) When 8051 receives data, it places the byte in
SBUF register (excluding start and stop bit) and raises RI flag to indicate that a byte is
in SBUF to pick up.
• Mode 0: in mode 0 SBUF configures to receive or transmit 8
data bits using RXD pin for both functions. Pin TXD is
connected to the internal shift frequency pulse source to
supply shift pulses to external circuits. Here the baud rate is
fixed at 1/12 of the oscillator frequency.
• Mode 1: In mode 1 8051’s UART becomes a 10 bit full-duplex
receiver/transmitter that may receive and transmit data at the
same time. Pin RXD receive all data. Pin TXD transmit all
data.
• 10 bit data is configured as a start bit, 8 data bits and a stop
bits. A TI flag is set once when all ten bits have been send.
• The RI flag is set when it received ten data. Of the original 10
bit start bit is discarded, the 8 data bits go to SBUF register,
and stop bit go to RB8 of SCON register.
• Timer 1 is used to generate baud rate. Typically timer1 is
used in mode 2 as an auto reload mode, which generate the
baud frequency.
2 SMOD oscillator frequency
• f baud= ------------ X --------------------------
32d 12d X [256 –(TH1)]
• Mode 2: Here 11 bits are transmitted, a start bits, 9 data
bits and a stop bits. The ninth data bit is copied from TB8
in SCON during transmit and stored in RB8 of SCON
during receive.both start and stop bit are discarded.
Baud rate is as follows:
2 SMOD
• F baud= ----------- x oscillator frequency
64d
Functions of RI and TI flag will be same as mode 1.
• Mode 3: mode 3 is identical to mode 2except that the
baud rate is determined exactly as in mode 1, using
timer 1 to generate communication frequency. Baud rate
is:
2 SMOD oscillator frequency
• f baud= ------------ X --------------------------
32d 12d X [256 –(TH1)]
POWER MODE CONTROLL(PCON) REGISTER
SMOD -------- --------- ---------- GF1 GF0 PD IDL
BIT SYMBOL FUNCTION
7 SMOD Serial baud rate modify bit. Set to 1 by program to
double the baud rate using timer 1 for modes 1, 2 and 3,
cleared to 0 by program to use timer 1 baud rate.
6 ------ not implemented
5 ------ not implemented
4 ------ not implemented
3 GF1 General purpose user flag bit 1, se/ cleared by program
2 GF0 General purpose user flag bit 0, se/ cleared by program
1 PD Power down bit, set to 1 by program to enter power
down mode.
0 IDL Idle mode bit, set to 1 by program to enter into idle mode
Programming the 8051 for serial communication
The machine cycle frequency of the 8051 = 11.0592 MHz / 12 = 921.6 kHz, and 921.6 kHz / 32 = 28,800 Hz
is the frequency provided by UART to timer 1 to set baud rate.
Notice that dividing 1/12th of the crystal frequency by 32 is the default value upon activation of the
8051RESET pin. We can change this default setting.
2. Program 8051 to receive bytes of data serially and put them in P1. Set
the baud rate 2400, 8-bit data, 1 stop bit.
Interrupts & Polling
Two ways by which a micro controller can serve several devices.
1. Reset: When this reset pin is activated, the 8051 jumps to address
location 0000.
2. Two interrupts are set aside for the timers: One for timer 0 and one
for timer 1.Memory locations 000BH and 001BH in the interrupt vector
table belong to timer 0 and timer 1 respectively.
IE REGISTER
ORG 0000H
LJMP MAIN
ORG 001BH
LJMP ISR_T1
ORG 0040H
MAIN: MOV TMOD,#10H ;timer1, mode 1
MOV P0,#0FFH
MOV TL1,#18H
MOV TH1,#0FCH
MOV IE,#88H ;enable timer 1 interrupt
SETB TR1
BACK: MOV A,P0
MOV P1,A
SJMP BACK
1.Level triggered:In level triggered mode, INT0 and INT1 pins are normally high
and if a low level signal is applied to them, it triggers the interrupt. Must be held in low
state until the start of execution of ISR. Must be removed before RETI.
Level-triggered
INT0
(Pin 3.2) IT0 0003
1 IE0
0 (TCON.1)
Edge-triggered
Level-triggered
INT1 0
(Pin 3.3) IT1 0013
1 1 IE1
0 (TCON.3)
Edge-triggered
Edge triggered:To make the interrupts edge triggered interrupts,we must program
the bits (IT0 AND IT1) of the TCON register. For edge triggered interrupt, source must
be held high at least one m/c cycle and then held low for at least one m/c cycle.
D7 D0
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Problem2:
Write the same program using edge triggered interrupt.
What difference will you observe if you run the two programs
using a switch.
TI
0023H
RI
Problem:
Write a program in which the 8051 gets data from P1 and sends it
to P2 continuously while incoming data from serial port is sent to P0.
XTAL = 12 MHz, baud rate 9600.
IP REGISTER
(bit addressable)
Problem: To make timer int. 1 highest priority, what will be the value in
IP register. What will be the sequence in which the interrupts are
serviced.
INTEGRATED CIRCUIT TO BUILD AN
MICROCONTROLLER BASED EMBEDDED SYSTEM
PC2
PC3
PB0
16
17
18
A 25
24
23
PB7
PB6
PB5
PB1 19 22 PB4
PB2 20 21 PB3
Interfacing with LCD +5V
+5V 10
K
G
1 2 3
14 13 12 11 10 9 8 7 5 4
D7 D6 D5 D4 D3 D2 D1 D0 R/W RS
12 P2.0
13 P2.1
1 P1.0
2 P1.1
3 P1.2
4 P1.3
5 P1.4
6 P1.5
7 P1.6
8 P1.7
RS, register select: If RS=0, the instruction command code register
is selected, allowing the user to send a command such as clear display,
cursor at home, etc. if RS=1 the data register is selected, allowing the user
to send data to be displayed on the LCD.
CONF:
DB 38H,0EH,06H,01H,C0H,FFH
MSG1:
DB: 41H,42H,43H,44H,FFH
CONF_DISP:
CLR A
MOVC A,@A+DPTR
INC DPTR
CJNE A,#FFH,COMD
RET
COMD:
ACALL COMNWRT
ACALL DELAY
SJMP CONF_DISP
DISPLAY:
CLR A
MOVC A,@A+DPTR
INC DPTR
CJNE A,#FFH,DATA
RET
DATA:
ACALL DATAWRT
ACALL DELAY
SJMP DISPLAY
COMNWRT:
MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
NOP
NOP
CLR P2.2
RET
DATAWRT:
MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
NOP
NOP
CLR P2.2
RET
DELAY:
MOV R1,#50H
HERE2: MOV R2,#FFH
HERE1: DJNZ R2,HERE1
DJNZ R1,HERE2
RET
INTERFACING A STEPPER MOTOR
+12V
+5V
STEPPER MOTOR
31 A 40
9 T
ULN 2803A
8
9 P2.3
18 C P2.4
LS373 LATCH
5 P2.5
19 1 P2.6
Full step operation with one coil energized
STEP A B C D
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1
1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1
• Write a program to run a stepper motor in full
step configuration.
MOV A,#66H
BACK: MOV P2,A
RR A
ACALL DELAY
SJMP BACK
DELAY:
MOV R2,#50H
H1: MOV R3,#FFH
H2: DJNZ R3,H2
DJNZ R2,H1
RET
Interfacing With ADC
• Input:
• analog signal with a voltage
e.g., pressure, light intensity, temperature,
sound
• Output
• digital value representing the voltage
• 8-bit, 10-bit, 12-bit, 16-bit, 24-bit etc
depends on the specific ADC's precision
Purposes of ADC
• Digitize a signal
• The world is analog; computer is digital
=> bridge the real world & computer
• Benefits
• no more noise due to processing
• can be stored/retrieved like any data
• separate timing handling from processing
ADC0808: Multi-(analog)-channel
• Easy interface to all microprocessors
• Operates with 5 VDC
• Adjusted voltage reference
• No zero or full-scale adjust required
• 8-channel multiplexer with address logic
• 0V to 5V input range with single 5V power
supply
• Outputs meet TTL voltage level specifications
Pin Description
Pin Description contd.
• IN0..IN7: analog input channels
•SC, EOC: (=WR, INTR)
start conv, end-of-conv
• OE: (=RD) output enable
• C B A : 3-bit channel select
• ALE: clock for latching CBA
• Do-D7: digital output
Analog Channel Selection
Timing Diagram
Interfacing 8051 With ADC0809
Pin Connection between 8051 and
ADC0809
• ALE BIT P2.4
• OE BIT P2.5
• SC BIT P2.6
• EOC BIT P2.7
• ADDR_A BIT P2.0
• ADDR_B BIT P2.1
• ADDR_C BIT P2.2
• DATA P1
Assembly Program
ORG 0000H
SETB P2.7 ;configure P2.7 as input
;port pin (EOC)
CLR P2.4 ;clear ALE
CLR P2.6 ;clear SC (start conversion)
CLR P2.5 ;clear OE (output enable)
BACK: CLR P2.2 ;ADDR_C
CLR P2.1 ;ADDR_B (select channel=1)
SETB P2.0 ;ADDR_A
ACALL DELAY
SETB P2.4 ; latch address (set ALE)
ACALL DELAY
Assembly Program contd.
SETB P2.6 ;start conversion (set SC)
ACALL DELAY
CLR P2.4 ;clear ALE
CLR P2.6 ; clear SC
HERE: JB P2.7, HERE ; check EOC
HERE1: JNB P2.7, HERE1 ;check EOC
SETB P2.5 ;set OE
ACALL DELAY
MOV A, P1 :save data in A register
CLR P2.5 :clear OE
SJMP BACK
Interfacing with Seven segment decoder
a
f g b
e c
d
Segment pattern
a b c d e f g
Common cathode
Segment circuit
Connection with 8051(CA)
Connection with 8051(CC)
Display Data for CA Data for CC
.gfedcba(d7-d0) .gfedcba(d7-d0)
0 C0 3F
1 F9 06
2 A4 5B
3 B0 4F
4 99 66
5 92 6D
6 82 7D
7 F8 07
8 80 7F
9 98 67
Program in CA Mode
HERE: MOV P2,#0C0H
ACALL DELAY
MOV P2,#0F9H
ACALL DELAY
MOV P2,#0A4H
ACALL DELAY
MOV P2,#0B0H
ACALL DELAY
MOV P2,#99H
ACALL DELAY
MOV P2,#92H
ACALL DELAY
MOV P2,#82H
ACALL DELAY
Program in CA Mode (CONTD.)
MOV P2,#0F8H
ACALL DELAY
MOV P2,#80H
ACALL DELAY
MOV P2,#98H
ACALL DELAY
SJMP HERE
Program in CC Mode
HERE: MOV P2,#3FH
ACALL DELAY
MOV P2,#06H
ACALL DELAY
MOV P2,#5BH
ACALL DELAY
MOV P2,#4FH
ACALL DELAY
MOV P2,#66H
ACALL DELAY
MOV P2,#6DH
ACALL DELAY
MOV P2,#7DH
ACALL DELAY
Program in CC Mode (CONTD.)
MOV P2,#07H
ACALL DELAY
MOV P2,#7FH
ACALL DELAY
MOV P2,#67H
ACALL DELAY
SJMP HERE
Program to display
HERE: SETB P3.5
MOV P1,#06H
SETB P3.4
MOV P1,#5BH
SETB P3.3
MOV P1,#4FH
SETB P3.2
MOV P1,66H
ACALL DELAY
SJMP HERE
THANK YOU