unsigned int duty_cycle;
unsigned int adc_val;
void InitMain() {
ADCON1 = 0xfe; // All ADC pin 1 to on
TRISA = 0x01; // PORTA is input
TRISB = 0x00 ;
TRISC = 0x3F; // Pins RC7, RC6 are outputs
PORTC = 0; // Set PORTC to
Pwm_Init(8000); // Initialize PWM module
// Timer 2
PR2=62; // CALBIRATED THIS BY CHANGIN IT TO 62?TAE
T2CON = (1<<TMR2ON);
// Initialize Control PWM
CCPR1L = 50; // Initial Duty
CCP1CON = 0x0f; // PWM mode set and 5,4 duty = 0
}
void main() {
InitMain();
Pwm_Start(); // Start PWM
while (2)
{
adc_val = adc_read(0) ;
portb = adc_val ;
portc = adc_val >> 2; // Send 2 most significant bits to Rc7, Rc6
adc_val = adc_read(0); //read analog input RA0
duty_cycle=adc_val*64; // CALIBRATED IT BY CHANGIN 100 TO 64
duty_cycle/=1024;
CCPR1L = duty_cycle;
}
}