Problem with SPI Flash memory

Hi all,
I try to read the Manufacturer ID of a SPI Flash W25Q80BV. The flash is 3.3V, so I use a CD4050 for level shifting 5V to 3.3V for SCK, SS, MOSI. MISO is connected directly between the flash and the Arduino.

The length wire is 15cm, and I have to slow down the clock to 1MHz in order to have no variation on the output of the flash.
I read "F8h" at this time in continous, whereas the datasheet indicates "EFh" for Spansion Manufacturer.

The code is very simple, so I don't know why I couldn't read the good value.
I use a Breadboard Mini Self-Adhesive and a Protoshield from sparkfun

Hereunder my code:

//SPI flash programmer for Winbond W25Q80BV DIP8
//Strap wire length 15cm
// CD4050 used as level shifter 5V to 3.3V
//Pins:
//Flash    CD4050    Arduino Uno
//  8(VCC)   1(VDD)    3.3V
//  1(/CS)   2(OUTA)        
//           3(INA)    10(SS)
//  5(DI)    4(OUTB)        
//           5(INB)    11(MOSI)
//  6(CLK)   6(OUTC)        
//           7(INB)    13(SCK)
//  4        8(VSS)    GND
//  2(DO)              12(MISO)
//  3(/WP)             3.3V
//  7(/HOLD)           3.3V
// CD4050 datasheet: http://www.fairchildsemi.com/ds/CD/CD4049UBC.pdf
// W25Q80BV datasheet: http://www.winbond.com/NR/rdonlyres/4D2BF674-7427-4FC8-AEF0-1A534DF74F16/0/W25Q80BV.pdf


#include <SPI.h>

//Pin Definition
//#define MOSI    11
//#define MISO    12 
//#define SCK     13
#define SS      10

//SPI command
#define ID    0x9F

//Read Manufacturer, Memory type and capacity
void deviceid()
{
  digitalWrite(SS,LOW); //start spi transfert
  SPI.transfer(ID);     //send JEDEC ID command
  byte MANUFACTURERID = SPI.transfer(0xFF);
  byte MEMORYTYPE = SPI.transfer(0xFF);
  byte CAPACITY = SPI.transfer(0xFF);
  digitalWrite(SS,HIGH); //end spi transfert

  Serial.println(MANUFACTURERID,HEX); //should read "EFh"

}


void setup()
{
  Serial.begin(9600);
  
//  digitalWrite(SS,HIGH); //already declared in SPI.cpp
//  pinMode(SS, OUTPUT);   //already declared in SPI.cpp
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV16); //16MHz div by 16=1MHz
  SPI.setDataMode(SPI_MODE0);
  SPI.begin();
  
}

void loop()
{
  deviceid();
  delay(100);
}

When I connect a Saleae logic anlyser, the output change to FFh, due to the input impedance of the analyser. So, I couldn't use the logic analyser to help me.

First, could you check my code, and tell me if it is correct or not ?

Uncomment these in void setup, put in this order:

// pinMode(SS, OUTPUT); //already declared in SPI.cpp
// digitalWrite(SS,HIGH); //already declared in SPI.cpp

I don't think SS is declared.
The master SPI device needs to know that D10 is an output to go into SPI master mode.

Thanks for the response. I have already try it, without sucess.

In SPI.cpp, there is the code for init:

void SPIClass::begin() {
  // Set direction register for SCK and MOSI pin.
  // MISO pin automatically overrides to INPUT.
  // When the SS pin is set as OUTPUT, it can be used as
  // a general purpose output port (it doesn't influence
  // SPI operations).

  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);
  
  digitalWrite(SCK, LOW);
  digitalWrite(MOSI, LOW);
  digitalWrite(SS, HIGH);

  // Warning: if the SS pin ever becomes a LOW INPUT then SPI 
  // automatically switches to Slave, so the data direction of 
  // the SS pin MUST be kept as OUTPUT.
  SPCR |= _BV(MSTR);
  SPCR |= _BV(SPE);
}

So, you could see that SS is already declared, and set to output.
I have see that if I used SPI_CLOCK_DIV16 the output is "F8" and sometimes "FC", and if I used SPI_CLOCK_DIV8 or less, the output stuck at "0". So I conclude to a timing problem, perhaps caused by the lenght of the jump wires ?????
I have to find a oscilloscope in order to see the waveform.

Do you think that the code is correct ? if yes, I will check the timing and the hardware.

Your code looks OK. I have found that fast SPI can be problematic with longer cable runs. If attaching the Logic analyzer changes results that suggests you are marginal. I can usually connect the Logic without altering results.

Bump.. Does anyone know why this would happen or how to fix it? I'm having the same result, when I connect my oscilloscope, it distorts the values read over spi to 0xFF values. How can I prevent this?

Pat

Use a dedicated logic probe or 10:1 oscilloscope probes. They will put less load on the circuit.

SPI is normally pretty strong against that kind of influence. Show us your circuit, please. There may be another problem.