GithubHelp home page GithubHelp logo

Changing the MFRC522 Receiver Gain about rfid HOT 8 CLOSED

mdxs avatar mdxs commented on June 25, 2024 3
Changing the MFRC522 Receiver Gain

from rfid.

Comments (8)

omersiar avatar omersiar commented on June 25, 2024

Once I tried Setting Register but obviously failed because of bit shifting. I was really confused as a Newbie, Now it is clear to me, thank you.

What do you suggest where to use Gain Control ? at Library level or Sketch level? Maybe a simple define help others to know there is a Gain Control with Dump Example or Calling PCD_Init(VariableGain); ? other MFRC522 Library for another platform comes with Maximum Gain by default.

byte gain = 0x07 ; // defines Receiver's gain dafault 48db max

/*RxGain [2:0]
  defines the receiver's signal voltage gain factor:
  000 18 dB HEX = 0x00
  001 23 dB HEX = 0x01
  010 18 dB HEX = 0x02  
  011 23 dB HEX = 0x03
  100 33 dB HEX = 0x04
  101 38 dB HEX = 0x05
  110 43 dB HEX = 0x06
  111 48 dB HEX = 0x07
  3 to 0 reserved - reserved for future use
*/



void MFRC522::PCD_Init(byte gain[]) {
  if (digitalRead(_resetPowerDownPin) == LOW) { //The MFRC522 chip is in power down mode.
    digitalWrite(_resetPowerDownPin, HIGH); // Exit power down mode. This triggers a hard reset.

    // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74ms. Let us be generous: 50ms.
    delay(50);
  }
  else { // Perform a soft reset
    PCD_Reset();
  }

  // When communicating with a PICC we need a timeout if something goes wrong.
  // f_timer = 13.56 MHz / (2*TPreScaler+1) where TPreScaler = [TPrescaler_Hi:TPrescaler_Lo].
  // TPrescaler_Hi are the four low bits in TModeReg. TPrescaler_Lo is TPrescalerReg.

  PCD_WriteRegister(TModeReg, 0x80);        // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds
  PCD_WriteRegister(TPrescalerReg, 0xA9);   // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25�s.
  PCD_WriteRegister(TReloadRegH, 0x03);     // Reload timer with 0x3E8 = 1000, ie 25ms before timeout.
  PCD_WriteRegister(TReloadRegL, 0xE8);

  PCD_WriteRegister(TxASKReg, 0x40);        // Default 0x00. Force a 100 % ASK modulation independent of the ModGsPReg register setting
  PCD_WriteRegister(ModeReg, 0x3D);     // Default 0x3F. Set the preset value for the CRC coprocessor for the CalcCRC command to 0x6363 (ISO 14443-3 part 6.2.4)
  PCD_WriteRegister(RFCfgReg, (gain<<4));       // Set Rx Gain to max
  PCD_AntennaOn();              // Enable the antenna driver pins TX1 and TX2 (they were disabled by the reset)
} // End PCD_Init()

from rfid.

mdxs avatar mdxs commented on June 25, 2024

Well, I think the PCD_WriteRegister approach for RFCfgReg shown in above modified PCD_Init might be incorrect; as that is setting more bits than what the specification says are to be used... this is why that bit shifting is needed.

I would recommend to change:

  PCD_WriteRegister(RFCfgReg, (gain<<4));       // Set Rx Gain to max

Into:

  PCD_ClearRegisterBitMask(RFCfgReg, (0x07<<4));
  PCD_SetRegisterBitMask(RFCfgReg, (gain<<4));       // Set Rx Gain

In that customization of PCD_Init; and also consider why byte gain[] is used as parameter... shouldn't that be simply byte gain (and check the prototype declaration in the .h file)?

Now that may help you customization of the library, but I wonder if it should be done this way in the miguelbalboa/rfid version of the library. As the gain can be changed in the Sketch after calling PCD_Init, it doesn't make sense to me to force everybody to consider the extra parameter.

And it might not be correct to assume a proper default in the library... I guess the hardware/chip provides a default if it is not specifically set via the library. So perhaps manufacturers have a specific reason to use their setting as default.

So, what to do? I guess the best would be to provide some #define entries to describe the RxGain table you provided in a comment block. Together with a helper function to set it to one of those values, using the Clear- and SetRegisterBitMask combo approach. Perhaps adding an if-then guard to help preventing accidental out-of-bounds.

@miguelbalboa would you like a PR for that; and if so, can you suggest some function name to use?

from rfid.

mdxs avatar mdxs commented on June 25, 2024

Perhaps something like this:

void MFRC522::PCD_SetAntennaGain(byte mask) {
    if ((mask >= (0x00<<4)) && (mask <= (0x07<<4))) {
        PCD_ClearRegisterBitMask(RFCfgReg, (0x07<<4));
        PCD_SetRegisterBitMask(RFCfgReg, mask);
    }
}

where the if should actually check for some defined values (not using #define as I said before, but rather some enum in the .h part).

from rfid.

mdxs avatar mdxs commented on June 25, 2024

Closed with #47 being merged into the library.

from rfid.

albapa avatar albapa commented on June 25, 2024

Hi, should there be a difference whether PCD_SetAntennaGain() is called before or after PCD_Init()? Thanks in advance.

from rfid.

omersiar avatar omersiar commented on June 25, 2024

Should be, because when you call gain control it modify pcd's specific
register you should init before any other register change.
21 Tem 2015 16:25 tarihinde "albapa" [email protected] yazdı:

Hi, should there be a difference whether PCD_SetAntennaGain() is called
before or after PCD_Init()? Thanks in advance.


Reply to this email directly or view it on GitHub
#43 (comment).

from rfid.

albapa avatar albapa commented on June 25, 2024

Thanks for your reply.

I was asking because in the original version the antenna gain was set in PCD_Init() before calling PCD_AntennaOn(). In the update, if you call PCD_SetAntennaGain() after PCD_Init(), this will naturally happen also after switching on the antenna.

I suppose my question really is whether setting the appropriate register corresponding to maximum gain should happen before or after switching on the antenna or it doesn't matter. Thanks!

from rfid.

YIGITER-CLOAK-AND-DAGGER avatar YIGITER-CLOAK-AND-DAGGER commented on June 25, 2024

When I use the function in my code mfrc522.PCD_SetRegisterBitMask(mfrc522.RFCfgReg, (0x07<<4)) after initializing the reader The RFID reader will misread the codes for the RFID. It doesn't matter the level of gain. 20% of the time it reads it wrong. When I remove it there is no error. I have tested it with multiple types and the error persists.

from rfid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.