SPI Stall

I’m trying to get the SPI working, and it seems to be stalling when using the write_spi function with the potentiometer example. I’m using an arduino non ng usb. I’m using a different pot, it works sometimes but “stalls” most of the time. The code I’m using has the pot acting a voltage divider, ADC 0 measures the potential on the Wiper, the A terminal is at 5V and the B terminal is grounded. An external LED (on pin 7) turns on before the spi write and off right after (this is how I detect the stall, the LED won’t turn off).

Anyhow, here’s the code:

#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK  13//sck
#define SLAVESELECT 9//ss

byte pot=0;
byte resistance=0;
char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

void setup()
{
  Serial.begin(9600);
  byte i;
  byte clr;
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(6,LOW);
    digitalWrite(7,LOW);
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
  // SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 (fastest)
  SPCR = (1<<SPE)|(1<<MSTR);
  clr=SPSR;
  clr=SPDR;
  delay(10);


}

byte write_pot(int value)
{
  digitalWrite(SLAVESELECT,LOW);
  //3 byte opcode
  spi_transfer(64);
  spi_transfer(0);
  spi_transfer(value);
  digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}

void loop()
{
  
  digitalWrite(7,HIGH);
 write_pot(100);
  pot++;

  Serial.print(pot);
  Serial.print(" : ");
  Serial.println(analogRead(0));
    digitalWrite(7,LOW);
      delay(1000);
}

I’m new to SPI, but even with the chip unhooked the code stalls, so I’m wondering if there’s a problem with the Arduino, I’ve read about problems with the NG but this is the old one.

Thanks in advance.

Read this thread, SPI is not working correctly without some modification on the board:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1163734586/7#7

Ah thanks: is this the case even if I don't have an NG board, mine looks different from the one in the picture so I'm not sure what to modify.

Do not modify your board

that mod applies only to Arduino NG with on-board LED

massimo

Do not modify your board

that mod applies only to Arduino NG with on-board LED

massimo

Oops... Sorry, didn't see he was using an earlier version.... :-[

When the code doesn’t stall, does the IO to the digital pot seem to work? i.e., do you get expected (or at least different) values on the voltage divider?

You say you’re using a different pot; is it by the same manufacturer?

SPI has 4 different “modes” based on clock polarity and phase. Evidently this wasn’t clearly defined in the standard, so there are implementation variations. The two SPCR bits that affect this are CPOL and CPHA. The code for the digital pot and EEPROM tutorials use 0 for both those values, so the SPCR setup looks like

  SPCR = (1<<SPE)|(1<<MSTR);

The other three settings are:

  SPCR = (1<<SPE)|(1<<MSTR)|(1<<CPOL);
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<CPHA);
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA);

The last one (of course :slight_smile: ) worked for the DS1722 I am playing with.

You may try changing these SPI clock settings (especially if your digital pot is by a vendor other than Analog Devices) and see if it helps.

-j

What is bizarre is that the stall occurs regardless of what is hooked up to the board: If I take an Arduino that has nothing attached except the LED, it stalls.

I will try the other clock modes. Thanks!

What is bizarre is that the stall occurs regardless of what is hooked up to the board: If I take an Arduino that has nothing attached except the LED, it stalls.

Ah, well that's a different animal. I thought you meant it stalls when talking to the device via SPI. If it stalls no matter what's connected, I doubt changing the SPI clock mode will make a difference.

-j

If someone else could try that code and see if it stalls that would be great: I've tried it on two different units and it seems to on both, when I comment out the write_spi line in the loop it doesn't stall, so I'm guessing that the error is related to that.