I have two 18F PICs working next to each other. One is requesting data over UART from another source, their both receiving the (same) incoming data.
The first PIC (18F4450) which is requesting the data works fine, but on the second PIC (18F46K22) the received bytes are 'moving' back and forward within the array I need them in.. Which makes it useless.
This is happening using this code:
loopVar = 0;
do{
while(UART1_Data_Ready() == 1){ // stay here until data buffer full
uart_rd1[loopVar] = UART1_Read(); // read the received data,
loopVar++;
}
}while((loopVar <= 38)); // exit control
Just to be clear, this works fine for the request/receive PIC, but not for the receiving only PIC.
I did some research and I found that maybe a UART interrupt routine could work. So I wrote this:
void interrupt()
{
if (RC1IF_bit) // If interrupt is generated by RCIF
{
uart_rd1[LoopVar] = UART1_Read(); // Read data and store it to array
LoopVar++; // Increment string index
if (LoopVar == 39) // If index = 39,
{
LoopVar = 0; // set it to zero
ready = 1; // Ready for parsing data
}
RC1IF_bit = 0; // Set RCIF to 0
}
}
With this for interrupt init:
GIE_bit = 1; // Enable Global interrupt
RC1IE_bit = 1; // Enable USART Receiver interrupt
PEIE_bit = 1; // Enable Peripheral interrupt
But the different bytes within the array aren't correct at all.
Any ideas what I'm doing wrong on the UART interrupt part? Or maybe a better solution for the UART receiving issue in the first place?