lyi619 发表于 2011-7-20 21:22:57

FM1702射频卡启动步骤

在做一个基于FM1702的ic射频卡,有点不明白它的原理。程序启动后,在重复寻卡处为什么要读取ErrorFlag然后读取FIFOLength并且当返回值是0x02时才可以下一步,否则一直重复上述步骤。这是读卡芯片与卡内部定义好的还是这个程序有问题啊?哪位做过帮个忙,指点一下,如果问题说得不清楚,我再补充。

lyi619 发表于 2011-7-25 20:36:28

我自己顶一下吧。
现在芯片可以初始化了,但是紧接着的寻卡,怎么都不行,我也参考了网上好多的例子,我觉得我的可以啊,但是怎么都没用,我把程序源代码贴在这,哪位大侠做过,热心帮帮忙,大家都理解那种想做找不到问题解决方法抓狂的心理,帮帮忙,谢谢了。
#include<reg52.h>
#include<intrins.h>
#include<FM1702.h>
#define ucharunsigned char
#define uint   unsigned int
uchar code keya[]={0xff,0xff,0xff,0xff,0xff,0xff};
uchar code keyb[]={0xff,0xff,0xff,0xff,0xff,0xff};//卡的密匙
uchar code changekey={0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f};//密匙变换存储区
uchar Fbuff;   //发送FIFO缓存
uchar Jbuff;   //接收FIFO缓存
uchar UID;      //卡型及卡号
uchar change;
sbitcs=P2^0;
sbitsck=P2^1;
sbitmosi=P2^2;
sbitmiso=P2^3;
sbitFM1702rst=P3^4;
sbit led9=P1^0;
sbit led8=P1^1;
sbit led7=P1^2;
sbit led6=P1^3;
sbit led5=P1^4;
/*****************函数声明**************************/
//voidspi_write(uchar m);
uchar spi(uchar m);
voidfifo_clear();
voidresig_write(uchar reg,uchar da);
uchar resig_read(uchar reg);
ucharfifo_read(uchar count,uchar *s);
voidfifo_write(uchar count,uchar *s);
uchar inti_1702();
voidcard_halt();
voidcard_scan();
uchar card_type();
uchar card_anticoll();
voidcard_select();
voidcard_authtication1(uchar m);
uchar card_authtication2();
uchar card_read(uchar m);
uchar card_write(uchar m);
uchar card_incre(uchar m,uchar x);
uchar card_dec(uchar m,uchar x);
ucharloadkey();
voiddelay(uchar m);
ucharHL_card_active(uchar picc_k);




