Changeset - 9c8ca2112fb4
[Not reviewed]
Merge default
0 7 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-04-03 16:56:18
ethanzonca@CL-ENS241-08.cedarville.edu
merge
7 files changed with 95 insertions and 41 deletions:
0 comments (0 inline, 0 general)
slave/slave/config.h
Show inline comments
 
/*
 
 * config.h
 
 *
 
 * Created: 10/25/2012 10:00:09 PM
 
 *  Author: mkanning
 
 */
 
 
 
 #ifndef CONFIG_H_
 
 #define CONFIG_H_
 
 
 #define F_CPU 11059200				// Clock frequency (used in calculations)
 
 
//Serial
 
#define USART0_BAUDRATE 115200
 
#define USART1_BAUDRATE 115200 
 
 
// Circular serial buffer size. Must be at least MAX_CMD_LEN + 5
 
#define BUFFER_SIZE 32
 
 
// 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 10000		// Camera pulse frequency (Should be 30000 for 30 Secs)
 
#define CAMERA_PULSE 500			// Camera pulse duration
 
#define CAMERA_FREQ 5000		// Camera pulse frequency (Should be 30000 for 30 Secs)
 
#define CAMERA_PULSE 2500			// 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 0x26		// Read 0x27 - Write 0x26
 
 #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
 
 
 
 
 #endif /* CONFIG_H_ */
 
\ No newline at end of file
slave/slave/lib/i2c.c
Show inline comments
 
/*
 
 * i2c.c
 
 *
 
 * Created: 11/7/2012 7:18:23 PM
 
 *  Author: kripperger
 
 */ 
 
 
#include <util/delay.h>
 
#include <inttypes.h>
 
#include <compat/twi.h>
 
#include "loopTimer.h"
 
#include "../config.h"
 
#include "i2c.h"
 
 
uint32_t startTime = 0;
 
 
/* I2C clock in Hz */
 
#define SCL_CLOCK  300000L
 
 
 
/*************************************************************************
 
 Initialization of the I2C bus interface. Need to be called only once
 
*************************************************************************/
 
void i2c_init(void)
 
{
 
  /* initialize TWI clock: 300 kHz clock, TWPS = 0 => prescaler = 1 */
 
  
 
  TWSR = 0;                         /* no prescaler */
 
  TWBR = ((F_CPU/SCL_CLOCK)-16)/2;  /* must be > 10 for stable operation */
 
  
 
 
}/* i2c_init */
 
 
 
 
/*************************************************************************	
 
  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();
 
//	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
//	{
 
//		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();
 
//	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
//	{
 
//		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;
 
 
}/* i2c_start */
 
 
 
 
/*************************************************************************
 
 Issues a start condition and sends address and transfer direction.
 
 If device is busy, use ack polling to wait until device is ready
 
 
 
 Input:   address and transfer direction of I2C device
 
*************************************************************************/
 
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!
 
			}
 
		}
 
    
 
    	// 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!
 
			}
 
		}
 
    
 
    	// 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!
 
				}
 
			}
 
	        
 
    	    continue;
 
    	}
 
    	//if( twst != TW_MT_SLA_ACK) return 1;
 
    	break;
 
     }
 
 
}/* i2c_start_wait */
 
 
 
 
/*************************************************************************
 
 Issues a repeated start condition and sends address and transfer direction 
 
 
 Input:   address and transfer direction of I2C device
 
 
 
 Return:  0 device accessible
 
          1 failed to access device
 
*************************************************************************/
 
unsigned char i2c_rep_start(unsigned char address)
 
{
 
    return i2c_start( address );
 
 
}/* i2c_rep_start */
 
 
 
 
/*************************************************************************
 
 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();
 
//	startTime = time_millis();
 
	while(TWCR & (1<<TWSTO))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
//		if ((time_millis() - startTime) > 10)
 
//		{
 
//			break;	// Timeout Reached!
 
//		}
 
	}
 
 
}/* i2c_stop */
 
 
 
 
/*************************************************************************
 
  Send one byte to I2C device
 
  
 
  Input:    byte to be transfered
 
  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!
 
		}
 
	}
 
	
 
	
 
	// 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!
 
		}
 
	}  
 
 
    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!
 
		}
 
	}
 
	
 
    return TWDR;
 
 
}/* i2c_readNak */
 
 
 
 
/*************************************************************************
 
 Write one byte to the I2C device, read is followed by a stop condition 
 
 
 
 Return:  void
 
*************************************************************************/
 
