📄 em4100.c
字号:
#include<EM4100.h>
#include<intrins.h>
uchar counter=0;
bit temp1;
bit temp2;
bit sys_flag=0;
bit start_flag=0;
bit RX_flag=0;
bit _data;
bit Ready=0;
ulong card_sn=0;
uchar col_check[4];//column pority
uchar row;
uchar col;
uchar even=0; //row pority
uchar TH0_TEMP;
uchar TL0_TEMP;
/* Initial of timer0 */
void EM4100_INI()//24MHZ
{
TMOD=0x21;
//TH0=0xfe;//need modify using different crastal
//need modify using different crastal
//TL0=0x17;//need modify using different crastal 24M
//TL0=0x1a; //12M
// TH0=0xfe; //need modify using different crastal
// TL0=0x18; //need modify using different crastal
// TH0=TH0_TEMP=0xff;
// TL0=TL0_TEMP=0x19;
TH0=TH0_TEMP=0xfe;
TL0=TL0_TEMP=0x40;
ET0=1;
TR0=1;
EA=1;
}
/* return the statue */
uchar EM4100_READY()
{
return (uchar)Ready;
}
/* return the card number */
ulong Get_SN()
{
ulong i=card_sn;
card_sn=0;
Ready=0;
return i;
}
//Enable the timer
void Get_Enable()
{
ET0=1;
}
//exactitude 256us timer isr
void Timer0() interrupt 1
{
// uchar a,b,c,d,e,f;
TH0=TH0_TEMP;
_nop_();
TL0=TL0_TEMP; //8M
/* a=(uchar)RF_IN;
_nop_();_nop_();_nop_();_nop_();//_nop_();_nop_();_nop_();_nop_();_nop_();
b=(uchar)RF_IN;
_nop_();_nop_();_nop_();_nop_();//_nop_();_nop_();_nop_();_nop_();_nop_();
c=(uchar)RF_IN;
_nop_();_nop_();_nop_();_nop_();//_nop_();_nop_();_nop_();_nop_();_nop_();
d=(uchar)RF_IN;
_nop_();_nop_();_nop_();_nop_();//_nop_();_nop_();_nop_();_nop_();_nop_();
f=(uchar)RF_IN;
_nop_();_nop_();_nop_();_nop_();//_nop_();_nop_();_nop_();_nop_();_nop_();
b=(uchar)RF_IN;
_nop_();_nop_();_nop_();_nop_();//_nop_();_nop_();_nop_();_nop_();_nop_();
a=a+b+c+d+e+f;
if(a>2)temp2=1; //sample
else temp2=0; */
temp2=RF_IN;
if(RX_flag) //receive data
{
if(start_flag)
{
start_flag=0;
temp1=temp2;
}
else
{
start_flag=1;
counter++; //data volumn
if(temp1==1&&temp2==0)_data=1;//flop data "1"
else if(temp1==0&&temp2==1)_data=0; //flip data "0"
else //error
{
RX_flag=0; //turn to normal state
start_flag=1;
counter=0;
card_sn=0; row=col=0;
return;
}
if(counter>10) //receive the last three bytes
{
/* check the data */
if(counter==25||counter==30||counter==35||counter==40||counter==45||counter==50||counter==55)
{
if(temp2)even++;
if((even&0x01)!=0)even=0;
else //error
{
col_check[0]=col_check[1]=col_check[2]=col_check[3]=0;
row=col=0;
RX_flag=0; //turn to normal state
start_flag=1;
counter=0;
card_sn=0;
return;
}
}
else //receive the data
{
if(col==3){col=0;row++;}
else col++;
if(counter<=50)//receive valid data
{
card_sn<<=1;
card_sn+=(ulong)_data;
}
if(_data){even++;col_check[col]++;} //check
}
}
if(counter==54) //transmit complet! do not care the last bit "0"
{
if((col_check[0]&0x01)&&(col_check[0]&0x01)&&(col_check[0]&0x01)&&(col_check[0]&0x01))
col_check[0]=col_check[1]=col_check[2]=col_check[3]=0; //check right
else //check error
{
col_check[0]=col_check[1]=col_check[2]=col_check[3]=0;
row=col=0;
RX_flag=0; //turn to normal state
start_flag=1;
counter=0;
card_sn=0;
return;
}
Ready=1; //data are ready
counter=0; //turn to normal state
RX_flag=0;
start_flag=1;
sys_flag=0;
ET0=0; row=col=0;
}
}
}
else //finding the header
{
if(sys_flag)
if(temp2==0)//flop
{
counter++;
sys_flag=0;
}
else
{
sys_flag=1; //attention!!! must be a started of flop
counter=0;
}
else if(temp2==1)sys_flag=1;
if(counter==9) //the header is over
{
RX_flag=1; //enable receive data
start_flag=1;
counter=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -