Changeset - 82d01332900d
[Not reviewed]
default
0 5 0
mkroening@CL-ENS241-07.cedarville.edu - 12 years ago 2013-04-03 21:31:54
mkroening@CL-ENS241-07.cedarville.edu
Sensor fixes
5 files changed with 69 insertions and 94 deletions:
0 comments (0 inline, 0 general)
slave/slave/config.h
Show inline comments
 
@@ -21,26 +21,26 @@
 
// Maximum payload size of command
 
#define MAX_PAYLOAD_LEN 16
 
 
// Number of datatypes to transmit per module type
 
#define DATATYPES_GENERIC 3
 
#define DATATYPES_SENSOR 8
 
#define DATATYPES_GEIGER 4
 
#define DATATYPES_CAMERA 3
 
 
//Sensors and IO
 
#define SENSOR_LOOP 200				// Frequency of sensor reads (in ms) (should be 200)
 
#define HEATER_THRESHOLD 0			// Temperature threshold in Fahrenheit where heater is activated
 
#define CAMERA_FREQ 5000		// Camera pulse frequency (Should be 30000 for 30 Secs)
 
#define CAMERA_PULSE 2500			// Camera pulse duration
 
#define CAMERA_FREQ 30000		// Camera pulse frequency (Should be 30000 for 30 Secs)
 
#define CAMERA_PULSE 500 			// Camera pulse duration
 
 
 //I2C Addresses
 
 #define EEPROM_ADDR 0xA0		// Read 0xA1 - Write 0xA0
 
 #define BOARDTEMP_ADDR 0x90	// Read 0x91 - Write 0x90
 
 #define PRESSURE_ADDR 0xEE		// Read 0xEF - Write 0xEE
 
 #define HUMID_ADDR 0x27		// Read 0x28 - Write 0x27
 
 #define LIGHT_ADDR 0x94		// Read 0x95 - Write 0x94
 
 #define ACCEL_ADDR	0x38		// Read 0x39 - Write 0x38
 
 #define RTC_ADDR 0xB2			//DEBUG [Used for testing]      // Read 0xA3 - Write 0xA2
 
 
 
slave/slave/lib/i2c.c
Show inline comments
 
@@ -37,43 +37,43 @@ void i2c_init(void)
 
  Issues a start condition and sends address and transfer direction.
 
  return 0 = device accessible, 1= failed to access device
 
*************************************************************************/
 
unsigned char i2c_start(unsigned char address)
 
