Other Parts Discussed in Thread: LM2841,
Dear Sir :
I plan use DAC161S997 , transfer SPI data to 4-20mA output , i almost use typical application circle . just change LDO to switch power LM2841. now i can communicated with MCU with Uart, but i can't get the right answer from DAC, when i send command to DAC . i use simulate SPI function . the code as below , pls. help me review it , very appreciate if you can give me some suggestion.
when i run test () , only get "FF" ,"FF","FF" from DAC .
/* read 1 bit from SPI. return data */
u8 SpiReadByte(void)
{
u8 i,rByte=0;
SCLK=0;
for(i=0;i<8;i++)
{
SCLK=1;
rByte<<=1;
rByte|=MISO;
SCLK=0;
}
return rByte;
}
/*SPI send 1 bit */
void SpiSendByte(u8 dt)
{
u8 i;
for(i=0;i<8;i++)
{
SCLK=0;
if((dt<<i)&0x80)
MOSI=1;
else
{
MOSI=0;
}
SCLK=1;
}
SCLK=0;
}
Void test ()
{
u8 outData[3], i;
CS_DAC = 0;
outData[0] = 0x04;
outData[1] = Data >> 8;
outData[2] = Data & 0xff;
for (i=0; i<3; i++)
{
SpiSendByte (outData[i]);
Delay_ms(20);
}
Delay_ms(2);
SpiSendByte(0x89);
w[0]=SpiReadByte();
SpiSendByte(0xff);
w[1]=SpiReadByte();
SpiSendByte(0xff);
w[2]=SpiReadByte();
CS_DAC = 1
}