【问题标题】:Using SPI with arduino and LPS331AP将 SPI 与 arduino 和 LPS331AP 一起使用
【发布时间】:2013-11-06 19:35:35
【问题描述】:

您好,我一直在尝试使用 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);
}

【问题讨论】:

    标签: arduino spi


    【解决方案1】:

    在沮丧地认为这件事有一些复杂的启动后,我决定尝试另一个芯片,它的工作方式与预期的完全一样:) 所以芯片本身有问题,这就是为什么我一无所获......这真的很容易下面的代码以 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多