This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

dsp 28335 DAC function



I am using f28335 and when i have written a code for sine-triangle PWM. in that i am taking sine wave directly in a array format. when i am trying to see the sine wave through dac, i am doing wrong. i don't have any idea on dac. here i am attaching my code. can anybody correct this please......

#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File

// Prototype statements for functions found within this file.
void InitEPwm1Example(void);
void InitEPwm2Example(void);
void InitEPwm3Example(void);
interrupt void epwm1_isr(void);
interrupt void epwm2_isr(void);
interrupt void epwm3_isr(void);
void i2c_dac_exapmle(void);
void parallel_dac_exapmle(void);
void update_MCP4728(void);
void config_MCP4728(void);
void delay_loop(void);

int sine[1000] = {0,15,30,44,59,74,88,103,118,132,147,162,176,191,206,220,235,250,265,279,294,308,323,337,352,366,381,395,410,425,439,453,468,482,497,511,526,
 	540,554,568,583,597,611,625,640,654,668,682,696,710,724,738,752,766,780,794,807,821,835,849,862,876,890,903,917,930,944,957,971,984,998,1011,1024,1037,
 	1051,1064,1077,1090,1103,1116,1129,1142,1154,1167,1180,1193,1205,1218,1231,1243,1255,1268,1280,1292,1305,1317,1329,1341,1353,1365,1377,1389,1401,1413,
 	1424,1436,1448,1459,1471,1482,1493,1505,1516,1527,1538,1549,1560,1571,1582,1593,1604,1615,1625,1636,1646,1657,1667,1677,1688,1698,1708,1718,1728,1738,
 	1748,1757,1767,1777,1786,1796,1805,1815,1824,1833,1842,1851,1860,1869,1878,1887,1895,1904,1913,1921,1929,1938,1946,1954,1962,1970,1978,1986,1994,2002,
 	2009,2017,2024,2032,2039,2046,2053,2060,2067,2074,2081,2088,2094,2101,2107,2114,2120,2126,2132,2138,2144,2150,2156,2162,2168,2173,2179,2184,2189,2194,
 	2199,2205,2209,2214,2219,2224,2228,2233,2237,2242,2246,2250,2254,2258,2262,2266,2269,2273,2276,2280,2283,2287,2290,2293,2296,2299,2302,2304,2307,2309,
 	2312,2314,2317,2319,2321,2323,2324,2326,2328,2330,2331,2333,2334,2335,2336,2337,2338,2339,2340,2341,2341,2342,2342,2343,2343,2343,2343,2343,2343,2343,
 	2342,2342,2341,2341,2340,2339,2338,2337,2336,2335,2334,2333,2331,2330,2328,2326,2324,2323,2321,2319,2317,2314,2312,2309,2307,2304,2302,2299,2296,2293,
 	2290,2287,2283,2280,2276,2273,2269,2266,2262,2258,2254,2250,2246,2242,2237,2233,2228,2224,2219,2214,2209,2205,2199,2194,2189,2184,2179,2173,2168,2162,
 	2156,2150,2144,2138,2132,2126,2120,2114,2107,2101,2094,2088,2081,2074,2067,2060,2053,2046,2039,2032,2024,2017,2009,2002,1994,1986,1978,1970,1962,1954,
 	1946,1938,1929,1921,1913,1904,1895,1887,1878,1869,1860,1851,1842,1833,1824,1815,1805,1796,1786,1777,1767,1757,1748,1738,1728,1718,1708,1698,1688,1677,
 	1667,1657,1646,1636,1625,1615,1604,1593,1582,1571,1560,1549,1538,1527,1516,1505,1493,1482,1471,1459,1448,1436,1424,1413,1401,1389,1377,1365,1353,1341,
 	1329,1317,1305,1292,1280,1268,1255,1243,1231,1218,1205,1193,1180,1167,1154,1142,1129,1116,1103,1090,1077,1064,1051,1037,1024,1011,998,984,971,957,944,
 	930,917,903,890,876,862,849,835,821,807,794,780,766,752,738,724,710,696,682,668,654,640,625,611,597,583,568,554,540,526,511,497,482,468,453,439,425,410,
 	395,381,366,352,337,323,308,294,279,265,250,235,220,206,191,176,162,147,132,118,103,88,74,59,44,30,15,0,-15,-30,-44,-59,-74,-88,-103,-118,-132,-147,-162,
 	-176,-191,-206,-220,-235,-250,-265,-279,-294,-308,-323,-337,-352,-366,-381,-395,-410,-425,-439,-453,-468,-482,-497,-511,-526,-540,-554,-568,-583,-597,
 	-611,-625,-640,-654,-668,-682,-696,-710,-724,-738,-752,-766,-780,-794,-807,-821,-835,-849,-862,-876,-890,-903,-917,-930,-944,-957,-971,-984,-998,-1011,
 	-1024,-1037,-1051,-1064,-1077,-1090,-1103,-1116,-1129,-1142,-1154,-1167,-1180,-1193,-1205,-1218,-1231,-1243,-1255,-1268,-1280,-1292,-1305,-1317,-1329,
 	-1341,-1353,-1365,-1377,-1389,-1401,-1413,-1424,-1436,-1448,-1459,-1471,-1482,-1493,-1505,-1516,-1527,-1538,-1549,-1560,-1571,-1582,-1593,-1604,-1615,
 	-1625,-1636,-1646,-1657,-1667,-1677,-1688,-1698,-1708,-1718,-1728,-1738,-1748,-1757,-1767,-1777,-1786,-1796,-1805,-1815,-1824,-1833,-1842,-1851,-1860,
 	-1869,-1878,-1887,-1895,-1904,-1913,-1921,-1929,-1938,-1946,-1954,-1962,-1970,-1978,-1986,-1994,-2002,-2009,-2017,-2024,-2032,-2039,-2046,-2053,-2060,
 	-2067,-2074,-2081,-2088,-2094,-2101,-2107,-2114,-2120,-2126,-2132,-2138,-2144,-2150,-2156,-2162,-2168,-2173,-2179,-2184,-2189,-2194,-2199,-2205,-2209,
 	-2214,-2219,-2224,-2228,-2233,-2237,-2242,-2246,-2250,-2254,-2258,-2262,-2266,-2269,-2273,-2276,-2280,-2283,-2287,-2290,-2293,-2296,-2299,-2302,-2304,
 	-2307,-2309,-2312,-2314,-2317,-2319,-2321,-2323,-2324,-2326,-2328,-2330,-2331,-2333,-2334,-2335,-2336,-2337,-2338,-2339,-2340,-2341,-2341,-2342,-2342,
 	-2343,-2343,-2343,-2343,-2343,-2343,-2343,-2342,-2342,-2341,-2341,-2340,-2339,-2338,-2337,-2336,-2335,-2334,-2333,-2331,-2330,-2328,-2326,-2324,-2323,
 	-2321,-2319,-2317,-2314,-2312,-2309,-2307,-2304,-2302,-2299,-2296,-2293,-2290,-2287,-2283,-2280,-2276,-2273,-2269,-2266,-2262,-2258,-2254,-2250,-2246,
 	-2242,-2237,-2233,-2228,-2224,-2219,-2214,-2209,-2205,-2199,-2194,-2189,-2184,-2179,-2173,-2168,-2162,-2156,-2150,-2144,-2138,-2132,-2126,-2120,-2114,
 	-2107,-2101,-2094,-2088,-2081,-2074,-2067,-2060,-2053,-2046,-2039,-2032,-2024,-2017,-2009,-2002,-1994,-1986,-1978,-1970,-1962,-1954,-1946,-1938,-1929,
 	-1921,-1913,-1904,-1895,-1887,-1878,-1869,-1860,-1851,-1842,-1833,-1824,-1815,-1805,-1796,-1786,-1777,-1767,-1757,-1748,-1738,-1728,-1718,-1708,-1698,
 	-1688,-1677,-1667,-1657,-1646,-1636,-1625,-1615,-1604,-1593,-1582,-1571,-1560,-1549,-1538,-1527,-1516,-1505,-1493,-1482,-1471,-1459,-1448,-1436,-1424,
 	-1413,-1401,-1389,-1377,-1365,-1353,-1341,-1329,-1317,-1305,-1292,-1280,-1268,-1255,-1243,-1231,-1218,-1205,-1193,-1180,-1167,-1154,-1142,-1129,-1116,
 	-1103,-1090,-1077,-1064,-1051,-1037,-1024,-1011,-998,-984,-971,-957,-944,-930,-917,-903,890,-876,-862,-849,-835,-821,-807,-794,-780,-766,-752,-738,-724,
 	-710,-696,-682,-668,-654,-640,-625,-611,-597,-583,-568,-554,-540,-526,-511,-497,-482,-468,-453,-439,-425,-410,-395,-381,-366,-352,-337,-323,-308,-294,
 	-279,-265,-250,-235,-220,-206,-191,-176,-162,-147,-132,-118,-103,-88,-74,-59,-44,-30,-15};


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

// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example

// For this case just init GPIO pins for ePWM1, ePWM2, ePWM3
// These functions are in the DSP2833x_EPwm.c file
 /*  InitEPwm1Gpio();
   InitEPwm2Gpio();
   InitEPwm3Gpio();
   EALLOW;
  */ 
/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user. 
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.
EALLOW;
    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;    // Enable pull-up on GPIO0 (EPWM1A)
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;    // Enable pull-up on GPIO1 (EPWM1B)
    GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0;    // Enable pull-up on GPIO2 (EPWM2A)
    GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0;    // Enable pull-up on GPIO3 (EPWM2B)      
    GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0;    // Enable pull-up on GPIO4 (EPWM3A)
    GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0;    // Enable pull-up on GPIO5 (EPWM3B)   
    GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0;    // Enable pull-up on GPIO6 (EPWM4A)
    GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0;    // Enable pull-up on GPIO7 (EPWM4B)
    GpioCtrlRegs.GPAPUD.bit.GPIO8 = 0;    // Enable pull-up on GPIO8 (EPWM5A)
    GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0;    // Enable pull-up on GPIO9 (EPWM5B)   
    GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0;    // Enable pull-up on GPIO10 (EPWM6A)
    GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0;    // Enable pull-up on GPIO11 (EPWM6B)      

