# What is a Square Wave Generator : Circuit Diagram & Advantages

## What is a square wave generator?

A square wave generator is a non-sinusoidal waveform oscillator that is capable of generating square waves. The Schmitt trigger circuit is an implementation of square wave generators. Another name for the square wave generator is an Astable or a free-running multivibrator.

## Square wave generator using op amp

A square wave generator using an operational amplifier is also called an astable multivibrator. When an operational amplifier is forced to operate in the saturation region, it generates square waves. The output of the op-amp swings between the positive and the negative saturation and produces square waves. That’s why the op-amp circuit here is also known as a free-running multivibrator.

## Square wave generator working

The circuit of the op-amp contains a capacitor, resistors, and a voltage divider. The capacitor C and the resistor R are connected with the inverting terminal, as shown in figure 1. The non-inverting terminal is connected to a voltage divider network with resistors R1 and R2. A supply voltage is provided to the op-amp. Let us assume that the voltage across the non-inverting terminal is V1 and across the inverting terminal is V2. Vd is the differential voltage between the inverting and the non-inverting terminal. Initially, the capacitor has no charge. Therefore, we can take V2 as zero.

We know, Vd = V1-V2

As initially, V2=0, Vd = V1

We know, V1 is a function of output offset voltage, R1, and R2. The leakage results in the generation of the output offset voltage.

Vd can be positive or negative. It depends upon the polarity of the output offset voltage.

Let us assume initially, Vd is positive. So the capacitor has no charge, and the op-amp has maximum gain. So the positive differential voltage will drive the op-amp’s output voltage Vo towards the positive saturation voltage.

So, V1=R1/R1+R2Vsat

At this point, the capacitor starts charging towards the positive saturation voltage through the resistor R. It will increase its voltage from zero to a particular value. After reaching a value slightly greater than V1, the op-amp will give a negative output voltage, and reach the negative saturation voltage. Then the equation becomes,

Vd = -V1+V2

-V1=R1/R1+R2(-Vsat)

As V1 is negative now, the capacitor starts discharging towards negative saturation voltage up to a certain value. After reaching a value slightly less than V1, the output voltage will move to positive saturation voltage again.

This total phenomenon repeatedly happens , generating the square waves(shown in figure 2). Therefore we get square waves that switch between +Vsat and -Vsat.

Therefore, V1=R1/R1+R2(Vsat)

The time period of the output of square wave, T=2RCln (2R1+R2/R2)

## Triangular wave generator using op amp

There are two parts of a triangular wave generator circuit. One part generates the square wave, and the second part converts the square wave into a triangular waveform. The first circuit consists of an op-amp and a voltage divider connected to the op-amp’s non-inverting terminal. The inverting terminal is grounded.

The output of this op-amp acts as input for the second part, which is an integrator circuit. That contains another operational amplifier whose inverting terminal is connected with a capacitor and a resistor, as shown in figure 3. The non-inverting terminal of the op-amp is made ground. Let’s say the first output is Vo1 and the second output is Vo2. Vo2 is connected with the first op-amp as feedback.

The comparator S1 continuously compares the voltage of point A(figure 3) with ground voltage, i.e., zero. According to the positive and the negative value, the square wave is generated at Vo1. In the waveform, we see that when the voltage at point A is positive, S1 gives +Vsat as output. This output provides input for the second op-amp that produces a negative-going ramp Voltage Vr as output. Vr gives negative voltage up to a certain value. After some time, the voltage at A falls below zero, and S1 gives -Vsat as output.

At this stage, the value of Vr starts increasing towards the positive saturation voltage. When the value crosses +Vr, the output of the square wave goes up to +Vsat. This phenomenon goes on continuously, providing the square wave as well as the triangular wave ( shown in figure 4).

For this entire circuit, we notice that when Vr gets changed from positive to negative, a positive saturation voltage is developed. Similarly, when Vr gets changed from negative to positive, a negative saturation voltage is developed. Resistor R3 is connected to Vo1 while, resistor R2 is connected to Vo2. Therefore, the equation can be written as,

-Vr/R2 = -(+Vsat/R3)

Vr = -R2/R3(-Vsat)

The peak to peak output voltage Vpp=Vr-(-Vr)=2Vr=2R2/R3(Vsat)

Output at the integrator circuit is given by,

Here, Vo=Vpp and Vinput= -Vsat

So, by putting the values we get,

Therefore,

So, frequency

## Time period of square wave generator

The time period of the square wave generator,

R = resistance

