Blocking code to Non-Blocking help.
Posted: Tue Nov 12, 2019 11:06 am
Hello guys!
I have an RFID manchester code decoding code snippet.
This code is a blocking code right now, it is using delays.
I want it to port to a Non-Blocking code.
How can i achive this? Should i use millis() for track the time between the impulses or should i attach an interrupt every time i need a signal?
The actual code:
I have an RFID manchester code decoding code snippet.
This code is a blocking code right now, it is using delays.
I want it to port to a Non-Blocking code.
How can i achive this? Should i use millis() for track the time between the impulses or should i attach an interrupt every time i need a signal?
The actual code:
Code: Select all
static bool decodeTag(unsigned char *buf)
{
unsigned char i = 0;
unsigned short timeCount;
unsigned char timeOutFlag = 0;
unsigned char row, col;
unsigned char row_parity;
unsigned char col_parity[5];
unsigned char dat;
unsigned char j;
while (1)
{
timeCount = 0;
while (0 == digitalRead(RFID_Intr_Pin))
{
if (timeCount >= 7000)
{
break;
}
else
{
timeCount++;
}
}
if (timeCount >= 600)
{
return false;
}
timeCount = 0;
delayMicroseconds(320);
if (digitalRead(RFID_Intr_Pin))
{
for (i = 0; i < 8; i++)
{
timeCount = 0;
while (1 == digitalRead(RFID_Intr_Pin))
{
if (timeCount == 7000)
{
timeOutFlag = 1;
break;
}
else
{
timeCount++;
}
}
if (timeOutFlag){break;}
else
{
delayMicroseconds(320);
if ( 0 == digitalRead(RFID_Intr_Pin) )
{
break;
}
}
}
if (timeOutFlag)
{
timeOutFlag = 0;
return false;
}
if (i == 8)
{
timeOutFlag = 0;
timeCount = 0;
while (1 == digitalRead(RFID_Intr_Pin))
{
if (timeCount == 7000)
{
timeOutFlag = 1;
break;
}
else
{
timeCount++;
}
if (timeOutFlag)
{
timeOutFlag = 0;
return false;
}
}
col_parity[0] = col_parity[1] = col_parity[2] = col_parity[3] = col_parity[4] = 0;
for (row = 0; row < 11; row++)
{
row_parity = 0;
j = row >> 1;
for (col = 0, row_parity = 0; col < 5; col++)
{
delayMicroseconds(320);
if (digitalRead(RFID_Intr_Pin))
{
dat = 1;
}
else
{
dat = 0;
}
if (col < 4 && row < 10)
{
buf[j] <<= 1;
buf[j] |= dat;
}
row_parity += dat;
col_parity[col] += dat;
timeCount = 0;
while (digitalRead(RFID_Intr_Pin) == dat)
{
if (timeCount == 7000)
{
timeOutFlag = 1;
break;
}
else
{
timeCount++;
}
}
if (timeOutFlag)
{
break;
}
}
if (row < 10)
{
if ((row_parity & 0x01) || timeOutFlag) //Row parity
{
timeOutFlag = 1;
break;
}
}
}
if ( timeOutFlag || (col_parity[0] & 0x01) || (col_parity[1] & 0x01) || (col_parity[2] & 0x01) || (col_parity[3] & 0x01) ) //Column parity
{
timeOutFlag = 0;
return false;
}
else
{
return true;
}
}
return false;
}
}
}