Changeset - b17234097037
[Not reviewed]
default
0 5 0
mkroening@CL-ENS241-07.cedarville.edu - 12 years ago 2013-01-31 17:25:13
mkroening@CL-ENS241-07.cedarville.edu
Hum and Pres read, added i2c_read16
5 files changed with 166 insertions and 62 deletions:
0 comments (0 inline, 0 general)
slave/slave/lib/i2c.c
Show inline comments
 
@@ -235,7 +235,27 @@ uint8_t i2c_read(unsigned char addr, uns
 
	i2c_rep_start(addr+I2C_READ);   // set device address and read mode
 
	data = i2c_readNak();               // read one byte
 
	i2c_stop();
 
	
 
    return data;
 
 
}/* i2c_read */
 
\ No newline at end of file
 
}/* i2c_read */
 
 
///////////added for humidity
 
uint16_t i2c_read16(unsigned char addr)
 
{
 
	uint16_t   data;
 
	uint8_t   dataL;
 
	
 
	i2c_start_wait(addr+I2C_WRITE);		// set device address and write mode
 
	
 
	i2c_rep_start(addr+I2C_READ);   // set device address and read mode
 
	data = i2c_readAck();               // read one byte
 
	dataL = i2c_readNak();
 
	i2c_stop();
 
	
 
	data = data << 8;
 
	data = data | dataL;
 
	
 
	return data;
 
 
}/* i2c_read16 */
 
\ No newline at end of file
slave/slave/lib/i2c.h
Show inline comments
 
