You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.ino 2.5KB

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