0% found this document useful (0 votes)
99 views

Fundamentals of Microcontroller & Its Application: Unit-Iv

This document provides information about interrupts in the 8051 microcontroller. It discusses the five types of interrupts, including the external, timer, and serial interrupts. It describes the interrupt structure and flags, as well as the interrupt enable register (IE) and interrupt priority register (IP) that are used to enable interrupts and set their priority levels. The document also covers programming external interrupts, including how to select the trigger type as either level-triggered or edge-triggered. An example is given of timer interrupt programming to blink an LED using a timer 0 interrupt service routine.

Uploaded by

Rajan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Fundamentals of Microcontroller & Its Application: Unit-Iv

This document provides information about interrupts in the 8051 microcontroller. It discusses the five types of interrupts, including the external, timer, and serial interrupts. It describes the interrupt structure and flags, as well as the interrupt enable register (IE) and interrupt priority register (IP) that are used to enable interrupts and set their priority levels. The document also covers programming external interrupts, including how to select the trigger type as either level-triggered or edge-triggered. An example is given of timer interrupt programming to blink an LED using a timer 0 interrupt service routine.

Uploaded by

Rajan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

2019 DEPARTMENT OF

Pattern ELECTRICAL ENGINEERING

Fundamentals of

CLASS NOTES
Microcontroller
& its Application

UNIT-IV
Mr. RAJAN PATEL
Asst. Prof.
Electrical Engg. Dept., GCOERC
Course Outcome: CO-4
Elaborate interrupt structure of 8051 and program to handle interrupt and ADC809.
Teaching Hours: 6
Content: -
Interrupt structure of 8051 and SFR associated with interrupts Programming of External
hardware interrupts in C. Interfacing of ADC 0809 with 8051.
 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 Interrupts in 8051 Microcontroller
 The 8051 microcontroller can recognize five different events that cause the main program
to interrupt from the normal execution. These five sources of interrupts in 8051are:
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
 The Timer and Serial interrupts are internally generated by the microcontroller, whereas
the external interrupts are generated by additional interfacing devices or switches that are
externally connected to the microcontroller.
 These external interrupts can be edge triggered or level triggered.
 When an interrupt occurs, the microcontroller executes the interrupt service routine so that
memory location corresponds to the interrupt that enables it.
 The Interrupt corresponding to the memory location is given in the interrupt vector table
below.
Interrupt Flag Interrupt vector address
Reset - 0000H
INT0 (Ext. int. 0) IE0 0003H
Timer 0 TF0 000BH
INT1 (Ext. int. 1) IE1 0013H
Timer 1 TF1 001BH
Serial TI/RI 0023H

Prepared By: - Mr. R. V. Patel 2


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

Bit 7 – EA: Enable All Bit


1 = Enable all interrupts
0 = Disable all interrupts

Prepared By: - Mr. R. V. Patel 3


Bit 6,5 – Reserved bits
Bit 4 – ES: Enable Serial Interrupt Bit
1 = Enable serial interrupt
0 = Disable serial interrupt
Bit 3 – ET1: Enable Timer1 Interrupt Bit
1 = Enable Timer1 interrupt
0 = Disable Timer1 interrupt
Bit 2 – EX1: Enable External1 Interrupt Bit
1 = Enable External1 interrupt
0 = Disable External1 interrupt
Bit 1 – ET0: Enable Timer0 Interrupt Bit
1 = Enable Timer0 interrupt
0 = Disable Timer0 interrupt
Bit 0 – EX0: Enable External0 Interrupt Bit
1 = Enable External0 interrupt
0 = Disable External0 interrupt
 Interrupt priority
 Priority to the interrupt can be assigned by using the interrupt priority register (IP)
 Interrupt priority after Reset:
Priority Interrupt source Intr. bit / flag
1 External Interrupt 0 INT0
2 Timer Interrupt 0 TF0
3 External Interrupt 1 INT1
4 Timer Interrupt 1 TF1
5 Serial interrupt (TI/RI)

 In the table, interrupts priorities upon reset are shown.


 As per 8051 interrupt priorities, the lowest priority interrupts are not served until the
microcontroller is finished with higher priority ones.
 In a case when two or more interrupts arrive microcontroller queues them according to
priority.
 IP Register: Interrupt priority register
 8051 has an interrupt priority register to assign priority to interrupts.

Prepared By: - Mr. R. V. Patel 4


Bit
7,6,5 – Reserved bits.
Bit 4 – PS: Serial Interrupt Priority Bit
1 = Assign a high priority to serial interrupt.
0 = Assign low priority to serial interrupt.
Bit 3 – PT1: Timer1 Interrupt Priority Bit
1 = Assign high priority to Timer1 interrupt.
0 = Assign low priority to Timer1 interrupt.
Bit 2 – PX1: External Interrupt 1 Priority Bit
1 = Assign high priority to External1 interrupt.
0 = Assign low priority to External1 interrupt.
Bit 1 – PT0: Timer0 Interrupt Priority Bit
1 = Assign high priority to Timer0 interrupt.
0 = Assign low priority to Timer0 interrupt.
Bit 0 – PX0: External0 Interrupt Priority Bit
1 = Assign high priority to External0 interrupt.
0 = Assign low priority to External0 interrupt.
 External interrupts in 8051
 8051 has two external interrupt INT0 and INT1.
 8051 controller can be interrupted by external Interrupt, by providing level or edge on
external interrupt pins PORT3.2, PORT3.3.
 External peripherals can interrupt the microcontroller through these external interrupts if
global and external interrupts are enabled.
 Then the microcontroller will execute current instruction and jump to the Interrupt Service
Routine (ISR) to serve to interrupt.
 In the polling, method the microcontroller has to continuously check for a pulse by
monitoring pin, whereas, in the interrupt method, the microcontroller does not need to poll.
Whenever an interrupt occurs microcontroller serves the interrupt request.
 External interrupt has two types of activation level
o Edge triggered (Interrupt occur on rising/falling edge detection)
o Level triggered (Interrupt occur on high/low-level detection)

Prepared By: - Mr. R. V. Patel 5


 In 8051, two types of activation levels are used. These are,
1. Low level triggered
Whenever a low level is detected on the INT0/INT1 pin while global and external interrupts
are enabled, the controller jumps to interrupt service routine (ISR) to serve interrupt.
2. Falling edge triggered
Whenever falling edge is detected on the INT0/INT1 pin while global and ext. interrupts are
enabled, the controller jumps to interrupt service routine (ISR) to serve interrupt.
 There are lower four flag bits in the TCON register required to select and monitor the
external interrupt type and ISR status.
 TCON: Timer/ counter Register

Bit 3- IE1:
External Interrupt 1 edge flag, set by hardware when interrupt on INT1 pin occurred and cleared
by hardware when interrupt get processed.
Bit 2- IT1:
This bit selects the external interrupt event type on INT1 pin,
1= sets interrupt on falling edge
0= sets interrupt on low level
Bit 1- IE0:
Interrupt0 edge flag, set by hardware when interrupt on INT0 pin occurred and cleared by
hardware when an interrupt is processed.
Bit 0 - IT0:
This bit selects the external interrupt event type on the INT0 pin.
1= sets interrupt on falling edge
0= sets interrupt on low level
Interrupt programming in 8051
 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.

Prepared By: - Mr. R. V. Patel 6


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.
Example:- Let's see the timer interrupt programming using Timer0 model for blinking LED
using interrupt method:
#include< reg51 .h>
sbit Blink Led = P2^0; // LED is connected to port 2 Zeroth pin
void timer0_ISR (void) interrupt 1 //interrupt no. 1 for Timer0
{
Blink Led=~Blink Led; // Blink LED on interrupt
TH0=0xFC; // loading initial values to timer
TL0=0x66;
}
void main()
{
TMOD=0x0l; // mode 1 of Timer0
TH0 = 0xFC: // initial value is loaded to timer
TL0 = 0x66:
ET0 =1; // enable timer 0 interrupt
TR0 = 1; // start timer
while (1); // do nothing
}
 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.
