/*
 * main.c
 *
 *  Created on: 09-Jan-2024
 *      Author: 39138
 */

#include "F28x_Project.h"
#include "device.h"
#include "math.h"
#include "Peripherals.h"

#include "Main.h"

//#include "stdint.h"

//uint16_t au16adcAResults[BUFFERSIZE] ={0,};
STR_CHECK strCheck;
float32_t gf32OmegaT;
float32_t gf32Sine;
float32_t gf32Sine_DAC;
float32_t gf32ModulationIndex = 1.0;
uint16_t gu16DAC_value;
int16_t gi16DAC_value;
volatile uint16_t gu16ADC_temp;
volatile int16_t gi16ADC_temp;
volatile int32_t gi32ADC_temp;
volatile float32_t gf32ADC_temp;
volatile float32_t gf32ADC_temp1;
volatile uint16_t temp;
int flag =0;
int16_t au16adcAResults[BUFFERSIZE] ={0.};
int32_t ai32LogPresent[BUFFERSIZE] ={0,};
int32_t ai32LogPrev[BUFFERSIZE] ={0,};
int16_t ai16LogTemp[BUFFERSIZE] ={0,};
//float32_t af32LogFloat[BUFFERSIZE] ={0,};

void main(void)
{
    PeripheralsInit();
    System_Initial();
    //flag = 1;
    while(1){}
}

interrupt void Main_ISR(void)
{
    //flag = 2;
    static uint16_t u16index = 0;


    //DAC_CODE........GENERATE_SINE_WAVE
    if(gf32OmegaT>=2.0*PI_VALUE)
    {
       gf32OmegaT-=(2.0*PI_VALUE);
    }
    else
    {
        gf32OmegaT += (OMEGA*PLL_SAMPLING);
    }

    gf32Sine                = gf32ModulationIndex * sin(gf32OmegaT);
    gf32Sine_DAC            = 0.5 + (gf32Sine* 0.5);
    gu16DAC_value           = gf32Sine_DAC * ADC_VALUE;
    DacbRegs.DACVALS.all    = gu16DAC_value;
    gi16DAC_value           = gu16DAC_value - 2048;


    //ADC_CHECK_________________________________________


    gi16ADC_temp                        = AdccResultRegs.ADCRESULT0;
    strCheck.strinput.i16Prev_Sample    = AdccResultRegs.ADCRESULT1;
    strCheck.strinput.i16Present_Sample = gi16ADC_temp -  strCheck.strinput.i16Prev_Sample;
//    strCheck.strinput.i32Second_Sample = -1;
    strCheck.strinput.i32Second_Sample  = (int32_t)strCheck.strinput.i16Present_Sample;

    if ((strCheck.strinput.i32Second_Sample > 65000) && (strCheck.strinput.i32Second_Sample < -65000))
    {
        strCheck.strOutput.i32Present_Sample = (strCheck.strinput.i32Second_Sample ^ 0xFFFFFFFF);
    }
    else
    {
        strCheck.strOutput.i32Present_Sample  = strCheck.strinput.i32Second_Sample;

    }
if (u16index < BUFFERSIZE)
{
    u16index++;
}
else
{
    u16index = 0;
}

    AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;  //CLEAR FLAG
    //
    // Acknowledge the interrupt
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
void System_Initial(void)
{
    strCheck.strCalibrate.i32Present_Sample =1024;
    strCheck.strCalibrate.i32Prev_Sample = 1024;
}




