CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

RC522 RFID module SPI
Goto page Previous  1, 2, 3, 4, 5
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
oleoarruda



Joined: 07 Jan 2021
Posts: 1

View user's profile Send private message

I need a help
PostPosted: Thu Jan 07, 2021 12:28 pm     Reply with quote

Hello everyone.

I'm having trouble reading MIFARE cards using the MFRC522 module.
I know where the problem is, but I don't know how to fix it.
I am able to communicate with the module via SPI. When I call the function to identify if there is a card, it transmits the 0x26 byte to the antenna and waits to receive a byte from the card. However, she is not receiving anything, even with the card next to the antenna.

Attached is the code for a part of the main. I didn't put the configuration pins, because I think everything is ok.
Code:

MFRC522_Init();

    /*--------------- Loop embarcado ---------------*/
    while (1)
    {

        UART_Write(0x11);
        {
        if( MFRC522_isCard( TagType ) )
            UART_Write(0xCC);
           
            if( MFRC522_ReadCardSerial( &UID ) )
            { 
               
                //sprintf(texto,"%lu",codigo_rfid);// lo pasa a unsigned long
                           
                //LongIntToHex(codigo_rfid,texto);
                   
               
                UART_Write(0xAA);
               
            }
        }

Attached is the library code:
(The problem is in the MFRC522_ToCard function. After executing the command, it loops, counting 25 ms, waiting for a bit from the COMMIRQREG register to be set, indicating that a data has been received.
But even with the card, it receives nothing (the loop ends without setting the bit in the register)).
Code:

//conecci�n de modulo RFID   usando SPI pot software

#include "SPI-Flash.h"

//MF522 Command word
#define PCD_IDLE              0x00               //NO action; Cancel the current command
#define PCD_AUTHENT           0x0E               //Authentication Key
#define PCD_RECEIVE           0x08               //Receive Data
#define PCD_TRANSMIT          0x04               //Transmit data
#define PCD_TRANSCEIVE        0x0C               //Transmit and receive data,
#define PCD_RESETPHASE        0x0F               //Reset
#define PCD_CALCCRC           0x03               //CRC Calculate

// Mifare_One card command word
#define PICC_REQIDL          0x26               // find the antenna area does not enter hibernation
#define PICC_REQALL          0x52               // find all the cards antenna area
#define PICC_ANTICOLL        0x93               // anti-collision
#define PICC_SElECTTAG       0x93               // election card
#define PICC_AUTHENT1A       0x60               // authentication key A
#define PICC_AUTHENT1B       0x61               // authentication key B
#define PICC_READ            0x30               // Read Block
#define PICC_WRITE           0xA0               // write block
#define PICC_DECREMENT       0xC0               // debit
#define PICC_INCREMENT       0xC1               // recharge
#define PICC_RESTORE         0xC2               // transfer block data to the buffer
#define PICC_TRANSFER        0xB0               // save the data in the buffer
#define PICC_HALT            0x50               // Sleep
//And MF522 The error code is returned when communication
#define MI_OK                 0
#define MI_NOTAGERR           1
#define MI_ERR                2
//------------------MFRC522 Register---------------
//Page 0:Command and Status
#define     RESERVED00            0x00
#define     COMMANDREG            0x01
#define     COMMIENREG            0x02
#define     DIVLENREG             0x03
#define     COMMIRQREG            0x04
#define     DIVIRQREG             0x05
#define     ERRORREG              0x06
#define     STATUS1REG            0x07
#define     STATUS2REG            0x08
#define     FIFODATAREG           0x09
#define     FIFOLEVELREG          0x0A
#define     WATERLEVELREG         0x0B
#define     CONTROLREG            0x0C
#define     BITFRAMINGREG         0x0D
#define     COLLREG               0x0E
#define     RESERVED01            0x0F
//PAGE 1:Command
#define     RESERVED10            0x10
#define     MODEREG               0x11
#define     TXMODEREG             0x12
#define     RXMODEREG             0x13
#define     TXCONTROLREG          0x14
#define     TXAUTOREG             0x15
#define     TXSELREG              0x16
#define     RXSELREG              0x17
#define     RXTHRESHOLDREG        0x18
#define     DEMODREG              0x19
#define     RESERVED11            0x1A
#define     RESERVED12            0x1B
#define     MIFAREREG             0x1C
#define     RESERVED13            0x1D
#define     RESERVED14            0x1E
#define     SERIALSPEEDREG        0x1F
//PAGE 2:CFG
#define     RESERVED20            0x20
#define     CRCRESULTREGM         0x21
#define     CRCRESULTREGL         0x22
#define     RESERVED21            0x23
#define     MODWIDTHREG           0x24
#define     RESERVED22            0x25
#define     RFCFGREG              0x26
#define     GSNREG                0x27
#define     CWGSPREG              0x28
#define     MODGSPREG             0x29
#define     TMODEREG              0x2A
#define     TPRESCALERREG         0x2B
#define     TRELOADREGH           0x2C
#define     TRELOADREGL           0x2D
#define     TCOUNTERVALUEREGH     0x2E
#define     TCOUNTERVALUEREGL     0x2F
//PAGE 3:TEST REGISTER
#define     RESERVED30            0x30
#define     TESTSEL1REG           0x31
#define     TESTSEL2REG           0x32
#define     TESTPINENREG          0x33
#define     TESTPINVALUEREG       0x34
#define     TESTBUSREG            0x35
#define     AUTOTESTREG           0x36
#define     VERSIONREG            0x37
#define     ANALOGTESTREG         0x38
#define     TESTDAC1REG           0x39
#define     TESTDAC2REG           0x3A
#define     TESTADCREG            0x3B
#define     RESERVED31            0x3C
#define     RESERVED32            0x3D
#define     RESERVED33            0x3E
#define     RESERVED34            0x3F
static void MFRC522_Wr( char addr, char value )
{
        MFRC522_CS = 0;
        SPI_Write( ( addr << 1 ) & 0x7E );
        SPI_Write( value );
        MFRC522_CS = 1;
}
static uint8_t MFRC522_Rd( char addr )
{
        uint8_t value;
        MFRC522_CS = 0;
        SPI_Write( (( addr << 1 ) & 0x7E) | 0x80 );
        value = SPI_Read();
        MFRC522_CS = 1;
        return value;
}
static void MFRC522_Clear_Bit( char addr, char mask )
{
     MFRC522_Wr( addr, MFRC522_Rd( addr ) & (~mask) );
}
static void MFRC522_Set_Bit( char addr, char mask )
{
     MFRC522_Wr( addr, MFRC522_Rd( addr ) | mask );
}
void MFRC522_Reset()
{
        MFRC522_Wr( COMMANDREG, PCD_RESETPHASE );
}
void MFRC522_AntennaOn()
{
 MFRC522_Set_Bit( TXCONTROLREG, 0x03 );
}
void MFRC522_AntennaOff()
{
 MFRC522_Clear_Bit( TXCONTROLREG, 0x03 );
}
void MFRC522_Init()
{
     MFRC522_CS_Direction = 0;
     MFRC522_Rst_Direction = 0;
     MFRC522_CS = 1;
     MFRC522_Rst = 1;

     MFRC522_Reset();

     MFRC522_Wr( TMODEREG, 0x8D );      //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
     MFRC522_Wr( TPRESCALERREG, 0x3E ); //TModeReg[3..0] + TPrescalerReg
     MFRC522_Wr( TRELOADREGL, 30 );
     MFRC522_Wr( TRELOADREGH, 0 );
     MFRC522_Wr( TXAUTOREG, 0x40 );
     MFRC522_Wr( MODEREG, 0x3D );

    //  MFRC522_Wr( TMODEREG, 0x8D );      //Tauto=1; f(Timer) = 6.78MHz/TPreScaler
    //  MFRC522_Wr( TPRESCALERREG, 0x3E ); //TModeReg[3..0] + TPrescalerReg
    //  MFRC522_Wr( TRELOADREGL, 30 );
    //  MFRC522_Wr( TRELOADREGH, 0 );

     

    //  MFRC522_Wr( TXAUTOREG, 0x40 );    //100%ASK
    //  MFRC522_Wr( MODEREG, 0x3D );      // CRC valor inicial de 0x6363

     //MFRC522_Clear_Bit( STATUS2REG, 0x08 );//MFCrypto1On=0
     //MFRC522_Wr( RXSELREG, 0x86 );      //RxWait = RxSelReg[5..0]
     //MFRC522_Wr( RFCFGREG, 0x7F );     //RxGain = 48dB
    UART_Write(0x99);
     MFRC522_AntennaOn();
    MFRC522_Wr( RFCFGREG, 0x50 );

}
char MFRC522_ToCard( char command, char *sendData, char sendLen, char *backData, unsigned *backLen )
{
  char _status = MI_ERR;
  char irqEn = 0x00;
  char waitIRq = 0x00;
  char lastBits;
  char n;
  unsigned i;

  switch (command)
  {
    case PCD_AUTHENT:       //Certification cards close
    {
      irqEn = 0x12;
      waitIRq = 0x10;
      break;
    }
    case PCD_TRANSCEIVE:    //Transmit FIFO data
    {
      irqEn = 0x77;
      waitIRq = 0x30;
      break;
    }
    default:
      break;
  }
  MFRC522_Wr( COMMIENREG, irqEn | 0x80 );
  MFRC522_Clear_Bit( COMMIRQREG, 0x80 );
  MFRC522_Set_Bit( FIFOLEVELREG, 0x80 );
  MFRC522_Wr( COMMANDREG, PCD_IDLE );


  //Writing data to the FIFO
  for ( i=0; i < sendLen; i++ )
  {
    MFRC522_Wr( FIFODATAREG, sendData[i] );
  }
 

  //Execute the command
  MFRC522_Wr( COMMANDREG, command );
  if (command == PCD_TRANSCEIVE )
  {
    MFRC522_Set_Bit( BITFRAMINGREG, 0x80 ); //StartSend=1,transmission of data starts
  }
    //UART_Write(MFRC522_Rd(FIFODATAREG));

  //Waiting to receive data to complete
  //i according to the clock frequency adjustment, the operator M1 card maximum waiting time 25ms???
 
  i = 25;

  do
  {
    __delay_ms(1);
    //CommIrqReg[7..0]
    //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
   
    n = MFRC522_Rd( COMMIRQREG );
    i--;
         

  }
  while ( i && !(n & 0x01) && !( n & waitIRq ) );
  MFRC522_Clear_Bit( BITFRAMINGREG, 0x80 );    //StartSend=0
   


  UART_Write(MFRC522_Rd(RFCFGREG));
 UART_Write(0x78);
  UART_Write(MFRC522_Rd(STATUS2REG));
  UART_Write(0x22);
  UART_Write(i);
  UART_Write(0x33);
  UART_Write(n);
  if (i != 0)
  {
    if (!(MFRC522_Rd(ERRORREG) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
    {
      _status = MI_OK;
      if (n & irqEn & 0x01)
      {
        _status = MI_NOTAGERR; //??
      }
      if (command == PCD_TRANSCEIVE)
      {

        n = MFRC522_Rd(FIFOLEVELREG);

        lastBits = MFRC522_Rd(CONTROLREG) & 0x07;
        if (lastBits)
        {
          *backLen = (n - 1) * 8 + lastBits;
        }
        else
        {
          *backLen = n * 8;
        }
        if (n == 0)
        {
          n = 1;
        }
        if (n > 16)
        {
          n = 16;
        }
        //Reading the received data in FIFO
        for (i = 0; i < n; i++)
        {
          backData[i] = MFRC522_Rd(FIFODATAREG);
        }

        backData[i] = 0;
      }
    }
    else
    {
      _status = MI_ERR;
    }
  }
  //MFRC522_Set_Bit( CONTROLREG, 0x80 );
  //MFRC522_Wr( COMMANDREG, PCD_IDLE );
  return _status;
}
char MFRC522_Request(char reqMode, char *TagType)
{

  char _status;
  unsigned backBits; //The received data bits

  MFRC522_Wr(BITFRAMINGREG, 0x07); //TxLastBists = BitFramingReg[2..0]   ???

  TagType[0] = reqMode;
  _status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
  if ((_status != MI_OK) || (backBits != 0x10))
  {
    _status = MI_ERR;
  }
  return _status;
}
void MFRC522_CRC(char *dataIn, char length, char *dataOut)
{
  char i, n;
  MFRC522_Clear_Bit(DIVIRQREG, 0x04);
  MFRC522_Set_Bit(FIFOLEVELREG, 0x80);

  //Escreve dados no FIFO
  for (i = 0; i < length; i++)
  {
    MFRC522_Wr(FIFODATAREG, *dataIn++);
  }

  MFRC522_Wr(COMMANDREG, PCD_CALCCRC);

  i = 0xFF;
  //Espera a finaliza��o do Calculo do CRC
  do
  {
    n = MFRC522_Rd(DIVIRQREG);
    i--;
  } while (i && !(n & 0x04)); //CRCIrq = 1

  dataOut[0] = MFRC522_Rd(CRCRESULTREGL);
  dataOut[1] = MFRC522_Rd(CRCRESULTREGM);
}
char MFRC522_SelectTag(char *serNum)
{
  char i;
  char _status;
  char size;
  unsigned recvBits;
  char buffer[9];

  //MFRC522_Clear_Bit( STATUS2REG, 0x08 );   //MFCrypto1On=0

  buffer[0] = PICC_SElECTTAG;
  buffer[1] = 0x70;

  for (i = 2; i < 7; i++)
  {
    buffer[i] = *serNum++;
  }

  MFRC522_CRC(buffer, 7, &buffer[7]);

  _status = MFRC522_ToCard(PCD_TRANSCEIVE, buffer, 9, buffer, &recvBits);
  if ((_status == MI_OK) && (recvBits == 0x18))
  {
    size = buffer[0];
  }
  else
  {
    size = 0;
  }
  return size;
}
//hibernation
void MFRC522_Halt()
{
  unsigned unLen;
  char buff[4];

  buff[0] = PICC_HALT;
  buff[1] = 0;
  MFRC522_CRC( buff, 2, &buff[2] );
  MFRC522_Clear_Bit( STATUS2REG, 0x80 );
  MFRC522_ToCard( PCD_TRANSCEIVE, buff, 4, buff, &unLen );
  MFRC522_Clear_Bit( STATUS2REG, 0x08 );
}
char MFRC522_Auth( char authMode, char BlockAddr, char *Sectorkey, char *serNum )
{
  char _status;
  unsigned recvBits;
  char i;
  char buff[12];

  //Verify the command block address + sector + password + card serial number
  buff[0] = authMode;
  buff[1] = BlockAddr;

  for ( i = 2; i < 8; i++ )
  {
    buff[i] = Sectorkey[i-2];
  }

  for ( i = 8; i < 12; i++ )
  {
    buff[i] = serNum[i-8];
  }

  _status = MFRC522_ToCard( PCD_AUTHENT, buff, 12, buff, &recvBits );

  if ( ( _status != MI_OK ) || !( MFRC522_Rd( STATUS2REG ) & 0x08 ) )
  {
    _status = MI_ERR;
  }

  return _status;
}
char MFRC522_Write( char blockAddr, char *writeData )
{
  char _status;
  unsigned recvBits;
  char i;
  char buff[18];
  buff[0] = PICC_WRITE;
  buff[1] = blockAddr;

  MFRC522_CRC( buff, 2, &buff[2] );
  _status = MFRC522_ToCard( PCD_TRANSCEIVE, buff, 4, buff, &recvBits );
  if ( (_status != MI_OK) || (recvBits != 4) || ( (buff[0] & 0x0F) != 0x0A) )
  {
    _status = MI_ERR;
  }
  if (_status == MI_OK)
  {
    for ( i = 0; i < 16; i++ )                //Data to the FIFO write 16Byte
    {
      buff[i] = writeData[i];
    }

    MFRC522_CRC( buff, 16, &buff[16] );
    _status = MFRC522_ToCard( PCD_TRANSCEIVE, buff, 18, buff, &recvBits );
    if ( (_status != MI_OK) || (recvBits != 4) || ( (buff[0] & 0x0F) != 0x0A ) )
    {
      _status = MI_ERR;
    }
  }
  return _status;
}
char MFRC522_Read( char blockAddr, char *recvData )
{
  char _status;
  unsigned unLen;
  recvData[0] = PICC_READ;
  recvData[1] = blockAddr;

  MFRC522_CRC( recvData, 2, &recvData[2] );

  _status = MFRC522_ToCard( PCD_TRANSCEIVE, recvData, 4, recvData, &unLen );
  if ( (_status != MI_OK) || (unLen != 0x90) )
  {
    _status = MI_ERR;
  }
  return _status;
}
char MFRC522_AntiColl( char *serNum )
{
  char _status;
  char i;
  char serNumCheck = 0;
  unsigned unLen;
  MFRC522_Wr( BITFRAMINGREG, 0x00 );                //TxLastBists = BitFramingReg[2..0]
  serNum[0] = PICC_ANTICOLL;
  serNum[1] = 0x20;
  MFRC522_Clear_Bit( STATUS2REG, 0x08 );
  _status = MFRC522_ToCard( PCD_TRANSCEIVE, serNum, 2, serNum, &unLen );
  if (_status == MI_OK)
  {
    for ( i=0; i < 4; i++ )
    {
      serNumCheck ^= serNum[i];
    }

    if ( serNumCheck != serNum[4] )
    {
      _status = MI_ERR;
    }
  }
  return _status;
}
//0x0044 = Mifare_UltraLight
//0x0004 = Mifare_One (S50)
//0x0002 = Mifare_One (S70)
//0x0008 = Mifare_Pro (X)
//0x0344 = Mifare_DESFire
char MFRC522_isCard( char *TagType )
{
   
    if (MFRC522_Request( PICC_REQALL, TagType ) == MI_OK)
        return 1;
    else
        return 0;
}
char MFRC522_ReadCardSerial( char *str )
{
char _status;
 _status = MFRC522_AntiColl( str );
 str[5] = 0;
 if (_status == MI_OK)
  return 1;
 else
  return 0;
}
umbertomori



Joined: 25 Feb 2021
Posts: 2

View user's profile Send private message

Verify presence tag
PostPosted: Thu Feb 25, 2021 4:28 am     Reply with quote

Hi, i use RC522, but i have a problem for verify when there is presence of a tag on the antenna....

For verify the presence on the rfid, i use:
Code:
unsigned char MFRC522_Request(unsigned char reqMode,unsigned char *TagType)
{
   unsigned char status; 
   unsigned int16 backBits;               //the data bits that received
   Write_MFRC522(BitFramingReg,0x07);     //TxLastBists = BitFramingReg[2..0]
   TagType[0]=reqMode;
   status=MFRC522_ToCard(PCD_TRANSCEIVE,TagType,1,TagType,&backBits);
   if((status != MI_OK) || (backBits != 0x10)){status=MI_ERR;}
   return status;           
}

But the answer is 02,00,02,00,02,00 ecc.

When the tag is present, the answer must be always 02.

There is another mode for read the presence of the tag in rfid?
Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 17026

View user's profile Send private message

PostPosted: Thu Feb 25, 2021 8:51 am     Reply with quote

Toggling like this is the standard response. You'll find lots of threads
online reporting this.
The way to test for a card is to do a read. If this returns an error, then
the card is not present.
umbertomori



Joined: 25 Feb 2021
Posts: 2

View user's profile Send private message

PostPosted: Fri Feb 26, 2021 6:40 am     Reply with quote

Ttelmah wrote:
Toggling like this is the standard response. You'll find lots of threads
online reporting this.
The way to test for a card is to do a read. If this returns an error, then
the card is not present.


I have to be very quick in polling to understand if there is a card in the radio field ...

Can you help me for sequence of the command for verify the presence of the card?
vasiliok



Joined: 17 May 2011
Posts: 10
Location: Kaunas, Lithuania

View user's profile Send private message

PostPosted: Mon May 10, 2021 7:32 am     Reply with quote

I have an interesting situation.
When I continiously reading RFID TAG it reads successfully every second time.
Anyone faced the same thing?
Or your reader reads TAG every time? Sure if TAG is near reader.
Ttelmah



Joined: 11 Mar 2010
Posts: 17026

View user's profile Send private message

PostPosted: Mon May 10, 2021 8:01 am     Reply with quote

As I said:
Quote:

Toggling like this is the standard response. You'll find lots of threads
online reporting this.


Alternate responses is what you do see if you repeatedly poll.
vasiliok



Joined: 17 May 2011
Posts: 10
Location: Kaunas, Lithuania

View user's profile Send private message

PostPosted: Mon May 10, 2021 8:24 am     Reply with quote

Ttelmah, ok!
Ttelmah



Joined: 11 Mar 2010
Posts: 17026

View user's profile Send private message

PostPosted: Mon May 10, 2021 11:42 pm     Reply with quote

I think what happens is reading the poll, clears the bit, and it then takes a
'scan time' to re-trigger.
Personally if I want to find a tag is present, I always use the interrupt
output to know when a tag has entered the field.
vasiliok



Joined: 17 May 2011
Posts: 10
Location: Kaunas, Lithuania

View user's profile Send private message

PostPosted: Tue May 11, 2021 9:35 am     Reply with quote

Thanks for reply and good ideas!

Ttelmah wrote:
I think what happens is reading the poll, clears the bit, and it then takes a
'scan time' to re-trigger.
Personally if I want to find a tag is present, I always use the interrupt
output to know when a tag has entered the field.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5
Page 5 of 5

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group