AnsweredHot!What is the exact use of SSPSTAT.BF in SPI master and slave

Page: 12345.. > >> Showing page 1 of 6
Author
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
2017/11/16 12:35:21 (permalink)
0

What is the exact use of SSPSTAT.BF in SPI master and slave

Hey guys this time no code no complex stuff . just simple question in master mode while writing to a slave is it valid to check if SSPSTAT.BF==1 before i send my byte or SSPSTAT.BF is just used in slave mode or what exactly 
Thank u
#1
qhb
Superb Member
  • Total Posts : 6257
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 12:37:37 (permalink)
+1 (1)
In Master mode, BF is set after a transfer completes.
You should have a single function for transferring data to/from the slave.
It should write the send data to SSPBUF, wait for BF to set, then read SSPBUF (which clears BF again).
 
#2
qhb
Superb Member
  • Total Posts : 6257
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 12:40:07 (permalink)
+1 (1)
Everyone who has trouble with Master mode SPI has tried to not do it as above...
 
 
#3
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 12:43:26 (permalink)
0

BF: Buffer Full Status bit (Receive mode only)
1 = Receive complete, SSPBUF is full
0 = Receive not complete, SSPBUF is empty

 
datasheet 18f4620 so they are wrong?
#4
qhb
Superb Member
  • Total Posts : 6257
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 12:54:18 (permalink)
+1 (1)
The "Receive mode only" comment is almost certainly wrong, as that PIC has the same SPI peripheral as hundreds of other PICs.
The SSPIF flag is set at the same time as the BF flag after a transfer, but you then have to manually clear it if you want to monitor that flag.
#5
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:02:13 (permalink)
0
and also in slave mode what is the operation of SSPSTAT.BF 
#6
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:15:33 (permalink)
0
if ur words are right i should receive 0F then 5 second then many useless byte then again 0F
but i judt receive 0F for just once
so there is something in ur words i think

//######################## SPI Master code PIC 18f6420. 4MHZ clock Crystal oscillator. HS oscillator Mode############################
volatile unsigned short int transmissionOrReceptionFlag=1;
volatile unsigned short int dataByteSend=0;
volatile unsigned short int dataBytereceive=0;
//###########################################################################################
void initializeSPI(void)
{
SSPCON1.SSPEN = 0;
TRISC.B5=0;
TRISC.B3=0;
TRISA.B5=1;
SSPSTAT=0b01000000;
SSPCON1=0b00010010;
INTCON = 0b00000000;
INTCON.PEIE = 1;
INTCON.GIE = 1;
PIE1.SSPIE = 1;
PIR1.SSPIF = 0;
IPR1.SSPIP=1;
SSPCON1.SSPEN = 1;
}
//###########################################################################################
void interrupt()
{
if(PIR1.SSPIF == 1)
{
transmissionOrReceptionFlag=1;
PIR1.SSPIF=0;
}
}
//###########################################################################################
void SPITrasmissionWait()
{
unsigned short int dummy;
while(transmissionOrReceptionFlag==0||SSPSTAT.BF==1)
{
if(SSPSTAT.BF==1){dummy=SSPBUF;}
}
}//###########################################################################################
void SPIReceptionWait()
{
while(transmissionOrReceptionFlag==0||SSPSTAT.BF==0){}
}
//###########################################################################################
void SPIMasterWriteByte(unsigned short int dataByte)
{
dataBytereceive=SSPBUF;
SPITrasmissionWait();
SSPBUF= dataByte;
transmissionOrReceptionFlag=0;
}
//###########################################################################################
void SPIMasterReceiveByte()
{
SPIReceptionWait();
dataByteReceive=SSPBUF ;
transmissionOrReceptionFlag=0;
}
//###########################################################################################
void main()
{
TRISA.B0=0;
initializeSPI();
Delay_ms(500);
PORTA.B0=0;
dataByteSend=0x0F;
while(1)
{ //Delay_ms(2);
SPIMasterWriteByte(dataByteSend);
if(dataByteSend==0x0F){Delay_ms(5000);}
//Delay_ms(100);
dataByteSend++;
if(dataByteSend==0xFF) dataByteSend=0;
}
}

#7
DarioG
farewell.
  • Total Posts : 53177
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: porcodioland
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:19:33 (permalink)
0
Are you using a SS (CS) line?

forget about me, subhumans. adieu

