
//From the header file that goes with the SPI files
sbit SPI_MOSI  = P2^2;
sbit SPI_MISO  = P2^3;
sbit SPI_CSn  = P2^4;
sbit SPI_SCK  = P2^5;

//The hardware code doesn't work:

void Init_SPI(void)
{
//P2_2	MOSI Push-pull
//P2_3	MISO Input only
//P2_4	SS   Push-pull
//P2_5   SCLK Push-pull
  P2M1 &= 0xCB;
  P2M1 |= 0x08;
  P2M2 &= 0xF7;
  P2M2 |= 0x34;

  P2 &= 0xCB;

  SPCTL = 0x52;     // configure SPI
  ESPI = 0;         // Disable SPI interrupt
  SPSTAT |= 0x80;   // Clear SPIF bit
}

unsigned char Send_Get_SPI(unsigned char Data)
{	
  SPI_CSn=0;       // Select the slave
  SPDAT=Data;      // Send Data
  while (!SPIF) ;  // Wait for transfer to be ready
  SPSTAT |= 0x80;   // Clear SPIF bit
  return SPDAT;    // return new data received
}




//This software procedure works if hardware SPI is disabled:

unsigned char Send_Get_SPI(unsigned char Data)
{
  unsigned char BitCount;
  unsigned char TempResult;
  SPI_SCK=0;
  SPI_CSn=0;
  TempResult=0;
  halWait(10);
  for (BitCount=8;BitCount>0;BitCount--)
  {
  	if (Data&0x80)
	{
		SPI_MOSI=1;
	}
	else
	{
		SPI_MOSI=0;
	}
	halWait(4);
	SPI_SCK=1;
	halWait(3);
	TempResult<<=1;
	TempResult+=(SPI_MISO==1);
	SPI_SCK=0;
	Data<<=1;
  }
  return(TempResult);
}