{
 
    uint8_t   twst;
 
 
	// send START condition
 
	TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
 
 
	// wait until transmission completed
 
//	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	while(!(TWCR & (1<<TWINT)));
 
//	{
 
//		if ((time_millis() - startTime) > 10)
 
//		{
 
//			break;	// Timeout Reached!
 
//		}
 
//	}
 
 
	// check value of TWI Status Register. Mask prescaler bits.
 
	twst = TW_STATUS & 0xF8;
 
	if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
 
 
	// send device address
 
	TWDR = address;
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
 
	// wail until transmission completed and ACK/NACK has been received
 
//	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	while(!(TWCR & (1<<TWINT)));
 
//	{
 
//		if ((time_millis() - startTime) > 10)
 
//		{
 
//			break;	// Timeout Reached!
 
//		}
 
//	}
 
 
	// check value of TWI Status Register. Mask prescaler bits.
 
	twst = TW_STATUS & 0xF8;
 
	if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
 
 
	return 0;
 
@@ -90,67 +90,67 @@ unsigned char i2c_start(unsigned char ad
 
*************************************************************************/
 
void i2c_start_wait(unsigned char address)
 
{
 
    uint8_t   twst;
 
 
 
    while ( 1 )
 
    {
 
	    // send START condition
 
	    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
 
    
 
    	// wait until transmission completed
 
		startTime = time_millis();
 
    	while(!(TWCR & (1<<TWINT)))
 
		{
 
			if ((time_millis() - startTime) > 10)
 
			{
 
				break;	// Timeout Reached!
 
			}
 
		}
 
		//startTime = time_millis();
 
    	while(!(TWCR & (1<<TWINT)));
 
		//{
 
			//if ((time_millis() - startTime) > 10)
 
			//{
 
				//break;	// Timeout Reached!
 
			//}
 
		//}
 
    
 
    	// check value of TWI Status Register. Mask prescaler bits.
 
    	twst = TW_STATUS & 0xF8;
 
    	if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
 
    
 
    	// send device address
 
    	TWDR = address;
 
    	TWCR = (1<<TWINT) | (1<<TWEN);
 
    
 
    	// wail until transmission completed
 
		startTime = time_millis();
 
    	while(!(TWCR & (1<<TWINT)))
 
		{
 
			if ((time_millis() - startTime) > 10)
 
			{
 
				break;	// Timeout Reached!
 
			}
 
		}
 
		//startTime = time_millis();
 
    	while(!(TWCR & (1<<TWINT)));
 
		//{
 
			//if ((time_millis() - startTime) > 10)
 
			//{
 
				//break;	// Timeout Reached!
 
			//}
 
		//}
 
    
 
    	// check value of TWI Status Register. Mask prescaler bits.
 
    	twst = TW_STATUS & 0xF8;
 
    	if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) ) 
 
    	{    	    
 
    	    /* device busy, send stop condition to terminate write operation */
 
	        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
 
	        
 
	        // wait until stop condition is executed and bus released
 
			startTime = time_millis();
 
	        while(TWCR & (1<<TWSTO))
 
			{
 
				if ((time_millis() - startTime) > 10)
 
				{
 
					break;	// Timeout Reached!
 
				}
 
			}
 
			//startTime = time_millis();
 
	        while(TWCR & (1<<TWSTO));
 
			//{
 
				//if ((time_millis() - startTime) > 10)
 
				//{
 
					//break;	// Timeout Reached!
 
				//}
 
			//}
 
	        
 
    	    continue;
 
    	}
 
    	//if( twst != TW_MT_SLA_ACK) return 1;
 
    	break;
 
     }
 
 
}/* i2c_start_wait */
 
 
 
 
/*************************************************************************
 
@@ -170,25 +170,25 @@ unsigned char i2c_rep_start(unsigned cha
 
 
 
/*************************************************************************
 
 Terminates the data transfer and releases the I2C bus
 
*************************************************************************/
 
void i2c_stop(void)
 
{
 
    /* send stop condition */
 
	TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
 
	
 
	// wait until stop condition is executed and bus released
 
//	startTime = time_millis();
 
	while(TWCR & (1<<TWSTO))
 
	while(TWCR & (1<<TWSTO));
 
	{
 
//		if ((time_millis() - startTime) > 10)
 
//		{
 
//			break;	// Timeout Reached!
 
//		}
 
	}
 
 
}/* i2c_stop */
 
 
 
 
/*************************************************************************
 
@@ -198,84 +198,84 @@ void i2c_stop(void)
 
  Return:   0 write successful 
 
            1 write failed
 
*************************************************************************/
 
unsigned char i2c_writeX( unsigned char data )
 
{	
 
    uint8_t   twst;
 
    
 
	// send data to the previously addressed device
 
	TWDR = data;
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
 
	// wait until transmission completed
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
	//startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)));
 
	//{
 
		//if ((time_millis() - startTime) > 10)
 
		//{
 
			//break;	// Timeout Reached!
 
		//}
 
	//}
 
	
 
	
 
	// check value of TWI Status Register. Mask prescaler bits
 
	twst = TW_STATUS & 0xF8;
 
	if( twst != TW_MT_DATA_ACK) return 1;
 
	return 0;
 
 
}/* i2c_write */
 
 
 
 
/*************************************************************************
 
 Read one byte from the I2C device, request more data from device 
 
 
 
 Return:  byte read from I2C device
 
*************************************************************************/
 
unsigned char i2c_readAck(void)
 
{
 
	TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
 
	
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}  
 
	//startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)));
 
	//{
 
		//if ((time_millis() - startTime) > 10)
 
		//{
 
			//break;	// Timeout Reached!
 
		//}
 
	//}  
 
 
    return TWDR;
 
 
}/* i2c_readAck */
 
 
 
 
/*************************************************************************
 
 Read one byte from the I2C device, read is followed by a stop condition 
 
 
 
 Return:  byte read from I2C device
 
*************************************************************************/
 
unsigned char i2c_readNak(void)
 
