Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

8051 Microcontroller

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

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.

The 8051-microcontroller programming is performed in embedded C language using


Keil software.

Features of 8051 Microcontroller:


Let's see the common feature of 8051 microcontroller:

o It having four register banks


o 64K bytes on-chip programmable memory (ROM)
o 128 bytes on-chip data memory (RAM)
o Address bus is 16-bit unidirectional
o Data bus is 8-bit bidirectional
o 128 user defined flags
o 16 bit timers
o 32 general purpose registers each of 8-bit
o 8051 microcontroller offers a number of special features such as ADC, UARTs,
Op-amp, etc.

Application of 8051 Microcontroller


The 8051 microcontroller applications include a large amount of machines because it is
used for incorporating inside a project or to assemble a machine using it.

Let's see the major applications of 8051 Microcontroller:

1. Energy Management : In energy management system the measuring device is


used for calculating the energy consumption in industrialized and domestic
applications. These systems are manufactured by integrating the microcontrollers
inside their architecture configuration.
2. Automobiles : Microcontroller 8051 is to be used for providing automobile
solutions. They are largely be used in hybrid motor vehicles to control engine
variations.
3. Touch screens: The advanced degree of microcontroller integrate the touch
sensing ability within their design .Transportable devices such as cell phones,
media players and gaming devices are some example of microcontroller
integrated with touch screens.
4. Medical Devices: Microcontroller is used in various medical devices such as
glucose and blood pressure measurement machine for monitoring and
measuring the exact result in real-time computational environment.

8051 Microcontroller Architecture


Let's see the internal architecture of 8051 Microcontroller represented in form of block
diagram as shown below:
Basic components present internally inside 8051 Microcontroller architecture are:

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.

Types of interrupt in 8051 Microcontroller:

Let's see the five sources of interrupts in 8051 Microcontroller:

o Timer 0 overflow interrupt - TF0


o Timer 1 overflow interrupt - TF1
o External hardware interrupt - INT0
o External hardware interrupt - INT1
o Serial communication interrupt - RI/TI

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.

Types of buses in 8051 Microcontroller:

Let's see the two types of bus used in 8051 microcontroller:

o Address Bus: 8051 microcontrollers is consisting of 16 bit address bus. It is


generally be used for transferring the data from Central Processing Unit to
Memory.
o Data bus: 8051 microcontroller is consisting of 8 bits data bus. It is generally be
used for transferring the data from one peripherals position to other peripherals.

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.

8051 Microcontroller Pin Diagram


Let's see the 40 pin Dual Inline Package (DIP) of microcontroller integrated circuit with
externally connected power supply and oscillator.
Consider the brief description of pin configuration inside microcontroller is:

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.

Embedded System I/O Programming


In 8051 Microcontroller, I/O operations are performed by using four ports and 40 pins.
I/O operation port uses 32 pins with each port has 8 pins. The remaining 8-pins are used
for providing

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.

I/O Ports and their Functions:


In microcontroller there are four Input/output ports P0, P1, P2, and P3, each port is 8-bit
port having 8 pins each. During RESET, all the ports are used as input ports. When the
port gets first 0, then it becomes an output port. For reconfigure it as an input, the high
signal (1) must be sent to a port.

8.2M
165
History of Java

Port 0 (Pin No 32 - Pin No 39):


Port 0 contains 8 pins. It can be used as input or output. In general we connect P0 with
10K-ohm pull-up resistors for using it as an input or output port being an open drain.

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.

Address Latch Enable (ALE) Pin no 31 indicates if PO is used as address or data.

When ALE=0 then it provides data D0-D7.

When ALE=1 then it provides address A0-A7.

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:-

1. MOV A, #FFH; (comments: A=FFH i.e. A=1111 1111)


