0

I am attempting to use an arduino MEGA 2560 to control a digital potentiometer (AD5206), but I am getting some strange behavior. I have attached the CLK and MOSI pins (52 and 51, respectively) to an oscilloscope, and have run the following code:

#include <SPI.h>
void setup() {
    SPI.begin();
    SPI.transfer(5);
    delay(500);
}

void loop() {
    SPI.transfer(5);
    delay(500);
}

When I look at the signal, the transfer in the setup() looks correct (5V x 00000011), but then the transfer in the loop() looks like it's shifted by -5V (-5V x 11111100). Has anyone ever run into this? Is it possible for the MEGA to output a negative voltage?

  • You are not controlling the chip properly. Read the datasheet, and learn how SPI communication works, then you will know what is blatantly missing from your code. – Majenko Apr 28 '17 at 19:43
  • I realize that the above code is not sufficient to control the AD5206. I have only posted a small piece of code that demonstrates the issue that I am seeing. The problem is that the 8bit spi signal is being shifted by -5V in the loop (but not in the setup). – john g. Apr 29 '17 at 20:38
  • Except where is the start of the 8 bit SPI signal? Where is your chip select control that frames each transaction and tells the chip where the start of communication is? – Majenko Apr 29 '17 at 20:45
  • In my actual code, I pull the SS low, send the pin address and the resistor value (2x8 bit signals). The code that I have posted is the most concise script that I can post that creates the issue that I am asking about. I am not concerned with the serial formatting for communicating with the AD5206. My question here is purely concerned with the Arduino serial signal being shifted by -5V in the loop. – john g. Apr 30 '17 at 21:05
  • Well, as it stands that code is invalid and doesn't demonstrate anything at all. With no synchronisation there is no clue what the different clock pulses mean. – Majenko Apr 30 '17 at 21:07
1

No, there is no problem with SPI on the Mega. If there were it would have been noticed by now.

Instead it's either the way you are measuring it or your method of generating the SPI signals that are at fault (and since you won't show us your real code we can't help you with that).

This is a trace of your provided "code" with the delays reduced to allow easier fitting on a single trace:

enter image description here

Clock goes low when SPI.begin() is called. Then there are the successive transfers - the first being in setup() and the rest in loop(). As you can see they are all identical.

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.