/* Configure ePWM-1 pins using GPIO regs*/
// This specifies which of the possible GPIO pins will be ePWM1 functional pins.
// Comment out other unwanted lines.

    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;   // Configure GPIO0 as EPWM1A
    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0;   // Configure GPIO1 as EPWM1B
    GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0;   // Configure GPIO0 as EPWM2A
    GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0;   // Configure GPIO1 as EPWM2B
    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1;   // Configure GPIO0 as EPWM3A
    GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1;   // Configure GPIO1 as EPWM3B
    GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 1;   // Configure GPIO0 as EPWM4A
    GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 1;   // Configure GPIO1 as EPWM4B
    GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 1;   // Configure GPIO0 as EPWM5A
    GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 1;   // Configure GPIO1 as EPWM5B
    GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 1;   // Configure GPIO0 as EPWM6A
    GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 1;   // Configure GPIO1 as EPWM6B
       
    EDIS;
   

// Step 3. Clear all interrupts and initialize PIE vector table:
// 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 DSP2833x_PieCtrl.c file.
   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
   InitPieVectTable();

// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
   EALLOW;  // This is needed to write to EALLOW protected registers
   PieVectTable.EPWM1_INT = &epwm1_isr;
  // PieVectTable.EPWM2_INT = &epwm2_isr;
   //PieVectTable.EPWM3_INT = &epwm3_isr;
   EDIS;    // This is needed to disable write to EALLOW protected registers

// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2833x_InitPeripherals.c
// InitPeripherals();  // Not required for this example


   EALLOW;
   SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
   EDIS;

   InitEPwm1Example();
   InitEPwm2Example();
   InitEPwm3Example();
     
   EALLOW;
   SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
   EDIS;

// Step 5. User specific code, enable interrupts
// Initalize counters:
   //EPwm1TimerIntCount = 0;
   //EPwm2TimerIntCount = 0;
  // EPwm3TimerIntCount = 0;

// Enable CPU INT3 which is connected to EPWM1-3 INT:
   IER |= M_INT3;

// Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
   PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
   PieCtrlRegs.PIEIER3.bit.INTx2 = 1;
   PieCtrlRegs.PIEIER3.bit.INTx3 = 1;
   PieCtrlRegs.PIEIER3.bit.INTx4 = 1;
   PieCtrlRegs.PIEIER3.bit.INTx5 = 1;
   PieCtrlRegs.PIEIER3.bit.INTx6 = 1;
   
    PieCtrlRegs.PIEIER2.bit.INTx1 = 1;
    PieCtrlRegs.PIEIER2.bit.INTx2 = 1;
    PieCtrlRegs.PIEIER2.bit.INTx3 = 1;

// Enable global Interrupts and higher priority real-time debug events:
   EINT;   // Enable Global interrupt INTM
   ERTM;   // Enable Global realtime interrupt DBGM
  //Uint16 config_MCP4728(void);
 
EALLOW;
  // Enable an GPIO output on GPIO6, set it high
   GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;   // Enable pullup on GPIO6
  // GpioDataRegs.GPASET.bit.GPIO0 = 1;   // Load output latch
   GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;  // GPIO6 = GPIO6
   GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;   // GPIO6 = output
   EDIS; 

// Step 6. IDLE loop. Just sit and loop forever (optional):
   for(;;)
   {
       asm("          NOP");
   }

}

void delay_loop(void)
{
    short      i;
    for (i = 0; i < 10000; i++) {}
}

