# Understanding a PIC FIFO

#### camerart

##### Active Member
Hi,
I'm using a PIC to control a radio module SX1278. This means there will be two FIFOs, one on the PIC and one on the SX.
This is regarding the PIC FIFO:
Am I correct, that a serial sentence enters the PIC via the RX pin, and is stored in the FIFO, till a HSEROUT command is given? How big is the FIFO on an 18LF2520 PIC?

Cheers, Camerart.

#### ericgibbs

##### Well-Known Member
Morning C,
The Receive buffer can hold 2 bytes of incoming data, if a third byte is received an error flag is raised, so the buffer [ fifo] holds two bytes.
E

Last edited:

#### camerart

##### Active Member
Morning E,
If I recall correctly, we've done this before with NMEA sentences (Which of course is one of the uses for this set-up) I can now visualise the HSERIN/OUT Oshonsoft simulations. I think what is puzzling me is that in real life it happens a bit quicker, so a sentence comes into the PIC completely in one string.
Where is the sentence held and how do I send it to the SX1278 at the correct timing?
Thanks.
C.

Last edited:

#### ericgibbs

##### Well-Known Member
hi C,
If you want to hold/buffer an incoming serial data string you would enable the UART's interrupt.
When the first byte of the incoming string appears in the UART buffer it will raise an interrupt.

Loop:
The ISR [Interrupt service subroutine] would read the recv buffer and move the received byte into a software buffer, the program would increment the software buffer address pointer.
If the received byte is LF [0x0a] that is the incoming string terminating byte. EXIT
else
keep reading the UART s RCIF and if set, read the UART recv buffer and move the byte to the software buffer.
Loop.

An alternative method is to use the RCIF to jump to the ISR on every byte, I prefer the former method.
The downside is, if the LF byte never arrives the program is stuck in the ISR loop!!! , You have to use a PIC Timer to check if its time to escape from the ISR.

It is also important that when entering the ISR due a RCIF, you test the OERR [over run error]
If there is an Error you must clear the error flags.

E

#### Pommie

##### Well-Known Member
I use a software fifo to keep the code flowing,
Code:
#define FifoLength 32
unsigned char RXFifoBuffer[FifoLength];
unsigned char *RXFifoStart;
unsigned char *RXFifoEnd;

void InitFifo(void){
RXFifoStart=RXFifoBuffer;
RXFifoEnd=RXFifoBuffer;
}

void PutRXFifo(unsigned char chr){
*RXFifoEnd++=chr;
if(RXFifoEnd==RXFifoBuffer+FifoLength)      //reached end
RXFifoEnd=RXFifoBuffer;                 //yes so wrap
//fifo full so deal with it!!
}
}

unsigned char GetRXFifo(void){
unsigned char chr;
while(RXFifoStart==RXFifoEnd);              //if fifo empty then wait
chr=*RXFifoStart++;
if(RXFifoStart==RXFifoBuffer+FifoLength)    //wrapped?
RXFifoStart=RXFifoBuffer;               //yes
return(chr);
}

unsigned char RXFifoCount(void){
if(RXFifoEnd>=RXFifoStart){
return(RXFifoEnd-RXFifoStart);
}else{
return(FifoLength-(RXFifoStart-RXFifoEnd));
}
}
Then in the interrupt I have,
Code:
    if(RCIF&&RCIE)
PutRXFifo(RCREG);
And in the main code I can do,
Code:
if(RXFifoCount()!=0){
var=GetRXFifo();
}
Just calling GetRXFifo will force it to wait until a character is received.

Mike.

#### camerart

##### Active Member
Hi M,
Thanks for the CODE, but it's written in a foreign language for me. I only speak BASIC, sorry. Also it appears to have a FIFO size where the FIFO is limited to a couple of BYTES.

Hi E,
This FIFO lark isn't an easy one is it
Can you visualise the speed of the other program here: http://www.electro-tech-online.com/...ion-receiving-using-scr-radio-modules.149198/
having the speed to accept an incoming sentence on the fly? This would solve a lot of things. In real life, when connected to 'say' a GPS module, it would send a message at 1 to 5 sentences/sec, so I suppose there would be no need for storage.
C.

#### Pommie

##### Well-Known Member
The fifo in the above code is set at 32 bytes (FifoLength) but can be any length. I actually use a fifo on both receive and transmit and find the transmit one most useful as I can send a long sentence without pausing the code.

Maybe Eric could convert above to basic. Does OBasic have pointers?

Mike.

#### camerart

##### Active Member
The fifo in the above code is set at 32 bytes (FifoLength) but can be any length. I actually use a fifo on both receive and transmit and find the transmit one most useful as I can send a long sentence without pausing the code.

Maybe Eric could convert above to basic. Does OBasic have pointers?

Mike.
Hi M,
Ok, thanks.
I'll finish the program here: http://www.electro-tech-online.com/...ion-receiving-using-scr-radio-modules.149198/ first to the best of my abilities, first, then try to test it and see a FIFO is necessary.

NOTE: When referring to the SX1278 FIFO, I will colour it BLUE, for clarification. Here we are talking about the 18LF2520 FIFO.
Eric, don't convert anything yet
Ok, back to the other thread!
C.

#### ericgibbs

##### Well-Known Member
having the speed to accept an incoming sentence on the fly?
hi,
IIRC you are programming the TX as well the RX ends of the LoRa radio link, so why are the rates so high.??
You can select a lower rate.??
E

#### camerart

##### Active Member
hi,
IIRC you are programming the TX as well the RX ends of the LoRa radio link, so why are the rates so high.??
You can select a lower rate.??
E
View attachment 106777
Hi E,
Well spotted, however, I notice that this is in the FSK/OOK not the LORA section of the DATA sheet. I would have to double check whether this is relevant. You will notice that the LORA sections are in light blue. Having said that, there are some shared sections. A DATA sheet to send you mad
EDIT: I searched, and don't think this is used in LORA MODE.

C.

Last edited: