这是总的程序
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "rc522_function.h"
#include "rc522_config.h"
//u8 KEYA[6]={8,8,8,8,8,8}; //管理员卡秘钥A (管理员权限)
//u8 KEYB[6]={6,5,4,3,2,1}; //管理员卡秘钥B (管理员权限)
u8 KEYA[6]={0xff,0xff,0xff,0xff,0xff,0xff};
u8 NEWKEYA[6]={1,2,3,4,5,6};
u8 NEWKEYB[6]={0x11,0x22,0x33,0x44,0x55,0x66};
u8 NEW[6]={0x66,0x55,0x44,0x33,0x22,0x11};
u8 KEYB[6]={0xff,0xff,0xff,0xff,0xff,0xff};
u8 writedata[6]={0x11,0x26,0x11,0x28};
u8 ucArray_ID [ 4 ];
u8 pdataread[10];
int KEYA_in(int block)//选定卡片并输出密码A,///输入卡片块地址(0-63)
{
u8 pan,bat,cad;
PcdSelect(ucArray_ID);//选定卡片
pan=0;
printf("正在输入密码..............\r\n");
pan=PcdAuthState(0x60, block ,NEWKEYB,ucArray_ID);//输入密码
if(pan==0x26)
{
printf("密码正确\r\n");
//PcdWrite(block+1,NEWKEYB); return 1;
}
else
{
printf("密码错误\r\n");
return 0;
}
}
void KEYB_in(int block)//选定卡片并输出密码A,///输入卡片块地址(0-63)
{
int t=0;
u8 pan;
PcdSelect(ucArray_ID);//选定卡片
t=0;
pan=0;
printf("正在输入密码..............\r\n");
while(t<5&&pan!=0x26)
{
t++;
pan=PcdAuthState(0x61, block ,NEWKEYB,ucArray_ID);//输入密码
}
if(pan==0x26)
{
printf("SUCCESS\r\n");
//PcdWrite(block+1,NEWKEYB);
}
else
{
printf("FAIL\r\n");
}
}
void write_kuai(int address,u8 * pdata)//在一个块写数据
{
u8 pan=0;
printf("正在对块%d写入数据..............\r\n",address);
pan=PcdWrite(address,pdata);
if(pan==0x26)
{
printf("写入成功\r\n");
}
else
{
printf("写入失败\r\n");
}
}
void read_kuai(int address)//读出数据块并打印数据到串口
{
u8 pan=0;
char num[10];
//u8 unum[10];
printf("正在对扇区%d读取数据..............\r\n",address);
pan=PcdRead(address,pdataread);
sprintf (num,"%2X%2X%2X%2X",pdataread [ 0 ],pdataread [ 1 ],pdataread [ 2 ],pdataread [ 3 ]);
//unum[0]=num[0];unum[1]=num[1];unum[2]=num[2];unum[3]=num[3];unum[4]=num[4];unum[5]=num[5];unum[6]=num[6];unum[7]=num[7];
if(pan==0x26)
{
printf("读取成功\r\n数据为%s\r\n",num);
}
else
{
printf("读取失败\r\n");
}
}
u8 IC_test ( void )
{
char cStr [ 30 ];
//u8 ucArray_ID [ 4 ]; //先后存放IC卡的类型和UID(IC卡序列号)
u8 ucStatusReturn; //返回状态
static u8 ucLineCount = 0;
while ( 1 )
{
if( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK ) //寻卡
{
ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ); //若失败再次寻卡
}
if ( ucStatusReturn == MI_OK )
{
if ( PcdAnticoll ( ucArray_ID ) == MI_OK ) //防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)
{
sprintf ( cStr, "The Card ID is: %02X%02X%02X%02X", ucArray_ID [ 0 ], ucArray_ID [ 1 ], ucArray_ID [ 2 ], ucArray_ID [ 3 ] );
printf ( "%s\r\n",cStr );
ucLineCount ++;
if ( ucLineCount == 17 ) ucLineCount = 0;
return 1;
}
}
}
}
int main(void)
{
u8 flag=0;
delay_init(); //延时函数初始化
uart_init(115200); //串口初始化为9600
LED_Init(); //初始化与LED连接的硬件接口
//LCD_Init();
RC522_Init (); //RC522模块所需外设的初始化配置
printf ( "WF-RC522 Test\r\n" );
PcdReset ();
M500PcdConfigISOType ( 'A' );//设置工作方式
while(1)
{
flag=IC_test ();
if(flag==1)//IC卡检测,返回UID
{
flag=0;
KEYA_in(26);
//KEYB_in(26);
// write_kuai(1,writedata);
// read_kuai(1);
}
LED0=!LED0;
delay_ms(1000);
}
}
以下只是修改密码那部分的内容
u8 KEYA[6]={0xff,0xff,0xff,0xff,0xff,0xff};
u8 NEWKEYA[6]={1,2,3,4,5,6};
u8 NEWKEYB[6]={0x11,0x22,0x33,0x44,0x55,0x66};
u8 KEYB[6]={0xff,0xff,0xff,0xff,0xff,0xff};
int KEYA_in(int block)//选定卡片并输出密码A,///输入卡片块地址(0-63)
{
u8 pan,bat,cad;
PcdSelect(ucArray_ID);//选定卡片
pan=0;
printf("正在输入密码..............\r\n");
pan=PcdAuthState(0x60, block ,NEWKEYB,ucArray_ID);//输入密码
if(pan==0x26)
{
printf("密码正确\r\n");
//PcdWrite(block+1,NEWKEYB); return 1;
}
else
{
printf("密码错误\r\n");
return 0;
}
}
KEYA_in(6); // 修改的是第二扇区的密码,也就是对数据块7进行修改,修改密码的数组长度为6,里面存储着A的新密码,
大佬,那你的意思是不是如果要修改密码,就定义一个长度为16的数组,其中A占前六个,中间4个为控制字,后6个是B的新密码,是不是这样子呢??如果是的话,那在修改密码前,进行验证的时候,是不是A和B的原密码都要验证呢??是先A后B,还是和修改时一样,定义长度为16的数组,一起验证
|