0

I am trying to program an AD5930 with my Arduino Mega, and I suspect that I have a software issue or a broken Mega. I am fairly new to coding Arduino's so I am hoping someone here knows better.

Here is my code:

#include <SPI.h>

#define DATAOUT 51//MOSI
#define SPI_CLOCK  52//sck
#define SLAVESELECT_1 53//ss
#define CNTRL 36//ss

int freq1 = 0;
int BurstLength1 = 0;
int freq2 = 0;
int BurstLength2 = 0;
int PRF_RATIO = 100;

void setup(){
  Serial.begin(9600);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE1);
  byte i;
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(CNTRL, OUTPUT);
  digitalWrite(CNTRL, LOW);
  pinMode(SPI_CLOCK, OUTPUT);
  pinMode(SLAVESELECT_1,OUTPUT);
  digitalWrite(SLAVESELECT_1,HIGH); //disable device
  SPI.begin();
  digitalWrite(SLAVESELECT_1,LOW); //Enable device
  SPI.transfer(0x0E);  //Send  Fstart Write Enable to Control Reg 
  SPI.transfer(0x13);
  digitalWrite(SLAVESELECT_1,HIGH);
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0xC7);  //Send Frequency to Fstart one byte at a time
  SPI.transfer(0x4C);
  digitalWrite(SLAVESELECT_1,HIGH);
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0xD0);  //Send Frequency to Fstart one byte at a time
  SPI.transfer(0x93);
  digitalWrite(SLAVESELECT_1,HIGH);
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0x06);  //Send  Fdelta Write Enable to Control Reg 
  SPI.transfer(0x13);
  digitalWrite(SLAVESELECT_1,HIGH);
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0x20);  //Send Frequency to Fdelta one byte at a time
  SPI.transfer(0x00);
  digitalWrite(SLAVESELECT_1,HIGH);      
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0x30);  //Send Frequency to Fdelta one byte at a time
  SPI.transfer(0x00);
  digitalWrite(SLAVESELECT_1,HIGH);
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0x10);  //Send Number of Increments
  SPI.transfer(0x02);
  digitalWrite(SLAVESELECT_1,HIGH);
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0x40);  //Send tINT Register
  SPI.transfer(0xEB);
  digitalWrite(SLAVESELECT_1,HIGH);
  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0x80);  //Tburst Register
  SPI.transfer(0x0A);
  digitalWrite(SLAVESELECT_1,HIGH);
  SPI.end();
}

void loop() {
  digitalWrite(CNTRL, HIGH);
  delay(1);
  digitalWrite(CNTRL, LOW);
  delay(25);
}

Logic Analyzer

I hooked up the Arduino to a logic analyzer and it looks like the code inside the setup function isn't happening. In the logic analyzer we can see that the control pin is being toggled which is the instruction in the loop method.

I know that I am getting a whole cycle as you can see the control pin toggle in the past as well when I trigger on the reset button in the logic analyzer.

Reset Trigger

This same result of nothing is happening in the ICSP header and pins 51(Data), and 52(Clk).

I hope I am just making a stupid mistake in the code. Thanks to whomever takes pity on me.

2
  • Try programming the suspected data pin as a GPIO and toggling that in your loop, too. Also try setting the clock divider to 128 or something slow, and putting a delay in between your manipulation of the slave select and your SPI transfer - at the lest those should all make it easier to capture anything which is going on. You could also consider finding Mega-compatible code for some other SPI gizmo and running it just to see the lines wiggle as a confidence check Aug 15 '14 at 15:40
  • @ChrisStratton Stratton Thanks, I will try all of these things when I get into the lab in about 9 hours. Aug 15 '14 at 16:42
0

I tested your setup with my Mega, and it worked OK after a couple of changes.

  • You should really set SS high first, before even making it an output. That way the pull-up is activated, and then the moment you make it output it stays high, rather than going to low, then back to high, which looks to the target like a SS pulse.
  • You don't need to do pinMode() calls on the other SPI pins.
  • Call SPI.begin() last - after setting SS high.

void setup(){

  Serial.begin(9600);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE1);

/// -----CHANGES--------
  digitalWrite(SLAVESELECT_1,HIGH); //disable device
  pinMode(SLAVESELECT_1,OUTPUT);
/// -------------

  SPI.begin();

  digitalWrite(SLAVESELECT_1,LOW); //Enable device
  SPI.transfer(0x0E);  //Send  Fstart Write Enable to Control Reg 
  SPI.transfer(0x13);
  digitalWrite(SLAVESELECT_1,HIGH);

  digitalWrite(SLAVESELECT_1,LOW);
  SPI.transfer(0xC7);  //Send Frequency to Fstart one byte at a time
  SPI.transfer(0x4C);
  digitalWrite(SLAVESELECT_1,HIGH);

  ...

As you can see from the logic analyzer, the output is OK:

Logic analyzer output

The numbers look right too:

Time [s], Analyzer Name, Decoded Protocol Result
-0.000001752000000,SPI,MOSI: 0x0E;  MISO: 0x00
0.000001248000000,SPI,MOSI: 0x13;  MISO: 0x00
0.000014496000000,SPI,MOSI: 0xC7;  MISO: 0x00
0.000017496000000,SPI,MOSI: 0x4C;  MISO: 0x00
0.000030744000000,SPI,MOSI: 0xD0;  MISO: 0x00
0.000033744000000,SPI,MOSI: 0x93;  MISO: 0x00
0.000046992000000,SPI,MOSI: 0x06;  MISO: 0x00
0.000049992000000,SPI,MOSI: 0x13;  MISO: 0x00
0.000063240000000,SPI,MOSI: 0x20;  MISO: 0x00
0.000066240000000,SPI,MOSI: 0x00;  MISO: 0x00
0.000079488000000,SPI,MOSI: 0x30;  MISO: 0x00
0.000082486000000,SPI,MOSI: 0x00;  MISO: 0x00
0.000095736000000,SPI,MOSI: 0x10;  MISO: 0x00
0.000098734000000,SPI,MOSI: 0x02;  MISO: 0x00
0.000111984000000,SPI,MOSI: 0x40;  MISO: 0x00
0.000114982000000,SPI,MOSI: 0xEB;  MISO: 0x00
0.000128232000000,SPI,MOSI: 0x80;  MISO: 0x00
0.000131230000000,SPI,MOSI: 0x0A;  MISO: 0x00

Try the changes to the code, and see what happens.

1
  • Did this answer help? If so, please accept it. If not, please explain why not. Stack Exchange is recycling this question as "unanswered". To help others, it is nice to know whether or not the answer worked.
    – Nick Gammon
    Feb 8 '16 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.