Example:- Consider the edge triggered external hardware interrupt programming is:-
void main()
{

Prepared By: - Mr. R. V. Patel 7


IT0 = 1; // Configure interrupt 0 for falling edge on INT0
EXO = 1; // Enabling the EX0 interrupt
EA =1; // Enabling the global interrupt flag
}
void ISR_ex0(void) interrupt 0
{
<body of interrupt>
}
 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.
Example: - Let's see the program for sending 'E' through serial port with 9600 baud rate
using Serial Interrupt:
void main()
{
TMOD = 0x20:
TH1= 0xFD; // baud rate for 9600 bps
SCON = 0x50;
TR1=1;
EA=l;
whlle(l);
}
void ISR_Serial(void) interrupt 4
{
if(TI==l)
{
SBUF= ?E?;
TI=0;
}
else
RI =0;
}

Prepared By: - Mr. R. V. Patel 8


Example: - Let’s program the external interrupt of AT89C51 such that, when falling edge is
detected on the INT0 pin then the microcontroller will toggle the P1.0 pin.
Programming steps
Enable global interrupt i.e. EA = 1
Enable external interrupt i.e. EX0 = 1
Enable interrupt trigger mode i.e. whether interrupt is edge triggered or level triggered, here we
will use falling edge trigger interrupt, so make IT0 = 1.
Program
#include <reg51.h> /* Include x51 header file */
sbit LED = P1^0; /* set LED on port1 */
void Ext_int_Init()
{
EA = 1; /* Enable global interrupt */
EX0 = 1; /* Enable Ext. interrupt0 */
IT0 = 1; /* Select Ext. interrupt0 on falling edge */
}
void External0_ISR() interrupt 0
{
LED = ~LED; /* Toggle pin on falling edge on INT0 pin */
}
void main()
{
Ext_int_Init(); /* Call Ext. interrupt initialize */
while(1);
}
 ACD 0808/0809
 ADC0808 is an 8 bit analog to digital converter with eight input analog channels, i.e., it can
take eight different analog inputs.
 The input which is to be converted to digital form can be selected by using three address
lines.
 The voltage reference can be set using the Vref+ and Vref- pins.
 The step size is decided based on set reference value. Step size is the change in analog input
to cause a unit change in the output of ADC.
 The default step size is 19.53mV corresponding to 5V reference voltage.
 ADC0808 needs an external clock to operate unlike ADC0804 which has an internal clock.
 The ADC needs some specific control signals for its operations like start conversion and bring
data to output pins.

Prepared By: - Mr. R. V. Patel 9


 When the conversion is complete the EOC pins goes low to indicate the end of conversion
and data ready to be picked up.
 Features
o Easy interface to all microprocessors
o Operates ratio metrically or with 5 V DC or analog span adjusted voltage reference
o No zero or full-scale adjust required
o 8-channel multiplexer with address logic
o 0V to 5V input range with single 5V power supply
o Outputs meet TTL voltage level specifications
o Standard hermetic or molded 28-pin DIP package
o 28-pin molded chip carrier package

 Selection of Channel
 We can select the any input channel by using the Address lines ADD A, ADD B and ADD C).
 We can select the input line IN0 by keeping all three address lines (ADD A, ADD B and ADD
C) Low.
 If we want to select input channel IN4 then we need to keep ADD A, ADD B low and ADD C
high.
 For selecting all the other input channels, have a look on the given table:

Prepared By: - Mr. R. V. Patel 10


ADC Channel Name ADD C ADD B ADD A
IN0 LOW LOW LOW
IN1 LOW LOW HIGH
IN2 LOW HIGH LOW
IN3 LOW HIGH HIGH
IN4 HIGH LOW LOW
IN5 HIGH LOW HIGH
IN6 HIGH HIGH LOW
IN7 HIGH HIGH HIGH
 Calculating Step Size
 ADC 0808 is an 8 bit ADC i.e. it divides the voltage applied at Vref+ & Vref- into 28 i.e. 256
