#include <xc.h>
#include <math.h>

#define _XTAL_FREQ 16000000  // 16 MHz oscillator frequency

#define SINE_TABLE_SIZE 256

uint8_t phaseA_index = 0;
uint8_t phaseB_index = SINE_TABLE_SIZE / 3;
uint8_t phaseC_index = (2 * SINE_TABLE_SIZE) / 3;
volatile uint16_t amplitude;

const uint16_t sine_table[256]=
{384, 393, 403, 412, 422, 431, 440, 450, 459, 468, 477, 486, 495, 504, 513, 522, 531, 540, 548, 557, 565, 573, 581, 589, 597, 605, 613, 620, 628, 635, 642, 649, 656, 662, 669, 675, 681, 687, 692, 698, 703, 708, 713, 718, 723, 727, 731, 735, 739, 742, 746, 749, 751, 754, 756, 759, 761, 762, 764, 765, 766, 767, 768, 768,
768, 768, 768, 767, 766, 765, 764, 762, 761, 759, 756, 754, 751, 749, 746, 742, 739, 735, 731, 727, 723, 718, 713, 708, 703, 698, 692, 687, 681, 675, 669, 662, 656, 649, 642, 635, 628, 620, 613, 605, 597, 589, 581, 573, 565, 557, 548, 540, 531, 522, 513, 504, 495, 486, 477, 468, 459, 450, 440, 431, 422, 412, 403, 393,
384, 375, 365, 356, 346, 337, 328, 318, 309, 300, 291, 282, 273, 264, 255, 246, 237, 228, 220, 211, 203, 195, 187, 179, 171, 163, 155, 148, 140, 133, 126, 119, 112, 106, 99, 93, 87, 81, 76, 70, 65, 60, 55, 50, 45, 41, 37, 33, 29, 26, 22, 19, 17, 14, 12, 9, 7, 6, 4, 3, 2, 1, 0, 0,
0, 0, 0, 1, 2, 3, 4, 6, 7, 9, 12, 14, 17, 19, 22, 26, 29, 33, 37, 41, 45, 50, 55, 60, 65, 70, 76, 81, 87, 93, 99, 106, 112, 119, 126, 133, 140, 148, 155, 163, 171, 179, 187, 195, 203, 211, 220, 228, 237, 246, 255, 264, 273, 282, 291, 300, 309, 318, 328, 337, 346, 356, 365, 375,};

void update_pwm(void) 
{
    uint16_t dutyA, dutyB, dutyC;

    dutyA = ((uint32_t)sine_table[phaseA_index] * amplitude) / 768;
    dutyB = ((uint32_t)sine_table[phaseB_index] * amplitude) / 768;
    dutyC = ((uint32_t)sine_table[phaseC_index] * amplitude) / 768;

    // Set PWM duty cycles
    PWM1DCH = (dutyA >> 2) & 0xFF; // Adjusted to extract higher 8 bits
    PWM1DCL = (dutyA & 0x03) << 6; // Adjusted to extract lower 2 bits

    PWM2DCH = (dutyB >> 2) & 0xFF;
    PWM2DCL = (dutyB & 0x03) << 6;

    PWM3DCH = (dutyC >> 2) & 0xFF;
    PWM3DCL = (dutyC & 0x03) << 6;

    // Increment phase indexes
    phaseA_index = (phaseA_index + 1) % SINE_TABLE_SIZE;
    phaseB_index = (phaseB_index + 1) % SINE_TABLE_SIZE;
    phaseC_index = (phaseC_index + 1) % SINE_TABLE_SIZE;
}
    
volatile uint8_t tmr0_reload_value = 20;  // Initial TMR0 reload value

void init_adc(void)
{
    // Configure ADC
    ADCON1bits.ADFM = 1;       // Right justified
    ADCON1bits.ADCS = 0b110;   // FOSC/64
    ADCON1bits.ADPREF = 0;     // VREF+ = VDD
    ADCON0bits.CHS = 0b00000;  // Select AN0
    ADCON0bits.ADON = 1;       // Enable ADC
    __delay_us(10);            // Acquisition time
    
}

void main(void) {
    // Oscillator configuration
    OSCCONbits.IRCF = 0b1111; // 16 MHz internal oscillator
    OSCCONbits.SCS = 0b10;    // Internal oscillator
    
    // Configure analog inputs
    ANSELAbits.ANSA0 = 1; // AN0 as analog input
    ANSELAbits.ANSA1 = 1; // AN1 as analog input
    TRISAbits.TRISA0 = 1; // Set RA0 as input
    TRISAbits.TRISA1 = 1; // Set RA1 as input

    // Configure Timer2 for PWM
    T2CONbits.T2CKPS = 0b00;  // Prescaler = 1
    PR2 = 199;                 // PR2 value for 20 kHz PWM frequency
    T2CONbits.TMR2ON = 1;     // Enable Timer2

    // Configure PWM Modules
    
    PWM1CON = 0xC0;
    PWM2CON = 0xC0;
    PWM3CON = 0xC0;
    
    // Set PWM pins as outputs
    TRISCbits.TRISC5 = 0;
    TRISCbits.TRISC3 = 0;
    TRISAbits.TRISA2 = 0;

    // Configure Timer0 for updates
    OPTION_REGbits.T0CS = 0;    // Timer0 Clock Source: Internal
    OPTION_REGbits.PSA = 0;     // Prescaler assigned to Timer0
    OPTION_REGbits.PS = 0b100;  // Prescaler = 1:16

    TMR0 = 256 - 20;            // Preload TMR0

    // Enable Timer0 interrupt
    INTCONbits.TMR0IE = 1;      // Enable Timer0 interrupt
    INTCONbits.GIE = 1;         // Enable Global Interrupts

   init_adc();

    while (1) {
    // Select AN0 for frequency control
    ADCON0bits.CHS = 0b00000; // Select AN0
    __delay_us(10);           // Acquisition time (as per datasheet)
    
    // Start ADC conversion for frequency control
    ADCON0bits.GO_nDONE = 1;    
    while (ADCON0bits.GO_nDONE); // Wait for conversion to complete

    // Read ADC result for frequency control
    uint16_t adc_value = ((uint16_t)ADRESH << 8) | ADRESL;

    // Map ADC value to TMR0 reload value
    tmr0_reload_value = 16 + ((uint32_t)adc_value * 33) / 1023;

    // Clamp the reload value to the valid range
    if (tmr0_reload_value < 16) tmr0_reload_value = 16;
    if (tmr0_reload_value > 49) tmr0_reload_value = 49;

    __delay_ms(10);
    
    // Select AN1 for amplitude control
    ADCON0bits.CHS = 0b00001; // Select AN1
    __delay_us(10);           // Acquisition time
    
    // Start ADC conversion for amplitude control
    ADCON0bits.GO_nDONE = 1;    
    while (ADCON0bits.GO_nDONE); // Wait for conversion to complete

    // Read ADC result for amplitude control
    amplitude = ((uint16_t)ADRESH << 8) | ADRESL;

    // Map ADC value to amplitude scaling
    amplitude = (uint32_t)amplitude * 768 / 1023;

    // Clamp the amplitude value
    if (amplitude > 768) amplitude = 768;

    __delay_ms(10);
}
   
}

void __interrupt() ISR(void) {
    if (INTCONbits.TMR0IF) {
        INTCONbits.TMR0IF = 0; // Clear Timer0 interrupt flag

        // Reload TMR0
        TMR0 = 256 - tmr0_reload_value;

        // Update PWM outputs
        update_pwm();
    }
}
no output on this code find potential erors and optimize the code