8051 Microcontroller
8051 Microcontroller
8051 Microcontroller
The 8051 microcontroller was invented in 1980's by Intel. Its foundation is based on
Harvard architecture and this microcontroller was developed principally for bringing it
to be used in Embedded Systems.
At first it was created by using NMOS technology but the use of NMOS consumed more
power to work therefore Intel re-launch the microcontroller 8051 using CMOS
technology and new edition came up with edition of letter 'C' in the title name,
therefore the new modified version of microcontroller is called by name 80C51.
CPU (Central Processing Unit): CPU act as a mind of any processing machine. It
synchronizes and manages all processes that are carried out in microcontroller. User has
no power to control the functioning of CPU. It interprets the program stored in ROM
and carries out from storage and then performs it projected duty. CPU manage the
different types of registers available in 8051 microcontroller.
Interrupts: Interrupts is a sub-routine call that given by the microcontroller when some
other program with high priority is request for acquiring the system buses the n
interrupts occur in current running program.
Interrupts provide a method to postpone or delay the current process, performs a sub-
routine task and then restart the standard program again.
Memory: For operation Micro-controller required a program. This program guides the
microcontroller to perform the specific tasks. This program installed in microcontroller
required some on chip memory for the storage of the program.
Microcontroller also required memory for storage of data and operands for the short
duration. In microcontroller 8051 there is code or program memory of 4 KB that is it has
4 KB ROM and it also comprise of data memory (RAM) of 128 bytes.
Bus : Bus is a group of wires which uses as a communication canal or acts as means of
data transfer. The different bus configuration includes 8, 16 or more cables. Therefore, a
bus can bear 8 bits, 16 bits all together.
Oscillator: As the microcontroller is digital circuit therefore it needs timer for their
operation. To perform timer operation inside microcontroller it required externally
connected or on-chip oscillator. Microcontroller is used inside an embedded system for
managing the function of devices. Therefore, 8051 uses the two 16 bit counters and
timers. For the operation of this timers and counters the oscillator is used inside
microcontroller.
Pins 1-8: These pins belongs to Port 1 of microcontroller. Port 1 is used as domestically
pulled up, quasi bi directional input/output port.
Pin 9: It is a RESET pin which is utilized to set the microcontroller 8051 to its primary
value. During the beginning of an application the RESET pin is to be set elevated for two
machine rotations.
Pins 10-17: These pins belong to Port 3 of microcontroller. Port 3 can be used for
number of functions such as timer input, interrupts, serial communication indicator for
transmitting (TxD) and receiving (RxD). It is also known as domestic pull up port with
quasi bi direction port embedded within.
Pins 18 and 19: These pins are generally be used for interfacing outer crystal oscillator
with given system clock.
Pin 20: This pin titled as Vss. It symbolizes ground voltage or 0 V is connected to this pin
of microcontroller.
Pin 21-28: These pins belong to port 2 of microcontroller. Port 2 can be used as
Input/output port, senior order address bus are multiplexed with this quasi bi directional
port.
Pin 29: This pin belongs to Program Store Enable or PSEN. It is used for interpreting the
sign from outer program memory.
Pin 30: This pin belongs to External Access or EA input is used for permit or prohibits
outer memory interfacing. If there is no outer memory need, this pin is set to high by
linking it with supply voltage .
Pin 31: This pin belongs to Address Latch Enable or ALE is used for de-multiplexing the
address data indication of port 0 for outer memory interfacing.
Pin 32-39: These pins belong to Port 0 of the microcontroller. Port 0 can be used as
input/output port, lower order address and data bus signals are multiplexed with this
port. This pin act as bi directional Input/output port and outer connected pull up
resistors are necessary for utilizing these ports as Input/output.
Pin 40: This pin is used to provide power supply to the circuit.
Let's see the 40-Pin Plastic Dual Inline Package (PDIP) integrated circuit of
microcontroller:
Note - In DIP structure of microcontroller we can recognize the first and last pin by
using the cut present at middle of IC this cut is called as Notch of microcontroller. The
first pin is present at left side of notch and last pin is present at right side of notch.
8.2M
165
History of Java
It is also referred as AD0-AD7, which allowing it to be used as both address and data
transfer port. When we want to excess the external ROM, then P0 is used as both Data
and Address Bus.
Let's see the structure of port 0 with externally connected pull up resistor:
Let's see an assembly language code for making the Port 0 to be worked as an input:-
It is also an 8-bit Port and can be worked as either input or output. It doesn't require
external connected pull-up resistors because they are already present internally. Upon
reset, Port 1 worked as an input port.
If port 1 is configured as an output port, then to use port 1 as input port again, we write
1 to all bits of port 1 as shown in the below code:-
Port 2 uses a total of 8 pins and it can also be used as input and output operation. Same
as Port 1, P2 also not require external pull- up resistors. Port 2 can be used along with
P0 to provide 16-bit address for an external memory. Therefore it is designated as (A0-
A7), as shown in pin diagram.
If Port 2 is configured as an output port, then for using it as an input port again we write
1 to all bits of port 2 as shown in the below code:-
Port 3 is also of 8 bits and it can be used as Input/output. This port provides some
important signals.
P3.1 and P3.0 are RxD (Receiver) and TxD (Transmitter) respectively and it is collectively
used for serial communication.
P3.3 and P3.2 pins are used as external interrupts.
P3.6 and P3.7 are Write (WR) and Read (RD) pins.
13 INT 1 P3.3<
While using a port in a single-bit manner, we provide the syntax "SETB X.Y", where X is
the port number varies from 0 to 3, and Y is a bit number varies from 0 to 7.
Let's see an assembly code for toggling the bit of P1.5 continuously:
1. AGAIN:
2. SETB P1.5
3. ACALL DELAY
4. CLR P1.5
5. ACALL DELAY
6. SJMP AGAIN
Instruction Function
JBC bit, target Jump to target if bit = 1, clear bit (jump if bit, then clear)
Generally the following addressing modes are used for executing the instruction in
8085 microcontroller:-
1. MOV A, #4AH
1. MOV A, #data
Consider the following illustration describes the above set of instructions and their
execution. The opcode 74H is saved at 0202 address. The data 4AH is saved at 0203
address inside a program memory. After reading an opcode 74H, the data at the next
memory address is copied to accumulator A (E0H is the address of accumulator). Since
an instruction is of 2-bytes and it is executed in single cycle, the program counter will
increment by 2 and will point to 0204 address of the program memory.
Indexed Addressing Mode
Let us consider two examples for understanding the concept of indexed addressing
mode. Take a look at the below instructions:
1. MOVC A, @A+DPTR
2. MOVC A, @A+PC
Here PC is the program counter and DPTR is the data pointer (both are 16-bit registers).
Consider the first example.
1. MOVC A, @A+DPTR
In this the source operand is @A+DPTR. It has a source data from that place. Here the
contents of DPTR are added with the current content of the accumulator. This addition
provides a new address that is the address of the source data. The data pointed by this
address is further transferred to the accumulator.
Consider the opcode is 93H. The DPTR having the value of 01FE, here 01 is located in
DPH side (higher 8 bits) and FE is located in DPL side (lower 8 bits). Consider
Accumulator has the value 02H. After that a 16-bit addition is done and 01FE H+02H
result in 0200 H. Data present at the location 0200H will become transferred to the
accumulator. The previous value of the accumulator (02H) will be replaced by the new
data from 0200H. The new data inside the accumulator is highlighted in the illustration.
The register bank#0 (7th register) has the address 07H. When the MOV instruction is
executed, the data stored in register 07H is moved inside an accumulator. As the
register 07H holding the data 1FH, therefore 1FH is moved to the accumulator.
Note - we are not using '#' in direct addressing mode, as that used in immediate mode
because If we had used '#', the data value 07H is transferred to the accumulator instead
of 1FH.
Now, consider the following illustration. It shows how the instruction is executed:-
The program counter will be incremented by 2 and will point to 0204 memory location.
The opcode for an instruction MOV A, address of operation is E5H. Whenever the
instruction at 0202 is run (E5H), the accumulator will become active and ready to receive
data. Then the PC jumps to the next address at 0203 and look up the address of the
location 04H where the source data (i.e. transferred to accumulator) is located. At 04H,
the controller finds the data 1FH and transfers this to the accumulator and hence the
execution of instruction is completed.
1. MOV A, R4
At a time, the registers are able to take values from R0 to R7. There are 32 such registers
are present in microcontroller. In order to use these 32 registers with just 8 variables for
addressing the registers, register banks are used.
At one time, a single register bank is to be selected. Selection of a register bank is made
possible by using a Special Function Register (SFR) it named as Processor
Status Word (PSW). PSW is an 8-bit SFR where each bit can be programmed as
required. Bits are assigned from PSW.0 to PSW.7.
Here the value present inside R0 is to be considered as an address, it is used for holding
the data to be transferred inside an accumulator. Example: If register R0 has the value
20H, and data 2FH is stored at the address location of 20H, then the value of 2FH will
become transferred to the accumulator after executing the above instruction.
Now, consider the following illustration. It shows how the instruction is executed:-
Consider the opcode for MOV A, @R0 is E6H. Program control moves to address
location 20H where it locates the data 2FH and then transfer 2FH inside an accumulator.
It is a 1-byte instruction and the program counter (PC) is increments by 1 and moves to
0203 location of the program memory.
Note : Only the registers R0 and R1 are allowed to be used as a register indirect
addressing instruction. Therefore, the programmer can develop an instruction either
using @R0 or @R1. All other register banks are not allowed.
Next →← Prev
Mov Rx,a This instruction Copy the data from acc register 1 1
to Rx
Mov add,a This instruction Copy the data from acc direct 1 2
to direct address add
For example:
Consider the below table lists the conditional jumps instruction used in 8051
Instructions Action
JC Jump if CY = 1
JNC Jump if CY ≠ 1
JB Jump if bit = 1
JZ Jump if A = 0
JNZ Jump if A ≠ 0
Assembly Language
Assembly language is introduced for providing mnemonics or symbols for the machine
level code instructions. Assembly language program is consisting of mnemonics that is
translated into machine code. A program that is used for this conversion is known
as assembler.
Assembly language is also called as low-level language because it directly works with
the internal structure of CPU. For programming in assembly language, a programmer
must have the knowledge of all the registers in a CPU.
Different programming languages like C, C++, Java and various other languages are
called as high-level languages because they are not dealing with the internal details of
CPU.
Assembling and Running of 8051 Program
Let's see the steps for creating, assembling and running an assembly language program
are as follows:
o Each label name should be unique. The name used as label in assembly language
programming consist of alphabetic letters in both lowercase and uppercase,
numbers from 0 to 9, and special characters such as at the rate (@), question
mark (?), underscore(_), and dollar ($) etc.
o Reserved words are not allowed to be used as a label in the program. For
example, MOV and ADD words are reserved words.
o The first character must be an alphabetical character, it cannot be a number.
Interrupts provide a method to postpone or delay the current process, performs a sub-
routine task and then restart the standard program again.
The timer and serial interrupts are internally produced by the microcontroller, whereas
the external interrupts are produced by additional interfacing devices or switches that
are externally connected with the microcontroller. These external interrupts can be level
triggered or edge triggered.
When interrupt occur then the microcontroller executes the interrupt service routine.
Therefore the memory location corresponds to interrupt enables it. Consider the
interrupt corresponding to the memory location is shown in the interrupt vector table
below.
Let's see the timer interrupt programming using Timer0 model for blinking
LED using interrupt method:
1. void main()
2. {
3. IT0 = 1; // Configure interrupt 0 for falling edge on INT0
4. EXO = 1; // Enabling the EX0 interrupt
5. EA =1; // Enabling the global interrupt flag
6. }
7. void ISR_ex0(void) interrupt 0
8. {
9. <body of interrupt>
10. }
Let's see the program for sending 'E' through serial port with 9600 baud
rate using Serial Interrupt:
1. void main()
2. {
3. TMOD = 0x20:
4. TH1= 0xFD; // baud rate for 9600 bps
5. SCON = 0x50;
6. TR1=1;
7. EA=l;
8. whlle(l);
9. }
10. void ISR_Serial(void) interrupt 4
11. {
12. if(TI==l)
13. {
14. SBUF= ?E?;
15. TI=0;
16. }
17. else
18. RI =0;
19. }