• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeRFIDHow to use the RFID reader module HY502B with Ardu ...
Previous Next

How to use the RFID reader module HY502B with Arduino

Posted by: Alvin Jin , January 16, 2014

Original: http://www.cutedigi.com/blog/?p=66

The RFID reader HY502B has SPI interface. In the following,we are going to show how to use HY502B with Arduino and display the ID obtained on the I2C serial LCD.

Experimental Setup:

图片4

 

Wiring Instructions:

YHY502B uses SPI interface.

  • J1-1(SCL)  —-> Arduino D13
  • J1-2(MISO) —-> Arduino D12
  • J1-3(MOSI) —-> Arduino D11
  • J1-4(NSS)  —-> Arduino D10
  • J1-7(SIG)  —-> Arduino D9

I2C 16×2 LCD screen is connected to Arduino using I2C:

  • J1-1(SDA) —-> Arduino A4
  • J1-1(SCL) —-> Arduino A5

Download the code which is shown in the bottom of this post, the ID will be shown on the 16×2 LCD screen:

 

图片5

#include "Wire.h"
#include "LiquidCrystal.h"
#include "string.h"
#define uchar unsigned char
#define uint unsigned int
LiquidCrystal lcd(0);

//SPI Bus state definitions
#define SPI_RDY 0xF0 // ready
#define spibusy 0xaa // busy
#define spiread 0xbb // write
#define spiwrite 0xcc // read

#define SCL_0 digitalWrite(13,LOW)
#define SCL_1 digitalWrite(13,HIGH)
#define MISO digitalRead(12)
#define MOSI_0 digitalWrite(11,LOW)
#define MOSI_1 digitalWrite(11,HIGH)
#define NSS_0 digitalWrite(10,LOW)
#define NSS_1 digitalWrite(10,HIGH)
#define SIG digitalRead(9)

#define SUCCESS 0
#define FAILURE 1
uchar g_cReceBuf[10];
uchar ComPWRdwn[] = {0x02, 0x03};
uchar ComAutoSearchCard[] = {0x03, 0x13, 0x01};
uchar ComGetCardSn[] = {0x02, 0x20};
uchar ComHaltCard[] = {0x02, 0x12};

void port_init()
{
pinMode(13,OUTPUT);
pinMode(12,INPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,INPUT);
}

unsigned char SPIRWByte(unsigned char cSendByte)
{
unsigned char i = 8;
unsigned char cRecByte;
while (i--)
{
cRecByte *= 2;
SCL_0;
delayMicroseconds(10);
if((cSendByte & 0x80)==0x80) MOSI_1;
else MOSI_0;
cSendByte *= 2;
cRecByte |= (unsigned char)(MISO);
SCL_1;
delayMicroseconds(10);
}
SCL_1;
return cRecByte;
}

unsigned char spi_cStatus(void)
{
unsigned char cStatus;
NSS_0;
cStatus=SPIRWByte(spibusy);
cStatus=SPIRWByte(0xFF);
NSS_1;
return cStatus;
}

unsigned char SPI_Read(unsigned char *cP)
{
unsigned char cCnt,cStatus;
unsigned char cCheckSum = 0;
for (cCnt=0; cCnt<100; cCnt++)
{
cStatus=spi_cStatus();
if(cStatus==0xF0)
{
cCnt=253;
}
delay(10);
}

if(cCnt==254)
{
NSS_0;
cCnt=SPIRWByte(spiread);
cP[0]=0x01;
for (cCnt=0; cCnt32)
{
NSS_1;
return FAILURE;
}
}
cP[cCnt] = SPIRWByte(0xFF);
NSS_1;
if (cCheckSum == cP[cCnt])
{
return SUCCESS;
}
}
return FAILURE;
}

unsigned char SPI_Write(unsigned char *cP)
{
unsigned char i,cStatus;
unsigned char cCheckSum = 0;
NSS_0;
cStatus=SPIRWByte(spiwrite);
for(i=0; i

 

Tags: RFID

Share!
Tweet

Alvin Jin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors