基于51的FM1702射频卡程序.docx

上传人:b****5 文档编号:6219662 上传时间:2023-01-04 格式:DOCX 页数:28 大小:19.82KB
下载 相关 举报
基于51的FM1702射频卡程序.docx_第1页
第1页 / 共28页
基于51的FM1702射频卡程序.docx_第2页
第2页 / 共28页
基于51的FM1702射频卡程序.docx_第3页
第3页 / 共28页
基于51的FM1702射频卡程序.docx_第4页
第4页 / 共28页
基于51的FM1702射频卡程序.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

基于51的FM1702射频卡程序.docx

《基于51的FM1702射频卡程序.docx》由会员分享,可在线阅读,更多相关《基于51的FM1702射频卡程序.docx(28页珍藏版)》请在冰豆网上搜索。

基于51的FM1702射频卡程序.docx

基于51的FM1702射频卡程序

#include

#include

#include

#defineucharunsignedchar

#defineuintunsignedint

ucharFbuff[16];//发送FIFO缓存

ucharJbuff[16];//接收FIFO缓存

ucharUID[7];//卡型及卡号

ucharData[4];//按键值存储区

ucharcodeseg[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};//数码管段选对应0~f

ucharcount=0;//按键回传的值

ucharshuaka,chongzhi,keyflag=0;

sbitk2=P3^5;//充值‘加值键(实现加1或者加10)

sbitk3=P3^2;//刷卡’减值键(实现减1或者减10)

sbitk4=P3^3;//确认键

sbitnWR=P2^4;//74hc373片选

/*****************1702函数声明**************************/

ucharspi(ucharm);

voidfifo_clear();

voidresig_write(ucharreg,ucharda);

ucharresig_read(ucharreg);

ucharfifo_read(ucharcount,uchar*s);

voidfifo_write(ucharcount,uchar*s);

ucharinti_1702();

voidcard_halt();

ucharrequest();

ucharcard_anticoll();

ucharcard_select();

ucharcard_authtication(ucharm);

ucharcard_read(ucharm);

ucharcard_write(ucharm);

ucharloadkey();

voiddelay(ucharm);

/********************按键显示函数声明***************/

voiddisplay();

voidkeyscan();

voiddata1deal();

voiddata2deal(ucharm);

voidlcdclear();

/***********************子函数解释******************/

/*********************************************************/

//解释:

这是spi总线的读写时序,所有的寄存器操作基于此时序,非常重要

//

//

//输入:

要写入的16进制参数

//

//

//输出:

内部传回的16进制参数

//

/********************************************************/

ucharspi(ucharm)

{

uchari,temp=0;

for(i=0;i<8;i++)

{

sck=0;

if(m&0x80)

mosi=1;

else

mosi=0;

m<<=1;

sck=1;

temp<<=1;

if(miso)

temp|=0x01;

}

sck=0;

mosi=0;

returntemp;

}

/*********************************************************/

//解释:

写寄存器函数

//

//

//输入:

寄存器地址以及要写入的参数

//

//

//输出:

//

//注意!

在所有的资料中都没有给出寄存器寻址时的格式,下边的有,看仔细了,如果连寄存器都找不到,后边的就不用看了

/********************************************************/

voidresig_write(ucharreg,ucharda)

{

sck=0;

reg<<=1;

cs=0;

reg=reg&0x7e;

spi(reg);

spi(da);

cs=1;

}

/*********************************************************/

//解释:

读寄存器函数

//

//

//输入:

寄存器地址

//

//

//输出:

该寄存器目前的值

//

//注意!

读写寄存器时指令不一样,仔细看。

/********************************************************/

ucharresig_read(ucharreg)

{

uchartemp;

sck=0;

_nop_();

_nop_();

cs=0;

reg<<=1;

reg|=0x80;

spi(reg);

temp=spi(0x00);

cs=1;

returntemp;

}

/*********************************************************/

//解释:

FIFO缓冲器的读函数

//

//

//输入:

读取的字节个数,返回的值存放首地址

//

//

//输出:

读成功的话会返回真值,否则返回0

//

//

/********************************************************/

ucharfifo_read(ucharcount,uchar*s)

{

uchari,temp;

temp=resig_read(FIFOLength);

if(temp

return0;

else

{

for(i=0;i

{

temp=resig_read(FIFODaTa);

*(s+i)=temp;

}

}

return1;

}

/*********************************************************/

//解释:

FIFO缓冲器的写函数

//

//

//输入:

写入的字节个数,要写入的值的存放首地址

//

//

//输出:

//

//

/********************************************************/

voidfifo_write(ucharcount,uchar*s)

{

uchari,temp;

for(i=0;i

{

temp=*(s+i);

resig_write(FIFODaTa,temp);

}

}

/*********************************************************/

//解释:

芯片FM1702初始化函数

//

//

//输入:

//

//

//输出:

初始化成功的话返回真值,否则返回0

//

//

//注意:

初始化步骤非常重要,一定要按照使用手册的启动步骤来,参考所给pdf

/********************************************************/

ucharinti_1702()

{

uchartemp,i;

FM1702rst=1;

mosi=1;

sck=1;

delay(20);

FM1702rst=0;

delay(20);

while(resig_read(Command));

resig_write(0x00,0x80);

temp=resig_read(Command);

while(temp);

resig_write(0x00,0x00);//切换到线性寻址(此命令非常重要,否则无法启动发送)

resig_write(TimerClock,0x0b);//address2AH/*定时器周期设置寄存器*/

resig_write(TimerControl,0x02);//address2BH/*定时器控制寄存器*/

resig_write(TimerReload,0x42);//address2CH/*定时器初值寄存器*/

resig_write(InterruptEn,0x7f);//address06H/*中断使能/禁止寄存器*/

resig_write(InterruptRq,0x7f);//address07H/*中断请求标识寄存器*/

resig_write(MFOUTSelect,0x02);//address26H/*mfOUT选择配置寄存器*/

resig_write(TxControl,0x5b);//address11H/*发送控制寄存器*/

resig_write(RxControl2,0x01);

resig_write(RxWait,0x07);

for(i=0;i<16;i++)

{

Fbuff[i]=0;

Jbuff[i]=0;

}

lcdclear();

if(temp==0x00)

return1;

else

return0;

}

/*********************************************************/

//解释:

延时函数,单次延时为5ms

//

//

//输入:

要延时的次数

//

//

//输出:

//

//

/********************************************************/

voiddelay(ucharm)//单次定时为10ms

{

TMOD=0x01;

while(m--)

{

TH0=0xfe;

TL0=0x33;

TR0=1;

while(!

TF0);

TF0=0;

TR0=0;

}

}

/*********************************************************/

//解释:

卡的回应函数,判断是否有卡在读写器跟前

//

//

//输入:

//

//

//输出:

如果有卡在的话返回真值,否则返回0

//

//

/********************************************************/

ucharrequest()

{

uchartemp;

resig_write(CRCResultLSB,0x63);

resig_write(CWConductance,0x3f);

resig_write(BitFraming,0x07);

resig_write(ChannelRedundancy,0x03);

temp=resig_read(Control);

temp=temp&0x7f;

resig_write(Control,temp);

resig_read(FIFOLength);

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

Fbuff[0]=0x52;

fifo_write(1,Fbuff);

resig_write(Command,0x1e);

delay

(1);

temp=resig_read(FIFOLength);

if(temp==0x02)

return1;

else

return0;

}

/*********************************************************/

//解释:

防冲突函数(我所做的此函数没有手册的那种功能,只是实现了单卡交易功能)

//

//

//输入:

//

//

//输出:

防冲突成功的话返回真值,否则返回0

//

//实现功能:

如果有多张卡在读卡器区域,只选中一张,并读取验证这张卡的ID,然后存储

/********************************************************/

ucharcard_anticoll()//防冲突函数

{

uchartemp,i;

resig_write(DecoderControl,0x28);

resig_write(Control,0x08);

resig_write(ChannelRedundancy,0x03);

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

Fbuff[0]=0x93;

Fbuff[1]=0x20;

fifo_write(2,Fbuff);

resig_write(Command,0x1e);

delay

(2);

temp=resig_read(FIFOLength);

if(temp==0x05)

{

temp=0;

fifo_read(5,UID);

delay

(1);

for(i=0;i<5;i++)

{

temp=temp^UID[i];

}

if(temp==0)

return1;

else

return0;

}

else

return0;

}

/*********************************************************/

//解释:

选卡函数

//

//

//输入:

//

//

//输出:

选卡成功的话返回真值,否则返回0

//

//实现功能:

将要交易的卡号发给卡,如果回应正确则选卡成功

/********************************************************/

ucharcard_select()

{

uchari,temp;

resig_write(ChannelRedundancy,0x0f);

resig_write(Control,0x08);

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

Fbuff[0]=0x93;

Fbuff[1]=0x70;

for(i=0;i<5;i++)

{

Fbuff[i+2]=UID[i];

}

fifo_write(7,Fbuff);

resig_write(Command,0x1e);

delay

(2);

temp=resig_read(FIFOLength);

if(temp==0x01)

return1;

else

return0;

}

/*********************************************************/

//解释:

三重认证函数

//

//

//输入:

要操作的块号

//

//

//输出:

如果三重认证的话返回真值,否则返回0

//

//实现功能:

这是卡片与读卡器之间的默认通信协议,次程序执行成功后才可以与卡进行交易

/********************************************************/

ucharcard_authtication(ucharm)

{

uchari,temp;

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

Fbuff[0]=0x60;

Fbuff[1]=m;

for(i=0;i<4;i++)

{

Fbuff[i+2]=UID[i];

}

fifo_write(6,Fbuff);

resig_write(InterruptEn,0xa5);

resig_write(Command,0x0c);

delay

(2);

temp=resig_read(SecondaryStatus);

temp=temp&0x07;

if(temp==0)

{

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

resig_write(InterruptEn,0xa4);

resig_write(Command,0x14);

delay

(2);

temp=temp=resig_read(Control);

temp&=0x08;

if(temp==0x08)

return1;

else

return0;

}

return0;

}

/*********************************************************/

//解释:

读卡函数

//

//

//输入:

要读的考号

//

//

//输出:

读卡成功的话返回真值,否则返回0

//

//实现功能:

读取指定卡号的内容,并传送给FIFO

/********************************************************/

ucharcard_read(ucharm)

{

uchartemp;

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

resig_write(ChannelRedundancy,0x0f);

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

Fbuff[0]=0x30;

Fbuff[1]=m;

fifo_write(2,Fbuff);

resig_write(Command,0x1e);

delay(0x04);

temp=resig_read(FIFOLength);

if(temp==16)

{

fifo_read(16,Jbuff);

return1;

}

else

return0;

}

/*********************************************************/

//解释:

写卡函数

//

//

//输入:

要写入的卡号

//

//

//输出:

写卡成功的话返回真值,否则返回0

//

//实现功能:

将发送缓冲区的数据写入指定的块号

/********************************************************/

ucharcard_write(ucharm)

{

uchartemp;

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

Fbuff[0]=0xA0;

Fbuff[1]=m;

fifo_write(2,Fbuff);

resig_write(Command,0x1e);

delay

(2);

temp=resig_read(FIFOLength);

if(temp==1)

{

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

Fbuff[0]=Jbuff[0];

fifo_write(16,Fbuff);

resig_write(Command,0x1e);

delay(10);

temp=resig_read(FIFOLength);

if(temp==1)

return1;

else

return0;

}

else

return0;

}

/*********************************************************/

//解释:

加载密匙函数

//

//

//输入:

//

//

//输出:

成功的话返回真值,否则返回0

//

//实现功能:

建立与卡通信协议的第一步,将密匙发送给卡,验证密匙是否成功,成功的话执行三重认证

/********************************************************/

ucharloadkey()

{

uchartemp;

temp=resig_read(Control);

temp=temp|0x01;

resig_write(Control,temp);

fifo_write(12,changekey);

resig_write(Command,0x19);

delay(3);

temp=resig_read(ErrorFlag);

if(temp==0)

return1;

else

return0;

}

/*********************************************************/

//解释:

设置卡的暂停态函数

//

//

//输入:

//

//

//输出:

执行成功的话返回真值,否则返回0

//

//实现功能:

将卡设置为终止交易态,若要再次交易必须从头重新开始认证

/********************************************************/

voidcard_halt()

{

uchartemp;

resig_write(Interr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1