1

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?

Felix
  • 89
  • 1
  • 12
  • 1) Insure `LoopVar` is valid before using it in the array - at least as part of this debug. 2) recommend `if (LoopVar >= 39)` 3) Describe "'moving' back and forward" more. 4) Is the second getting correct, but misplaced data. 5) double check baud. – chux - Reinstate Monica Jun 03 '13 at 21:07
  • @chux Thnx for your reply. 1) How do you mean exactly? 2) Changed it 3) I have the idea its receiving some data, but its jumping through the array (trying to find the rights words here) 3) Yeah I think it is misplaced. In the case of a GPS module you can search for strings to sort of sync the message. In this case its an array of 39 variable bytes/values only. 5) Its 9600 on both PICs. – Felix Jun 03 '13 at 21:24
  • Insure LoopVar is valid before using it in the array 'if ((loopVar < (loopVar >= 39)) loopVar = 0;` Trying to insure your `uart_rd1[]` does not write where it should not. 6) Preset `uart_rd1` with, say '!' to know entire array is used. Create counter to see how many are arriving. Sorry, so far only debug tips. Maybe you are receiving what is truly on the comport, it is just unexpected data? – chux - Reinstate Monica Jun 03 '13 at 21:33
  • @chux Yeah I made sure LoopVar is incrementing from 0 through to 39 correctly. I declared `uart_rd[]` as an array of 39, prefilled with !(0x21) in the entire array. To honest I was hoping to do the complete job with one PIC. Now the first is controlling a LED-bar and the second a LCD. The problem I had that even when running on 64Mhz the value writing to the LCD is creating to much of a lag in the LED-bar. Maybe there is a solution for that? – Felix Jun 03 '13 at 21:38
  • Lets try chat in room "USART". – chux - Reinstate Monica Jun 03 '13 at 21:41
  • Need further assistance on this issue? – chux - Reinstate Monica Jun 04 '13 at 16:05
  • @chux I do on the multitasking we discussed yesterday. I'm looking at some pre-emptive RTOS multitasking methodes, but I'm not sure that'll do the trick.. – Felix Jun 04 '13 at 16:27
  • I'll reply in room USART – chux - Reinstate Monica Jun 04 '13 at 16:47

0 Answers0