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
 
@@ -9,7 +9,27 @@
 
#include <inttypes.h>
 
#include <avr/io.h>
 
#include <avr/interrupt.h>
 
#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
 
@@ -6,11 +6,11 @@
 
 */ 
 
 
 
#ifndef SENSORS_H_
 
#define SENSORS_H_
 
 
 
uint8_t readSpiTemp(void);
 
 
 
 
#endif /* SENSORS_H_ */
 
\ No newline at end of file
slave/slave/lib/spi.c
Show inline comments
 
@@ -10,33 +10,35 @@
 

	
 
#include "spi.h"
 

	
 

	
 
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()
 
{
 
  SPCR0 = 0;
 
}
 
@@ -50,6 +52,7 @@ uint8_t send_spi(uint8_t out)
 

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

	
slave/slave/lib/spi.h
Show inline comments
 
@@ -10,17 +10,20 @@
 
#ifndef SPI_H_
 
#define SPI_H_
 

	
 
#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 */
 
#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 */
 
@@ -60,9 +63,8 @@ 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
 
@@ -8,13 +8,13 @@
 
 #include <inttypes.h>
 
 #include <avr/io.h>
 
 #include <avr/interrupt.h>
 
 #include "config.h"
 
 #include <util/delay.h>
 
 #include "modules.h"
 
 
 
 #include "lib/spi.h"
 
 
 
 
 
 void modules_setup(uint8_t id)
 
 {
 
 
	switch(id)
 
@@ -75,12 +75,13 @@
 
 {
 
	  
 
 }
 
  
 
 void modules_sensors_setup()
 
 {
 
	 setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
 
	  
 
 }
 
  
 
 void modules_geiger_setup()
 
 {
 
	// Pin setup
slave/slave/slave.cproj
Show inline comments
 
@@ -166,13 +166,8 @@
 
      <SubType>compile</SubType>
 
    </Compile>
 
  </ItemGroup>
 
  <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)