MRC522(1):卡片ID号的读写
基本步骤:
一、寻卡
二、防冲突
三、选择卡
四、验证扇区密码(每个扇区都有密匙A和密匙B,验证正确才能对该扇区的某块进行读写)
五、读/写
*此篇只含有前两步
代码部分
#include "public.h"
#include "bmp.h"
sbit beep = P1^3;//定义蜂鸣器
unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char code ID_1card[4] = {0x0A,0x3F,0x26,0x28}; //蓝小
unsigned char code ID_2card[4] = {0xF4,0xEF,0x48,0x2A}; //白大
void main()
{
//unsigned char status; // status用于获取当前RC522状态
Uart1Init();EA = 1;ES = 1;
RC522_Reset(); // RC522初始化
OLED_Init(); //OLED显示屏初始化
OLED_Clear(); //清屏函数
OLED_Fill(0xff);delay_1ms(1000);OLED_Fill(0x00);delay_1ms(1000);
OLED_ShowString(0,0,"ID_Address:",16);
P1M1=0x00;P1M0=0x08;beep = 0;
while (1)
{
if(PcdRequest(PICC_REQIDL,Get_Sbuff)!=MI_OK)// 寻卡
{
if(PcdRequest(PICC_REQIDL,Get_Sbuff) == MI_OK)// 防冲撞
{
if(PcdAnticoll(Get_Sbuff)==MI_OK)
{
beep = 1;delay_1ms(300);beep = 0;//蜂鸣器鸣响30ms
Uart1_Byte(Get_Sbuff[0]);//通过串口发送ID 发送前四个字节
Uart1_Byte(Get_Sbuff[1]);
Uart1_Byte(Get_Sbuff[2]);
Uart1_Byte(Get_Sbuff[3]);
OLED_ShowCard();
}
PcdHalt(); //休眠卡
}
}
}
}
OELD显示ID和串口显示ID
oled显示的是10进制的ID序列号,串口传输的是16进制的ID序列号