PWM WAVE GENRATORE

#include<reg51.h>
#include<stdio.h>
#include"lcd.h"
sbit Add_A=P0^7; // Address pins for selecting input channels.
sbit Add_B=P0^6;
sbit Add_C=P0^5;

sbit ALE=P0^4; //address latch enable
sbit START=P0^0; //start conversion
sbit EOC=P0^1; //end of conversion
sbit OE=P0^2; //output enable
sbit CLK=P0^3; // clock
sbit Increment=P3^0;
sbit Decrement=P3^1;
sbit Enter=P3^2;
sbit pwm=P3^4;

#define Data_Bus P2
#define Half Cycle Delay 10 // usecs
#define AN0 0
#define AN1 1
#define AN2 2
#define AN3 3
#define AN4 4
#define AN5 5
#define AN6 6
#define AN7 7

float voltage=0;
unsigned char volt=0;
unsigned int Ton=0;
unsigned int Toff=0; // Function Declarations
void Init ADC(void);
unsigned int ReadADC(unsigned char);
void __delay_us(unsigned int );
void Duty_Delay();
void PWM(unsigned char x);
void main()
{
unsigned char ADC_Value = 0; // To capture ADC value
unsigned int i=0;
InitADC(); // Initialize ADC
LCD_INIT();
LCD_COMMAND(0x01);
LCD_STRING("POWER CONVERTOR ");
LCD_COMMAND(0xC0);
LCD_STRING(" CONTROL SYSTEM ");
for(i=0;i<60;i++)
DELAY();
for(i=0;i<50;i++)
DELAY();
LCD_COMMAND(0x01);
LCD_STRING(" VOLTAGE IS ");
ADC_Value = ReadADC(AN0); // Read ADC value from Channel 0
voltage=(ADC_Value);
voltage=(voltage*33/255);
LCD_COMMAND(0xC0);
LCD_FLOAT(voltage);
LCD_STRING(" VOLT ");
for(i=0;i<50;i++)
DELAY();
while(1)
{
for(i=0;i<50;i++)
DELAY();
LCD_COMMAND(0x01);
LCD_STRING(" VOLTAGE IS ");
ADC_Value = ReadADC(AN0); // Read ADC value from Channel 0
voltage=(ADC_Value);
voltage=(voltage*33/255);
LCD_COMMAND(0xC0);
LCD_FLOAT(voltage);
LCD_STRING(" VOLT ");
for(i=0;i<50;i++)
DELAY();
if(Increment==0) //Check if Increment pin is pressed
{
volt=0;
while(Enter!=0) //loop untill enter is pressed
{
if(Increment==0)
{
++volt;
if(volt>10)
volt=10;
LCD_COMMAND(0x01);
LCD_COMMAND(0x80);
LCD_STRING(" VOLTAGE IS ");
LCD_COMMAND(0xC0);
LCD_INT(volt*3);
LCD_STRING(" VOLT ");
}
if(Decrement==0)
{
--volt;
if(volt<1)
volt=1;
LCD_COMMAND(0x01);
LCD_COMMAND(0x80);
LCD_STRING(" VOLTAGE IS ");
LCD_COMMAND(0xC0);
LCD_INT(volt*3);
LCD_STRING(" VOLT ");
}

} // end of while(Enter!=0)
LCD_COMMAND(0xC0);
LCD_STRING(" ZERO ");
//PWM(volt*10); //volt=0;
} // end of if(increment==0)
if(Decrement==0) //Check if Decrement pin is pressed
{
volt=0;
while(Enter!=0) //loop untill enter is pressed
{
if(Decrement==0)
{
if(volt<=1)
volt=1;
else
--volt;
LCD_COMMAND(0x01);
LCD_COMMAND(0x80);
LCD_STRING(" VOLTAGE IS ");
LCD_COMMAND(0xC0);
LCD_INT(volt*3);
LCD_STRING(" VOLT ");
}
if(Increment==0)
{
++volt;
if(volt>10)
volt=10;
LCD_COMMAND(0x01);
LCD_COMMAND(0x80);
LCD_STRING(" VOLTAGE IS ");
LCD_COMMAND(0xC0);
LCD_INT(volt*3);
LCD_STRING(" VOLT ");
}
} // end of while(Enter!=0)
LCD_COMMAND(0xC0);
LCD_STRING(" ZERO "); // PWM(volt*10); //volt=0;
} //end of if(decrement==0)
PWM(volt);
} //end of while(1)
} //end of main()


void PWM(unsigned char x) //400HZ=2500us Duty cycle=Ton/Ton+Toff*100
{
unsigned char i=0;
if(x==1)
{
pwm=1;
__delay_us(100);
pwm=0;
__delay_us(900);
}
if(x==2)
{
pwm=1;
__delay_us(200);
pwm=0;
__delay_us(800);
}
if(x==3)
{
pwm=1;
__delay_us(300);
pwm=0;
__delay_us(700);
}
if(x==4)
{
pwm=1;
__delay_us(400);
pwm=0;
__delay_us(600);
}
if(x==5)
{
pwm=1;
__delay_us(500);
pwm=0;
__delay_us(500);
}
if(x==6)
{
pwm=1;
__delay_us(600);
pwm=0;
__delay_us(400);
}
if(x==7)
{
pwm=1;
__delay_us(700);
pwm=0;
__delay_us(300);
}
if(x==8)
{
pwm=1;
__delay_us(800);
pwm=0;
__delay_us(200);
}
if(x==9)
{
pwm=1;
__delay_us(900);
pwm=0;
__delay_us(100);
}
if(x==10)
{
pwm=1;
__delay_us(1000);
}
else
pwm=0;
}

void InitADC(void)
{
Add_A = 0; // Make output
Add_B = 0; // Make output
Add_C = 0; // Make output
ALE = 0; // Make output
EOC = 1; // Make output
OE = 0; // Make output
START = 0; // Make output
CLK = 0; // Make output

Data_Bus = 0xFF; // Make Inputs
}

unsigned int ReadADC(unsigned char Channel)
{
unsigned int i = 0;
unsigned int ADC_value = 0;

// Select Channel
switch(Channel)
{
case AN0: Add_C = 0; Add_B = 0; Add_A = 0; break;
case AN1: Add_C = 0; Add_B = 0; Add_A = 1; break;
case AN2: Add_C = 0; Add_B = 1; Add_A = 0; break;
case AN3: Add_C = 0; Add_B = 1; Add_A = 1; break;
case AN4: Add_C = 1; Add_B = 0; Add_A = 0; break;
case AN5: Add_C = 1; Add_B = 0; Add_A = 1; break;
case AN6: Add_C = 1; Add_B = 1; Add_A = 0; break;
case AN7: Add_C = 1; Add_B = 1; Add_A = 1; break;
}

__delay_us(HalfCycleDelay); // 250kHz Frequency
ALE = 1; // Enable Address Latch
CLK = 1; // Make CLK High
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 0; // Make CLK Low
START = 1; // Start ADC Conversion
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 1; // Make CLK High
ALE = 0; // Disable Address Latch
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 0; // Make CLK Low
START = 0; // Complete the start pulse

for(i=0;i<2000;i++)
{
CLK = !CLK; // Toggle Clock
__delay_us(HalfCycleDelay); // 250kHz Frequency

if(!EOC) // Wait for EOC to be low
break;
}

for(i=0;i<2000;i++)
{
CLK = !CLK; // Toggle Clock
__delay_us(HalfCycleDelay); // 250kHz Frequency

if(EOC) // Wait for EOC to be High
break;
}

CLK = 0; // Make CLK Low
OE = 1; // Enable Output
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 1; // Make CLK High
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 0; // Make CLK Low
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 1; // Make CLK High

ADC_value = Data_Bus; // Read value

__delay_us(HalfCycleDelay); // 250kHz Frequency
OE = 0; // Disable Output
CLK = 0; // Make CLK Low
__delay_us(HalfCycleDelay); // 250kHz Frequency

return ADC_value; // Return ADC value
}

void __delay_us(unsigned int d)
{
unsigned int i, limit;
limit = d/15;
for(i=0;i<limit;i++);
}

void DELAY()
{
int k;
for(k=0;k<10000;k++);
}

void Duty_Delay()
{
int k;
for(k=0;k<5000;k++);
}