标题:
stc89c52rc驱动rc522源程序 实测有效
[打印本页]
作者:
usb123
时间:
2017-9-27 12:54
标题:
stc89c52rc驱动rc522源程序 实测有效
之前自己用这个模块的时候也找了很多了历程来测试,但是都是没有通过,最后得出的结论是spi通信的问题,,自己也懒得去找问题,,所以就搁置了现在把调试通过的程序拿来分享给大家,可以读卡的,其他功能需要自己开发哦
单片机源程序如下:
#include "lpcreg.h"
#include "main.h"
#include "mfrc522.h"
#include <STDIO.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 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;
PcdHalt();
}
}
}
/////////////////////////////////////////////////////////////////////
//系统初始化
/////////////////////////////////////////////////////////////////////
void InitializeSystem()
{
P0M1 = 0x0; P0M2 = 0x0;
P1M1 = 0x0; P1M2 = 0x0;
P3M1 = 0x0; P3M2 = 0xFF;
P0 = 0xFF; P1 = 0xFF; P3 = 0xFF;P2 = 0xFF;
TMOD=0x21; //设T0为方式1,GATE=1;
SCON=0x50;
TH1=0xFD; //波特率为4800bps
TL1=0xFD;
// TH1=0XE0; //9600
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
测试通过程序.zip
(95.54 KB, 下载次数: 170)
2017-9-27 18:33 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
lyj201486
时间:
2018-3-16 12:16
正好需要
作者:
简简单单♂
时间:
2018-6-2 23:18
正好需要,感谢楼主
作者:
25Hzzz
时间:
2018-11-19 17:44
太棒了,希望我能成功
作者:
qwe1818
时间:
2019-12-12 23:33
感谢楼主分享,楼主好人,我终于找到了。
作者:
48262565
时间:
2021-4-22 21:11
想知道 usb转ttl rc522 51引脚的接线方法
作者:
tarchen
时间:
2024-10-1 09:06
正好需要,感谢楼主
作者:
celinelg
时间:
2025-3-4 21:23
看起来不错啊
作者:
celinelg
时间:
2025-3-4 21:24
希望对自己有所启发吧
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1