HY502B模块前面已经做过简单介绍,能在Arduino下成功读取S50卡的ID信息。现在做更进一步的讨论,把程序代码移植到pcDuino上,通过pcDuino连接HY502B模块看能否实现读卡功能。
接线方法类似与Arduino:
YHY502B使用SPI接口:
J1-1(SCL) —-> pcduino D13
J1-2(MISO) —-> pcduino D12
J1-3(MOSI) —-> pcduinoD11
J1-4(NSS) —-> pcduino D10
J1-7(SIG) —-> pcduino D9
串口屏使用IIC接口:
J1-1(SDA) —-> pcduino SDA
J1-1(SCL) —-> pcduino SCL
连接好之后可以看到串口屏已经亮了,说明电源正常,此时没有显示文字信息。
1、打开LXTerminal进入代码目录
2、编译代码生成可执行文件
3、运行可执行文件
4、运行成功之后串口屏上会出现上图文字信息。拿一张S50卡靠近HY502B,可以看到串口屏上出现了卡得ID信息,所以pcDuino连接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) ;
}
}
}
来源:oschina
链接:https://my.oschina.net/u/1174645/blog/141949