您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

main.ino 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <Arduino.h>
  2. #include <pins_arduino.h>
  3. #include <SoftwareSerial.h>
  4. #include <SCLib.h>
  5. #define ledPin 13
  6. #define READER_VCC
  7. #define READER_RST 4
  8. #define READER_CLK 3
  9. #define READER_GND
  10. #define READER_VPP
  11. #define READER_IO 2
  12. #define CARD_VCC 11
  13. #define CARD_RST 7
  14. #define CARD_CLK 9
  15. #define CARD_GND
  16. #define CARD_VPP
  17. #define CARD_IO 10
  18. #define CARD_PRE 12
  19. SmartCardReader cardReader(CARD_IO, CARD_RST, CARD_VCC, CARD_PRE, CARD_CLK, true);
  20. uint8_t atr[MAX_ATR_BYTES];
  21. int atr_size;
  22. bool readerReset = false;
  23. int baudRate = -1;
  24. int etu = -1;
  25. #ifndef digitalPinToInterrupt
  26. int digitalPinToInterrupt(int pin)
  27. {
  28. if (pin == 2)
  29. {
  30. return INT0;
  31. }
  32. else if (pin == 3)
  33. {
  34. return INT1;
  35. }
  36. return -1;
  37. }
  38. #endif
  39. void onReaderReset()
  40. {
  41. readerReset = true;
  42. }
  43. void computeEtu()
  44. {
  45. baudRate = 9600;
  46. etu = 1000000 / baudRate;
  47. }
  48. void sendAtrByte(char byte)
  49. {
  50. unsigned long nextBitTime = micros() + (2 * etu);
  51. digitalWrite(READER_IO, HIGH);
  52. int bitNumber = 0;
  53. int parity = 0;
  54. while (bitNumber < 11)
  55. {
  56. if (nextBitTime <= micros())
  57. {
  58. if (bitNumber == 0)
  59. {
  60. digitalWrite(READER_IO, LOW);
  61. }
  62. else if (bitNumber < 9)
  63. {
  64. int a = bitRead(byte, bitNumber - 1);
  65. parity += a;
  66. digitalWrite(READER_IO, a);
  67. }
  68. else if (bitNumber == 9)
  69. {
  70. digitalWrite(READER_IO, parity % 2);
  71. }
  72. else if (bitNumber == 10)
  73. {
  74. digitalWrite(READER_IO, HIGH);
  75. break;
  76. }
  77. nextBitTime += etu;
  78. ++bitNumber;
  79. }
  80. }
  81. }
  82. void sendAtr(const uint8_t* atr, size_t size)
  83. {
  84. pinMode(READER_IO, OUTPUT);
  85. for (int i = 0; i < size; ++i)
  86. {
  87. sendAtrByte(atr[i]);
  88. delayMicroseconds(4*etu);
  89. }
  90. }
  91. void setup() {
  92. Serial.begin(9600);
  93. Serial.println("Initializing...");
  94. pinMode(READER_CLK, INPUT);
  95. pinMode(READER_RST, INPUT);
  96. pinMode(READER_IO, OUTPUT);
  97. digitalWrite(READER_IO, HIGH);
  98. pinMode(ledPin, OUTPUT);
  99. digitalWrite (ledPin, LOW);
  100. Serial.println("Initialized");
  101. atr_size = cardReader.activate(atr, sizeof(atr));
  102. cardReader.deactivate();
  103. }
  104. void loop() {
  105. if (digitalRead(READER_RST))
  106. {
  107. if (!readerReset)
  108. {
  109. // atr_size = cardReader.activate(atr, sizeof(atr));
  110. // uint8_t atr[] = {0x3B, 0x65, 0x00, 0x00, 0x20, 0x63, 0xCB, 0x6C, 0x80};
  111. // int atr_size = sizeof(atr);
  112. computeEtu();
  113. sendAtr(atr, atr_size);
  114. readerReset = true;
  115. digitalWrite(ledPin, !digitalRead(ledPin));
  116. // cardReader.deactivate();
  117. }
  118. }
  119. else
  120. {
  121. if (readerReset) {
  122. readerReset = false;
  123. }
  124. }
  125. }