1

I'm working in the communication of a PIC18F4550 and the PC with a pair of xBEE S2C. And I am using xc8 to compile the code.

I send some characters to the PIC from the PC with an Xbee then I send a '/r', and the PIC has to return me the characters that I sent. It works for 9 iterations, then it crashes. The image shows the
Serial Console (red characters are the response of the PIC).

I´ve tried resetting the EUSART but this doesn´t seem to work. Always fails at the 9th iteration. I´ve read some posts of OERR and I tried a lot of things but nothing has solved my problem.

EDIT: ***NOTE: This Error presents if the transmit interval of the package is less than 1500 ms. And I need to transmit at least every 300ms.

Someone has an idea of what could it be? Thanks

 #define _XTAL_FREQ 8000000
 volatile char bufferRx[60];
 volatile char bufferTx[60];
 volatile char dum;
 int RxFlag,ContRx, ContTx;

void interrupt isr()
{   
  if(RCSTAbits.OERR)
    {
      RCSTAbits.CREN = 0;
      RCSTAbits.CREN = 1;         
    }
x = RCREG;
if(x== 13)
  {    
   bufferRx[ContRx] = x;
   RxFlag=1;
  }
 else
 {
  bufferRx[ContRx] = x;
  }
 ContRx++;
 }
void main(void)
{
//////////////////////////////////////////////////////////////////
//CONFIGURACIONES

//OSCILLATOR
 OSCCONbits.IRCF= 0b111;
 OSCCONbits.SCS=0b10;
//PORTS    
 PORTB = 0;
 TRISB=1;
 TRISC=0b10000000;
//INTERRUPTIONS
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
PIE1bits.RCIE=1;
PIE1bits.TXIE=0;
PIR1bits.RCIF=0;
//RCSTA  TXSTA
RCSTAbits.SPEN=1;
RCSTAbits.RX9=0;
RCSTAbits.CREN=1;
TXSTAbits.BRGH=0;
TXSTAbits.SYNC=0;
TXSTAbits.TXEN = 1;
TXSTAbits.TX9=0; 
//BAUDRATE BAUDCON
BAUDCONbits.ABDEN = 0;
BAUDCONbits.WUE = 0;
BAUDCONbits.TXCKP = 0;
BAUDCONbits.RXCKP = 0;
BAUDCONbits.BRG16=0;
SPBRG=51;
//////////////////////////////////////////////////////////////////
while(1)
{ 
    while(RCSTAbits.FERR)
    {
     dum = RCREG;       
    }
    if(RCSTAbits.OERR)
    {
      RCSTAbits.CREN = 0;
      RCSTAbits.CREN = 1;         
    }
    ContTx=0;
    if(RxFlag==1)
    {           
          for(int x=0;x<ContRx;x++)
          {
        bufferTx[x] = bufferRx[x];
        TXREG=bufferTx[x]; 
         while(TXSTAbits.TRMT==0);
         {          
         __delay_ms(1); 
              bufferTx[x]= 00;
              bufferRx[x]= 00;
         }             
        ContTx++;
          }  
          RxFlag=0;
          ContRx=0;  
    }        
}       
}
D.Martinez
  • 11
  • 2

1 Answers1

2

You are not using the "volatile" keyword anywhere although you are modifying some globals inside the ISR and using them outside.

hafeez
  • 159
  • 1
  • 6
  • Thanks for answering, do you mean to add the keyword to all the vars that are modified in the isr? volatile int ContRx,ContTx,RxFlag; volatile char x; volatile char buffer[60]; volatile char dum; – D.Martinez Feb 12 '18 at 18:49
  • Adding for ContRx,& RxFlag should be enough. As those are the ones modified inside the ISR and then based on the value of RxFlag the response decision is made in the main function. What I suspect is that something is happening to the RxFlag at the 9th iteration and the response is not sent. – hafeez Feb 12 '18 at 19:35
  • I think it is important to mention that this problem of the 9th iteration presents if the transmit interval of the packet that i send from the PC is less than 1500ms . But I need to transmit at least every 300ms. – D.Martinez Feb 12 '18 at 21:03