Forum: Mikrocontroller und Digitale Elektronik RFID Reader RC522 bekomme keine Version


von Johannes (Gast)


Lesenswert?

Hallo, ich versuche ein RFID-Reader (RC522) an einem ATMEGA in betrieb 
zu nehmen. Allerdings komme ich bei der Initialisierung schon nicht 
weiter. Ich möchte zunächst die Version abfragen, bekomme dort aber 
immer 0x00 zurückgeschickt. Die Verkabelung habe ich schon öfters 
überprüft, konnte aber keinen Fehler finden.

RFID-Modul - ATMEGA
SDA - PB2 (SS)
SCK - PB5 (SCK)
MOSI - PB3 (MOSI)
MISO - PB4 (MISO)
IRQ - //
GND - GND
RST - PC6 (RESET)
3.3V - 3.3V

Der Atmega wird mit 5V betrieben. Die Massen von 5V und 3.3V sind aber 
miteinander verbunden.

Evtl könnt ihr einen Fehler sehen?

main.c
1
int main(void)
2
{
3
  uint8_t byte;
4
  uint8_t str[MAX_LEN];
5
    uart_init(MYUBRR);
6
  spi_init();
7
  pcd_init();
8
  sei();
9
  
10
  byte = pcd_readRegister(VersionReg);
11
  uart_sendString("Firmware version: ");
12
  switch(byte)
13
  {
14
    case 0x00:
15
    case 0xFF:  uart_sendString("WARNING: Communication failure, is the MFRC522 properly connected?"); break;
16
    case 0x88:  uart_sendString("clone");  break;
17
    case 0x90:  uart_sendString("v0.0");  break;
18
    case 0x91:  uart_sendString("v1.0");  break;
19
    case 0x92:  uart_sendString("v2.0");  break;
20
    case 0x12:  uart_sendString("counterfeit chip");  break;
21
    default:  uart_sendString("No reader found"); break;
22
  }



mfrc522.c
1
#include "../spi/spi.h"
2
#include "mfrc522.h"
3
#include "../defines.h"
4
#include <util/delay.h>
5
6
/**
7
 * Initializes the MFRC522 chip.
8
 */
9
void pcd_init()
10
{
11
  pcd_reset();
12
13
    
14
  // Reset baud rates
15
  pcd_writeRegister(TxModeReg, 0x00);
16
  pcd_writeRegister(RxModeReg, 0x00);
17
  // Reset ModWidthReg
18
  pcd_writeRegister(ModWidthReg, 0x26);
19
20
  pcd_writeRegister(TModeReg, 0x80);      // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds
21
  pcd_writeRegister(TPrescalerReg, 0xA9);    // TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => f_timer=40kHz, ie a timer period of 25?s.
22
  pcd_writeRegister(TReloadRegH, 0x03);    // Reload timer with 0x3E8 = 1000, ie 25ms before timeout.
23
  pcd_writeRegister(TReloadRegL, 0xE8);
24
  
25
  pcd_writeRegister(TxASKReg, 0x40);    // Default 0x00. Force a 100 % ASK modulation independent of the ModGsPReg register setting
26
  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)
27
  pcd_antennaOn();            // Enable the antenna driver pins TX1 and TX2 (they were disabled by the reset)
28
  
29
}
30
31
/**
32
 * Performs a soft reset on the MFRC522 chip and waits for it to be ready again.
33
 */
34
void pcd_reset()
35
{
36
  pcd_writeRegister(CommandReg, PCD_SoftReset);  // Issue the SoftReset command.
37
  // The datasheet does not mention how long the SoftRest command takes to complete.
38
  // But the MFRC522 might have been in soft power-down mode (triggered by bit 4 of CommandReg)
39
  // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74?s. Let us be generous: 50ms.
40
  uint8_t count = 0;
41
  do {
42
    // Wait for the PowerDown bit in CommandReg to be cleared (max 3x50ms)
43
    _delay_ms(50);
44
  } while ((pcd_readRegister(CommandReg) & (1 << 4)) && (++count) < 3);
45
}
46
47
/**
48
 * Writes a byte to the specified register in the MFRC522 chip.
49
 * The interface is described in the datasheet section 8.1.2.
50
 */
