Microcontroller › 8051 › GENERATING VARIABLE PWM › Try the code and update the
Try the code and update the result,
#include <reg51.h>
sbit INCREASE = P2^6;
sbit DECREASE = P2^7;
sbit MTR = P1^0;
void MSDelay(int value);
void main()
{
unsigned int on,off;
on=20; //initial value of ON time delay
off=0; //initial value of OFF time delay
while (1)
{
while(ONE==1 && TWO==1) //when both keys are open
{
//—-generating PWM
//
MTR=1;
MSDelay(on); //applying ON time delay
MTR=0;
MSDelay(off); //applying OFF time delay
//
//
}
//—when any of the key is down, exit from while loop and check which key is down–//
if(INCREASE==0 && on<20) //if key increase is down check whether it has max value or not
{
on++;
off–; //—-increment ON time delay if it is not maximum–//
//
//
}
else if (TWO==0 && on>0) // if key decrease is down check whether it ON time delay >0//
{
on–;
off++; //—-decrement ON time delay if it is not zero—-//
//
//
//***********************************//
//ADD DELAY HERE
MSDelay(1000);
}
}
}
void MSDelay(int value) // Delay function //
{
int x;
for(x=0;x<=value;x++);
}