Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OF 8051 MICROCONTROLLER
BY
A.NARMADA
Assoc.Prof/ECE
PROGRAMMING CONCEPTS
Addressing modes
• MOV A, Rr
• MOV Rr, A
DIRECT ADDRESSING MODE
• All 128 bytes of internal RAM &
SFRs
SFR Address(hex)
SFR Address(hex)
PSW 0D0
A 0E0 SBUF 99
B 0F0 SCON 98
DPL 82 SP 81
DPH 83 TCON 88
IE 0A8 TMOD 89
IP 0B8 THO 8C
P0 80 TLO 8A
TH1 8D
P1 90
TL1 8B
P2 0A0
P3 0B0 e.x mov a,80h
PCON 87 Mov 0a8h,77h or mov ie,77h
Indirect Addressing mode
• Only registers Ro, R1 may be used for indirect addressing.
• The number in register R0,R1 must be a RAM address. (00h –
7Fh)
Examples
Ex:
XCH A, Rr
XCH A, address
XCH A, @Rr
XCHD A, @Rr
Logical operations
Boolean operation 8051 mnemonics
AND ANL
OR ORL
XOR XRL
NOT CPL
BYTE -LEVEL LOGICAL OPRATIONS:
ANL A ,#n
ANL A ,add
ANL A ,#Rr
ANL A,#@R0 or R1
ANL add,A
ANL add,#n
Similarly for OR and XOR operations
CLR A
CPL A
• Only internal RAM or SFRs may be logically manipulated
Bit level logical operations
Internal RAM bit address SFR Bit Addresses
ANL C,b AND C and the addressed bit; put the result in C
ANL C,/b AND C and the complement of the addressed bit;
put the result in C; the addressed bit is not altered
ORL C,b OR C and the addressed bit; put the result in C
ORL C,/b OR C and the complement of the addressed bit; put
the result in C; the addressed bit is not altered CPL
C Complement the C flag
CPL b Complement the C flag
CLR C clear the c flag to 0
CLR b clear the address bit to 0
MOV C,b copy the addressed bit to the C flag
MOV b,C copy the C flag to the addressed bit
SETB C set the flag to 1
SETB b set the addressed bit to 1
7 6 5 4 3 2 1 0
RLC A
C 7 6 5 4 3 2 1 0
Carry Flag
RR A
7 6 5 4 3 2 1 0
RRC A
7 6 5 4 3 2 1 0 C
Carry Flag
SWAP A
7 6 5 4 3 2 1 0
High Nibble Low Nibble
Arithmetic operations
INC Destination Increment destination by 1
destination
MUL AB Multiply the contents of register
A and B
DIV AB Divide the contents of register A
by the contents of register B
DA A Decimal Adjust the A register
INC Rr
INC DIRECT ADDRESS
INC INDIRECT ADDRESS
INC DPTR
DEC Rr
DEC DIRECT ADDRESS
DEC INDIRECT ADDRESS
ADD/ADDC A, #N
ADD/ADDC A, add
ADD/ADDC A,Rr
ADD/ADDC A,@Rp
SUBB with all the Addressing modes.
No SUB
MUL AB ; multiply A by B.
(A) low order byte of product
(B) high order byte of product
• DIV AB ; Divide a by B.
(A) < --- quotient (B) < --- remainder.
• DA A ; Decimal adjust the A register.
• These jump opcodes are one reason the 8051 in such a powerful
microcontroller.
• Bit Jumps
• Bit jumps all operate according to the status of the carry lag in the PSW or
the status of any bit addressable location.
• CJNE A, add, radd compare the contents of the A register with the
contents of the direct address; if they are not equal,
then jump to the relative address; set the carry flag
to 1 if a is less than the contents of the direct
address; other wise, set the carry flag to 0.
• CJNE A, #n, radd compare the contents of the A register with the
immediate number n ; if they are not equal,
then jump to the relative address; set the carry flag
to 1 if A is less than the number; other wise, set the
carry flag to 0.
• CJNE Rn, #n, radd compare the contents of register Rn with the
immediate number n; if they are not equal, then
jump to the relative address; set the carry flag to 1
if Rn is less than the number; other wise, set the carry
flag to 0.
CJNE @Rp,#n, radd compare the contents of the address contained
in register Rp to the number n; if they are not
equal,then jump to the relative address; set the
carry flag to 1 if the contents of the address in
Rp are less than the number; other wise, set
the carry flag to 0.
INTERRUPT ADDRESS(HEX)CALLED
IEO 0003
TFO 000B
IE1 0013
TF1 001B
SERIAL 0023
P0.0
P0.1
P
8751 P0.2
P0.3
O
P0.4 R
P0.5 T0
8951 P0.6
P0.7
Read-modify-write feature:-
E. g : Mov p1,#55H
Again: XRL P1, #0FFH
ACALL Delay
SJMP Again
Single- bit addressability of ports:-
E.g. Toggle P1.2 continuously
Back: CPL P1.2
ACALL Delay
SJMP Back
Single Bit Instructions
Instruction Function
SETB bit Set the bit(bit=1)
CLR bit Clear the bit(bit=0)
CPL bit Complement the bit (bit= not bit)
Instruction Function
SETB C Make CY=1
CLR C Clear carry bit (CY=0)
CPL C Complement carry bit
MOV b,c Copy carry status to bit location (CY=b)
MOV c,b Copy bit location status to carry (b=CY)
JNC target Jump to target if CY=0
JC target Jump to target if CY=1
ANL C ,bit AND CY with bit and save it on CY
ANL C,/bit AND CY with inverted bit and save it on CY
ORL C,bit OR CY with bit and save it on CY
ORL C,/bit OR CY with inverted bit and save it on CY
Assume that RAM bit location 12H holds the status of whether there has been a
phone call or not .If it high , it means there has been a new call since it was
checked the last time. Write a program to display “New Messages “ on an LCD if bit
RAM 12H is high. If it is low, the LCD should say “ No New Messages”.
Solution:
MOV C, 12H ;copy bit location 12H to carry
JNC NO ; check to see if is high
MOV DPTR,#400H ; yes, load address of messages
LCALL DISPLAY ;display message
ORG 400H
YES_MG : DB “New Messages”
ORG 420H
NO_MG : DB “No New Messages”
TIMER / COUNTER PROGRAMMING :-
Timer programming:-
Mode1 Programming
Characteristics and operations of mode 1:
• It is a 16- bit timer; therefore , it allows values of 0000 to FFFFH to be loaded into
the timer’s registers TL and TH.
• After TH and TL are loaded with a 16-bit initial value, the timer must be started .
This is done by “SETB TR0”for timer 0 and “SETB TR1” for timer 1.
• After the timer is started, it starts to count up. It counts up until it reaches its limit of
FFFFH .When it rolls over from FFFFH to 0000, it sets high a flag bit called TF
(timer flag). This timer flag can be monitored. When this timer flag is raised, one
option would be to stop the timer with the instructions “CLR TR0” for timer 0.
Again, it must be noted that each timer has its own timer flag:TF0 and TF1.
• After the timer reaches its limit and rolls over, in order to repeat the process the
registers TH and TL must be reloaded with the original value, and TF must be
reset to 0.
Steps to program in mode 1:-
• Load the TMOD value register indicating which timer (timer 0 or timer 1)
is to be used and which timer mode (0 or 1) is selected.
• Load registers TL and TH with initial count values.
• Starts the timer.
• Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction
to see if it is raised .get out of the loop when TF becomes high.
• Stop the timer.
• Clear the TF flag for the next round.
• Go back to step 2 to load TH and TL again.
Creating a square wave of 50% duty cycle on the P1.5 bit. Timer 0 is used to generate
the time delay .
MOV TMOD, # 01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0, # 0F2H ;TL0=F2H,The low byte
MOV THO,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE ;load TH,Tl again
;------------- delay using timer 0
DELAY: SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0 until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
Solution:
• In the above program notice the following steps
• TMOD is loaded.
• FFF2H is loaded into TH0-TL0.
• P1.5 is toggled for the high and low portions of the pulse.
• The DELAY subroutine using the timer is called.
• In the DELAY subroutine the timer 0 is started by the “SETB TR0” instruction.
• Timer 0 counts up with the passing of each clock, which is provided by the crystal
oscillator. As the timer counts up , it goes through the states of FFF3, FFF4, FFF5, FFF6,
FFF7, FFF8, FFF9, FFFA, FFFB, and so on until it reaches FFFFH. One more clock rolls
it to 0,raising the timer flag (TF0=1).At that point, the JNB instruction falls through.
• Timer 0 is stopped by the instruction “CLR TR0”.The DELAY subroutine ends. and the
process is repeated.
Notice the to repeat the process , we must reload the TL and TH registers, and start the
timer again.
TOTAL 27
T = 2 * 27 * 1.085µs = 58.59 µs and F = 17067.75Hz
Finding values to be loaded into the timer:-
With crystal frequency - 11.0592 MHz
Steps:-
• Divide the desired time delay by 1.0 85µs.
• Perform 65536-n,where n is the decimal value we got in step1.
• Convert the result of step 2 to hex:yyxx H.
• Set TL=xx TH=yy
Example:
Assume that XTAL = 11.0592MHz what value do we need to load into the timer’s
registers if we want to have a time delay of 5ms. Show the program for timer 0 to
create a pulse width of 5ms on P2.3
Solution:
Since XTAL = 11.0592MHz, the counter counts up every 1.085µs.This means that out
of many 1.085 µs intervals we must make a 5ms pulse. To get that , we divide one by
the other.
We need 5ms / 1.085 µs =4608 clocks. To achieve that we need to load into TL
and TH the value 65536-4608 =60928= EE00h. Therefore, we have TH= EE and
TL = 00.
Mode2:-
Characteristics and operations of mode2
• It is an 8-bit timer ; therefore , it allows only values of 00 to FF H to be loaded into
the timers registers TH.
• After TH is loaded with the 8 bit- value , the 8051 gives a copy of it to TL. Then the
timer must be started. This is done by the instructions “SETB TR0” for timer 0 and
“SETB TR1” for timer 1.This is just like mode1.
• After the timer is started, its starts to count by incrementing the TL register. It
counts up until it reaches its limit of FF H. When it rolls over from FF h to 00, it
sets high the TF (timer Flag). If we are using timer 0 ,TF goes high; if we are
timer 1 , TF is raised.
• When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded
automatically with the original value kept by the TH register .To repeat the
process, we must simply clear TF and let it go without any need bye the
programmer to reload the original value. This makes mode2 an auto-reload, in
contrast with mode1 in which the programmer has to reload TH and TL.
Steps to program in mode2:-
• Load the TMOD value register indicating which timer (timer 0 or timer 1) is
to be used , and the timer mode ( mode 2) is selected.
• Load the TH registers with the initial count value.
• Start the timer.
• Keep monitoring the TF with the “JNB TFx, target” instruction to see
whether it is raised. Get out of the loop when TF goes high.
• Clear the TF flag.
• Go back to step 4, since mode 2 is auto- reload.
Example:-
Assume that XTAL= 11.0592M Hz , find (a) the frequency of the square wave
generated on pin P1.0 in the following program, and (b) the smallest frequency
achievable in this program, and the TH value to do that.
a) First notice the target address of SJMP. In mode 2 we do not need to reload TH
since it is auto-reload. Now(256-05) x 1.085 µs is the high potion of the pulse.
Since it is a 50% duty cycle wave, the period T is twice that ;as a result T = 2 x
272.33 µs and the frequency = 1.83597 kHz
b) To get the smallest frequency , we need the largest T and that is achieved when
TH = 00. In that case, we have T = 2 x 256x1.085 µs =555.52 µs and the
frequency = 1.8 kHz
To achieve the large time delay:-
Reset 0000 9
External hardware interrupt 0003 P3.2
0(INT 0)
Timer 0 interrupt (TF0) 000B
TF Interrupt
ExEeeErrkkkuiuii
eee
Example:-
Assume that pin 3.3(INT1) is connected to a pulse generator, write a program in which the falling edge of the pulse will
send a high to P1.3, which is connected to an LED(or bezzer).In other words, the LED is turned on and off at the same
rate as the pulses are applied to the INT1 pin. This is an edge –triggered version.
Solution:
ORG 0000H
LJMP MAIN
-------- ISR for hardware interrupt INT1 to turn on the LED
ORG 0013H ;INTI ISR
SETB P1.3 ;turn on the LED
MOV R3,#255
BACK: DJNZ R3,HERE ;keep the buzzer on for a while
CLR P1.3 ;turn off the buzzer
RETI
------ mian program for initialization
ORG 30H
MAIN: SETB TCON.2 ;make INTI edge-trigger interrupt
MOV IE,#10000100B ;enable external INT1
HERE: SJMP HERE ;stay here until get interrupted.
END
Minimum pulse duration to detect edge-triggered interrupts
Programming the serial communication
interrupt:-
Serial interrupt is invoked by TI or RI flags
Example:-
Write a program in which the 8051 reads data from P1 and writes it to P2 continuously while giving a copy of
it to the serial COM port to be transferred serially.Assume that XTAL=11.0592.set the baud rate at 9600.
Solution:-
ORG 0;
LJMP MAIN
ORG 32H
LJMP SERIAL ;jump to serial interrupt ISR
ORG 30H
MAIN: MOV P1,#0FFH ;make P1 an input port
MOV TMOD,#20H ;timer 1 mode 2 (auto –reload)
MOV TH1,#0FDH ;9600 baud rate
MOV SCON,#50H ;8-bit, 1 stop, ren enabled
MOV IE,#10010000B ;enable serial interrupt
SETB TR1 ;start timer 1
BACK: MOV A, P1 ;read data from port 1
MOV SBUF,A ;give a copy to SBUF
MOV P2,A ;send it to P2
SJMP BACK ;stay in loop indefinitely
--------serial port ISR
ORG 100H;
SERIAL: JB TI,TRANS ;jump if TI is high
MOV A,SBUF ;otherwise due to receive
CLR RI ;clear RI since CPU does not
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU does not
RETI ;return from ISR
END
Interrupt priority in the 8051 upon reset :-