如果你有更好的想法?
但却没有经验?
很简单
首先,从度娘那里要模块的教程
然后,和sd模块的源代码结合起来
我将以我这个航电来举例子
bmp180的教程
XXXXXXXXXXXXXXXXXXXXXXXXX/BLOG_ARTICLE_XXXXXXXXXXM
sd卡的教程
XXXXXXXXXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXml
将里面的读取某个传感器的值改为教程里面已经弄好的值
然后合二为一
以上步骤建议用版本较低的下载器,不容易error
bmp180的调试代码
/*
Get pressure and temperature from the BMP085.
XXXXXXXXXint it out at 9600 baud to serial monitor.
*/
#include <Wire.h>
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
const unsigned char OSS = 0; // Oversampling Setting
// 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()
{
XXXXXXXXXgin(9600);
XXXXXXXgin();
bmp085Calibration();
}
void loop()
{
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
XXXXXXXXXint("Temperature: ");
XXXXXXXXXint(temperature, DEC);
XXXXXXXXXintln(" *0.1 deg C");
XXXXXXXXXint("Pressure: ");
XXXXXXXXXint(pressure, DEC); 这些是重点
XXXXXXXXXintln(" Pa");
XXXXXXXXXintln();
delay(1000);
}
// 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;
XXXXXXXginTransmission(BMP085_ADDRESS);
XXXXXXXnd(address);
Wire.endTransmission();
XXXXXXXquestFrom(BMP085_ADDRESS, 1);
while(!Wire.available())
return XXXXXXXceive();
}
// 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;
XXXXXXXginTransmission(BMP085_ADDRESS);
XXXXXXXnd(address);
Wire.endTransmission();
XXXXXXXquestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2)
msb = XXXXXXXceive();
lsb = XXXXXXXceive();
return (int) msb<<8 | lsb;
}
// Read the uncompensated temperature value
unsigned int bmp085ReadUT()
{
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
XXXXXXXginTransmission(BMP085_ADDRESS);
XXXXXXXnd(0xF4);
XXXXXXXnd(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp085ReadInt(0xF6);
return ut;
}
// Read the uncompensated pressure value
unsigned long bmp085ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
XXXXXXXginTransmission(BMP085_ADDRESS);
XXXXXXXnd(0xF4);
XXXXXXXnd(0x34 + (OSS<<6));
Wire.endTransmission();
// Wait for conversion, delay time dependent on OSS
delay(2 + (3<<OSS));
// Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
XXXXXXXginTransmission(BMP085_ADDRESS);
XXXXXXXnd(0xF6);
Wire.endTransmission();
XXXXXXXquestFrom(BMP085_ADDRESS, 3);
// Wait for data to become available
while(Wire.available() < 3)
msb = XXXXXXXceive();
lsb = XXXXXXXceive();
xlsb = XXXXXXXceive();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}
/*
此例子展示如果记录三个模拟引脚数值到SD卡,使用SD库。
电路部分
* 模拟引脚使用模拟口0,1,2大家可以根据情况接入模拟传感器,
如果没有,也可以完成此实验,只不过数值是不停跳动的干扰源。
* SD卡与arduino控制板使用SPI总线连接
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
#include <SD.h>
// CS引脚为pin4,这里也沿用官方默认设置
const int chipSelect = 4; //设定CS接口
void setup()
{
XXXXXXXXXgin(9600); //设置串口通信波特率为9600
XXXXXXXXXint("Initializing SD card..."); //串口输出数据Initializing SD card...
pinMode(10, OUTPUT);
if (!XXXXXgin(chipSelect)) { //如果从CS口与SD卡通信失败,串口输出信息Card failed, or not present
XXXXXXXXXintln("Card failed, or not present"); 在这个的后面,可以加入点亮指示灯的操作
return;
}
XXXXXXXXXintln("card initialized."); //与SD卡通信成功,串口输出信息card initialized. 如果在上面有指示灯点亮这个操作,这里要关掉
}
void loop()
{
// 定义数组
String dataString = "";
// 读取三个传感器值,写入数组
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
绿色部分要删去
// 打开文件,注意在同一时间只能有一个文件被打开
// 如果你要打开另一个文件,就需要先关闭前一个
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// 打开datalog.txt文件,读写状态,位置在文件末尾。
if (dataFile) {
XXXXXXXXXXXintln(dataString); 把 dataString 改为上面的那个中间的气压 温度等
XXXXXXXXXXXose(); 这个是保存步骤,很重要,一旦删去,就无法保存
// 数组dataString输出到串口
XXXXXXXXXintln(dataString); 绿色这部分可以删去
}
// 如果无法打开文件,串口输出错误信息error opening datalog.txt
else {
XXXXXXXXXintln("error opening datalog.txt");
}
}
然后将两个程序合二为一,loop组并入loop组 setup并入setup,注意,千万遗漏