void i2c_write(unsigned char addr, unsigned char reg, unsigned char data)
 
{
 
	i2c_start_wait(addr+I2C_WRITE);     // set device address and write mode
 
	i2c_writeX(reg);                     // write register address
 
	i2c_writeX(data);                    // write value data to register
 
	i2c_stop();                         // set stop condition = release bus
 
 
}/* i2c_write */
 
 
 
 
/*************************************************************************
 
 Read one byte from the I2C device, read is followed by a stop condition 
 
 
 
 Return:  byte read from I2C device
 
*************************************************************************/
 
uint8_t i2c_read(unsigned char addr, unsigned char reg)
 
{
 
	uint8_t   data;
 
	
 
	i2c_start_wait(addr+I2C_WRITE);		// set device address and write mode
 
	i2c_writeX(reg);						// write register address
 
	
 
	i2c_rep_start(addr+I2C_READ);   // set device address and read mode
 
	data = i2c_readNak();               // read one byte
 
	i2c_stop();
 
	
 
    return data;
 
 
}/* 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
 
}/* i2c_read16 */
 
 
uint16_t i2c_humidRead()
 
{
 
	uint16_t   data;
 
	uint8_t   dataL;
 
	
 
	//i2c_start_wait(HUMID_ADDR+I2C_WRITE);	// set device address and write mode
 
	//i2c_rep_start(HUMID_ADDR+I2C_READ);   // set device address and read mode
 
	
 
	i2c_start(HUMID_ADDR+I2C_WRITE);	// Measurement Request
 
	i2c_stop();					// Stop
 
	
 
	
 
	i2c_start(HUMID_ADDR+I2C_READ);		// Measurement Request	
 
 
	i2c_readAck();               // read one byte
 
	i2c_readNak();
 
	data = i2c_readAck();               // read one byte
 
	dataL = i2c_readAck();	
 
	i2c_stop();
 
	
 
	data = data << 8;
 
	data = data | dataL;
 
	
 
	return data;
 
 
}
 
\ No newline at end of file
slave/slave/lib/i2c.h
Show inline comments
 
/*
 
 * i2c.h
 
 *
 
 * Created: 11/7/2012 7:18:33 PM
 
 *  Author: kripperger
 
 */ 
 
 
 
#ifndef I2C_H_
 
#define I2C_H_
 
 
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
 
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
 
#endif
 
 
#include <avr/io.h>
 
 
 
/** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */
 
#define I2C_READ    1
 
/** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */
 
#define I2C_WRITE   0
 
 
 
void i2c_init(void);	//initialize the I2C master interace. Need to be called only once
 
void i2c_stop(void);	//Terminates the data transfer and releases the I2C bus
 
 
unsigned char i2c_start(unsigned char addr);	//Issues a start condition and sends address and transfer direction 
 
	//addr is the address and transfer direction of I2C device
 
	//Returns   0   device accessible 
 
	//Returns   1   failed to access device 
 
 
unsigned char i2c_rep_start(unsigned char addr);	//Issues a repeated start condition and sends address and transfer direction
 
	//addr is the address and transfer direction of I2C device
 
	//Returns   0   device accessible
 
	//Returns   1   failed to access device
 
	
 
void i2c_start_wait(unsigned char addr);	//Issues a start condition and sends address and transfer direction. If device is busy, use ack polling to wait until device ready 
 
	//addr is the address and transfer direction of I2C device
 
 
unsigned char i2c_writeX(unsigned char data);	//Send one byte to I2C device
 
	//data is the byte to be transfered
 
	//Returns   0   write successful
 
	//Returns   1   write failed
 
 
unsigned char i2c_readAck(void);	//read one byte from the I2C device, request more data from device
 
	//Returns byte read from I2C device
 
 
unsigned char i2c_readNak(void);	//read one byte from the I2C device, read is followed by a stop condition
 
	//Returns byte read from I2C device
 
 
unsigned char i2c_readX(unsigned char ack);	//read one byte from the I2C device. Implemented as a macro, which calls either i2c_readAck or i2c_readNak
 
#define i2c_readX(ack)  (ack) ? i2c_readAck() : i2c_readNak(); 
 
	//When ack is 1: send ack, request more data from device
 
	//When ack is 0: send nak, read is followed by a stop condition
 
	//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
 
uint16_t i2c_read16(unsigned char addr);
 
 
uint16_t i2c_humidRead(void);
 
 
#endif /* I2C_H_ */
 
\ No newline at end of file
slave/slave/lib/inputOutput.c
Show inline comments
 
@@ -5,102 +5,102 @@
 
 *  Author: kripperger
 
 */ 
 
 
#include <avr/io.h>
 
#include "../config.h"
 
#include "inputOutput.h"
 
#include "led.h"
 
#include "sensors.h"
 
 
int8_t	moduleID;	// Slave Module ID from rotary dip switch (or EEPROM)
 
 
 void io_configure()
 
 {
 
	// Configure ports/pins
 
	DDRB |= (1 << DDB4);		// Set PB4 to Output for Heater (also allows SCK (on SPI bus) to operate)
 
 
	DDRC &= ~(1 << DDC2);		// Set PC2 to input for rotary dip
 
	DDRC &= ~(1 << DDC3);		// Set PC3 to input for rotary dip
 
	DDRC &= ~(1 << DDC4);		// Set PC4 to input for rotary dip
 
	DDRC &= ~(1 << DDC5);		// Set PC5 to input for rotary dip
 
	
 
	DDRA &= ~(1 << DDA7);		// Set PA7 to input for battery voltage divider
 
 
	//ADC register configurations for battery level detection on PA7
 
	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 << 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
 
	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
 
	
 
 }
 
 
 
  
 
 void io_readModuleId()
 
 {
 
	// Get ID from rotary dip and return it. 
 
	moduleID = 0;
 
	
 
	/*
 
	// This method is temporary as the next release will read the module ID from EEPROM
 
	PORTC |= (1 << PC2);	// Pull pins on rotary dip high
 
	PORTC |= (1 << PC3);	// Pull pins on rotary dip high
 
	PORTC |= (1 << PC4);	// Pull pins on rotary dip high
 
	PORTC |= (1 << PC5);	// Pull pins on rotary dip high
 
	
 
	moduleID = ((PINC & 0b00011100) >> 2);		// Read Dip Encoder
 
	moduleID = ~moduleID;						//Invert Dip reading
 
	moduleID = (moduleID & 0b0111);				//Mask bits
 
	*/
 
 
	while(moduleID==0)           //UNCOMMENT ALL THIS
 
	{
 
		moduleID = i2c_read(EEPROM_ADDR, 0x05);
 
	}
 
 }
 
 
 
 
 
 uint8_t io_getModuleId()
 
 {
 
	return moduleID;
 
 }
 
 
 
 
 
 void io_heaterOn()
 
 {		
 
	PORTB |= (1 << PB4);	//ON
 
 }
 
 
 
 void io_heaterOff()
 
 {
 
	PORTB &= ~(1 << PB4);	//OFF
 
 }
 
 
 
 uint8_t io_heaterStatus()
 
 {
 
	 uint8_t state;
 
	 state = 0;
 
	 
 
	 state = ((PORTB & 0b00010000) >> 4);
 
	 
 
	 return state;
 
 }
 
 
 
  void io_regulateTemp()
 
  {
 
	  // Gets board temperature and enables heater if below threshold
 
	  if (sensors_getBoardTemp() <= HEATER_THRESHOLD)
 
	  {
 
		  io_heaterOn();
 
		  led_on(3);
 
	  } 
 
	  else if (sensors_getBoardTemp() > (HEATER_THRESHOLD + 5))
 
	  else if (sensors_getBoardTemp() > (HEATER_THRESHOLD + 4))
 
	  {
 
		  io_heaterOff();
 
		  led_off(3);
 
	  }
 
  }
 
\ No newline at end of file
slave/slave/lib/sensors.c
Show inline comments
 