uchar spi(uchar m)
{
        uchar i,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;
}
void resig_write(uchar reg,uchar da)
{
        sck=0;
        reg<<=1;
        cs=0;
        reg=reg&0x7e;
        spi(reg);
        spi(da);
        cs=1;
}
uchar resig_read(uchar reg)
{
        uchar temp;
        sck=0;
        _nop_();
        _nop_();
        cs=0;
        reg<<=1;
        reg|=0x80;
        spi(reg);
        temp=spi(0x00);
        cs=1;
        return temp;
       
}
voidfifo_clear()
{
        uchar temp;
        temp=resig_read(Control);
        temp=temp|0x01;
        resig_write(Control,temp);
        while(resig_read(FIFOLength))        ;
}
uchar fifo_read(uchar count,uchar *s)
{
        uchar i,temp;
        temp=resig_read(FIFOLength);
        if(temp<count)
        return0;
        else
        {
                for(i=0;i<count;i++)
                {                                       
                        temp=resig_read(FIFODaTa);
                        *(s+i)=temp;
                }               
        }

}
void fifo_write(uchar count,uchar *s)
{
        uchar i,temp;
    fifo_clear();
        for(i=0;i<count;i++)
        {
                temp=*(s+i);
                resig_write(FIFODaTa,temp);
        }
}
ucharinti_1702()
{
        uchar temp;
        FM1702rst=1;
    delay(0x0a);                       
        FM1702rst=0;                               
        delay(0x0f);
        temp=resig_read(Command);               
        if(temp==0)
        resig_write(0x00,0x80);                             
        temp=resig_read(Command);          
        while(temp);
        if(temp==0x00)          
        return 1;          
        else
        return 0;       
}
void delay(uchar m)    //单次定时为10ms
{
        TMOD=0x01;
        while(m--)
                {       
                       TH0=0xdc;
                       TL0=0x00 ;
                        TR0=1;
                        while(!TF0);
                        TF0=0;
                        TR0=0;
                }
          
}
voidcard_scan()
{
        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);
        Fbuff=0x52;                          
        fifo_write(1,Fbuff);                         
        resig_write(Command,0x1e);                                                                 
        fifo_clear();                                   
}
ucharcard_type()
{
        uchar temp1,temp2;
//        fifo_read(2,UID);
//        temp1=UID;
//        temp2=UID;
        temp1=resig_read(FIFOLength);   
//        if((temp1==0x04)||(temp1==0x03)||(temp1==0x05)||(temp1==0x53)&&(temp2==0))
//        {       
                if(temp1==0x02)
                return 0x01;
        //        fifo_read(2,UID);
//        }
        else
        return 0;
}
uchar card_anticoll()                                                        //防冲突函数
{
        uchar temp,i;
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x03);
        Fbuff=0x93;
        Fbuff=0x20;
        fifo_write(2,Fbuff);
        resig_write(Command,0x1e);
        delay(1);
        temp=resig_read(FIFOLength);
        if(temp==0x05)       
        {
                temp=0;
                fifo_read(5,UID);
                for(i=0;i<5;i++)
                {
                        temp=temp^UID;       
                }
                if(temp==0)
                return 1;
                else
                return 0;
        }
        else
        return 0;
}
void   card_select()
{
        uchar i;
        resig_write(Control,0x01);
        Fbuff=0x93;
        Fbuff=0x70;
        for(i=0;i<5;i++)
        {
                Fbuff=UID;
        }
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x0f);
        fifo_write(7,Fbuff);
        resig_write(Command,0x1e);
}
void    card_authtication1(uchar m)
{
        resig_write(Control,0x01);
        Fbuff=0x60;
        Fbuff=m;
        fifo_write(6,Fbuff);
        resig_write(Command,0x0c);
}
uchar    card_authtication2()
{
        uchar temp;
        resig_write(Control,0x01);
        resig_write(Command,0x14);
    temp=resig_read(Control);
        temp=temp&0x08;
        if(temp!=0x08)
        return 1;
        else
        return 0;
}
uchar card_read(uchar m)
{
        uchar temp;
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x0f);
        resig_write(Control,0x01);
        Fbuff=0x30;
        Fbuff=m;
        fifo_write(2,Fbuff);
        resig_write(Command,0x1e);
        temp=resig_read(FIFOLength);
        if(temp==16)
        {
                return 1;
                fifo_read(16,Jbuff);
        }
        else
        return 0;       
}
ucharcard_write(uchar m)
{
        uchar temp;
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x0f);
        resig_write(Control,0x01);
        Fbuff=0xA0;
        Fbuff=m;
        fifo_write(2,Fbuff);
        resig_write(Command,0x1e);
        temp=resig_read(FIFOLength);
        if(temp==1)
        {
                return 1;
                resig_write(Control,0x01);
                fifo_write(16,Fbuff);
        }
        else
        return 0;       
}
ucharcard_incre(uchar m,uchar x)
{
        uchar temp;
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x07);
        resig_write(Control,0x01);
        Fbuff=0xC1;
        Fbuff=m;
        fifo_write(2,Fbuff);
        resig_write(Command,0x1e);
        temp=resig_read(FIFOLength);
        if(temp==0x01)
        {
               
                resig_write(Control,0x01);
                change=x;
                fifo_write(4,change);
          resig_write(Command,0x1a);
                return 1;
        }
        return   0;
}
ucharcard_dec(uchar m,uchar x)
{
        uchar temp;
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x07);
        resig_write(Control,0x01);
        Fbuff=0xC0;
        Fbuff=m;
        fifo_write(2,Fbuff);
        resig_write(Command,0x1e);
        temp=resig_read(FIFOLength);
        if(temp==0x01)
        {
                return 1;
                resig_write(Control,0x01);
                change=x;
                fifo_write(4,change);
          resig_write(Command,0x1a);
        }
        return0;
}
uchar loadkey()
{
        uchar temp;
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x0f);
        resig_write(Control,0x01);
        fifo_write(12,changekey);
        resig_write(Command,0x19);
        temp=resig_read(ErrorFlag);
        temp=temp&0x40;
        if(temp==0x40)
        return 0;
        else
        return 0x01;
}
voidcard_halt()
{
        resig_write(CRCResultLSB,0x63);
        resig_write(CWConductance,0x3f);
        resig_write(ChannelRedundancy,0x03);
        resig_write(Control,0x01);
        Fbuff=0x50;
        Fbuff=0x00;
        fifo_write(2,Fbuff);
        resig_write(Command,0x1e);
}
ucharHL_card_active(uchar picc_k)
{
        uchar temp;
    card_halt();
        temp=card_type();
        if(temp)
        {
                temp=card_anticoll();
                if(temp)
                {
                  card_select();
                  card_authtication1(picc_k);
                        if(temp)
                        {
                                temp=card_authtication2();
                                if(temp)
                                {
                                        temp=loadkey();
                                        if(temp)
                                        {
                                        return 1          ;
                                        }
                                }       
                        }       
                }
        }

return 0;
}
void main()
{
        uchar temp;

        temp=inti_1702();
        if(temp==0x01)
        led9=0;       
        card_scan();
        temp=resig_read(PrimaryStatus);
        temp=temp&0xf0;
        if(temp==0x00)
        led8=0;          
        while(1)
        {        card_scan();       
                temp=card_type();       
                if(temp==0x01)
                {
                          led8=~led8;
                          delay(0x0f);
                }       
        //        card_anticoll()        ;             
        }
}

目前程序状态寄存器说明读卡器和卡都是空闲状态,不明白,求解,拜托大侠们了。

sunnydragon 发表于 2011-7-26 15:51:25

我也在调试FM1702SL,不知道你的问题解决了吗?

lyi619 发表于 2011-7-27 09:58:14

回复【2楼】sunnydragon
-----------------------------------------------------------------------

你的调出来没?上边那个问题解决了,但是又遇到一个问题,三重认证最后一个认证执行后,程序如果重新开始执行只能执行到request
而且这个request还是有时候能有时候不能,后边防冲突什么的再也不行了,我不知道哪出问题了,好纳闷啊,只要注释掉三重认证最后一个认证,程序就可以循环执行。真奇怪。你做到什么程度了?

sunnydragon 发表于 2011-7-27 13:35:13

我比你速度慢点,现在芯片不能初始化,读Command寄存器一直是0xff,把示波器或者是万用表的表笔,往MISO引脚上一放,它就能读出0x00了,不明白为什么。可否加QQ互相讨论下。QQ:383016632

lyi619 发表于 2011-7-28 19:12:39

程序正常了,但是好纳闷啊?怎么只能执行一次啊,第二次就死在防冲突里边了,怎么回事啊???高手都去哪了啊?

stevenli 发表于 2011-7-28 21:12:02

1702网上大把的代码,在读IC卡这些芯片里面最简单的估计就是这个了

lyi619 发表于 2011-7-29 07:58:03

回复【6楼】stevenli
-----------------------------------------------------------------------

大把大把的倒是真的,你看过从网上下下来有用的吗?要是有,那你做的还是少啊

lovelywwei 发表于 2011-7-29 11:21:34

程序启动后,在重复寻卡处为什么要读取ErrorFlag然后读取FIFOLength并且当返回值是0x02时才可以下一步,否则一直重复上述步骤.
当然要这么做.查询方式下就是这样的.读FIFOLength返回值是0x02表示收到了卡片返回的数据,这两个字节代表卡片类型.像M1卡是0400.ErrorFlag也要判断看是不是有错误产生.你也可以用外部中断方式,需要配置RX中断有效,收到卡片数据后会触发中断.再在中断中去判断.
我没有玩过1702,但RC500,RC531,RC632,RC522,PN512之类的都差不多.

lyi619 发表于 2011-7-29 21:58:24

回复【8楼】lovelywwei
-----------------------------------------------------------------------

恩啊,现在做出来了,但是有一个问题啊,程序可以读,可以写,但是先读,然后写入数据不论用按键选的多少,写入的永远是160,这是怎么回事啊????求助
页: [1]
查看完整版本: FM1702射频卡启动步骤