- //------------------------------------------------------
- //功能:把数组封包成韦根26的格式,并发送出去
- // 原理是把每个字节的低4位取出,来计算这个字节的值
- //入口:str=要封包的数组,
- //出口:DATA0P3.0;DATA1=P3.1
- //设计:大鹏,大鹏艾迪,2006/4/11
- //------------------------------------------------------
- /*#include <52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long int*/
- extern sbit WG_DATA0 =P3^2;
- extern sbit WG_DATA1 =P3^3 ;
- void send_wiegand26(uchar *str)
- {
- //| wiegand[0] | wiegand[1] | wiegand[2] |
- //| *str *(str + 1) | *(str + 2) *(str + 3)| *(str + 4) *(str + 5)|
- uchar idata i;
- static uchar data one_num; //计算1的个数
- uchar data check_temp; //韦根包奇偶效验中间暂存
- bit data even; //韦根包前12位偶效验
- bit data odd; //韦根包后12位齐效验
- static uchar data wiegand[3]; //韦根包数据24位
- // EA=0;//禁止中断
- //--------------------------------端口方向定义
- // P3M0 = 0x00; //普通I/O口
- // P3M1 = 0x00;
- //================================数组到韦根包的转化
- wiegand[0] = wiegand[0]|((*str)<<4);//原理是把每个字节的低4位取出,来计算这个字节的值
- wiegand[0] = wiegand[0]|(*(str+1)&0x0f);
- //--------------------------------计算前8位1的个数,为偶效验用
- check_temp = wiegand[0];
- for(i = 0;i<8;i++)
- {
- if(check_temp&0x01) //(check_temp&0x01)
- {
- one_num++;
- }
- check_temp >>= 1;
- }
- wiegand[1] = wiegand[1]|(*(str+2)<<4);
- //--------------------------------计算接下来的4位1的个数,为偶效验用
- check_temp = wiegand[1];
- for(i = 0;i<4;i++)
- {
- if(check_temp&0x80)
- {
- one_num++;
- }
- check_temp<<=1;
- }
- //--------------------------------判断1的个数
- one_num%2 == 0 ? (even = 0):( even = 1);
- one_num = 0;
- wiegand[1] = wiegand[1]|(*(str+3)&0x0f);
- //--------------------------------计算接下来的4位1的个数,为奇效验用
- check_temp = wiegand[1];
- for(i = 0;i<4;i++)
- {
- if(check_temp&0x01){one_num++;}
- check_temp>>=1;
- }
- wiegand[2] = wiegand[2]|(*(str+4)<<4);
- wiegand[2] = wiegand[2]|(*(str+5)&0x0f);
- //--------------------------------计算接下来的8位1的个数,为奇效验用
- check_temp = wiegand[2];
- for(i = 0;i<8;i++)
- {
- if(check_temp&0x01){one_num++;}
- check_temp >>= 1;
- }
- //--------------------------------判断1的个数
- one_num%2 == 0 ? (odd = 1):( odd = 0);
- one_num = 0;
- //================================启动发送,用定时器做时间延时
- //--------------------------------韦根 输出端初始化
- WG_DATA0 = 1;
- WG_DATA1 = 1;
- //--------------------------------发送偶效验
- TMOD=0X01; //T0为方式1计数
- EA=1; EX1=0; EX0=0; //开中断,禁止INT0,INT1中断
- if(even) //enen韦根包前12位偶效验标志
- {
- WG_DATA1 = 0;
- //-------------------------延时100us
- TR0 = 0; //停止T0计算
- TH0 = (65536 - 158)/256; //定时100us 20MHz晶体
- TL0 = (65536 - 158)%256;
- TF0 = 0; //T0中断请求标志清0
- ET0 = 0; //T0中断禁止
- TR0 = 1; //启动T0计算
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA1 = 1;
- }
- else
- {
- WG_DATA0 = 0;
- //------------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA0 = 1;
- }
- //----------------------------延时一个发送周期
- TR0 = 0;
- TH0 = (65536 - 1582)/256; //定时1000us//1500us
- TL0 = (65536 - 1582)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- //-------------------------------发送24位数据
- for(i = 0;i<24;i++)
- {
- //---------------------------韦根 输出端初始化
- WG_DATA0 = 1;
- WG_DATA1 = 1;
- if((wiegand[0])&0x80)
- {
- WG_DATA1 = 0;
- //----------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA1 = 1;
- }
- else
- {
- WG_DATA0 = 0;
- //---------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA0 = 1;
- }
- (*(long*)&wiegand[0]) <<= 1;
- //-------------------------------延时一个发送周期
- TR0 = 0;
- TH0 = (65536 - 1582)/256; //定时1000us//1500us
- TL0 = (65536 - 1582)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- }
- //==============================发送奇效验位
- //------------------------------韦根 输出端初始化
- WG_DATA0 = 1;
- WG_DATA1 = 1;
- if(odd) //韦根包后12位齐效验标志
- {
- WG_DATA1 = 0;
- //-------------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA1 = 1;
- }
- else
- {
- WG_DATA0 = 0;
- //-------------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA0 = 1;
- }
- }
- /*
- void send_wiegand34(uchar *str)
- {
- //| wiegand[0] | wiegand[1] | wiegand[2] |
- //| *str *(str + 1) | *(str + 2) *(str + 3)| *(str + 4) *(str + 5)|
- uchar data i;
- static uchar data one_num; //计算1的个数
- uchar data check_temp; //韦根包奇偶效验中间暂存
- bit data even; //韦根包前16位偶效验
- bit data odd; //韦根包后16位齐效验
- static uchar data wiegand[4]; //韦根包数据32位
- // EA=0;//禁止中断
- //--------------------------------端口方向定义
- // P3M0 = 0x00; //普通I/O口
- // P3M1 = 0x00;
- //================================数组到韦根包的转化
- wiegand[0] = wiegand[0]|((*str)<<4);//原理是把每个字节的低4位取出,来计算这个字节的值
- wiegand[0] = wiegand[0]|(*(str+1)&0x0f);
- //--------------------------------计算前8位1的个数,为偶效验用
- check_temp = wiegand[0];
- for(i = 0;i<8;i++)
- {
- if(check_temp&0x01) //(check_temp&0x01)
- {
- one_num++;
- }
- check_temp >>= 1;
- }
- wiegand[1] = wiegand[1]|(*(str+2)<<4);
- //--------------------------------计算接下来的4位1的个数,为偶效验用
- check_temp = wiegand[1];
- for(i = 0;i<4;i++)
- {
- if(check_temp&0x80)
- {
- one_num++;
- }
- check_temp<<=1;
- }
- //--------------------------------判断1的个数
- one_num%2 == 0 ? (even = 0):( even = 1);
- one_num = 0;
- wiegand[1] = wiegand[1]|(*(str+3)&0x0f);
- //--------------------------------计算接下来的4位1的个数,为奇效验用
- check_temp = wiegand[1];
- for(i = 0;i<4;i++)
- {
- if(check_temp&0x01){one_num++;}
- check_temp>>=1;
- }
- wiegand[2] = wiegand[2]|(*(str+4)<<4);
- wiegand[2] = wiegand[2]|(*(str+5)&0x0f);
- //--------------------------------计算接下来的8位1的个数,为奇效验用
- check_temp = wiegand[2];
- for(i = 0;i<8;i++)
- {
- if(check_temp&0x01){one_num++;}
- check_temp >>= 1;
- }
- //--------------------------------判断1的个数
- one_num%2 == 0 ? (odd = 1):( odd = 0);
- one_num = 0;
- //================================启动发送,用定时器做时间延时
- //--------------------------------韦根 输出端初始化
- WG_DATA0 = 1;
- WG_DATA1 = 1;
- //--------------------------------发送偶效验
- // TMOD=0X01; //T0为方式1计数
- // EA=1; EX1=0; EX0=0; //开中断,禁止INT0,INT1中断
- if(even) //enen韦根包前12位偶效验标志
- {
- WG_DATA1 = 0;
- //-------------------------延时100us
- TR0 = 0; //停止T0计算
- TH0 = (65536 - 158)/256; //定时100us 20MHz晶体
- TL0 = (65536 - 158)%256;
- TF0 = 0; //T0中断请求标志清0
- ET0 = 0; //T0中断禁止
- TR0 = 1; //启动T0计算
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA1 = 1;
- }
- else
- {
- WG_DATA0 = 0;
- //------------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA0 = 1;
- }
- //----------------------------延时一个发送周期
- TR0 = 0;
- TH0 = (65536 - 1582)/256; //定时1000us //1382//1500us
- TL0 = (65536 - 1582)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- //-------------------------------发送24位数据
- for(i = 0;i<32;i++)
- {
- //---------------------------韦根 输出端初始化
- WG_DATA0 = 1;
- WG_DATA1 = 1;
- if((wiegand[0])&0x80)
- {
- WG_DATA1 = 0;
- //----------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA1 = 1;
- }
- else
- {
- WG_DATA0 = 0;
- //---------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA0 = 1;
- }
- (*(long*)&wiegand[0]) <<= 1;
- //-------------------------------延时一个发送周期
- TR0 = 0;
- TH0 = (65536 - 1582)/256; //定时1000us //1500us -1382
- TL0 = (65536 - 1582)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- }
- //==============================发送奇效验位
- //------------------------------韦根 输出端初始化
- WG_DATA0 = 1;
- WG_DATA1 = 1;
- if(odd) //韦根包后12位齐效验标志
- {
- WG_DATA1 = 0;
- //-------------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA1 = 1;
- }
- else
- {
- WG_DATA0 = 0;
- //-------------------------延时100us
- TR0 = 0;
- TH0 = (65536 - 158)/256; //定时100us
- TL0 = (65536 - 158)%256;
- TF0 = 0;
- ET0 = 0;
- TR0 = 1;
- while (!TF0) { ;}
- TF0 = 0;
- WG_DATA0 = 1;
- }
- } */