n00b SPI problem

Thank you bens and franklin! :slight_smile:

It was a timing issue. I added a delay(10); after asserting the slaveselect and the data started to flow. :slight_smile:

Thank you bens and franklin! :slight_smile:

It was a timing issue. I added a delay(10); after asserting the slaveselect and the data started to flow. :slight_smile:

Can you post the working code?

I have completed the code for the SCP and the following should print out temperature in Fahrenheit and pressure in Pascals. The other register are defined if you wanted to return the revision ID or opstatus etc. I think the problem the author had was not timing but never sending a initial command to choose the SCP operation mode. This line is in the setup(). Feel free to ask a question or let me know if you see something inefficient or wrong.

// define spi bus pins
#define SLAVESELECT 10
#define SPICLOCK 13
#define DATAOUT 11      //MOSI
#define DATAIN 12       //MISO
#define UBLB(a,b)  ( ( (a) << 8) | (b) ) 
#define UBLB19(a,b) ( ( (a) << 16 ) | (b) )

//Addresses
#define REVID 0x00      //ASIC Revision Number
#define OPSTATUS 0x04   //Operation Status
#define STATUS 0x07     //ASIC Status
#define START 0x0A      //Constant Readings
#define PRESSURE 0x1F   //Pressure 3 MSB
#define PRESSURE_LSB 0x20 //Pressure 16 LSB
#define TEMP 0x21       //16 bit temp

char rev_in_byte;           
int temp_in;
unsigned long pressure_lsb;
unsigned long pressure_msb;
unsigned long temp_pressure;
unsigned long pressure;

void setup()
{
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device  
  
  SPCR = B01010011; //MPIE=0, SPE=1 (on), DORD=0 (MSB first), MSTR=1 (master), CPOL=0 (clock idle when low), CPHA=0 (samples MOSI on rising edge), SPR1=0 & SPR0=0 (500kHz)
  clr=SPSR;
  clr=SPDR;
  delay(10);
  Serial.begin(9600);                        
  delay(500);

  Serial.println("Initialize High Speed Constant Reading Mode"); 
  write_register(0x03,0x09);
}

void loop()
{
  
  rev_in_byte = read_register(REVID);
  
  pressure_msb = read_register(PRESSURE);
  pressure_msb &= B00000111;
  pressure_lsb = read_register16(PRESSURE_LSB);
  pressure = UBLB19(pressure_msb, pressure_lsb);
  pressure /= 4;
  
  Serial.print("PRESSURE [");
  Serial.print(pressure, DEC);
  Serial.println("]");
  
  temp_in = read_register16(TEMP);
  temp_in = temp_in / 20;
  temp_in = ((1.8) *temp_in) + 32;
  Serial.print("TEMP F [");
  Serial.print(temp_in , DEC);
  Serial.println("]");
 
  delay(1500);
  
}

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


char read_register(char register_name)
{
    char in_byte;
    register_name <<= 2;
    register_name &= B11111100; //Read command
   
    digitalWrite(SLAVESELECT,LOW); //Select SPI Device
    spi_transfer(register_name); //Write byte to device
    in_byte = spi_transfer(0x00); //Send nothing, but we should get back the register value
    digitalWrite(SLAVESELECT,HIGH);
    delay(10);
    return(in_byte);
   
}

float read_register16(char register_name)
{
    byte in_byte1;
    byte in_byte2;
    float in_word;
    
    register_name <<= 2;
    register_name &= B11111100; //Read command

    digitalWrite(SLAVESELECT,LOW); //Select SPI Device
    spi_transfer(register_name); //Write byte to device
    in_byte1 = spi_transfer(0x00);    
    in_byte2 = spi_transfer(0x00);
    digitalWrite(SLAVESELECT,HIGH);
    in_word = UBLB(in_byte1,in_byte2);
    return(in_word);
}

void write_register(char register_name, char register_value)
{
    register_name <<= 2;
    register_name |= B00000010; //Write command

    digitalWrite(SLAVESELECT,LOW); //Select SPI device
    spi_transfer(register_name); //Send register location
    spi_transfer(register_value); //Send value to record into register
    digitalWrite(SLAVESELECT,HIGH);
}

Hurray! It works great. Thanks SO much for posting that! You saved me hours of frustration ;0)

Conor, sorry to bring up an old thread but I tried your code and it worked but I noticed if I had power to the SCP1000 it would heat up and if I removed power it still worked. How did you have yours hooked up to the Arduino? Thanks.

Franklin,

I have not noticed any over heating by the SCP. It is a 3.3 V device, so if you are using a 5v arduino to power it that might be the problem. I am using the voltage and ground from a Sparkfun wee (lillypad equivalent). Other than that, pins 10-13 for ss, sck, mosi, miso.

I'm running this code on a Funnel IO from Sparkfun, which is a 3.3v/8MHz Arduino.

I'm getting good serial communications, but all 0's when I run the code. (Every once in a while, I get random numbers).

Any ideas?

So it's now working (hurray!)

UPDATED: Scratch that, it was actually a bad wiring problem on my part.

UPDATED 2: I'm getting weird readings, like:

MSB = 0x5
LSB = 0xFFFFE6F0

Which means my pressure is 0x5FFFFE6F0, which just seems a bit high for 1000ft above sea level?

My temperature is reasonable (currently 22.8C), and adjusts accordingly, and my Pressure reading has jitter in it of about the right magnitude.

The 0xFFFF in the first half of the LSB is suspicious to me, but I'm running verbatim the code posted above.

Just as an update, I have it working now!

Here's the code I needed to do to make it work:

pressure_lsb = read_register16(PRESSURE_LSB);
pressure_lsb &= 0x0000FFFF;

Does this make any sense at all?

My read_register16 function looks thus:

