Changeset - f859ce69ebdb
[Not reviewed]
default
0 4 0
kripperger@CL-SEC241-09.cedarville.edu - 12 years ago 2013-02-01 17:06:48
kripperger@CL-SEC241-09.cedarville.edu
Master comms
4 files changed with 39 insertions and 3 deletions:
0 comments (0 inline, 0 general)
slave/slave/lib/i2c.h
Show inline comments
 
@@ -15,51 +15,51 @@
 
 
#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
 
unsigned char i2c_read16(unsigned char addr);
 
uint16_t i2c_read16(unsigned char addr);
 
 
#endif /* I2C_H_ */
 
\ No newline at end of file
slave/slave/lib/masterComm.c
Show inline comments
 
@@ -19,105 +19,137 @@
 
 
uint8_t dataTypes;
 
char buff2[64];
 
 
 
char masterComm_checksum(const char* stringPtr)
 
{
 
	char sum = 0;
 
	while(*stringPtr != 0x00)
 
	{
 
		sum += *stringPtr;
 
		stringPtr++;
 
	}
 
	return sum;
 
}
 
 
 
void masterComm_types()
 
{
 
	switch(io_getModuleId())
 
	{
 
		case 0:
 
			// Generic
 
			dataTypes = DATATYPES_GENERIC;
 
			break;
 
			
 
		case 1:
 
			// Sensors
 
			dataTypes = DATATYPES_SENSOR;
 
			break;
 
			
 
		case 2:
 
			// Geiger
 
			dataTypes = DATATYPES_GEIGER;
 
			break;
 
			
 
		case 3:
 
			// Camera
 
			dataTypes = DATATYPES_CAMERA;
 
			break;
 
			
 
		default:
 
			dataTypes = DATATYPES_GENERIC;
 
			break;
 
	}
 
}
 
 
 
void masterComm_packetSend_unsigned(uint8_t id, uint32_t data)
 
{
 
	serial0_sendChar('[');
 
	snprintf(buff2,64,"%u%u",id,data);
 
	serial0_sendString(buff2);
 
	serial0_sendChar(']');
 
	serial0_sendChar(masterComm_checksum(buff2));
 
}
 
 
void masterComm_packetSend_signed(uint8_t id, int32_t data)
 
{
 
	serial0_sendChar('[');
 
	snprintf(buff2,64,"%u%d",id,data);
 
	serial0_sendString(buff2);
 
	serial0_sendChar(']');
 
	serial0_sendChar(masterComm_checksum(buff2));
 
}
 
 
 
 
void masterComm_modules()
 
{
 
	// Send BoardTemperature (Common for all modules)
 
	masterComm_packetSend_signed(0,sensors_getBoardTemp());
 
 
	
 
	// Send Heater Status (Common for all modules)
 
	serial0_sendChar('[');
 
	snprintf(buff2,64,"0%u",sensors_getBoardTemp());
 
	snprintf(buff2,64,"1%u",/*Heater Status Function Here */2);
 
	serial0_sendString(buff2);
 
	serial0_sendChar(']');	
 
	serial0_sendChar(masterComm_checksum(buff2));	
 
	
 
	// Send Battery Level (Common for all modules)
 
	serial0_sendChar('[');
 
	snprintf(buff2,64,"2%u",/*Heater Status Function Here */2);
 
	serial0_sendString(buff2);
 
	serial0_sendChar(']');	
 
	serial0_sendChar(masterComm_checksum(buff2));	
 
	
 
	
 
	// Send module specific sensor readings
 
	switch(io_getModuleId())
 
	{
 
		case 0:
 
			// Generic
 
			
 
			break;
 
		
 
		case 1:
 
			// Sensors
 
			
 
			// Send SPI Temperature (Air)
 
			serial0_sendChar('[');
 
			snprintf(buff2,64,"2%u",sensors_getSpiTemp());
 
			serial0_sendString(buff2);
 
			serial0_sendChar(']');
 
			serial0_sendChar(masterComm_checksum(buff2));
 
			
 
			// Send Ambient Light (Needs to be formatted)
 
			serial0_sendChar('[');
 
			snprintf(buff2,64,"3%u",geiger_getCpm());		//FIX
 
			serial0_sendString(buff2);
 
			serial0_sendChar(']');
 
			serial0_sendChar(masterComm_checksum(buff2));
 
			
 
/*			
 
			// Send CPM (radiation)
 
			serial0_sendChar('[');
 
			snprintf(buff2,64,"7%u",geiger_getCpm());
 
			serial0_sendString(buff2);
 
			serial0_sendChar(']');
 
			serial0_sendChar(masterComm_checksum(buff2));
 
		
 
			// Send CPM (radiation)
 
			serial0_sendChar('[');
 
			snprintf(buff2,64,"7%u",geiger_getCpm());
 
			serial0_sendString(buff2);
 
			serial0_sendChar(']');
 
			serial0_sendChar(masterComm_checksum(buff2));
 
		
 
			// Send CPM (radiation)
 
			serial0_sendChar('[');
 
			snprintf(buff2,64,"7%u",geiger_getCpm());
 
			serial0_sendString(buff2);
 
			serial0_sendChar(']');
 
			serial0_sendChar(masterComm_checksum(buff2));
 
*/
 
			
slave/slave/lib/masterComm.h
Show inline comments
 
/*
 
 * masterComm.h
 
 *
 
 * Created: 1/22/2013 3:40:43 PM
 
 *  Author: kripperger
 
 */ 
 
 
#ifndef MASTERCOMM_H_
 
#define MASTERCOMM_H_
 
 
char masterComm_checksum(const char* stringPtr);
 
 
void masterComm_types();	// Calculates the number of types the module has
 
 
void masterComm_packetSend_unsigned(uint8_t id, uint32_t data);	//Creates the sensor specific packets and sends them
 
void masterComm_packetSend_signed(uint8_t id, int32_t data);
 
 
void masterComm_modules();	// Sends sensor data depending on module
 
 
void masterComm_checkParser();	// Runs parser and checks for data request
 
void masterComm_send();		// Sends data after being requested
 
 
 
 
 
#endif /* MASTERCOMM_H_ */
 
\ No newline at end of file
slave/slave/lib/sensors.c
Show inline comments
 
@@ -174,57 +174,57 @@ void sensors_readPressure()
 
		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
 
}
 

	
 
void sensors_readHumid()
 
{
 
	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()
 
{
 
	// FOR FIRST BYTE:
 
	light = i2c_read(LIGHT_ADDR, 0x03);
 
	// exponent = 8xE3 + 4xE2 + 2xE1 + E0
 
	// mantissa = 8xM7 + 4xM6 + 2xM5 + M4
 
	// light = 2^(exponent)*mantissa*0.72
 

	
 
	// 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
 
}
 

	
 
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;
 
}
 
 
int sensors_getHumid(void)			// Gets relative humidity from variable
 
uint16_t sensors_getHumid(void)			// Gets relative humidity from variable
 
{
 
	return humid;
 
}
 
 
uint8_t sensors_getLight(void)		// Gets light from variable
 
{
 
	return light;
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)