Lab 21/ HW 21
Lab 21 (BPM180):
// This is the top of the program
#include <Wire.h>
#include "BMP180Lib.c"
#include <SD.h>
#define BMP180_ADDRESS 0x77 // I2C address of BMP180
// Read 1 byte from the BMP180 at 'address'
char bmp180Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP180_ADDRESS, 1);
while(!Wire.available())
;
return Wire.read();
}
// Read 2 bytes from the BMP180
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp180ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP180_ADDRESS, 2);
while(Wire.available()<2)
;
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
short temperature;
long pressure;
// Use these for altitude conversions
const float p0 = 101325; // Pressure at sea level (Pa)
float altitude;
const int CS_PIN = 10;
void setup()
{
Serial.begin(9600);
Wire.begin();
bmp180Calibration();
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
}
// Stores all of the bmp180'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 bmp180Calibration()
{
ac1 = bmp180ReadInt(0xAA);
ac2 = bmp180ReadInt(0xAC);
ac3 = bmp180ReadInt(0xAE);
ac4 = bmp180ReadInt(0xB0);
ac5 = bmp180ReadInt(0xB2);
ac6 = bmp180ReadInt(0xB4);
b1 = bmp180ReadInt(0xB6);
b2 = bmp180ReadInt(0xB8);
mb = bmp180ReadInt(0xBA);
mc = bmp180ReadInt(0xBC);
md = bmp180ReadInt(0xBE);
}
// Read the uncompensated temperature value
unsigned int bmp180ReadUT()
{
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp180ReadInt(0xF6);
return ut;
}
// Read the uncompensated pressure value
unsigned long bmp180ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(0xF4);
Wire.write(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)
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP180_ADDRESS, 3);
// Wait for data to become available
while(Wire.available() < 3)
;
msb = Wire.read();
lsb = Wire.read();
xlsb = Wire.read();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}
unsigned long previousmillis = 0;
void loop()
{
temperature = bmp180GetTemperature(bmp180ReadUT());
pressure = bmp180GetPressure(bmp180ReadUP());
altitude = (float)44330 * (1 - pow(((float) pressure/p0), 0.190295));
unsigned long timestamp = millis();
if(timestamp - previousmillis >= 2)
{
previousmillis = timestamp;
File dataFile = SD.open("PTA.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timestamp);
dataFile.print(",");
dataFile.print(temperature*0.1);
dataFile.print(",");
dataFile.print(pressure);
dataFile.print(",");
dataFile.println(altitude, 2);
dataFile.close();
Serial.print(timestamp);
Serial.print(",");
Serial.print(temperature*0.1);
Serial.print(",");
Serial.print(pressure);
Serial.print(",");
Serial.println(altitude, 2);
}
else
{
Serial.println("Couldn't open log file");
}
}
}
// This is the top of the program
#include <Wire.h>
#include "BMP180Lib.c"
#include <SD.h>
#define BMP180_ADDRESS 0x77 // I2C address of BMP180
// Read 1 byte from the BMP180 at 'address'
char bmp180Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP180_ADDRESS, 1);
while(!Wire.available())
;
return Wire.read();
}
// Read 2 bytes from the BMP180
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp180ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP180_ADDRESS, 2);
while(Wire.available()<2)
;
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
short temperature;
long pressure;
// Use these for altitude conversions
const float p0 = 101325; // Pressure at sea level (Pa)
float altitude;
const int CS_PIN = 10;
void setup()
{
Serial.begin(9600);
Wire.begin();
bmp180Calibration();
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
}
// Stores all of the bmp180'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 bmp180Calibration()
{
ac1 = bmp180ReadInt(0xAA);
ac2 = bmp180ReadInt(0xAC);
ac3 = bmp180ReadInt(0xAE);
ac4 = bmp180ReadInt(0xB0);
ac5 = bmp180ReadInt(0xB2);
ac6 = bmp180ReadInt(0xB4);
b1 = bmp180ReadInt(0xB6);
b2 = bmp180ReadInt(0xB8);
mb = bmp180ReadInt(0xBA);
mc = bmp180ReadInt(0xBC);
md = bmp180ReadInt(0xBE);
}
// Read the uncompensated temperature value
unsigned int bmp180ReadUT()
{
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp180ReadInt(0xF6);
return ut;
}
// Read the uncompensated pressure value
unsigned long bmp180ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(0xF4);
Wire.write(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)
Wire.beginTransmission(BMP180_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP180_ADDRESS, 3);
// Wait for data to become available
while(Wire.available() < 3)
;
msb = Wire.read();
lsb = Wire.read();
xlsb = Wire.read();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}
unsigned long previousmillis = 0;
void loop()
{
temperature = bmp180GetTemperature(bmp180ReadUT());
pressure = bmp180GetPressure(bmp180ReadUP());
altitude = (float)44330 * (1 - pow(((float) pressure/p0), 0.190295));
unsigned long timestamp = millis();
if(timestamp - previousmillis >= 2)
{
previousmillis = timestamp;
File dataFile = SD.open("PTA.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timestamp);
dataFile.print(",");
dataFile.print(temperature*0.1);
dataFile.print(",");
dataFile.print(pressure);
dataFile.print(",");
dataFile.println(altitude, 2);
dataFile.close();
Serial.print(timestamp);
Serial.print(",");
Serial.print(temperature*0.1);
Serial.print(",");
Serial.print(pressure);
Serial.print(",");
Serial.println(altitude, 2);
}
else
{
Serial.println("Couldn't open log file");
}
}
}
Comments
Post a Comment