8051
8051
Introduction
Block Diagram and Pin Description of the 8051
Registers
Some Simple Instructions
Structure of Assembly language and Running
an 8051 program
Memory mapping in 8051
8051 Flag bits and the PSW register
Addressing Modes
16-bit, BCD and Signed Arithmetic in 8051
Stack in the 8051
LOOP and JUMP Instructions
CALL Instructions
I/O Port Programming
Introduction
General-purpose microprocessor
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example : Intel’s x86, Motorola’s 680x0
Address Bus
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
Embedded System
• Embedded system means the processor is embedded into that
application.
• An embedded product uses a microprocessor or microcontroller to
do one task only.
• In an embedded system, there is only one application software that
is typically burned into ROM.
• Example : printer, keyboard, video game player
Three criteria in Choosing a Microcontroller
CPU
Bus Serial
4 I/O Ports
OSC Control Port
P0 P1 P2 P3 TxD RxD
Address/Data
Comparison of the 8051 Family Members
• Vcc ( pin 40 ):
– Vcc provides supply voltage to the chip.
– The voltage source is +5V.
• GND ( pin 20 ): ground
• XTAL1 and XTAL2 ( pins 19,18 ):
– These 2 pins provide external clock.
– Way 1 : using a quartz crystal oscillator
– Way 2 : using a TTL oscillator
– Example 4-1 shows the relationship between XTAL and the
machine cycle.
Pins of 8051 ( 2/4 )
C2
XTAL2
30pF
C1
XTAL1
30pF
GND
Figure 4-2 (b). XTAL Connection to an External Clock Source
N XTAL2
C
• Using a TTL oscillator
• XTAL2 is unconnected. EXTERNAL
OSCILLATOR
SIGNAL XTAL1
GND
Example :
Solution:
RESET Value of Some 8051 Registers:
10 uF 31
EA/VPP
30 pF X1
19
11.0592 MHz
8.2 K
X2
18
30 pF
9 RST
Figure 4-3 (b). Power-On RESET with Debounce
Vcc
31
EA/VPP
X1
10 uF 30 pF
X2
RST
9
8.2 K
Pins of I/O Port
R0
DPTR DPH DPL
R1
R2 PC PC
R3
R5
R6
R7
MOV DPTR,#7634H
MOV DPL,#34H
MOV DPH,#76H
Note 1:
MOV A,#72H ≠ MOV A,72H
After instruction “MOV A,72H ” the content of 72’th byte of RAM will replace in Accumulator.
8086 8051
MOV AL,72H MOV A,#72H
MOV AL,’r’ MOV A,#’r’
MOV BX,72H
MOV AL,[BX] MOV A,72H
Note 2:
MOV A,R3 ≡ MOV A,3
ADD A, Source ;A=A+SOURCE
SETB C ; CY=1
SETB P0.0 ;bit 0 from port 0 =1
SETB P3.7 ;bit 7 from port 3 =1
SETB ACC.2 ;bit 2 from ACCUMULATOR =1
SETB 05 ;set high D5 of RAM loc. 20h
SETB C ;CY=1
SUBB A,R5 ;A=A-R5-1
SETB C ;CY=1
ADC A,R5 ;A=A+R5+1
DEC byte ;byte=byte-1
INC byte ;byte=byte+1
INC R7
DEC A
DEC 40H ; [40]=[40]-1
RR – RL – RRC – RLC A
EXAMPLE:
RR A
Structure of Assembly language
and Running an 8051 program
EDITOR
PROGRAM
ORG 0H Myfile.asm
MOV R5,#25H ASSEMBLER
Myfile.lst
MOV A,#0 Myfile.obj
Other obj file
END OH
PROGRAM
Myfile.hex
Memory mapping in 8051
4k 8k 32k
0000H 0000H 0000H
0FFFH
DS5000-32
1FFFH
8751
AT89C51
8752
AT89C52 7FFFH
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
• PSW Register
CY AC F0 RS1 RS0 OV -- P
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
Instructions that Affect Flag Bits:
Note: X can be 0 or 1
Example:
MOV A,#88H
ADD A,#93H
88 10001000
+93 +10010011
---- --------------
11B 00011011 Example:
CY=1 AC=0 P=0 MOV A,#9CH
ADD A,#64H
9C 10011100
Example: +64 +01100100
MOV A,#38H ---- --------------
ADD A,#2FH 100 00000000
CY=1 AC=1 P=0
38 00111000
+2F +00101111
---- --------------
67 01100111
CY=0 AC=1 P=1
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
Example :
Num EQU 30
…
MOV R0,Num
MOV DPTR,#data1
…
ORG 100H
data1: db “IRAN”
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.
In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB
insructions.
Example:
Write a program to copy a block of 10 bytes from RAM location sterting at 37h to RAM
location starting at 59h.
Solution:
MOV R0,#37h ; source pointer
MOV R1,#59h ; dest pointer
MOV R2,#10 ; counter
L1: MOV A,@R0
MOV @R1,A
INC R0 jump
INC R1
DJNZ R2,L1
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:
Assuming that ROM space starting at 250h contains “Hello.”, write a program to transfer the
bytes into RAM locations starting at 40h.
Solution:
ORG 0
MOV DPTR,#MYDATA
MOV R0,#40H
L1: CLR A
MOVC A,@A+DPTR
JZ L2
MOV @R0,A
INC DPTR
INC R0
SJMP L1
L2: SJMP L2
;-------------------------------------
ORG 250H
MYDATA:DB “Hello”,0
END
Notice the NULL character ,0, as end of string and how we use the JZ instruction to
detect that.
• Example:
Write a program to get the x value from P1 and send x 2 to P2, continuously .
Solution:
ORG 0
MOV DPTR, #TAB1
MOV A,#0FFH
MOV P1,A
L01:
MOV A,P1
MOVC A,@A+DPTR
MOV P2,A
SJMP L01
;----------------------------------------------------
ORG 300H
TAB1: DB 0,1,4,9,16,25,36,49,64,81
END
16-bit, BCD and Signed
Arithmetic in 8051
Exercise:
30H
• The stack pointer in the 2FH
8051 is only 8 bits wide, Bit-Addressable RAM
Solution:
MOV A,#0;
MOV R2,#10
AGAIN: ADD A,#03
DJNZ R2,AGAING ;repeat until R2=0 (10 times)
MOV R5,A
• Other conditional jumps :
JZ Jump if A=0
JC Jump if CY=1
JB Jump if bit=1
LJMP(long jump)
LJMP is an unconditional jump. It is a 3-byte instruction in
which the first byte is the opcode, and the second and third
bytes represent the 16-bit address of the target location. The
20byte target address allows a jump to any memory location
from 0000 to FFFFH.
SJMP(short jump)
In this 2-byte instruction. The first byte is the opcode and the
second byte is the relative address of the target location. The
relative address range of 00-FFH is divided into forward and
backward jumps, that is , within -128 to +127 bytes of
memory relative to the address of the current PC.
CJNE , JNC
Exercise:
• 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.
• ACALL (absolute call)
TB1
Read pin P0.x
8051 IC
Hardware Structure of I/O Pin
• Each pin of I/O ports
– 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
Tri-state Buffer
Output Input
Tri-state control
(active high)
L L H H Low
H H Highimpedance
(open-circuit)
Writing “1” to Output Pin P1.X
Clk Q 0 M1
output 1
Write to latch
TB1
Read pin
8051 IC
Writing “0” to Output Pin P1.X
Clk Q 1 M1
output 0
Write to latch
TB1
Read pin
8051 IC
Port 1 as Output ( Write to a Port )
• Send data to Port 1 :
MOV A,#55H
BACK: MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
– Let P1 toggle.
– You can write to P1 directly.
Reading Input v.s. Port Latch
• When reading ports, there are two possibilities :
– Read the status of the input pin. ( from external pin value )
• MOV A, PX
• JNB P2.1, TARGET ; jump if P2.1 is not set
• JB P2.1, TARGET ; jump if P2.1 is set
• Figures C-11, C-12
– Read the internal latch of the output port.
• ANL P1, A ; P1 ← P1 AND A
• ORL P1, A ; P1 ← P1 OR A
• INC P1 ; increase P1
• Figure C-17
• Table C-6 Read-Modify-Write Instruction (or Table 8-5)
• See Section 8.3
Reading “High” at Input Pin
1 1 P1.X pin
Internal CPU bus D Q
P1.X
0 M1
Write to latch Clk Q
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
Reading “Low” at Input Pin
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
Port 1 as Input ( Read from Port )
• In order to make P1 an input, the port must be programmed by writing 1 to
all the bit.
0 1 P1.X pin
Internal CPU bus D Q
1 P1.X
0
Write to latch Clk Q M1
3. write result to latch Read
pin=0 Read latch=0
Write to latch=1
TB1
Read pin
8051 IC
Read-modify-write Feature
• Read-modify-write Instructions
– Table C-6
• This features combines 3 actions in a single instruction :
1. CPU reads the latch of the port
2. CPU perform the operation
3. Modifying the latch
4. Writing to the pin
– Note that 8 pins of P1 work independently.
Port 1 as Input ( Read from latch )
Read latch
TB2
TB1
Read pin P1.x
8051 IC
Port 0 ( pins 32-39 )
• P0 is an open drain.
– Open drain is a term used for MOS chips in the same way
that open collector is used for TTL chips.
• When P0 is used for simple data I/O we must connect it to
external pull-up resistors.
– Each pin of P0 must be connected externally to a 10K ohm
pull-up resistor.
– With external pull-up resistors connected upon reset, port 0
is configured as an output port.
Port 0 with Pull-Up Resistors
Vcc
10 K
Port 0
P0.0
DS5000 P0.1
P0.2
8751 P0.3
8951 P0.4
P0.5
P0.6
P0.7
Dual Role of Port 0
• When connecting an 8051/8031 to an external memory, the 8051
uses ports to send addresses and read instructions.
– 8031 is capable of accessing 64K bytes of external memory.
– 16-bit address : P0 provides both address A0-A7, P2 provides
address A8-A15.
– Also, P0 provides data lines D0-D7.
• When P0 is used for address/data multiplexing, it is connected to the
74LS373 to latch the address.
– There is no need for external pull-up resistors as shown in
Chapter 14.
74LS373
PSEN OE
ALE 74LS373 OC
G
P0.0 A0
D
P0.7 A7
D0
D7
EA
P2.0 A8
P2.7 A15
8051 ROM
Reading ROM (1/2)
2. 74373 latches the
1. Send address to
PSEN address and send to
ROM OE
ALE ROM
G 74LS373 OC
P0.0 A0
D
P0.7 A7
Address
D0
D7
EA
P2.0 A8
P2.7 A12
8051 ROM
Reading ROM (2/2)
2. 74373 latches the
address and send to
PSEN ROM OE
ALE 74LS373 OC
G
P0.0 A0
D
P0.7 Address A7
D0
D7
EA 3. ROM send the
instruction back
P2.0 A8
P2.7 A12
8051 ROM
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.
Port 2 ( pins 21-28 )
• Port 2 does not need any pull-up resistors
since it already has pull-up resistors internally.
• In an 8031-based system, P2 are used to
provide address A8-A15.
Port 3 ( pins 10-17 )
• Port 3 does not need any pull-up resistors since it already
has pull-up resistors internally.
• Although port 3 is configured as an output port upon reset,
this is not the way it is most commonly used.
• Port 3 has the additional function of providing signals.
– Serial communications signal : RxD, TxD ( Chapter
10 )
– External interrupt : /INT0, /INT1 ( Chapter 11 )
– Timer/counter : T0, T1 ( Chapter 9 )
– External memory accesses in 8031-based
system : /WR, /RD ( Chapter 14 )
Port 3 Alternate Functions
P3 Bit 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
Contents:
I/O Programming; Bit
Manipulation
Time delay Generation and
calculation
Timer/Counter Programming
-Timers
- Counters
Interrupts Programming
Serial Communication
I/O Programming; Bit Manipulation
• To toggle every bit of P1 continuously, 3 ways exists:
• Way 1: Send data to Port 1 through ACC :
BACK: MOV A,#55H ;A=01010101B
MOV P1,A
ACALL DELAY
MOV A,#0AAH ;A=10101010B
MOV P1,A
ACALL DELAY
SJMP BACK
• Way 2: Access Port 1 directly :
BACK: MOV P1,#55H ;P1=01010101B
ACALL DELAY
MOV P1,#0AAH ;P1=10101010B
ACALL DELAY
SJMP BACK
• Read-modify-write feature :
MOV P1,#55H ;P1=01010101B
AGAIN: XRL P1,#0FFH
ACALL DELAY
SJMP AGAIN
– The instruction XRL P1,#0FFH do EX-OR P1 and FFH ( That is, to toggle P1. )
Bit Manipulation
• Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8
bits.
• This table shows how to name each pin for each I/O port.
• Example:
Solution:
SETB P1.2 ;make P1.2 an input
MOV A,#45H ;A=45H
AGAIN:JNB P1.2,AGAIN;get out when P.2=1
MOV P0,A ;issue A to P0
SETB P2.3 ;make P2.3 high
CLR P2.3 ;make P2.3 low for H-to-L
Note :
1. JNB: jump if no bit ( jump if P1.2 = 0 )
2. a H-to-L pulse by the sequence of instructions SETB and CLR.
Single-Bit Addressability of Ports
P0 P1 P2 P3 Port Bit
P0. P1. P2. P3. D0
P0.
0 P1.
0 P2.
0 P3.
0 D1
P0.
1 P1.
1 P2.
1 P3.
1 D2
P0.
2 P1.
2 P2.
2 P3.
2 D3
P0.
3 P1.
3 P2.
3 P3.
3 D4
P0.
4 P1.
4 P2.
4 P3.
4 D5
P0.
5 P1.
5 P2.
5 P3.
5 D6
P0.
6 P1.
6 P2.
6 P3.
6 D7
7 7 7 7
Time delay Generation and
calculation
• Machine cycle
Solution:
250x(1+1+1+1+2)+2x1.085 us=1627.5 us
Timers /Counters Programming
• The 8051 has 2 timers/counters:
timer/counter 0 and timer/counter 1. They
can be used as
1. The timer is used as a time delay
generator.
– The clock source is the internal crystal
frequency of the 8051.
2. An event counter.
– External input from input pin to count the
number of events on registers.
– These clock pulses cold represent the
Timer
• Set the initial value of registers
• Start the timer and then the 8051 counts
up.
• Input from internal system clock
8051
(machine cycle)
• When the registers equal to 0 and the
P2 P1 to
8051 sets a bit toSet denote time out LCD
Timer 0 TH0
TL0
Counter
• Count the number of events
– Show the number of events on registers
– External input from T0 input pin (P3.4) for
Counter 0
– External input from T1 input 8051
pin (P3.5) for
Counter 1 TH0
– External input from Tx input pin.P1 to
TL0
– We use Tx to denote T0 or T1. LCD
P3.4
a switch T0
Registers Used in
Timer/Counter
• TH0, TL0, TH1, TL1
• TMOD (Timer mode register)
• TCON (Timer control register)
• You can see Appendix H (pages 413-415)
for details.
• Since 8052 has 3 timers/counters, the
formats of these control registers are
different.
– T2CON (Timer 2 control register), TH2 and
TL2 used for 8052 only.
Basic Registers of the Timer
• Both timer 0 and timer 1 are 16 bits wide.
– These registers stores
• the time delay as a timer
• the number of events as a counter
– Timer 0: TH0 & TL0
• Timer 0 high byte, timer 0 low byte
– Timer 1: TH1 & TL1
• Timer 1 high byte, timer 1 low byte
– Each 16-bit timer can be accessed as two
separate registers of low byte and high byte.
Timer Registers
TH0 TL0
Timer 0
TH1 TL1
Timer 1
TMOD Register
• Timer mode register: TMOD
MOV TMOD,#21H
– An 8-bit register
– Set the usage mode for two timers
• Set lower 4 bits for Timer 0 (Set to 0000 if not
used)
• Set upper 4 bits for Timer 1 (Set to 0000 if not
(MSB) used) (LSB)
– NotC/T
GATE bit-addressable
M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
Figure 9-3. TMOD Register
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 Timer or counter selected cleared for timer
operation (input from internal system clock).
Set for counter operation (input from Tx
input pin).
(MSB) (LSB)
M1 GATE Mode
C/T
bitM11 M0 GATE C/T M1 M0
M0 Mode bit1 0
Timer Timer 0
C/T (Clock/Timer)
• This bit is used to decide whether the
timer is used as a delay generator or an
event counter.
• C/T = 0 : timer
• C/T = 1 : counter
Gate
• Every timer has a mean of starting and
stopping.
– GATE=0
• Internal control
• The start and stop of the timer are controlled by
way of software.
• Set/clear the TR for start/stop timer.
– GATE=1
• External control
• The hardware way of starting and stopping the
timer by software and an external source.
• Timer/counter is enabled only while the INT pin is
M1, M0
• M0 and M1 select the timer mode for timers
0 & 1.
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
XTAL
oscillator 12 ÷
C/T = 0
TH TL TF
overflow
TF goes high
TR flag
when FFFF 0
Timer Delay Calculation for
XTAL = 11.0592 MHz
(a) in hex (b) in decimal
Solution:
Look at the following steps.
(a) The period of the square wave = 1 / 50 Hz = 20
ms.
(b) The high or low portion of the square wave = 10
ms.
(c) 10 ms / 1.085 s = 9216
Example 9-12 (2/2)
MOV TMOD,#10H ;timer 1, mode
1
AGAIN: MOV TL1,#00 ;Timer value =
DC00H
MOV TH1,#0DCH
SETB TR1 ;start
BACK: JNB TF1,BACK
CLR TR1 ;stop
CPL P2.3
CLR TF1 ;clear timer
flag 1
Generate a Large Time Delay
• The size of the time delay depends on
two factors:
– They crystal frequency
– The timer’s 16-bit register, TH & TL
• The largest time delay is achieved by
making TH=TL=0. What if that is not
enough?
• Example 9-13 show how to achieve large
time delay.
Example 9-13
Examine the following program and find the time
delay in seconds.
Exclude the overhead due to the instructions in the
loop.
MOV TMOD,#10H
MOV R3,#200
AGAIN: MOV TL1,#08
MOV TH1,#01
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
DJNZ R3,AGAIN
Solution:
TH – TL = 0108H = 264 in decimal
Timer Mode 0
• Mode 0 is exactly like mode 1 except that
it is a 13-bit timer instead of 16-bit.
– 8-bit TH0 + 5-bit TL0
• The counter can hold values between
0000 to 1FFF in TH0-TL0.
– 213-1= 2000H-1=1FFFH
• We set the initial values TH0-TL0 to count
up.
• When the timer reaches its maximum of
1FFFH, it rolls over to 0000, and TF0 is
Timer Mode 2
• 8-bit timer.
– It allows only values of 00 to FFH to be
loaded into TH0.
• Auto-reloading
• TL0 is incremented continuously when
TR0=1.
• In the following example, we want to
generate a delay with 200 MCs on timer 0.
• See Examples 9-14 to 9-16
Steps of Mode 2 (1/2)
1. Chose mode 2 timer 0
– MOV TMOD,#02H
2. Set the original value to TH0.
– MOV TH0,#38H
3. Clear the flag to TF0=0.
– CLR TF0
4. After TH0 is loaded with the 8-bit value,
the 8051 gives a copy of it to TL0.
– TL0=TH0=38H
5. Start the timer.
Steps of Mode 2 (2/2)
6. The 8051 starts to count up by
incrementing the TL0.
– TL0= 38H, 39H, 3AH,....
7. When TL0 rolls over from FFH to 00, the
8051 set TF0=1. Also, TL0 is reloaded
automatically with the value kept by the
TH0.
– TL0= FEH, FFH, 00H (Now TF0=1)
– The 8051 auto reload TL0=TH0=38H.
– Go to Step 6 (i.e., TL0 is incrementing
Timer 1 Mode 2 with External
Input
XTAL
oscillator 12 ÷
C/T = 0
overflow
TL1 TF1 flag
reload
TR1 TH1
TF goes high
when FF 0
Example 9-15
Find the frequency of a square wave generated on
pin P1.0.
Solution:
MOV TMOD,#2H ;Timer 0,mode 2
MOV TH0,#0
AGAIN:MOV R5,#250 ;count 250 times
ACALL DELAY
CPL P1.0
SJMP AGAIN
DELAY:SETB TR0 ;start
BACK: JNB TF0,BACK
CLR TR0 ;stop
CLR TF0 ;clear TF
Example 9-16
Assuming that we are programming the timers for
mode 2, find the
value (in hex) loaded into TH for each of the
following cases.
(a) MOV TH1,#-200 (b) MOV TH0,#-60 (c) MOV
TH1,#-3
(d) MOV TH1,#-12 (e) MOV TH0,#-48
Decimal 2’s complement (TH
Solution: value)
Some 8051
-200 =assemblers
- C8H 38H provide this way.
-200 = --C8H
60 =
- 2’s
3CH C4H
complement of –200 = 100H –
C8H -= 338 H FDH
- 12 F4H
Example 9-17 (1/2)
Find (a) the frequency of the square wave
generated in the
following code, and (b) the duty cycle of this wave.
Solution:
“MOV TH0,#-150” uses 150 clocks.
The DELAY subroutine = 150 × 1.085 s = 162 s.
The high portion of the pulse is twice tat of the low
portion (66% duty cycle).
The total period = high portion + low portion
= 325.5 s + 162.25 s = 488.25 s
Example 9-17 (2/2)
MOV TMOD,#2H ;Timer 0,mode 2
MOV TH0,#-150 ;Count=150
AGAIN:SETB P1.3 high
ACALL DELAY period
ACALL DELAY low
CLR P1.3 period
ACALL DEALY
SJMP AGAIN
overflow
Timer 0 flag
external TH0 TL0 TF0
input
Pin 3.4
TF0 goes high
C/T = 1 TR0 when FFFF 0
Counter Mode 2
• 8-bit counter.
– It allows only values of 00 to FFH to be
loaded into TH0.
• Auto-reloading
• TL0 is incremented if TR0=1 and external
pulse occurs.
• See Figure 9.6, 9.7 for logic view
• See Examples 9-18, 9-19
Example 9-18 (1/2)
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
TL 1 count on P2.
Solution:
MOV TMOD,#01100000B ;mode 2,
counter 1
MOV TH1,#0
SETB P3.5 ;make T1
input port
AGAIN:SETB TR1 ;start
BACK: MOV A,TL1
MOV P2,A ;display in
Example 9-18 (2/2)
We use timer 1 as an event counter where it counts
up as clock pulses are fed into pin3.5.
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 8051
up , we must make P3.5
P2 is connected to 8 LEDs
an input port by making it
high. P2 to
and input T1 to pulse.
LEDs
P3.5
T1
Example 9-19 (1/3)
Assume that a 1-Hz frequency pulse is connected
to input pin 3.4.
Write a program to display counter 0 on an LCD.
Set the initial
value of TH0 to -60.
Solution:
Note that on the first round, it starts from 0 and
counts 256 events, since on RESET,8051 TL0=0. To
solve this problem, load TH0 with -60 at the
beginning of the program. P1 to
LCD
P3.4
1 Hz clock T0
Example 9-19 (2/3)
ACALL LCD_SET_UP ;initialize
the LCD
MOV TMOD,#00000110B ;Counter
0,mode2
MOV TH0,#-60
SETB P3.4 ;make T0 as
input
AGAIN:SETB TR0 ;starts the
counter
BACK: MOV A,TL0 ; every 60
events
ACALL CONV ;convert in
Example 9-19 (3/3)
;converting 8-bit binary to ASCII
CONV: MOV B,#10 ;divide by 10
DIV AB
MOV R2,B ;save low digit
MOV B,#10 ;divide by 10
once more
DIV AB
ORL A,#30H ;make it ASCII
MOV R4,A R4 R3 R2
MOV A,B
ORL A,#30H
MOV R3,A
MOV A,R2
A Digital Clock
• Example 9-19 shows a simple digital
clock.
– If we feed an external square wave of 60 Hz
frequency into the timer/counter, we can
generate the second, the minute, and the
hour out of this input frequency and display
the result on an LCD.
• You might think that the use of the
instruction “JNB TF0,target” to monitor
the raising of the TF0 flag is a waste of the
microcontroller’s time.
GATE=1 in TMOD
• All discuss so far has assumed that
GATE=0.
– The timer is stared with instructions “SETB
TR0” and “SETB TR1” for timers 0 and 1,
respectively.
• If GATE=1, we can use hardware to
control the start and stop of the timers.
– INT0 (P3.2, pin 12) starts and stops timer 0
– INT1 (P3.3, pin 13) starts and stops timer 1
– This allows us to start or stop the timer
Example for GATE=1
• The 8051 is used in a product to sound an
alarm every second using timer 0.
• Timer 0 is turned on by the software
method of using the “SETB TR0”
instruction and is beyond the control of the
user of that product.
• However, a switch connected to pin P3.2
can be used to turn on and off the timer,
thereby shutting down the alarm.
Timer/Counter 0
XTAL
oscillator 12 ÷
C/T = 0
C/T = 1
T0 Pin
Pin 3.4 TR0
Gate
INT0 Pin
Pin 3.2
Interrupts Programming
• An interrupt is an external or internal event that interrupts
the microcontroller to inform it that a device needs its
service.
ORG 30H
Enabling and disabling an interrupt:
Example:
Write a program using interrupts to simultaneously create 7
kHz and 500 Hz square waves on P1.7 and P1.6.
Solution:
ORG 0
LJMP MAIN
ORG 000BH
LJMP T0ISR 8051 143s
ORG 001BH 71s
LJMP T1ISR P1.7
ORG 0030H
MAIN: MOV TMOD,#12H
MOV TH0,#-71
SETB TR0
SETB TF1 2ms
MOV IE,#8AH 1ms
P1.6
MOV IE,#8AH
SJMP $
T0ISR: CLR P1.7
RETI
T1ISR: CLR TR1
MOV TH1,#HIGH(-1000)
MOV TL1,#LOW(-1000)
SETB TR1
CPL P1.6
RETI
END
External Interrupts:
Level-triggered (default)
INT0
)Pin 3.2( 0
0003
IT0
1 IE0
2 (TCON.3)
Edge-triggered
Level-triggered (default)
INT0
)Pin 3.3( 0 0013
IT1
1 IE1
2 (TCON.3)
Edge-triggered
Exercise
• We have a motor that send pulses to
micro proportional to it’s r.p.m. write a
program that if the number of pulses per
10-second are less than 100, send 1 to
P1.0, and if more than 200, send 1 to P1.1
• Write a program and design hardware that
connect key-pad to micro and identifies
which key is pressed.
Serial
Communication
Basics of serial communication
• Baud Rate
Start and stop bits
RxD and TxD pins in the 8051
• TxD pin 11 of the 8051 (P3.1)
• RxD pin 10 of the 8051 (P3.0)
SBUF register
MOV SBUF,#’D’ ;load SBUF=44H, ASCII for ‘D’
MOV SBUF,A ;copy accumulator into SBUF
MOV A,SBUF ;copy SBUF into accumulator
MAX232