2

嗨,我一直在尝试用 SPI 写入 LPS331AP,但我似乎做不到。不过我能读得很好。使用逻辑分析仪我可以看到我正在发送我认为我正在发送的内容,所以我认为我误解了要发送到压力传感器以写入它的位。

这是我的代码:

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

1 回答 1

0

经过一些挫折,认为这件事有一些复杂的启动,我决定尝试另一个芯片,它的工作方式与预期的完全一样:)所以芯片本身出了点问题,这就是为什么我一无所获......这真的很容易,而且代码下面以 C 为单位获取您的温度,以 mbars 为单位获取您的压力(使用 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);
}
于 2013-10-31T11:58:27.770 回答