Anda di halaman 1dari 5

1.

// program to generate a square wave with an ON time of 3ms and an OFF time
of 10ms on all pins of port 0 using timer.

#include<reg51.h>
void main(void)
{
//P1=0x00;
TMOD=0X01;

//Timer 0 , Mode 1

while(1)
{
P0=~P0;
TH0=0Xdb;
TL0=0xff;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;

//wait until flag becomes 1


//Clear the flag
//Stop the timer

TH0=0XF5;
TL0=0x33;
TR0=1;
P0=~P0;
while(TF0==0);
TF0=0;
TR0=0;

//Make the pins low


//wait until flag becomes 1
//Clear the flag
//Stop the timer

}
}

OUTPUT:

// Make the pins high

2. // program to generate a square wave of 10 kHz using timer 1. Assume


XTAL=20MHz.
#include<reg51.h>
sbit pin=P2^1;
void main()
{
TMOD=0X10;
while(1)
{
TH1=0XFF;
frequency
TL1=0XAE;
TR1=1;
while(TF1==0);
TR1=0;
TF1=0;
pin=~pin;

// Timer1, Mode1

// For 10kHz wave & 20MHz crystal

// Start timer1
// Check for timer flag
//

Stop timer1

// Complement

}
}

OUTPUT:

3. // program to count the number of pulses received from system.


#include<reg51.h>
void main(void)
{
int i;
T0=1;
TMOD=0x05;
TL0=0x00;
TH0=0x00;
while(1)
{

//Make input pin


//Counter 0 , Mode 1

do
{
TR0=1;
P0=TL0;
port 0

//Start the counter


//Display the value of TL0 to

P2=TH0;

//Display the value of TH0 to

port 2
}
while(TF0==0);
TF0=0;
TR0=0;
}
}

OUTPUT:

// program to realize digital clock with provision for selection of 12 Hrs or 24 Hrs
modes.
#include<reg51.h>

void delay(void);
void main(void)
{
int i;
int hr,sec,min;
//P0=0;P1=0;P2=0;
while(1)
{
for(hr=0;hr<60;hr++)
{
P1=16*(hr/10)+(hr%10);
for(min=0;min<60;min++)
{
P0=16*(min/10)+(min%10);
for(sec=0;sec<60;sec++)
{
for(i=0;i<20;i++)
{
{delay(); }
if(i=9)
}
}
}
}
}
}
void delay()
{
TMOD=0X01;
TL0=0Xfd;
TH0=0X4b;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}

Anda mungkin juga menyukai