RuiViana

ListaCarCode_ZeroLeft.ino

Oct 22nd, 2018
189
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #define MAX_BITS 100
  3. #define WEIGAND_WAIT_TIME  3000
  4.  
  5. unsigned char databits[MAX_BITS];
  6. unsigned char bitCount;
  7. unsigned char flagDone;
  8. unsigned long weigand_counter;
  9.  
  10. unsigned long facilityCode = 0;
  11. unsigned long cardCode = 0;
  12. //----------------------------------------------------------------
  13. void ISR_INT0()
  14. {
  15.   bitCount++;
  16.   flagDone = 0;
  17.   weigand_counter = WEIGAND_WAIT_TIME;
  18. }
  19. //----------------------------------------------------------------
  20. void ISR_INT1()
  21. {
  22.   databits[bitCount] = 1;
  23.   bitCount++;
  24.   flagDone = 0;
  25.   weigand_counter = WEIGAND_WAIT_TIME;
  26. }
  27. //----------------------------------------------------------------
  28. void setup()
  29. {
  30.   pinMode(13, OUTPUT);  // SAIDA DE LED
  31.   pinMode(2, INPUT);     // DATA0 (INT0)
  32.   pinMode(3, INPUT);     // DATA1 (INT1)
  33.   Serial.begin(9600);
  34.   Serial.println("");
  35.   attachInterrupt(0, ISR_INT0, FALLING);
  36.   attachInterrupt(1, ISR_INT1, FALLING);
  37.   weigand_counter = WEIGAND_WAIT_TIME;
  38. }
  39. //----------------------------------------------------------------
  40. void loop()
  41. {
  42.   if (!flagDone)
  43.   {
  44.     if (--weigand_counter == 0)
  45.       flagDone = 1;
  46.   }
  47.   // NÃO USAR
  48.   if (bitCount > 0 && flagDone)
  49.   {
  50.     unsigned char i;
  51.     Serial.print("");
  52.     Serial.print("");
  53.     Serial.print("");
  54.     if (bitCount == 35)
  55.     {
  56.       for (i = 2; i < 14; i++)
  57.       {
  58.         facilityCode <<= 1;
  59.         facilityCode |= databits[i];
  60.       }
  61.       for (i = 14; i < 34; i++)
  62.       {
  63.         cardCode <<= 1;
  64.         cardCode |= databits[i];
  65.       }
  66.       printBits();
  67.     }
  68.     else if (bitCount == 26)
  69.     {
  70.       for (i = 1; i < 9; i++)
  71.       {
  72.         facilityCode <<= 1;
  73.         facilityCode |= databits[i];
  74.       }
  75.       // card code = bits 10 to 23
  76.       for (i = 9; i < 25; i++)
  77.       {
  78.         cardCode <<= 1;
  79.         cardCode |= databits[i];
  80.       }
  81.       printBits();
  82.     }
  83.     else
  84.     {
  85.       Serial.println("");  //SEM ESPAÇO
  86.     }
  87.     bitCount = 0;
  88.     facilityCode = 0;
  89.     cardCode = 0;
  90.     for (i = 0; i < MAX_BITS; i++)
  91.     {
  92.       databits[i] = 0;
  93.     }
  94.   }
  95. }
  96. // NÃO MOSTAR SEPARAÇÃO
  97. //----------------------------------------------------------------
  98. void printBits()
  99. {
  100.   Serial.print("");
  101.   Serial.print(facilityCode);
  102.   Serial.print("");
  103.   //  Serial.println(cardCode);
  104.   String saida = "";
  105.   String minhaStr = (String)cardCode;
  106.   int meuLgh = minhaStr.length();
  107.   for ( int i = 0; i < 5 - meuLgh; i++)
  108.   {
  109.     saida += "0";
  110.   }
  111.   saida += minhaStr;
  112.   Serial.println(saida);
  113. }
RAW Paste Data