MFRC522电路原理图:
天线部分:
单片机+MFRC522测试源程序如下:
/**********************RC522实验开发板例程************************
* 晶振:11.0592MHZ
******************************************************************/
//实验目的:
/*-----------------------------------------------
名称:MF RC522
内容:通过标准程序静态显示字符
引脚定义如下:
sbit MF522_NSS = P1^7; //SDA
sbit MF522_SCK = P1^6;
sbit MF522_SI = P1^5;
sbit MF522_SO = P1^4;
sbit MF522_RST = P1^3;
//蜂鸣器OR指示灯
sbit LED_GREEN = P2^3;
------------------------------------------------*/
#include "lpcreg.h"
#include "main.h"
#include "mfrc522.h"
#include #include 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 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 main( ) { unsigned char status,i; unsigned int temp; InitializeSystem( ); PcdReset(); PcdAntennaOff(); PcdAntennaOn(); while ( 1 ) { status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡 if (status != MI_OK) { InitializeSystem( ); PcdReset(); PcdAntennaOff(); PcdAntennaOn(); continue; } printf("卡的类型:"); for(i=0;i<2;i++) { temp=g_ucTempbuf[i]; printf("%X",temp); } status = PcdAnticoll(g_ucTempbuf);//防冲撞 if (status != MI_OK) { continue; } ////////以下为超级终端打印出的内容//////////////////////// printf("卡序列号:"); //超级终端显示, for(i=0;i<4;i++) { temp=g_ucTempbuf[i]; printf("%X",temp); } /////////////////////////////////////////////////////////// status = PcdSelect(g_ucTempbuf);//选定卡片 if (status != MI_OK) { continue; } status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码 if (status != MI_OK) { continue; } status = PcdWrite(1, data1);//写块 if (status != MI_OK) { continue; } while(1) { status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡 if (status != MI_OK) { InitializeSystem( ); PcdReset(); PcdAntennaOff(); PcdAntennaOn(); continue; } status = PcdAnticoll(g_ucTempbuf);//防冲撞 if (status != MI_OK) { continue; } status = PcdSelect(g_ucTempbuf);//选定卡片 if (status != MI_OK) { continue; } status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码 if (status != MI_OK) { continue; } status = PcdValue(PICC_DECREMENT,1,data2);//扣款 if (status != MI_OK) { continue; } status = PcdBakValue(1, 2);//块备份 if (status != MI_OK) { continue; } status = PcdRead(2, g_ucTempbuf);//读块 if (status != MI_OK) { continue; } printf("卡读块:"); //超级终端显示, for(i=0;i<16;i++) { temp=g_ucTempbuf[i]; printf("%X",temp); } printf("n"); LED_GREEN = 0; delay1(100); LED_GREEN = 1; delay1(100); LED_GREEN = 0; delay1(200); LED_GREEN = 1;
上一篇:数码管显示时钟可设置断电记忆
下一篇:简单做的基于51单片机的数码管时钟
推荐阅读最新更新时间:2024-11-07 16:54