/**********************RC522实验开发板例程************************
* CPU:STC89C52RC
* 晶振: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 <STDIO.H>
#include <string.h>
unsigned char code data1[16] = {0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code data4[16] = {0x01,0x02,0x03,0x04,0x05,0x06,0xff,0x07,0x80,0x69,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code data2[4] = {0,0,0,0x01};
unsigned char code DefaultKey[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char code DefaultKey2[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
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, 04, DefaultKey, g_ucTempbuf);//验证卡片密码
if (status != MI_OK)
{ continue; }
status = PcdWrite(04, data1);//写块
status = PcdWrite(07, data4);//写密码块
status = PcdAuthState(PICC_AUTHENT1A, 08, DefaultKey, g_ucTempbuf);//验证卡片密码
status = PcdWrite(10, data1);//写块
status = PcdWrite(11, data4);//写密码块
/////以上是写空白卡的操作,下面是改写卡的操作,空白卡扇区密码是DefaultKey为FFFFFFFFFFFF可以顺利写入 ,但已经更改密码的卡程序不往下运行了,请大神帮忙改一下.让它既可以写空白卡也可以对写过的卡进行写卡。///
status = PcdAuthState(PICC_AUTHENT1A, 04, DefaultKey2, g_ucTempbuf);//验证卡片密码
if (status != MI_OK)
{ continue; }
status = PcdWrite(04, data1);//写块
status = PcdAuthState(PICC_AUTHENT1A, 10, DefaultKey2, g_ucTempbuf);//验证卡片密码
status = PcdWrite(10, data1);//写块
if (status != MI_OK)
{ continue; }
LED_GREEN = 0;
delay1(1000);
LED_GREEN = 1;
delay1(1000);
/* // {}
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=0xFa; //波特率为4800bps
TL1=0xFa;
TH0=0;
TL0=0;
TR0=1;
ET0=1; //允许T0中断
TR1=1; //开启定时器
TI=1;
EA=1; //开启总中断
ES = 1;
RI = 1;
}
|