#include <xc.h>
#include <stdint.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 uint8_t tmr0_reload_value = 20;  // Initial TMR0 reload value

volatile uint16_t dutyA = 0;
volatile uint16_t dutyB = 0;
volatile uint16_t dutyC = 0;

volatile uint16_t amplitude_scaling = 768;  // Initialized to maximum amplitude

const uint16_t sine_table[SINE_TABLE_SIZE] = {
    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) 
{
    // Set PWM duty cycles (assuming 10-bit resolution)
    PWM1DCH = (dutyA >> 2) & 0xFF;
    PWM1DCL = (dutyA & 0x0003) << 6;

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

    PWM3DCH = (dutyC >> 2) & 0xFF;
    PWM3DCL = (dutyC & 0x0003) << 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;
}

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 Timer2 for PWM
    T2CONbits.T2CKPS = 0b00;  // Prescaler = 1
    PR2 = 199;                // PR2 value for 20 kHz PWM frequency (adjust as needed)
    T2CONbits.TMR2ON = 1;     // Enable Timer2

    // Configure PWM Modules
    PWM1CON = 0xC0;  // Enable PWM1, active-high
    PWM2CON = 0xC0;  // Enable PWM2, active-high
    PWM3CON = 0xC0;  // Enable PWM3, active-high

    // Set PWM pins as outputs
    TRISCbits.TRISC5 = 0;  // PWM1 output
    TRISCbits.TRISC3 = 0;  // PWM2 output
    TRISAbits.TRISA2 = 0;  // PWM3 output

    // 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:32

    TMR0 = 256 - tmr0_reload_value;  // Preload TMR0

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

    // Initialize ADC
    init_adc();

    while (1) {
        // *** Frequency Control via AN0 ***
        // Read AN0 for Timer0 reload value (frequency control)
        ADCON0bits.CHS = 0b00000;  // Select AN0
        __delay_us(10);            // Acquisition time
        ADCON0bits.GO_nDONE = 1;   
        while (ADCON0bits.GO_nDONE); // Wait for conversion to complete
        uint16_t adc_value_tmr = ((uint16_t)ADRESH << 8) | ADRESL;

        // Map AN0 ADC value to TMR0 reload value (adjust as needed)
        tmr0_reload_value = 16 + ((uint32_t)adc_value_tmr * 33) / 1023;
        if (tmr0_reload_value < 16) tmr0_reload_value = 16;
        if (tmr0_reload_value > 49) tmr0_reload_value = 49;

        // *** Amplitude Control via AN1 ***
        // Read AN1 for Amplitude Control
        ADCON0bits.CHS = 0b00001;  // Select AN1
        __delay_us(10);            // Acquisition time
        ADCON0bits.GO_nDONE = 1;   
        while (ADCON0bits.GO_nDONE); // Wait for conversion to complete
        uint16_t adc_value_amp = ((uint16_t)ADRESH << 8) | ADRESL;

        // Set amplitude scaling factor (0 - 1023)
        amplitude_scaling = adc_value_amp;
        if (amplitude_scaling > 768) amplitude_scaling = 768;

        // *** Compute Scaled Duty Cycles ***
        // Get the raw sine values
        uint16_t raw_dutyA = sine_table[phaseA_index];
        uint16_t raw_dutyB = sine_table[phaseB_index];
        uint16_t raw_dutyC = sine_table[phaseC_index];

        // Scale the amplitude with 32-bit arithmetic
        dutyA = (uint16_t)(((uint32_t)raw_dutyA * amplitude_scaling) / 768);
        dutyB = (uint16_t)(((uint32_t)raw_dutyB * amplitude_scaling) / 768);
        dutyC = (uint16_t)(((uint32_t)raw_dutyC * amplitude_scaling) / 768);

        // Ensure duty cycles do not exceed maximum PWM value
        if (dutyA > 1023) dutyA = 768;
        if (dutyB > 1023) dutyB = 768;
        if (dutyC > 1023) dutyC = 768;
    }
}

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

        // Reload TMR0
        TMR0 = 256 - tmr0_reload_value;

        // Update PWM outputs with precomputed values
        update_pwm();
    }
}