当前位置:   article > 正文

Arduino下HY502B读卡实验

502b,cc

HY502B模块支持SPI接口,数字电路具有双电压工作模式(TTLCOMS),主要在一些计费系统和身份识别读卡器系统中应用,该系列模块功耗低,工作电压范围2.7v5.5v

现在简单介绍下ArduinoHY502B读卡在串口屏上显示ID信息的应用。

HY502B

图片1

图片2

Serial UART 16×2 LCD

图片3

接线方法:

图片4

YHY502B使用SPI接口

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

 

串口屏使用IIC接口

J1-1(SDA) —-> Arduino A4

J1-1(SCL) —-> Arduino A5

 

线路连接好之后,拿一张S50卡贴近HY502B感应区,就会看到串口屏上显示该卡的ID信息了

图片5

程序代码如下:

  1. #include "Wire.h"
  2. #include "LiquidCrystal.h"
  3. #include "string.h"
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. LiquidCrystal lcd(0);
  7. //SPI Bus state definitions
  8. #define SPI_RDY 0xF0 // ready
  9. #define spibusy 0xaa // busy
  10. #define spiread 0xbb // write
  11. #define spiwrite 0xcc // read
  12. #define SCL_0 digitalWrite(13,LOW)
  13. #define SCL_1 digitalWrite(13,HIGH)
  14. #define MISO digitalRead(12)
  15. #define MOSI_0 digitalWrite(11,LOW)
  16. #define MOSI_1 digitalWrite(11,HIGH)
  17. #define NSS_0 digitalWrite(10,LOW)
  18. #define NSS_1 digitalWrite(10,HIGH)
  19. #define SIG digitalRead(9)
  20. #define SUCCESS 0
  21. #define FAILURE 1
  22. uchar g_cReceBuf[10];
  23. uchar ComPWRdwn[] = {0x02, 0x03};
  24. uchar ComAutoSearchCard[] = {0x03, 0x13, 0x01};
  25. uchar ComGetCardSn[] = {0x02, 0x20};
  26. uchar ComHaltCard[] = {0x02, 0x12};
  27. void port_init()
  28. {
  29. pinMode(13,OUTPUT);
  30. pinMode(12,INPUT);
  31. pinMode(11,OUTPUT);
  32. pinMode(10,OUTPUT);
  33. pinMode(9,INPUT);
  34. }
  35. unsigned char SPIRWByte(unsigned char cSendByte)
  36. {
  37. unsigned char i = 8;
  38. unsigned char cRecByte;
  39. while (i--)
  40. {
  41. cRecByte *= 2;
  42. SCL_0;
  43. delayMicroseconds(10);
  44. if((cSendByte & 0x80)==0x80) MOSI_1;
  45. else MOSI_0;
  46. cSendByte *= 2;
  47. cRecByte |= (unsigned char)(MISO);
  48. SCL_1;
  49. delayMicroseconds(10);
  50. }
  51. SCL_1;
  52. return cRecByte;
  53. }
  54. unsigned char spi_cStatus(void)
  55. {
  56. unsigned char cStatus;
  57. NSS_0;
  58. cStatus=SPIRWByte(spibusy);
  59. cStatus=SPIRWByte(0xFF);
  60. NSS_1;
  61. return cStatus;
  62. }
  63. unsigned char SPI_Read(unsigned char *cP)
  64. {
  65. unsigned char cCnt,cStatus;
  66. unsigned char cCheckSum = 0;
  67. for (cCnt=0; cCnt<100; cCnt++)
  68. {
  69. cStatus=spi_cStatus();
  70. if(cStatus==0xF0)
  71. {
  72. cCnt=253;
  73. }
  74. delay(10);
  75. }
  76. if(cCnt==254)
  77. {
  78. NSS_0;
  79. cCnt=SPIRWByte(spiread);
  80. cP[0]=0x01;
  81. for (cCnt=0; cCnt<cP[0]; cCnt++)
  82. {
  83. cP[cCnt] = SPIRWByte(0xFF);
  84. cCheckSum ^= cP[cCnt];
  85. if(cP[0]>32)
  86. {
  87. NSS_1;
  88. return FAILURE;
  89. }
  90. }
  91. cP[cCnt] = SPIRWByte(0xFF);
  92. NSS_1;
  93. if (cCheckSum == cP[cCnt])
  94. {
  95. return SUCCESS;
  96. }
  97. }
  98. return FAILURE;
  99. }
  100. unsigned char SPI_Write(unsigned char *cP)
  101. {
  102. unsigned char i,cStatus;
  103. unsigned char cCheckSum = 0;
  104. NSS_0;
  105. cStatus=SPIRWByte(spiwrite);
  106. for(i=0; i<cP[0]; i++)
  107. {
  108. cCheckSum ^= cP[i];
  109. cStatus=SPIRWByte(cP[i]);
  110. }
  111. cStatus=SPIRWByte(cCheckSum);
  112. NSS_1;
  113. return cStatus;
  114. }
  115. void setup()
  116. {
  117. lcd.begin(16, 2);
  118. lcd.clear();
  119. lcd.print("read card id:");
  120. lcd.setBacklight(HIGH);
  121. port_init();
  122. }
  123. void loop()
  124. {
  125. uchar cStatus,i;
  126. uchar *cPa;
  127. while (1)
  128. {
  129. lcd.setCursor(0,0);
  130. lcd.print("read card id:");
  131. lcd.setCursor(0,1);
  132. lcd.print("no card,waiting.");
  133. if(SIG==LOW)
  134. {
  135. delay(100);
  136. cPa = ComGetCardSn;
  137. SPI_Write(cPa);
  138. delay(100);
  139. cStatus = SPI_Read(g_cReceBuf);
  140. //SPI_Write(ComHaltCard);
  141. lcd.setCursor(0,1);
  142. lcd.print("card ID:");
  143. for(i=2;i<6;i++)
  144. lcd.print(g_cReceBuf[i],HEX);
  145. while(!SIG) ;
  146. lcd.clear();
  147. }
  148. }
  149. }

转载于:https://my.oschina.net/pcduino/blog/141948

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/839657
推荐阅读
相关标签
  

闽ICP备14008679号