Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/15/06 08:40
Modified:
  05/15/06 08:53

Read: times


 
#116201 - P89LPC931 SPI problem
Hello,

I use a P89LPC931 in my design with an SPI slave chip. I have connected the MOSI of the 931 to the slave, MISO to MISO and SPICLK with the clock of the slave device. I have connected the SS input/output of the 931 with the CSn of the slave. I want to ignore the SS input level of the 931 to avoid that the 931 goes into slave mode. The SS line is configured as an output to select the slave. I have problems with configuring the SPI. Software SPI works, but I can't get the hardware SPI to work. The format of the SPI transfer should be: MSB first on the output, SCLK low in idle state, Data sampled on rising edge, changed on falling edge.
Can someone help me with getting it to work?
Thanks in advance.

Kind regards,
Mark

//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);
}



List of 2 messages in thread
TopicAuthorDate
P89LPC931 SPI problem            01/01/70 00:00      
   Solved the problem            01/01/70 00:00      

Back to Subject List