{
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
	
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
	//startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)));
 
	//{
 
		//if ((time_millis() - startTime) > 10)
 
		//{
 
			//break;	// Timeout Reached!
 
		//}
 
	//}
 
	
 
    return TWDR;
 
 
}/* i2c_readNak */
 
 
 
 
/*************************************************************************
 
 Write one byte to the I2C device, read is followed by a stop condition 
 
 
 
 Return:  void
 
*************************************************************************/
slave/slave/lib/sensors.c
Show inline comments
 
@@ -177,26 +177,27 @@ void sensors_readPressure()
 
		pressure = (b7 << 1) / b4;
 
	}
 
	
 
	else
 
	{
 
		pressure = (b7 / b4) << 1;
 
	}
 
	
 
	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
 
	pressure = pressure - 21000;	// Linear Offset (Debug?)
 
	
 
	altitude = (float)44330 * (1 - pow(((float) pressure/101325), 0.190295));
 
	altitude = (float)44330 * (1 - pow(((float) pressure/101325), 0.190295));	// 101325
 
}
 
 
void sensors_readHumid()
 
{
 
	//i2c_write(HUMID_ADDR, 0x00, 0x00);		//Measurement Request
 
	//humid = i2c_read16(HUMID_ADDR);
 
 
	humid = i2c_humidRead();
 
 
	//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;
 
}
slave/slave/modules.c
Show inline comments
 
