Making PWM with analog input control in PIC18F4550
With the following code I could make it work    

#define TMR2PRESCALE 16
long PWM_freq = 8000;

void PWM_Initialize() 
{
   PR2 = ("%#x", (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1); //Setting the PR2 formulae using Datasheet 
   CCPR1L = 0x1F;
   TRISCbits.RC1 = 0; 
   TRISCbits.RC2 = 0;
   T2CON = 0x03;   
   CCP1CON = 0x0C;
   TMR2 = 0;
   T2CONbits.TMR2ON = 1; 
}

void PWM_Duty(unsigned int duty)
{
  if(duty &lt; 1023)
  {
      duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE));
      CCPR1L = duty&gt;&gt;2;
  }
}

void ADC_Initialize()
{    
   ADCON1bits.PCFG = 0;
   ADCON1bits.VCFG = 0;
   ADCON0 = 0;
   ADCON2bits.ACQT = 3;
   ADCON2bits.ADCS = 5;
   ADCON2bits.ADFM = 1;
   ADCON0bits.ADON = 1;
}

unsigned int ADC_Read(unsigned char ch)
{
   if(ch &gt; 13)return 0;
   ADCON0 = 0;
   ADCON0 = (ch&lt;&lt;2);
   ADCON0bits.ADON = 1;
   ADCON0bits.GO_DONE = 1;
   while(ADCON0bits.GO_DONE == 1);
   ADCON0bits.ADON = 0;
   return ADRES;
}

void main()
{    
   int adc_value;  
   TRISC = 0;
   TRISA = 1;
   TRISD = 0; 
   ADC_Initialize();
   PWM_Initialize();

   do
   {
       adc_value = ADC_Read(0);
       PWM_Duty(adc_value);    
       __delay_ms(50);       
   }
   while(1); //Infinite Loop  
}