C = capacitance of the capacitor connected with the inverting terminal of the op-amp R1 and R2 = resistance of the voltage divider.

## Square wave generator frequency formula

The frequency of the square wave generator,

## Variable frequency square wave generator

Most commonly, multivibrator circuits are used in generating square waves. RC or LR circuits can generate a periodic sequence of quasi-rectangular voltage pulses utilizing the saturation characteristic of the amplifier. The variable frequency square wave generator circuit consists of four major components- A linear amplifier and an inverter with a total gain of K, a clipper circuit with some specific input-output characteristics, and a differentiator comprising RC or LR network with the time constant ?. The time period of the obtained signal is

T=2?ln(2K-1)

This multivibrator circuit can produce uniform voltage pulses because of the symmetrical saturation characteristic of the clipper circuit. We can vary the oscillation frequency by varying either the time constant of the differentiator or the gain of the amplifier.

## AVR square wave generator

It is possible to generate different waveforms using AVR microcontrollers by interfacing a Digital to Analog Converter(DAC). The DAC converts the microcontroller provided digital inputs into analog outputs, and thus generates different analog waveforms. The DAC output is actually the current equivalent of the input. So, we use 741 operational amplifier integrated circuit as a current to voltage converter.

The microcontroller gives low and high outputs in alternate fashion as an input to DAC after applying some delay. Then the DAC generates corresponding alternate analog outputs through the op-amp circuit to produce a square waveform.

## High frequency square wave generator

High-frequency square wave generators produce accurate waveforms with minimum external hardware components. The output frequency can range from 0.1 Hz to 20 MHz. The duty cycle is also variable. The high-frequency square wave generators are used in-

• ‌Precision Function Generators
• ‌Voltage-Controlled Oscillators
• ‌Frequency Modulators
• ‌Pulse-Width Modulators
• ‌Phase Lock Loops
• ‌Frequency Synthesizer
• ‌FSK Generators

## Time Period and Frequency Derivation of Square Wave Generator

According to the ideal op-amp conditions, the current through it is zero. Therefore, by applying Kirchhoff’s law, we can write,

The ratio R1/R1+R2 is known as the feedback fraction and is denoted by β.

When V1 reaches positive saturation voltage,

V0 = +Vsat,

V1/β = +Vsat

Or, V1 =  βVsat

Similarly, when V1 reaches negative saturation voltage,

V0 = -Vsat,

V1/β = -Vsat

Or, V1 = -βVsat

By this time, the capacitor has charged to CV1 = CβV0; it again starts discharging. So, according to the general capacitor equation with an initial charge Q0,

Q=CV(1-et/RC)+Q0et/RC

We know, here V = -V0 and Q0=βCV0

So,

Now, when Q goes to -CV1 = -CβV0, another switch occurs at t=T/2. At this time,

Therefore,

Frequency

## 555 square wave generator 50% duty cycle

The square wave generator can be constructed using the 555 timer integrated circuit. It is efficient for generating square pulses of lower frequency and adjustable duty cycle. The left part of the IC includes the Pins 1-4- Ground, Trigger, Output, and Reset. Pins 5-8 are on the right side. Pin 5, pin 6, pin7, and pin 8 are the control voltage, the threshold, the discharge, and the positive supply voltage respectively. The main circuitry consists of the 555 IC, two resistors, two capacitors, and a voltage source of 5-15 Volts. This circuit can further be optimized using a diode to produce a perfect square wave. The 555 timer can easily create square waves in astable mode.

The circuit diagram is shown in figure 5. Pin 2(Trigger) and pin 6(Threshold) are connected so that the circuit continuously triggers itself on each cycle. The capacitor C charges through both the resistors but discharges only through R2 connected to pin 7(discharge). The timer starts when pin 2 voltage decreases below 1/3VCC. If the 555 timer is triggered through pin 2, the pin 3 output becomes high. When this voltage climbs up to 2/3VCC, the cycle finishes, and the pin 3 output becomes low. This phenomenon results in a square wave output.

The below equations determine the charging time or Ton and the discharging time or Toff:

Ton= 0.693(R1+R2)C

Toff= 0.693R2C

So the total cycle time T = 0.693(R1+R2+R2)C =0.693(R1+2R2)C

Therefore, frequency f = 1/T = 1.44/(R1+2R2)C

Duty cycle =Ton/T=R1+R2/R1+2R2

## 555 variable frequency square wave generator

