|
|
View previous topic :: View next topic |
Author |
Message |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
4922 SPI not working |
Posted: Fri Nov 22, 2013 3:29 am |
|
|
Hi,
i tired the following code, but its not working...
not sure what is the problem.
Code: |
void write_dac(int16 dac_value)
{
char i;
char temp;
output_low(DAC_CS);
output_high(DAC_LDAC);
temp=(dac_value>>8) & 0xFF;
temp=temp|0x30;
for(i=0;i<8;i++)
{
output_high(DAC_CLK);
output_bit(DAC_DI,((temp<<1)&0x80));
output_low(DAC_CLK);
delay_ms(100);
}
temp=dac_value;
for(i=0;i<8;i++)
{
output_high(DAC_CLK);
output_bit(DAC_DI,(temp<<1)&0x80);
output_low(DAC_CLK);
delay_ms(100);
}
output_high(DAC_CS);
output_low(DAC_LDAC);
|
thanks,
Sid |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Fri Nov 22, 2013 4:35 am |
|
|
Why not use the SPI drivers supplied?..... Even if you must use software SPI (chip doesn't have the hardware), CCS has working code that is likely to be much more efficient than this.....
There are quite a few things wrong though:
a) LDAC, must be _high_ at the start before the CS drops. At the end of the sequence, CS needs to go high, then LDAC drop, and rise again. This is what transfers the data to the DAC.
b) You should also make sure you start with the clock low before CS drops.
c) You are rotating the byte, before you perform the 'and'. You will be sending bit 7 as the first bit.....
d) Data is clocked on the rising edge of CS, not the falling edge.
e) You don't need/want the delays.
f )You seem to be calling your data output pin 'DI'. Designed to confuse.
So:
Code: |
void send_byte(int8 val)
{
int8 mask=0x80; //clock out 8 bits starting from bit 7
do
{
if (mask&val)
output_high(DAC_DI);
else
output_low(DAC_DI);
output_high(DAC_CLK);
mask>>=1;
output_low(DAC_CLK);
}
while (mask!=0);
}
void write_dac(int16 dac_value, int1 unit)
{
output_high(DAC_LDAC);
output_low(DAC_CLK); //ensure lines start at correct levels
dac_val &=0x0FFF; //Twelve bits to send
bit_set(dac_val,12); //shutdown off
bit_set(dac_val,13); //low gain
if (unit)
bit_set(dac_val,15); //select channel
output_low(DAC_CS);
send_byte(make8(dac_val,1)); //send MSB
send_byte(make8(dac_val,0)); //send LSB
output_high(DAC_CS); //complete transfer
//now clock the byte into the DAC registers
output_low(DAC_LDAC);
output_high(DAC_LDAC);
}
|
Adds a 'unit' variable to select the channel to use.
Now the 'send_byte' code could be replaced with the standard spi_xfer command, and #use SPI on the same pins.
No guarantees, typed from the data sheet, and untested, but should give the signal required by the chip.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|