Changeset - 7dda76b540dc
[Not reviewed]
default
0 13 0
kripperger@CL-SEC241-09.cedarville.edu - 12 years ago 2012-11-29 15:20:53
kripperger@CL-SEC241-09.cedarville.edu
Function setup and module flow
13 files changed with 79 insertions and 60 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
 
 
#define USART0_BAUDRATE 115200
 
#define USART1_BAUDRATE 115200
 
 
 //I2C Addresses
 
 #define BOARDTEMP_ADDR 0xA5	//THIS VALUE IS WRONG
 
 #define BOARDTEMP_ADDR 0x90	// Read 0x91 - Write 0x90
 
 #define PRESSURE_ADDR 0xA1		//THIS VALUE IS WRONG
 
 #define HUMID_ADDR 0xA4		//THIS VALUE IS WRONG
 
 #define RTC_ADDR 0xA2			//DEBUG       //read A3 - write A2
 
 #define RTC_ADDR 0xA2			//DEBUG [Used for testing]      // Read 0xA3 - Write 0xA2
 
 
 
 
 #endif /* CONFIG_H_ */
 
\ No newline at end of file
slave/slave/lib/geiger.c
Show inline comments
 
@@ -17,28 +17,28 @@ volatile uint8_t seconds;		// Counts Sec
 
volatile uint16_t counts;		// Counts the pulses
 
volatile uint16_t cpm;			// Counts per minuite
 
 
 
ISR(TIMER2_OVF_vect)    // Timer 2 overflow interrupt handler
 
{
 
	// This executes every second.  Update real-time clocks.
 
	// Used only in Geiger module
 
	
 
	seconds++;
 
	if (seconds==60)
 
	{
 
		seconds = 0;
 
		cpm = counts;
 
		counts = 0;
 
	}
 
}
 
 
ISR(PCINT0_vect)    // Interrupt on PA0
 
{
 
	// Interrupts when pulse received from Geiger tube
 
	counts++;	// Increment counter.
 
}
 
 
uint16_t geiger_getCpm()
 
inline uint16_t geiger_getCpm()
 
{
 
	return cpm;
 
}
 
\ No newline at end of file
slave/slave/lib/geiger.h
Show inline comments
 
/*
 
 * geiger.h
 
 *
 
 * Created: 11/19/2012 9:24:17 PM
 
 *  Author: kripperger
 
 */ 
 
 
 
#ifndef GEIGER_H_
 
#define GEIGER_H_
 
 
uint16_t geiger_getCpm();
 
inline uint16_t geiger_getCpm();
 
 
 
#endif /* GEIGER_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 <inttypes.h>
 
#include <compat/twi.h>
 
#include "../config.h"
 
#include "i2c.h"
 
 
 
/* I2C clock in Hz */
 
#define SCL_CLOCK  100000L
 
 
 
 
/*************************************************************************
 
 Initialization of the I2C bus interface. Need to be called only once
 
*************************************************************************/
 
void i2c_init(void)
 
