SPI Multimaster mode between two PICs 18f452

Status
Not open for further replies.

absfast123

New Member
I want to setup multi master mode. in which one out of both PICs can transmit data by becomming master on their own need.(as i told in start this my actual need). I made one pic as master and tramitting data to slave pic(working fine). but on Slave side when i change some code for making it slave for the time being. i mean each time initialization of slave mode and closing of SPI module in getSPIData() routine does'nt work. but when i initialize spi module before while(1) just one time and donot close spi module it works fine and continuesly get data from master. Code is given as under.

Code:
#include<spi.h> 
#include <p18cxxx.h> 
#include<delays.h>
      

typedef union {
 struct {
   unsigned char v1;
   unsigned char v2;
 };
 unsigned short val;
} WORD_VAL;
WORD_VAL TotalBytes;
unsigned char strout[200];
void InitSPI(void){
   OpenSPI(SLV_SSON, MODE_00, SMPMID);
   TRISAbits.TRISA5 = 1;    //CS
   TRISCbits.TRISC4 = 1;    //DI
   TRISCbits.TRISC5 = 0;    //DO
   TRISCbits.TRISC3 = 1;    //SCLK
}

void GetSPIData(void)
{
InitSPI();
if(!PORTAbits.RA5)
   {
       TotalBytes.v1=getcSPI();
       TotalBytes.v2=getcSPI();
       getsSPI(strout,200);        
   }
CloseSPI();
}

void main(void)
{ 

unsigned int i=0;

TotalBytes.val=0;

lcd_init();
calc_display(strout);

clearscreen();
while(1)
{
GetSPIData();    
strout[0]='B';
   if(++i>=5000){
       if(TotalBytes.val==536)    
       {
           strout[0]='A';
           
       }    
       strout[1]='\0';    
       calc_display(strout);
       i=0;
   }     

}
}

Thanks in advance,
AB Sami.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…