Hello, I am writing code to control and Analog Devices DAC (5372) and I have been reading up on example SPI protocol code on the forum. Below is the code I am currently using and I was wondering what advice people had to add and whether or not I am doing it right to begin with. I am using an arduino nano which I understand has a limitation as to how many bits it can send in one transfer, which is why I do 3 seperate transfers to send all of my 24-bit message to the DAC. I am very new to this and I appreciate any help I can get!
I should also add that I have the SS(pin 10) connected to the DAC's SYNC, MOSI(Pin 11) connected to SDI, and SCK (pin 13) connected to SCLK. Pins A0-A3 are connected to reset,clear,LDAC, and busy, but I don't exactly know if they are as pertinent to SPI writing to the DAC.
#include <SPI.h>
#include "pins_arduino.h"
void setup (void)
{
}
void loop (void)
{
digitalWrite(SS, LOW); //
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();
delay (5000); // 5 seconds delay to start logic analyser.
char c;
// enable Slave Select
digitalWrite(SS, HIGH); // SS is pin 10
byte address = 0x11;//Dummy value
int data = 0xffff;//Your data with the 8 bit
byte highByte = data >> 8; //high byte of data
byte lowByte = (data << 8) & 0xff; //low byte of data
//Now send it
SPI.transfer(address);
SPI.transfer(data);
SPI.transfer(highByte);
SPI.transfer(lowByte);
// disable Slave Select
digitalWrite(SS, LOW);
// turn SPI hardware off
SPI.end ();
while (1); //loop
}
Your chip is not using the SPI interface. But you still can use the SPI hardware to send the signals to that chip, given no other component in your system makes use of that interface.
If you want to execute your code exactly once (as your intention seems to be) move it to the setup() routine. Don't use loop() and add an endless while loop at the end.
Add
pinMode(SS, OUTPUT);
at the beginning of the code to activate SPI master mode.
Remove the SPI.end().
The values you currently write to the device don't make sense in my interpretation of the datasheet. Why did you choose these values?
I will try this, thanks! I am still tinkering with a lot of copy and pasted code in this program. When you say that the chip is not using the SPI interface what do you mean exactly? Also what do you think I should be using as values according to the data sheet? I assumed that the falling edge of the SS is what triggered the DAC to start reading data bits, I understand that was not really shown in the initial code so hopefully the version I have below is more accurate. Do you think it is required to split the data up into two transfers or is it alright to send in one? Also is it alright to just send it as a binary or should I just keep it as hex?
include <SPI.h>
#include "pins_arduino.h"
void setup (void)
{
pinMode(SS, OUTPUT);
digitalWrite(SS, LOW);
SPI.begin ();
delay (5000);
// enable Slave Select
digitalWrite(SS, HIGH); // SS is pin 10, rising edge
long int data = 0b110000001111111111111111;
digitalWrite(SS, LOW); //falling edge, DAC begins to read
//data transfer
SPI.transfer(data);
}
void loop (void)
{
}
When you say that the chip is not using the SPI interface what do you mean exactly?
A chip conforming to the SPI bus standard must tri-state MISO if the chip select signal is pulled high. As that chip doesn't offer such a functionality it's not actually using the SPI interface but some proprietary which can be used with many MCU's SPI hardware if that chip is the only device connected.
Do you think it is required to split the data up into two transfers or is it alright to send in one?
You must call SPI.transfer() once per byte! The method just sends one byte, the transfer is completed when the chip select goes high again (although the datasheet is not very clear about that detail but that's how SPI usually works). So after you transferred the three bytes you should write HIGH to the SS pin to activate the chip.
Also is it alright to just send it as a binary or should I just keep it as hex?
The MCU will always send it as binary, the decimal or hexadecimal notations are only for the programmer, the compiler translates all numbers into a binary format for the processor. So you can use any form in the program, use what's easiest for you, the compiler won't complain.