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