/**
 * ---------------------------------------------------------------------------------------
 * ---------------------------------------------------------------------------------------
 * 功能描述：
 * 	   产生变周期三角波
 * 	   ADC 0-3V
 * 	   DAC 0-3V
 * ---------------------------------------------------------------------------------------
 * 时间：2020-10-26
 * ---------------------------------------------------------------------------------------
 */

#include "F28x_Project.h"
#include "SciStdio.h"
#include<math.h>
#include<stdio.h>
#include"Adc.h"


// 初始化 DAC 寄存器指针
volatile struct DAC_REGS* DAC_PTR[4] = {0x0,&DacaRegs,&DacbRegs,&DaccRegs};

// 定义 ADC 参考的选择
#define REFERENCE_VDAC		0
#define REFERENCE_VREF		1

// 定义 DAC 通道
#define DACA				1
#define DACB				2
#define DACC				3


#define REFERENCE			REFERENCE_VREF
#define DAC_NUM				DACA
float dacval = 0;

float32 a1[1024];
unsigned int n=0;
// 存储ADC结果寄存器
//Uint16 AdcaResult[6];
Uint16 AdcbResult[1];
//Uint16 AdccResult[6];
//Uint16 AdcdResult[6];

// 存储ADC实际电压值
//float Adca_V[6];
float Adcb_V[1];
//float Adcc_V[6];
//float Adcd_V[6];
#define DATA_SIZE 256
Uint16 DATA_BUF[DATA_SIZE]; //一个周期采样点
Uint16 resultsIndex;
volatile Uint16 bufferFull;

// 函数声明
void configureDAC(Uint16 dac_num);
void test();
void ConfigureEPWM(void);

__interrupt void cpu_timer0_isr(void);
__interrupt void adcb_isr(void);

void main(void)
{
    float T=7.2;  //设置DAC锯齿波周期为2.4ms

    //double step = 0;
	// 系统初始化，配置PLL，关闭看门狗，使能外设时钟

    test();

    InitSysCtrl();

    // 关中断
    DINT;

    // 初始化Pie控制寄存器
    InitPieCtrl();

    // 不使能中断，清中断标志
    IER = 0x0000;
    IFR = 0x0000;

    // 初始化中断向量表
    InitPieVectTable();

    // 配置DAC输出
    configureDAC(DAC_NUM);

    //注册中断服务函数
    EALLOW;
    PieVectTable.TIMER0_INT = &cpu_timer0_isr;
    PieVectTable.ADCB_EVT_INT = &adcb_isr;
    EDIS;

    //初始化CPU定时器
    InitCpuTimers();

    // 配置CPU的时钟  T*(float)(10^(3)/3072
    // 200MHz CPU Freq, 1 second Period (in uSeconds)
    ConfigCpuTimer(&CpuTimer0, 200,T*(float)1000/1024);
    CpuTimer0Regs.TCR.all = 0x4000;   //启动定时器

    // 配置 ADCs 及 使能 ADC power
    ConfigureADC();

    ConfigureEPWM();  //配置EPWM

    // 设置 ADCs 转换序列
    SetupADCSoftware();

    //使能对应的中断
    IER |= M_INT1;
    IER |= M_INT13;
    IER |= M_INT14;
    //使能全局中断和实时调试中断
    EINT;
    ERTM;

    //使能中断向量表对应的中断
    PieCtrlRegs.PIEIER1.bit.INTx7 = 1;  //使能定时器中断

    //Initialize results buffer
    for(resultsIndex = 0;resultsIndex < DATA_SIZE;resultsIndex++)
    {
        DATA_BUF[resultsIndex] = 0;
    }
    resultsIndex = 0;
    bufferFull = 0;

    //enable PIE interrupt ADC_B
    PieCtrlRegs.PIEIER1.bit.INTx2 = 1;

    //sync ePWM
    EALLOW;
    CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1;

    // 初始化串口
    //SCIStdioInit();
    //
    //take conversions indefinitely in loop
    //
    do
       {
           //
           //start ePWM
           //
           EPwm1Regs.ETSEL.bit.SOCAEN= 1;  //enable SOCA
           EPwm1Regs.TBCTL.bit.CTRMODE = 0; //unfreeze, and enter up count mode

           //
           //wait while ePWM causes ADC conversions, which then cause interrupts,
           //which fill the results buffer, eventually setting the bufferFull
           //flag
           //
           while(!bufferFull);
           bufferFull = 0; //clear the buffer full flag

           //
           //stop ePWM
           //
           EPwm1Regs.ETSEL.bit.SOCAEN = 0;  //disable SOCA
           EPwm1Regs.TBCTL.bit.CTRMODE = 3; //freeze counter

           //
           //at this point, AdcaResults[] contains a sequence of conversions
           //from the selected channel
           //

           //
           //software breakpoint, hit run again to get updated conversions
           //
           //asm("   ESTOP0");
       }while(1);
}

void test()
{
    //double a1[1024];
    int t1;
    for(t1=0;t1<1024;t1++)
    {
        a1[t1]=1.5+(float)t1/2560;
    }
}


void configureDAC(Uint16 dac_num)
{
	EALLOW;
	DAC_PTR[dac_num]->DACCTL.bit.DACREFSEL = REFERENCE;
	DAC_PTR[dac_num]->DACOUTEN.bit.DACOUTEN = 1;
	DAC_PTR[dac_num]->DACVALS.all = 0;
	DELAY_US(10); // Delay for buffered DAC to power up
	EDIS;
}

void ConfigureEPWM(void)
{
    EALLOW;
    // Assumes ePWM clock is already enabled
    EPwm1Regs.ETSEL.bit.SOCAEN  = 0;    // Disable SOC on A group
    EPwm1Regs.ETSEL.bit.SOCASEL  = 4;   // Select SOC on up-count
    EPwm1Regs.ETPS.bit.SOCAPRD = 1;       // Generate pulse on 1st event
    EPwm1Regs.CMPA.bit.CMPA = 0x0800;     // Set compare A value to 2048 counts
    EPwm1Regs.TBPRD = 0x1000;             // Set period to 4096 counts
    EPwm1Regs.TBCTL.bit.CTRMODE = 3;      // freeze counter
    EDIS;
}

__interrupt void cpu_timer0_isr(void)
{
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器，对其全部清除，不接受其他中断
    //CpuTimer0.InterruptCount++;
    dacval = (Uint16)((a1[n]/3.0)*4095);
    n = (n==1023)?0:(n+1);
    DAC_PTR[DAC_NUM]->DACVALS.all = dacval;
 }

// adca1_isr - Read ADC Buffer in ISR
//
interrupt void adcb_isr(void)
{
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
    DATA_BUF[resultsIndex++] = AdcbResultRegs.ADCRESULT0;
    if(DATA_SIZE <= resultsIndex)
    {
        resultsIndex = 0;
        bufferFull = 1;
    }

    AdcbRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //clear INT1 flag

    //
    // Check if overflow has occurred
    //
    if(1 == AdcbRegs.ADCINTOVF.bit.ADCINT1)
    {
        AdcbRegs.ADCINTOVFCLR.bit.ADCINT1 = 1; //clear INT1 overflow flag
        AdcbRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //clear INT1 flag
    }

}


