Go Down

Topic: n00b SPI problem (Read 13 times) previous topic - next topic


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.

Code: [Select]
// 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) )

#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(DATAIN, INPUT);
 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)

 Serial.println("Initialize High Speed Constant Reading Mode");

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);
 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);


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

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);
   in_word = UBLB(in_byte1,in_byte2);

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


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.



    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?

Go Up