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 63 insertions and 46 deletions:
0 comments (0 inline, 0 general)
slave/slave/config.h
Show inline comments
 
@@ -10,8 +10,9 @@
 
 #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
slave/slave/lib/led.c
Show inline comments
 
@@ -11,6 +11,11 @@
 
 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	
 
	
 
	 
 
 }
 
slave/slave/lib/sensors.c
Show inline comments
 
@@ -14,22 +14,33 @@
 
#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
 
@@ -9,7 +9,7 @@
 
#ifndef SENSORS_H_
 
#define SENSORS_H_
 
 
uint8_t readSpiTemp(void);
 
int16_t readSpiTemp(void);
 
 
 
slave/slave/lib/spi.c
Show inline comments
 
@@ -11,31 +11,28 @@
 
#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
 
	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()
slave/slave/lib/spi.h
Show inline comments
 
@@ -13,10 +13,10 @@
 
#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)
 
@@ -49,10 +49,7 @@
 

	
 

	
 
// 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);
slave/slave/modules.c
Show inline comments
 
@@ -78,7 +78,7 @@
 
  
 
 void modules_sensors_setup()
 
 {
 
	 setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
 
	 setup_spi();
 
	  
 
 }
 
  
slave/slave/slave.c
Show inline comments
 
@@ -19,7 +19,7 @@
 
#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"
 
@@ -32,10 +32,8 @@
 
void micro_setup()
 
{
 
	// Generic microcontroller config options
 
	
 
	//DDRA = 0xFE;		//PORTA is output //DEBUG
 
 
 
	sei();
 
	DDRB |= (1 << DDRB4);		// Set PB4 to Output for Heater
 
}
 
 
 
@@ -45,15 +43,15 @@ int main(void)
 
	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///////////////////////////////////////////////////////////////////////////////////////
 
	
 
@@ -62,6 +60,7 @@ int main(void)
 
	//PORTA |= (1 << PA1);	//DEBUG///////////////////////////////////////////////////////////////////////////////
 
	//PORTA=0;//DEBUG/////////////////////////////////////////////////////////////////////////////////////////////
 
	temp=0;//DEBUG////////////////////////////////////////////////////////////////////////////////////////////////
 
	//serial0_sendString("Starting\r\n");
 
	
 
    while(1)
 
    {
 
@@ -69,9 +68,10 @@ int main(void)
 
		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//////////////////////////////////////////////////////
 
        
slave/slave/slave.cproj
Show inline comments
 
@@ -150,6 +150,12 @@
 
    <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>
0 comments (0 inline, 0 general)