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:
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:
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?
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: