Skip to main content

Implementation of PWM in 8051 microcontroller

Pulse Width Modulation is the waveform whose amplitude and phase remains the constant while the width of the pulse varies. This type of waveform can be implemented to control circuit like motor, fan, light bulb intensity etc. You can write the code in embedded C language and burn the hex file on 8051 microcontroller memory.

Code:

#include "REG52.h"
long int i,j,k;
void delay(int a)
{
for(i=0;i<=a;i++)
{
for(j=0;j<=500;j++);
}
}

void main()
{
while(1)
{
for(k=0;k<=10;k++)
{
P2=0xff;
delay(10);
P2=0x00;
delay(10-k);
}
for(k=255;k>=0;k--)
{
P2=0x00;
delay(k);
P2=0xff;
delay(10-k);
}
}
}

Explanation of code:

- In this we have implemented the delay to generate PWM waveform of varying width. You can make circuit of anything and generate PWM to control the motion or intensity of light bulb. In this code, we have implemented the for() loop from 0-10 and, in for() loop, make any port of 8051 ON/OFF with requesting some delay so that it can be paused for sometime and ON for few minutes. Hence, this concept produces the varying waveform.

Output on Logic Analyzer:

PWM waveform in Keil microvision4
This output is analyzed on Keil µvision4 logic analyzer. You can setup your pin on which output is taken and set the hexadecimal in setup window of logic analyzer.

Comments