在pcDuino上使用RFID读卡模块HY502B

pc朵拉 发布于 2013/11/30 00:35
阅读 508
收藏 2

华为云11月刊推送:DIY微信问答机器人,高性能计算代码的20个技巧!>>>

HY502B是一个14.3Mhz近距离RIFD 读卡模块。它的具体资料可以在 LinkSprite Wiki 上获得。 HY502B通过SPI接口和主机通讯。 由于pcDuino提供了SPI通讯的API, 把HY502B集成到pcDuino应该不是一件难事。
  • 链接图

HY502B_1

  1. J1-1 (SCL)  of  HY502B -> D13 of pcDuino
  2. J1-2 (MISO) of  HY502B -> D12 of pcDuino
  3. J1-3 (MOSI) of HY502B -> D11 of pcDuino
  4. J1-4 (NSS) HY502B -> D10 of pcDuino
  5. J-17 (SIG) HY502B -> D9 of pcDuino

当然,HY502B的 VCC和GND 还要连接到pcDuino上。

我们用一个I2C接口的16×2的LCD屏幕来显示结果。 LCD和pcDuino接线如下:

  1. J1-1(SDA) —> SDA of pcDuino
  2. J1-1(SCL) —>  SCL of pcDuino

同样, LCD的VCC和GND还要连接到pcDuino上。

连接好后,我们给pcDuino上电,可以看到LCD的背光亮了,但是屏幕上面没有字符显示。

  • 获得pcDuino的Arduino风格API

我们需要利用github来获得pcDuino上面的Arduino编程风格的API。

如果你使用的pcDuino没有安装过git, 我们需要安装git:

$sudo apt-get install git

然后,我们用刚刚安装的git来获取pcDuino上面的Arduino编程风格的API:


  • HY502B 的读卡程序

HY502B 的读卡程序如下:

#include "core.h"
#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(2);
if((cSendByte & 0x80)==0x80) MOSI_1;
else MOSI_0;
cSendByte *= 2;
cRecByte |= (unsigned char)(MISO);
SCL_1;
delayMicroseconds(2);
}
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; cCnt<cP[0]; cCnt++)
{
cP[cCnt] = SPIRWByte(0xFF);
cCheckSum ^= cP[cCnt];
if(cP[0]>32)
{
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<cP[0]; i++)
{
cCheckSum ^= cP[i];
cStatus=SPIRWByte(cP[i]);
}
cStatus=SPIRWByte(cCheckSum);
NSS_1;
return cStatus;
}
 
void setup()
{
lcd.begin(16, 2);
lcd.clear();
lcd.print("read card id:");
lcd.setBacklight(HIGH);
port_init();
}
 
void loop()
{
uchar cStatus,i;
uchar *cPa;
while (1)
{
lcd.setCursor(0,0);
lcd.print("read card id:");
lcd.setCursor(0,1);
lcd.print("no card,waiting.");
if(SIG==LOW)
{
delay(100);
cPa = ComGetCardSn;
SPI_Write(cPa);
delay(100);
cStatus = SPI_Read(g_cReceBuf);
//SPI_Write(ComHaltCard);
lcd.setCursor(0,1);
lcd.print("card ID:");
for(i=2;i<6;i++)
lcd.print(g_cReceBuf[i],HEX);
while(!SIG) ;
}
}
}

上面用到的C代码可以在这里(HY502B)下载。

  • 编译
修改\test\Makefile:

 
 
 
 
LIBS=-L../../sample/core -larduino -lspi
INCS=-I../../sample/core/include
TARGET=../../sample/test
 
OBJS = io_test adc_test pwm_test spi_test adxl345_test HY502B
 
all: $(OBJS)
@mkdir -p $(TARGET)
@mv $(OBJS) $(TARGET)
 
io_test: io_test.c
$(CC) $(LIBS) $(INCS) < -o $@
 
HY502B: HY502B.c
$(CC) $(LIBS) $(INCS) < -o $@
 
adc_test: adc_test.c
$(CC) $(LIBS) $(INCS) < -o $@
 
pwm_test: pwm_test.c
$(CC) $(LIBS) $(INCS) < -o $@
 
spi_test: spi_test.c
$(CC) $(LIBS) $(INCS) < -o $@
 
adxl345_test: adxl345_test.c
$(CC) &amp;lt; -o $@
clean:
@for i in $(OBJS); do rm -f $(TARGET)/$$i; done

再打开命令终端编译库文件:

ubuntu@ubuntu:~$ cd c_enviroment

ubuntu@ubuntu:~/c_enviroment $ make

  • 运行

在 test 目录下, 输入 ”$./HY502B":

HY502B_1

帖子出自: http://cnlearn.linksprite.com/?p=368






加载中
返回顶部
顶部