MFRC522/test/main.cpp

118 lines
2.6 KiB
C++
Raw Normal View History

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"
2020-12-25 11:56:38 +00:00
#include <util/delay.h>
2020-12-10 21:17:19 +00:00
2020-12-25 11:56:38 +00:00
#define uint8_t unsigned char
2020-12-10 21:17:19 +00:00
#define uint unsigned int
#define MAX_LEN 16
const int chipSelectPin = 10;
const int NRSTPD = 5;
2020-12-25 11:56:38 +00:00
uint8_t serNum[5];
uint8_t writeDate[16] ={'T', 'e', 'n', 'g', ' ', 'B', 'o', 0, 0, 0, 0, 0, 0, 0, 0,0};
2020-12-10 21:17:19 +00:00
2020-12-25 11:56:38 +00:00
uint8_t 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-25 11:56:38 +00:00
MFRC522 *mfrc522;
MFRC522 mfrc522_1(PB4, &PORTB, PB2, &PORTB);
MFRC522 mfrc522_2(PB3, &PORTB, PB2, &PORTB);
printf("%x %x ", &PORTB, &DDRB);
printf("%x %x ", &PORTC, &DDRC);
int reader;
2020-12-10 21:17:19 +00:00
while(1) {
2020-12-25 11:56:38 +00:00
uint8_t i;
uint8_t status;
uint8_t str[MAX_LEN];
uint8_t RC_size;
uint8_t blockAddr; //Select the address of the operation 063
2020-12-10 21:17:19 +00:00
2020-12-11 02:57:10 +00:00
// searching card, return card type
2020-12-25 11:56:38 +00:00
while(1) {
mfrc522_1.Init();
if(mfrc522_1.Request(PICC_REQIDL, str) == MI_OK) {
mfrc522 = &mfrc522_1;
printf("Reader 1\n\r");
reader = 1;
break;
}
mfrc522_2.Init();
if(mfrc522_2.Request(PICC_REQIDL, str) == MI_OK) {
mfrc522 = &mfrc522_2;
printf("Reader 2\n\r");
reader = 0;
break;
}
}
2020-12-10 21:17:19 +00:00
2020-12-11 02:57:10 +00:00
2020-12-25 11:56:38 +00:00
status = mfrc522->Anticoll(str);
2020-12-11 02:57:10 +00:00
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
2020-12-25 11:56:38 +00:00
RC_size = mfrc522->SelectTag(serNum);
2020-12-11 02:57:10 +00:00
if (RC_size != 0)
{}
2020-12-10 21:17:19 +00:00
2020-12-25 11:56:38 +00:00
if(reader) {
2020-12-10 21:17:19 +00:00
// write data card
blockAddr = 7; // data block 7
2020-12-25 11:56:38 +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 ;
2020-12-25 11:56:38 +00:00
writeDate[15] = writeDate[15] + 1;
status = mfrc522->Write(blockAddr, writeDate);
2020-12-10 21:17:19 +00:00
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-25 11:56:38 +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 ;
2020-12-25 11:56:38 +00:00
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++)
{
2020-12-25 11:56:38 +00:00
printf("%x", 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
}
2020-12-25 11:56:38 +00:00
printf("\n\r\n\n\n");
mfrc522->Halt(); // command card into sleeping mode
_delay_ms(1000);
2020-12-10 21:17:19 +00:00
}
}