steps.
Step Size = (Vref+ - Vref-)/256
 Suppose Vref+ is connected to Vcc i.e. 5V & Vref- is connected to the Gnd then the step size
will be
Step size= (5 - 0)/256= 19.53 mv
 How to use the ADC0808?
 Start
 Select the channel.
 A Low – High transition on ALE to latch in the address.
 A Low – High transition on Start to reset the ADC’s SAR.
 A High – Low transition on ALE.
 A High – Low transition on start to start the conversion.
 Wait for End of cycle (EOC) pin to become high.
 Make Output Enable pin High.
 Take Data from the ADC’s output
 Make Output Enable pin Low.
 Stop
 ADC0808 interfacing with 8051
 Circuit Diagram

Prepared By: - Mr. R. V. Patel 11


LCD:
RS – P1.0
RW – GND
EN – P1.1
LCD Data Lines – P1 LSB 4 bits
ADC:
Output Enable – P2.0
EOC – P2.1
Start – P2.2
Addr A – P2.4
Addr B – P2.5
Addr C – P2.6
ALE – P2.7
ADC Data lines – Po
Code
#include<reg51.h>
#define adc_data P0
#define lcd P1
unsigned char temp, gas;
sbit rs=P1^0;
sbit en=P1^1;
sbit OE =P2^0;
sbit EOC =P2^1;
sbit START =P2^2;
sbit AA =P2^4;
sbit BB =P2^5;
sbit CC =P2^6;

Prepared By: - Mr. R. V. Patel 12


sbit ALE =P2^7;
unsigned char adc_val;
void lcd_init();
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void lcd_delay();
unsigned char adc(unsigned int ch);
void ch_sel(unsigned int sel);

void main()
{
lcd_init();
show("TEMP: ");
cmd(0xc0);
show("GAS : ");
while(1) {
cmd(0x86);
temp=adc(0); //Reading Value from ADC Channel 0
dat((temp/100)%10 +0x30);
dat((temp/10)%10 +0x30);
dat(temp%10 +0x30);
cmd(0xc6);
gas=adc(1); //Reading Value from ADC Channel 1
dat((gas/100)%10 +0x30);
dat((gas/10)%10 +0x30);
dat(gas%10 +0x30);
}
}

void lcd_init()
{
cmd(0x02);
cmd(0x28);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
}

void cmd(unsigned char a)


{
rs=0;
lcd&=0x0F;

Prepared By: - Mr. R. V. Patel 13


P1|=(a&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
lcd&=0x0f;
lcd|=(a<<4&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
}

void dat(unsigned char b)


{
rs=1;
lcd&=0x0F;
lcd|=(b&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
lcd&=0x0f;
lcd|=(b<<4&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
}

void show(unsigned char *s)


{
while(*s) {
dat(*s++);
}
}

void lcd_delay()
{
unsigned int lcd_delay;
for(lcd_delay=0;lcd_delay<=1000;lcd_delay++);
}

Prepared By: - Mr. R. V. Patel 14


unsigned char adc(unsigned int ch)
{
adc_data=0xff;
ALE=START=OE=AA=BB=CC=0;
EOC=1;
ch_sel(ch);
ALE=1;
START=1;
ALE=0;
START=0;
while(EOC==1);
while(EOC==0);
OE=1;
adc_val=adc_data;
OE=0;
return adc_val;
}

void ch_sel(unsigned int sel)


{
switch(sel) {
case 0: CC=0;BB=0;AA=0; break; //000
case 1: CC=0;BB=0;AA=1; break; //001
case 2: CC=0;BB=1;AA=0; break; //010
case 3: CC=0;BB=1;AA=1; break; //011
case 4: CC=1;BB=0;AA=0; break; //100
case 5: CC=1;BB=0;AA=1; break; //101
case 6: CC=1;BB=1;AA=0; break; //110
case 7: CC=1;BB=1;AA=1; break; //111
}
}

Prepared By: - Mr. R. V. Patel 15

You might also like