Update

One new tab added. Open in browser view if it is not visible. (25/08/2022 08:48)

Controlling Output of a DC Motor by PWM using a timer in 8051

PWM or Pulse width modulation is a way of controlling output DC voltage of a pin by setting a duty cycle. Duty cycle means the time for which the pulse is in high state compared to the total time of the pulse.
For example, a signal (10101010) has 50% duty cycle, because the pulse remains high for 1/2 of the period or low for 1/2 of the period.
So if the pulse is high for 100% of the time then the speed shall be maximum and if it is low for 100% of the time then the motor should come to a halt.

What I have done in this code is, Kept the duration of the low signal as constant and then calculated the time for which signal should be high.
Here, the pulse is low for 200 micro seconds. So to have a 50% duty cycle, the time for which the pulse should go high should also be 200 micro seconds.
Similarly to have a 60% duty cycle, time for which pulse should be high will be,
x/x+200    =    60/100
hence x=300 micro seconds.


// VARY SPEED OF DC MOTOR USING PWM
#include<reg51.h>
/*
8051 MICROCONTROLLER
DC MOTOR
10 SPST PUSH BUTTON SWITCHES TO CONTROL THE SPEED USING PWM
*/
sbit v=P1^0; //Output DC Voltage varied using pwm, where pulse width controlled by timer 0
// SWITCHES
sbit set0=P2^0;
sbit set10=P2^1;
sbit set20=P2^2;
sbit set30=P2^3;
sbit set40=P2^4;
sbit set50=P2^5;
sbit set60=P2^6;
sbit set70=P2^7;
sbit set80=P1^1;
sbit set90=P1^2;
sbit set100=P1^3;
void low()
{
v=0;
TH0=0XFF;
TL0=0X47;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;


}
void high(char h,char l)
{
v=1;
TH0=h;
TL0=l;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;

low();
}
void main()
{
TMOD=0X01;// 16 BIT TIMER

P2=0xFF; // ALL PINS SET HIGH
P1=0X0F; // 4 PINS SET HIGH
if(set0==0) high(0XFF,0XFF);
else if(set10==0) high(0XFF,0XE9);
else if(set20==0) high(0XFF,0XE6);
else if(set30==0) high(0XFF,0XAA);
else if(set40==0) high(0XFF,0X7A);
else if(set50==0) high(0XFF,0X47);
else if(set60==0) high(0XFE,0XD3);
else if(set70==0) high(0XFE,0X2C);
else if(set80==0) high(0XFC,0XDF);
else if(set90==0) high(0XF8,0XF7);
else high(0XF0,0x5F);

}

Simulation



No comments: