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
 
@@ -7,14 +7,15 @@
 
 
 
 #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
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
 
@@ -11,25 +11,36 @@
 
#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
 
@@ -6,11 +6,11 @@
 
 */ 
 
 
 
#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
 
@@ -8,37 +8,34 @@
 
 */
 

	
 

	
 
#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;
 
}
slave/slave/lib/spi.h
Show inline comments
 
@@ -10,16 +10,16 @@
 
#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
 
@@ -46,16 +46,13 @@
 
#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);
slave/slave/modules.c
Show inline comments
 
@@ -75,14 +75,14 @@
 
 {
 
	  
 
 }
 
  
 
 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
slave/slave/slave.c
Show inline comments
 
@@ -16,65 +16,65 @@
 
#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);
slave/slave/slave.cproj
Show inline comments
 
@@ -147,12 +147,18 @@
 
    <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>
0 comments (0 inline, 0 general)