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
 
@@ -3,13 +3,33 @@
 
 *
 
 * 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"
 
 
 
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
 
/*
 
 * sensors.h
 
 *
 
 * Created: 11/19/2012 9:24:50 PM
 
 *  Author: kripperger
 
 */ 
 
 
 
#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
 
@@ -4,52 +4,55 @@
 
 *
 
 *
 
 *
 
 *
 
 */
 

	
 

	
 
#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;
 
}
 

	
 
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
 
@@ -4,29 +4,32 @@
 
 *
 
 *
 
 *
 
 */
 

	
 

	
 
#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 */
 

	
 
// 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)
 
@@ -54,15 +57,14 @@ void setup_spi(uint8_t mode,   // timing
 
// 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
 
@@ -2,25 +2,25 @@
 
 * modules.c
 
 *
 
 * Created: 11/9/2012 11:44:22 AM
 
 *  Author: kripperger
 
 */ 
 
 
 #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)
 
	{
 
		case 0:
 
			modules_generic_setup();
 
			break;
 
			
 
		case 1:
 
@@ -69,24 +69,25 @@
 
	 }
 
	  
 
 }
 
 
 
 
 
 void modules_generic_setup()
 
 {
 
	  
 
 }
 
  
 
 void modules_sensors_setup()
 
 {
 
	 setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
 
	  
 
 }
 
  
 
 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
slave/slave/slave.cproj
Show inline comments
 
@@ -160,19 +160,14 @@
 
      <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>
 
  <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)