单片机读取ID卡(EM4100卡)/ 125KHz RFID 曼彻斯特码 读卡程序
uint8_t GetBitValue(uint8_t bitsCnt)
{
return (ManchesterCodeBits[bitsCnt / 8] >> (7 - bitsCnt % 8)) & 0x01;
}
/**
* @brief 数据校验
* @note 此函数在主循环中执行,或放在过滤函数(例如连续多次读到的数据均无错误才认为读到卡)中执行
* @param pBuff 存储卡号的数组
* @retval None
* @author PWH @ ****
* @date 2020
*/
uint8_t GetCardNO(uint8_t * pBuff)
{
uint8_t bitsCnt = 0;
uint8_t i = 0;
uint8_t j = 0;
uint16_t Value = 0;
uint8_t status = CARDFALSE;
if (!CodeBitsRecComplete) return CARDREADING; //有卡时,1秒满13次
//无卡,1秒4 - 6次
status = CARDFALSE;
/* 找 0 1 1111 1111 (1停止位+9引导位) 如果bit63-bit72仍不是,则没必要继续找 */
while (bitsCnt <= 63)
{
Value = 0;
for (j = 0; j <= 9; j++)
{
Value <<= 1;
Value |= GetBitValue(bitsCnt + j);
}
if ( Value == 0x01ff ) //找到 0 1 1111 1111
{
bitsCnt += 10; //跳过停止位和引导位
break;
}
else //没 找到 0 1 1111 1111
{
bitsCnt++;
}
}
/* 找到了 0 1 1111 1111 */
if (bitsCnt != 64)
{ /* 数据行0校验 */
if ( (GetBitValue(bitsCnt) + GetBitValue(bitsCnt + 1) + GetBitValue(bitsCnt + 2) + GetBitValue(bitsCnt + 3)) % 2 == GetBitValue(bitsCnt + 4) )
{ /* 数据行1校验 */
if ( (GetBitValue(bitsCnt + 5) + GetBitValue(bitsCnt + 6) + GetBitValue(bitsCnt + 7) + GetBitValue(bitsCnt + 8)) % 2 == GetBitValue(bitsCnt + 9) )
{ /* 数据行2校验 */
if ( (GetBitValue(bitsCnt + 10) + GetBitValue(bitsCnt + 11) + GetBitValue(bitsCnt + 12) + GetBitValue(bitsCnt + 13)) % 2 == GetBitValue(bitsCnt + 14) )
{ /* 数据行3校验 */
if ( (GetBitValue(bitsCnt + 15) + GetBitValue(bitsCnt + 16) + GetBitValue(bitsCnt + 17) + GetBitValue(bitsCnt + 18)) % 2 == GetBitValue(bitsCnt + 19) )
{ /* 数据行4校验 */
if ( (GetBitValue(bitsCnt + 20) + GetBitValue(bitsCnt + 21) + GetBitValue(bitsCnt + 22) + GetBitValue(bitsCnt + 23)) % 2 == GetBitValue(bitsCnt + 24) )
{ /* 数据行5校验 */
if ( (GetBitValue(bitsCnt + 25) + GetBitValue(bitsCnt + 26) + GetBitValue(bitsCnt + 27) + GetBitValue(bitsCnt + 28)) % 2 == GetBitValue(bitsCnt + 29) )
{ /* 数据行6校验 */
if ( (GetBitValue(bitsCnt + 30) + GetBitValue(bitsCnt + 31) + GetBitValue(bitsCnt + 32) + GetBitValue(bitsCnt + 33)) % 2 == GetBitValue(bitsCnt + 34) )
{ /* 数据行7校验 */
if ( (GetBitValue(bitsCnt + 35) + GetBitValue(bitsCnt + 36) + GetBitValue(bitsCnt + 37) + GetBitValue(bitsCnt + 38)) % 2 == GetBitValue(bitsCnt + 39) )
{ /* 数据行8校验 */
if ( (GetBitValue(bitsCnt + 40) + GetBitValue(bitsCnt + 41) + GetBitValue(bitsCnt + 42) + GetBitValue(bitsCnt + 43)) % 2 == GetBitValue(bitsCnt + 44) )
{ /* 数据行9校验 */
if ( (GetBitValue(bitsCnt + 45) + GetBitValue(bitsCnt + 46) + GetBitValue(bitsCnt + 47) + GetBitValue(bitsCnt + 48)) % 2 == GetBitValue(bitsCnt + 49) )
{ /* 数据列0校验 */
if ( (GetBitValue(bitsCnt) + GetBitValue(bitsCnt + 5) + GetBitValue(bitsCnt + 10) + GetBitValue(bitsCnt + 15) + GetBitValue(bitsCnt + 20) + GetBitValue(bitsCnt + 25) + GetBitValue(bitsCnt + 30) + GetBitValue(bitsCnt + 35) + GetBitValue(bitsCnt + 40) + GetBitValue(bitsCnt + 45)) % 2 == GetBitValue(bitsCnt + 50) )
{ /* 数据列1校验 */
if ( (GetBitValue(bitsCnt + 1) + GetBitValue(bitsCnt + 6) + GetBitValue(bitsCnt + 11) + GetBitValue(bitsCnt + 16) + GetBitValue(bitsCnt + 21) + GetBitValue(bitsCnt + 26) + GetBitValue(bitsCnt + 31) + GetBitValue(bitsCnt + 36) + GetBitValue(bitsCnt + 41) + GetBitValue(bitsCnt + 46)) % 2 == GetBitValue(bitsCnt + 51) )
{ /* 数据列2校验 */
if ( (GetBitValue(bitsCnt + 2) + GetBitValue(bitsCnt + 7) + GetBitValue(bitsCnt + 12) + GetBitValue(bitsCnt + 17) + GetBitValue(bitsCnt + 22) + GetBitValue(bitsCnt + 27) + GetBitValue(bitsCnt + 32) + GetBitValue(bitsCnt + 37) + GetBitValue(bitsCnt + 42) + GetBitValue(bitsCnt + 47)) % 2 == GetBitValue(bitsCnt + 52) )
{ /* 数据列3校验 */
if ( (GetBitValue(bitsCnt + 3) + GetBitValue(bitsCnt + 8) + GetBitValue(bitsCnt + 13) + GetBitValue(bitsCnt + 18) + GetBitValue(bitsCnt + 23) + GetBitValue(bitsCnt + 28) + GetBitValue(bitsCnt + 33) + GetBitValue(bitsCnt + 38) + GetBitValue(bitsCnt + 43) + GetBitValue(bitsCnt + 48)) % 2 == GetBitValue(bitsCnt + 53) )
{ /* 停止位 */
if (GetBitValue(bitsCnt + 54) == 0)
{
status = CARDTRUE;
/* 4字节卡号 */
pBuff[0] = GetBitValue(bitsCnt + 10) << 7 | GetBitValue(bitsCnt + 11) << 6 | GetBitValue(bitsCnt + 12) << 5 | GetBitValue(bitsCnt + 13) << 4 | GetBitValue(bitsCnt + 15) << 3 | GetBitValue(bitsCnt + 16) << 2 | GetBitValue(bitsCnt + 17) << 1 | GetBitValue(bitsCnt + 18);
pBuff[1] = GetBitValue(bitsCnt + 20) << 7 | GetBitValue(bitsCnt + 21) << 6 | GetBitValue(bitsCnt + 22) << 5 | GetBitValue(bitsCnt + 23) << 4 | GetBitValue(bitsCnt + 25) << 3 | GetBitValue(bitsCnt + 26) << 2 | GetBitValue(bitsCnt + 27) << 1 | GetBitValue(bitsCnt + 28);
pBuff[2] = GetBitValue(bitsCnt + 30) << 7 | GetBitValue(bitsCnt + 31) << 6 | GetBitValue(bitsCnt + 32) << 5 | GetBitValue(bitsCnt + 33) << 4 | GetBitValue(bitsCnt + 35) << 3 | GetBitValue(bitsCnt + 36) << 2 | GetBitValue(bitsCnt + 37) << 1 | GetBitValue(bitsCnt + 38);
pBuff[3] = GetBitValue(bitsCnt + 40) << 7 | GetBitValue(bitsCnt + 41) << 6 | GetBitValue(bitsCnt + 42) << 5 | GetBitValue(bitsCnt + 43) << 4 | GetBitValue(bitsCnt + 45) << 3 | GetBitValue(bitsCnt + 46) << 2 | GetBitValue(bitsCnt + 47) << 1 | GetBitValue(bitsCnt + 48);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
for (i = 0; i < 16; i++)
{
ManchesterCodeBits[i] = 0;
}
CodeBitsRecComplete = 0;
return status;
}