View previous topic :: View next topic |
Author |
Message |
Knight_FlyCn
Joined: 10 May 2013 Posts: 16 Location: Việt Nam
|
18F4620 get data RX port |
Posted: Sun May 26, 2013 12:05 pm |
|
|
I read data from RX (RFID tag - Reader: RDM6300) by code:
Code: |
volatile char StartFlag = 0;
volatile char StopFlag = 0;
char array[12];
int i;
#INT_RDA
void interrupt()
{
RCIF = 0;
if (RCREG == 0X02) /* check for start byte */
{
StartFlag = 1;
}
if (StartFlag == 1 && RCREG != 0X03)
{
array[i] = RCREG;/* store received data into an array */
i++;
}
if (RCREG == 0X03) /* 10 byte data is received */
{
array[i] = '\0';
StopFlag = 1;
i = 0;
}
}
|
But don't anything in array[i] . I don't known and who can help me ?
And when I connect PIC with PC. I used code:
Why I used it for RFID don't working _________________ Thinh |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun May 26, 2013 12:49 pm |
|
|
One thing I notice is Start_Flag, once set, always stays 1
You should fetch RCREG into a temp variable for test
You should test for the end byte second, not last. then
you can remove the end byte test from your data
input routine.
Without more to go on... _________________ Google and Forum Search are some of your best tools!!!!
Last edited by dyeatman on Sun May 26, 2013 12:53 pm; edited 1 time in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun May 26, 2013 12:49 pm |
|
|
Sorry, can't help.
Your code is neither complete nor compilable, so it's not possible to copy and paste to test.
Could be allsorts of issues, you don't provide enough information.
I suggest you read and follow the forum guidelines.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun May 26, 2013 1:23 pm |
|
|
Comments:
RCIF=0;
Does nothing. RCIF _cannot be cleared till the character is read_.
The compiler automatically tries to clear RCIF for you when the routine exits, but this too won't work, because of the next problem.
Big problem is that you must _always_ physically read the byte received. Now a test for equality, won't necessarily read the byte. So when 2 is received the interrupt can't be cleared, and the code will loop for ever.
Use:
Code: |
#INT_RDA
void interrupt()
{
int8 rxchar;
rxchar=getc(); //now you have read the char
if (rxchar == 0X02) // check for start byte
{
StartFlag = TRUE;
i=0; //make sure we start at the beginning
}
else
{
if (StartFlag)
{
if (rxchar == 0X03)
{
array[i] = '\0';
StopFlag = TRUE;
StartFlag = FALSE;
i = 0;
}
else
array[i++] = rxchar; // store received data into an array
}
}
}
|
Make the array slightly larger, just in case something unexpected arrives. |
|
|
|