用51单片机和RC522进行读卡现实,卡的类型能读,卡序列号读不出来,求大神
时间:10-02
整理:3721RD
点击:
#include <reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include <stdio.h>
#include "main.h"
#include "mfrc522.h"
#include "lcd1602.h"
#include <string.h>
unsigned char code data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};
//M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
unsigned char code data2[4] = {0,0,0,0x01};
unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char car_no[4];
unsigned char g_ucTempbuf_o[20];
unsigned char g_ucTempbuf[20];
void delay1(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void semi2string(unsigned char *byte_in,unsigned char *byte_out)
{
unsigned char count,i,j;
j=0;
for(i=0;byte_in[i]!='\0';i=i+1)
{
count=(byte_in[i]&0xf0)>>4;
if (count>0x9)
byte_out[j]=count%9+0x40;
else
byte_out[j]=count+0x30;
j++;
count=byte_in[i]&0x0f;
if (count>0x9)
byte_out[j]=count%9+0x40;
else
byte_out[j]=count+0x30;
j++;
}
byte_out[j]='\0';
}
void main( )
{
unsigned char status,i;
unsigned int temp;
LCD_Init(); //初始化液晶屏
DelayMs(10); //延时用于稳定,可以去掉
LCD_Clear(); //清屏
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
while (1)
{
if( PcdRequest(0x52, &g_ucTempbuf[0] ) != MI_OK )//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
{
if( PcdRequest( 0x52, &g_ucTempbuf[0] ) != MI_OK )//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
{
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
return;
}
}
status = PcdAnticoll(&g_ucTempbuf[2]);//防冲撞,返回卡号
if (status != MI_OK)
{ continue; }
semi2string(g_ucTempbuf,g_ucTempbuf_o);
LCD_Write_String(0,0,g_ucTempbuf_o);//写数字
PcdHalt();
}
#include <stdio.h>
#include "main.h"
#include "mfrc522.h"
#include "lcd1602.h"
#include <string.h>
unsigned char code data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};
//M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
unsigned char code data2[4] = {0,0,0,0x01};
unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char car_no[4];
unsigned char g_ucTempbuf_o[20];
unsigned char g_ucTempbuf[20];
void delay1(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void semi2string(unsigned char *byte_in,unsigned char *byte_out)
{
unsigned char count,i,j;
j=0;
for(i=0;byte_in[i]!='\0';i=i+1)
{
count=(byte_in[i]&0xf0)>>4;
if (count>0x9)
byte_out[j]=count%9+0x40;
else
byte_out[j]=count+0x30;
j++;
count=byte_in[i]&0x0f;
if (count>0x9)
byte_out[j]=count%9+0x40;
else
byte_out[j]=count+0x30;
j++;
}
byte_out[j]='\0';
}
void main( )
{
unsigned char status,i;
unsigned int temp;
LCD_Init(); //初始化液晶屏
DelayMs(10); //延时用于稳定,可以去掉
LCD_Clear(); //清屏
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
while (1)
{
if( PcdRequest(0x52, &g_ucTempbuf[0] ) != MI_OK )//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
{
if( PcdRequest( 0x52, &g_ucTempbuf[0] ) != MI_OK )//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
{
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
return;
}
}
status = PcdAnticoll(&g_ucTempbuf[2]);//防冲撞,返回卡号
if (status != MI_OK)
{ continue; }
semi2string(g_ucTempbuf,g_ucTempbuf_o);
LCD_Write_String(0,0,g_ucTempbuf_o);//写数字
PcdHalt();
}