To make a variable frequency square wave generator, we take a 555 timer IC. At first, we make pin 2 and pin 6 short-circuited. Then we connect a jumper wire between pin 8 and pin 4. We connect the circuit to positive Vcc. Pin 1 is connected to the ground. A capacitor of 10 nF is attached with pin 5. A variable capacitor is joined with pin 2. Pin 4 and pin 8 are made short-circuit. A 10 Kohm resistor is connected between pin 7 and pin 8. A 100 Kohm potentiometer is connected between pin 6 and pin 7. This circuit produces square waveforms. We can adjust the frequency with the help of the potentiometer.

## ATtiny85 square wave generator

The ATtiny85 8-bit AVR microcontroller based on RISC CPU, has an 8 pin interface and 10 bit ADC converter. The timer in ATtiny85 sets up the Pulse width modulation mode and helps in varying the duty cycle so that the proper square wave is generated.

## Square wave sound generator

Square waves are one of the four fundamental waves that create sound. The other three waves are the triangular wave, sine wave, and sawtooth wave. Together the waves can produce different sounds if we vary the amplitude and frequency. If we increase the voltage, i.e., the amplitude, the volume of the sound increases. If we increase the frequency, the pitch of the sound increases.

## 1khz square wave generation in 8051

We can program the 8051 microcontrollers to generate a square wave of the desired frequency. Here, the frequency of the signal is 1 kHz, so the time period is 1 millisecond. The 50% duty cycle is best for perfect square waves. So, Ton=Toff= 0.5 ms.

Circuit and connections: To make the circuit, we need the following components-

• ‌8051 microcontroller
• ‌Digital to analog converter
• Resistors and capacitors
• Operational amplifier

We connect the reset pin to the voltage source (Vcc) and the DAC data pins to port 1 of the 8051 microcontroller. The most significant bit has to be connected with the A1 pin (pin 5) on the DAC and the least significant bit with the A8 pin.

Logic: At first, we set any of the 8051 ports to logic 1 or high and then wait for some time to get a constant DC voltage. This time is known as delay. Now we set the same port to logic 0 or low and again wait for some time. The process continues in a loop until we turn off the microcontroller.

## Square wave generator using IC 741 | square wave generator using op amp 741

The IC 741 square wave generator circuit is depicted in the figure above(figure 6). The operational amplifier in the circuit built using the general IC 741. Pin 2 of the IC is connected to the inverting terminal, and pin 3 is connected to the non-inverting terminal. Pin 7 and pin 4 are connected to the positive and negative supply voltage, respectively. The output is connected to pin 6. The capacitor, the resistor, and the voltage divider are connected, as shown in the figure.

The working principle of IC 741 circuit is similar to that of the general square wave generator. The capacitor keeps on charging and discharging between the positive and the negative saturation voltage. Thus it produces the square wave.

The time period T=2RC ln (2R1+R2/R2)

The frequency is the reciprocal of the time period, i.e., f=1/2RC ln (2R1+R2/R2)

## MATLAB code to generate square wave

The Matlab command to generate a square wave is given below-

``````clc
close all
clear  #clearing all previous data
t=1:0.01:50;  #defining X axis from 1 to 50 with step 0.01
Y=square (t,50);   #taking a variable Y for a square wave with 50% duty cycle
plot(Y,t);  #plotting the curve
xlabel('Time');  #labelling X-axis as Time
ylabel('Amplitude');  #labelling Y-axis as Amplitude
title('Square Wave'); #the title of the plot is Square Wave
axis([-2 1000 5 -5]);  #modifying the graph for visualization
``````

## Square wave generator using transistor | transistor square wave generator

Another technique of building a square wave generator (Astable Multivibrator) is using a BJT or bipolar junction transistor. The operation of this square wave generator or astable multivibrator depends upon the switching property of the BJT. When a BJT acts as a switch, it has two states- on and off. If we connect +Vcc in the collector terminal of the BJT when the input voltage Vi is less than 0.7 volt, the BJT is said to be in the off state. In the off state, the collector and the emitter terminal get disconnected from the circuit.

Therefore, the transistor behaves to be an open switch. So the Ic=0 (Ic is the collector current) and the voltage drop between the collector terminal and the emitter terminal(Vce) is positive Vcc.

Now when Vi>0.7 volt, the BJT is in on state. We short the collector and the emitter terminal. Therefore, Vce=0 and the current Ic will be the saturation current(Icsat).

The circuit diagram is shown in figure 7. Here, the transistors S1 and S2 look identical, but they have different doping properties. S1 and S2 have load resistors RL1 and RL2 and are biased through R1 and R2, respectively. The collector terminal of S2 is connected to the base terminal of S1 through the capacitor C1, and the collector terminal of S1 is connected to the base terminal of S2 through the capacitor C2. So, we can say that the astable multivibrators are made with two identical common-emitter configurations.

