13.56M读卡器开发详解二(RC522驱动程序)

13.56M读卡器开发详解二

1. 硬件接口介绍

根据上一篇的介绍,小编使用了RC522SPI口通信方式和51单片机进行通信。硬件接口设计此处不再附图。只是将接口配置列写如下:

sbit  spi_cs=P1^6;

sbit  spi_ck=P1^5;

sbit  spi_mosi=P1^4;

sbit  spi_miso=P1^3;

sbit  spi_rst=P1^2;

 

#define SET_SPI_CS  spi_cs=1

#define CLR_SPI_CS  spi_cs=0

#define SET_SPI_CK  spi_ck=1

#define CLR_SPI_CK  spi_ck=0

#define SET_SPI_MOSI  spi_mosi=1

#define CLR_SPI_MOSI  spi_mosi=0

#define STU_SPI_MISO  spi_miso

#define SET_RC522RST  spi_rst=1

#define CLR_RC522RST  spi_rst=0

 

2. 51单片机模拟SPI通信函数

/*****************************************************************************

Name        : delay_ns(unsigned int data ns)

Description : 纳秒延时函数

Input       : None

Output      : None

Return      : None

*****************************************************************************/

void delay_ns(unsigned int data ns)

{

  unsigned int xdata i;

  for(i=0;i<ns;i++)

  {

    nop();

    nop();

    nop();

  }

}

/*****************************************************************************

Name        : SPIReadByte(void)

Description : 读SPI数据

Input       : None

Output      : None

Return      : None

*****************************************************************************/

unsigned char SPIReadByte(void)

{

  unsigned char idata SPICount;                   // Counter used to clock out the data

  

  unsigned char idata SPIData;                  

  SPIData = 0;

  for (SPICount = 0; SPICount < 8; SPICount++)  // Prepare to clock in the data to be read

  {

    SPIData <<=1;                               // Rotate the data

    CLR_SPI_CK; nop();nop();                    // Raise the clock to clock the data out of the MAX7456

   if(STU_SPI_MISO)

   {

     SPIData|=0x01;

   }

    SET_SPI_CK;   nop();nop();                  // Drop the clock ready for the next bit

  }                                                             // and loop back

  return (SPIData);                             // Finally return the read data

/*****************************************************************************

Name        : SPIReadByte(void)

Description :写SPI数据

Input       : None

Output      : None

Return      : None

*****************************************************************************/

void SPIWriteByte(unsigned char data SPIData)

{

  unsigned char idata SPICount;                 // Counter used to clock out the data

  for (SPICount = 0; SPICount < 8; SPICount++)

  {

    if (SPIData & 0x80)

    {

      SET_SPI_MOSI;

    }

    else

    {

      CLR_SPI_MOSI;

    } nop();nop();

    CLR_SPI_CK;nop();nop();

    SET_SPI_CK;nop();nop();

    SPIData <<= 1;

  }          

}                          

3.RC522芯片的寄存器、命令字等定义

  3.1命令字等定义

   这些宏定义都是根据RC522数据手册编写的代码。

    

  3.2. RC522寄存器地址定义

 

 

 

4.  Rc522的14443协议代码驱动程序

  4.1寻卡程序

 

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值