Wiegand Data aFormat Reader and Arduino
code: [select]
/*
* hid rfid reader wiegand interface arduino uno
* written daniel smith, 2012.01.30
* www.pagemac.com
*
* program decode wiegand data hid rfid reader (or, theoretically,
* other device outputs weigand data).
* wiegand interface has 2 data lines, data0 , data1. these lines normall held
* high @ 5v. when 0 sent, data0 drops 0v few us. when 1 sent, data1 drops
* 0v few us. there few ms between pulses.
*
* reader should have @ least 4 connections (some readers have more). connect red wire
* 5v. connect black ground. connect green wire (data0) digital pin 2 (int0).
* connect white wire (data1) digital pin 3 (int1). that's it!
*
* operation simple - each of data lines connected hardware interrupt lines. when
* 1 drops low, interrupt routine called , bits flipped. after time of
* of not receiving bits, arduino decode data. i've added 26 bit and
* 35 bit formats, can add more.
*/
#define max_bits 100 // max number of bits
#define weigand_wait_time 3000 // time wait weigand pulse.
unsigned char databits[max_bits]; // stores of data bits
unsigned char bitcount; // number of bits captured
unsigned char flagdone; // goes low when data being captured
unsigned int weigand_counter; // countdown until assume there no more bits
unsigned long facilitycode=0; // decoded facility code
unsigned long cardcode=0; // decoded card code
// interrupt happens when goes low (0 bit)
void isr_int0()
{
//serial.print("0"); // uncomment line display raw binary
bitcount++;
flagdone = 0;
weigand_counter = weigand_wait_time;
}
// interrupt happens when int1 goes low (1 bit)
void isr_int1()
{
//serial.print("1"); // uncomment line display raw binary
databits[bitcount] = 1;
bitcount++;
flagdone = 0;
weigand_counter = weigand_wait_time;
}
void setup()
{
pinmode(13, output); // led
pinmode(2, input); // data0 (int0)
pinmode(3, input); // data1 (int1)
serial.begin(9600);
serial.println("rfid readers");
// binds isr functions falling edge of , int1
attachinterrupt(2, isr_int0, falling);
attachinterrupt(3, isr_int1, falling);
weigand_counter = weigand_wait_time;
}
void loop()
{
// waits make sure there have been no more data pulses before processing data
if (!flagdone) {
if (--weigand_counter == 0)
flagdone = 1;
}
// if have bits , weigand counter went out
if (bitcount > 0 && flagdone) {
unsigned char i;
serial.print("read ");
serial.print(bitcount);
serial.print(" bits. ");
// decode bits differently depending on how many bits have
// see www.pagemac.com/azure/data_formats.php mor info
if (bitcount == 35)
{
// 35 bit hid corporate 1000 format
// facility code = bits 2 14
(i=2; i<14; i++)
{
facilitycode <<=1;
facilitycode |= databits[i];
}
// card code = bits 15 34
(i=14; i<34; i++)
{
cardcode <<=1;
cardcode |= databits[i];
}
printbits();
}
else if (bitcount == 26)
{
// standard 26 bit format
// facility code = bits 2 9
(i=1; i<9; i++)
{
facilitycode <<=1;
facilitycode |= databits[i];
}
// card code = bits 10 23
(i=9; i<25; i++)
{
cardcode <<=1;
cardcode |= databits[i];
}
printbits();
}
else {
// can add other formats if want!
serial.println("unable decode.");
}
// cleanup , ready next card
bitcount = 0;
facilitycode = 0;
cardcode = 0;
for (i=0; i<max_bits; i++)
{
databits[i] = 0;
}
}
}
void printbits()
{
// hope can figure out function does
serial.print("fc = ");
serial.print(facilitycode);
serial.print(", cc = ");
serial.println(cardcode);
}
so trying use code decode 35 bit card. however, when first run program, starts reading random values not sure comes from.
also, when try read card, there nothing outputs onto serial monitor. supposedly, code worked many users in 2013ish issue here?
what connected input pins? if not actively pulled high or low external device, random data.
you're checking falling edges assume pin high; can change
to
and see if silves problem.
you're checking falling edges assume pin high; can change
code: [select]
pinmode(2, input); // data0 (int0)
pinmode(3, input); // data1 (int1)
to
code: [select]
pinmode(2, input_pullup); // data0 (int0)
pinmode(3, input_pullup); // data1 (int1)
and see if silves problem.
Arduino Forum > Using Arduino > Project Guidance > Wiegand Data aFormat Reader and Arduino
arduino
Comments
Post a Comment