float read_register16(char register_name)
{
    byte in_byte1;
    byte in_byte2;
    float in_word;
    
    register_name <<= 2;
    register_name &= B11111100; //Read command

    digitalWrite(SLAVESELECT,LOW); //Select SPI Device
    spi_transfer(register_name); //Write byte to device
    in_byte1 = spi_transfer(0x00);    
    in_byte2 = spi_transfer(0x00);
    digitalWrite(SLAVESELECT,HIGH);
    in_word = UBLB(in_byte1,in_byte2);
    return(in_word);
}

Could this be because of the cast from float to unsigned long? I'm using Arduino 0013, did the types change between then and now?

Hello,

I'm trying to get Temp and Pressure with a SCP1000 using the code above.

I've got
PRESSURE [123007]
TEMP F [21]
PRESSURE [123007]
TEMP F [21]

Strange values.

Below is the wiring to Arduino Pro Mini
SCK -> pin 13
MISO -> pin 12
MOSI -> pin 11
Now I got a doubt. From the program above :
SLAVESELECT 10

What is SLAVESELECT from the SCP1000. I'm using Sparkfun breadk out, and got only CSB / DRDY.
How should I connect SLAVESELECT ?

Thanks for your help.

Mersing.

Is it possible to use 2 scp1000 sensors with a single arduino? If so, how do you identify which is which?

MOSI, MISO, and SCK are buses, but the chip select is unique for each device. If the device is a correctly designed SPI device, just wire the second one up identical to the first, except use a different line for chip select.

-j

What is SLAVESELECT from the SCP1000. I'm using Sparkfun breadk out, and got only CSB / DRDY.
How should I connect SLAVESELECT ?

CSB should be connected to SLAVESELECT.

Does someone have the Sparkfun break out version working?

I got values like this:
PRESSURE [1073741760]
TEMP C [38]

PS: My room temp is no more then 23C

Thx
Geko

Hello everybody,

(Excuse my english, I'm french)

I have tried your code with a "Seeduino", and it works very fine.
But I need a smaller board for my project .

I 'm trying despairingly this code on a "Stickduino" or a "Pro mini" (5V with a regulator, but I have tried also with the supply of the Pro mini 3V)
Impossible to obtain correct values (it's always '0')

I have verified all the solders, all the wires....Nothing !!

Please anay idea or suggestion...?

Hi,
I just thought I'd add something to this old thread. It seems some people have been having trouble getting the code to work. I too had my fair share of problems and have posted them below in an effort to help others in the future who may have similar problems. I'm not sure if the problems of the previous 2 poster are related to mine, but perhaps they have found a solution at this stage.

Anyway, I bought the SCP1000 breakout board from sparkfun and tried to interface it to an arduino duemilnova. The first problem was the 5V I/O pins on the arduino and the 3.3V pins on the SCP1000. To overcome this I got the logic level converters also from sparkfun and wired them up. Still the code did not work. The details provided by sparkfun for the logic level converters are a little misleading if not read carefully, on the low voltage side you will get half of the voltage on the high side, and not the actual low voltage. The problem I had was that I was driving 2.5V to the SCP1000 I/O pins and had the SCP1000 powered of a 3.3V supply. There is mention in the datasheet for the SCP1000 that Vcc cannot differ from the I/O voltages by more than a certain amount. (I don't have to hand the exact values). So the solution was to power the SCP1000 of 2.5V. This actually produces less error comapared to a higher voltage according to the datasheet also, which is a bonus. I have been getting what seem to be acuarate readings of pressure and temperature ever since.

Anyway hopfully this post will save somebody some time in getting their sensor to work.

High all,

I'm new to Arduino, this Forum and how to use them, but you have to start somewhere.

I bought the Arduino Duemilanove and SCP1000 (pressure sensor from Sparkfun.) I've been reading this thread, but what I need is also a schematic how to connect the SCP1000 to the Arduino

Can anyone help me with a schematic and code for the Duemilanove.

Many thanks, DOSER.

For those who are reading this Post. This is about my Arduino Duemilanove working with a Sparkfun SPC1000 Pressure Sensor.

The code listed beneath in this Post is working OK. Except that the temp in C is a bit high. If you devide by 30 instead of 20 it is more realistic.

temp_in = read_register16(TEMP);
temp_in = temp_in / 30;
// temp_in = ((1.8) *temp_in) + 32; Add this to measure in Fahrenheit.
Serial.print("TEMP C ["); // Change C in F if above is applied.
Serial.print(temp_in , DEC);
Serial.println("]");

As a remark on the 3.3V. It has to be added to the Sensor.
But if you use a LD1117V33 voltage regulator you can support this with the 5V from the Arduino board and has 3.3V available for the SPC1000.

connect the following SPC1000 ports to the Arduino Duemilanove
DRDY - not connected
CSB - I/O port 10
MISO - I/O port 12
MOSI - I/O port 11
SCK - I/O port 13
3.3V - Vout of the LD1117V33
GND - Groud

I've had it running in 15 min. Great. :smiley: :smiley:

Greatings from the Netherlands.

DOSER.

Hi,

Just one question.... I obtain something like this for the pressure:
PRESSURE [1073741760]
in what units? 1073,741760 mbar? or...
Thanks!

Hi there,

normally the world pressure will be between 980 hPa and 1040 hPa. A value of 1073 hPa or the older value of mBar is way to high. Maybe with a the addition of a constant like 1073 x 0.95 = 1020 to get a realistic value.

In my setup I got the exact pressure in Pascal and had to convert it to hPa, by adding a constant of 0,01.

Temperature was a bigger problem, I got something of 30 degrees C on a 20 degrees C day, so there I had to adjust this value.

Normally is works OK.

Good luck.