51
void pcd_writeRegister(  uint8_t reg,    ///< The register to write to. One of the PCD_Register enums.
52
            uint8_t value    ///< The value to write.
53
) {
54
  ENABLE_CHIP();
55
  spi_transmit(reg);            // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
56
  spi_transmit(value);
57
  DISABLE_CHIP();
58
}
59
60
/**
61
 * Reads a byte from the specified register in the MFRC522 chip.
62
 * The interface is described in the datasheet section 8.1.2.
63
 */
64
uint8_t pcd_readRegister(  uint8_t reg  ///< The register to read from. One of the PCD_Register enums.
65
                ) {
66
  uint8_t value;
67
  ENABLE_CHIP();
68
  spi_transmit(0x80 | reg);          // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3.
69
  value = spi_transmit(0);          // Read the value back. Send 0 to stop reading.
70
  DISABLE_CHIP();
71
  return value;
72
}
73
74
/**
75
 * Turns the antenna on by enabling pins TX1 and TX2.
76
 * After a reset these pins are disabled.
77
 */
78
void pcd_antennaOn() {
79
  uint8_t value = pcd_readRegister(TxControlReg);
80
  if ((value & 0x03) != 0x03) {
81
    pcd_writeRegister(TxControlReg, value | 0x03);
82
  }
83
}
84
85
/**
86
 * Turns the antenna off by disabling pins TX1 and TX2.
87
 */
88
void pcd_antennaOff() {
89
  pcd_clearRegisterBitMask(TxControlReg, 0x03);
90
}

spi.h
1
#ifndef SPI_H_
2
#define SPI_H_
3
4
#include "spi_config.h"
5
6
void spi_init(void);
7
uint8_t spi_transmit(char cData);
8
9
10
#define ENABLE_CHIP() (SPI_PORT &= (~(1<<SPI_SS)))
11
#define DISABLE_CHIP() (SPI_PORT |= (1<<SPI_SS))
12
13
#endif /* SPI_H_ */

spi.c
1
#include "../defines.h"
2
#include <avr/io.h>
3
#include "spi.h"
4
5
void spi_init(void)
6
{
7
  /* Set MOSI SCK and SS output, all others input */
8
  SPI_DDR = (1<<SPI_MOSI)|(1<<SPI_SCK)|(1<<SPI_SS);
9
  /* Enable SPI, Master, set clock rate fck/16 */
10
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
11
}
12
13
uint8_t spi_transmit(char cData)
14
{
15
  /* Start transmission */
16
  SPDR = cData;
17
  /* Wait for transmission complete */
18
  while(!(SPSR & (1<<SPIF)));
19
  return SPDR;
20
}

von Stefan (Gast)


Lesenswert?

Hallo,

>Der Atmega wird mit 5V betrieben. Die Massen von 5V und 3.3V sind aber
miteinander verbunden.

Was ist mit SS, SCK, MOSI, MISO, RESET
Hast du die 1 zu 1 verbunden ???
Ich hab jetzt gerade mal das Datenblatt durchgeschaut und nichts von
5V-tolerant oder dergleichen gefunden.

Eventuell ist der RC522 schon tot !!!

Gruss

von Stefan (Gast)


Lesenswert?

Nochmal ich,

Vorausgesetzt dein RC522 geht noch dann folgende Änderung denn
deine SPI Initialisierung geht so nicht wie du sie gemacht hast.
Probiers mal damit, damit gehts bei mir:
1
  SPCR = (1<< SPE)|(1<<DORD)|(1<<MSTR)|(1<<CPHA),(1<<SPR0);      // CPOL, CPHA, SPR1, SPR0  (mode 1, Fcpu/16 )

Gruss

von Stefan (Gast)


Lesenswert?

Nochmal, da schon späte Stunde

das komma zwischen
(1<<CPHA),(1<<SPR0)
ist natürlich falsch!
1
 SPCR = (1<< SPE)|(1<<DORD)|(1<<MSTR)|(1<<CPHA)|(1<<SPR0);

Gruss

von Stefan (Gast)


Lesenswert?

Hallo,

alles zurück, ich habe gerade bemerkt dass ich aus dem falschen Programm 
kopiert habe. Blöde Sache!

Deine Initialisierung ist schon richtig !!!
Tut mir leid !!!

Gruss Stefan

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.