0
\$\begingroup\$

I have two MCP 4822 DACs with a dspic33fj128mc804. The initialization of both the DACs is getting done as required. However, when I try to change the values to be written on the DAC they don't get changed. The value stays same as the initialized value.

Below is my code. Can anyone please help me with this , please?

//Initialize DAC

void initDACs(void) { TRISB&=~0x4010; DAC1CS=DAC2CS=1; _SPI1IF = 0; // Clear the interrupt flag _SPI1IE = 0; // Disable the interrupt

//Initialize SPI

SPI1CON1=0x073e; //16bit, master, prescale 4, CKP0 CKE1 SMP 1

SPI1STAT=0x8000; //enable SPI

DAC1CS=DAC2CS=0;
SPI1BUF = 0x3000;   //Set output of A to 0V
while(!_SPI1IF); //wait until SPI has finished
_SPI1IF = 0; 
DAC1CS=DAC2CS=1;

SPI1STAT &=~0x40;
_SPI1IF = 0;

DAC1CS=DAC2CS=0;
SPI1BUF = 0xb000; // Set output of B to 0V
_SPI1IE = 1;

spiReady=0;

}

// Function to set values on the DAC

void channelSetDAC(int v, int ch) {

unsigned int sendData;

if(ch>4) return;
if (SPI1STATbits.SPITBF) return;

if(ch &1)
{
sendData= 0x3000 | (v & 0xfff);

}
else
{
sendData= 0xb000 | (v & 0xfff);
}

if (ch <3) DAC1CS=0; else DAC2CS=0;

SPI1BUF=sendData;
spiReady=0;

}

// Function to write values on the DAC

void sMethod() { channelSetDAC(0x7d0,0); }

\$\endgroup\$
  • \$\begingroup\$ How does the write trigger? It's been awhile since I've written stuff for the PIC, but does simply loading SPI1BUF cause the module to issue the transaction? \$\endgroup\$ – Krunal Desai Dec 24 '15 at 18:04
1
\$\begingroup\$

The first function is a little confusing, whereas on the second initialization you don't wait for the transmission to end but seem to turn on SPI interrupts. What is the handler doing?

In the second function you don't wait for the end of the transmission and I also don't see the raising of DAC1CS/DAC2CS. Note that if you raise CS before 16 bits have been transferred, nothing happens. If you raise CS but LDAC {is high nothing happens|floats nothing may happen}.

I assume you have the LDAC pin grounded.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.