You are on page 1of 7

1)// Program for toggling bits of P0 and P1 for 450 ms

#include<reg51.h>
inti,j;
void delay();

//delay function is declared

void main()
{
P1 = 0x00;

//both ports are initialized to 0

P0 = 0x00;
while(1)
{
P1 = 0x00;
P0 = 0xff;
delay();

//delay function call

P1 = 0xff;
P0 = 0x00;
delay();
}
}

void delay()
{
for(i=0;i<75;i++)
for(j=0;j<1000;j++);
}

//To create 450ms delay

Output:

2) //Program to toggle bits of P0, P1 and P2 after every 1/4th of a second.


#include<reg51.h>
inti,j;
void delay();

//delay function is declared

void main()
{
P0 = 0x00;

//all the ports are initialized to 0

P1 = 0x00;
P2 = 0x00;
while(1)
{
P0 = 0x00;

//toggling of P0,P1,P2

P1 = 0x00;
P2 = 0x00;
delay();

//delay function call

P0 = 0xff;

//toggling of P0,P1,P2

P1 = 0xff;
P2 = 0xff;
delay();

}
}

void delay()

//To create 1/4th second delay

{
for(i=0;i<40;i++)
for(j=0;j<1000;j++);
}

Output :

3)//program to get a byte of data from P1, wait for sec and then send the data to P2

#include<reg51.h>

inti,j,mydata;
void delay();

//delay function is declared

mydata = 0x55;

//data on P1 that is send to P2

void main()
{

while(1)
{
P1 = mydata; //data is sent on P1
delay();

//delay of half second

P2 = P1;

//data from P1 is sent to P2

}
}

void delay()

//To create 1/2 second delay

{
for(i=0;i<80;i++)
for(j=0;j<1000;j++);
}

4) //Program to create 50% duty cycle

#include<reg51.h>
inti,j;
void delay();
sbitmypin = P1^0;
void main()
{

//delay function is declared


//setting mypin as P1.0

while(1)
{
mypin = 0;
delay();
mypin = 1;

//delay function call

delay();
}
}

void delay()
{
for(i=0;i<10;i++)
for(j=0;j<1000;j++);
}

Output :

//To create delay

//Program to create 66% duty cycle

#include<reg51.h>
inti,j;
void delay1();

//delay function is declared

void delay2();
sbitmypin = P1^3;

//setting mypin as P1.0

void main()
{

while(1)
{
mypin = 0;
delay1();
mypin = 1;
delay2();
}
}

void delay1()

//To create delay

{
for(i=0;i<40;i++)
for(j=0;j<1000;j++);
}
void delay2()
{
for(i=0;i<80;i++)
for(j=0;j<1000;j++);
}

//To create delay

Output :

You might also like