{
 
  /* initialize TWI clock: 100 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);
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
slave/slave/lib/inputOutput.c
Show inline comments
 
/*
 
 * io.c
 
 *
 
 * Created: 11/7/2012 7:17:52 PM
 
 *  Author: kripperger
 
 */ 
 
 
 #include <avr/io.h>
 
 
 
 uint8_t io_getModuleId() {
 
	 
 
	// Get ID from rotary dip and return it. 
 
	uint8_t id;
 
 
	id = 0; //DEBUG
 
 
	return id;
 
 }
 
\ No newline at end of file
slave/slave/lib/inputOutput.h
Show inline comments
 
/*
 
 * io.h
 
 *
 
 * Created: 11/7/2012 7:18:08 PM
 
 *  Author: kripperger
 
 */ 
 
 
 
 #ifndef IO_H_
 
 #define IO_H_
 
 
 uint8_t io_getModuleId();	// Get ID from rotary dip and return it.
 
 
 
 
 #endif /* IO_H_ */
 
\ No newline at end of file
slave/slave/lib/led.c
Show inline comments
 
/*
 
* led.c
 
*
 
* Created: 10/25/2012 10:03:22 PM
 
*  Author: mkanning
 
*/
 
 
#include <avr/io.h>
 
#include "led.h"
 
 
void led_configure()
 
{
 
	// Configure ports/pins for LEDs
 
	DDRB |= (1 << DDRB0);		// Set PB0 to Output
 
	DDRB |= (1 << DDRB1);		// Set PB1 to Output
 
	DDRB |= (1 << DDRB2);		// Set PB2 to Output
 
	DDRB |= (1 << DDRB3);		// Set PB3 to Output	
 
	
 
	// Setup PWM
 
		//TODO
 
	
 
}
 
 
void led_on(uint8_t led)
 
{
 
	// Turn the specified LED on
 
	 
 
}
 
 
void led_off(uint8_t led)
 
{
 
	// Turn the specified LED off
 
	 
 
}
 
 
void led_toggle(uint8_t led)
 
{
 
	// Toggle the specified LED
 
	 
 
}
 
\ No newline at end of file
slave/slave/lib/sensors.c
Show inline comments
 
/*
 
 * sensors.c
 
 *
 
 * Created: 11/19/2012 9:25:01 PM
 
 *  Author: kripperger
 
 */ 
 
 
 
#include <inttypes.h>
 
#include <avr/io.h>
 
#include <avr/interrupt.h>
 
#include "../config.h"
 
#include <util/delay.h>
 
#include "sensors.h"
 
#include "spi.h"
 
 
#include "serial.h"	//DEBUG////////////////////////////////////////////////////////////////////////////
 
int16_t	spiTemp;	// Thermocouple Temperature (from spi)
 
int8_t	boardTemp;	// Board Temperature (from i2c)
 
 
int16_t readSpiTemp()
 
 
void sensors_readSpiTemp()
 
{
 

	
 
	char buff[32];		//DEBUG////////////////////////////////////////////////////////////////////////
 

	
 
	// 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));
 
	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()
 
{
 
	int8_t temperature = i2c_read(BOARDTEMP_ADDR, 0x00);	// Read only the first byte of data (we don't need the resolution here)
 
	
 
			sprintf(buff, "Temperature: %d     Error: %u\r\n", temperature,(two & 0x01));		//DEBUG/////////////////////////////
 
			serial0_sendString(buff);													//DEBUG/////////////////////////////
 
	
 
	return temperature;
 
}
 
\ No newline at end of file
 
	boardTemp = temperature;
 
}
 

	
 
int16_t sensors_getSpiTemp(void)	// Gets spi temperature from variable
 
{
 
	return spiTemp;
 
}
 
 
int8_t sensors_getBoardTemp(void)	// Gets board temperature from variable
 
{
 
	return boardTemp;
 
}
 
slave/slave/lib/sensors.h
Show inline comments
 
/*
 
 * sensors.h
 
 *
 
 * Created: 11/19/2012 9:24:50 PM
 
 *  Author: kripperger
 
 */ 
 
 
 
#ifndef SENSORS_H_
 
#define SENSORS_H_
 
 
int16_t readSpiTemp(void);
 
 
void sensors_readSpiTemp(void);		// Reads spi temperature
 
void sensors_readBoardTemp(void);	// Reads board temperature
 
 
int16_t sensors_getSpiTemp(void);	// Gets spi temperature from variable
 
int8_t sensors_getBoardTemp(void);	// Gets board temperature from variable
 
 
#endif /* SENSORS_H_ */
 
\ No newline at end of file
slave/slave/lib/spi.c
Show inline comments
 
/* 
 
 * 
 
 *
 
 *
 
 *
 
 *
 
 *
 
 */
 

	
 

	
 
#include "spi.h"
 

	
 

	
 
void setup_spi()
 
{
 
	DESELECT_TEMP;
 
	
 
	// specify pin directions for SPI pins on port B
 
	DDRB |= (1<<SPI_MOSI_PIN);	// output
 
	DDRB &= ~(1<<SPI_MISO_PIN); // input
 
	DDRB |= (1<<SPI_SCK_PIN);	// output
 
	DDRA |= (1<<SPI_SS_PIN);	// output
 
		
 
	// initialize SPI with lowest frequency; max. 400kHz during identification mode of card
 
	SPCR0 = (0 << SPIE0) | // SPI Interrupt Enable
 
	(1 << SPE0)  | // SPI Enable
 
	(0 << DORD0) | // Data Order: MSB first
 
	(1 << MSTR0) | // Master mode
 
	(0 << CPOL0) | // Clock Polarity: SCK low when idle
 
	(0 << CPHA0) | // Clock Phase: sample on rising SCK edge
 
	(1 << SPR10);   // Clock Frequency: f_OSC / 128
 
	//(1 << SPR00); // commentnig this out means /64, which gives over 100khz as required
 
	SPSR0 &= ~(1 << SPI2X0); // No doubled clock frequency
 

	
 

	
 
}
 

	
 
void disable_spi()
 
{
 
  SPCR0 = 0;
 
}
 

	
 
uint8_t send_spi(uint8_t out)
 
{
 
  SPDR0 = out;
 
  while (!(SPSR0 & (1<<SPIF0)));
 
  return SPDR0;
 
}
 

	
 
// Used in slave mode
 
uint8_t received_from_spi(uint8_t data)
 
{
 
  SPDR0 = data;
 
  return SPDR0;
 
}
 

	
slave/slave/modules.c
Show inline comments
 
@@ -57,86 +57,85 @@
 
		  
 
		 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();
 
	 
 
 }
 
  
 
 void modules_geiger_setup()
 
 {
 
	// Pin setup
 
	DDRA &= ~(1 << DDRA0);	// 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
 
	
 
	sei();					// Enable all interrupts
 
 
 
 
 
 }
 
  
 
  
 
  
 
 void modules_cameras_setup()
 
 {
 
	  	  
 
 }
 
  
 
 
 
 
 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_readSpiTemp();			//Data Read
 
	sensors_readBoardTemp();		//Data Read
 
	 
 
 }
 
  
 
 void modules_geiger()
 
 {
 
	// Gathers data and performs functions for geiger daughter board
 
		// This is taken care of in interrupt
 
	  
 
 }
 
  
 
 void modules_cameras()
 
 {
 
	// Gathers data and performs functions for cameras daughter board
 
  
 
 } 
 
  
 
