interface spi with P89v51rd2

Thread Starter

Darpan747

Joined Feb 26, 2014
8
Hello, P89v51rd2 is an extended version of 8051 which supports spi communication.
I have two controllers.
I have done ss,mosi,miso connections and clock connections.
When i send some data from master to slave it is working correct as it gets displayed on the lcd.
The problem I am facing is when I go for data transfer from slave to master that is using miso line.
Can anyone suggest what changes to be made?
Or which bit to be set or reset to make data transfer from slave to master in that same code?
 

Ian Rogers

Joined Dec 12, 2012
1,136
The whole idea of SPI is the circular buffer, whatever is placed in the serial buffer on the slave will be clocked out whilst the new data is being clocked in...

If your master has binary 00110011 in the serial buffer
And your slave has 11001100 in its serial buffer
After 8 clocks they will be swapped... the master will hold 11001100 and the slave will hold 00110011..

MOSI is master out slave in... and MISO is Master in slave out.. So to answer your question, it's is always done this way...
 

Thread Starter

Darpan747

Joined Feb 26, 2014
8
Hello, I understood the concept you said.
BUt y data transfer from master to slave is working.
When i tried to implement the slave to data transfer, the SPIF is not set in slave, that means my data is not transferred from slave.
This i came to know as i had kept indications at various places in code like displaying on lcd.
Here are my codes:

MASTER CODE:

Rich (BB code):
#include<p89v51rd2.h>
#include<lcd_ron.h>
#include<DELAY.h>


#define ss_pin P1_4	//SS(chip select) pin of the master

//function to setup the master configuration for the SPI communication
void spi_config_master()
{
 ss_pin=0;	//make the SS(chip select) pin logic low because the SS pin of the slave is active low
 SPCR=0X50;	//SPI enable bit=1; MSTR bit=1 (to configure it as master) and frequency for SPI comm. = (F)osc/4
 SPSR=0X00;	//disable the SPI interrupt
 SPDAT=0X00;	//initialize the SPI data register to 0X00
}

void master_receive()				//receive data from slave
{
while(1)
{
if(SPSR==0X80)						// if SPIF bit is set
{
	unsigned char val;
	P3_3=0;							//make led on(active low)
P2=SPDAT;
	val=P2+0x30;

DELAY(500);
	lcd_char(val);					//also display the received data on lcd
	//P3_3=1;
	//DELAY(500);
	SPSR=0X00;						//clear SPIF bit
}
else
{
	P3_3=1;
	DELAY(500);
}
}
}




void main()
{
 //unsigned char m;
	//initialize the port values
 P0=0XFF;
 P1=0xFF;
 P3=0XFF;
 P2=0XFF;
lcd_init();
 spi_config_master();	//setup the master configuration
 //infinite loop 
while(1)
	{
 master_receive();
}
//SPDAT=0x00;

		//end of while loop
}


SLAVE CODE:

Rich (BB code):
#include<p89v51rd2.h>
#include<lcd_ron.h>
#include<DELAY.h>


unsigned char val;
void ini()				//spi slave config
{
	 SPCR=0X40;							//SPI enable bit=1; MSTR bit=0 (to configure it as slave) and frequency for SPI comm. = (F)osc/4
 SPSR=0X00;								//disable the SPI interrupt
	SPDAT=0X00;							//initialize the SPI data register to 0X00
	//SPIF=0;
}


void spi_slave_tx(unsigned char data_given_to_master)
{
	//P1_4=1;
	unsigned char temp;				//temporary variable to store the data to be given to slave
 temp=data_given_to_master;				//store the data to be passed to the master
 SPDAT=temp;

	P3_3=0;
	DELAY(100);
	if(SPIF==1)
{										//wait while the transmission is complete ( the SPIF bit in the SPSR register goes high when the transmission is complete)
 SPSR=0X00;										//clear the SPIF bit 

	P3_3=1;
	DELAY(100);
	
}

}


void main()
{

	//initialize the port values
 P0=0XFF;
 P1=0xFF;
 P3=0XFF;
 P2=0XFF;
lcd_init();
ini();	//setup the slave configuration
 //infinite loop to continuously send/transmit data to master
while(1)
	{
		
  spi_slave_tx(0x08);	//send the character(here the number 8 is transmitted)
  DELAY(5);
		//random delay so that the processes on the slave side are completed and hence the slave data buffer (SPDAT of slave) is not filled with the garbage values 
}
//SPDAT=0x00;

		//end of while loop
}
 
Last edited by a moderator:

Ian Rogers

Joined Dec 12, 2012
1,136
I noticed you are calling master_recieve() extremely quickly... You might want a delay in there to slow the code down a bit.

I can't test your code.. I use ISIS to sim but ISIS doesn't support SPI on that model!!
 
Top