interrupt void epwm1_isr(void)
{
	
	float ma=0.8;
    float pa,pb,pc;       // final phase-A, phase-B, phase_c voltages after multiplication with ma
    static float j=0;     
    int ia,ib,ic;         // arry index to phase-A, phase-B, phase_c voltages
   static int daca=0,dacb=0,dacc=0;
   static int MCP4728_CH_A=0,MCP4728_CH_B=0,MCP4728_CH_C=0,MCP4728_CH_D=0;

   // Clear INT flag for this timer
   EPwm1Regs.ETCLR.bit.INT = 1;

   // Acknowledge this interrupt to receive more interrupts from group 3
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
   

// --------------------------------------------------repeating sine wave    
   if(j>=1000)
   {
   j=0;
   }

//--------------------------------------------------sine wave for A,B,C phases     
   ia=j;
   ib=j+334;
   ic=j+668;

//------------------------------------------------ for B,C phases if it goes beyond 1000 it should come to starting   
   if(ib>=1000)
   {
   	ib=ib-1000;
   }
   if(ic>=1000)
   {
   	ic=ic-1000;
   }

// -----------------------------------------------to control the sine wave magnitude with modulation index

pa=ma*sine[ia];
pb=ma*sine[ib];
pc=ma*sine[ic];
   
//----------------------------------------------adding offset to bring the sine wave with in one triangler wave

// A-phase

if(pa>=-2343 && pa<=0)
{
	pa=pa+2343;

 //GpioDataRegs.GPBSET.all    =0x0001;
}

//B-phase

if(pb>=-2343 && pb<=0)
{
	pb=pb+2343;

// GpioDataRegs.GPBSET.all    =0x0002;
}

// C-phase

if(pc>=-2343 && pc<=0)
{
	pc=pc+2343;

 //GpioDataRegs.GPBSET.all    =0x0004;
}
 /*
  // Setup compare
   EPwm1Regs.CMPA.half.CMPA = pa;
   // Setup compare
   EPwm2Regs.CMPA.half.CMPA = pb;
   // Setup compare
   EPwm3Regs.CMPA.half.CMPA = pc;
  */ 
     j=j+50*ma;


/*----------------------------------writing dac start-------------
  
//void i2c_dac_exapmle(void);
  //Uint16 update_MCP4728(void);
 
   EALLOW;
  // Enable an GPIO output on GPIO6, set it high
   GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;   // Enable pullup on GPIO6
  // GpioDataRegs.GPASET.bit.GPIO0 = 1;   // Load output latch
   GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;  // GPIO6 = GPIO6
   GpioCtrlRegs.GPADIR.bit.GPIO0 = 0;   // GPIO6 = output
   EDIS;   
  
   for(;;)
   {   
       GpioDataRegs.GPADAT.bit.GPIO0  = pa;
     
       delay_loop();
}/*------------------------------writing dac end--------------------
}



void InitEPwm1Example()
{

   EPwm1Regs.TBPRD = 2343;                        // Set timer period
   EPwm1Regs.TBPHS.half.TBPHS = 0x0000;           // Phase is 0
   EPwm1Regs.TBCTR = 0x0000;                      // Clear counter

   // Setup TBCLK
   EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up
   EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;        // Disable phase loading
   EPwm1Regs.TBCTL.bit.HSPCLKDIV = 0x2;  //TB_DIV4;       // Clock ratio to SYSCLKOUT
   EPwm1Regs.TBCTL.bit.CLKDIV = 0x3;   //TB_DIV4;

 //  EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;    // Load registers every ZERO
 //  EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
 //  EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
 //  EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;

   EPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Select INT on Zero event
   EPwm1Regs.ETSEL.bit.INTEN = 1; // Enable INT
   EPwm1Regs.ETPS.bit.INTPRD = ET_1ST; // Generate INT on 1st event
     // Set actions
   EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;             // Set PWM1A on Zero
   EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;
   
   EPwm1Regs.AQCTLB.bit.CAU = AQ_CLEAR;          // Set PWM1B on Zero
   EPwm1Regs.AQCTLB.bit.CAD = AQ_SET; 

}


void InitEPwm2Example()
{

   EPwm2Regs.TBPRD = 2343;                        // Set timer period
   EPwm2Regs.TBPHS.half.TBPHS = 0x0000;           // Phase is 0
   EPwm2Regs.TBCTR = 0x0000;                      // Clear counter

   // Setup TBCLK
   EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up
   EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE;        // Disable phase loading
   EPwm2Regs.TBCTL.bit.HSPCLKDIV = 0x2;   //TB_DIV4;       // Clock ratio to SYSCLKOUT
   EPwm2Regs.TBCTL.bit.CLKDIV = 0x3;      //TB_DIV4;          // Slow just to observe on the scope

    // Set actions
   EPwm2Regs.AQCTLA.bit.CAU = AQ_SET;             // Set PWM2A on Zero
   EPwm2Regs.AQCTLA.bit.CAD = AQ_CLEAR;


   EPwm2Regs.AQCTLB.bit.CAU = AQ_CLEAR;           // Set PWM2A on Zero
   EPwm2Regs.AQCTLB.bit.CAD = AQ_SET;
}

void InitEPwm3Example()
{

   EPwm3Regs.TBPRD = 2343;                         // Set timer period
   EPwm3Regs.TBPHS.half.TBPHS = 0x0000;            // Phase is 0
   EPwm3Regs.TBCTR = 0x0000;                       // Clear counter


   // Setup TBCLK
   EPwm3Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up
   EPwm3Regs.TBCTL.bit.PHSEN = TB_DISABLE;        // Disable phase loading
   EPwm3Regs.TBCTL.bit.HSPCLKDIV = 0x2;   //TB_DIV4;       // Clock ratio to SYSCLKOUT
   EPwm3Regs.TBCTL.bit.CLKDIV = 0x3;      //TB_DIV4;          // Slow so we can observe on the scope

  
   // Set actions
   EPwm3Regs.AQCTLA.bit.CAU = AQ_SET;              // Set PWM3A on Zero
   EPwm3Regs.AQCTLA.bit.CAD = AQ_CLEAR;


   EPwm3Regs.AQCTLB.bit.CAU = AQ_CLEAR;            // Set PWM3A on Zero
   EPwm3Regs.AQCTLB.bit.CAD = AQ_SET;
}
/* -------- dac function start--------------------------------------------------------------------------------
/*
void i2c_dac_exapmle(void)
{
	static int MCP4728_CH_A=0,MCP4728_CH_B=0,MCP4728_CH_C=0,MCP4728_CH_D=0;
for(;;)
{
MCP4728_CH_A++;
MCP4728_CH_A&=0xff;
MCP4728_CH_B=MCP4728_CH_A;
MCP4728_CH_C=MCP4728_CH_A;
MCP4728_CH_D=MCP4728_CH_A;
while(I2caRegs.I2CMDR.bit.STP == 1);
while(I2caRegs.I2CSTR.bit.BB == 1);
update_MCP4728();
}
}

Uint16 update_MCP4728(void)
{
	int MCP4728_CH_A, MCP4728_CH_B,MCP4728_CH_C,MCP4728_CH_D;
//NOTE
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.

if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}
// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
//wait until bus is free - upto 6k updates per second is possible
while(I2caRegs.I2CMDR.bit.STP == 1);
while(I2caRegs.I2CSTR.bit.BB == 1);
// Setup slave address
I2caRegs.I2CSAR = 0x60;
// Setup number of bytes to send
I2caRegs.I2CCNT = 8;
// Setup data to send
// I2caRegs.I2CDXR = 0x50;
I2caRegs.I2CDXR = (MCP4728_CH_A>>8)&0x0F;
I2caRegs.I2CDXR = MCP4728_CH_A&0xff;
I2caRegs.I2CDXR = (MCP4728_CH_B>>8)&0x0F;
I2caRegs.I2CDXR = MCP4728_CH_B&0xff;
I2caRegs.I2CDXR = (MCP4728_CH_C>>8)&0x0F;
I2caRegs.I2CDXR = MCP4728_CH_C&0xff;
I2caRegs.I2CDXR = ((MCP4728_CH_D>>8)&0x0F)|0x80;
I2caRegs.I2CDXR = MCP4728_CH_D&0xff;
// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20;
return I2C_SUCCESS;
}
Uint16 config_MCP4728(void)
{
//update some of the EEPRO locations to set gain, reference voltage etc
//wait until bus is free
while(I2caRegs.I2CMDR.bit.STP == 1);
while(I2caRegs.I2CSTR.bit.BB == 1);
// Setup slave address
I2caRegs.I2CSAR = 0x60;
// Setup number of bytes to send
I2caRegs.I2CCNT = 9;
// Setup data to send
I2caRegs.I2CDXR = 0x50;
//For setting internal reference with gain 1.
I2caRegs.I2CDXR = 0x80;
I2caRegs.I2CDXR = 0;
I2caRegs.I2CDXR = 0x80;
I2caRegs.I2CDXR = 0;
I2caRegs.I2CDXR = 0x80;
I2caRegs.I2CDXR = 0;
I2caRegs.I2CDXR = 0x80;
I2caRegs.I2CDXR = 0;
// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20;
return I2C_SUCCESS;
} /*----------------------------dac function end---------------
*/

//===========================================================================
// No more.
//===========================================================================