New to SPI ... DAC help? - Page 1
Forum section: electronics / beginners

author: BiOzZ
author_rank: Regular Contributor
timestamp: April 05, 2013, 06:35:53 am
content: OK well im not REALLY new to SPI just new to understanding just WHAT i need to send to an IC by reading its datasheeti have a TI TLV5618AIPhttp://pdf1.alldatasheet.com/datasheet-pdf/view/158193/TI/TLV5618AIP.htmlwhat do i send over the SPI bus to get some voltages out of the thing? i cant find any arduino libraries for this so i have to send it with the SPI library but what do i send? do i send 1 16 bit commands? 2 8 bit? 4 4bit?if someone can give me what i need to send off 5V to get 5V 2.5V and 0V i think i can work out the inbetween!a simple cut of arduino code would be helpful also! tho i think i got that much down ... its good for reassurancethanks allot!

author: ve7xen
author_rank: Super Contributor
timestamp: April 05, 2013, 07:39:19 am
content: SPI is not really a well defined interface. There are several parameters you need to be careful of, in addition to sending the right bits on the wire. In this case, looking at the timing diagram, you can see that input bits are latched at the falling edge of SCK (so you want your controller to setup on the rising edge). You can also see that the MSB is sent first and CS is active low. I don't know anything about Arduino, but in the AVR that means you need to set DORD=1,CPOL=0,CPHA=1 (I think) in SPCR. You need to drive ~CS yourself, and of course observe the timing requirements in the datasheet.As far as what to send on the wire, the chip expects a 16-bit word in the format on page 11 of the datasheet. You can send it as two separate 8-bit words, since you're using an 8-bit micro, but make sure to observe the correct bit order (MSB first).If the descriptions of the bits are unclear, I'll paraphrase for you:D15 (R1) & D12 (R0) - Together these select a command for the data byte. If both are 0, DAC B is updated. If R0 is set, an internal buffer is written with the data you send. If R1 is set, DAC A is updated with the data you send simultaneously with DAC B being updated with whatever is stored in the internal buffer you wrote before. This so so you can update both DACs at the same instant even though it takes two commands to update them.D14 (SPD) - I don't really know what this does. It enables 'fast mode' when set, but I can't find what fast mode does from a quick skim.D13 (PWR) - When set, turn off the DACsD11-D0 (data) - Data bits to load into the DACOutput voltage of a DAC is given by Vref * (data / 2n) wherenis the number of bits in the DAC. In this case the DAC includes a 2x output amplifier, so the output is double: Vout = 2 * Vref * (data / 2n). Do some algebra and you get data = (2n* Vout) / (2 * Vref)What are you using as Vref? If you put 5V in here with 5V Vcc, you will probably damage the DAC, as it's only specified for a REF max input of Vcc - 1.5V.Anyway let's say you're using 2.5V. So to calculate the value to send, just plug it into the formula:data = (212* 2.5) / (2 * 2.5)data = (4096 * 2.5) / 5data = 10240 / 5data = 2048You might use a function like this semi-C++ to set the DAC. You can do it a lot more efficiently with fixed-point math, but whatever, you get the point:Code:const double vref = 2.5;// volts = desired output voltage// dac = 0 or 1 for DAC A or B respectivelyvoid set_dac_v(double volts, uint8_t dac) {uint16_t register;if (dac == 0)// To set DAC A we need to set R1 which is bit 15register |= (1<<15)// SPD and PWR are 0, we aren't using the BUFFER feature so we don't care about R0// Now we calculate the DAC value (1<<12 is equivalent to 2^12)// The + 0.5 will cause C's cast to integer to round instead of truncateuint16_t value = ((1<<12) * volts) / (2 * vref) + 0.5;// We'll mask the whole thing to 12 bitsvalue &= 0x0fff;// And OR it into the register we're going to send, since it belongs right-justified anywayregister |= value;// Then we just send it, MSB firstspi_send_byte(register >> 8);spi_send_byte(register & 0xff);}

author: BiOzZ
author_rank: Regular Contributor
timestamp: April 05, 2013, 07:54:08 am
content: thank you very much!for Vref i was using 3.3v because i was trying to figure it out on a bus pirate so hopefully its fine!ill test it out in the morning .... but thanks again!