单片机读取ID卡(EM4100卡)/ 125KHz RFID 曼彻斯特码 读卡程序


【本文发布于https://blog.youkuaiyun.com/Stack_/article/details/116952678,未经许可不得转载,转载须注明出处】


一、读卡电路原理图

在这里插入图片描述
这个图是别人画的,不是太懂原理。


MCU产生125K 方波,经过74HC04和4.7欧电阻后到达铜质线圈,ID卡靠近线圈,LM358会输出“曼彻斯特编码”的数据。MCU采集后校验通过得到卡号。


二、曼彻斯特码

【参考资料】


曼彻斯特编码的ID卡每次输出64bit数据,其载波编码为曼彻斯特码。

在这里插入图片描述

曼彻斯特码调制方式下,EM4100卡片每传送一位数据的时间是64个振荡周期。


125KHZ载波时,卡片传送一bit数据理论频率为125KHz / 64 = 1.953125KHz。


得一个周期为1 000 000us / 1.953125KHz = 512us。


曼彻斯特码一个完整的数据跳变为一个周期(512us),


存在空跳则半个跳变为半个周期(256us)。


假设MCU的PC5产生125K方波,PD2作检波。PD2配置为外部中断输入,且中断触发方式为上升沿&下降沿。

  1. 如果捕获到一个边沿,距离上一个边沿时间为512us,则读取此时的PD2电平
低电平此bit为1
高电平此bit为0
  1. 如果捕获到一个边沿,距离上一个边沿时间为256us,则此次不作判断;
    再次捕获到一个边沿时,判断:
上次bit = 1此次bit为1
上次bit = 0此次bit为0
或者
PD2 = 0此次bit为1
PD2 = 1此次bit为0

三、接收解析程序

以STM8S103为例,主频16MHz

/**
  * @brief  定时器
  * @note   
  * @param  None
  * @retval None
  * @author PWH @ 优快云 Tyrion.Mon
  * @date   2020
  */
void Tim1_Init(void)
{
	Tim1_DeInit();
	TIM1_TimeBaseInit(1, TIM1_COUNTERMODE_UP, 9999, 0); //2分频,计数模式-向上,溢出值,重载值
	TIM1_ARRPreloadConfig(ENABLE);//使能数值自动重装载功能
	TIM1_Cmd(ENABLE);
}

根据此配置得

计数器频率16MHz / 2 = 8MHz
计数周期1000000us / 8MHz = 0.125us
一个周期(512us)计数值为512us / 0.125 = 4096
半个周期(256us)计数值为256us / 0.125 = 2048



因为一次传输64bit,所以一次接收128bit数据,其中肯定包含完整的一组数据。
用一个数组存储采集的数据

uint8_t ManchesterCodeBits[16] = {0}; //125KhzID卡 曼彻斯特码(Manchester code)  一次输出64位数据信息  ,采集两组 64bit*2 = 128bit  128/8=16Bytes
/**
  * @brief  外部中断配置
  * @note   
  * @param  None
  * @retval None
  * @author PWH @ 优快云 Tyrion.Mon
  * @date   2020
  */
void Exti_init(void)
{
    GPIO_Init(GPIOD,GPIO_PIN_2,GPIO_MODE_IN_PU_IT);
    EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD, EXTI_SENSITIVITY_RISE_FALL);
}
uint8_t CodeBitsRecComplete = 0;	//为0接收,为1校验
/**
  * @brief  PD中断服务函数
  * @note   
  * @param  None
  * @retval None
  * @author PWH  @ 优快云 Tyrion.Mon
  * @date   2020
  */
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
{
	static uint8_t BitsCnt = 0;   //接收到的bit计数,接收完128bit后暂停接收,进入校验步骤
	uint8_t Value = 0;
	uint16_t temp = 0;
	static uint8_t flag = 0;
	if (!CodeBitsRecComplete)
	{
		temp |= TIM1->CNTRH;
		temp <<= 8;
		temp |= TIM1->CNTRL;    //temp为计数器当前值
		if (temp > 3000 && temp < 5000) // 1个周期计数值约4096
		{
			TIM1->CR1 &= 0xFE;    //关定时器
			TIM1->CNTRH = 0;      //清零计数器
			TIM1->CNTRL = 0;
			TIM1->CR1 |= 0x01;    //开定时器
			if (flag)             //上一个跳变为空跳
			{
				Value = ((ManchesterCodeBits[(BitsCnt - 1) / 8] >> (7 - (BitsCnt - 1) % 8)) & 0x01) ? 1 : 0;    //上一bit为1则为1,否则为0
			}
			else
				Value = (GPIOD->IDR) & 0x04 ? 0 : 1;
			flag = 0;
			ManchesterCodeBits[BitsCnt / 8] |= (Value << (7 - BitsCnt % 8));
			BitsCnt++;
			if (BitsCnt > 127)
			{
				CodeBitsRecComplete = 1;
				BitsCnt = 0;
			}
		}
		else if (temp > 1000 && temp < 3000) // 1/2个周期计数值约2048
		{
			flag = 1;     //检测到一个空跳
		}
	}
}

四、EM4100卡片64位数据存储格式和发送格式

在这里插入图片描述

引导码1 1111 1111 (9bit)
D00 - D13版本号之类的(8bit)
D20 - D93卡号(32bit)
Px行校验 (10bit)
PCx列校验 (4bit)
S0停止位 (1bit)


传输顺序

bit 0 - bit 8引导位 1 1111 1111 (9bit)
bit 9 - bit 13D00 D01 D02 D03 P0
bit 14 - bit 18D10 D11 D12 D13 P1
bit 54 - bit 58D90 D91 D92 D93 P9
bit 59 - bit 62PC0 PC1 PC2 PC3
bit 63S0


五、数据校验程序

uint8_t GetBitValue(uint8_t bitsCnt)
{
	return (ManchesterCodeBits[bitsCnt / 8] >> (7 - bitsCnt % 8)) & 0x01;
}

/**
  * @brief  数据校验
  * @note   此函数在主循环中执行,或放在过滤函数(例如连续多次读到的数据均无错误才认为读到卡)中执行
  * @param  pBuff 存储卡号的数组
  * @retval None
  * @author PWH   @ 优快云 Tyrion.Mon
  * @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;
}


在理解校验原理的基础上用循环结构对校验进行简写

if (bitsCnt != 64)
{
    status = CARDTRUE;
    /* 对10组行数据进行校验 */
    for (i = bitsCnt; i <= (bitsCnt + 45); i += 5)
    {
        Value = 0;
        for (j = 0; j <= 3; j++)
        {
            Value +=  GetBitValue(i + j);   //每行4bit数据累加
        }
        if ( Value % 2 != GetBitValue(i + 4) )
        {
            status = CARDFALSE;
            break;
        }
    }
    if (status == CARDTRUE)
    {
        /* 对4组列数据进行校验 */
        for (i = bitsCnt; i <= (bitsCnt + 3); i++)
        {
            Value = 0;
            for (j = 0; j <= 45; j += 5)
            {
                Value +=  GetBitValue(i + j);   //每列10bit数据累加
            }
            if ( Value % 2 != GetBitValue(i + 50) )
            {
                status = CARDFALSE;
                break;
            }
        }
    }
    if (status == CARDTRUE)
    {
        /* 停止位 */
        if (GetBitValue(bitsCnt + 54) == 0)
        {
            status = CARDTRUE;
            /* 4字节卡号 */
            for (i = 0; i < 4; i++)
            {
                pBuff[i] = 0;
                for (j = 0; j <= 8; j++)
                {
                    if (j != 4)
                    {
                        pBuff[i] <<= 1;
                        pBuff[i] |= GetBitValue(bitsCnt + (i + 1) * 10 + j);
                    }
                }
            }
        }
        else
            status = CARDFALSE;
    }
}



这个只是简单的实现,识别速度慢,偶有识别错误。可以通过优化大大提高识别速度以及达到接近百分百的准确率。
125kHZ低频读卡器功能概述: 本资源中包含读卡器原理图及PCB文件、读卡器源程序和电脑端上位机源程序读卡器可以直接制作使用,可以使用附件中的上位机与读卡器进行通信,控制读卡器对T5557类片进行读写操作。对T5557片的配置必须为(RF/32,曼侧斯特编),这也是国内T5557的常用配置。要对其他配置的T5557片或其他低频片(EM4305类和EMID片)进行读写,硬件上是支持的,只需根据片文档修改读卡器源程序,下载更新便可。为方便程序更新,硬件电路上已经实现自动下载,使用串口下载程序时无需手动设置BOOT0和BOOT1。 应用场景: 低频RFID主要用在短距离、低成本的应用中,如门禁控制、校园、煤气表、水表等。 在相关领域的开发中,可以使用该读卡器作为前台管理的设备,对客户进行管理。也可以将读卡器改成终端设备,如RFID门锁上的读卡器,用来验证片信息并控制门锁;或煤气表、水表上的读卡器,根据片内的余量信息控制煤气和水的供应。另外,也可以作为学习曼彻斯特的学习工具。 设计思路: 使用STM32F103R8T6输出125kHZ的PWM信号作为载波信号,将信号输入到功率放大电路,在线圈上产生125kHZ的正弦信号。T5557片从125kHZ信号中获取能量和控制命令,并根据命令将要传回的信息使用曼彻斯特对载波信号进行调制。读卡器端的检波电路和滤波放大电路将被调制信号提取处理成单片机能识别的数字信号,输入到STM32F103R8T6的输入捕获功能引脚,再通过STM32F103R8T6将捕获的信号进行曼彻斯特,最后将解信息根据自己设定的通讯协议进行数据打包,发送到电脑上位机。 系统框图: 硬件平台: 1.主控制器:STM32F103R8T6 2.USB转TTL:CH340G 3.模拟电路:LM358 AMS1117-3.3V 125kHZ谐振线圈(345UH线圈搭配4700PF电容)谐振频率在线计算工具 4.蜂鸣器: 3V供电 软件平台: 开发工具:Keil uVision5 上位机编写工具:Microsoft Visual C# 学习版 成本估算: 芯片询价和在线购买链接https://www.bom2buy.com/元器件估算价格约为30元 项目进度: 1.能对AT5557/55567/5577片(配置为RF/32,曼彻斯特)进行整页读,密读和密写操作; 2.有相应的上位机,方便读写指令的发送和数据解读; 3.上位机与读卡器之间通讯加入了数据校验,提高了通讯的可靠性。 未来更新: 1.加入EM4305类和EMID片的读写功能; 2.加上触摸显示屏和电池,做成便携版 3.做成袖珍版,加入与手机通讯的接口(OTG或蓝牙)
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值