Changeset - 448ac1359093
[Not reviewed]
default
0 6 0
kripperger@CL-SEC241-09.cedarville.edu - 12 years ago 2013-03-18 21:59:28
kripperger@CL-SEC241-09.cedarville.edu
Enable Serial1, WDT, Battery sense, LED Timers
6 files changed with 35 insertions and 26 deletions:
0 comments (0 inline, 0 general)
slave/slave/config.h
Show inline comments
 
@@ -7,39 +7,39 @@
 
 
 
 #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 100				// Frequency of sensor reads (in ms) (should be 200)
 
#define HEATER_THRESHOLD 40			// Temperature threshold in Fahrenheit where heater is activated
 
#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
 
 
 //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 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/inputOutput.c
Show inline comments
 
@@ -75,32 +75,32 @@ int8_t	moduleID;	// Slave Module ID from
 
	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(1);
 
		  led_on(3);
 
	  } 
 
	  else if (sensors_getBoardTemp() > (HEATER_THRESHOLD + 5))
 
	  {
 
		  io_heaterOff();
 
		  led_off(1);
 
		  led_off(3);
 
	  }
 
  }
 
\ No newline at end of file
slave/slave/lib/masterComm.c
Show inline comments
 
/*
 
 * masterComm.c
 
 *
 
 * Created: 1/22/2013 3:40:53 PM
 
 *  Author: kripperger
 
 */ 
 
 
 
#include <inttypes.h>
 
#include <avr/io.h>
 
#include <stdio.h>
 
#include "../config.h"
 
#include "masterComm.h"
 
#include "serial.h"
 
#include "serparser.h"
 
#include "inputOutput.h"
 
#include "sensors.h"
 
#include "geiger.h"
 
#include "led.h"
 
#include "loopTimer.h"
 
 
uint8_t dataTypes;
 
volatile uint32_t lastRX;
 
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;
 
			
 
@@ -139,36 +142,42 @@ void masterComm_modules()
 
		default:
 
			
 
			break;
 
	}
 
}
 
 
 
void masterComm_send()
 
{
 
	masterComm_types();		// Calculates how many data types to send
 
	
 
	// Return request with number of data types to be sent
 
	serial0_sendChar('[');						// Send opening bracket
 
	snprintf(buff2,64,"@%u",dataTypes);				// Send package (@ reply and number of data types)
 
	serial0_sendString(buff2);
 
	serial0_sendChar(']');						// Send closing bracket
 
	serial0_sendChar(masterComm_checksum(buff2));	// Calculate and send checksum
 
	
 
	masterComm_modules();	// Send sensor data
 
}
 
 
 
void masterComm_checkParser()
 
{
 
	if ((time_millis() - lastRX) > 200)	// Timer for LED
 
	{
 
		led_off(2);
 
	}
 
	
 
	if (serparser_parse() == PARSERESULT_PARSEOK)
 
	{
 
		if (getPayloadType() == ('@'-0x30))		// Request for data recieved
 
		{
 
			led_on(2);
 
			lastRX = time_millis();
 
			
 
			// Send all data
 
			masterComm_send();
 
			//led_off(2);
 
		}	
 
	}
 
}
 
 
slave/slave/lib/sensors.c
Show inline comments
 
@@ -7,48 +7,49 @@
 
 
 
#include <inttypes.h>
 
#include <math.h>
 
#include <avr/io.h>
 
#include <avr/interrupt.h>
 
#include "../config.h"
 
#include <util/delay.h>
 
#include "sensors.h"
 
#include "spi.h"
 
#include "i2c.h"
 
 
int16_t	spiTemp;	// Thermocouple Temperature (from spi)
 
int8_t	boardTemp;	// Board Temperature (from i2c)
 
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
 
uint8_t battL;		// Low byte of ADC
 
uint16_t batt;		// Read battery voltage from ADC
 
uint16_t vBatt;		// battery voltage
 
 
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 pressure;
 
uint32_t altitude;
 
@@ -196,67 +197,68 @@ 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()
 
{
 
	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 batt;
 
	return vBatt;
 
}
 
 
uint32_t sensors_getAltitude(void)
 
{
 
	return altitude;
 
}
 
\ No newline at end of file
slave/slave/lib/serial.c
Show inline comments
 
