nano130 小黄板 连接 RFID-RC522模块

nano130 小黄板 连接 RFID-RC522模块

连接方式:
串口:

管脚小黄板号码符号pc端
PC1073RXDTX
PC1172TXDRX

SPI接口

管脚小黄板号码符号颜色RC522管脚
VDD33VDD33VDD333.3v
PB964RST绿RST
GNDGNDGNDGND
不接IRQ
PE366MISOMISO
PE465MOSIMOSI
PE267SCLKSCK
PE168SS0SDA

连接图片:
这里写图片描述

首先需要确认:RC522 芯片特点:
参考文档《MFRC522数据手册.pdf》
SPI 通信的参数:
这里写图片描述

对应RFID-RC522的原理图《SG-RC522射频模块原理图.pdf》:
这里写图片描述

SPI 通信参数

根据以上,可以知道 模块是在硬件电路上设置为SPI通信模式。
下面继续研究 文档《MFRC522数据手册.pdf》第10章Page46
找出SPI的通信参数:
这里写图片描述

文档中的描述:

A serial peripheral interface (SPI compatible) is supported to enable high speed
communication to the host. The SPI Interface can handle data speed of up to 10 Mbit/s. In
the communication with a host MFRC522 acts as a slave receiving data from the external
host for register settings and to send and receive data relevant for the communication on
the RF interface.
SPI 最高速度可以达到10M bit/s
MFRC522作为SPI Slave设备 ,主机可以对RC522 设置寄存器,发送和接收RF接口通信相关的数据

另外一个描述:

On both lines (MOSI, MISO) each data byte is sent by MSB first. Data on MOSI line
should be stable on rising edge of the clock line and can changed on falling edge. The
same is valid for the MISO line. Data is provided by the MFRC522 on falling edge and is
stable during rising edge.
MSB 高位首先发送
MOSI线上的数据在上跳沿的时候,需要保持稳定。在下跳沿的时候,更改数据。

根据以上信息,得到SPI 主机需要设置为:
type0
MSB
片选管脚低有效
通信时钟可以是2Mhz

手动控制SPI 片选端

基本通信设置完毕,高一层的通信流程:

Read Data:

To read out data using the SPI compatible interface the following byte order has to be
used. It is possible to read out up to n-data bytes.
The first sent byte defines both, the mode itself and the address byte.

这里写图片描述

Write Data:

To write data to the MFRC522 using the SPI interface the following byte order has to be
used. It is possible to write out up to n-data bytes by only sending one’s address byte.
The first send byte defines both, the mode itself and the address byte.

这里写图片描述

Address byte

The address byte has to fulfil the following format:
The MSB bit of the first byte defines the used mode. To read data from the MFRC522 the
MSB bit is set to logic 1. To write data to the MFRC522 the MSB bit has to be set to
logic 0. The bits 6 to 1 define the address and the LSB shall be set to logic 0.

这里写图片描述

复位设置:

第17章:主要是关注延时时间

Reset Timing Requirements

The reset signal is filtered by a hysteresis circuit and a spike filter (rejects signals shorter
than 10 ns) before it enters the digital circuit. In order to perform a reset, the signal has to
be low for at least 100 ns.

以上完成之后,怎么验证,MCU单片机与RC522 通过SPI通信成功了呢?

参看手册:Page14
这里写图片描述
Page25:
这里写图片描述

使用如下代码:

//******************************************************************/
//功    能:复位RC522
//返    回: 成功返回MI_OK
//******************************************************************/
char PcdReset()
{
  int ret ;
  RF_POWER_ON                          ;
  RST522_1                             ;
  Delay(1)                             ;
  RST522_0                             ;
  Delay(1)                             ;
  RST522_1                             ;
  Delay(1)                             ;

  ret = ReadRawRC(CommandReg);
  printf("A Rest CommandReg = 0x%x\n",ret);

  WriteRawRC(CommandReg,PCD_RESETPHASE);
  Delay(1)                             ;

  ret = ReadRawRC(ModeReg);
  printf("A Rest ModeReg = 0x%x\n",ret);

  WriteRawRC(ModeReg,0x3D)             ;

  ret = ReadRawRC(ModeReg);
  printf("B Rest ModeReg = 0x%x\n",ret);  

 }

运行结果:

A Rest CommandReg = 0x20

A Rest ModeReg = 0x3f

B Rest ModeReg = 0x3d

ReadRawRC 和WriteRawRC 的由来,找到与datasheet的对应关系:


/////////////////////////////////////////////////////////////////////
//功    能:读RC632寄存器
//参数说明:Address[IN]:寄存器地址
//返    回:读出的值
/////////////////////////////////////////////////////////////////////
unsigned char ReadRawRC(unsigned char Address)
{
     unsigned char i, ucAddr;
     unsigned char ucResult=0;

 //   printf("ReadRawRC:");

 if(SPI_IS_BUSY(SPI0)){
     return  ;
 }    


   // printf(":R_addr:%x",Address);   
   ucAddr = ((Address<<1)&0x7E)|0x80;

    SPI_SET_DATA_WIDTH(SPI0,8);


    RC522_NSS_LOW();

    spiTxData[0] =ucAddr ;

      SPI_WRITE_TX0(SPI0,ucAddr);

    SPI_TRIGGER(SPI0);
    while(SPI_IS_BUSY(SPI0));


    spiTxData[0] = 0xFF;    
    SPI_WRITE_TX0(SPI0,0xFF);

    SPI_TRIGGER(SPI0);
     while(SPI_IS_BUSY(SPI0));

    ucResult = SPI_READ_RX0(SPI0);

    RC522_NSS_HI();   
   // printf(":%x\n",spiRxData[0]);

  //  SYS_Delay(10);
    return ucResult ;

}

另外一个函数:

/////////////////////////////////////////////////////////////////////
//功    能:写RC632寄存器
//参数说明:Address[IN]:寄存器地址
//          value[IN]:写入的值
/////////////////////////////////////////////////////////////////////
void WriteRawRC(unsigned char Address, unsigned char value)
{
    unsigned char i, ucAddr;


  // printf("WriteRawRC:");
  if(SPI_IS_BUSY(SPI0)){
      return  ;
  }    


  // printf(":W_addr:%x",Address); 
   //printf(":%x \n",value);      

    ucAddr = ((Address<<1)&0x7E);

     SPI_SET_DATA_WIDTH(SPI0,8);


    RC522_NSS_LOW();

    spiTxData[0] =ucAddr ;
     SPI_WRITE_TX0(SPI0,ucAddr);

    SPI_TRIGGER(SPI0);
    while(SPI_IS_BUSY(SPI0));


    spiTxData[0] =value ;  
    SPI_WRITE_TX0(SPI0,value); 
    SPI_TRIGGER(SPI0);
    while(SPI_IS_BUSY(SPI0));

    RC522_NSS_HI();    

  //  SYS_Delay(10);

}

对应源码:
https://download.csdn.net/download/wowocpp/10342990

源码是从:
《Realplay】MFRC-522 RC522 RFID射频 IC卡感应模块 送S50复旦卡\TJDZ-RC522射频卡用户使用手册资料Ver_1.0\MSP430F149读写卡参考例程》

移植过来的

注意,从淘宝上买的RFID-RC522模块,买了同样外观的雷同的两种模块,一种能用,一种不能用,具体原因不知。
如果程序不通,有可能是程序不对,也有可能是模块不对。

程序运行log(有空补上)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值