nano130 小黄板 连接 RFID-RC522模块
连接方式:
串口:
管脚 | 小黄板号码 | 符号 | pc端 |
---|---|---|---|
PC10 | 73 | RXD | TX |
PC11 | 72 | TXD | RX |
SPI接口
管脚 | 小黄板号码 | 符号 | 颜色 | RC522管脚 |
---|---|---|---|---|
VDD33 | VDD33 | VDD33 | 红 | 3.3v |
PB9 | 64 | RST | 绿 | RST |
GND | GND | GND | 黑 | GND |
不接 | IRQ | |||
PE3 | 66 | MISO | 蓝 | MISO |
PE4 | 65 | MOSI | 紫 | MOSI |
PE2 | 67 | SCLK | 灰 | SCK |
PE1 | 68 | SS0 | 白 | SDA |
连接图片:
首先需要确认: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(有空补上)