Write a C program to produce a 100 Hz square wave with 50 du
Solution
To generate square wave, you have to use timer which will count ON period and OFF period of your square wave. It is easy to generate the Square wave in 8051 controller. You need to select any port pin to generate the square wave. Anyway i have written the C programming code below for producing a square wave with 50% duty cycle at Port P1^0.
#include<reg51.h> //Define 8051 Registers
void DelayMs(unsigned int a); //Delay function
sbit Port1=P1^0; //Set the bit in P1^0
//————————-
// Main Program
//————————-
void main()
{
while(1) //Loop forever
{
Port1 = 1; //Set the bit0 in port1 to high
DelayMs(200); //Delay time for 20ms
Port1 = 0; //Set the bit0 in port1 to low
DelayMs(200); //Delay time for 20ms
}
}
///—————————————-
// DELAY at 11.0592MHz crystal.
// Calling the routine takes about 22us, and then
// each count takes another 1.02ms.
//—————————————-
void DelayMs(unsigned int count)
{
unsigned int i;
while(count)
{
i = 115;
while(i>0) i–;
count–;
}
}