@@ -99,166 +99,169 @@ void sensors_setupPressure()
 
	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);
 
	uint8_t one = send_spi(0xFF);
 
	_delay_us(100);
 
	uint8_t two = send_spi(0xFF);
 
	_delay_us(100);
 
	uint8_t three = send_spi(0xFF);
 
	_delay_us(100);
 
	uint8_t four = send_spi(0xFF);
 
	DESELECT_TEMP;
 
	
 
	int16_t temperature = ((one<<4)|(two>>4));	// Shift and place into larger int. (Cuts off Decimal)
 
	temperature = (temperature & (0x0800)) ? (temperature & 0xF000) : temperature;	// Sign extend
 
	
 
	//int16_t temperature = ((one<<6)|(two>>2));	// Shift and place into larger int. (Includes Decimal)
 
	//temperature = (temperature & (0x2000)) ? (temperature & 0xC000) : temperature;	// Sign extend
 
	
 
	temperature = (two & 0x01) ? 0x00DE : temperature;	// Error Condition. If error is detected output is set to 222 degrees (0x00DE)
 
	
 
	// Note: Temperature still needs to be scaled in order to be accurate (eg. boil water). Do this before implementing.
 
	spiTemp = temperature;
 
}
 
 
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()
 
{
 
	i2c_write(PRESSURE_ADDR, 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(PRESSURE_ADDR, 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)
 
	{
 
		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
 
	
 
	altitude = (float)44330 * (1 - pow(((float) pressure/101325), 0.190295));
 
}
 
 
void sensors_readHumid()
 
{
 
	humid = i2c_read16(HUMID_ADDR);
 
	//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;
 
	 //humid = (humid / 16383) * 100;
 
}
 
 
void sensors_readLux()
 
{
 
	lightH = i2c_read(LIGHT_ADDR, 0x03);
 
	lightL = i2c_read(LIGHT_ADDR, 0x04);
 
	
 
	exponent = lightH;
 
	exponent = exponent >> 4;
 
	
 
	lightH = lightH << 4;
 
	mantissa = lightH | lightL;
 
 
	lux = (float)(pow(2,exponent) * mantissa) * 0.045;
 
}
 
 
void sensors_readBatt()
 
{
 
	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;
 
	vBatt = (batt * 10.0) / 67.4;
 
}
 
 
 
 
 
 
 
 
 
int16_t sensors_getSpiTemp(void)	// Gets spi temperature from variable
 
{
 
	return spiTemp;
 
}
 
 
int8_t sensors_getBoardTemp(void)	// Gets board temperature from variable
 
{
 
	return boardTemp;
 
}
 
 
int32_t sensors_getPressure(void)	// Gets pressure from variable
 
{
 
	return pressure;
 
}
 
 
uint16_t sensors_getHumid(void)			// Gets relative humidity from variable
 
{
 
	return humid;
 
}
 
 
uint32_t sensors_getLux(void)		// Gets light from variable
 
{
 
	return lux;
 
}
 
 
uint16_t sensors_getBatt(void)		// Gets battery voltage from variable
 
{
 
	return vBatt;
 
}
 
 
uint32_t sensors_getAltitude(void)
 
{
 
	return altitude;
 
}
 
\ No newline at end of file
slave/slave/modules.c
Show inline comments
 
@@ -40,132 +40,132 @@
 
			modules_cameras_setup();
 
			break;
 
			
 
		default:
 
			modules_generic_setup();
 
			break;
 
	}
 
	  
 
 }
 
 
 
 void modules_run(uint8_t id)
 
 {
 
	switch(id)
 
	{
 
		case 0:
 
			modules_generic();
 
			break;
 
		  
 
		case 1:
 
			modules_sensors();
 
			break;
 
		  
 
		case 2:
 
			modules_geiger();
 
			break;
 
		  
 
		case 3:
 
			modules_cameras();
 
			break;
 
		  
 
		default:
 
			modules_generic();
 
			break;
 
	}
 
	  
 
 }
 
 
 
 
 
 void modules_generic_setup()
 
 {
 
	  
 
 }
 
  
 
 void modules_sensors_setup()
 
 {
 
	DESELECT_TEMP;
 
	setup_spi();
 
	sensors_setupPressure();
 
 }
 
  
 
 void modules_geiger_setup()
 
 {
 
	// Pin setup
 
	DDRA &= ~(1 << DDA0);	// PA0 is an input
 
	
 
	
 
	 
 
	// Setup for interrupt input on PA0 (PCINT0)
 
	PCMSK0 |= (1 << PCINT0);	// Enable interrupt for PA0
 
	PCICR |= (1 << PCIE0);		// Enable ioc section PCIF0
 
	
 
	// Setup for interrupt from Timer2
 
	ASSR &= ~(1 << EXCLK);	// Disable external clock input (enabling crystal use)
 
	ASSR |= (1 << AS2);		// Enable timer2 async mode with an external crystal	
 
	_delay_ms(250);			// 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();	 
 
	 
 
	 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
 
	
 
 }
 
  
 
 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_readHumid();			//Data Read
 
	sensors_readLux();				//Data Read
 
	 
 
 }
 
  
 
 void modules_geiger()
 
 {
 
	// No data gatering function needed for geiger daughter board
 
		// This is taken care of in interrupt (See geiger.c)
 
	  
 
 }
 
  
 
 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() - 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();
 
			}
 
		//	if ((time_millis() - lastPicture) > CAMERA_FREQ)	// Frequency of photos
 
		//	{
 
		//		cameras_sendPulse();
 
		//		lastPicture = time_millis();
 
		//	}
 
			
 
			pulseOn = time_millis();
 
		}
 
		//	pulseOn = time_millis();
 
		//}
 
		
 
 } 
 
  
 
