View previous topic :: View next topic |
Author |
Message |
jdasari Guest
|
Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 7:58 am |
|
|
I've spent close to two days trying to get this to work and haven't really made any progress. Need some advice here....
Setup and Objective:
--------------------
I have a PIC 16F877 connected to a DAC 5334 via SPI with a 3.9 reference voltage feed to the DAC. The objective is to get the DAC to output a particular voltage depending on what the user choice happens to be.
Issue at hand:
---------------
The DAC has 2 registers, a 16 bit input register which acts as a buffer and the other a 16 bit DAC register. Since the max I can send out via SPI is 8 bit, I have been trying to write the MSB first and the LSB second into the input register and then update the DAC register from the input register using the code below
void set_voltage()
{
//setup_spi(SPI_MASTER |SPI_L_TO_H |SPI_CLK_DIV_16);
setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_4);
delay_us(30);
output_low(PIN_C0);
spi_write(32);
spi_write (24);
delay_us(30);
spi_write(64);
spi_write(0);
delay_us(30);
output_high(PIN_C0);
delay_us(30);
} // End of the set_voltage routine
I have been using the 10 data bits to code in the required voltage level into the 16 bit data frame and whenever I run the code, the DAC either gets stuck at 1V or ramps back and forth anywhere from 0.47 V to 6V.
What am I doing wrong? Also, should I be writing Hex or Dec in SPI?
Any help will be greatly appreciated.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13252 |
|
|
john cutler
Joined: 06 Sep 2003 Posts: 82 Location: Hot Tub, California
|
Re: Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 10:07 am |
|
|
<font face="Courier New" size=-1>:=I've spent close to two days trying to get this to work and haven't really made any progress. Need some advice here....
:=
:=Setup and Objective:
:=--------------------
:=I have a PIC 16F877 connected to a DAC 5334 via SPI with a 3.9 reference voltage feed to the DAC. The objective is to get the DAC to output a particular voltage depending on what the user choice happens to be.
:=
:=Issue at hand:
:=---------------
:=The DAC has 2 registers, a 16 bit input register which acts as a buffer and the other a 16 bit DAC register. Since the max I can send out via SPI is 8 bit, I have been trying to write the MSB first and the LSB second into the input register and then update the DAC register from the input register using the code below
:=
:=void set_voltage()
:={
:= //setup_spi(SPI_MASTER |SPI_L_TO_H |SPI_CLK_DIV_16);
:= setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_4);
:= delay_us(30);
:= output_low(PIN_C0);
:= spi_write(32);
:= spi_write (24);
:= delay_us(30);
:= spi_write(64);
:= spi_write(0);
:= delay_us(30);
:= output_high(PIN_C0);
:= delay_us(30);
:=
:=} // End of the set_voltage routine
:=
:=I have been using the 10 data bits to code in the required voltage level into the 16 bit data frame and whenever I run the code, the DAC either gets stuck at 1V or ramps back and forth anywhere from 0.47 V to 6V.
:=
:=What am I doing wrong? Also, should I be writing Hex or Dec in SPI?
:=
:=Any help will be greatly appreciated.
Don't know the part, but why are you writing twice to the dac before bringing the chip enable high?
:= output_low(PIN_C0);
:= spi_write(32);
:= spi_write (24); - don't you need to bring PIN_C0 after here to latch the data?
:= delay_us(30); and maybe
output_low(PIN_C0); here to send next 16 bits of data to the DAC?
:= spi_write(64);
:= spi_write(0);
:= delay_us(30);
:= output_high(PIN_C0);</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13255 |
|
|
jdasari Guest
|
Re: Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 1:17 pm |
|
|
:=Don't know the part, but why are you writing twice to the dac
before bringing the chip enable high?
-- Once to Update the input register and then the second is the command to update the DAC register with what is in the Input register.
don't you need to bring PIN_C0 after here to latch the data?
:=:= delay_us(30); and maybe
output_low(PIN_C0);
here to send next 16 bits of data to the DAC?
---Will try this out.
Do I need to write Dec numbers or Hex? The sp_write() function description says it takes a Dec, but some of the sample codes I've seen on the forum seem to be using Hex.
Thanks for the reply John...
___________________________
This message was ported from CCS's old forum
Original Post ID: 13258 |
|
|
Warren Massey Guest
|
Re: Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 2:28 pm |
|
|
:=Do I need to write Dec numbers or Hex? The sp_write() function description says it takes a Dec, but some of the sample codes I've seen on the forum seem to be using Hex.
The simple answer is to use either one, the compiler is going to turn whichever into binary anyway. People just use the hex format 'cause it can be easier to understand when the word represented is a collection of miscellaneous bits like those found in a CSR. If all the bits represent a pure number, people often prefer decimal. The compiler doesn't care as long as you tell it how to interpret the value.
I'd like to also go on a say that there is nothing magic about the SPI routines CCS provides. They didn't handle 16-bit SPI reads well for me so I wrote my own 16-bit SPI read routine and it was easy enough to do. 16-bit SPI writes would not be much more difficult, you just need to add some delays so the SPI line states exist for reasonable amounts of time. Just make sure you know how an SPI transaction is supposed be conducted and start coding...
___________________________
This message was ported from CCS's old forum
Original Post ID: 13263 |
|
|
Warren Massey Guest
|
Re: Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 2:30 pm |
|
|
<font face="Courier New" size=-1>:=Do I need to write Dec numbers or Hex? The sp_write() function description says it takes a Dec, but some of the sample codes I've seen on the forum seem to be using Hex.
The simple answer is to use either one, the compiler is going to turn whichever into binary anyway. People just use the hex format 'cause it can be easier to understand when the word represented is a collection of miscellaneous bits like those found in a CSR. If all the bits represent a pure number, people often prefer decimal. The compiler doesn't care as long as you tell it how to interpret the value.
I'd like to also go on and say that there is nothing magic about the SPI routines CCS provides. They didn't handle 16-bit SPI reads well for me so I wrote my own 16-bit SPI read routine and it was easy enough to do. 16-bit SPI writes would not be much more difficult, you just need to add some delays so the SPI line states exist for reasonable amounts of time. Just make sure you know how an SPI transaction is supposed be conducted and start coding...</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13264 |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
Re: Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 5:37 pm |
|
|
:=I've spent close to two days trying to get this to work and haven't really made any progress. Need some advice here....
:=
:=Setup and Objective:
:=--------------------
:=I have a PIC 16F877 connected to a DAC 5334 via SPI with a 3.9 reference voltage feed to the DAC. The objective is to get the DAC to output a particular voltage depending on what the user choice happens to be.
:=
:=Issue at hand:
:=---------------
:=The DAC has 2 registers, a 16 bit input register which acts as a buffer and the other a 16 bit DAC register. Since the max I can send out via SPI is 8 bit, I have been trying to write the MSB first and the LSB second into the input register and then update the DAC register from the input register using the code below
:=
:=void set_voltage()
:={
:= //setup_spi(SPI_MASTER |SPI_L_TO_H |SPI_CLK_DIV_16);
:= setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_4);
:= delay_us(30);
:= output_low(PIN_C0);
:= spi_write(32);
:= spi_write (24);
:= delay_us(30);
:= spi_write(64);
:= spi_write(0);
:= delay_us(30);
:= output_high(PIN_C0);
:= delay_us(30);
:=
:=} // End of the set_voltage routine
:=
:=I have been using the 10 data bits to code in the required voltage level into the 16 bit data frame and whenever I run the code, the DAC either gets stuck at 1V or ramps back and forth anywhere from 0.47 V to 6V.
:=
:=What am I doing wrong? Also, should I be writing Hex or Dec in SPI?
:=
:=Any help will be greatly appreciated.
Don't know that dac, couldn't google it either.
Here's some code snippets for a 16 bit dac that I have used, the Burr-Brown DAC714. It may be of use to you. Application Bulletin AB-130 gives hints.
Regards
Kenny
//RC1 To CLK on DAC714
//RC2 To A1 on DAC714
//RC3 SCLK in SPI mode (to A0 on DAC714)
//RC5 SPI mode serial data out (to SDI on DAC714)
#define CLK PIN_C1
#define A1 PIN_C2
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4|SPI_SS_DISABLED);
void send_to_dac714(int16 dac_data) {
int temp8;
output_bit(CLK, 0);
temp8 = dac_data >> 8;
SPI_WRITE(temp8);
temp8 = dac_data & 0xff;
SPI_WRITE(temp8);
output_bit(A1, 0);
output_bit(A1, 1);
output_bit(CLK, 1);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13267 |
|
|
jdasari Guest
|
Re: Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 8:42 pm |
|
|
Thanks Kenny. We are using Maxim DAC 5354. Sorry about the typo.
Also, what does SPI_SS_DISABLED do?
Regards
Jayant
___________________________
This message was ported from CCS's old forum
Original Post ID: 13273 |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
Re: Using SPI to communicate with a DAC |
Posted: Mon Mar 31, 2003 10:00 pm |
|
|
:=Thanks Kenny. We are using Maxim DAC 5354. Sorry about the typo.
:=Also, what does SPI_SS_DISABLED do?
:=
:=Regards
:=Jayant
From data sheets it is only relevant in slave mode. If /SS pin is set to Vdd the SPI module will be reset. It's not relevant in this case. The wizard must have added it automagicly. I don't use the wizard at all now.
Regards
Kenny
___________________________
This message was ported from CCS's old forum
Original Post ID: 13275 |
|
|
jdasari Guest
|
Thanks! |
Posted: Tue Apr 01, 2003 8:31 am |
|
|
<font face="Courier New" size=-1>Thanks folks. Looks like its finally working. Appreciate all your input.
Jayant
</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13283 |
|
|
|