簡體   English   中英

Atmega328上的Wiegand協議實現

[英]Wiegand Protocol Implementation on Atmega328

我正在嘗試用鍵盤為海康威視讀卡器編寫一個庫。 以下是有關該讀者的一些信息 我還以Arduino庫為例。 該庫適用於讀取數據並使用相同的外部中斷引腳: https//github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino

我在谷歌搜索了很多但是沒有我能找到的C例子。 我從零編寫了我的代碼,但它沒有用。 我可以看到一些輸出數據,但它與實際數據沒有任何共同之處。 所以這里是一個例子:我把卡放在Reader上,它是Wiegand 26協議,ID為:0x1532FC,輸出為0x94F8。

也許有人會幫我找錯。

所以這是我的代碼:

#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();
   }`

如果需要更多細節,請寫信。

有一個愚蠢的錯誤:

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

這應該是:

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

但是如果有人要使用這個代碼,他們應該考慮輸出包括奇偶校驗位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM