Funny 'if' routine

Status
Not open for further replies.

Dakta

New Member
I'm writing a small PIC program in MikroC that will receive sequences of bytes from a serial port and look for patterns.

Because the patterns will be 8 bytes long, I'm using an array on a rolling basis, as a buffer - first in, first out style. The serial reception is handled by an interrupt.

I've tested the receiving and sending, if I program it to return all bytes to me, it works perfectly. The problem I'm having is that it is not correctly identifying the patterns that I am looking for.

Here's the 'test' stage code:

Code:
void interrupt() { //Serial Data Received
 
 //put received data into buffer
 //shuffle buffer up array
 if (UART1_Data_Ready() == 1) {
 RX_Buffer[0] = RX_Buffer[1];
 RX_Buffer[1] = RX_Buffer[2];
 RX_Buffer[2] = RX_Buffer[3];
 RX_Buffer[3] = RX_Buffer[4];
 RX_Buffer[4] = RX_Buffer[5];
 RX_Buffer[5] = RX_Buffer[6];
 RX_Buffer[6] = RX_Buffer[7];
 RX_Buffer[7] = UART1_Read();
 }

//check for sequences starting with '0' - return 255 if sequence found
if (RX_Buffer[0] == 0) UART1_Write(255);

 }

All that's supposed to happen there is that it will add receiving data to the array (chucking the oldest out) so we have the most recent 8 received bytes. Then it will return '255' to the computer if the sequence (position 0) is equal to 0.

However, this doesn't work - when zero's are sent, more than 8 in a sequence, the 'if' routine never identifies it.

I added the code to make sure the data was being received correctly:

Code:
UART1_Write(RX_Buffer[0]);

to the bottom of the interrupt routine, it confirms that the element of the array is containing the value I'm looking for.

It's been a while since I used C so I'm a little rusty, but I can't work out what's wrong with my 'if'. Any ideas?
 
Last edited:
I beleive I 'may' have solved the problem.

I beleive the problem is a bug in the simulation tool i'm using as I tried programming the device and did not seem to be able to replicate the error
 
Rather than shuffle the array contents I suggest you use a variable to index into the array (probably 2 variables - one for reading and one for writing) and increment the variable so that at all times you read the array with abc = RX_BUFFER[read_posn] and write to the array with RX_BUFFER[write_posn] = incoming_value. Then increment the _posn variables and set them back to 0 when you get to the end of the array.
 
Thanks for the suggestion, are there technical reasons why that would be superior or would it just be more simple/managable?
 
I was mainly thinking of it being simpler but it may also be faster. Also it is a technique that may be appropriate in other circumstances.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…