Documente Academic
Documente Profesional
Documente Cultură
8051 AND
PROGRAMMING.
PORT 0 :
PORT 0 occupies a total of 8 pins
( from pin number 32 to 39 )
It can be used for I/P or O/P .
To use the pins of port zero as a both I/P or O/P
port each pin must be connected externally to a 10k
pull up resistor .
Ex .
MOV A , #FFH
MOV P0 , A
MOV A , P0
PORT 0 as O/P
port :
Ex .
MOV A , #00H
MOV P0 , A
P0RT 1:
Ex :
port 1 .
MOV P1, A
EX
:
BACK :MOV
A,P1
MOV A,# FFH
MOV P2,A
SJMP BACK
MOV P1, A
MOV A,P1
MOV A , #00H
MOV P2 ,A
PORT 2 :
PORT 2
PORT 2 as I/P
port :
EX :
MOV A, # FFH
MOV P2 ,A
MOV A,P2
PORT 2 as O/P
port :
EX :
MOV A , #00H
MOV P2 ,A
PORT 3 :
PORT 3 as I/P
port :
Ex :
MOV A , #FFH
MOV P3 , A
MOV A , P3
Ex :
MOV A , #00H
MOV P3 , A
MOV P0,A
PORT 3 as O/P
port :
P3.0 RXD
P3.1 TXD
P3.2 INTO
P3.3 INTO
P3.4 T0
P3.5 T1
P3.6 Wr
P3.7 Rd
TIMER/COUNTER
PROGRAMMING IN 8051 :
TMOD Register.
GATE 1
C/T 1
M1
M0
GATE 0
C/T 0
M1
M0
M1
0
0
M0
0
1
MODE
0
1
DESCRIPTION
8-BIT AUTO
RELOAD
SPLIT MODE
13-BIT TIMER
16-BIT TIMER
TIMER/COUNTER PROGRAMMING
IN 8051 :
ANSWER
CHARACTERISTICS OF MODE 1
PROGRAMMING :
Write a program to create square wave of 50% duty cycle on the P1.5 bit .
MOV TMOD , #01
HERE : MOV TL0 , #16H
MOV TH0 , #FFH
CPL P1.5
ACALL DELAY
SJMP HERE
CLR TR0
CLR TF0
RET
Timer 0 , Mode 1
Counter programming :
8051 INTERRUPTS
INTERRUPT
1)If Interrupts are used
POLLING
1) Polling can monitor the
of microcontroller based on
the priority assigned to it .
This does not lead to the
wastage of microcontroller time
INTERRUPT
2)Interrupts can be assigned
Priority .
3)interrupt method
Microcontroller can ignore
a device request for service
POLLING
2) In this method it is not
possible to assign priority
since it checks all devices
in a round robin fashion .
3) This is not possible in
polling .
It finishes the instruction it is executing and saves the address of the next
instruction on the stack .
It also saves the current status of all the interrupts internally .
It jumps to a fixed location in memory called the interrupt vector table that
holds the address of the I S R .
The microcontroller gets the address of the I S R from the interrupt
vector table and jumps to it .
It starts to execute the interrupt service subroutine until it reaches the last
instruction of the subroutine which is RETI ( Return from the interrupt ).
Upon executing the RETI instruction , The microcontroller returns to the
place where it was interrupted . First it gets the program counter ( P C )
address from the stack by popping the top two bytes of the stack into the
P C . Then it starts to execute from that address .
1. Reset :
When Reset pin is activated the 8051 jumps to
address location 0000 .
Two interrupts are set aside for the timers .
One for timer 0 and one for timer 1( TF0 & TFI)
Two interrupts are set aside for external hardware
interrupts . Pin no. 12(P3.2) and 13(P3.3) are for
the external hardware interrupts INT0 and INT1 .
Serial communication has a single interrupt that
belongs to both receive and transfer .(TI &RI)
INTERRUPT
RESET
INT0
INT1
Timer 0 interrupt (TF0)
Timer 1 interrupt (TF1)
Serial comm. Interrupt (RI & TI)
ROM location
0000
0003
0013
000B
001B
0023
PIN
9
12
13
-
EA
ET2 ES
EA
- ET2 ES
The timer flag (TF) is raised when the timer rolls over .
If the timer interrupt in the IE register is enabled , Whenever the timer rolls
over , TF is raised and microcontroller is interrupted and jumps to the
interrupt vector table .
In this way the microcontroller can do other things until it is notified that
the timer has rolled over .
The 8051 has two external hardware interrupts they are INT0 and INT1 .
Level triggered
Edge triggered
Level Triggered Interrupt:In the level triggered mode , INTO & INT1 pins are
normally high.
The low level signal at the INT pin must be removed before the execution of the
last instruction of ISR i.e. RETI, otherwise another interrupt will be generated.
T F 1 T R 1 T F 0 T R 0 IE 1
07
IE 1 External interrupt one edge flag.
IT 1
IE 0
IT 0
00
used .
Set or cleared by software to specify edge / low level triggered external interrupt is
If IT0 & IT1 both are 0s , means that the IT0 & INT1 pins are low level triggered .
If both these bits are high , it means that INT0 & INT1 are edge triggered.
Priority
Interrupt
6
TF2
5
RI & TI
4
TF1
3
INT 1
2
TF 0
1
INT 0
If interrupts INT 0 , TF 0 , & INT 1 are activated at the same time , then 8051
checks all five interrupts according to sequence . If any is activated it service it
in sequence . Therefore when above three interrupts are activated , INT 0 is
serviced first , then timer 0 ( TF 0) and finally INT1 is serviced.
PS
PT1
PX1
PT0
PX0
In data framing all the bytes are packed between a start and a
Stop
stop
bitbit. D6 D5 D4 D3 D2 D1 D0 start bit
The start bit is zero and stop bit is one .When there is no
transfer of data the signal is one which is referred as mark .
Baud rate :
The rate of data transfer in serial data communication is started at bps bits/sec
and it is called baud rate . The below table will be what data should be stored in
TH1 register for various baud rates .
921.6K Hz//32=28,800Hz.
BAUD RATE
TH1 in decimal
TH1 in hexadecimal
28,800/3=9600
-3
FD
28,800/6 =4800
-6
FA
28,800/12 =2400
-12
F4
28,800/24 =1200
-24
E8
T h e S C O N re g is te r is a 8 b it re g is te r u s e d to p ro g ra m s ta rt b it , s to p b it
a n d d a ta b its .
These 2 bit are serial port mode specifier these two determine the framing of
data by specifying the number of bits between a start bit and the stop bit and the
number of stop bits used are
SM0
SM1
MODE
1 stop,1 start
In serial mode one for each byte of data to be transferred one start bit and one
stop bit is used between one start bit and one stop bit only one byte of data can
be transferred and received .
SM2 :
SM2 is a 5th bit of SCON register if this bit is one then 8051 can use
multiprocessing .
REN bit is 4th bit of SCON register when the REN bit is high it allows the 8051 to
receive data on the RXD line . If REN is low 8051 cannot receive the data .
TI bit :
RI :
RI i.e. receive interrupt is the 0 bit of SCON register.
When the 8051 receives data serially it gets rid of start and stop bits and places
the byte in SBUF register .
Then 8051 raises the RI flag bit to indicate that a byte have been received and
it should be picked up before it is lost .
1)The TMOD register is loaded with the value 20H , Indicating the use
of timer 1 in mode 2 to set the baud rate .
2)The TH1 is loaded with one of the values in the table below to set
baud rate for serial data transfer .
BAUD RATE
TH1( decimal )
TH1( Hexadecimal )
9600
-3
FD
4800
-6
FA
2400
-12
1200
-24
F4
E8
3)The SCON register is loaded with the value 50H indicating serial mode 1 ,
Where an 8 bit data is formed with start and stop bits .
6)The character byte to be transferred serially is written into the SBUF register .
7)The TI flag bit is monitored with the use of the instruction JNB TI, target to
see if the character has been transferred completely .
1)The TMOD register loaded with the value of 20H , Indicating the use of timer 1
in mode 2 to set baud rate .
2)The TH1 is loaded with the one of the value in the table below to set the baud
rate for serial data transfer .
If XTAL = 11.09Mhz , Timer =1 , TH1 register value for various baud rate .
BAUD RATE
TH1(Decimal )
TH1( Hexadecimal )
9600
-3
FD
4800
-6
FA
2400
-12
F4
1200
-24
E8
3)The SCON register is loaded with value 50H indicating serial mode 1 where
an 8 bit data is formed with start and stop bit .
6)The RI flag bit is monitored with the use of the instruction JNB RI, target to
see if an entire character have been received yet or not .
7)When RI is raised , SBUF has a byte . Its contents are moved into a safe
pulse .