Advertisement
antiquekid3

Documation Punched Card Reader Program for Arduino

Dec 12th, 2018
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.99 KB | None | 0 0
  1. #define CARD_OUTLINE 0x01
  2. #define ASCII_PRINT 0x02
  3. #define BINARY_PRINT 0x04
  4. #define REVERSE_PRINT 0x08
  5. #define COUNT_CARDS 0x10
  6.  
  7. //add interrupts
  8.  
  9. uint8_t mode;
  10.  
  11. void setup()
  12. {
  13.   pinMode(2, INPUT); //row 9 PD2
  14.   pinMode(3, INPUT); //row 8 PD3
  15.   pinMode(4, INPUT); //row 7 PD4
  16.   pinMode(5, INPUT); //row 6 PD5
  17.   pinMode(6, INPUT); //row 5 PD6
  18.   pinMode(7, INPUT); //row 4 PD7
  19.   pinMode(A0, INPUT); //row 3 PC0
  20.   pinMode(A1, INPUT); //row 2 PC1
  21.   pinMode(A2, INPUT); //row 1 PC2
  22.   pinMode(A3, INPUT); //row 0 PC3
  23.   pinMode(A4, INPUT); //row 11 PC4
  24.   pinMode(A5, INPUT); //row 12 PC5
  25.  
  26.   pinMode(8, INPUT); //index mark PB0
  27.   pinMode(9, INPUT); //ready PB1
  28.  
  29.   pinMode(10, OUTPUT); //pick PB2
  30.  
  31.   digitalWrite(2, HIGH); //row 9 PD2
  32.   digitalWrite(3, HIGH); //row 8 PD3
  33.   digitalWrite(4, HIGH); //row 7 PD4
  34.   digitalWrite(5, HIGH); //row 6 PD5
  35.   digitalWrite(6, HIGH); //row 5 PD6
  36.   digitalWrite(7, HIGH); //row 4 PD7
  37.   digitalWrite(A0, HIGH); //row 3 PC0
  38.   digitalWrite(A1, HIGH); //row 2 PC1
  39.   digitalWrite(A2, HIGH); //row 1 PC2
  40.   digitalWrite(A3, HIGH); //row 0 PC3
  41.   digitalWrite(A4, HIGH); //row 11 PC4
  42.   digitalWrite(A5, HIGH); //row 12 PC5
  43.  
  44.   digitalWrite(8, HIGH); //index mark PB0
  45.   digitalWrite(9, HIGH); //ready PB1
  46.  
  47.  
  48.   DDRC &= 0x3F;
  49.   DDRD &= 0xFC;
  50.   DDRB &= 0x03;
  51.   DDRB |= 0x04;
  52.  
  53.   Serial.begin(230400);
  54.   digitalWrite(10, HIGH); //pick signal, active low
  55.   delay(500);
  56.   Serial.println("\rPunched Card Reader v1.0");
  57.  
  58.   mode = CARD_OUTLINE | ASCII_PRINT | COUNT_CARDS;
  59.  
  60.   Serial.println("Setup:");
  61.   Serial.print("Print outline? [Y/n] ");
  62.   while (Serial.available() <= 0);
  63.   if (Serial.read() == 'n')
  64.   {
  65.     mode &= ~CARD_OUTLINE;
  66.     Serial.println('n');
  67.   }
  68.   else
  69.     Serial.println('y');
  70.   Serial.print("Print ASCII? [Y/n] ");
  71.   while (Serial.available() <= 0);
  72.   if (Serial.read() == 'n')
  73.   {
  74.     mode &= ~ASCII_PRINT;
  75.     Serial.println('n');
  76.   }
  77.   else
  78.     Serial.println('y');
  79.   Serial.print("Count cards? [Y/n] ");
  80.   while (Serial.available() <= 0);
  81.   if (Serial.read() == 'n')
  82.   {
  83.     mode &= ~COUNT_CARDS;
  84.     Serial.println('n');
  85.   }
  86.   else
  87.     Serial.println('y');
  88.   Serial.print("Print binary? [y/N] ");
  89.   while (Serial.available() <= 0);
  90.   if (Serial.read() == 'y')
  91.   {
  92.     mode |= BINARY_PRINT;
  93.     Serial.println('y');
  94.   }
  95.   else
  96.     Serial.println('n');
  97.   Serial.print("Print reverse? [y/N] ");
  98.   while (Serial.available() <= 0);
  99.   if (Serial.read() == 'y')
  100.   {
  101.     mode |= REVERSE_PRINT;
  102.     Serial.println('y');
  103.   }
  104.   else
  105.     Serial.println('n');
  106.   Serial.println("End of setup.");
  107.   Serial.println();
  108. }
  109.  
  110. uint16_t card_data[80];
  111.  
  112. uint8_t translate_ascii[] = " &-0123456789ABCDEFGHIJKLMNOPQR/STUVWXYZ:#@\'=\"[.<(+|]$*);^\\,%_>?";
  113. uint16_t translate_data[] =
  114. {
  115.   0x0, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1,
  116.   0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802, 0x801,
  117.   0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404, 0x402, 0x401,
  118.   0x300, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204, 0x202, 0x201,
  119.   0x82, 0x42, 0x22, 0x12, 0xA, 0x6,
  120.   0x882, 0x842, 0x822, 0x812, 0x80A, 0x806,
  121.   0x482, 0x442, 0x422, 0x412, 0x40A, 0x406,
  122.   0x282, 0x242, 0x222, 0x212, 0x20A, 0x206
  123. };
  124.  
  125. uint8_t read_card = 0;
  126.  
  127. uint32_t card_count = 0;
  128.  
  129. void loop()
  130. {
  131.   if (Serial.read() != -1) //toggle reading if a key is pressed
  132.   {
  133.     read_card = !read_card;
  134.     if (read_card)
  135.       Serial.println("Starting read...");
  136.     else
  137.       Serial.println("Stopping read...");
  138.   }
  139.   if ((mode & CARD_OUTLINE) == 0)
  140.     delay(10);
  141.   if (read_card && !(PINB & 0x2)) //if reader is ready...
  142.   {
  143.     PORTB &= ~0x04; //pick a card
  144.     delayMicroseconds(5);
  145.     PORTB |= 0x04; //turn off pick
  146.  
  147.     if (mode & COUNT_CARDS)
  148.     {
  149.       Serial.print(++card_count);
  150.       Serial.println(':');
  151.     }
  152.  
  153.     for (int i = 0; i < 80; i++)
  154.     {
  155.       while (PINB & 0x1); //wait for index pulse to go low
  156.       delayMicroseconds(200); //wait for transients to settle
  157.       card_data[i] = (~(((PIND & 0xFC) >> 2) | ((PINC & 0x3F) << 6))) & 0xFFF; //read data
  158.       delayMicroseconds(200); //delay a bit to help eliminate transient noise
  159.     }
  160.     //print card outline
  161.     if (mode & BINARY_PRINT)
  162.     {
  163.       for (int i = 0; i < 80; i++)
  164.       {
  165.         if (mode & REVERSE_PRINT)
  166.           Serial.print(card_data[i], HEX);
  167.         else
  168.           Serial.print(card_data[79 - i], HEX);
  169.         Serial.print(' ');
  170.       }
  171.       Serial.println();
  172.     }
  173.     if (mode & CARD_OUTLINE)
  174.     {
  175.       Serial.print("   ");
  176.       for (int i = 0; i < 81; i++)
  177.         Serial.print('_');
  178.       Serial.println();
  179.       Serial.print("  /");
  180.     }
  181.     if (mode & ASCII_PRINT)
  182.     {
  183.       for (int i = 0; i < 80; i++) //print ASCII conversion
  184.       {
  185.         char match = 0;
  186.         for (int j = 0; j < sizeof(translate_data) / sizeof(translate_data[0]); j++)
  187.         {
  188.           int k;
  189.           if (mode & REVERSE_PRINT)
  190.             k = 79 - i;
  191.           else
  192.             k = i;
  193.           if (card_data[k] == translate_data[j])
  194.           {
  195.             match = 1;
  196.             Serial.print(char(translate_ascii[j]));
  197.             break;
  198.           }
  199.         }
  200.         if (!match)
  201.           Serial.print('~'); //print '~' for invalid character
  202.       }
  203.     }
  204.     if (mode & CARD_OUTLINE)
  205.     {
  206.       Serial.println('|');
  207.       for (int i = 11; i >= 0; i--) //print punched card punches
  208.       {
  209.         if (i == 11)
  210.           Serial.print(" / "); //print card notch
  211.         else if (i == 10)
  212.           Serial.print("/  "); //print card notch
  213.         else
  214.           Serial.print("|  "); //print card outline
  215.         for (int j = 0; j < 80; j++)
  216.         {
  217.           int k;
  218.           if (mode & REVERSE_PRINT)
  219.             k = 79 - j;
  220.           else
  221.             k = j;
  222.           if (card_data[k] & (1 << i))
  223.             Serial.print('O');
  224.           else
  225.             Serial.print(' ');
  226.         }
  227.         if (mode & CARD_OUTLINE)
  228.           Serial.println('|');
  229.       }
  230.     }
  231.     else
  232.       Serial.println();
  233.   }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement