Pulse width modulation is the type of modulation where we alter the duty cycle of the square wave. we vary the on period and the off period of the square wave to generate the variable duty cycle. This ultimately changes the average voltage output of the square wave.
we can simulate the above behaviour using micro controller programmed to alter the voltage of the output pin. For certain period of the time the output will be heigh and for some period it will be low causing square wave generation. we can alter the time for which the output is heigh this simulates the PWM behaviour.
The circuit has four switches each one will change the on period of the square wave. based on the closed switch on period will be changed in the micro controller. The code which does this as fallows:
#include <regx52.h>
#define OutPut P2_0
#define Sw1 P1_0
#define Sw2 P1_1
#define Sw3 P1_2
#define Sw4 P1_3
int Period = 0;
void delay1ms()
{ int c=0;
while(c<1000)
{
TMOD=0x01;
TH0=0xFC;
TL0=0x66;
TR0= 1;
while(!TF0);
TR0=0;
TF0=0;
c++;
}
}
void main()
{
int j,i;
Sw1 = 1;
Sw2 = 1;
Sw3 = 1;
Sw4 = 1;
OutPut = 0;
while(1)
{
if(!Sw1)
{
Period = 1;
}
if(!Sw2)
{
Period = 2;
}
if(!Sw3)
{
Period = 3;
}
if(!Sw4)
{
Period = 4;
}
OutPut = 1;
for(i=0;i<Period;i++)
{
delay1ms();
}
OutPut = 0;
for(j=0;j<4-Period;j++)
{
delay1ms();
}
}
}
Total time period of the square wave is 4 seconds. Timer is used to generate the 1 ms delay.