#8
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:20:36 (permalink)
0
yes but ss is used in the slave  only
post edited by ubuntuman - 2017/11/16 13:27:25
#9
DarioG
farewell.
  • Total Posts : 53177
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: porcodioland
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:28:57 (permalink)
0
ok,
because you seem to have only one pin set as input, while they should be 2 or 3...

forget about me, subhumans. adieu

#10
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:32:15 (permalink)
0
in master ss pin input but not connected to anything
scl is output pin
sdi left for the peripheral as say datasheet
sdo is output
what else u want to be input?
#11
DarioG
farewell.
  • Total Posts : 53177
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: porcodioland
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:40:24 (permalink)
0
I assumed you were the slave, hence SCK as input. If you're the master, then you may not be driving CS

forget about me, subhumans. adieu

#12
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:43:36 (permalink)
0
 TRISC.B3=0; 

the sck is output , where is the sck input ?
#13
qhb
Superb Member
  • Total Posts : 6257
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 13:59:38 (permalink)
0
Post #4 says you have a 18f4620 
but your code says 18f6420. 
Which is correct?
 
#14
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:14:56 (permalink)
+1 (1)
sorry for the mistake i wrote in the code .. it is 18f4620
#15
qhb
Superb Member
  • Total Posts : 6257
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:15:05 (permalink)
0
Assuming it is a PIC16F4620, try this code which I htink does what you were trying to do.
 
 

//######################## SPI Master code PIC 18f6420. 4MHZ clock Crystal oscillator. HS oscillator Mode############################
//###########################################################################################
void initializeSPI(void)
{
    SSPCON1.SSPEN = 0;
    TRISC.B5=0; // RC5/SDO output
    TRISC.B3=0; // RC3/SCK output
    TRISC.B4=1; // RC4/SDI input
    SSPSTAT=0b01000000; // SMP=0, CKE=1
    SSPCON1=0b00010010; // SSPEN=0, CKP=1,SSPM=0010(Master Fosc/64)
    PIE1.SSPIE = 0; // disabled
    SSPCON1.SSPEN = 1;
}
//###########################################################################################
unsigned char spi_xfer_byte(unsigned char dat)
{
    SSPBUF = dat; //start transfer
    while (SSPSTAT.BF == 0); //wait until transfer complete
    return SSPBUF; //read received data and return it.
}

//###########################################################################################
void main()
{
    unsigned char dataByteSend;
    unsigned char dataByteRecv;

    TRISA.B0 = 0; //RA0 output for CS signal
    initializeSPI();
    Delay_ms(500);
    PORTA.B0=0; //assert CS
    dataByteSend=0x0F;
    while(1)
    {
        dataByteRecv = spi_xfer_byte(dataByteSend); //send and receive test data
        if(dataByteSend==0x0F)
            Delay_ms(5000);
        dataByteSend++;
        if(dataByteSend==0xFF)
            dataByteSend=0;
    }
}

#16
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:18:41 (permalink)
0
once i remove   ||SSPSTAT.BF==1 from this function it works fine in the way i tell u above . but i want to understand the role of this BF bit

void SPITrasmissionWait()
{
unsigned short int dummy;
while(transmissionOrReceptionFlag==0||SSPSTAT.BF==1)
{
if(SSPSTAT.BF==1){dummy=SSPBUF;}
}
}

 
#17
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:22:31 (permalink)
+1 (1)
i have the working way in sending data from master to slave . i just want to understand why the code in that way as if u say BF is same as the interrupt bit , why doesnt it work and enter in infinite loop of while in the function above .. onces i remove this part it works fine 
 
#18
qhb
Superb Member
  • Total Posts : 6257
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:22:54 (permalink)
+1 (1)
Do NOT try to implement separate send and receive functions. 
That is not how SPI works.
 
 
Examine the code I posted above.
 
 
#19
ubuntuman
Senior Member
  • Total Posts : 162
  • Reward points : 0
  • Joined: 2016/05/02 15:28:40
  • Location: 0
  • Status: offline
Re: What is the exact use of SSPSTAT.BF in SPI master and slave 2017/11/16 14:26:49 (permalink)
0
what should i read if the slave didnt send anything to me 
why should i put all in one function ? what if slave send me a byte without writing to her ? isnt this possible if i will program the slave pic  too 
#20
Page: 12345.. > >> Showing page 1 of 6
Jump to:
© 2017 APG vNext Commercial Version 4.5