/*
 * File: main.c
 * Example of using a 16-bit tuning word for a simple DDS (Direct Digital Synthesis)
 * on a PIC16F1503 with minimal float usage to reduce memory overhead.
 *
 * The code demonstrates:
 *  1. 16-bit phase accumulator (phAcc).
 *  2. 16-bit tuning word calculation using purely integer math.
 *  3. A 256-sample sine lookup table.
 *  4. 3-phase output using 3 PWM channels if available (PWM1, PWM2, PWM3).
 *
 * NOTE:
 *  - Adjust oscillator settings, #pragma config, and #defines for your hardware.
 *  - This example uses minimal float usage only for an optional setFrequency16Bit()
 *    function. If needed, you can remove or replace it with completely integer-based math.
 */

/*----------------------------------------------------------------------------*/
/* PIC16F1503 Configuration Bit Settings                                      */
/*----------------------------------------------------------------------------*/
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator: INTOSC
#pragma config WDTE = OFF       // Watchdog Timer Enable: off
#pragma config PWRTE = OFF      // Power-up Timer Enable: off
#pragma config MCLRE = OFF      // MCLR Pin Function: MCLR off (digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection: off
#pragma config BOREN = OFF      // Brown-out Reset Enable: off
#pragma config CLKOUTEN = OFF   // Clock Out Enable: off

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection: off
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable: off
#pragma config BORV = LO        // Brown-out Reset Voltage: low trip point
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset: off
#pragma config LVP = OFF        // Low-Voltage Programming Enable: off

#include <xc.h>
#include <stdint.h>

/*----------------------------------------------------------------------------*/
/* Defines                                                                     */
/*----------------------------------------------------------------------------*/
#define _XTAL_FREQ      16000000UL  // 16 MHz internal oscillator
#define SAMPLE_RATE     17085UL     // Approx. PWM or interrupt frequency

/*----------------------------------------------------------------------------*/
/* DDS Global Variables                                                       */
/*----------------------------------------------------------------------------*/


static volatile uint16_t phAcc = 0;

static volatile uint16_t tuningWord = 0;

static volatile uint8_t amplitude = 255;


static const uint8_t sine_LUT[256] = {
    0x7f, 0x82, 0x84, 0x87, 0x89, 0x8c, 0x8e, 0x91,
    0x93, 0x96, 0x98, 0x9b, 0x9d, 0xa0, 0xa2, 0xa5,
    0xa7, 0xaa, 0xac, 0xae, 0xb1, 0xb3, 0xb5, 0xb8,
    0xba, 0xbc, 0xbf, 0xc1, 0xc3, 0xc5, 0xc7, 0xc9,
    0xcb, 0xcd, 0xcf, 0xd1, 0xd3, 0xd5, 0xd7, 0xd9,
    0xdb, 0xdc, 0xde, 0xe0, 0xe1, 0xe3, 0xe5, 0xe6,
    0xe8, 0xe9, 0xea, 0xec, 0xed, 0xee, 0xef, 0xf1,
    0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf7, 0xf8,
    0xf9, 0xfa, 0xfa, 0xfb, 0xfb, 0xfc, 0xfc, 0xfd,
    0xfd, 0xfd, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe,
    0xfe, 0xfe, 0xfe, 0xfd, 0xfd, 0xfd, 0xfc, 0xfc,
    0xfb, 0xfb, 0xfa, 0xfa, 0xf9, 0xf8, 0xf7, 0xf7,
    0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xef, 0xee,
    0xed, 0xec, 0xea, 0xe9, 0xe8, 0xe6, 0xe5, 0xe3,
    0xe1, 0xe0, 0xde, 0xdc, 0xdb, 0xd9, 0xd7, 0xd5,
    0xd3, 0xd1, 0xcf, 0xcd, 0xcb, 0xc9, 0xc7, 0xc5,
    0xc3, 0xc1, 0xbf, 0xbc, 0xba, 0xb8, 0xb5, 0xb3,
    0xb1, 0xae, 0xac, 0xaa, 0xa7, 0xa5, 0xa2, 0xa0,
    0x9d, 0x9b, 0x98, 0x96, 0x93, 0x91, 0x8e, 0x8c,
    0x89, 0x87, 0x84, 0x82, 0x7f, 0x7c, 0x7a, 0x77,
    0x75, 0x72, 0x70, 0x6d, 0x6b, 0x68, 0x66, 0x63,
    0x61, 0x5e, 0x5c, 0x59, 0x57, 0x54, 0x52, 0x50,
    0x4d, 0x4b, 0x49, 0x46, 0x44, 0x42, 0x3f, 0x3d,
    0x3b, 0x39, 0x37, 0x35, 0x33, 0x31, 0x2f, 0x2d,
    0x2b, 0x29, 0x27, 0x25, 0x23, 0x22, 0x20, 0x1e,
    0x1d, 0x1b, 0x19, 0x18, 0x16, 0x15, 0x14, 0x12,
    0x11, 0x10, 0x0f, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
    0x08, 0x07, 0x07, 0x06, 0x05, 0x04, 0x04, 0x03,
    0x03, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
    0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04,
    0x05, 0x06, 0x07, 0x07, 0x08, 0x09, 0x0a, 0x0b,
};

void init_osc(void);
void setFrequency16Bit(uint16_t freq);
void init_pwm(void);

void main(void)

{
    // Initialize the internal oscillator
    init_osc();
    
    // Initialize PWM
    init_pwm();
    
    // Example: Set initial frequency to 180 Hz
    setFrequency16Bit(160);
     
    // Enable global interrupts
    INTCONbits.PEIE = 1;  // Peripheral Interrupt Enable
    INTCONbits.GIE  = 1;  // Global Interrupt Enable
    
    uint16_t lastFreq = 0;  // Track last frequency to avoid unnecessary updates
    // Main loop
    while (1)
    {
       
    }
}

void init_osc(void)

{
    // Set internal oscillator to 16 MHz
    OSCCONbits.IRCF = 0b1111; // 16 MHz
    OSCCONbits.SCS  = 0b10;   // Internal oscillator
}

void setFrequency16Bit(uint16_t freqHz) {
    // Improved tuning word calculation
    uint32_t temp = (uint32_t)freqHz * 65536UL;
    tuningWord = (uint16_t)(temp / SAMPLE_RATE);
}

void init_pwm(void)

{
    // Configure Timer2 for PWM
    // Prescaler = 1, PR2 adjusts PWM frequency
    T2CONbits.T2CKPS = 0b00;  // Prescaler = 1
    PR2 = 220;                // ~20 kHz if FOSC=16 MHz (approx)
    T2CONbits.TMR2ON = 1;     // Enable Timer2
    
    // Configure PWM modules (depending on your PIC)
    PWM1CON = 0xC0;  // Enable PWM1, active-high
    PWM2CON = 0xC0;  // Enable PWM2, active-high
    PWM3CON = 0xC0;  // Enable PWM3, active-high
    
    // Make sure PWM pins are outputs
    TRISCbits.TRISC5 = 0;  // PWM1 output
    TRISCbits.TRISC3 = 0;  // PWM2 output
    TRISAbits.TRISA2 = 0;  // PWM3 output
    
    // Enable Timer2 interrupt for the DDS update
    PIR1bits.TMR2IF = 0;
    PIE1bits.TMR2IE = 1;
}

void __interrupt() isr(void)

{
    // Timer2 interrupt
    if (PIE1bits.TMR2IE && PIR1bits.TMR2IF)
    {
        PIR1bits.TMR2IF = 0;
        
        // Increment phase accumulator by tuningWord
        phAcc += tuningWord;
        
        // Top 8 bits of phase accumulator => LUT index
        // phAcc is 16 bits, so (phAcc >> 8) = top 8 bits
        uint8_t indexA = (uint8_t)(phAcc >> 8);
        
        // For 3-phase, add an offset for indexB (120°) and indexC (240°).
        // 256/3 = 85.3 ~ 85 for 120°, 170 for 240°.
        uint8_t indexB = (uint8_t)(indexA + 85);
        uint8_t indexC = (uint8_t)(indexA + 170);
        
        // Retrieve samples from the LUT
        uint8_t sampA = sine_LUT[indexA];
        uint8_t sampB = sine_LUT[indexB];
        uint8_t sampC = sine_LUT[indexC];
        
        // Scale by amplitude (0..255)
        sampA = (uint8_t)(((uint16_t)sampA * amplitude) >> 8);
        sampB = (uint8_t)(((uint16_t)sampB * amplitude) >> 8);
        sampC = (uint8_t)(((uint16_t)sampC * amplitude) >> 8);
        
        // Update PWM duty cycles (8-bit duty high register for each PWM)
        PWM1DCH = sampA;
        PWM2DCH = sampB;
        PWM3DCH = sampC;
    }
}


 add adc for this code to varying frequency between 60 to 160