@@ -9,25 +9,24 @@
 
 #include <avr/io.h>
 
 #include <avr/interrupt.h>
 
 #include "config.h"
 
 #include <util/delay.h>
 
 #include "modules.h"
 
 #include "lib/spi.h"
 
 #include "lib/i2c.h"
 
 #include "lib/sensors.h"
 
 #include "lib/loopTimer.h"
 
 #include "lib/led.h"
 
 
 
 uint32_t lastPicture;
 
 uint8_t pulseOn;
 
 uint32_t lastRefresh;	// Time in ms when last geiger refresh occurred 
 
 
 
 void modules_setup(uint8_t id)
 
 {
 
	switch(id)
 
	{
 
		case 0:
 
			modules_generic_setup();
 
			break;
 
			
 
		case 1:
 
			modules_sensors_setup();
 
@@ -104,26 +103,25 @@
 
	ASSR &= ~(1 << EXCLK);	// Disable external clock input (enabling crystal use)
 
	ASSR |= (1 << AS2);		// Enable timer2 async mode with an external crystal	
 
	_delay_ms(100);			// Let external 32KHz crystal stabilize
 
	TCCR2B = 0x05;			// Set the prescaler to 128: 32.768kHz / 128 = 1Hz overflow
 
	TIFR2 = 0x01;			// Reset timer2 overflow interrupt flag
 
	TIMSK2 = 0x01;			// Enable interrupt on overflow
 
 }
 
  
 
  
 
  
 
 void modules_cameras_setup()
 
 {
 
	 lastPicture = time_millis();
 
	 pulseOn = time_millis();	 
 
	 lastPicture = time_millis();	 
 
	 
 
	 DDRA |= (1 << DDA0);	// Set PA0 to Output for Camera
 
	 DDRA |= (1 << DDA1);	// Set PA1 to Output for Camera
 
	 DDRA |= (1 << DDA2);	// Set PA2 to Output for Camera
 
	 DDRA |= (1 << DDA3);	// Set PA3 to Output for Camera
 
 }
 
  
 
 
 void modules_generic()
 
 {
 
	// Gathers data and performs functions for generic daughter board
 
	
 
@@ -147,34 +145,31 @@
 
		// This is taken care of in interrupt (See geiger.c)
 
		
 
		lastRefresh = time_millis();
 
		if ((time_millis() - lastRefresh) > 1000000)
 
		{
 
			geiger_refresh();	//Refreshes every 1000sec (16min)
 
		}			
 
	  
 
 }
 
  
 
 void modules_cameras()
 
 {
 
	 
 
	// Gathers data and performs functions for cameras daughter board
 
		
 
		//cameras_readAccelXYZ();
 
		
 
		//if ((time_millis() - pulseOn) > CAMERA_PULSE)
 
		//{
 
		//	PORTA &= ~(1 << PA0);	// Pull pin on usb low
 
		//	PORTA &= ~(1 << PA1);	// Pull pin on usb low
 
		//	PORTA &= ~(1 << PA2);	// Pull pin on usb low
 
		//	PORTA &= ~(1 << PA3);	// Pull pin on usb low
 
			
 
		//	if ((time_millis() - lastPicture) > CAMERA_FREQ)	// Frequency of photos
 
		//	{
 
		//		cameras_sendPulse();
 
		//		lastPicture = time_millis();
 
		//	}
 
			
 
		//	pulseOn = time_millis();
 
		//}
 
		
 
		if ((time_millis() - lastPicture) > CAMERA_FREQ)
 
		{
 
			cameras_sendPulse();
 
			lastPicture = time_millis();
 
		}
 
		else if ((time_millis() - lastPicture) > CAMERA_PULSE)
 
		{
 
			PORTA &= ~(1 << PA0);	// Pull pin on usb low
 
			PORTA &= ~(1 << PA1);	// Pull pin on usb low
 
			PORTA &= ~(1 << PA2);	// Pull pin on usb low
 
			PORTA &= ~(1 << PA3);	// Pull pin on usb low
 
		}		
 
 } 
 
  
 
\ No newline at end of file
slave/slave/slave.c
Show inline comments
 
@@ -28,26 +28,24 @@
 
#include "lib/led.h"
 
#include "lib/inputOutput.h"
 
#include "lib/i2c.h"
 
#include "lib/spi.h"
 
#include "lib/geiger.h"
 
#include "lib/sensors.h"
 
#include "lib/cameras.h"
 
#include "lib/loopTimer.h"
 
#include "lib/masterComm.h"
 
#include "lib/watchdog.h"
 

	
 
bool WDTreset = false;
 
 uint32_t lastPicture;
 
 uint8_t pulseOn;
 

	
 
void micro_setup()
 
{
 
	// Generic microcontroller config options
 
	WDTreset = ((MCUSR & 0b00001000) > 0);	// Check if WDT reset occured
 
	MCUSR = 0;		// Clear reset flags
 
	wdt_disable();	// Disable WDT
 
	_delay_ms(20);	// Power debounce
 
	sei();			// Enable interrupts
 
}
 

	
 

	
 
@@ -98,43 +96,24 @@ int main(void)
 

	
 
			snprintf(buff,128,"|ModuleID: %u |BoardTemp: %i |Millis: %lu |Lux: %lu |Press: %lu |Altitude: %lu |Batt: %u |Humidity: %u |spiTemp: %i \r\n ",io_getModuleId(),sensors_getBoardTemp(),time_millis(),sensors_getLux(),sensors_getPressure(),sensors_getAltitude(),sensors_getBatt(),sensors_getHumid(),sensors_getSpiTemp()); //DEBUG
 
			serial1_sendString(buff); //DEBUG
 
			
 
			led_off(0);
 
			lastLoop = time_millis();
 
			
 
// Writes ID to EEPROM, change for all modules and delete after programming
 
// 0 is for generic setup,	 1 is for sensors,	 2 is for Geiger,	3 is for cameras
 
//i2c_write(EEPROM_ADDR, 0x05, 0x02);			
 
			
 
		}	// IFloop
 
		
 
		//if (io_getModuleId() == 3)
 
		//{
 
		//	if ((time_millis() - pulseOn) > CAMERA_PULSE)
 
		//	{
 
		//		PORTA &= ~(1 << PA0);	// Pull pin on usb low
 
		//		PORTA &= ~(1 << PA1);	// Pull pin on usb low
 
		//		PORTA &= ~(1 << PA2);	// Pull pin on usb low
 
		//		PORTA &= ~(1 << PA3);	// Pull pin on usb low
 
				
 
		//		if ((time_millis() - lastPicture) > CAMERA_FREQ)	// Frequency of photos
 
		//		{
 
		//			cameras_sendPulse();
 
		//			lastPicture = time_millis();
 
		//		}
 
				
 
		//		pulseOn = time_millis();
 
		//	}
 
		//}
 

	
 
    }	// While(1)
 
	
 
	return 0;
 
}	// Main
 

	
 

	
 

	
 

	
 

	
 
		/********Examples of data reading and getting******************
 
		x = geiger_getCpm();				//Data get
0 comments (0 inline, 0 general)