\ 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 <inttypes.h>
 
#include <avr/io.h>
 
#include <compat/twi.h>
 
#include <util/delay.h>
 
#include <avr/interrupt.h>
 
#include "modules.h"
 
#include "lib/serial.h"		//DEBUG
 
#include "lib/serial.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"
 
 
 
void micro_setup()
 
{
 
	// Generic microcontroller config options
 
	sei();
 
	DDRB |= (1 << DDRB4);		// Set PB4 to Output for Heater
 
	sei();	// Enable interrupts
 
	DDRB |= (1 << DDRB4);		// Set PB4 to Output for Heater (also allows SCK to operate)
 
	
 
}
 
 
 
int main(void)
 
{
 
	// Initialize
 
	micro_setup();			// Generic microcontroller config options
 
	led_configure();		//
 
	led_configure();		// Configure ports and registers for LED operation
 
	i2c_init();				// Setup I2C
 
	serial0_setup();		// Config serial ports   //DEBUG
 
	serial0_setup();		// Config serial port
 
	
 
	uint8_t moduleID = io_getModuleId(); // Slave Module ID from rotary dip switch
 
	moduleID=1;	//DEBUG///////////////////////////////////////////////////////////////////////////////////////////
 
	modules_setup(moduleID);
 
	//moduleID=1;	//DEBUG///////////////////////////////////////////////////////////////////////////////////////////
 
	modules_setup(moduleID);	// Run setup functions for specific module
 
 
	
 
 
	//char buff[32];	//DEBUG///////////////////////////////////////////////////////////////////////////////////////
 
	
 
	uint8_t temp;	//DEBUG///////////////////////////////////////////////////////////////////////////////////////
 
	//PORTA &= ~(1 << PA1);	//DEBUG////////////////OFF///////////////////////////////////////////////////////////////
 
	//PORTA |= (1 << PA1);	//DEBUG///////////////ON////////////////////////////////////////////////////////////////
 
	
 
	
 
	//PORTA &= ~(1 << PA1);	//DEBUG///////////////////////////////////////////////////////////////////////////////
 
	//PORTA |= (1 << PA1);	//DEBUG///////////////////////////////////////////////////////////////////////////////
 
	//PORTA=0;//DEBUG/////////////////////////////////////////////////////////////////////////////////////////////
 
	temp=0;//DEBUG////////////////////////////////////////////////////////////////////////////////////////////////
 
	//char buff[32];	//DEBUG///////////////////////////////////////////////////////////////////////////////////////
 
	//serial0_sendString("Starting\r\n");
 
	
 
    while(1)
 
    {
 
		
 
		modules_run(moduleID);
 
				
 
		modules_run(moduleID);	// Runs specific module functions (like data reading)
 
								// Use program timer to run every so often. Time interval defined in config.h
 
 
 
 
 
		//sprintf(buff, "log: %u,%u,%u,%u\r\n", temp,temp2,temp3,temp4);
 
		//serial0_sendString(buff);
 
 
		temp=readSpiTemp();
 
 
        //serial_SendCommand('0','A',0,0);	//DEBUG: EXAMPLE//////////////////////////////////////////////////////
 
        
 
        //i2c_write(RTC_ADDR, 0x05, 0x3A);	//DEBUG: EXAMPLE//////////////////////////////////////////////////////
 
 
        _delay_ms(10);
 
        //temp = i2c_read(RTC_ADDR, 0x02);		//DEBUG: EXAMPLE: seconds/////////////////////////////////////////
 
        
 
      //  PORTA = temp;		//DEBUG///////////////////////////////////////////////////////////////////////////////
 
 
 
		
 
        _delay_ms(10);	//DEBUG
 
		
 
		
 
		
 
		
 
		/********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
 
		
 
		
 
		***************************************************/
 
		
 
 
    }
 
	
 
	return 0;
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)