Microcontroller 8051 - Notes
Microcontroller 8051 - Notes
1.1 Introduction
Microcontroller stands for a Controller which is of Micro size.
Microcontroller works on digital signals in bit level to control some process, i.e., a collection of
job/ tasks designed to meet certain objectives in the form of certain outputs. A microcontroller’s
size is small compared to its enormous capacity.
1.2 Evolution of Relevant Technology
It all started with General Purpose Microprocessors followed by Bit-Slice Processor. Later Special
purpose Processors like Digital signal processors and Microcontrollers were developed.
A General Purpose Microprocessor is an open-ended processor which is generic in nature to suit in
variety of applications. An additional set of hardware makes it complete and useful for physical
applications. In a bit slice processor a group of hardware and software instruction sets comes as
SLICE and one such SLICE can be connected to an existing one to enhance functionality. Special
purpose processors come with certain facilities that are introduced into a single-chip for dedicated
application. Each of these chips is self-sufficient to suit certain applications and needs hardware
for expanding functionality.
A DSP (Digital Signal Processor) is used for high speed extensive computation required in signal
processing. A microcontroller is used for special-purpose signal processing and has built-in blocks
for interfacing with external world. Application examples of microcontroller are Microwave Oven,
washing machine, sewing machine, auto ignition system, smart toys, smart DVD players, different
controllers in automobiles etc.
1.3 Generalized Architecture
Microprocessor Microcontroller
In 1981, Intel Corporation introduced an 8-bit microcontroller called the 8051. This
microcontroller has 128 bytes of on-chip RAM, 4K bytes of on-chip ROM, two timers, one serial
port and four ports all on a single chip. The 8051 is an 8-bit processor as its CPU can work only
on 8 bits of data at a time. Data larger than 8 bits have to be broken into 8-bit pieces to be
processed by the CPU.
One state (S) = One Phase -1 half (P1) + One Phase -2 half (P2).
One Machine Cycle (M) = 6 states (numbered S1 through S6).
One Machine Cycle (M) = 12 Clock Cycles. One Machine Cycle (M) = 12 / Clock Frequency
Two most common oscillator frequencies in 8051 are 12 MHz and 11.0592 MHz.
Indirect addressing must NOT refer to any SFR. Hence the below set of instructions are not valid.
MOV R1, #D0h
MOV @R1, #23h
First line makes R1 to point to PSW (an SFR). Now we can’t send any value (say, 23h for
example) to PSW by addressing R1 indirectly.
Also only R0 and R1 are used in indirect addressing.
Arithmetic operation examples for indirect addressing are shown below.
ADD A, @R0 A<< A+MR0 Data from location pointed by R0 is added to
A and result is stored in A
ADDC A, @R0 A<< A+ MR0 +C Data from location pointed by R0 is added to
A along with carry and result is stored in A
SUBB A, @R0 A<< A- MR0 -C Data from location referenced by R0 is
borrowed from A along with Carry and result
is stored in A
Example-1: PUSH
Example-2
MOV A, #7Bh A<< 7Bh
MOV 0F0H, #0FEh B << FEh Same as MOV B, #0FEh
MUL AB A<< 0Ah, B<< 7Ah, OV<< 1
7A0Ah
(2) Division:
DIV AB B A<< A/B
There should not be any space or comma between A and B in the instruction. A holds the quotient
and B holds the remainder.
Division by zero yields undefined result in A and B, and OV = 1.
3.7 Logical operations
3.7.1 Byte level logical operations
3.7.1.1 Rotate Operations: Work only on A
(i) RL A: Rotate the Accumulator left by single field
(iv) RRC A: Rotate the Accumulator right by single field with carry
Rotate right by single – field means division by 2, provided there is zero-filling from left side and
no ‘1’ is lost after shifting.
3.7.1.2 AND Operation
ANL A, #7Bh A<< A and 7Bh ANL=AND Logical
ANL A, 7Bh A<< A and M7B
ANL 7Bh, A M7B << A and M7B
ANL A, Ri A << A and Ri, Ɐi = 0(1)7
ANL A, @Ri A << A and MRi, i=1,2
ANL 7B, #7Bh M7B << M7B and 7Bh
3.7.1.3 OR Operation
ORL A, #7Bh A<< A or 7Bh ORL=OR Logical
ORL A, 7Bh A<< A or M7B
ORL 7Bh, A M7B << A or M7B
ORL A, Ri A << A or Ri, Ɐi = 0(1)7
ORL A, @Ri A << A or MRi, i=1,2
ORL 7B, #7Bh M7B << M7B or 7Bh
Though the above set of examples work on byte addressable locations (Accumulator /Register),
logical and/or/xor operations are carried out in a bit-wise fashion internally, i.e. , for each bit
location (say, D0-D7 ) logical operations are carried out separately resulting a single bit output for
that location.
No flag is affected unless the direct address refers to PSW in all the above three operations. Only
internal RAM and SFR are logically manipulated. Latch data is used in logical operation when
port is destination. Pin data is used in logical operation when port is source.
Example-1: ANL P0, A P0 << P0 and A
In this example P0 acts as both destination and source.
Now Latch does not allow port to act both ways in a single
instruction. Hence, instruction takes latch as source of data
and result, which is saved in to Latch, is called latch data.
Example-3: What will be the content of the accumulator after the execution of the steps (a)
and (b) as given below in 8051?
MOV A, #98h
ORL A, #57h -----------(a)
ANL A, #34h -----------(b)
A: 98h = 1001 1000
(a) 1001 1000 = 98h=A
Now, Starting bit-address of any location= (Byte-address – 20h) x 08h. This is further explained in
the table below.
Byte-Address Starting Bit-Address
20h 00h
21h 08h
22h 10h
23h 18h
This instruction SETB sets the corresponding bit referenced by the bit-address.
(ii) Example-2: To clear D4 of Accumulator:
CLR ACC.4
ACC is used for Accumulator in bit-based instruction.
A is used for Accumulator in byte-based instruction. To clear complete Accumulator CLR A
instruction is used.
MOV works with byte address with only one exception as above.
Bit level addresses as applicable to SFRs are as below:
SFR Name Byte Address Bit Address
A E0h E0h-E7h
B F0h F0h-F7h
IE A8h A8h-AFh
IP B8h B8h-BFh
P0 80h 80h-87h
P1 90h 90h-97h
P2 A0h A0-A7h
P3 B0h B0h-B7h
PSW D0h D0h-D7h
TCON 88h 88h-8Fh
Byte – address of these SFRs = bit-address of D0 (Least Significant Bit) of these SFRs.
3.8 other operations and few comments
(i) Decimal Adjust Accumulator: DA A
It converts hexadecimal number in A into equivalent binary coded decimal number.
The conversion rule is that if Hex digit > 9, digit=digit+6.
The conversion is automatically carried out by the instruction provided. ‘DA A’ comes just after
ADD or ADDC instruction. Numbers taking part in addition previously must be within the range
0-9h. If original byte is greater 9, addition performed is a hexadecimal addition and hence ‘DA A’
instruction will not work.
(ii) Overflow Flag (OV) << C7 xor C6, where C7 means carry out of bit-D7 and C6 means carry
out of bit-D6
(iii) Carry (C) << bit from D-7
(iv) Auxiliary Carry (AC) << bit from D-3
(v) INC increments the content of a register/ DPTR, DEC increments the content of a register/
DPTR,
3.9 Program Counter & program execution in 8051
PC or program counter is 16-bit SFR. It increments automatically when program control goes to a
line of instruction and it holds the address of next instruction as shown below.
At the end of execution of instruction-3, program control goes to the address of instruction
pointed to by PC and not going to instruction-4. This is shown in the figure below.
#2. num EQU 28h ‘num’ is just a name used for 28h. It is NOT a memory where 28h
is stored
Examples
numx EQU 3Bh hex format
numx EQU 00111011b binary format
numx EQU 59 decimal format
numx EQU 59d decimal format
#3. ALE92: DB 3Ah the data 3Ah is stored in address labeled as ALE92
Examples
ALE92: DB 3Ah hex data stored in label ALE92
ALE92: DB 58 decimal data stored in label ALE92
ALE92: DB 58d decimal data stored in label ALE92
ALE92: DB 01011011b binary data stored in label ALE92
ALE92: DB 3Ah, 40h, 2Ch, 0C1h 4 data stored in consecutive memory locations
in the form of an array, address being started from ALE92
ALE92: DB “hello” 5 ASCII data stored in consecutive memory
locations in the form of an array, address being started from ALE92
#4. ALE92: DW 0D43Ah the data 3Ah is stored in address labeled as
ALE92, and the data D4h is stored in address labeled as ALE92+1
#5. END/.END last line of source file (*.asm)
Assembly level program must be sequentially written, while assembler directives need not be
sequential.
4.3 Programming examples
1. Write a program to add two 16-bit numbers. The numbers are 2E5Fh and A3B4h. Store
the sum in R7 (higher byte), R6 (lower byte), R5 (carry).
2. Write a program to get hex data in the range 00-FFh from port-0 and convert it to
decimal. Save the digits in R5, R6, R7(LSB).
Illustration:
1) Let input hex data=DAh is available from port-0
Decimal equivalent of the number=13x16+10=218.
2) Using instruction DIV AB, Divide 218 by 10.
Here quotient=A=21 and remainder=B=8.
3) Store B in R7.
4) Again divide A (=21) by 10.
At this, A=quotient=2, B=remainder=1.
5) Store B in R6.
6) Finally Store A in R5.
Program:
PORTHIGH EQU 0FFh
DECRADIX EQU 10d decimal 10
ORG 0000h
MOV P0, #PORTHIGH configure P0 as input port
MOV B, #DECRADIX
MOV A, P0
DIV AB divide by decimal 10
MOV R7, B save lower digit in R7
MOV B, #DECRADIX
DIV AB divide by decimal 10
MOV R6, B save middle digit in R6
©AnirbanM@Dept of EE, Ghatal Govt Polytechnic 21
MOV R5, A save higher digit in R5
END
M1 M0 Mode Description
0 0 0 13- bit timer
0 1 1 16-bit timer
1 0 2 8-bit auto reload
mode
1 1 3 Split timer mode
In this program, every time the timer-0 runs in timer mode for 5 ms and then the output bit is
complemented (0 becoming 1 and 1 becoming 0). This way a half cycle of 5 ms is obtained for the
continuous square wave with time period 10 ms, i.e., frequency 100 Hz. Every time the timer
Example-1
1. Write a program to enable Timer-0 interrupt.
MOV IE, #88h (or)
SETB ET1
SETB EA
2. Write a program to enable External-0 interrupt on edge-sensitive signal.
SETB IT0
SETB EX0
SETB EA
6.3 Polling Sequence, Interrupt priority & Interrupt Sequence:
When more than one interrupt is activated, microcontroller, while busy in executing main
program, searches for interrupt signal at the background. Sequence of such events is called Polling
Sequence. The sequence is as shown below-
1. External-0 interrupt
2. Timer-0 interrupt
3. External-1 interrupt
4. Timer-1 interrupt
5. Serial interrupt.
Interrupt priority:
In spite of having polling sequence, interrupt priorities can forcibly be changed by user. If
priorities of two interrupts are kept same, sequence of interrupt service will be as per polling
sequence. Interrupt priorities are controlled by IP SFR.
ORG 0000h
MOV P1, #00h Configure P1 as output port
CLR A Clear the accumulator
ORG 0000h
CLR C clear the accumulator
MOV A, #00h send 00 to accumulator
MOV P1, A send the accumulator value to port P1
UP: ACALL DELAY call the delay function
ADD A, #10h add with accumulator 10h
MOV P1, A send the value of accumulator to P1
CJNE A, #50h, UP go on increasing the value of accumulator until it reaches 50h
DOWN: ACALL DELAY call the delay function
SUBB A, #10h subtract 10h from accumulator
MOV P1, A send the value of accumulator to P1
CJNE A, #00h, UP go on decreasing the value of accumulator until it reaches 00h
SJMP UP go back to the start of the program
DELAY: MOV R0, #20d delay function: arbitrary values of R0, R1 & R2 are taken
ORG 0000h
RPT: MOV TMOD, #15h configure timer-1 as timer and timer-0 as counter
SETB P3.4 configure P3.4 pin as input
MOV TL0, #00h set 0 as higher byte of initial count of counter
MOV TH0, #00h set 0 as lower byte of initial count of counter
SETB TR0 start counter
MOV R0, #14d R0 is so set that timer-1 runs for 1 second
AGAIN: MOV TL1, #00h set 0 as higher byte of initial count of timer
MOV TH1, #00h set 0 as lower byte of initial count of timer
SETB TR1 start timer
BACK: JNB TF1, BACK go on until the timer overflows
CLR TF1 clear the timer overflow
CLR TR1 stop the timer