0
5.0kviews
Write a C program to generate a square wave of 5 kHz. (Operate timer 0 in mode 1).
1 Answer
written 6.0 years ago by | • modified 5.9 years ago |
Crystal frequency= 11.0592 MHz
$ I/P clock= \frac {1}{2} \times ( crystal frequency) = \frac {1}{2} \times 11.0592 MHz = 921.6 KHz$
Tin =1.085μsec
For5 kHz square wave
Fout = 5KHz
Tout = $ \frac{1}{5 \times 10^3} = 200 \mu sec$
Tout =200μsec
Consider half of it=Tout = 100μ sec
$ N = \frac{Tout}{Tin} = \frac{100\mu}{1.085\mu} = 100\mu sec$
65536-92= (65444)D =FFA5 H
Program:
#include< reg51.h>
void delay(void);
sbit p=P3^5;
void main (void)
{
while (1)
{
p=~p;
delay();
}
}
void delay()
{
TMOD=0X01; $ \ \ \ \ \ \ \ \ \ \ $//set timer 0 in mode 1 i.e. 16 bit number
TL0=0XA5H; $ \ \ \ \ \ \ \ \ \ \ $//load TL register with LSB of count
TH0=0XFFH ; $ \ \ \ \ \ \ \ \ \ \ $//Load TH register with MSB of count
TR0 =1 ; $ \ \ \ \ \ \ \ \ \ \ $//Start timer 0
While(TF0= = 0); $ \ \ \ \ \ \ \ \ \ \ $ //wait until timer rolls over
TR0=0; $ \ \ \ \ \ \ \ \ \ \ $//Stop timer 0
TF0=0; $ \ \ \ \ \ \ \ \ \ \ $//Clear timer flag 0
}