#include #include #include #define ledPin 13 #define READER_VCC #define READER_RST 4 #define READER_CLK 3 #define READER_GND #define READER_VPP #define READER_IO 2 #define CARD_VCC #define CARD_RST #define CARD_CLK #define CARD_GND #define CARD_VPP #define CARD_IO bool readerReset = false; int baudRate = -1; int etu = -1; int digitalPinToInterrupt(int pin) { if (pin == 2) { return INT0; } else if (pin == 3) { return INT1; } return -1; } void onReaderReset() { readerReset = true; } void computeEtu() { baudRate = 9600; etu = 1000000 / baudRate; } void sendAtrByte(char byte) { unsigned long nextBitTime = micros() + (2 * etu); digitalWrite(READER_IO, HIGH); int bitNumber = 0; int parity = 0; while (bitNumber < 11) { if (nextBitTime <= micros()) { if (bitNumber == 0) { digitalWrite(READER_IO, LOW); } else if (bitNumber < 9) { int a = bitRead(byte, bitNumber - 1); parity += a; digitalWrite(READER_IO, a); } else if (bitNumber == 9) { digitalWrite(READER_IO, parity % 2); } else if (bitNumber == 10) { digitalWrite(READER_IO, HIGH); break; } nextBitTime += etu; ++bitNumber; } } } void sendAtr() { const char atr[] = {0x3B, 0x65, 0x00, 0x00, 0x20, 0x63, 0xCB, 0x6C, 0x81}; int size = sizeof(atr); pinMode(READER_IO, OUTPUT); for (int i = 0; i < size; ++i) { sendAtrByte(atr[i]); delayMicroseconds(4*etu); } } void setup() { Serial.begin(9600); Serial.println("Initializing..."); pinMode(READER_CLK, INPUT); pinMode(READER_RST, INPUT); pinMode(READER_IO, OUTPUT); digitalWrite(READER_IO, HIGH); pinMode(ledPin, OUTPUT); digitalWrite (ledPin, LOW); Serial.println("Initialized"); } void delayMs(unsigned int ms) { for (unsigned i = 0; i < ms; i++) { delayMicroseconds(1000); } } void loop() { if (digitalRead(READER_RST)) { if (!readerReset) { computeEtu(); sendAtr(); readerReset = true; digitalWrite(ledPin, !digitalRead(ledPin)); } } else { if (readerReset) { readerReset = false; } } }