/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=247401]@brief[/url] Use GPIO to send wiegand26 protocol
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"
/*---------------------------------------------------------------------------*/
/* Define */
/*---------------------------------------------------------------------------*/
#define delay_100us() Timer0_Delay100us(1)
#define WG_DATA1 P06
#define WG_DATA0 P05
/*---------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------*/
unsigned char out[3] = {0x55, 0x55, 0x55};
unsigned char in[3] = {0, 0, 0};
unsigned char count = 0; //capture data count
unsigned char inputcapture[26] = {0}; //for reciver data buffeer
/*---------------------------------------------------------------------------*/
/* Functions */
/*---------------------------------------------------------------------------*/
void delay_1500us(void)
{
Timer0_Delay100us(15);
}
void WG_send_bit_1(void)
{
WG_DATA1 = 0;
delay_100us();
WG_DATA1 = 1;
delay_1500us();
}
void WG_send_bit_0(void)
{
WG_DATA0 = 0;
delay_100us();
WG_DATA0 = 1;
delay_1500us();
}
//send wiegnad 26 byte.
//input data array 3 char
void send_wiegand26(unsigned char *str)
{
unsigned char data i;
unsigned char check_temp;
unsigned char data check;
bit even;
bit odd;
unsigned char data wiegand[3];
wiegand[0] = *str;
wiegand[1] = *(str + 1);
wiegand[2] = *(str + 2);
check = 0;
check_temp = wiegand[0];
//even data check for data 2-9
for(i = 0; i < 8; i++)
{
if((check_temp & 0x80) == 0X80)
{
check++;
}
check_temp = check_temp << 1;
}
if(check % 2 == 0)
even = 0;
else
even = 1;
check = 0;
//odd data check for data 10-25
check_temp = wiegand[1];
for(i = 0; i < 8; i++)
{
if(check_temp & 0x80 == 0X80)
{
check++;
}
check_temp = check_temp << 1;
}
check_temp = wiegand[2];
for(i = 0; i < 8; i++)
{
if(check_temp & 0x80 == 0X80)
{
check++;
}
check_temp = check_temp << 1;
}
if(check % 2 == 0)
odd = 1;
else
odd = 0;
//send header ep
if(even)
{
WG_send_bit_1();
}
else
{
WG_send_bit_0();
}
//send data
for(i = 0; i < 24; i++)
{
if((wiegand[0]) & 0x80)
{
WG_send_bit_1();
}
else
{
WG_send_bit_0();
}
(*(long*)&wiegand[0]) <<= 1;
}
//send op
if(odd)
{
WG_send_bit_1();
}
else
{
WG_send_bit_0();
}
}
//interrupt data input
void PinInterrupt_ISR(void) interrupt 7
{
if(WG_DATA1 == 0)
inputcapture[count] = 1;
count++;
PIF = 0x00; //clear interrupt flag
}
//get wiegand
//return 0 false, 1 pass.
//input data array for reciver data
unsigned char GET_wiegand26(unsigned char *str)
{
unsigned char data i;
unsigned char check_temp;
unsigned char data check;
bit even;
bit odd;
//enable interrupt
PICON = 0xFC; //PORT 0 INT, edge interrupt
PINEN = 0X60; //FALL edge
count = 0;
set_EPI; // Enable pin interrupt
set_EX0;
set_EA; // global enable bit
while(count < 26); //POLL DATA INPUT
clr_EA; // clear ea
clr_EPI; // clr pin interrupt
clr_EX0;
check = 0;
check_temp = 0;
//data 1-9
for(i = 1; i < 9; i++)
{
if((inputcapture[i]) == 1)
{
check++;
check_temp = check_temp | 0x01;
}
check_temp = check_temp << 1;
}
if(check % 2 == 0)
even = 0;
else
even = 1;
//compare even
if(even != inputcapture[0])
return 0;
str[0] = check_temp;
check = 0;
check_temp = 0;
//data 10-25
for(i = 9; i < 17; i++)
{
if((inputcapture[i]) == 1)
{
check++;
check_temp = check_temp | 0x01;
}
check_temp = check_temp << 1;
}
str[1] = check_temp;
check_temp = 0;
for(i = 17; i < 25; i++)
{
if((inputcapture[i]) == 1)
{
check++;
check_temp = check_temp | 0x01;
}
check_temp = check_temp << 1;
}
str[2] = check_temp;
if(check % 2 == 0)
odd = 1;
else
odd = 0;
//compare odd
if(odd != inputcapture[25])
return 0;
return 1;
}
void main(void)
{
//for wiegand in high state.
P05 = 1;
P06 = 1;
//initial all gpio for quasi mode
Set_All_GPIO_Quasi_Mode;
delay_1500us();
//send wiegand package
send_wiegand26(out);
while(1);
}
|