2020-12-10 21:17:19 +00:00
|
|
|
|
#include "../src/rc522.h"
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include "../lib/usart/src/usart.h"
|
|
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
|
|
#define uchar unsigned char
|
|
|
|
|
#define uint unsigned int
|
|
|
|
|
#define MAX_LEN 16
|
|
|
|
|
const int chipSelectPin = 10;
|
|
|
|
|
const int NRSTPD = 5;
|
|
|
|
|
|
|
|
|
|
uchar serNum[5];
|
|
|
|
|
uchar writeDate[16] ={'T', 'e', 'n', 'g', ' ', 'B', 'o', 0, 0, 0, 0, 0, 0, 0, 0,0};
|
|
|
|
|
|
2020-12-11 02:57:10 +00:00
|
|
|
|
uchar sectorKeyA[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
2020-12-10 21:17:19 +00:00
|
|
|
|
int main() {
|
|
|
|
|
usart::init(1000000);
|
|
|
|
|
printf("Hello");
|
2020-12-11 02:57:10 +00:00
|
|
|
|
PORTC |= (1<<PC6)|(1<<PC7);
|
2020-12-10 21:17:19 +00:00
|
|
|
|
while(1) {
|
2020-12-11 02:57:10 +00:00
|
|
|
|
MFRC522_Init();
|
2020-12-10 21:17:19 +00:00
|
|
|
|
uchar i;
|
|
|
|
|
uchar status;
|
2020-12-11 02:57:10 +00:00
|
|
|
|
uchar str[MAX_LEN];
|
|
|
|
|
uchar RC_size;
|
|
|
|
|
uchar blockAddr; //Select the address of the operation 0~63
|
2020-12-10 21:17:19 +00:00
|
|
|
|
|
|
|
|
|
|
2020-12-11 02:57:10 +00:00
|
|
|
|
// searching card, return card type
|
|
|
|
|
while(MFRC522_Request(PICC_REQIDL, str) != MI_OK);
|
2020-12-10 21:17:19 +00:00
|
|
|
|
|
2020-12-11 02:57:10 +00:00
|
|
|
|
|
|
|
|
|
status = MFRC522_Anticoll(str);
|
|
|
|
|
memcpy(serNum, str, 5);
|
|
|
|
|
if (status == MI_OK)
|
|
|
|
|
{
|
|
|
|
|
printf("The card's number is : %x%x%x%x%x%x\n\r",
|
|
|
|
|
serNum[0],
|
|
|
|
|
serNum[1],
|
|
|
|
|
serNum[2],
|
|
|
|
|
serNum[3],
|
|
|
|
|
serNum[4]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// select card, return card capacity
|
|
|
|
|
RC_size = MFRC522_SelectTag(serNum);
|
|
|
|
|
if (RC_size != 0)
|
|
|
|
|
{}
|
2020-12-10 21:17:19 +00:00
|
|
|
|
|
2020-12-11 02:57:10 +00:00
|
|
|
|
if(PINC & (1<<PC6)) {
|
2020-12-10 21:17:19 +00:00
|
|
|
|
// write data card
|
|
|
|
|
blockAddr = 7; // data block 7
|
2020-12-11 02:57:10 +00:00
|
|
|
|
status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum); // authentication
|
2020-12-10 21:17:19 +00:00
|
|
|
|
if (status == MI_OK)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// write data
|
|
|
|
|
blockAddr = blockAddr - 3 ;
|
|
|
|
|
status = MFRC522_Write(blockAddr, writeDate);
|
|
|
|
|
if(status == MI_OK)
|
|
|
|
|
{
|
|
|
|
|
printf("OK!\n\r");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-11 02:57:10 +00:00
|
|
|
|
} else {
|
2020-12-10 21:17:19 +00:00
|
|
|
|
// read card
|
|
|
|
|
blockAddr = 7; // data block 7
|
2020-12-11 02:57:10 +00:00
|
|
|
|
status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA, serNum); // authentication
|
2020-12-10 21:17:19 +00:00
|
|
|
|
if (status == MI_OK)
|
|
|
|
|
{
|
|
|
|
|
// read data
|
2020-12-11 02:57:10 +00:00
|
|
|
|
blockAddr = blockAddr - 3 ;
|
|
|
|
|
status = MFRC522_Read(blockAddr, str);
|
2020-12-10 21:17:19 +00:00
|
|
|
|
if (status == MI_OK)
|
|
|
|
|
{
|
|
|
|
|
printf("Read from the card ,the data is : \n\r");
|
|
|
|
|
for (i=0; i<16; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%c", str[i]);
|
2020-12-11 02:57:10 +00:00
|
|
|
|
if(writeDate[i] != str[i]) printf("Not equal");
|
2020-12-10 21:17:19 +00:00
|
|
|
|
}
|
|
|
|
|
printf("\n\r");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-11 02:57:10 +00:00
|
|
|
|
}
|
|
|
|
|
printf("\n\r");
|
|
|
|
|
MFRC522_Halt(); // command card into sleeping mode
|
2020-12-10 21:17:19 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|