Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CARD_OUTLINE 0x01
- #define ASCII_PRINT 0x02
- #define BINARY_PRINT 0x04
- #define REVERSE_PRINT 0x08
- #define COUNT_CARDS 0x10
- //add interrupts
- uint8_t mode;
- void setup()
- {
- pinMode(2, INPUT); //row 9 PD2
- pinMode(3, INPUT); //row 8 PD3
- pinMode(4, INPUT); //row 7 PD4
- pinMode(5, INPUT); //row 6 PD5
- pinMode(6, INPUT); //row 5 PD6
- pinMode(7, INPUT); //row 4 PD7
- pinMode(A0, INPUT); //row 3 PC0
- pinMode(A1, INPUT); //row 2 PC1
- pinMode(A2, INPUT); //row 1 PC2
- pinMode(A3, INPUT); //row 0 PC3
- pinMode(A4, INPUT); //row 11 PC4
- pinMode(A5, INPUT); //row 12 PC5
- pinMode(8, INPUT); //index mark PB0
- pinMode(9, INPUT); //ready PB1
- pinMode(10, OUTPUT); //pick PB2
- digitalWrite(2, HIGH); //row 9 PD2
- digitalWrite(3, HIGH); //row 8 PD3
- digitalWrite(4, HIGH); //row 7 PD4
- digitalWrite(5, HIGH); //row 6 PD5
- digitalWrite(6, HIGH); //row 5 PD6
- digitalWrite(7, HIGH); //row 4 PD7
- digitalWrite(A0, HIGH); //row 3 PC0
- digitalWrite(A1, HIGH); //row 2 PC1
- digitalWrite(A2, HIGH); //row 1 PC2
- digitalWrite(A3, HIGH); //row 0 PC3
- digitalWrite(A4, HIGH); //row 11 PC4
- digitalWrite(A5, HIGH); //row 12 PC5
- digitalWrite(8, HIGH); //index mark PB0
- digitalWrite(9, HIGH); //ready PB1
- DDRC &= 0x3F;
- DDRD &= 0xFC;
- DDRB &= 0x03;
- DDRB |= 0x04;
- Serial.begin(230400);
- digitalWrite(10, HIGH); //pick signal, active low
- delay(500);
- Serial.println("\rPunched Card Reader v1.0");
- mode = CARD_OUTLINE | ASCII_PRINT | COUNT_CARDS;
- Serial.println("Setup:");
- Serial.print("Print outline? [Y/n] ");
- while (Serial.available() <= 0);
- if (Serial.read() == 'n')
- {
- mode &= ~CARD_OUTLINE;
- Serial.println('n');
- }
- else
- Serial.println('y');
- Serial.print("Print ASCII? [Y/n] ");
- while (Serial.available() <= 0);
- if (Serial.read() == 'n')
- {
- mode &= ~ASCII_PRINT;
- Serial.println('n');
- }
- else
- Serial.println('y');
- Serial.print("Count cards? [Y/n] ");
- while (Serial.available() <= 0);
- if (Serial.read() == 'n')
- {
- mode &= ~COUNT_CARDS;
- Serial.println('n');
- }
- else
- Serial.println('y');
- Serial.print("Print binary? [y/N] ");
- while (Serial.available() <= 0);
- if (Serial.read() == 'y')
- {
- mode |= BINARY_PRINT;
- Serial.println('y');
- }
- else
- Serial.println('n');
- Serial.print("Print reverse? [y/N] ");
- while (Serial.available() <= 0);
- if (Serial.read() == 'y')
- {
- mode |= REVERSE_PRINT;
- Serial.println('y');
- }
- else
- Serial.println('n');
- Serial.println("End of setup.");
- Serial.println();
- }
- uint16_t card_data[80];
- uint8_t translate_ascii[] = " &-0123456789ABCDEFGHIJKLMNOPQR/STUVWXYZ:#@\'=\"[.<(+|]$*);^\\,%_>?";
- uint16_t translate_data[] =
- {
- 0x0, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1,
- 0x900, 0x880, 0x840, 0x820, 0x810, 0x808, 0x804, 0x802, 0x801,
- 0x500, 0x480, 0x440, 0x420, 0x410, 0x408, 0x404, 0x402, 0x401,
- 0x300, 0x280, 0x240, 0x220, 0x210, 0x208, 0x204, 0x202, 0x201,
- 0x82, 0x42, 0x22, 0x12, 0xA, 0x6,
- 0x882, 0x842, 0x822, 0x812, 0x80A, 0x806,
- 0x482, 0x442, 0x422, 0x412, 0x40A, 0x406,
- 0x282, 0x242, 0x222, 0x212, 0x20A, 0x206
- };
- uint8_t read_card = 0;
- uint32_t card_count = 0;
- void loop()
- {
- if (Serial.read() != -1) //toggle reading if a key is pressed
- {
- read_card = !read_card;
- if (read_card)
- Serial.println("Starting read...");
- else
- Serial.println("Stopping read...");
- }
- if ((mode & CARD_OUTLINE) == 0)
- delay(10);
- if (read_card && !(PINB & 0x2)) //if reader is ready...
- {
- PORTB &= ~0x04; //pick a card
- delayMicroseconds(5);
- PORTB |= 0x04; //turn off pick
- if (mode & COUNT_CARDS)
- {
- Serial.print(++card_count);
- Serial.println(':');
- }
- for (int i = 0; i < 80; i++)
- {
- while (PINB & 0x1); //wait for index pulse to go low
- delayMicroseconds(200); //wait for transients to settle
- card_data[i] = (~(((PIND & 0xFC) >> 2) | ((PINC & 0x3F) << 6))) & 0xFFF; //read data
- delayMicroseconds(200); //delay a bit to help eliminate transient noise
- }
- //print card outline
- if (mode & BINARY_PRINT)
- {
- for (int i = 0; i < 80; i++)
- {
- if (mode & REVERSE_PRINT)
- Serial.print(card_data[i], HEX);
- else
- Serial.print(card_data[79 - i], HEX);
- Serial.print(' ');
- }
- Serial.println();
- }
- if (mode & CARD_OUTLINE)
- {
- Serial.print(" ");
- for (int i = 0; i < 81; i++)
- Serial.print('_');
- Serial.println();
- Serial.print(" /");
- }
- if (mode & ASCII_PRINT)
- {
- for (int i = 0; i < 80; i++) //print ASCII conversion
- {
- char match = 0;
- for (int j = 0; j < sizeof(translate_data) / sizeof(translate_data[0]); j++)
- {
- int k;
- if (mode & REVERSE_PRINT)
- k = 79 - i;
- else
- k = i;
- if (card_data[k] == translate_data[j])
- {
- match = 1;
- Serial.print(char(translate_ascii[j]));
- break;
- }
- }
- if (!match)
- Serial.print('~'); //print '~' for invalid character
- }
- }
- if (mode & CARD_OUTLINE)
- {
- Serial.println('|');
- for (int i = 11; i >= 0; i--) //print punched card punches
- {
- if (i == 11)
- Serial.print(" / "); //print card notch
- else if (i == 10)
- Serial.print("/ "); //print card notch
- else
- Serial.print("| "); //print card outline
- for (int j = 0; j < 80; j++)
- {
- int k;
- if (mode & REVERSE_PRINT)
- k = 79 - j;
- else
- k = j;
- if (card_data[k] & (1 << i))
- Serial.print('O');
- else
- Serial.print(' ');
- }
- if (mode & CARD_OUTLINE)
- Serial.println('|');
- }
- }
- else
- Serial.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement