Changeset - a2396e650c96
[Not reviewed]
default
0 4 0
mkroening@CL-ENS241-07.cedarville.edu - 12 years ago 2013-02-21 16:52:38
mkroening@CL-ENS241-07.cedarville.edu
sensors.c streamlining
4 files changed with 12 insertions and 22 deletions:
0 comments (0 inline, 0 general)
slave/slave/lib/inputOutput.c
Show inline comments
 
@@ -25,17 +25,17 @@ int8_t	moduleID;	// Slave Module ID from
 
	
 
	
 
	DDRA &= ~(1 << DDA7);		// Set PA7 to input for battery voltage divider
 
 
	ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);	// Set prescaler for ADC, 128 gives ADC freq of 125 KHz
 
	ADMUX |= (1 << REFS0);									// Set ADC reference voltage to AVCC
 
	ADMUX |= (1 << ADLAR);									// Sets 10 bit ADC to 8 bit
 
	//ADMUX |= (1 << ADLAR);									// Sets 10 bit ADC to 8 bit
 
	ADMUX |= (1 << MUX2) | (1 << MUX1) | (1 << MUX0);		// Select ADC7 as the conversion channel
 
	ADCSRA |= (1 << ADATE);									// Enables auto trigger, determined in ADCSRB bits ADTS
 
	//ADCSRA |= (1 << ADIF);									// 
 
//ADCSRA |= (1 << ADIE);									// ADC interrupt enable set
 
	//ADCSRA |= (1 << ADIE);									// ADC interrupt enable set
 
	ADCSRB &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0));	// Set ADC auto trigger source to free running mode
 
	ADCSRA |= (1 << ADEN);									// Enable ADC
 
	ADCSRA |= (1 << ADSC);									// Start ADC measurements.  ADC should now continuously run conversions, which are stored in ADCH 0x79
 
	
 
 
 }
slave/slave/lib/sensors.c
Show inline comments
 
@@ -15,22 +15,22 @@
 
#include "sensors.h"
 
#include "spi.h"
 
#include "i2c.h"
 
 
int16_t	spiTemp;	// Thermocouple Temperature (from spi)
 
int8_t	boardTemp;	// Board Temperature (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 lightH;		// Higher byte from light sensor (from i2c)
 
uint8_t lightL;		// Lower byte from light sensor
 
uint8_t exponent;	// Exponent for Lux
 
uint8_t mantissa;	// Mantissa for Lux
 
uint32_t lux;		// Calculated Lux value
 
int8_t batt;		// Read battery voltage from ADC
 
uint8_t battL;		// Low byte of ADC
 
uint16_t batt;		// Read battery voltage from ADC
 
 
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;
 
@@ -183,13 +183,12 @@ void sensors_readPressure()
 
	
 
	x1 = (pressure >> 8) * (pressure >> 8);
 
	x1 = (x1 * 3038) >> 16;
 
	x2 = (-7357 * pressure) >> 16;
 
	pressure += (x1 + x2 + 3791) >> 4;				//This is the final value for our pressure
 
	
 
	//altitude = 44330 * (1 - pow((pressure / 101325), (1 / 5.255)));
 
	altitude = (float)44330 * (1 - pow(((float) pressure/101325), 0.190295));
 
}
 
 
void sensors_readHumid()
 
{
 
	humid = i2c_read16(HUMID_ADDR);
 
@@ -197,38 +196,30 @@ void sensors_readHumid()
 
	//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_readLux()
 
{
 
	// FOR FIRST BYTE:
 
	lightH = i2c_read(LIGHT_ADDR, 0x03);
 
	lightL = i2c_read(LIGHT_ADDR, 0x04);
 
	
 
	exponent = lightH;
 
	exponent = exponent >> 4;
 
	
 
	lightH = lightH << 4;
 
	mantissa = lightH | lightL;
 
	//mantissa = mantissa << 4;
 
	//mantissa = mantissa >> 4;
 
	
 
	//lux = (pow(2, exponent) * (float)(mantissa * 0.045));
 
 
	lux = (float)(pow(2,exponent) * mantissa) * 0.045;
 
	
 
	// FOR BOTH BYTES:
 
	// light = light << 4;
 
	// light = light | (0x0F & i2c_read(LIGHT_ADDR, 0x04));  //  This can be used to read in the 4 LSBs from the second register
 
	// exponent = 8xE3 + 4xE2 + 2xE1 + E0
 
	// mantissa = 128xM7 + 64xM6 + 32xM5 + 16xM4 + 8xM3 + 4xM2 + 2xM1 + M0
 
	// light = 2^(exponent)*mantissa*0.045
 
}
 
 
void sensors_readBatt()
 
{
 
	batt = ADCH;					// Read battery level from ADC
 
	battL = ADCL;					// Read low battery byte from ADC (all 8 bits)
 
	batt = ADCH;					// Read high battery byte from ADC (only two LSBs)
 
	batt = batt << 8;
 
	batt |= battL;
 
}
 
 
int16_t sensors_getSpiTemp(void)	// Gets spi temperature from variable
 
{
 
	return spiTemp;
 
}
 
@@ -250,14 +241,13 @@ uint16_t sensors_getHumid(void)			// Get
 
 
uint32_t sensors_getLux(void)		// Gets light from variable
 
{
 
	return lux;
 
}
 
 
//possibly uint8_t
 
int8_t sensors_getBatt(void)		// Gets battery voltage from variable
 
uint16_t sensors_getBatt(void)		// Gets battery voltage from variable
 
{
 
	return batt;
 
}
 
 
uint32_t sensors_getAltitude(void)
 
{
slave/slave/lib/sensors.h
Show inline comments
 
@@ -20,10 +20,10 @@ void sensors_readBatt(void);
 
 
int16_t sensors_getSpiTemp(void);	// Gets spi temperature from variable
 
int8_t sensors_getBoardTemp(void);	// Gets board temperature from variable
 
int32_t sensors_getPressure(void);	// Gets pressure from variable
 
uint16_t sensors_getHumid(void);	// Gets humidity from variable
 
uint32_t sensors_getLux(void);		// Gets lux from variable
 
int8_t sensors_getBatt(void);		// Gets battery voltage from variable
 
uint16_t sensors_getBatt(void);		// Gets battery voltage from variable
 
uint32_t sensors_getAltitude(void);	// Gets altitude from variable
 
 
#endif /* SENSORS_H_ */
 
\ No newline at end of file
slave/slave/slave.c
Show inline comments
 
@@ -79,13 +79,13 @@ int main(void)
 
			led_on(0);
 
			sensors_readBoardTemp();	// Read board temperature sensor (Common on all slaves) (Data Read)
 
			modules_run(io_getModuleId());		// Runs specific module functions (like data reading)
 
			
 
			io_regulateTemp();			// Gets board temperature and enables heater if below threshold
 

	
 
			snprintf(buff,128,"|ModuleID: %u |BoardTemp: %i |Millis: %lu |Lux: %lu |Pressure: %lu |Altitude: %lu |Battery: %i \r\n ",io_getModuleId(),sensors_getBoardTemp(),time_millis(),sensors_getLux(),sensors_getPressure(),sensors_getAltitude(),sensors_getBatt()); //DEBUG
 
			snprintf(buff,128,"|ModuleID: %u |BoardTemp: %i |Millis: %lu |Lux: %lu |Pressure: %lu |Altitude: %lu |Battery: %u \r\n ",io_getModuleId(),sensors_getBoardTemp(),time_millis(),sensors_getLux(),sensors_getPressure(),sensors_getAltitude(),sensors_getBatt()); //DEBUG
 
			serial0_sendString(buff); //DEBUG
 

	
 
			_delay_ms(2);		// Delay to prevent the sensor loop from running again before time_millis changes
 
			led_off(0);
 
			led_off(2);
 
		}
0 comments (0 inline, 0 general)