您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 基于51的FM1702射频卡程序
#includereg52.h#includeintrins.h#includeFM1702.h#defineucharunsignedchar#defineuintunsignedintucharFbuff[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~fucharcount=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;i8;i++){sck=0;if(m&0x80)mosi=1;elsemosi=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(tempcount)return0;else{for(i=0;icount;i++){temp=resig_read(FIFODaTa);*(s+i)=temp;}}return1;}/*********************************************************///解释:FIFO缓冲器的写函数//////输入:写入的字节个数,要写入的值的存放首地址//////输出://///********************************************************/voidfifo_write(ucharcount,uchar*s){uchari,temp;for(i=0;icount;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;i16;i++){Fbuff[i]=0;Jbuff[i]=0;}lcdclear();if(temp==0x00)return1;elsereturn0;}/*********************************************************///解释:延时函数,单次延时为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;elsereturn0;}/*********************************************************///解释:防冲突函数(我所做的此函数没有手册的那种功能,只是实现了单卡交易功能)//////输入://////输出:防冲突成功的话返回真值,否则返回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);del
本文标题:基于51的FM1702射频卡程序
链接地址:https://www.777doc.com/doc-2567915 .html