0% found this document useful (0 votes)
14 views29 pages

Unit 4

This document covers interrupt programming and interfacing for microcontrollers, specifically focusing on the 8051 architecture. It includes topics such as polling vs. interrupts, ADC/DAC interfacing, and practical applications like home security systems and robotics. Additionally, it provides sample C programs for various tasks involving timers and serial communication.

Uploaded by

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

Unit 4

This document covers interrupt programming and interfacing for microcontrollers, specifically focusing on the 8051 architecture. It includes topics such as polling vs. interrupts, ADC/DAC interfacing, and practical applications like home security systems and robotics. Additionally, it provides sample C programs for various tasks involving timers and serial communication.

Uploaded by

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

UNIT-IV

Interrupt programming
and Interfacing

CSEN3021-Microcontrollers
Shaik Jhani Bhasha
and Applications Assistant Professor
Dept. of EECE
GITAM School of
Technology
Hyderabad
z Syllabus
Interrupts Programming,
ADC, DAC and sensor interfacing,
8051 interfacing to external memory,
Development of Home Security System,
Elevator System,
Smart Card Application,
Soccer-Player Robot
INTERRUPTS PROGRAMMING

A single microcontroller can serve several devices by two ways

1. Polling
The microcontroller continuously monitors the status of a given device. When the
conditions met, it performs the service. After that, it moves on to monitor the next
device until everyone is serviced
The polling method is not efficient, since it wastes much of the microcontroller’s
time by polling devices that do not need service

2. Interrupts
An interrupt is an external or internal event that interrupts the microcontroller to
inform it that a device needs its service.
Whenever any device needs its service, the device notifies the microcontroller by
sending it an interrupt signal. Upon receiving an interrupt signal, the microcontroller
interrupts whatever it is doing and serves the device
The program which is associated with the interrupt is called the interrupt service
routine (ISR) or interrupt handler
z
Interrupt
Service Routine

•For every interrupt, there must be an interrupt service routine (ISR),


or interrupt handler.
• When an interrupt is invoked, the microcontroller runs the interrupt
service routine
• For every interrupt, there is a fixed location in memory that holds the
address of its ISR
• The group of memory locations set aside to hold the addresses of
ISRs is called interrupt vector table
Steps in Executing an Interrupt

Upon activation of an interrupt, the microcontroller goes through the following steps
1. It finishes the instruction it is executing and saves the address of the next instruction (PC) on
the stack
2. It also saves the current status of all the registers internally
3. It jumps to a fixed location in memory, called the interrupt vector table, that holds the address
of the ISR
4. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it.
It starts to execute the interrupt service subroutine until it reaches the last instruction of the
subroutine which is RETI (return from interrupt)
5. Upon executing the RETI instruction, the microcontroller returns to the place where it was
interrupted. First, it gets the program counter (PC) address from the stack by popping the top
two bytes of the stack into the PC. Then it starts to execute from that address
IE (Interrupt
Enable)
Register
IP(Interrupt Priority )Register
Write a C program that continuously gets a single bit of data from P1.7
and sends it to P1.0, while simultaneously creating a square wave of 200
μs period on pin P2.5. Use Timer 0 to create the square wave. Assume
that XTAL = 11.0592 MHz.
Timer 0 mode 2 (auto-reload) is used. One half of the period is
100 μs. 100/1.085 μs = 92, and TH0 = 256 - 92 = 164 or A4H
#include <reg51.h>
sbit SW =P1^7;
sbit LD =P1^0;
sbit WAVE =P2^5;
void timer0(void) interrupt 1 {
WAVE=~WAVE; //toggle pin
}
void main() {
SW=1; //make switch input
TMOD=0x02;
TH0=0xA4; //TH0=-92
IE=0x82; //enable interrupt for timer 0
while (1) {
LD=SW; //send switch to LED
}
}
Write a C program using interrupts to do the following:
(a) Receive data serially and send it to P0
(b) Read port P1, transmit data serially, and give a copy to P2
(c) Make timer 0 generate a square wave of 5 kHz frequency on P0.1
Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800.

Solution: void main() {


#include <reg51.h> unsigned char x;
sbit WAVE =P0^1; P1=0xFF; //make P1 an input
void timer0() interrupt 1 { TMOD=0x22;
WAVE=~WAVE; //toggle pin TH1=0xF6; //4800 baud rate
} SCON=0x50;
void serial0() interrupt 4 { TH0=0xA4; //5 kHz has T=200us
if (TI==1) { IE=0x92; //enable interrupts
TI=0; //clear interrupt TR1=1; //start timer 1
} TR0=1; //start timer 0
else { while (1) {
P0=SBUF; //put value on pins x=P1; //read value from pins
RI=0; //clear interrupt SBUF=x; //put value in buffer
} P2=x; //write value to pins
} }
..... }
Write a C program using interrupts to do the following:
(a) Generate a 10 KHz frequency on P2.1 using T0 8-bit auto-reload
(b) Use timer 1 as an event counter to count up a 1-Hz pulse and
display it on P0. The pulse is connected to EX1.
Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.

Solution:

#include <reg51.h> .....


sbit WAVE =P2^1; void main() {
Unsigned char cnt; cnt=0; //set counter to 0
void timer0() interrupt 1 { TMOD=0x42;
WAVE=~WAVE; //toggle pin TH0=0x-46; //10 KHz
} IE=0x86; //enable interrupts
void timer1() interrupt 3 { TR0=1; //start timer 0
cnt++; //increment counter while (1); //wait until interrupted
P0=cnt; //display value on pins }
}
....
Analog to Digital Converter (ADC) and Digital to

Analog to Analog Converter (DAC) are very important


components in electronic equipment. Since most
real-world signals are analog, these two
Digital (ADC) converting interfaces are necessary to allow
digital electronic equipment to process the
and Digital to analog signals. ADC converts the analog signal
collected by input equipment into a digital signal
Analog (DAC) that can be processed by computer. The
computer or microcontroller processes the
converters digital signal that is received. DAC converts the
processed digital signal back into the analog
signal which can be used by the output devices
ADC
0808/0809
•IN0-IN7: Analog Input channels
•D0-D7: Data Lines
•A, B, C: Analog Channel select
lines; A is LSB and C is MSB
•OE: Output enable signal
•ALE: Address Latch Enable
•EOC: End of Conversion signal
•Vref+/Vref-: Differential Reference
voltage input
•Clock: External ADC clock input
Connecting the ADC 0808 for
channel 1 and receiving
converted digital data
through port 1
Previous program in C
DAC 0808
INTERFACING #include< reg51.h>
unsigned char d;
Draw the interfacing diagram of void main(void)
DAC 0808 with 8951 microcontroller.
{
And write a Assembly and C
program to generate triangular while(1)
waveform using DAC. {
for(d=0; d<255; d++)
{
P1 = d;
}
for(d=255; d>0; d--)
{
P1 = d;
}
}
}
Generating crude sine wave using DAC
AGAIN: MOV DPTR, #TABLE
MOV R2, #COUNT #include <reg51.h>
BACK: CLR A Sfr DACDATA = P1;
MOVC A, @A+DPTR Void main ()
MOV P1,A {
INC DPTR unsigned char WAVEVALUE[12] = {128, 192, 238, 192, 128, 64,
DJNZ R2, BACK 17, 0, 17, 64, 128};
SJMP AGAIN unsigned char X;
ORG 300 while (1)
TABLE: DB 128, 192, 238, 192, {
DB 128, 64, 17, 0, 17, 64, 128 for (x=0; x<12; x++)
END {
DACDATA = WAVEVALUE[X];
}
}
}
Sensor interfacing
Reading and displaying the
temperature
In C
INTERFACING
TO EXTERNAL
MEMORY

•A memory chip contain 2x


location, where x is the
number of address pins
•Each location contains y bits,
where y is the number of data
pins on the chip
•The entire chip will contain 2x
× y bits
8051 Connection to 16K x8 External Data RAM
Home
security
system
void main()
#include<reg51.h> {
//P2=0x00;
void Serialwrite(char dat)
sbit PIR=P2^0; Serialbegin();
{ Serialprintln("ATE0");
void delay(int itime) SBUF=dat; delay(50);
{ while(!TI); while(1)
TI=0; {
int i,j; } if(PIR)
void Serialprintln(char *p) {
for(i=0;i<itime;i++)
{ Serialprintln("ATD+919821757249;");
for(j=0;j<1275;j++); delay(1000);
while(*p)
// lcdcmd(192);
} { // lcdprint("Message Sending.");
Serialwrite(*p); Serialprintln("AT+CMGF=1");
void Serialbegin()
p++; delay(50);
{ } Serialprintln("AT+CMGS=\"+919821757249\"");
Serialwrite(0x0d); delay(50);
TMOD=0x20; Serialprintln("Someone is Enter in your Place.");
}
SCON=0x50; delay(50);
Serialwrite(26);
TH1=0xfd; }
}
TR1=1;
}
}
Elevator System
SMART CARD
APPLICATION,
SOCCER-PLAYER ROBOT
Best wishes

You might also like