@@ -18,64 +18,61 @@
 
#include <avr/wdt.h>
 
 
// NOTE: USART ISRs for character reception are included in serparser.c
 
 
void serial0_setup() {
 
	//PORTD &= ~(1<<PD0);
 
	//PORTD |= (1<<PD1);
 
	
 
	/* Enable receiver and transmitter */
 
	UCSR0B |= (1<<RXEN0)|(1<<TXEN0); // Enable rx/tx
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01); // - 1 stop bit
 
	
 
	/* Set baud rate */
 
	UBRR0H = (unsigned char)(USART0_BAUD_PRESCALE>>8);
 
	UBRR0L = (unsigned char)USART0_BAUD_PRESCALE;
 
	
 
	UCSR0B |= (1 << RXCIE0); // Enable interrupt
 
	
 
	//UCSR0A |= (1<<RXC0);
 
}
 
 
 
 
 
 
 
void serial1_setup() {
 
//PROVEN in test project
 
 
	/* Enable receiver and transmitter */
 
	UCSR1B |= (1<<RXEN1)|(1<<TXEN1); // Enable rx/tx
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR1C |= (1 << UCSZ10) | (1 << UCSZ11); // - 1 stop bit
 
	
 
	/* Set baud rate */
 
	UBRR1H = (unsigned char)(USART1_BAUD_PRESCALE>>8);
 
	UBRR1L = (unsigned char)USART1_BAUD_PRESCALE;
 
	
 
	UCSR1B |= (1 << RXCIE1); // Enable interrupt
 
	//UCSR1B |= (1 << RXCIE1); // Enable interrupt
 
}
 
 
void serial1_ion() {
 
	UCSR1B |= (1<<RXEN1); // Enable rx
 
	UCSR1B |= (1 << RXCIE1); // Enable interrupt
 
}
 
void serial1_ioff() {
 
	UCSR1B &= ~(1<<RXEN1); // Disable rx
 
	UCSR1B &= ~(1 << RXCIE1); // Disable interrupt
 
}
 
 
void serial0_ion() {
 
	UCSR0B |= (1 << RXCIE0); // Enable interrupt
 
}
 
void serial0_ioff() {
 
	UCSR0B &= ~(1 << RXCIE0); // Disable interrupt
 
}
 
 
 
void serial0_sendChar( unsigned char byte )
 
{
 
	while (!(UCSR0A & (1<<UDRE0)));
 
	UDR0 = byte;
 
}
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 <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"
 

	
 

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

	
 

	
 
int main(void)
 
{
 
	// 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);
 
		
 
	// Power debounce
 
	_delay_ms(20);
 
		
 
	// Initialize	
 
	micro_setup();			// Generic microcontroller config options
 
	time_setup();			// Setup loop timer and interrupts (TIMER0)
 
	//watchdog_setup();		// Setup watchdog timer
 
	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
 
	serial0_setup();		// Config serial port 0
 
	serial1_setup();		// Config serial port 1
 
	
 
	_delay_ms(50);	// Setup hold delay
 
	
 
	io_readModuleId();
 
	modules_setup(io_getModuleId());	// Run setup functions for specific module
 

	
 
	uint32_t lastLoop = 0;
 
	
 
	// Serial output //DEBUG
 
	char buff[128];						//Buffer for serial output //DEBUG
 
	serial0_sendString("Starting Slave\r\n");	//DEBUG
 
	serial1_sendString("Starting Slave\r\n");	//DEBUG
 
			
 
    while(1)
 
    {	
 
		wdt_reset();
 
		
 
		// Master communication
 
		masterComm_checkParser();	//Checks parser for data requests from master
 

	
 
		// Main slave operations
 
		if ((time_millis() % SENSOR_LOOP) == 0)	// Uses program timer to run every so often. Time interval defined in config.h
 
		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
 
//			serial0_sendString(buff); //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
 
			serial1_sendString(buff); //DEBUG
 

	
 
			_delay_ms(1);		// Delay to prevent the sensor loop from running again before time_millis changes
 
			led_off(0);
 
			led_off(2);
 
			
 
			//led_output(i2c_read(EEPROM_ADDR, 0x05));	// Debugging, delete
 
			//led_output(io_getModuleId());
 
			lastLoop = 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
0 comments (0 inline, 0 general)