/**
 * ---------------------------------------------------------------------------------------
 * ---------------------------------------------------------------------------------------
 * 功能描述：
 * 	   产生变周期三角波
 * 	   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 768
Uint16 DATA_BUF[DATA_SIZE]; //一个周期采样点
// 函数声明
void configureDAC(Uint16 dac_num);
void test();
void Data_print(void);

__interrupt void cpu_timer0_isr(void);
__interrupt void cpu_timer1_isr(void);  //配置定时器中断设置adc采样率

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.TIMER1_INT = &cpu_timer1_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();

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

    ConfigCpuTimer(&CpuTimer1, 200,2.4*(float)1000/256); //ad采样率
    CpuTimer1Regs.TCR.all = 0x4000;   //启动定时器

    //使能对应的中断
    IER |= M_INT1;
    IER |= M_INT13;
    IER |= M_INT14;

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

    //使能全局中断和实时调试中断
    EINT;
    ERTM;

    // 初始化串口
    SCIStdioInit();
    // 打印adc数据
    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;
}

__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;
 }

__interrupt void cpu_timer1_isr(void)
{
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001赋给12组中断ACKnowledge寄存器，对其全部清除，不接受其他中断
    //    //大三角边每条边需采集256点，小三角每条边需采集128点 同一个采样率
//    DELAY_US(1);
//     //启动 ADCB 转换
//    AdcbRegs.ADCSOCFRC1.all = 0x003F; //SOC0 ~ SOC5
//    // 等待 ADCB 转换完成
//    while(AdcbRegs.ADCINTFLG.bit.ADCINT1 == 0);
//    AdcbRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;

    AdcbResult[0] = AdcbResultRegs.ADCRESULT5;
    // 计算 ADCs 的实际电压值
    //Adcb_V[0] = (float)AdcbResult[0]*3/4095;

    //int a1;
    //a1 = (int)((Adcb_V[0]-1)*10000);

     //串口打印 ADC 各个通道数据
    SCIprintf("%d   ",AdcbResult[0]);
    //SCIprintf("Adcb_V = 1.%d\n",a1);
    //SCIprintf("ADCINB0 = %d,Adcb_V = 1.%d\n",AdcbResult[0],a1);
}


