Go Down

Topic: How to use transfer16 function (Read 354 times) previous topic - next topic

Jcowsky

Nov 30, 2017, 08:08 pm Last Edit: Nov 30, 2017, 08:13 pm by Jcowsky
So I'm trying to use SPI.transfer16 to communicate with an AD9834 microcontroller to send it five control words, two bytes each, as described here. My code looks like this at the moment:
Code: [Select]

  Serial.begin(9600);
  const int SS = 10;
  pinMode(SS, OUTPUT);
  SPI.begin(); // SPI setup section:
  SPI.beginTransaction(SPISettings(40000000, MSBFIRST, SPI_MODE3));

  writeWord16(0x2100, SS);
  Serial.println("Reset asserted");Serial.println();delay(5000);
  writeWord16(0x71E1, SS);
  Serial.println("Freq0 LSB sent");Serial.println();delay(1000);
  writeWord16(0x4051, SS);
  Serial.println("Freq0 MSB sent");Serial.println();delay(1000);
  writeWord16(0xC000, SS);
  Serial.println("Phase0 sent");Serial.println();delay(1000);
  writeWord16(0x2000, SS);
  Serial.println("Reset unasserted");Serial.println();delay(1000);


Where the writeWord16 function is as follows:
Code: [Select]

void writeWord16(int Word, int SSnum)
{
  digitalWrite(SSnum, LOW);
  Serial.print("Using SS pin ");Serial.println(SSnum);
  SPI.transfer16(Word);
  digitalWrite(SSnum, HIGH);
  Serial.println("Control word written");
}


The problem is that it doesn't seem to be actually getting anything to the microcontroller I'm working with. I've done some looking on google, but a lot of what I've found has been outdated and frankly just confused me more.

I've got a few ideas what might be wrong, but I'd like some insight as to what might actually help. First, I'm not sure if I need to be pulling SS low and then high again before and after the transfer16 function in writeWord16. Does the transfer16 function take care of that itself? Second, do I need to format the data I'm sending in any particular manner? (i.e. are 0x2100, 8448, and 0b0010000100000000 all equivalent?) The examples I've found online have used them pretty interchangeably, so I assume so, but I'm not sure.

Assuming that's all correct, what other problems might I be running into? Like I said earlier, I don't think anything's actually making it through to the microcontroller. I assume this is because of some syntax or misuse of the function on the Arduino's end, but I can't figure out what that might be.

I'm using an Arduino Nano, D10 is hooked up to SS, D11 to MOSI, and D13 to SCLK. Here's the datasheet for the AD9834 I'm trying to interface with: http://www.analog.com/media/en/technical-documentation/data-sheets/AD9834.pdf

pylon

Code: [Select]
SPI.beginTransaction(SPISettings(40000000, MSBFIRST, SPI_MODE3));

According to the timing diagram on page 6 (Figure 5) of the datasheet this is device uses SPI mode 2.

Quote
First, I'm not sure if I need to be pulling SS low and then high again before and after the transfer16 function in writeWord16. Does the transfer16 function take care of that itself?
No, it doesn't, your sketch is responsible to do that.

Quote
Second, do I need to format the data I'm sending in any particular manner? (i.e. are 0x2100, 8448, and 0b0010000100000000 all equivalent?)
That's translated into a binary number by the compiler, so they are equivalent.


Jcowsky

I've set the spirit mode to two, but how can you tell from the timing diagram which mode to use? I've also double checked that I'm bringing SS low before trying to transmit and pulling it high after each time it transmits, and that seems to be correct too.
It's still not actually getting anything through to the output of the board though; it won't change from whatever state it's in when I start trying to communicate with it. (I.E. if the AD9834 is putting out a sine wave I can't get it to reset to no output, and when it's got no output I can't get it to create a sine wave) Any other ideas what I might be doing wrong?

pylon

Quote
how can you tell from the timing diagram which mode to use?
I think Wikipedia has very good article about this. The diagram in the "Clock polarity and phase" section shows you quite effectively which mode to choose.

Quote
Any other ideas what I might be doing wrong?
Provide schematics of your complete setup.

Jcowsky

#4
Dec 07, 2017, 08:47 pm Last Edit: Dec 07, 2017, 10:12 pm by Jcowsky
Ok, so if it reads SDATA on the falling edge of the SCK and has SCK idle high, it's mode 2?

I wasn't sure how to embed the schematic in the post, so I've attached a screencap of it. Both the AD9834 and the Arduino Nano are connected to a laptop with a USB for power and uploading code. I think my problem might be that the laptop the AD9834 is getting power from is prohibiting the Arduino from controlling the AD9834 while it's connected, but I'm not sure about that. All connections to the AD9834 are soldered onto the eval board except DGND, which is connected by a screw down clamp. On the Arduino side, everything is connected via a breadboard.

Also, I've got all my code in the setup loop of the sketch. I can't imagine this would cause any problems, but is there a reason to put it in the main loop instead?

Edit: The L LED is on even when the Arduino isn't plugged into the laptop but is still connected to the AD9834. Could that be indicative of a problem, or is that normal? That would mean that SCLK is getting a voltage from somewhere in the AD9834, could that be overriding whatever SCK I try to send in from the Arduino?

pylon

Quote
Ok, so if it reads SDATA on the falling edge of the SCK and has SCK idle high, it's mode 2?
Yep.

Quote
I wasn't sure how to embed the schematic in the post, so I've attached a screencap of it. Both the AD9834 and the Arduino Nano are connected to a laptop with a USB for power and uploading code.
I'm missing at least a Vdd connection (to DVDD and AVDD).

The datasheet tells about a lot more that should be in the circuit. FSELECT and PSELECT should have a connection to GND even if the actual selection is done by the software (a floating pin changes the selection to often), the IOUT and IOUTB must be connected to AGND by a 200Ω resistor. FS_ADJUST sets the full scale and must connect by a resistor to AGND. COMP must be connected to AGND by a capacitor, etc.
Did you read the datasheet before designing the circuit?

Go Up