Changeset - 79192435782d
[Not reviewed]
default
0 9 0
kripperger@CL-SEC241-09.cedarville.edu - 12 years ago 2012-11-27 21:55:17
kripperger@CL-SEC241-09.cedarville.edu
Spi temp
9 files changed with 94 insertions and 77 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 USART_BAUDRATE 19200
 
 
#define USART0_BAUDRATE 115200
 
#define USART1_BAUDRATE 115200
 
 
 //I2C Addresses
 
 #define BOARDTEMP_ADDR 0xA5	//THIS VALUE IS WRONG
 
 #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
 
 
 
 
 #endif /* CONFIG_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
 
 */
 
* 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
 
	 
 
 }
 
#include <avr/io.h>
 
#include "led.h"
 
 
 void led_on(uint8_t led)
 
 {
 
	 // Turn the specified LED on
 
	 
 
 }
 
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	
 
	
 
	
 
}
 
 
 void led_off(uint8_t led)
 
 {
 
	 // Turn the specified LED off
 
void led_on(uint8_t led)
 
{
 
	// Turn the specified LED on
 
	 
 
 }
 
}
 
 
 void led_toggle(uint8_t led)
 
 {
 
	 // Toggle the specified LED
 
void led_off(uint8_t led)
 
{
 
	// Turn the specified LED off
 
	 
 
 }
 
\ No newline at end of file
 
}
 
 
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
 
 
uint8_t readSpiTemp()
 
int16_t readSpiTemp()
 
{
 

	
 
	char buff[32];//DEBUG
 

	
 
	// select TEMP wait 100 microseconds then read four bytes
 
	SELECT_TEMP;
 
	_delay_us(100);
 
	unsigned char one = send_spi(0xFF);
 
	uint8_t one = send_spi(0xFF);
 
	_delay_us(100);
 
	unsigned char two = send_spi(0xFF);
 
	uint8_t two = send_spi(0xFF);
 
	_delay_us(100);
 
	unsigned char three = send_spi(0xFF);
 
	uint8_t three = send_spi(0xFF);
 
	_delay_us(100);
 
	unsigned char four = send_spi(0xFF);
 
	uint8_t four = send_spi(0xFF);
 
	DESELECT_TEMP;
 
	
 
	uint8_t error = (two & 0x01);
 
	
 
	//return ((0x1F & one) << 7) | (two >> 1);
 
	return 0;	//DEBUG
 
	int16_t temperature = ((one<<6)&(two>>2));
 
	temperature = (temperature & (0x2000)) ? (temperature & 0xC000) : temperature;	// Sign extend
 
	
 
	
 
			sprintf(buff, "log: %u,%u,%u,%u\r\n", one,two,three,four);
 
			serial0_sendString(buff);
 
	
 
	return temperature;
 
}
 
\ No newline at end of file
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_
 
 
uint8_t readSpiTemp(void);
 
int16_t readSpiTemp(void);
 
 
 
 
#endif /* SENSORS_H_ */
 
\ No newline at end of file
slave/slave/lib/spi.c
Show inline comments
 
/* 
 
 * 
 
 *
 
 *
 
 *
 
 *
 
 *
 
 */
 

	
 

	
 
#include "spi.h"
 

	
 

	
 
void setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock)
 
void setup_spi()
 
{
 
	DESELECT_TEMP;
 
	
 
  // specify pin directions for SPI pins on port B
 
	if (clock == SPI_SLAVE) {		// if slave SS and SCK is input
 
		DDRB &= ~(1<<SPI_MOSI_PIN); // input
 
		DDRB |= (1<<SPI_MISO_PIN);	// output
 
		DDRB &= ~(1<<SPI_SS_PIN);	// input
 
		DDRB &= ~(1<<SPI_SCK_PIN);	// input
 
	} else {
 
		DDRB |= (1<<SPI_MOSI_PIN);	// output
 
		DDRB &= ~(1<<SPI_MISO_PIN); // input
 
		DDRB |= (1<<SPI_SCK_PIN);	// output
 
		DDRB |= (1<<SPI_SS_PIN);	// output
 
	}
 
	 SPCR0 = ((interrupt ? 1 : 0)<<SPIE0) // interrupt enabled
 
		| (1<<SPE0)			// enable SPI
 
		| (dord<<DORD0)		// LSB or MSB
 
		| (((clock != SPI_SLAVE) ? 1 : 0) <<MSTR0)  // Slave or Master
 
		| (((mode & 0x02) == 2) << CPOL0)			// clock timing mode CPOL
 
		| (((mode & 0x01)) << CPHA0)				// clock timing mode CPHA
 
		| (((clock & 0x02) == 2) << SPR10)			// cpu clock divisor SPR1
 
		| ((clock & 0x01) << SPR00);				// cpu clock divisor SPR0
 
	SPSR0 = (((clock & 0x04) == 4) << SPI2X0);	// clock divisor SPI2X
 
	// 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;
 
}
 

	
 
uint8_t received_from_spi(uint8_t data)
 
{
 
  SPDR0 = data;
 
  return SPDR0;
 
}
 

	
slave/slave/lib/spi.h
Show inline comments
 
/* 
 
 *
 
 *
 
 *
 
 *
 
 *
 
 */
 

	
 

	
 
#ifndef SPI_H_
 
#define SPI_H_
 

	
 
#include <avr/io.h>
 

	
 

	
 
#define SPI_SS_PIN PORTA1
 
#define SPI_SCK_PIN PORTB7
 
#define SPI_MOSI_PIN PORTB5
 
#define SPI_MISO_PIN PORTB6
 
#define SPI_SS_PIN PA1
 
#define SPI_SCK_PIN PB7
 
#define SPI_MOSI_PIN PB5
 
#define SPI_MISO_PIN PB6
 

	
 
#define SELECT_TEMP PORTA &= ~(1<<PA1)
 
#define DESELECT_TEMP PORTA |= (1<<PA1)
 

	
 

	
 
// SPI clock modes
 
#define SPI_MODE_0 0x00 /* Sample (Rising) Setup (Falling) CPOL=0, CPHA=0 */
 
#define SPI_MODE_1 0x01 /* Setup (Rising) Sample (Falling) CPOL=0, CPHA=1 */
 
#define SPI_MODE_2 0x02 /* Sample (Falling) Setup (Rising) CPOL=1, CPHA=0 */
 
#define SPI_MODE_3 0x03 /* Setup (Falling) Sample (Rising) CPOL=1, CPHA=1 */
 

	
 
// data direction
 
#define SPI_LSB 1 /* send least significant bit (bit 0) first */
 
#define SPI_MSB 0 /* send most significant bit (bit 7) first */
 

	
 
// whether to raise interrupt when data received (SPIF bit received)
 
#define SPI_NO_INTERRUPT 0
 
#define SPI_INTERRUPT 1
 

	
 
// slave or master with clock diviser
 
#define SPI_SLAVE 0xF0
 
#define SPI_MSTR_CLK4 0x00 /* chip clock/4 */
 
#define SPI_MSTR_CLK16 0x01 /* chip clock/16 */
 
#define SPI_MSTR_CLK64 0x02 /* chip clock/64 */
 
#define SPI_MSTR_CLK128 0x03 /* chip clock/128 */
 
#define SPI_MSTR_CLK2 0x04 /* chip clock/2 */
 
#define SPI_MSTR_CLK8 0x05 /* chip clock/8 */
 
#define SPI_MSTR_CLK32 0x06 /* chip clock/32 */
 

	
 

	
 

	
 
// setup spi
 
void setup_spi(uint8_t mode,   // timing mode SPI_MODE[0-4]
 
	       int dord,             // data direction SPI_LSB|SPI_MSB
 
	       int interrupt,        // whether to raise interrupt on recieve
 
	       uint8_t clock); // clock diviser
 
