RFID RC522 with SPI and ATMega32A initialisation

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear All,

I am working in a project where ATMega32A Microcontroller(without any external crystal) is used with RFID RC522 and 1602A LCD Display. Programmer used USBasp. 

using Atmel Studio 7.0 for compilation and avrdude in DOS prompt for transferring the hex file to ATMega32A.

From Internet I got MIFARE-RFID-with-AVR-master and utilised the same with required modifications.

What I find is that mfrc522_init(); is not giving the desired output, it is giving 0x00 and unable to detect the Reader.

 

If I bypass mfrc522_init(); then it is detecting the version of the RFID.

 

Any guidance / help will be highly appreciated.

 

 

 

 

 

 

st

Last Edited: Thu. Apr 4, 2019 - 08:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SubrataT wrote:

What I find is that mfrc522_init(); is not giving the desired output, it is giving 0x00 and unable to detect the Reader.

 

If I bypass mfrc522_init(); then it is detecting the version of the RFID.

Looks like a missing semi-colon on line 47 ?

 

Seriously, how could anyone possibly help when you aren't showing any code, schematic/wiring or any other details?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your prompt reply.

Sorry, for not providing the complete information.

However, Now I am attaching the files for your understanding. 

main.c is the main file.

As layout diagram is not prepared at this moment, I am attaching connection information in a pdf file.

Thanks

st 

Attachment(s): 

st

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You may need a small delay (NOP or two) between when the CS (NSS) line is changed (ENABLE_CHIP()) to when you start sending SPI data in order to meet the setup time as per the DS.

Check for any timing constraints needed during the init register function as that seems to be your issue.

 

Jim

 

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Jim,

Sorry for the delayed response.
As per your advice I updated the code.
I have observed that within the function mfrc522_init(); (available in mfrc522.c ) all the register loadings are happening properly. I have confirmed it by invoking read function just before and after invoking write function.

code snippet are given below.

void mfrc522_init()
{
    uint8_t byte;
    mfrc522_reset();
    
    mfrc522_write(TModeReg, 0x8D);
    mfrc522_write(TPrescalerReg, 0x3E);
    mfrc522_write(TReloadReg_1, 30);   
    mfrc522_write(TReloadReg_2, 0);    
    mfrc522_write(TxASKReg, 0x40);    
    mfrc522_write(ModeReg, 0x3D);
    
    byte = mfrc522_read(TxControlReg);
    if(!(byte&0x03))
    {
        mfrc522_write(TxControlReg,byte|0x03);
        byte = mfrc522_read(TxControlReg);  // without this reader is not detected
    }

    
}

void mfrc522_write(uint8_t reg, uint8_t data)
{
    ENABLE_CHIP();
    //_NOP();
    spi_transmit((reg<<1)&0x7E);
    spi_transmit(data);
    DISABLE_CHIP();
}

uint8_t mfrc522_read(uint8_t reg)
{
    uint8_t data;    
    ENABLE_CHIP();
    //_NOP();
    spi_transmit(((reg<<1)&0x7E)|0x80);
    data = spi_transmit(0x00);
    DISABLE_CHIP();
    return data;
}

Observations are given below:
1. byte = mfrc522_read(TxControlReg); within the if statement is the additional line I have introduced. without this line the statement byte = mfrc522_read(VersionReg); in the main.c is returning 0x00 and the reader is not detected. so with the present code the VersionReg is read properly and it is returning 0x92 and reader is being detected properly.

2. after executing 
    byte = mfrc522_read(TxControlReg);
    
    TxControlReg value was 0x80. This I have displayed in the LCD.
    Now after executing the following 
    
    if(!(byte&0x03))
    {
        mfrc522_write(TxControlReg,byte|0x03);

    the TxControlReg value, instead of showing 0x83, it is showing 0x00. 
    
    That means the TxControlReg is not setting as desired. as per datasheet it is necessary.
    
     
 As per your advice I introduced _NOP(); after ENABLE_CHIP(); in both write and read functions and observed that it is not making any difference hence afterwards I commented those lines.
 
 
 Updated main.c and mfrc522.c for detecting card are attached.
 Other files will remain as it was earlier.
 

Thanks

ST
 

Attachment(s): 

st

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SubrataT wrote:

Hi Jim,

Sorry for the delayed response.
As per your advice I updated the code.
I have observed that within the function mfrc522_init(); (available in mfrc522.c ) all the register loadings are happening properly. I have confirmed it by invoking read function just before and after invoking write function.

code snippet are given below.

void mfrc522_init()
{
    uint8_t byte;
    mfrc522_reset();
    
    mfrc522_write(TModeReg, 0x8D);
    mfrc522_write(TPrescalerReg, 0x3E);
    mfrc522_write(TReloadReg_1, 30);   
    mfrc522_write(TReloadReg_2, 0);    
    mfrc522_write(TxASKReg, 0x40);    
    mfrc522_write(ModeReg, 0x3D);
    
    byte = mfrc522_read(TxControlReg);
    if(!(byte&0x03))
    {
        mfrc522_write(TxControlReg,byte|0x03);
        byte = mfrc522_read(TxControlReg);  // without this reader is not detected
    }

    
}

void mfrc522_write(uint8_t reg, uint8_t data)
{
    ENABLE_CHIP();
    //_NOP();
    spi_transmit((reg<<1)&0x7E);
    spi_transmit(data);
    DISABLE_CHIP();
}

uint8_t mfrc522_read(uint8_t reg)
{
    uint8_t data;    
    ENABLE_CHIP();
    //_NOP();
    spi_transmit(((reg<<1)&0x7E)|0x80);
    data = spi_transmit(0x00);
    DISABLE_CHIP();
    return data;
}

Observations are given below:
1. byte = mfrc522_read(TxControlReg); within the if statement is the additional line I have introduced. without this line the statement byte = mfrc522_read(VersionReg); in the main.c is returning 0x00 and the reader is not detected. so with the present code the VersionReg is read properly and it is returning 0x92 and reader is being detected properly.

2. after executing 
    byte = mfrc522_read(TxControlReg);
    
    TxControlReg value was 0x80. This I have displayed in the LCD.
    Now after executing the following 
    
    if(!(byte&0x03))
    {
        mfrc522_write(TxControlReg,byte|0x03);

    the TxControlReg value, instead of showing 0x83, it is showing 0x00. 
    
    That means the TxControlReg is not setting as desired. as per datasheet it is necessary.
    
     
 As per your advice I introduced _NOP(); after ENABLE_CHIP(); in both write and read functions and observed that it is not making any difference hence afterwards I commented those lines.
 
 
 Updated main.c and mfrc522.c for detecting card are attached.
 Other files will remain as it was earlier.
 

Thanks

ST
 

 

I am still stuck at the same location.

I have tried to update TxControlReg with different values but no change it is still showing 0x00 after update and 0x80 before update.

Does it indicate that the Reader has become defective?

I have done the self test and it is showing correct values.

Is there any other method of testing the RFID522 Reader? I have the value of ErrorReg also it is showing 0x00;

Any help will be highly appreciated.

st

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you disconnect your programmer when you test the code? Take a picture of your hardware setup and post it here.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

Do you disconnect your programmer when you test the code? Take a picture of your hardware setup and post it here.

 

Result is same whether I keep the usbasp connected or get the power source separately. In case of separately powered I take the power from 9V DC and the use 7805 for getting +5V. and +3.3V for RFID 522 is derived from resistor divider (three 10K ohm resistances are connected in series across +5V  and the potential across two resistances and ground is given to feed RFID522). A 3.6V zener has been used to feed RFID522.

Pictures of both types of connections are attached.

Diagram is not readily available now but pin to pin connection char is attached.

Kindly intimate any further requirement.

Attachment(s): 

st

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Electrically you have some problems. Firstly, find the datasheet for a 7805 and add the required capacitors. Secondly, your method of deriving the 3.3V for he rc522 is also inadequate. Use a regulator ic like a lm1117-3 along with the required capacitors. Next add some 100nF capacitors across the power to the mega32. Hopefully that should sort out the power situation and your circuit has a much better chance of working.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Electrically you have some problems. Firstly, find the datasheet for a 7805 and add the required capacitors. Secondly, your method of deriving the 3.3V for he rc522 is also inadequate. Use a regulator ic like a lm1117-3 along with the required capacitors. Next add some 100nF capacitors across the power to the mega32. Hopefully that should sort out the power situation and your circuit has a much better chance of working.

 

Thank you Mr. Kartman.

I have added the components as per your observation and it worked perfectly.

Many Thanks to you for your help.

It's now working fine.

My sincere thanks to clawson and kiobk also for their guidance & suggestion.

 

 

st

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We've seen a number of people here make the same mistakes. Is there some website that shows a 7805 ( or just about any other linear regular chip) without capacitors? If so, tell us so we can give it some negative feedback. Most regulators really need the capacitors otherwise they become oscillators, not regulators - this can really ruin your day as you can't see it.

 

As you've observed, things tend to work much better with the required components.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Could you say me what you specifically changed in the circuit please? I think that a have the same problem but i'm not sure. Thank you :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that you're replying to a post that is very nearly a year old.

 

Isbel24 wrote:
Could you say me what you specifically changed in the circuit please?

surely, that is stated very clearly in the post you replied to (#10) ?

 

  • Kartman said what's missing;
  • SubrataT  said (s)he'd added them.

 

In reply to that, Kartman further explained - in #11.

 

For details of how to properly connect-up your particular regulator, see its datasheet.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i just want to know if deriving the voltage for the RC522 was the problem

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As noted,  SubrataT was asked for feedback a year ago, when this thread was active.

(s)he never responded then - so unlikely to now ...

 

Probably better to start your own thread describing what you actually have - with schematics & photos - and what problem(s) you are experiencing, and what you've done so far in attempting to resolve them.

 

See the tips in my signature for posting assistance.

 

If you do start a new thread, post a link here so that people know where it is.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isbel24 wrote:
Here it is.

I wrote:
describing what you actually have - with schematics & photos - and what problem(s) you are experiencing, and what you've done so far in attempting to resolve them.

and you did none of that.

 

so it got locked.

 

frown

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What's going on? What am I doing wrong?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isbel24 wrote:
What's going on? What am I doing wrong?

 

Starting another thread in one language while tacking on to this one in another us a start.

 

Stick with this thread...

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user