Changeset - 29a47f4a335d
[Not reviewed]
default
0 6 0
kripperger@CL-SEC241-09.cedarville.edu - 13 years ago 2012-11-27 14:55:18
kripperger@CL-SEC241-09.cedarville.edu
spi
6 files changed with 50 insertions and 29 deletions:
0 comments (0 inline, 0 general)
slave/slave/lib/sensors.c
Show inline comments
 
@@ -12,4 +12,24 @@
 
#include "../config.h"
 
#include <util/delay.h>
 
#include "sensors.h"
 
#include "spi.h"
 
 
 
uint8_t readSpiTemp()
 
{
 
	// select TEMP wait 100 microseconds then read four bytes
 
	SELECT_TEMP;
 
	_delay_us(100);
 
	unsigned char one = send_spi(0xFF);
 
	_delay_us(100);
 
	unsigned char two = send_spi(0xFF);
 
	_delay_us(100);
 
	unsigned char three = send_spi(0xFF);
 
	_delay_us(100);
 
	unsigned char four = send_spi(0xFF);
 
	DESELECT_TEMP;
 
	
 
	
 
	//return ((0x1F & one) << 7) | (two >> 1);
 
	return 0;	//DEBUG
 
}
 
\ 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);
 
 
 
slave/slave/lib/spi.c
Show inline comments
 
@@ -13,27 +13,29 @@
 

	
 
void setup_spi(uint8_t mode, int dord, int interrupt, uint8_t clock)
 
{
 
	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
 
	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
 
}
 

	
 
void disable_spi()
 
@@ -53,3 +55,4 @@ uint8_t received_from_spi(uint8_t data)
 
  SPDR0 = data;
 
  return SPDR0;
 
}
 

	
slave/slave/lib/spi.h
Show inline comments
 
@@ -13,11 +13,14 @@
 
#include <avr/io.h>
 

	
 

	
 
#define SPI_SS_PIN PORTA1		//DEBUG 
 
#define SPI_SS_PIN PORTA1
 
#define SPI_SCK_PIN PORTB7
 
#define SPI_MOSI_PIN PORTB5
 
#define SPI_MISO_PIN PORTB6
 

	
 
#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 */
 
@@ -63,6 +66,5 @@ uint8_t send_spi(uint8_t out);
 
uint8_t received_from_spi(uint8_t out);
 

	
 

	
 

	
 
#endif /* SPI_H_ */
 

	
slave/slave/modules.c
Show inline comments
 
@@ -11,7 +11,7 @@
 
 #include "config.h"
 
 #include <util/delay.h>
 
 #include "modules.h"
 
 
 
 #include "lib/spi.h"
 
 
 
 
 
 void modules_setup(uint8_t id)
 
@@ -78,6 +78,7 @@
 
  
 
 void modules_sensors_setup()
 
 {
 
	 setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
 
	  
 
 }
 
  
slave/slave/slave.cproj
Show inline comments
 
@@ -169,10 +169,5 @@
 
  <ItemGroup>
 
    <Folder Include="lib" />
 
  </ItemGroup>
 
  <ItemGroup>
 
    <None Include="lib\SPIreadme.txt">
 
      <SubType>compile</SubType>
 
    </None>
 
  </ItemGroup>
 
  <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
 
</Project>
 
\ No newline at end of file
0 comments (0 inline, 0 general)