#include <htc.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 20000000
#endif
void adc_init(void)
{
TRISA = 0xFF; //PortA as Input
TRISB = 0x00; //PortB as Outputs
PORTB = 0x00; //Initialise
ADCON1 = 0b00000000; //left justified,all analog
ADCON0 = 0b10100001; //Fosc/16 ; pin5 as analog in ;
}
void main(void)
{
adc_init();
/////////////////////////setting up the pwm next for ccp1
TRISC = 0x00; // set PORTC as output
PORTC = 0x00; // clear PORTC
PR2 = 0b10011011 ; //period
T2CON = 0b00000111 ; //timer2 on,prescaler 16
CCP1CON = 0b00111100 ; //pwm enable
//////////////////////////going into loop to refresh adc value as well as duty cycle
while(1)
{
__delay_ms(2); //wait
GODONE=1; /* enable A2D converter */
while(GODONE); //wait for conversion to complete
CCPR1L=ADRESH; //update duty cycle from adc value
}
}