<code class="lang-text">/*
arduino到bmp085
SDA--A4
SCL--A5
GND--GND
VIN--3.3V(切记是3.3V,不是5V或者VIN,切记!!!)
arduino--SD(TF)模块
5V--D7
CS--D4
MOSI--D11
SCK--D13
MISO--D12
GND--GND
arduino
错误指示灯--D10
运行指示灯--D9
arduino--MOS模块
GND--GND
D8--SIG
不反对用于利润小于所有成本(包括一小时20的正常焊工的工资)的20%及以下的商业活动
转载请注明来源为 bbs.kechuang.org
KC—h13
*/
#include <sd.h>
#include <wire.h>
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
const unsigned char OSS = 0; // Oversampling Setting
const int chipSelect = 4;
// Calibration values
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5;
short temperature;
long pressure;
void setup()
{
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);
pinMode(8,OUTPUT);
digitalWrite(8,LOW);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
Wire.begin();
bmp085Calibration();
Serial.print("Initializing SD card..."); //串口输出数据Initializing SD card...
Serial.print("temperature pressure");
if (!SD.begin(chipSelect)) { //如果从CS口与SD卡通信失败,串口输出信息Card failed, or not present
Serial.println("Card failed, or not present");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
return;
}
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
Serial.println("card initialized."); //与SD卡通信成功,串口输出信息card initialized.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("temperature pressure");
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
Serial.println("error opening datalog.txt");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
dataFile.close();
}
void loop()
{
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
int aa = temperature / 10;
Serial.print(aa, DEC);
Serial.print(" ");
Serial.print(pressure, DEC);
Serial.println();
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
dataFile.print(aa,DEC);
dataFile.print(" ");
dataFile.print(pressure, DEC);
dataFile.println();
Serial.println("datalog.txt has printed");// 串口输出datalog.txt has printed
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
Serial.println("error opening datalog.txt");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
delay(40);
int h = pressure;
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
int bb = temperature / 10;
Serial.print(bb, DEC);
Serial.print(" ");
Serial.print(pressure, DEC);
Serial.println();
if (dataFile) {
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
dataFile.print(bb,DEC);
dataFile.print(" ");
dataFile.print(pressure, DEC);
dataFile.println();
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
Serial.println("error opening datalog.txt");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
delay(40);
int i = pressure;
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
int cc = temperature / 10;
Serial.print(cc, DEC);
Serial.print(" ");
Serial.print(pressure, DEC);
Serial.println();
if (dataFile) {
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
dataFile.print(cc,DEC);
dataFile.print(" ");
dataFile.print(pressure, DEC);
dataFile.println();
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
Serial.println("error opening datalog.txt");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
delay(40);
int j = pressure;
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
int dd = temperature / 10;
Serial.print(dd, DEC);
Serial.print(" ");
Serial.print(pressure, DEC);
Serial.println();
if (dataFile) {
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
dataFile.print(dd,DEC);
dataFile.print(" ");
dataFile.print(pressure, DEC);
dataFile.println();
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
Serial.println("error opening datalog.txt");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
delay(40);
int k = pressure;
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
int ee = temperature / 10;
Serial.print(ee, DEC);
Serial.print(" ");
Serial.print(pressure, DEC);
Serial.println();
if (dataFile) {
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
dataFile.print(ee,DEC);
dataFile.print(" ");
dataFile.print(pressure, DEC);
dataFile.println();
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
Serial.println("error opening datalog.txt");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
dataFile.close();
Serial.println("saved");//串口输出saved
delay(40);
int l = pressure;
if((h<i) && (i<j) (j<k) (k<l) (l>h + 50)){
digitalWrite(8,HIGH);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("OPENED");
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
Serial.println("error opening datalog.txt");
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}
}
}
// Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
}
// Calculate temperature given ut.
// Value returned will be in units of 0.1 deg C
short bmp085GetTemperature(unsigned int ut)
{
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
return ((b5 + 8)>>4);
}
// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up)
{
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<oss) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1) b4; else p="(b7/b4)<<1;" x1="(p">>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
return p;
}
// Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 1);
while(!Wire.available())
return Wire.read();
}
// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2) msb="Wire.read();" lsb="Wire.read();" return (int) msb<<8 | lsb; } read the uncompensated temperature value unsigned int bmp085readut() { unsigned ut; write 0x2e into register 0xf4 this requests a reading wire.begintransmission(bmp085_address); wire.write(0xf4); wire.write(0x2e); wire.endtransmission(); wait at least 4.5ms delay(5); two bytes from registers 0xf6 and 0xf7 ut="bmp085ReadInt(0xF6);" pressure long bmp085readup() char msb, lsb, xlsb; up="0;" 0x34+(oss<<6) request w oversampling setting wire.write(0x34 + (oss<<6)); for conversion, delay time dependent on oss delay(2 (3<<oss)); (msb), (lsb), 0xf8 (xlsb) wire.write(0xf6); wire.requestfrom(bmp085_address, 3); data to become available while(wire.available() < 3) xlsb="Wire.read();" up="(((unsigned" long) msb << 16) ((unsigned lsb 8) (unsigned xlsb)>> (8-OSS);
return up;
}</2)></1)></oss)></i)></wire.h></sd.h></code>