這是總的程序
#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的數組,一起驗證
|