@@ -56,7 +56,10 @@ unsigned char i2c_readX(unsigned char ac
 
	//Returns byte read from I2C device
 
 
void i2c_write(unsigned char addr, unsigned char reg, unsigned char data); //Does complete write. Can be used as stand alone function.
 
 
unsigned char i2c_read(unsigned char addr, unsigned char reg);
 
 
/////////////////added for humidity
 
unsigned char i2c_read16(unsigned char addr);
 
 
#endif /* I2C_H_ */
 
\ No newline at end of file
slave/slave/lib/sensors.c
Show inline comments
 
@@ -14,16 +14,90 @@
 
#include "sensors.h"
 
#include "spi.h"
 
#include "i2c.h"
 
 
int16_t	spiTemp;	// Thermocouple Temperature (from spi)
 
int8_t	boardTemp;	// Board Temperature (from i2c)
 
uint16_t pressure;	// Pressure (from i2c)
 
//int humid;		// Humidity (from i2c)
 
//Should these two be int16_t?  CHANGED TO INT32 BASED ON DATASHEET
 
int32_t ut;			// Temperature from BMP085 (from i2c)
 
int32_t up;			// Pressure from BMP085 (from i2c)
 
uint16_t humid;		// Humidity (from i2c)
 
uint8_t light;		// Lux reading (from i2c)
 
 
int16_t ac1;		// The following 11 variables are the calibration values for the BMP085
 
int16_t ac2;
 
int16_t ac3;
 
uint16_t ac4;
 
uint16_t ac5;
 
uint16_t ac6;
 
int16_t b1;
 
int16_t b2;
 
int16_t mb;
 
int16_t mc;
 
int16_t md;
 
 
int32_t x1;			// The following variables are needed to calculate the true pressure
 
int32_t x2;
 
int32_t x3;
 
int32_t b3;
 
uint32_t b4;
 
int32_t b5;
 
int32_t b6;
 
uint32_t b7;
 
int32_t trueTemp;
 
int32_t truePres;
 
 
 
 
void sensors_setupPressure()
 
{
 
	//This function reads in the calibration values from the BMP085.  This is done only once.
 
	ac1 = i2c_read(PRESSURE_ADDR, 0xAA);
 
	ac1 = ac1 << 8;
 
	ac1 = ac1 | i2c_read(PRESSURE_ADDR, 0xAB);
 
	
 
	ac2 = i2c_read(PRESSURE_ADDR, 0xAC);
 
	ac2 = ac2 << 8;
 
	ac2 = ac2 | i2c_read(PRESSURE_ADDR, 0xAD);
 
	
 
	ac3 = i2c_read(PRESSURE_ADDR, 0xAE);
 
	ac3 = ac3 << 8;
 
	ac3 = ac3 | i2c_read(PRESSURE_ADDR, 0xAF);
 
	
 
	ac4 = i2c_read(PRESSURE_ADDR, 0xB0);
 
	ac4 = ac4 << 8;
 
	ac4 = ac4 | i2c_read(PRESSURE_ADDR, 0xB1);
 
	
 
	ac5 = i2c_read(PRESSURE_ADDR, 0xB2);
 
	ac5 = ac5 << 8;
 
	ac5 = ac5 | i2c_read(PRESSURE_ADDR, 0xB3);
 
	
 
	ac6 = i2c_read(PRESSURE_ADDR, 0xB4);
 
	ac6 = ac6 << 8;
 
	ac6 = ac6 | i2c_read(PRESSURE_ADDR, 0xB5);
 
	
 
	b1 = i2c_read(PRESSURE_ADDR, 0xB6);
 
	b1 = b1 << 8;
 
	b1 = b1 | i2c_read(PRESSURE_ADDR, 0xB7);
 
	
 
	b2 = i2c_read(PRESSURE_ADDR, 0xB8);
 
	b2 = b2 << 8;
 
	b2 = b2 | i2c_read(PRESSURE_ADDR, 0xB9);
 
	
 
	mb = i2c_read(PRESSURE_ADDR, 0xBA);
 
	mb = mb << 8;
 
	mb = mb | i2c_read(PRESSURE_ADDR, 0xBB);
 
	
 
	mc = i2c_read(PRESSURE_ADDR, 0xBC);
 
	mc = mc << 8;
 
	mc = mc | i2c_read(PRESSURE_ADDR, 0xBD);
 
	
 
	md = i2c_read(PRESSURE_ADDR, 0xBE);
 
	md = md << 8;
 
	md = md | i2c_read(PRESSURE_ADDR, 0xBF);
 
}
 
 
void sensors_readSpiTemp()
 
{
 
	// Select TEMP wait 100 microseconds then read four bytes
 
	SELECT_TEMP;
 
	_delay_us(100);
 
@@ -53,24 +127,68 @@ void sensors_readBoardTemp()
 
	boardTemp = i2c_read(BOARDTEMP_ADDR, 0x00);		// Read only the first byte of data (we don't need the resolution here)
 
	boardTemp = ((boardTemp*18)/10) + (32);			// Converting Celsius to Fahrenheit
 
	boardTemp = boardTemp - 3;						// Linear offset
 
}
 

	
 
void sensors_readPressure()
 
{//needs editing
 
	//write 0x34 + (oss<<6) into 0xF4 (write is 0xEE), wait
 
	pressure = i2c_read(PRESSURE_ADDR, 0xF6);
 
	pressure = pressure << 8;
 
	pressure = pressure | i2c_read(PRESSURE_ADDR, 0xF7);
 
	//pressure = (MSB<<16 + LSB<<8 + XLSB(NOT USED)) >> (8-oss)
 
	//calculate true pressure with calibration data from modules.c, maybe write own function used here
 
{
 
	i2c_write(0xEE, 0xF4, 0x2E);				//write 0x2E (temp) into 0xF4 (control register), (write is 0xEE)
 
	_delay_us(4500);							//wait 4.5 ms
 
	ut = i2c_read(PRESSURE_ADDR, 0xF6);
 
	ut = ut << 8;
 
	ut = ut | i2c_read(PRESSURE_ADDR, 0xF7);	//ut = MSB<<8 + LSB
 
	
 
	i2c_write(0xEE, 0xF4, 0x34);				//write 0x34 (pressure) into 0xF4 (control register), (write is 0xEE)
 
	_delay_us(4500);							//wait 4.5 ms
 
	up = i2c_read(PRESSURE_ADDR, 0xF6);
 
	up = up << 8;
 
	up = up | i2c_read(PRESSURE_ADDR, 0xF7);	//up = (MSB<<16 + LSB<<8 + XLSB(NOT USED)) >> (8-oss)
 
	
 
	//calculate true temperature
 
	x1 = ((ut - ac6) * ac5) >> 15;
 
	x2 = (mc << 11) / (x1 + md);
 
	b5 = x1 + x2;
 
	trueTemp = (b5 + 8) >> 4;
 
	
 
	//calculate b3
 
	b6 = b5 - 4000;
 
	x1 = (b2 * (b6 * b6) >> 12) >> 11;
 
	x2 = (ac2 * b6) >> 11;
 
	x3 = x1 + x2;
 
	b3 = ((ac1 * 4 + x3) + 2) / 4;
 
	
 
	//calculate b4
 
	x1 = (ac3 * b6) >> 16;
 
	x2 = (b1 * ((b6 * b6) >> 12)) >> 16;
 
	x3 = ((x1 + x2) + 2) >> 2;
 
	b4 = (ac4 * (x3 + 32768)) >> 15;
 
	
 
	b7 = (up - b3) * 50000;
 
	
 
	if (b7 < 0x80000000)
 
	{
 
		truePres = (b7 << 1) / b4;
 
	}
 
	
 
	else
 
	{
 
		truePres = (b7 / b4) << 1;
 
	}
 
	
 
	x1 = (truePres >> 8) * (truePres >> 8);
 
	x1 = (x1 * 3038) >> 16;
 
	x2 = (-7357 * truePres) >> 16;
 
	truePres += (x1 + x2 + 3791) >> 4;				//This is the final value for our pressure
 
}
 

	
 
void sensors_readHumid()
 
{
 
	//humid = i2c_read(HUMID_ADDR, 0xXX);
 
	humid = i2c_read16(HUMID_ADDR);
 
	
 
	//calculations to relative humidity: humid = (humid/((2^14) - 1))*100%       >> is divide by power, << is multiply by power, 2^14-1 = 16383
 
	humid = (humid / 16383) * 100;
 
}
 

	
 
void sensors_readLight()
 
{
 
	light = i2c_read(LIGHT_ADDR, 0x03);
 
	// light = light << 4;
 
@@ -92,20 +210,20 @@ int16_t sensors_getSpiTemp(void)	// Gets
 
 
int8_t sensors_getBoardTemp(void)	// Gets board temperature from variable
 
{
 
	return boardTemp;
 
}
 
 
uint16_t sensors_getPressure(void)
 
int32_t sensors_getPressure(void)	// Gets pressure from variable
 
{
 
	return pressure;
 
	return truePres;
 
}
 
 
//int sensors_getHumid(void)
 
//{
 
	//return humid;
 
//}
 
int sensors_getHumid(void)			// Gets relative humidity from variable
 
{
 
	return humid;
 
}
 
 
uint8_t sensors_getLight(void)
 
uint8_t sensors_getLight(void)		// Gets light from variable
 
{
 
	return light;
 
}
 
\ No newline at end of file
slave/slave/lib/sensors.h
Show inline comments
 
@@ -7,19 +7,20 @@
 
 
 
#ifndef SENSORS_H_
 
#define SENSORS_H_
 
 
 
void sensors_setupPressure(void);	// Reads pressure calibration values
 
void sensors_readSpiTemp(void);		// Reads spi temperature
 
void sensors_readBoardTemp(void);	// Reads board temperature
 
void sensors_readPressure(void);	// Reads pressure
 
void sensors_readHumid(void);		// Reads humidity
 
void sensors_readLight(void);		// Reads lux
 
 
int16_t sensors_getSpiTemp(void);	// Gets spi temperature from variable
 
int8_t sensors_getBoardTemp(void);	// Gets board temperature from variable
 
uint16_t sensors_getPressure(void);	// Gets pressure from variable
 
//int sensors_getHumid(void);	// Gets humidity from variable
 
int32_t sensors_getPressure(void);	// Gets pressure from variable
 
uint16_t sensors_getHumid(void);	// Gets humidity from variable
 
uint8_t sensors_getLight(void);		// Gets lux from variable
 
 
#endif /* SENSORS_H_ */
 
\ No newline at end of file
slave/slave/modules.c
Show inline comments
 
@@ -76,13 +76,13 @@
 
 }
 
  
 
 void modules_sensors_setup()
 
 {
 
	 DESELECT_TEMP;
 
	 setup_spi();
 
	//setup_pressure();
 
	sensors_setupPressure();
 
 }
 
  
 
 void modules_geiger_setup()
 
 {
 
	// Pin setup
 
	DDRA &= ~(1 << DDA0);	// PA0 is an input
 
@@ -120,15 +120,15 @@
 
  
 
 void modules_sensors()
 
 {
 
	// Gathers data and performs functions for sensor daughter board
 
	sensors_readBoardTemp();		//Data Read
 
	sensors_readSpiTemp();			//Data Read
 
	//sensors_readPressure();			//Data Read
 
	//sensors_readHumid();			//Data Read
 
	//sensors_readLight();			//Data Read
 
	sensors_readPressure();			//Data Read
 
	sensors_readHumid();			//Data Read
 
	sensors_readLight();			//Data Read
 
	 
 
 }
 
  
 
 void modules_geiger()
 
 {
 
	// No data gatering function needed for geiger daughter board
 
@@ -138,44 +138,6 @@
 
  
 
 void modules_cameras()
 
 {
 
	// Gathers data and performs functions for cameras daughter board
 
  
 
 } 
 
  
 
  ///////////do I need to change .h file?
 
  void setup_pressure()
 
  {
 
	  int16_t ac1 = i2c_read(PRESSURE_ADDR, 0xAA);
 
	  ac1 = ac1 << 8;
 
	  ac1 = ac1 | i2c_read(PRESSURE_ADDR, 0xAB);
 
	  int16_t ac2 = i2c_read(PRESSURE_ADDR, 0xAC);
 
	  ac2 = ac2 << 8;
 
	  ac2 = ac2 | i2c_read(PRESSURE_ADDR, 0xAD);	  
 
	  int16_t ac3 = i2c_read(PRESSURE_ADDR, 0xAE);
 
	  ac3 = ac3 << 8;
 
	  ac3 = ac3 | i2c_read(PRESSURE_ADDR, 0xAF);
 
	  uint16_t ac4 = i2c_read(PRESSURE_ADDR, 0xB0);
 
	  ac4 = ac4 << 8;
 
	  ac4 = ac4 | i2c_read(PRESSURE_ADDR, 0xB1);
 
	  uint16_t ac5 = i2c_read(PRESSURE_ADDR, 0xB2);
 
	  ac5 = ac5 << 8;
 
	  ac5 = ac5 | i2c_read(PRESSURE_ADDR, 0xB3);
 
	  uint16_t ac6 = i2c_read(PRESSURE_ADDR, 0xB4);
 
	  ac6 = ac6 << 8;
 
	  ac6 = ac6 | i2c_read(PRESSURE_ADDR, 0xB5);
 
	  int16_t b1 = i2c_read(PRESSURE_ADDR, 0xB6);
 
	  b1 = b1 << 8;
 
	  b1 = b1 | i2c_read(PRESSURE_ADDR, 0xB7);
 
	  int16_t b2 = i2c_read(PRESSURE_ADDR, 0xB8);
 
	  b2 = b2 << 8;
 
	  b2 = b2 | i2c_read(PRESSURE_ADDR, 0xB9);
 
	  int16_t mb = i2c_read(PRESSURE_ADDR, 0xBA);
 
	  mb = mb << 8;
 
	  mb = mb | i2c_read(PRESSURE_ADDR, 0xBB);
 
	  int16_t mc = i2c_read(PRESSURE_ADDR, 0xBC);
 
	  mc = mc << 8;
 
	  mc = mc | i2c_read(PRESSURE_ADDR, 0xBD);
 
	  int16_t md = i2c_read(PRESSURE_ADDR, 0xBE);
 
	  md = md << 8;
 
	  md = md | i2c_read(PRESSURE_ADDR, 0xBF);
 
  }
 
\ No newline at end of file
0 comments (0 inline, 0 general)