简体   繁体   English

Atmega328上的Wiegand协议实现

[英]Wiegand Protocol Implementation on Atmega328

I'm trying to write a library for Hikvision card reader with keypad. 我正在尝试用键盘为海康威视读卡器编写一个库。 Here is some information about that reader . 以下是有关该读者的一些信息 Also I took as an example Arduino library. 我还以Arduino库为例。 This library works well for reading Data and uses same external interrupt pins: https://github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino 该库适用于读取数据并使用相同的外部中断引脚: https//github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino

I searched a lot in Google but there are no C examples that i could find. 我在谷歌搜索了很多但是没有我能找到的C例子。 I wrote my code from zero but it didn't worked. 我从零编写了我的代码,但它没有用。 I can see some output data but it has nothing in common with real data. 我可以看到一些输出数据,但它与实际数据没有任何共同之处。 So here is example: I put card on Reader which is Wiegand 26 protocol with ID:0x1532FC and Output is 0x94F8. 所以这里是一个例子:我把卡放在Reader上,它是Wiegand 26协议,ID为:0x1532FC,输出为0x94F8。

Maybe Someone will help me to find a mistake. 也许有人会帮我找错。

So here is my code: 所以这是我的代码:

#define F_CPU 16000000
#include <avr/io.h>
#include "c:\users\irakli\Documents\Atmel Studio\7.0\wiegand C\wiegand C\UART.h"
#include <avr/interrupt.h>
#include <util/delay.h>

`unsigned long CardID = 0; // DATA
 int bit_count= 0; //Wiegand type

 int Send;

 int DataArray[26] = {};
 int *ptr = DataArray;

 void WiegandReadD0(void) // INT0 Port PD3
 {
   bit_count++;
   CardID = CardID << 1;
   _delay_ms(1);    
 }
 void WiegandReadD1(void) // INT1 Port PD4
 {
   bit_count++;
   CardID = CardID << 1;
   CardID = CardID + 0x01;
   _delay_ms(1);
 }

 void WiegandSendD0(void) // PD5 Send Data0
 {
   PORTD = (0 << 5);
   _delay_us(20);
   PORTD = (1 << 5);
   _delay_ms(2);
 }
 void WiegandSendD1(void) // PD6 Send Data1
 {
   PORTD = (0 << 6);
   _delay_us(20);
   PORTD = (1 << 6);
   _delay_ms(2);
 }

 void Send_Data()
 {
   unsigned long data;
   int type;

   data = CardID;
   type = bit_count;

   for(int i = 0; i <= (type - 1); i++) // Decimal To Binary Array 
   {
     if(((1 << i) & data) != 0)
     {
        *(ptr + i) = 1;
     }
     else
     {
        *(ptr + i) = 0;
     }
   } // END Of FOR

   for(int i = (type - 1); i >= 0; --i) //Data Send Function
   {
     Send = *(ptr + i);

     if(Send == 1)
        WiegandSendD1();
     if(Send == 0)
        WiegandSendD0();
   }
  }

  int main(void)
  {
    Uart_init();
    sei();

    DDRD = (0 << 3) | (0 << 4); // Input Ports For Reading Wiegand
    PORTD = (0 << 3) | (0 << 4); // No Pull-up
    EIMSK = (1 << INT0) | (1 << INT1);  // Enable External Interrupts On PD3 
                                        // and PD4 

    DDRD = (1 << 5) | (1 << 6); // Output Ports For Sending Wiegand. PD5 = 
                                // D0, PD6 = D1.
    PORTD = (1 << 5) | (1 << 6); // PUll-ups Enable

    while (1) 
    {   
      _delay_ms(1000);
      printf("Card ID = %ld\t Card HEX = %.x\t Wiegand Type = %d\t\n",CardID, CardID, bit_count);
      _delay_ms(500);

      Send_Data();

      CardID = 0;
      bit_count = 0;
    }    
   }

   ISR(INT0_vect)
   {    
      WiegandReadD0();
   }
   ISR(INT1_vect) 
   {    
      WiegandReadD1();
   }`

If more details are needed please write. 如果需要更多细节,请写信。

There is a silly mistake in: 有一个愚蠢的错误:

printf("Card ID = %ld\t Card HEX = %.x\t Wiegand Type = %d\t\n",CardID, CardID, bit_count);

here should be: 这应该是:

printf("Card ID = %l\t Card HEX = %.x\t Wiegand Type = %d\t\n",CardID, CardID, bit_count);

!! BUT if someone were to use this code they should consider that output includes parity bits. 但是如果有人要使用这个代码,他们应该考虑输出包括奇偶校验位。 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM