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

Set 4 Interrupt Programs

1. The document contains 5 embedded C programs using interrupts. The first program generates a square wave on P2.5 while continuously sending the data from P1.7 to P1.0. The second generates a 10KHz square wave on P2.1 using Timer 0. The third gets data from P1 and sends it to P2 while toggling an LED every 2 seconds. The fourth generates a square wave on all port 0 pins at half the frequency of the INT0 pin. The fifth lights the port 0 or port 2 LEDs depending on which switch is pressed on P3.2 or P3.3.

Uploaded by

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

Set 4 Interrupt Programs

1. The document contains 5 embedded C programs using interrupts. The first program generates a square wave on P2.5 while continuously sending the data from P1.7 to P1.0. The second generates a 10KHz square wave on P2.1 using Timer 0. The third gets data from P1 and sends it to P2 while toggling an LED every 2 seconds. The fourth generates a square wave on all port 0 pins at half the frequency of the INT0 pin. The fifth lights the port 0 or port 2 LEDs depending on which switch is pressed on P3.2 or P3.3.

Uploaded by

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

Set 4 INTERRUPT PROGRAMS

1. Write an Embedded 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 micro period on pin
P2.5. Use timer 0 to create the square wave. Assume XTAL=11.0592MHz

#include<reg51.h>

sbit led=P1^0;//bit for led glowing


SET 4 EMBEDDED SYSTEMfor switch
sbit sw=P1^7;//bit 1
sbit wave=P2^5;//bit for switch generation

void timer0_ISR () interrupt 1//Interrupt function


{
wave=~wave;//generation of square wave
}

void main ()
{
sw=1;
TMOD=0X02;//timer 0 in mode 1
IE=0x82;//Enabling the IE register
TH0=0xA4;//Assigining the delay
//TL0=0xA4;
TR0=1;//starting of the timer

while (1)
{
if(sw==1)//linking the led to switch
led=0;
else
led=1;

}
}

1
SET 4 EMBEDDED SYSTEM 2

2
2. Write an Embedded C program to do the following using interrupts
a. Generate a 10KHz square wave on P2.1 using Timer 0 in auto reload mode

#include<reg51.h>

sbit led=P1^0;//bit for led glowing


sbit sw=P1^7;//bit for switch
sbit wave=P2^5;//bit for switch generation

void timer0_ISR()interrupt 1//Interrupt function


{
wave=~wave;//generation of square wave
}

void main()
{
sw=1;
TMOD=0X02;//timer 0 in mode 1
TH0=0xD1;//Assigning the delay
TR0=1;//starting of the timer
IE=0x82;//Enabling the IE register

while (1)
SET 4 EMBEDDED
{ SYSTEM 3
if(sw==1)//linking the led to switch
{
led=0;
}
else
led=1;

}
}

3
SET 4 EMBEDDED SYSTEM 4

4
3. Write an Embedded C program using interrupts to get data from P1 and
send it to P2 while Timer 1 is turning on and off the LED connected to P0.4
every 2 seconds

#include<reg51.h>
sbit led=P1^0;//bit for led glowing
sbit sw=P1^7;//bit for switch
sbit wave=P2^5;//bit for switch generation
int count=0;

void timer0_ISR () interrupt 1//Interrupt function


{
count++;
if(count==28)
{
wave=~wave;//generation of square wave
count=0;
}
}
void main ()
SET 4 EMBEDDED SYSTEM 5
{
sw=1;
TMOD=0X01;//timer 0 in mode 1
TH0=0x00;//Assigning the delay
TL0=0x00;//
TR0=1;//starting of the timer
IE=0x82;//Enabling the IE register

while (1)
{
if(sw==1)//linking the led to switch
{
led=0;
}
else
led=1;
}
}

5
SET 4 EMBEDDED SYSTEM 6

6
4. Generate from all pins of Port 0, a square wave which is half the frequency
of the signal applied at INT0 pin

#include<reg51.h>
#define led P0
unsigned char count=0;

void isr()interrupt 0
{
led=~led;//inverting the led
}

void main ()
{

TCON=0x01;//setting of the TCON REGISTER


IE=0X81;//setting of the IE register
while (1) SYSTEM
SET 4 EMBEDDED 7
{
//led=count++;//Assigning of the count to led
}

7
SET 4 EMBEDDED SYSTEM 8

8
5. Two switches are connected to pins P3.2 and P3.3. When a switch is
pressed, the corresponding line goes low. Write a program to
a. Light all LEDs connected to port 0, if the first switch is pressed.
b. Light all LEDs connected to port 2, if the second switch is pressed.

#include<reg51.h>

void INT0_ISR()interrupt 0//External interrupt 0 ISR


{
P0=0XFF;//switch ON the LEDs connected to port 0

void INT1_ISR()interrupt 2//external interrupt 1 ISR


{
P2=0XFF;//switch ON the LEDs connected to port 2
}

void main()
{
TCON=0X00;//set INT0 AND INT1 as level triggerd
IE=0X85;//enable the INT0 and INT1 interrupt
SET 4 EMBEDDED SYSTEM 9
while(1)
{
P0=0X00;//switch OFF the LEDs connected to port 0
P2=0X00;//switch OFF the LEDs connected to port 0
}
}

9
SET 4 EMBEDDED SYSTEM 10

10

You might also like