Pregunta

Hi I've been trying to write to the LPS331AP with SPI and I can't seem to. I'm able to read fine though. And using a logic analyzer I can see I am sending what I think I"m sending so I think I have a misunderstanding of what bits to send to the pressure sensor to write to it.

Here's my code:

#include <SPI.h>

byte WHO_AM_I = 0B00001111;
byte READCTRL_REG1 = 0B10100000;
byte CTRL_REG1 =     0B00100000;

const int CS = 10;

//SPI.h sets these for us
/*
const int SDI = 11;
const int SDO = 12;
const int SCL = 13;
*/

void setup() {
Serial.begin(9600);
// start the SPI library;
SPI.begin();
// initalize the chip select pin;
pinMode(CS, OUTPUT);


byte Write = CTRL_REG1;
byte Value = 0B11100000;

digitalWrite(CS, LOW); 

SPI.transfer(Write);
SPI.transfer(Value);

digitalWrite(CS, HIGH);
delay(1000);
}


void loop() {

byte result = 0;
byte Read = READCTRL_REG1;  

digitalWrite(CS, LOW); 

Serial.println(Read, BIN);
SPI.transfer(Read);
result = SPI.transfer(0x00);
Serial.println(result, BIN);

digitalWrite(CS, HIGH);
delay(1000);
}
¿Fue útil?

Solución

After some frustration thinking there's some complex startup to this thing I decided to try another chip and it worked exactly like it's supposed to : ) So something was wrong with the chip itself that's why I was getting nowhere...It's really easy and the code below gets your temperature in C and your pressure in mbars(using SPI).

#include <SPI.h> //add arduino SPI library;
//list of all registers binary addresses;
byte REF_P_XL           = 0B00001000;
byte REF_P_L            = 0B00001001;
byte REF_P_H            = 0B00001010;
byte WHO_AM_I           = 0B00001111;
byte RES_CONF           = 0B00010000;
byte CTRL_REG1          = 0B00100000;
byte CTRL_REG2          = 0B00100001;
byte CTRL_REG3          = 0B00100010;
byte INT_CFG_REG        = 0B00100011;
byte INT_SOURCE_REG     = 0B00100100;
byte THS_P_LOW_REG      = 0B00100101;
byte THS_P_HIGH_REG     = 0B00100110;
byte STATUS_REG         = 0B00100111;
byte PRESS_POUT_XL_REH  = 0B00101000;
byte PRESS_OUT_L        = 0B00101001;
byte PRESS_OUT_H        = 0B00101010;
byte TEMP_OUT_L         = 0B00101011;
byte TEMP_OUT_H         = 0B00101100;
byte AMP_CTRL           = 0B00110000;

byte Read  = 0B10000000;
byte Write = 0B00000000;

//chip select pin on arduino;
const int CS = 10;
/*
SPI.h sets these for us in arduino
const int SDI = 11;
const int SDO = 12;
const int SCL = 13;
*/

void setup() {
  Serial.begin(9600);
  //start the SPI library;
  SPI.begin();
  //initalize the chip select pins;
  pinMode(CS, OUTPUT);

//activate borometer at 12.5Hz
WriteRegister(CTRL_REG1,0B11100000);
delay(100);

}

void loop() {

byte Temp_H = ReadRegister(TEMP_OUT_H);
byte Temp_L = ReadRegister(TEMP_OUT_L);  
byte Pressure_H = ReadRegister(PRESS_OUT_H);  
byte Pressure_L = ReadRegister(PRESS_OUT_L);
byte Pressure_XL = ReadRegister(PRESS_POUT_XL_REH);


int Temperature = Temp_H <<8 | Temp_L;
long int Pressurei = Pressure_H <<8 | Pressure_L;
long int Pressure = Pressurei <<8 | Pressure_XL;


Serial.print("Pressure_H: ");
Serial.println(Pressure_H, BIN);
Serial.print("Pressure_L: ");
Serial.println(Pressure_L, BIN);
Serial.print("Pressure_XL: ");
Serial.println(Pressure_XL, BIN);

Serial.print("Pressure: ");
Serial.println(Pressure/4096, DEC);
Serial.print("Temperature: ");
Serial.println(42.5 + (Temperature/480), DEC);

delay(1000);

}

byte ReadRegister(byte Address){
  byte result = 0;
  digitalWrite(CS, LOW); 
  SPI.transfer(Read | Address);
  result = SPI.transfer(0x00);
/*  Serial.print(Address, BIN);
  Serial.print(" : ");
  Serial.println(result, BIN); */
  digitalWrite(CS, HIGH);
return(result);  
}

void WriteRegister(byte Address, byte Value){
  digitalWrite(CS, LOW);
  SPI.transfer(Write | Address);
  SPI.transfer(Value);
  digitalWrite(CS, HIGH);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top