DC Motor Control using PWM with ADC

Requirements:
  1. AT89S52/AT89C51 micro controller
  2. ADC0804 ADC chip
  3. Preset (10K)
  4. 555 timer (with apt. resistor and capacitor values)
  5. 7805 IC
  6. L293D IC
  7. DC motor
Description:
This project involves a combination of PWM (pulse width modulation) and ADC (Analog-Digital converter) to drive a DC motor at various speeds and in both clock and anti directions.
PWM is a concept with which one can modify the pulse voltage varying it from 0% duty cycle to 100%.Duty cycle represents the time for which pulse is high when compared to the full pulse length.
This means for a 25% one, you’ll we get 25% of the input voltage and the criterion is same for the rest of them. Now in the coding part I have made the changes in the duty cycle in accordance with the digital output I get from the ADC. The ADC is connected to my microcontroller at PORT1. The preset which I have used here to send the analog input can be replaced by a “joystick” with inbuilt preset to make it more realistic and interesting to operate.



#include<reg51.h>
#include<delay.h>
sbit R=P3^0;
sbit WD=P3^1;
sbit INT=P3^2;
sbit n=P2^1;
sbit p=P2^6;
int read_ADC()
{
        int temp;
                       R=1;
                       WD=0;
                       WD=1;
                       while(INT==1);
                       R=1;
                       R=0;
                       temp=P1;
            return temp;
}
void main()
{
           unsigned char value=128;
           INT=1;
           P1=0xff;
           value=read_ADC();
           while(1)
            {
                        if(value>125 && value<130)
                                   {n=0;p=0;}
                        else if(value<=125)
                                    {
                                              n=0;p=1;
                                              delay_msec(6-(value/25));
                                              n=0;p=0;
                                             delay_msec(value/25);
                                    }
                        else if(value>=130)
                                    {
                                               value=255-value;
                                                n=1;p=0;
                                                delay_msec(6-(value/25));
                                                n=0;p=0;
                                                delay_msec(value/25);
                                    }
                        value=read_ADC();
           }          
}