Set 4 Interrupt Programs
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>
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>
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;
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 ()
{
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 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