标签:
LPS25H是ST生产的MEMS数字气压传感器,一些文档里也叫LPS331AP。主要特性有:
因为传感器IC大多工作在3.3V附近,因此干脆用工作于3.3V/8MHz版本的Arduino Pro Mini进行调试,避免了用UNO时接口电平转换的麻烦。采用I2C接口进行通讯。未利用INT1和FIFO的功能。
LPS25H Pro Mini 3.3V/8MHz
VDD <------> 3.3V
GND <------> GND
SCL <------> A5 (SCL)
SDA <------> A4 (SDA)
1 /* 2 Barometer based on LPS25H sensor and Arduino Pro Mini(3.3V) 3 */ 4 5 #include <Wire.h> 6 7 #define ADDRESS_LPS25H 0x5D 8 #define CTRL_REG1 0x20 9 #define CTRL_REG2 0x21 10 #define PRESS_OUT_XL 0x28 11 12 byte buffer[5]; 13 14 boolean ready = false; 15 int tempOut; 16 long presOut; 17 float tempVal; 18 float presVal; 19 20 void setup() 21 { 22 Wire.begin(); 23 Serial.begin(9600); 24 25 //power down the device (clean start) 26 Wire.beginTransmission(ADDRESS_LPS25H); 27 Wire.write(CTRL_REG1); 28 Wire.write(0x00); 29 Wire.endTransmission(); 30 31 //turn on the sensor, set the one-shot mode, and set the BDU bit 32 Wire.beginTransmission(ADDRESS_LPS25H); 33 Wire.write(CTRL_REG1); 34 Wire.write(0x84); 35 Wire.endTransmission(); 36 } 37 38 void loop() 39 { 40 //run one-shot measurement 41 Wire.beginTransmission(ADDRESS_LPS25H); 42 Wire.write(CTRL_REG2); 43 Wire.write(0x01); 44 Wire.endTransmission(); 45 46 //wait until the measurement is completed 47 while (ready == false) 48 { 49 delay(5); //conversion time: ~37ms 50 Wire.beginTransmission(ADDRESS_LPS25H); 51 Wire.write(CTRL_REG2); 52 Wire.endTransmission(); 53 Wire.requestFrom(ADDRESS_LPS25H, 1); 54 if (Wire.read() == 0x00) 55 { 56 ready = true; 57 } 58 // Serial.println("waiting..."); 59 } 60 61 //read the result 62 Wire.beginTransmission(ADDRESS_LPS25H); 63 Wire.write(PRESS_OUT_XL | 0x80); //read multiple bytes 64 Wire.endTransmission(); 65 66 Wire.requestFrom(ADDRESS_LPS25H, 5); 67 if (Wire.available() >= 5) 68 { 69 for (int i = 0; i < 5; i++) 70 { 71 buffer[i] = Wire.read(); 72 } 73 } 74 ready = false; 75 76 //calculation 77 presOut = (long(buffer[2]) << 16) | (long(buffer[1]) << 8) | long(buffer[0]); 78 presOut = (presOut << 8) >> 8; //PRESS_OUT_H/_L/_XL and is represented as 2’s complement 79 presVal = presOut/4096.0; 80 81 tempOut = (buffer[4] << 8) | buffer[3]; 82 tempVal = 42.5 + tempOut/480.0; 83 84 Serial.print(presVal); Serial.print(" hPa\t"); 85 Serial.print(tempVal); Serial.println(" `C"); 86 87 delay(2000); 88 }
MCU每隔两秒测量一次气压和温度数据,并通过串口打印结果。
标签:
原文地址:http://www.cnblogs.com/zlbg/p/4237201.html