\ No newline at end of file
slave/slave/slave.c
Show inline comments
 
/*
 
 * Slave Firmware
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 *
 
 * Kyle Ripperger
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Matthew Kroening
 
 *
 
 */
 

	
 

	
 
#include "config.h"
 

	
 
#include <stdio.h>
 
#include <stdbool.h>
 
#include <inttypes.h>
 
#include <avr/io.h>
 
#include <compat/twi.h>
 
#include <util/delay.h>
 
#include <avr/cpufunc.h>
 
#include <avr/interrupt.h>
 
#include <avr/wdt.h>
 
#include "modules.h"
 
#include "lib/serial.h"
 
#include "lib/serparser.h"
 
#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
 
}
 

	
 

	
 

	
 
int main(void)
 
{
 

	
 
		
 
	// Initialize	
 
	micro_setup();			// Generic microcontroller config options
 
	time_setup();			// Setup loop timer and interrupts (TIMER0)
 
	watchdog_setup();		// Setup watchdog timer
 
	led_configure();		// Configure ports and registers for LED operation
 
	io_configure();			// Configure IO ports and registers
 
	i2c_init();				// Setup I2C
 
	serial0_setup();		// Config serial port 0
 
	serial1_setup();		// Config serial port 1
 
	
 
	_delay_ms(50);	// Setup hold delay
 
	if(WDTreset) led_on(1);	// Turn on LED if WDT reset has occurred	
 
	
 
	io_readModuleId();
 
	modules_setup(io_getModuleId());	// Run setup functions for specific module
 

	
 
	uint32_t lastLoop = 0;	// Time in ms when last loop occurred 
 
	
 
	// Serial output //DEBUG
 
	char buff[128];						//Buffer for serial output //DEBUG
 
	serial1_sendString("Starting Slave\r\n");	//DEBUG
 
			
 
    while(1)
 
    {	
 
		wdt_reset();	// Resets WDT (to prevent restart)
 
		
 
		// Master communication
 
		masterComm_checkParser();	//Checks parser for data requests from master
 

	
 
		// Main slave operations
 
		if ((time_millis() - lastLoop) > SENSOR_LOOP)	// Uses program timer to run every so often. Time interval defined in config.h
 
		{
 
			led_on(0);
 
			
 
			sensors_readBatt();				// Read Battery level
 
			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: %u \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 |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, 0x03);			
 
			
 
		}
 

	
 
		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();
 
			}
 
		}
 

	
 
    }
 
	
 
	return 0;
 
}
 

	
 

	
 

	
 

	
 

	
 
		/********Examples of data reading and getting******************
 
		x = geiger_getCpm();				//Data get
 
		x = sensors_getSpiTemp();			//Data get
 
		x = sensors_getBoardTemp();			//Data get
 
		
 
		sensors_readSpiTemp();				//Data Read
 
		sensors_readBoardTemp();			//Data Read
 
		
 
		led_output(0xFF);					//Output value to LED array
 
		i2c_write(RTC_ADDR, 0x05, 0x3A);	//i2c Write Example
 
		
 
		PORTA &= ~(1 << PA1);	//OFF
 
		PORTA |= (1 << PA1);	//ON
 
		PORTB ^= (1 << PB0);	//Toggle
 
		
 
		sprintf(buff, "log: %u,%u,%u,%u\r\n", temp,temp2,temp3,temp4);
 
		serial0_sendString(buff);
 
		
 
		**************************************************************/
 
		
 
		
 
		
 
		
 
\ No newline at end of file
0 comments (0 inline, 0 general)