You are on page 1of 11

EMBEDDED SYSTEM SET 2 05/20/2021

1)Generate square wave using Timer0 mode1 with 50 percent duty


cycle. find the time delay caused by the timer for the crystal
frequency f= 11.0592Mhz
//Program to blink the led using the timer
#include<reg51.h>
//delay function
void delay ()
{
TMOD=0x01;//Timer 0 in mode 1
TL0=0XF2;
TH0=0XFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
sbit led=P2^0;//Assigning of the port 2 to the led

void main ()
{
while (1)
{
led=0;//led is OFF
delay () ;//calling the delay function
led=1;//led is ON
delay1() ;//calling the delay function
}
}

Page 1 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

Page 2 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

2)Generate rectangular pulse ON=3 msecs, OFF=10msecs,


f=22MHz. use timer 1 mode 1
//Program to blink the led using the timer

#include<reg51.h>
//Delay function for 3ms
void delay3MS ()
{

TMOD=0x01;
TL0=0X80;
TH0=0XEA;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
//Delay function for 10ms
void delay10MS ()
{

TMOD=0x01;
TL0=0X54;
TH0=0XB8;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}

sbit led=P2^0;//Assigning of port 2

void main ()
{
//led=0;
while (1)
{
led=0;
delay3MS () ;//Calling of delay function of 3ms
led=1;
delay10MS () ;//Calling of delay function of 10ms

}
}

Page 3 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

Page 4 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

3) Generate a pulse train of 2 secs time period at P2.4. Use timer 1


mode1, f=22MHz.

//Program to produce pulse train of 22seconds

#include<reg51.h>

//Delay function of 2s

void delay_2s ()

int i;

for(i=0;i<30;i++)//repeat 30 times to get the 2seconds delay

TMOD=0x01;//using the timer0 in mode 1

TL0=0X00;//

TH0=0X00;//

TR0=1;//Initialize the Time run register =1

while(TF0==0);

TR0=0;

TF0=0;

sbit led = P2^4;//Assigning the port 2.4 to led

void main()

while(1)

led=0;

delay_2s();//Calling the delay function

led=1;

delay_2s();//Calling the delay function

Page 5 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

Page 6 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

4)Generate the following waveform at P1.2 using f=22 MHz

//Program to generate waveform of 50ms and 20ms

#include<reg51.h>
//function for 50ms
void delay50ms ()
{

TMOD=0x01;//timer 0 in mode 1
TL0=0XFE;
TH0=0X4B;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
//function for delay of 20ms
void delay20ms ()
{
TMOD=0x01;//timer 0 in mode 1
TL0=0X75;
TH0=0XB8;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
sbit led = P2^2;//Assigning the port 2.2 to led
Page 7 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

void main ()
{
int i;
while (1)
{
led=0;
delay50ms () ;//calling the delay function 50ms
led=1;
delay50ms () ;//calling the delay function 50ms

for(i=0;i<5;i++)//generating the delay for 5 times


{
led=0;
delay20ms();//calling the delay function 20ms
led=1;
delay20ms();////calling the delay function 20ms
}
}
}

Page 8 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

Page 9 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

5)Generate a square wave of 1Khz at P1.2 assuming f=22MHz

//Program to generate the square wave of 1kHz


#include<reg51.h>
//function for creating the 1Khz wave
void delay1khz ()
{
int i;
for(i=0;i<15;i++)
{
TMOD=0x01;//timer 0 in mode 1
TL0=0X00;
TH0=0X00;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
}
sbit led=P2^2;//Initialization of port 2.2 to led

void main()
{
while (1)
{
led=0;
delay1khz ();
led=1;
delay1khz ();
}
}

Page 10 of 11
EMBEDDED SYSTEM SET 2 05/20/2021

Page 11 of 11

You might also like