void setup_spi();
 

	
 
// disable spi
 
void disable_spi(void);
 

	
 
// send and receive a byte of data (master mode)
 
uint8_t send_spi(uint8_t out);
 

	
 
// receive the byte of data waiting on the SPI buffer and
 
// set the next byte to transfer - for use in slave mode
 
// when interrupts are enabled.
 
uint8_t received_from_spi(uint8_t out);
 

	
 

	
 
#endif /* SPI_H_ */
 

	
slave/slave/modules.c
Show inline comments
 
@@ -57,50 +57,50 @@
 
		  
 
		 case 2:
 
			modules_geiger();
 
			break;
 
		  
 
		 case 3:
 
			modules_cameras();
 
			break;
 
		  
 
		 default:
 
			modules_generic();
 
			break;
 
	 }
 
	  
 
 }
 
 
 
 
 
 void modules_generic_setup()
 
 {
 
	  
 
 }
 
  
 
 void modules_sensors_setup()
 
 {
 
	 setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
 
	  
 
	 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
 
 
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"		//Not made yet
 
#include "lib/serial.h"		//DEBUG
 
#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
 
	
 
	//DDRA = 0xFE;		//PORTA is output //DEBUG
 
 
 
	sei();
 
	DDRB |= (1 << DDRB4);		// Set PB4 to Output for Heater
 
}
 
 
 
int main(void)
 
{
 
	// Initialize
 
	micro_setup();			// Generic microcontroller config options
 
	led_configure();		//
 
	i2c_init();				// Setup I2C
 
	//serial_setup();		// Config serial ports
 
	serial0_setup();		// Config serial ports   //DEBUG
 
	
 
	uint8_t moduleID = io_getModuleId(); // Slave Module ID from rotary dip switch
 
	//moduleID=2;	//DEBUG///////////////////////////////////////////////////////////////////////////////////////////
 
	moduleID=1;	//DEBUG///////////////////////////////////////////////////////////////////////////////////////////
 
	modules_setup(moduleID);
 
 
	
 
 
	
 
	//char buff[32];	//DEBUG///////////////////////////////////////////////////////////////////////////////////////
 
	
 
	uint8_t temp;	//DEBUG///////////////////////////////////////////////////////////////////////////////////////
 
	
 
	
 
	//PORTA &= ~(1 << PA1);	//DEBUG///////////////////////////////////////////////////////////////////////////////
 
	//PORTA |= (1 << PA1);	//DEBUG///////////////////////////////////////////////////////////////////////////////
 
	//PORTA=0;//DEBUG/////////////////////////////////////////////////////////////////////////////////////////////
 
	temp=0;//DEBUG////////////////////////////////////////////////////////////////////////////////////////////////
 
	//serial0_sendString("Starting\r\n");
 
	
 
    while(1)
 
    {
 
		
 
		modules_run(moduleID);
 
 
 
 
		//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///////////////////////////////////////////////////////////////////////////////
 
 
 
 
    }
 
	
 
	return 0;
 
}
 
\ No newline at end of file
slave/slave/slave.cproj
Show inline comments
 
@@ -129,45 +129,51 @@
 
    <Compile Include="lib\i2c.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\i2c.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\inputOutput.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\inputOutput.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\led.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\led.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sensors.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\sensors.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\serial.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\serial.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\spi.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="lib\spi.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="modules.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="modules.h">
 
      <SubType>compile</SubType>
 
    </Compile>
 
    <Compile Include="slave.c">
 
      <SubType>compile</SubType>
 
    </Compile>
 
  </ItemGroup>
 
  <ItemGroup>
 
    <Folder Include="lib" />
 
  </ItemGroup>
 
  <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
 
</Project>
 
\ No newline at end of file
0 comments (0 inline, 0 general)