|
這是總的程序
#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 (管理員權(quán)限)
//u8 KEYB[6]={6,5,4,3,2,1}; //管理員卡秘鑰B (管理員權(quán)限)
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)//在一個(gè)塊寫數(shù)據(jù)
{
u8 pan=0;
printf("正在對塊%d寫入數(shù)據(jù)..............\r\n",address);
pan=PcdWrite(address,pdata);
if(pan==0x26)
{
printf("寫入成功\r\n");
}
else
{
printf("寫入失敗\r\n");
}
}
void read_kuai(int address)//讀出數(shù)據(jù)塊并打印數(shù)據(jù)到串口
{
u8 pan=0;
char num[10];
//u8 unum[10];
printf("正在對扇區(qū)%d讀取數(shù)據(jù)..............\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數(shù)據(jù)為%s\r\n",num);
}
else
{
printf("讀取失敗\r\n");
}
}
u8 IC_test ( void )
{
char cStr [ 30 ];
//u8 ucArray_ID [ 4 ]; //先后存放IC卡的類型和UID(IC卡序列號)
u8 ucStatusReturn; //返回狀態(tài)
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 ) //防沖撞(當(dāng)有多張卡進(jìn)入讀寫器操作范圍時(shí),防沖突機(jī)制會從其中選擇一張進(jìn)行操作)
{
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(); //延時(shí)函數(shù)初始化
uart_init(115200); //串口初始化為9600
LED_Init(); //初始化與LED連接的硬件接口
//LCD_Init();
RC522_Init (); //RC522模塊所需外設(shè)的初始化配置
printf ( "WF-RC522 Test\r\n" );
PcdReset ();
M500PcdConfigISOType ( 'A' );//設(shè)置工作方式
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);
}
}
以下只是修改密碼那部分的內(nèi)容
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); // 修改的是第二扇區(qū)的密碼,也就是對數(shù)據(jù)塊7進(jìn)行修改,修改密碼的數(shù)組長度為6,里面存儲著A的新密碼,
大佬,那你的意思是不是如果要修改密碼,就定義一個(gè)長度為16的數(shù)組,其中A占前六個(gè),中間4個(gè)為控制字,后6個(gè)是B的新密碼,是不是這樣子呢??如果是的話,那在修改密碼前,進(jìn)行驗(yàn)證的時(shí)候,是不是A和B的原密碼都要驗(yàn)證呢??是先A后B,還是和修改時(shí)一樣,定義長度為16的數(shù)組,一起驗(yàn)證
|
|