2. MOV P0, A; (Port 0 all pin have 1's so that it work as Input)

Port 1 (Pin No 1-Pin No 8):

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:-

1. MOV A, #FFH ; Value of Accumulator register A=FF hex


2. MOV P1, A ; Port 1 worked as an input port
3. MOV A, P1 ; get data from P1
4. MOV R5, A ; save it in Register R5
5. ACALL DELAY ; wait
6. MOV A, P1 ; get another data from P1
7. MOV R6, A ; save it in R6
8. ACALL DELAY ; wait
9. MOV A, P1 ; get data from P1
10. MOV R7, A ; save it in R7

Port 2 (Pin No 21-Pin No 28):

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:-

1. MOV A, #FFH ; Value of Accumulator register A=FF hex


2. MOV P2, A ; Port 2 worked as an input port
3. BACK:
4. MOV A, P1 ; get data from P2
5. MOV P1, A ; save it in Port 1
6. SJMP BACK ; Keep doing that

Port 3 (Pin No 10-Pin No 17):

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.5 and P3.4 are used as timers T1 and T0 respectively.

P3.6 and P3.7 are Write (WR) and Read (RD) pins.

Let's see the Port 3 table showing an individual pin function:

Pin Function P3 Bit

10 Receiver (RxD) P3.0

11 Transmitter (TxD) P3.1<

12 Complement of INT 0 P3.2<

13 INT 1 P3.3<

14 Timer 0(TO) P3.4<

15 Timer 1(T1) P3.5<

16 Write (WR) P3.6<

17 Complement of read (RD) P3.7<

I/O Ports and Bit Addressability:


It is a mostly used feature of 8051 while writing code for 8051. Sometimes there is a
need to use only 1 or 2 bits of the port instead of using entire 8-bits. 8051
microcontroller provides the feature to use each bit of the ports.

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.

For example: - "SETB P1.3" sets high bit 3 of port 1.

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

Single Bit Instruction:

Instruction Function

CLR bit Clear the bit (bit=0)

CPL bit Complement the bit (bit=NOT bit)

SETB bit Set the bit (bit = 1)

JB bit, target Jump to target if bit = 1 (jump if bit)

JBC bit, target Jump to target if bit = 1, clear bit (jump if bit, then clear)

JNB bit, target Jump to target if bit = 0 (jump if no bit)

Addressing Modes of 8085


Microcontroller
Addressing Modes are an instruction set used for communicating with the central
processing unit (CPU) for performing any specific task.

Generally the following addressing modes are used for executing the instruction in
8085 microcontroller:-

o Immediate addressing mode


o Indexed addressing mode
o Direct addressing mode
o Register direct addressing mode
o Register indirect addressing mode

The detailed explanations of each addressing mode are given below:

Immediate Addressing Mode


Let's start with an example.

1. MOV A, #4AH

In simple, we can write,

1. MOV A, #data

It is known as immediate because 8-bit data is send immediately to the accumulator


(destination operand).

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.

Direct Addressing Mode


Direct addressing mode is used for addressing an operand. Here, an address of the data
(source data) is present as an operand. Let's consider an example.
1. MOV A, 07H

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:-

As shown in an above illustration, it is a 2-byte instruction which requires 1 cycle to


complete.

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.

Register Direct Addressing Mode:


Inside a register direct addressing mode, we use the register name directly (as source
operand).

Let us consider an example.

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.

Now, consider the following illustration. It represents how instruction is executed:-


Data (2F) movement is shown in bold. 2F is become transferred to the accumulator from
data memory location 0CH and it is shown by dotted line. 0CH is an address location of
Register 4 of register bank #1. The instruction shown above is of 1 byte and it requires 1
cycle for complete execution. It means that we can save memory by using direct
addressing mode.

Register Indirect Addressing Mode


In register indirect addressing mode, the address of data is stored inside the register as
an operand.
1. MOV A, @R0

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

8051 Instruction Set


An instruction is a command given to the microcontroller for performing a specified
operation on presented data. The instruction set of microcontroller is a collection of
instructions that the microcontroller is designed to execute.

Data moving or handling Instructions:

Mnemonics Operational descriptions Addressing No. of No. of


modes cycles bytes
used occupied

Mov a,#num This instruction Copy the immediate immediate 1 2


data number in to acc

Mov Rx,a This instruction Copy the data from acc register 1 1
to Rx

Mov a,Rx This instruction Copy the data from Rx register 1 1


to acc

Mov This instruction Copy the immediate immediate 1 2


Rx,#num data num in to Rx

Mov a,add This instruction Copy the data from direct 1 2


direct address location add to acc

Mov add,a This instruction Copy the data from acc direct 1 2
to direct address add

Mov This instruction Copy the immediate direct 2 3


add,#num data num in to direct address
Mov This instruction Copy the data from direct 2 3
add1,add2 add2 to add1

Mov Rx,add This instruction Copy the data from direct 2 2


direct

Mov add,Rx This instruction Copy the data from Rx direct 2 2


to direct address add

Mov @Rp,a This instruction Copy the data in acc to Indirect 1 2


address in Rp

Mov a,@Rp This instruction Copy the data that is at Indirect 1 1


address in Rp to acc

Mov This instruction Copy the data that is at Indirect 2 2


add,@Rp address in Rp to add

Mov This instruction Copy the data in add Indirect 2 2


@Rp,add to address in Rp

Mov This instruction Copy the immediate Indirect 1 2


@Rp,#num byte num to the address in Rp

Movx a,@Rp This instruction Copy the content of Indirect 2 1


external add in Rp to acc

Movx This instruction Copy the content of Indirect 2 1


a,@DPTR external add in DPTR to acc

Movx @Rp,a This instruction Copy the content of Indirect 2 1


acc to the external add in Rp

Movx This instruction Copy the content of Indirect 2 1


@DPTR,a acc to the external add in DPTR

Movc In this the address of instruction is indirect 2 1


a,@a+DPTR formed by adding acc and DPTR and
their content is copied to acc

Movc a, In this the address of instruction is indirect 2 1


@a+PC formed by adding acc and PC and its
content is copied to acc

Push add In this instruction Increment Stack Direct 2 2


Pointer (SP) and copy the data from
source add to internal RAM address
contained in SP

Pop add This instruction copy the data from an direct 2 2


internal RAM address contained in SP
to destination add and decrement SP

Xch a, Rx This instruction Exchange the data Register 1 1


between acc and Rx

Xch a, add This instruction Exchange the data Direct 1 2


between acc and given add

Xch a,@Rp This instruction Exchange the data Indirect 1 1


between acc and address in Rp

Xchd a, @Rp This instruction Exchange only lower indirect 1 1


nibble of acc and address in Rp

Loop and Jump Instructions:


Looping operation in the 8051:

On repeating a sequence of instructions a certain number of times will result in the


formation of loop. The looping operation is used for running the same set of subroutine
inside a program number of times as per the requirement.
Consider the instruction DJNZ register; label is used for performing a loop operation.
In this instruction, the register is decremented by 1; if this is not zero, then 8051 jumps
to the target address referred by the label.

Example: Multiply 15 by 10 using the technique of a repeated addition

Solution: Multiplication can be performed by adding the multiplicand repeatedly, as


many times as that of multiplier.

For example:

1. MOV A,#0 ;A = 0,clean ACC


2. MOV R1,#10 ; the multiplier is replaced in R1
3. Add A,#15 ;add the multiplicand to the ACC
4.
5. AGAIN: DJNZ R1,
6. AGAIN: repeat until R1 = 0 (10 times)
7.
8. MOV R5 , A ;save A in R5 ;R5 (96H)
Conditional Jump Instruction:

Consider the below table lists the conditional jumps instruction used in 8051

Instructions Action

JC Jump if CY = 1

JNC Jump if CY ≠ 1

JNB Jump if bit = 0

JB Jump if bit = 1

JZ Jump if A = 0

DJNZ Decrement and Jump if register ≠ 0

JNZ Jump if A ≠ 0

CJNE A, data Jump if A ≠ data

CJNE reg, #data Jump if byte ≠ data

JBC Jump if bit = 1 and clear bit

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:

OOPs Concepts in Java


o Editor Program : At first, we use an editor for type in a program. Editors like MS-
DOS program that comes with all Microsoft operating systems can be used for
creating or edit a program. The editor produces an ASCII file. The ?asm?
extension for a source file is used by an assembler during next step.
o Assembler Program: The "asm" source file contain the code created in Step 1. It
is transferred to an 8051 assembler. The assembler is used for converting the
assembly language instructions into machine code instructions and it produced
the .obj file (object file) and .lst file (list file). It is also called as source file
because some assembler requires that this file must have "src" extension.
o Linker Program: The linker program is used for generating one or more object
files and produces an absolute object file with an extension "abs".
o OH Program: The OH program fetches the "abs" file and fed it to a program
called "OH". OH is called as object to hex converter it creates a file with an
extension "hex" that is ready for burn in to the ROM.

Labels in assembly Language


All labels used in assembly language follow the certain rules as given below:

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 in 8051 Microcontroller


It is a sub-routine calls that given by the microcontroller when some other program with
high priority is request for acquiring the system buses than interrupt 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.

Types of interrupt in 8051 Microcontroller


Let's see the five sources of interrupts in 8051 Microcontroller:

o Timer 0 overflow interrupt - TF0


o External hardware interrupt - INT0
o Timer 1 overflow interrupt - TF1
o External hardware interrupt - INT1
o Serial communication interrupt - RI/TI

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.

Java Try Catch

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.

Interrupt Structure of 8051 Microcontroller


After 'RESET' all the interrupts get disabled, and therefore, all the interrupts is enabled
by software. From all the five interrupts, if anyone or all interrupt are activated, this will
sets the corresponding interrupt flags as represent in the figure which corresponds to
Interrupt structure of 8051 microcontroller:-
All the interrupts can be set or cleared by some special function register that is also
known as interrupt enabled (IE), and it is totally depends on the priority, which is
executed by using interrupt priority register.

Interrupt Enable (IE) Register


IE register is used for enabling and disabling the interrupt. This is a bit addressable
register in which EA value must be set to one for enabling interrupts. The individual bits
in this register enables the particular interrupt like timer, serial and external inputs.
Consider in the below IE register, bit corresponds to 1 activate the interrupt and 0
disable the interrupt.
Interrupt Priority Register (IP)
Using IP register it is possible to change the priority levels of an interrupts by clearing or
setting the individual bit in the Interrupt priority (IP) register as shown in figure. It allows
the low priority interrupt can interrupt the high-priority interrupt, but it prohibits the
interruption by using another low-priority interrupt. If the priorities of interrupt are not
programmed, then microcontroller executes the instruction in a predefined manner and
its order are INT0, TF0, INT1, TF1, and SI.
Interrupt programming in 8051
1. Timer Interrupt Programming: In microcontroller Timer 1 and Timer 0
interrupts are generated by time register bits TF0 AND TF1. This timer interrupts
programming by C code involves:
o Selecting the configuration of TMOD register and their mode of operation.
o Enables the IE registers and corresponding timer bits in it.
o Choose and load the initial values of TLx and THx by using appropriate
mode of operation.
o Set the timer run bit for starting the timer.
o Write the subroutine for a timer and clears the value of TRx at the end of
the subroutine.

Let's see the timer interrupt programming using Timer0 model for blinking
LED using interrupt method:

1. #include< reg51 .h>


2. sbit Blink Led = P2^0; // LED is connected to port 2 Zeroth pin
3. void timer0_ISR (void) interrupt 1 //interrupt no. 1 for Timer0
4. {
5. Blink Led=~Blink Led; // Blink LED on interrupt
6. TH0=0xFC; // loading initial values to timer
7. TL0=0x66;
8. }
9. void main()
10. {
11. TMOD=0x0l; // mode 1 of Timer0
12. TH0 = 0xFC: // initial value is loaded to timer
13. TL0 = 0x66:
14. ET0 =1; // enable timer 0 interrupt
15. TR0 = 1; // start timer
16. while (1); // do nothing
17. }

2. External Hardware Interrupt Programming

Microcontroller 8051 is consisting of two external hardware interrupts: INT0 and


INT1 as discussed above. These interrupts are enabled at pin 3.2 and pin 3.3. It
can be level triggered or edge triggered. In level triggering, low signal at pin 3.2
enables the interrupt, while at pin 3.2 high to low transition enables the edge
triggered interrupt.

Let us see the programmable feature of 8051 microcontroller are:

o Enables the equivalent bit of external interrupt in Interrupt Enable (IE)


register.
o If it is level triggering, then write subroutine appropriate to this interrupt,
or else enable the bit in TCON register corresponding to the edge
triggered interrupt.

Consider the edge triggered external hardware interrupt programming is:-

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. }

2. Serial Communication Interrupt Programming It is used when there is a need


to send or receive data. Since one interrupt bit is used for both Transfer Interrupt
(TI) and Receiver Interrupt (RI) flags, Interrupt Service Routine (ISR) must examine
these flags for knowing the actual interrupt. By the logical OR operation of RI and
TI flags causes the interrupt and it is clear by the software alone. Consider the
steps involved in serial communication interrupt programming are:-

o Configure the Interrupt Enable register for enabling serial interrupt.


o Configure the SCON register for performing transferring and receiving
operation.
o Write a subroutine for given interrupt with appropriate function.

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. }

You might also like