Hello there.
I’m making a project that gets through SPI access to an RFID RC-522 and an SD card.
My code is compilind but its not running, maybe because its my first time using SPI with multiples slaves and i’m probably doing something wrong with the SS.
My code is this:
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>
#define SDCD_SS 4
#define RST_PIN 9
#define RFID_SS 10
#define BAR_CODE_EAN_LENGTH 13
#define TIPO_LENGTH 1
#define COR_LENGTH 3
#define PARTICULARIDADES_LENGTH 10
#define MARCA_LENGTH 3
#define TAMANHO_LENGTH 2
MFRC522 RFID(RFID_SS, RST_PIN);
MFRC522::MIFARE_Key key;
const int RFID_LENGTH_SECTOR = 18;
String aux = "";
File myFile;
typedef struct TRfid
{
byte BAR_CODE[13];
byte TIPO;
byte COR[3];
byte PARTICULARIDADES[10];
byte MARCA[3];
byte TAMANHO[2];
} RfidCode;
RfidCode code;
void DumpSector(MFRC522::Uid *uid, byte sector);
void DumpCode();
void setup()
{
//Initialize Log
Serial.begin(115200);
Serial.println("INICIO");
pinMode(SDCD_SS, OUTPUT);
pinMode(RFID_SS, OUTPUT);
digitalWrite(SDCD_SS, HIGH);
digitalWrite(RFID_SS, HIGH);
InitializeRFID();
InitializeSD();
}
void loop()
{
SPIRFID();
if(!RFID.PICC_IsNewCardPresent())
return;
if(!RFID.PICC_ReadCardSerial())
return;
Dump();
DumpRfidCode();
delay(100);
SPISD();
myFile = SD.open("TEXT.TXT", FILE_WRITE);
if(myFile)
{
myFile.println("OKOKOKOKOKOK");
}
myFile.close();
}
void InitializeRFID()
{
Serial.println("Initialize RFID.");
SPIRFID();
SPI.begin();
RFID.PCD_Init();
memset(&key, 0xFF, sizeof(key));
SPISD();
}
void InitializeSD()
{
Serial.println("Initialize SD.");
SPISD();
if(!SD.begin(SDCD_SS))
{
Serial.println("FAILED!!!");
return;
}
Serial.println("SD OK!!!");
SPIRFID();
}
void Dump()
{
byte no_of_sectors = 16;
for (char i = no_of_sectors - 1; i >= 0; i--)
{
DumpSector(&(RFID.uid), i);
}
RFID.PICC_HaltA(); // Halt the PICC before stopping the encrypted session.
RFID.PCD_StopCrypto1();
}
void DumpSector(MFRC522::Uid *uid, byte sector)
{
byte status;
byte firstBlock; // Address of lowest address to dump actually last block dumped)
byte no_of_blocks; // Number of blocks in sector
bool isSectorTrailer; // Set to true while handling the "last" (ie highest address) in the sector.
byte c1, c2, c3; // Nibbles
byte c1_, c2_, c3_; // Inverted nibbles
bool invertedError; // True if one of the inverted nibbles did not match
byte g[4]; // Access bits for each of the four groups.
byte group; // 0-3 - active group for access bits
bool firstInGroup; // True for the first block dumped in the group
no_of_blocks = 4;
firstBlock = sector * no_of_blocks;
// Dump blocks, highest address first.
byte byteCount;
byte buffer[18];
byte blockAddr;
isSectorTrailer = true;
for (char blockOffset = no_of_blocks - 1; blockOffset >= 0; blockOffset--)
{
blockAddr = firstBlock + blockOffset;
// Establish encrypted communications before reading the first block
if (isSectorTrailer)
{
status = RFID.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, firstBlock, &key, uid);
if (status != MFRC522::STATUS_OK)
{
Serial.print("PCD_Authenticate() failed: ");
Serial.println(RFID.GetStatusCodeName(status));
return;
}
}
//Serial.print(blockAddr < 10 ? " " : (blockAddr < 100 ? " " : " ")); // Pad with spaces
//Serial.print(blockAddr);
//Serial.print(" ");
// Read block
byteCount = sizeof(buffer);
status = RFID.MIFARE_Read(blockAddr, buffer, &byteCount);
if (status != MFRC522::STATUS_OK)
{
Serial.print("MIFARE_Read() failed: ");
Serial.println(RFID.GetStatusCodeName(status));
continue;
}
byte c = 0;
byte p = 0;
byte m = 0;
byte t = 0;
switch(blockAddr)
{
case 63:
for (byte index = 0; index < BAR_CODE_EAN_LENGTH; index++)
{
code.BAR_CODE[index] = buffer[index];
/*Serial.print(buffer[index] < 0x10 ? " 0" : " ");
Serial.print(buffer[index], HEX);
if ((index % 4) == 3)
{
Serial.print(" ");
}*/
}
break;
// case 62:
// .
// .
// .
// case 2:
case 1:
for (byte index = 0; index < (16 - 1 - COR_LENGTH + 1); index++)
{
/*Serial.print(buffer[index] < 0x10 ? " " : " ");
Serial.print(" ");
if ((index % 4) == 3)
{
Serial.print(" ");
}*/
}
for (byte index = (16 - TIPO_LENGTH - COR_LENGTH + 1); index < 16; index++)
{
if(index == (16 - TIPO_LENGTH - COR_LENGTH + 1)) code.TIPO = buffer[index];
if(index > (16 - TIPO_LENGTH - COR_LENGTH + 1)) { code.COR[c] = buffer[index]; c++; }
/*Serial.print(buffer[index] < 0x10 ? " 0" : " ");
Serial.print(buffer[index], HEX);
if ((index % 4) == 3)
{
Serial.print(" ");
}*/
}
break;
case 0:
for (byte index = 0; index < (COR_LENGTH - 2 + PARTICULARIDADES_LENGTH + MARCA_LENGTH + TAMANHO_LENGTH); index++)
{
if(index == 0) { code.COR[2] = buffer[index]; }
if(index > 0 && index <= PARTICULARIDADES_LENGTH) { code.PARTICULARIDADES[p] = buffer[index]; p++; }
if(index > PARTICULARIDADES_LENGTH && index <= PARTICULARIDADES_LENGTH + MARCA_LENGTH) { code.MARCA[m] = buffer[index]; m++; }
if(index > PARTICULARIDADES_LENGTH + MARCA_LENGTH && index < 16) { code.TAMANHO[t] = buffer[index]; t++; }
/*Serial.print(buffer[index] < 0x10 ? " 0" : " ");
Serial.print(buffer[index], HEX);
if ((index % 4) == 3)
{
Serial.print(" ");
}*/
}
break;
default:
/*for (byte index = 0; index < 16; index++)
{
Serial.print(buffer[index] < 0x10 ? " 0" : " ");
Serial.print(buffer[index], HEX);
if ((index % 4) == 3)
{
Serial.print(" ");
}
}*/
break;
}
//Serial.println();
free(buffer);
}
return;
}
void DumpRfidCode()
{
Serial.print("BAR_CODE:");
for(int i = 0; i < BAR_CODE_EAN_LENGTH; i++)
{
Serial.print(code.BAR_CODE[i] < 0x10 ? " 0" : " ");
Serial.print(code.BAR_CODE[i], DEC);
}
Serial.println();
Serial.print("TIPO:");
Serial.print(code.TIPO < 0x10 ? " 0" : " ");
Serial.print(code.TIPO, DEC);
Serial.println();
Serial.print("COR:");
for(int i = 0; i < COR_LENGTH; i++)
{
Serial.print(code.COR[i] < 0x10 ? " 0" : " ");
Serial.print(code.COR[i], DEC);
}
Serial.println();
Serial.print("PARTICULARIDADES:");
for(int i = 0; i < PARTICULARIDADES_LENGTH; i++)
{
Serial.print(code.PARTICULARIDADES[i] < 0x10 ? " 0" : " ");
Serial.print(code.PARTICULARIDADES[i], DEC);
}
Serial.println();
Serial.print("MARCA:");
for(int i = 0; i < MARCA_LENGTH; i++)
{
Serial.print(code.MARCA[i] < 0x10 ? " 0" : " ");
Serial.print(code.MARCA[i], DEC);
}
Serial.println();
Serial.print("TAMANHO:");
for(int i = 0; i < TAMANHO_LENGTH; i++)
{
Serial.print(code.TAMANHO[i] < 0x10 ? " 0" : " ");
Serial.print(code.TAMANHO[i], DEC);
}
Serial.println();
}
void SPIRFID()
{
digitalWrite(SDCD_SS, HIGH);
digitalWrite(RFID_SS, LOW);
delay(50);
}
void SPISD()
{
digitalWrite(RFID_SS, HIGH);
digitalWrite(SDCD_SS, LOW);
delay(50);
}
But every time I try to open an SD file my code prints nothing or broken characters.
I’m desperate, please HELP =P!!!
TKS