Spi and sensor scp100

hello all, I have each day trying to run a barometric sensor, SCP100 via SPI, but without success, I tried to adapt the C + + code from a Arduino to micro.net.
My problem is mostly the SPI communication.

if anyone can help me with translation of the code, I would appreciate very much.
greetings
-------------ARDUINO CODE----------------

//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;       
float temp_in; 
double altitude;
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  
  //digitalWrite(10, HIGH); //physical SS pin high before setting SPCR
  
  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,0x0A); 
  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; 
  
  pressure=pressure/100;
  
  Serial.print("PRESSURE ["); 
  Serial.print(pressure, DEC); 
  Serial.println("] Hpa"); 
  
  temp_in = read_register16(TEMP); 
  temp_in = temp_in / 20; 
  temp_in = (temp_in / (1.8)); 
} 

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


------------MY CODE--------------------------------------

public void Inicializacion() 
{ 
    // Create a new SPI 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) 



    SPI_Out = new SPI(new SPI.Configuration(
                         EmbeddedMaster.Pins.E43, // SS on GPIO pin 10
                         false,                  // SS active-low
                         0,                      // No setup time
                         0,                      // No hold time
                         false,                  // Clock low on idle
                         false,                  // Data valid on falling edge
                         500,                   // 500khz clock rate
                         SPI.SPI_module.SPI1     // SPI device 1
                         )
                         );

    Thread.Sleep(500);

    Debug.Print("RESET\r\n");
    WriteReg8(0x06, 0x01);
    Thread.Sleep(500);


    Debug.Print("Initialize High Speed Constant Reading Mode");
    WriteReg8(0x03, 0x0A);
    Thread.Sleep(500);

    rev_in_byte = ReadReg8(REVID);
    
    pressure_msb = ReadReg8(PRESSURE);
    pressure_msb &= 0x07;
    pressure_lsb = ReadReg16(PRESSURE_LSB);
    pressure = ((pressure_msb << 16) | pressure_lsb);
    pressure /= 4;
    pressure = pressure / 100;

    temp_in = ReadReg16(TEMP);
    temp_in = temp_in / 20;
    temp_in = (temp_in / (1.8)); 


}
protected byte ReadReg8(byte Reg)
{
    byte[] Regx8 = new byte[1];
    byte[] Buff1x8 = new byte[1];

    Reg <<= 2;
    Reg &= 0xFC; //Read command

        Regx8[0] = Reg;
        SPI_Out.WriteRead(Regx8, Buff1x8, 1);
        SPI_Out.Write(new byte[] { 0x00 });
        return Buff1x8[0];
}



protected void WriteReg8(byte Reg, byte Value)
{
    Reg <<= 2;
    Reg |= 0x02; //le estamos diciendo que escriba
    byte[] Buff2x8 = new byte[ 2 ];

        Buff2x8[0] = Reg;
        Buff2x8[1] = Value;
        SPI_Out.Write(Buff2x8);
}

protected ushort ReadReg16(ushort Reg)
{
 ushort[] Regx16 = new ushort[ 1 ];
 ushort[] Buff1x16 = new ushort[1];

 Reg <<= 2;
 Reg &= 0xFC; //Read command


        Regx16[0] = Reg;
        SPI_Out.WriteRead(Regx16, Buff1x16, 1);
        SPI_Out.Write(new byte[] { 0x00 });
        return Buff1x16[0];
}

I modified your post so the code is more readable. Also, you look like you are using Embedded Master not FEZ…This forum is for FEZ only.

Now, to your question…I looked everywhere on the web and i am not able to find the datasheet of your sensor! Do you have a link?
Are you sure about the SS (slave select) pin? You use pin 43 but then the comment is for IO10!!
Also, are you sure it is SPI1? not SPI2? What pins did you use?

I did a quick look at your code and your read routines look wrong, the 8 and the 16.

This is what you have

protected byte ReadReg8(byte Reg)
{
    byte[] Regx8 = new byte[1];
    byte[] Buff1x8 = new byte[1];
 
    Reg <<= 2;
    Reg &= 0xFC; //Read command
 
        Regx8[0] = Reg;
        SPI_Out.WriteRead(Regx8, Buff1x8, 1);
        SPI_Out.Write(new byte[] { 0x00 });
        return Buff1x8[0];
}
 

I think it should be like this (I didn’t test this code)

protected byte ReadReg8(byte Reg)
{
    byte[] temp = new byte[2];
    byte[] Buff1x8 = new byte[1];
 
    Reg &= 0xFC; //Read command
 
        temp[0] = Reg;
        SPI_Out.WriteRead(temp, temp);//I am using same buffer to send and receive. First byte is send and the second byte is receive
        return temp[1];//the second byte is what we have read
}
 

You will also need to fix the readreg16

hi, thanks for the reply.

Sorry for the error sensor name. SCP1000 is the correct name, I miss a zero.
The motherboard I’m using is the “USBizi.” I have it connected to the PIN Header2 E43 (USBizi c #) —> SSEL0.

http://www.sparkfun.com/datasheets/Components/scp1000_product_family_specification_rev_0.08-3.pdf

proved the change of the function of reading data from 8 and 16 bits.
thank you very much for your reply.

The old board you have is not “beginner friendly” like FEZ. Also, it was discontinued months ago. So, if you want to use the board, you are on your own. No support is provided.

I highly recommend you get a FEZ Domino to get you started and then you can use the old board once you learn about USBizi.

To see the differences between the boards, check the schematics
http://www.ghielectronics.com/downloads/USBizi/USBizi-DevSys_sch.pdf
http://www.tinyclr.com/downloads/Domino/FEZDomino_sch.pdf

Do not forget to update the firmware to the latest 4.0. From your code, it looks like you are using an old 3.0 SDK?

I did give you the fix for your code…you should use it and it will probably work but since you are not using FEZ, no more support please…all I know, you could be using the wrong pins and we would spend days trying to solve this :frowning: