//功能:把数组封包成韦根26的格式,并发送出去
// 原理是把每个字节的低4位取出,来计算这个字节的值
//入口:str=要封包的数组,
//出口:DATA0P3.0;DATA1=P3.1
//设计:大鹏,大鹏艾迪,2006/4/11
void delay_100us(void)
{
//-------------------------延时100us
TR0 = 0;
TH0 = (65536 - 78)/256; //定时100us
TL0 = (65536 - 78)%256;
TF0 = 0;
ET0 = 0;
TR0 = 1;
while (!TF0) { ;}
}
void delay_1500us(void)
{
TR0 = 0;
TH0 = (65536 - 1382)/256; //定时1500us
TL0 = (65536 - 1382)%256;
TF0 = 0;
ET0 = 0;
TR0 = 1;
while (!TF0) { ;}
}
void WG_send_bit_1(void)
{
WG_DATA1 = 0;
//----------------------延时100us
delay_100us();
WG_DATA1 = 1;
//-------------------------------延时一个发送周期
delay_1500us();
}
void WG_send_bit_0(void)
{
WG_DATA0 = 0;
//----------------------延时100us
delay_100us();
WG_DATA1 = 1;
//-------------------------------延时一个发送周期
delay_1500us();
}
void send_wiegand26(uchar *str)
{
//| wiegand[0] | wiegand[1] | wiegand[2] |
//| *str *(str + 1) | *(str + 2) *(str + 3)| *(str + 4) *(str + 5)|
uchar data i;
uchar data check_temp; //韦根包奇偶效验中间暂存
bit data even; //韦根包前12位偶效验
bit data odd; //韦根包后12位齐效验
uchar data wiegand[3]; //韦根包数据24位
//--------------------------------端口方向定义
P3M0 = 0x00; //普通I/O口
P3M1 = 0x00;
//================================数组到韦根包的转化
wiegand[0] = wiegand[0]|((*str)<<4);//原理是把每个字节的低4位取出,来计算这个字节的值
wiegand[0] = wiegand[0]|(*(str+1)&0x0f);
wiegand[1] = wiegand[1]|(*(str+2)<<4);
wiegand[1] = wiegand[1]|(*(str+3)&0x0f)
wiegand[2] = wiegand[2]|(*(str+4)<<4);
wiegand[2] = wiegand[2]|(*(str+5)&0x0f);
//--------------------------------计算前12位1的个数是否为偶数,为偶效验用
check_temp = wiegand[1]&0xf0;
check_temp ^= wiegand[0];
check_temp ^= check_temp>>4;
check_temp ^= check_temp>>2;
check_temp ^= check_temp>>1;
even=!(check_temp&1);
//--------------------------------计算后12位1的个数是否为偶数,为奇效验用
check_temp = wiegand[1]&0x0f;
check_temp ^= wiegand[2];
check_temp ^= check_temp>>4;
check_temp ^= check_temp>>2;
check_temp ^= check_temp>>1;
odd=check_temp&1;
//================================启动发送,用定时器做时间延时
//--------------------------------韦根 输出端初始化
WG_DATA0 = 1;
WG_DATA1 = 1;
//--------------------------------发送偶效验
if(even)
{
WG_send_bit_1();
}
else
{
WG_send_bit_0();
}
//-------------------------------发送24位数据
for(i = 0;i<24;i++)
{
//---------------------------韦根 输出端初始化
if((wiegand[0])&0x80)
{
WG_send_bit_1();
}
else
{
WG_send_bit_0();
}
(*(long*)&wiegand[0]) <<= 1;
}
//==============================发送奇效验位
if(odd)
{
WG_send_bit_1();
}
else
{
WG_send_bit_0();
}
}