Author Topic: Digispark Wiegand reader w/ LCD display  (Read 2877 times)

Macjbraun

  • Newbie
  • *
  • Posts: 6
Digispark Wiegand reader w/ LCD display
« on: February 11, 2013, 12:31:23 pm »
I got a wiegand format card reader working, with a little help from PinChangeInterrupt.h. This may help those trying to get interrupts working.
 
Code: [Select]

 /* Crazy People
 * By Mike Cook April 2009
 * Three RFID readers outputing 26 bit Wiegand code to pins:-
 * Reader A (Head) Pins 2 & 3
 * Interrupt service routine gathers Wiegand pulses (zero or one) until 26 have been recieved
 * Then a sting is sent to processing
 */
// Modified to support the Digispark Feb 2013 JAB
#include <TinyWireM.h>
#include <USI_TWI_Master.h>
#include <LiquidCrystal_I2C.h>
#include <PinChangeInterrupt.h>
#define PIN1 3  //Data0
#define PIN2 4  //Data1
 
LiquidCrystal_I2C lcd(0x20,16,2);  // set the LCD address to 0x20 (varies) for a 16 chars and 2 line display
//SDA to P0
//SCL to P2
volatile long reader1 = 0;
volatile int reader1Count = 0;
void reader1One(void) {
  reader1Count++;
  reader1 = reader1 << 1;
  reader1 |= 1;
}
void reader1Zero(void) {
  reader1Count++;
  reader1 = reader1 << 1; 
}
void setup()
{
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  pinMode(PIN1, INPUT); digitalWrite(PIN1, HIGH);
attachPcInterrupt(PIN1, reader1Zero, RISING);  // add more attachInterrupt code as required
  pinMode(PIN2, INPUT); digitalWrite(PIN2, HIGH);
attachPcInterrupt(PIN2, reader1One, RISING);
  delay(10);
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for(int i = 3; i<5; i++){
  pinMode(i, OUTPUT);
  digitalWrite(i, HIGH); // enable internal pull up causing a one
  digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
  pinMode(i, INPUT);
  digitalWrite(i, HIGH); // enable internal pull up
  }
  delay(10);
  // put the reader input variables to zero
  reader1 = 0;
  reader1Count = 0;
  lcd.init();
  lcd.backlight();
  lcd.print("Ready");
}
void loop() {
  if(reader1Count >=26){
  lcd.backlight();
  lcd.clear();
 int serialNumber=(reader1 >> 1) & 0x3fff;
 int siteCode= (reader1 >> 17) & 0xff;  //changed from 0x3ff to mask parity bit.
 lcd.setCursor(0, 0);
 lcd.print(siteCode);
 lcd.setCursor(0,1);
 lcd.print(serialNumber);
  reader1 = 0;
  reader1Count = 0;
  delay(2000);
  lcd.noBacklight();
     }   
}