Arduino Weigand 26bit Decoder ---2
In Wiegand 26 / Wiegand 34 / Wiegand 36 and all other Wiegand protocols used 2 wires (D0 and D1) to connect reader to MCU.
In standby D0 and D1 connected to VCC(+5V). All data from reader goes in binary format - If reader transmitting 0, it connecting D0 wire to ground for 20-50mks then connecting back to VCC and waits for 2ms before sending next bit. If goes '1' - D1 will be connected to ground fo 20-50mks and so on.
If no change in lines for more than 2ms - transfer complete.
If no change in lines for more than 2ms - transfer complete.
Picture:
Wiegand protocol — logic level
In logic level wiegand26 have 26 bit of data, 8 for — facility code, 16 for card number, and 2 for parity control.
Facility code – number of building or company where card is used.
Card number – card number
Facility code – number of building or company where card is used.
Card number – card number
* https://github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino
*/
#include <Wiegand.h>
//I2C + I2C LCD Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*==========================================
SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V
==========================================*/
WIEGAND wg;
void setup() {
Serial.begin(9600);
wg.begin();
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
// 輸出初始化文字
lcd.setCursor(0, 0); // 設定游標位置在第一行行首
lcd.clear();
lcd.setCursor(0, 0); // 設定游標位置在第1行行首
lcd.print("Reading RFID....");
lcd.setCursor(0, 1); // 設定游標位置在第2行行首
lcd.print("Wiegand 26 bit..");
}
void loop() {
if(wg.available())
{
lcd.clear();
lcd.setCursor(0, 0); // 設定游標位置在第1行行首
lcd.print("DEC Code=");
lcd.print(wg.getCode());
lcd.setCursor(0, 1); // 設定游標位置在第2行行首
lcd.print("HEX Code=");
lcd.print(wg.getCode(),HEX);
Serial.print("Wiegand HEX = ");
Serial.print(wg.getCode(),HEX);
Serial.print(", DECIMAL = ");
Serial.print(wg.getCode());
Serial.print(", Type W");
Serial.println(wg.getWiegandType());
}
}
參考文件:
4) 程式庫
Wiegand 24 and Wiegand 36 Protocol Library for Arduino
char dat[26]; // 26 bit for data
unsigned char fcode; // for facility code (0..255)
unsigned int code; // for card number (0..65535)
while(){
if(PIND.3==0){ // D1 on ground?
dat[i]=1; // bit = 1
i++;
while(PIND.3==0){};//waiting for release
}
if(PIND.4==0){// D0 on ground?
dat[i]=0; // bit = 0
i++;
while(PIND.4==0){}; //waiting for release
}
if(i==27) { // i=27, 26 bits collected
//now parsing
b=0;
for(a=9;a>1;a--){ //first 8bit in revers order
if(dat[a]==1) fcode=fcode+stepen(b); //if bit =1,
//adding position^2
//(2^0 , 2^1 , 2^2 and etc)
b++; // inc position
}
b=0; // reset bit counter
for(a=25;a>9;a--){//same for next 16 bit (card number)
if(dat[a]==1) code=code+stepen(b);
b++;
}
}
//If all OK, we have
//facility code — in var fcode
//card number – in var code
//Now searching...
//all numbers in 2 arrays in eeprom
//unsigned char fcode[MAX]; - facility code
//unsigned int code[MAX]; - card number
//MAX – number of "lines" in memory
// for attiny2313 - 40 code numbers
for(i=0;i<MAX;i++){//going thru array
// cheching only facility code in this step
if(fcode == fcodedb[i]){ //if code found
//checking number in same position
if(code==codedb[i]){ //number found...
//...we have exact numbers
card_found=1; // flag = card found
break; //breaking - no need to check next lines
}
}
}
//card found?
if(card_found){//yes!
PORTD.5=1;//opening lock
PORTD.6=0;//GREEN LED
delay_ms(5000); //5 second to enter
PORTD.5=0;//locking back
PORTD.6=1;//turning off LED
} else{ //NO! card not found
PORTB,0=0;//RED led
delay_ms(3000); //delay for 3 seconds
PORTB.0=1;//turn of red led
}
//now clear all data
fcode=0; // facility code
code=0; // card number
i=0; // bit counter
card_found=0; // flag
}
//go to first line... and wait for signal on D0 or D1
}
沒有留言:
張貼留言