The output is obtained from any of the two collectors to the ground. Suppose we are taking Vc2 as the output. So the entire circuit is connected to the supply voltage Vcc. The negative terminal of Vcc is grounded. When we close the switch K, both the transistors try to stay in the on state. But eventually, one of them stays in the on state and the other one in the off state. When S1 is in the on state, the collector and the emitter terminal of S1 get shorted. So, Vc1=0. Meanwhile, S2 is in the off state.

Therefore, the collector current Ic2=0 and Vc2=+Vcc. So for the T1 time interval, the transistor Vc1 remains in logic 1, and Vc2 remains in logic 0. While S2 is in the off state, the capacitor C2 gets charged. Let us say the voltage across C2 is Vc2. So we connect the positive terminal of the capacitor to the base of S2, and the negative terminal of the capacitor to the emitter of S2. So the voltage Vc2 is directly provided to the base and the emitter terminal of S2.

As the capacitor is continuously charging, after some time, Vc2 goes up above 0.7 volts. At this point, S2 comes to the on state, and the voltage difference between the collector and the emitter terminal of S2 equals zero. Now, S1 acts in the on state, and the output voltage of S1 is +Vcc. The capacitor C1 starts charging, and when the voltage across the capacitor crosses 0.7 volts, S1 again changes its state. So for the T1 time interval, the transistor Vc1 remains in logic 0, and Vc2 remains in logic 1.

This phenomenon repeats automatically until the power supply is turned off. The continuous transition between Vcc and 0 generates the square wave.

## Square wave generator using NAND Gate

The use of a NAND gate is one of the simplest ways to make a square wave generator. We need the following components to build the circuit are- two NAND gates, two resistors, and one capacitor. The circuit is shown in figure 8. The resistor-capacitor network is the timing element in this circuit. The G1 NAND gate controls its output. The output of this RC network is fed back to G1 through the resistor R1 as input. This procedure occurs until the capacitor is fully charged.

When the voltage across C reaches the positive threshold of G1, the NAND gates change states. Now the capacitor discharges up to the negative threshold of G1, and again the gates change their states. This process occurs in a loop and produces a square waveform. The frequency of this waveform is calculated using, f=1/2.2RC

## Square wave generator using Schmitt Trigger

The working of a Schmitt trigger square wave generator circuit is quite similar to the NAND gate implementation. The Schmitt trigger circuit is shown in figure 9. Here also, the RC network provides the timing. The inverter takes its output in the form of a feedback as one of the inputs.

Initially, the NOT gate input is less than the minimum threshold voltage. So the output state is High. Now the capacitor begins to charge through the resistor R1. When the voltage across the capacitor touches the maximum threshold voltage, the output state again drops to low. This cycle repeats again and again and generates the square wave. The frequency of the square wave is found by f=1/1.2RC

## Square wave generator verilog code | square wave generator using verilog

```````timescale 1ns / 1ps
module square_wave_generator(
input clk,
input rst_n,
output square_wave
);
// Input clock is 100MHz
localparam CLK_FREQ = 100000000;
// Counter to toggle the clock
integer counter = 0;

reg square_wave_reg = 0;
assign square_wave = square_wave_reg;
always @(posedge clk) begin

if (rst_n) begin
counter <= 8'h00;
square_wave_reg <= 1'b0;
end

else begin

// If counter is zero, toggle square_wave_reg
if (counter == 8'h00) begin
square_wave_reg <= ~square_wave_reg;

// Generate 1Hz Frequency
counter <= CLK_FREQ/2 - 1;
end

// Else count down
else
counter <= counter - 1;
end
end
endmodule
``````

## 8051 C program to generate square wave

``````#include <reg51.h> // including 8051 register file
sbit pin = P1^0; // declaring a variable type SBIT
for P1.0
main()
{
P1 = 0x00; // clearing port
TMOD = 0x09; // initializing timer 0 as 16 bit timer
TH0 = 0x3C; // 50000 counts timer 0 will be
overflow
pin = 1; // sending high logic to P1.0
TR0 = 1; // starting timer
while(TF0 == 0) {} // waiting for first overflow for 50 ms
TH0 = 0x3C;
pin = 0; // sending 0 to P1.0
while(TF0 == 0) {} // waiting for 50 ms again
goto loop; // continuing with the loop
}
``````

## 8253 square wave generator

8253 is a programmable interval timer. It has 3 16-bit counters and operates in six modes. Each of the counters has three modes as -CLK(input click frequency), OUT(output waveform), and GATE(to enable or disable the counter). Mode 3 is known as the square wave generator mode. In this operating mode, the out is high when the count is loaded. The count is then gradually decremented. When it comes down to zero, the out becomes low, and again the count starts loading. Thus a square wave is generated.

An adjustable square wave generator can be built using a potentiometer in place of a general voltage divider. As the resistor value is changeable, we can adjust the parameters of the square wave output.

## Advantages of square wave generator

A square wave generator has the following advantages-

• The circuit can be easily designed. It does not need any complex structure.
• It is cost-effective.
• Maintenance of the square wave generator is very easy.
• A square wave generator can produce signals with maximum frequencies.

## Comparator square wave generator

Comparator circuits that are efficient in hysteresis are used to make square wave generators. Hysteresis refers to the action of providing positive feedback to the comparator. This hysteresis occurs for Schmitt trigger and Logic gate square wave generators, and almost perfect square waves are generated.

## High voltage square wave generator

The high voltage square wave generator can be made using a MOSFET (metal-oxide-semiconductor field-effect transistor). This square wave generator device is effective in producing square waves of different amplitudes.

## Square to sine wave generator | square wave to sine wave generator

The square wave to sine wave converter circuit makes use of multiple RC networks. It has three resistors and three capacitors. The three-stage RC filter first changes the square wave into a triangular wave and then converts it into the sine wave. The values of the resistor and the capacitor decide the frequency of the square wave.

## Digital square wave generator

Digital function generators are one of the most preferred ways of generating square pulses. It is called direct digital synthesis (DSS). The components required for DSS are a phase accumulator, a digital to analog converter, and a look-up table containing waveforms. DSS generates an arbitrary periodic waveform from a ramp signal and thus generates a digital ramp. This technique is accurate and highly stable.

## 1 mHz square wave generator circuit

The Schmitt trigger oscillator circuit is one of the most effective ways to generate a 1 mhz square wave. The circuit comprises a couple of Schmitt inverters, a variable resistor, some capacitors, and resistors.

## Square wave generator chip

741 Operational amplifier IC is the most popular chip for the generation of square waves. Besides this, 555 timer IC is also used to make square wave generator circuits.

## Square wave generator application | application of square wave generator

The applications of a square wave generator are-

• ‌It is used to generate square waves and other circuits that produce triangular or sinusoidal waves from square waves.
• ‌Square wave generators are useful in controlling clock signals.
• ‌It is used in musical instruments to emulate various sounds.
• ‌Function generators, Cathode Ray Oscilloscopes, make use of square wave generators.

## How do you find the frequency of a square wave generator?

For a square wave generator, T=2RC ln (2R1+R2/R2). The frequency of the wave is determined from this equation.

Therefore, frequency f=1/2RC ln (2R1+R2/R2)

## What is the triangular waveform generator?

A triangular waveform generator is an electronic waveform generator circuit.

A triangular waveform generator generates triangular waves. Generally, a square wave generator combined with an integrator circuit produces triangular waves.

## How can you generate square wave and triangular wave?

An astable multivibrator circuit is considered one of the best practices to generate square waves. It involves an operational amplifier, a capacitor, a resistor, and one voltage divider network.

We can use the output square wave achieved from an astable multivibrator as the input of an integrator circuit in order to generate square waves. Also, we can use a Schmitt trigger feedback circuit with an integrator to get triangular waves.

## What are the applications of a square wave generator?

A square waveform generator is widely used in electronics.

Some useful applications of a square wave generator are-

• Clock Signals
• ‌Emulation of sound from various instruments
• ‌Sine wave/triangular wave converter circuits
• ‌Transistor switching
• ‌Amplifier response checking
• ‌Control system operations

## I want to make a variable duty cycle square wave generator where input voltage is 12V. What will be the requirement and how to make it?

A square wave generator, combined with diodes can help in varying the duty cycle.

The square wave generator circuit given below allows us to make changes in the duty cycle. Two diodes are connected in parallel here, but in opposite directions. One diode starts working when the output is high, the other one comes into operation when the output is low. When the output is high, the D1 diode starts operating. Similarly when the output is low, D2 operates. Thus, the circuit goes to logic high and low and generates a square waveform.

The time period T=2RC ln (2R1+R2/R2)

## How to generate a square wave using an op-amp?

We know, there are numerous ways to generate a square wave.

An operational amplifier when used with a capacitor, a resistor and a voltage divider, produces output as square wave. The square wave generation happens when the output switches between the positive and the negative saturation voltage continuously.

## How can I generate a square wave from a triangular wave by using only a resistor and capacitor?

We know, a differentiator circuit gives square wave as output when it takes triangular wave input.

So, to generate a square wave from a triangular wave, we can keep the capacitor in series with the source and ground the resistor first. By this, we can make a high-pass filter. If the frequency of the triangular wave is lesser than the cut-off frequency of the high-pass filter, then the filter differentiates the triangular wave and produces a square wave.

## What is the equation of the square wave?

A square wave can be represented in different forms.

The most common equation of a square wave is –

x(t)=sgn(sin 2πt/T)=sgn(sin(2πft))

y(t)=sgn(cos 2πt/T)=sgn(cos(2πft))

Where, T= Time period and f=frequency of the wave.

We can modify the equation according to the conditions given.

## How to convert a triangular wave into a square wave?

Square wave is nothing but the integral of a triangular wave.

To convert a triangular wave into a square wave, we can use a differentiator amplifier circuit. This circuit comprises an op amp, a capacitor and a resistor.

## What happens if a square wave passes through a capacitor?

Different waveform generators use capacitor in their circuitry.

If a square wave passes through a capacitor, it can generate different types of waveforms according to the other circuit parameters.

## What is the application of an audio frequency sine and square wave generator?

Musical instruments make use of high quality waveform generators.

An audio frequency sine and square wave generator is used as an audio oscillator. The circuit consists of a wein bridge oscillator which provides the best audio frequency range.

## What is the difference between pulse wave and square wave?

Square wave is nothing but a subset of the pulse wave.

A square wave is a special type of pulse wave where the positive halves of the cycle equal the negative halves. A pulse wave with 50% duty cycle is said to be a square wave.

## How to generate a trapezoidal waveform from an op amp?

We can generate a trapezoidal waveform in three steps.

This method gives almost a trapezoidal shaped waveform.

• Generating a square wave
• ‌Converting the square wave into a triangular wave using an integrator
• Using clipper circuit to limit the voltage without affecting the rest of the waveform.

## What is the advantage of using a square waveform as an input signal?

A square waveform is a periodic waveform which is non-sinusoidal in nature. The amplitude of a square wave have fixed maxima and minima at a particular frequency.

The main advantages of using a square waveform as an input signal is-

• ‌It has a wide bandwidth of frequencies.
• ‌Easy and quick visualization in an oscilloscope is possible with square waves.
• ‌Square waveforms can indicate issues to be fixed.

## Does the LC circuit convert square wave output voltage to pure sinusoidal output? If so, what is the operation behind it?

An LC circuit is a network consisting of single or multiple inductor and capacitor.

Yes, LC filter circuits efficiently convert square waves into sine waves. The filter circuit allows only the fundamental frequency of the square wave to pass and filter out other high frequency harmonics. Thus the square wave gets converted into a sine wave.

## Why we will get square wave as output in comparator circuit?

A comparator circuit compares an AC sinusoidal signal with a DC reference signal.

The input signal upon becoming larger than the reference signal, yields a positive output. When it is less than the reference signal, the output is negative. In both the scenarios, the difference of the signals is so large that it is considered to be equivalent with the maximum possible output (±Vsat). So, it is assertive that the output continuously dangles between positive and negative saturation voltage. That’s why we get square waves as comparator output.

## How do I generate a square wave for different duty cycles in 8051 using embedded C?

``````#include<reg51.h>
sbitpbit=PI^7;
void delay_on();
void delay_off();
void main()
{
TMOD=0x01;  //initializing timer 0 in mode 1
while(1);        // repeating this
delay_on();   //800 microsecond delay
pbit=0;            //output pin low
delay_off();  //200 microsecond delay
}
}
//function for 800 microsecond delay
Void delay_on()
{
TH0=OxFD;
TR0=1;   //turning the timer 0 ON
while(!TF0);   //waiting for timer overflow
TR0=0;      //switching the timer 0 OFF
TF0=0;      //clearing the overflow flag
}
//function for 200 microsecond delay
Void delay_off()
{
TH0=OxFF;
TL0=0x48;
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}   //clearing TF0
``````

## How do we write an embedded C code to generate a square wave of 50 Hz?

``````#include<reg51.h>
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<922;j++);
}
void main()
{
while(1)
{
p1=255;
delay(10);
p1=0;
delay(10);
}
}
``````