#include "../sys_include/F28x_Project.h"

//initialize DAC pointer
volatile struct DAC_REGS* DAC_PTR[4] = {0x0,&DacaRegs,&DacbRegs,&DaccRegs};

//definitions for selecting DAC reference
#define REFERENCE_VDAC		0
#define REFERENCE_VREF		1

//definitions for DAC number
#define DACA				1
#define DACB				2
#define DACC				3

//specify configuration
#define REFERENCE			REFERENCE_VDAC
#define DAC_NUM				DACA
Uint16 dacval = 2048;

//local functions
void configureDAC(Uint16 dac_num);

void main(void)
{
// Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the F2837xS_SysCtrl.c file.
	InitSysCtrl();

// Disable CPU interrupts
	DINT;

// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags are cleared.
// This function is found in the F2837xS_PieCtrl.c file.
	InitPieCtrl();

// Clear all interrupts and initialize PIE vector table:
	IER = 0x0000;
	IFR = 0x0000;
	InitPieVectTable();

// Configure DAC
	configureDAC(DAC_NUM);

	while(1)
	{
		DAC_PTR[DAC_NUM]->DACVALS.all = dacval;
		DELAY_US(2);
	}
}

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