📄 readers.c
字号:
#include "readers.h"
#include "main.h"
#include "access_control.h"
#include "utility.h"
#include "time.h"
static BOOL bMsg1,bMsg2; //indicate a message is available
//uart buffer
static BYTE xdata msg1_buf[15]; //receive buffer of channel 1
static BYTE xdata msg2_buf[15]; //receive buffer of channel 2
static BYTE msg1Cnt,msg2Cnt; //counter of receive buffer
//simulated uart
static BYTE mask1,mask2; //the mask of the receive bit for uart
static BOOL bTR1,bTR2; //indicate transmit or receive,for both using timer interrupt service
static BOOL bSend1,bSend2; //indicate a bit is sent
//wiegand
static BOOL bWgStart1,bWgStart2;//wiegand start bit flag
static ULONG xdata wgMask1,wgData1;
static ULONG xdata wgMask2,wgData2;
void reply_msg(BYTE channel,BYTE rid,BOOL b_valid);
BOOL parse_msg(BYTE* msg_buf,BYTE len,BYTE *rid/*reader id*/,USER *pUsr)
{
BYTE buf[7],i,bcc=0;
ULONG *pULONG;
text2hex(msg_buf,buf,len);
for(i=0;i<(len/2);i++)bcc+=buf[i];
if(bcc==0)
{
*rid = buf[0];
pULONG = (ULONG*)&buf[2];
pUsr->tag = *pULONG;
return TRUE;
}
return FALSE;
}
BOOL parse_wiegand(ULONG wgData,USER *usr)
{
BYTE i,check=0;
ULONG mask =1;
for(i=0;i<13;i++)
{
if(wgData & mask)check++;
mask <<=1;
}
if(check%2) //odd parity
{
for(i=0;i<13;i++)
{
if(wgData & mask)check++;
mask <<=1;
}
if(check%2)//even parity
{
usr->tag = (wgData&0x1ffffff)>>1;
return TRUE;
}
}
return FALSE;
}
void process_reader_msg()
{
BYTE rid;//reader id
USER usr;
if(CONFIG) //uart
{
if(bMsg1)
{
bMsg1 = FALSE;
if(parse_msg(msg1_buf,msg1Cnt,&rid,&usr))access_control(rid,&usr);
// reply_msg(CHANNEL1,rid,access_control(rid,&usr));
}
if(bMsg2)
{
bMsg2 = FALSE;
if(parse_msg(msg2_buf,msg2Cnt,&rid,&usr))access_control(rid,&usr);
// reply_msg(CHANNEL2,rid,access_control(rid,&usr));
}
}
else //wiegand
{
if(wg_cnt>1)bWgStart1=bWgStart2=FALSE;//wiegand is timeout
if(bMsg1)
{
bMsg1 = FALSE;
if(parse_wiegand(wgData1,&usr))access_control(0,&usr);
}
if(bMsg2)
{
bMsg2 = FALSE;
if(parse_wiegand(wgData2,&usr))access_control(1,&usr);
}
}
}
void ex0int(void) interrupt 0 //D0RX_2
{
BYTE i;
if(CONFIG )
{//uart mode
bTR1 = RECEIVE; //communication direct
TH0=PRE_BAUD_H;TL0=PRE_BAUD_L;//for middle sample
TR0=TRUE; //timer begin
mask1=START; //start flag
EX0=FALSE; //turn off interrupt
}
else //wiegand
{
wg_cnt=0; //wiegand time count begin
for(i=100;i>0;i--)if(D0RX_2)return;//restrain disturbance
while(!D0RX_2); // prevent from multi-interrupt
if(bWgStart2)
{
wgMask2 >>=1;
if(wgMask2==1)
{
bWgStart2 = FALSE;
bMsg2 = TRUE;
}
}
else
{
bWgStart2 = TRUE;
wgMask2 = 0x02000000;
wgData2 = 0;
}
}
}
void timer0int(void) interrupt 1 using 2
{
static BOOL bSTX=FALSE;
static BYTE rec;
TH0=BAUD_H; TL0=BAUD_L;//reload
if(bTR1==RECEIVE)
{
if(mask1==START)
{
if(D0RX_2)/*an illusive start bit*/
{TR0=FALSE;EX0=TRUE;return;}
else { mask1=1;rec=0;}//initialize
}
else if(mask1) {if(D0RX_2)rec+=mask1; mask1<<=1;}
else
{
TR0 = FALSE;/* turn off timer0*/
EX0 = TRUE;
if(D0RX_2)//stop bit should be high
{
if(rec==STX){msg1Cnt=0;bSTX = TRUE;}
else if(rec==ETX){ bSTX = FALSE;bMsg1=TRUE; }
else if(bSTX)
{
msg1_buf[msg1Cnt%15]=rec;
msg1Cnt++;
}
}
}
}
else bSend1 = TRUE;
}
void ex1int(void) interrupt 2 //D0RX_1
{
BYTE i;
if(CONFIG)
{//uart mode
bTR2 = RECEIVE; //communication direct
TH1=PRE_BAUD_H;TL1=PRE_BAUD_L;//for middle sample
TR1=TRUE; //timer begin
mask2=START; //start flag
EX1=FALSE; //turn off interrupt
}
else //wiegand
{
wg_cnt=0; //wiegand time count begin
for(i=100;i>0;i--)if(D0RX_1)return;//restrain disturbance
while(!D0RX_1); // prevent from multi-interrupt
if(bWgStart1)
{
wgMask1 >>=1;
if(wgMask1==1)
{
bWgStart1 = FALSE;
bMsg1 = TRUE;
}
}
else
{
bWgStart1 = TRUE;
wgMask1 = 0x02000000;
wgData1 = 0;
}
}
}
void timer1int(void) interrupt 3 using 3
{
static BOOL bSTX=FALSE;
static BYTE rec;
TH1=BAUD_H; TL1=BAUD_L;//reload
if(bTR2==RECEIVE)
{
if(mask2==START)
{
if(D0RX_1)/*an illusive start bit*/
{TR1=FALSE;EX1=TRUE;return;}
else { mask2=1;rec=0;}//initialize
}
else if(mask2) {if(D0RX_1)rec+=mask2; mask2<<=1;}
else
{
TR1 = FALSE;/* turn off timer0*/
EX1 = TRUE;
if(D0RX_1)//stop bit should be high
{
if(rec==STX){msg2Cnt=0;bSTX = TRUE;}
else if(rec==ETX){ bSTX = FALSE;bMsg2=TRUE; }
else if(bSTX)
{
msg2_buf[msg2Cnt%15]=rec;
msg2Cnt++;
}
}
}
}
else bSend2 = TRUE;
}
//void send(BYTE channel,BYTE* buf,BYTE len);
/*
void reply_msg(BYTE channel,BYTE rid,BOOL b_valid)
{
BYTE buf[9];
BYTE i,bcc;
buf[0]=';';
hex2text(&rid,&buf[1],1);//reader id
i = b_valid ? 0xa1 : 0xa2;
hex2text(&i,&buf[3],1); //valid?
bcc = rid+i;
bcc = ~bcc +1;
hex2text(&bcc,&buf[5],1);//fill buf with bcc
buf[7]=0x0d;
send(channel,buf,8);
}
/*
void send_byte_ch1(BYTE c)
{
BYTE mask;
bTR1 = TRANSMIT;
D1TX_1 = 0;//start bit
TH0 = BAUD_H; TL0=BAUD_L;
TR0 = TRUE;
bSend1 = FALSE; while(!bSend1);
mask='\x1';
do
{
D1TX_1 = c & mask;
bSend1 = FALSE; while(!bSend1);
}while(mask<<=1);
D1TX_1 = 1;//stop bit
bSend1 = FALSE; while(!bSend1);
TR0 = FALSE;
}
void send_byte_ch2(BYTE c)
{
BYTE mask;
bTR2 = TRANSMIT;
D1TX_2 = 0;//start bit
TH1 = BAUD_H; TL1=BAUD_L;
TR1 = TRUE;
bSend2 = FALSE; while(!bSend2);
mask='\x1';
do
{
D1TX_2 = c & mask;
bSend2 = FALSE; while(!bSend2);
}while(mask<<=1);
D1TX_1 = 1;//stop bit
bSend1 = FALSE; while(!bSend1);
TR0 = FALSE;
}
/*void send(BYTE channel,BYTE *buf,BYTE len)
{
BYTE i;
switch(channel)
{
case CHANNEL1:
for(i=0;i<len;i++)send_byte_ch1(buf[i]);
break;
case CHANNEL2:
for(i=0;i<len;i++)send_byte_ch2(buf[i]);
break;
}
}
*/
//-------------------------------------------------------------------
//wiegand
void wiegand_d1(void) interrupt 7
{
BYTE i;
KBCON = 0;//clear KBIF --keypad interrupt flag
wg_cnt=0; //time count begin
if(!D1TX_1)
{//D1TX_1
for(i=100;i>0;i--)if(D1TX_1)return;//restrain disturbance
while(!D1TX_1); // prevent from multi-interrupt
if(bWgStart1)
{
wgMask1 >>=1;
wgData1 |= wgMask1;
if(wgMask1==1)
{
bWgStart1 = FALSE;
bMsg1 = TRUE;
}
}
else
{
bWgStart1 = TRUE;
wgMask1 = 0x02000000;
wgData1 = 0x02000000;
}
}
else if(!D1TX_2)//D1TX_2
{
for(i=100;i>0;i--)if(D1TX_2)return;//restrain disturbance
while(!D1TX_2);// prevent from multi-interrupt
if(bWgStart2)
{
wgMask2 >>=1;
wgData2 |= wgMask2;
if(wgMask2==1)
{
bWgStart2 = FALSE;
bMsg2 = TRUE;
}
}
else
{
bWgStart2 = TRUE;
wgMask2 = 0x02000000;
wgData2 = 0x02000000;
}
}
KBCON = 0;// prevent from multi-interrupt
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -