找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3922|回复: 3
打印 上一主题 下一主题
收起左侧

FM1702与单片机读卡写卡通讯程序源码+说明+注意事项+资料下载

[复制链接]
跳转到指定楼层
楼主
51单片机驱动FM1702实现读写卡的资料包分享:


FM1702天线设计与电路设计及编程指南等:



程序使用说明:
1.程序用到的数码管、FM1702、按键的所有引脚均有定义,如果用户按照这个定义,程序上电后应该是数  码管无显示内容。
2.当有卡片靠近后,数码管先显示卡号序列号的高两位,显示大约2秒,然后读取卡片的块8的数值并显示
  显示时间大约为6秒
3.按键是双功能键。按键2和3是充值和刷卡键,4是确认键,只有先按2或者3然后按4,否则直接按4程序  不会反应。当按了4后,此时按键2和3就变成增值或者减值按键,同时短按一下是增1或者减1,长按是  增10或者减10.确认了数值后,再次按4键,程序会根据你按得是充值键还是刷卡键进行响应的加或减运  算,并将运算后得值写入块8,然后再次读取,并显示在数码管上。在你按键确认数值之前,数码管会  显示当前你的按键值是多少。
4.总结使用流程就是:放卡到读卡器—按动2或3键—按4—再按2或3(长按短按效果不一样,数码管会显  示)—再按4—卡拿开


不足之处:
1.没有用到芯片自带的E2PROM,所以程序没有它的读写程序。
2.没有用到卡的自增自减运算函数,这个涉及卡的控制字符,卡的资料有详细,所以也没写
3.防衝突没有实现那种公平的防衝突,此程序实现的是随机选卡


注意事项:
网上关于射频卡的资料很多关键的地方都没有说明,类似SPI时序,寄存器读写指令,FIFO读写时间等等,所以下列几点一定要注意,这是我在做这个时遇到的问题。
1.首先,spi时序,注意读时和写时的sck,以及左移函数的位置,很重要。也可以把SPI的读和写分开写  ,会的自己试一下。
2.1702的寄存器读写并不是单纯的把寄存器地址写进去,而是有一定规则的。不论读写,寄存器都是先左  移一位,即有效的六位地址(做的时候你就明白为什麽是六位)处于一个字节的中间,最低位不论读写  都为0,高位当读时是1,写时是0,这点非常重要,查遍所有资料,都没有这点。
3.1702初始化时,按照pdf所给的启动步骤来,当时我觉得最后一条转换线性寻址好像没用到,所以没写  ,结果怎麽也不好使,后来一加,什麽都好使了。这也是很关键的一点。
4.延时问题,当FIFO有数据要发送或者接受时,启动0x1e命令后,根据FIFO字节数要加适当延时,这是很  关键的点,具体参考程序。


   大致就这些了,程序实现的功能比较简单,后续我也许会加上12864,做成一个界面化的带密码修改,以及卡的识别的小型一卡通系统,类似于学校那种什麽饭卡、交通、图书馆、水卡的东西,如果做的话,我会把后续的传上来。敬请期待....


51单片机源程序如下:

  1. #include<reg52.h>
  2. #include<intrins.h>
  3. #include<FM1702.h>
  4. #define uchar  unsigned char
  5. #define uint   unsigned int

  6. uchar Fbuff[16];                               //发送FIFO缓存
  7. uchar Jbuff[16];                              //接收FIFO缓存
  8. uchar UID[7];                                      //卡型及卡号
  9. uchar Data[4];                                  //按键值存储区
  10. uchar code  seg[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //数码管段选对应0~f
  11. uchar count=0           ;                         //按键回传的值
  12. uchar shuaka,chongzhi,keyflag=0;
  13. sbit k2=P3^5;                                         //充值‘加值键(实现加1或者加10)
  14. sbit k3=P3^2;                                         //刷卡’减值键(实现减1或者减10)
  15. sbit k4=P3^3;                                       //确认键
  16. sbit nWR=P2^4;                                      //74hc373片选
  17. /*****************1702函数声明**************************/
  18. uchar spi(uchar m);
  19. void  fifo_clear();
  20. void  resig_write(uchar reg,uchar da);
  21. uchar resig_read(uchar reg);
  22. uchar  fifo_read(uchar count,uchar *s);
  23. void  fifo_write(uchar count,uchar *s);
  24. uchar inti_1702();
  25. void  card_halt();
  26. uchar request();
  27. uchar card_anticoll();
  28. uchar card_select();
  29. uchar card_authtication(uchar m)         ;
  30. uchar card_read(uchar m);
  31. uchar card_write(uchar m);
  32. uchar loadkey();
  33. void  delay(uchar m);
  34. /********************按键显示函数声明***************/

  35. void display();
  36. void keyscan() ;
  37. void  data1deal() ;
  38. void  data2deal(uchar m);
  39. void lcdclear();
  40. /***********************子函数解释******************/
  41. /*********************************************************/
  42. //解释: 这是spi总线的读写时序,所有的寄存器操作基于此时序,非常重要
  43. //
  44. //
  45. //输入:要写入的16进制参数
  46. //
  47. //
  48. //输出:内部传回的16进制参数
  49. //
  50. /********************************************************/
  51. uchar spi(uchar m)
  52. {
  53.         uchar i,temp=0;
  54.         for(i=0;i<8;i++)
  55.         {
  56.                 sck=0;
  57.                 if(m&0x80)
  58.                 mosi=1;
  59.                 else
  60.                 mosi=0;
  61.                 m<<=1;
  62.                 sck=1;
  63.                 temp<<=1;
  64.                 if(miso)
  65.                 temp|=0x01;

  66.         }
  67.         sck=0;
  68.         mosi=0;
  69.         return  temp;
  70. }
  71. /*********************************************************/
  72. //解释:写寄存器函数
  73. //
  74. //
  75. //输入:寄存器地址 以及要写入的参数
  76. //
  77. //
  78. //输出:
  79. //
  80. //注意!!在所有的资料中都没有给出寄存器寻址时的格式,下边的有,看仔细了,如果连寄存器都找不到,后边的就不用看了
  81. /********************************************************/
  82. void resig_write(uchar reg,uchar da)
  83. {
  84.         sck=0;
  85.         reg<<=1;
  86.         cs=0;
  87.         reg=reg&0x7e;
  88.         spi(reg);
  89.         spi(da);
  90.         cs=1;
  91. }
  92. /*********************************************************/
  93. //解释:读寄存器函数
  94. //
  95. //
  96. //输入:寄存器地址
  97. //
  98. //
  99. //输出:该寄存器目前的值
  100. //
  101. //注意!!读写寄存器时指令不一样,仔细看。
  102. /********************************************************/
  103. uchar resig_read(uchar reg)
  104. {
  105.         uchar temp;
  106.         sck=0;
  107.         _nop_();
  108.         _nop_();
  109.         cs=0;
  110.         reg<<=1;
  111.         reg|=0x80;
  112.         spi(reg);
  113.         temp=spi(0x00);
  114.         cs=1;
  115.         return temp;
  116.         
  117. }
  118. /*********************************************************/
  119. //解释:FIFO缓衝器的读函数
  120. //
  121. //
  122. //输入:读取的字节个数 ,返回的值存放首地址
  123. //
  124. //
  125. //输出:读成功的话会返回真值,否则返回0
  126. //
  127. //
  128. /********************************************************/
  129. uchar fifo_read(uchar count,uchar *s)
  130. {
  131.         uchar i,temp;
  132.         temp=resig_read(FIFOLength);
  133.         if(temp<count)
  134.         return  0;
  135.         else
  136.         {
  137.                 for(i=0;i<count;i++)
  138.                 {                                       
  139.                         temp=resig_read(FIFODaTa);
  140.                         *(s+i)=temp;
  141.                 }               
  142.         }
  143.         return 1;
  144. }
  145. /*********************************************************/
  146. //解释:FIFO缓衝器的写函数
  147. //
  148. //
  149. //输入:写入的字节个数 ,要写入的值的存放首地址
  150. //
  151. //
  152. //输出:
  153. //
  154. //
  155. /********************************************************/
  156. void fifo_write(uchar count,uchar *s)
  157. {
  158.         uchar i,temp;
  159.         for(i=0;i<count;i++)
  160.         {
  161.                 temp=*(s+i);
  162.                 resig_write(FIFODaTa,temp);
  163.         }
  164. }
  165. /*********************************************************/
  166. //解释:芯片FM1702初始化函数
  167. //
  168. //
  169. //输入:
  170. //
  171. //
  172. //输出:初始化成功的话返回真值,否则返回0
  173. //
  174. //
  175. //注意:初始化步骤非常重要,一定要按照使用手册的启动步骤来,参考所给pdf
  176. /********************************************************/
  177. uchar  inti_1702()
  178. {
  179.         uchar temp,i;
  180.         FM1702rst=1;
  181.         mosi=1;
  182.         sck=1;
  183.     delay(20);                        
  184.         FM1702rst=0;                                 
  185.         delay(20);
  186.         while(resig_read(Command));
  187.         resig_write(0x00,0x80);                              
  188.         temp=resig_read(Command);            
  189.         while(temp);
  190.         resig_write(0x00,0x00);                                           //切换到线性寻址(此命令非常重要,否则无法启动发送 )
  191.         resig_write(TimerClock,0x0b);                           //address 2AH    /* 定时器週期设置寄存器 */   
  192.         resig_write(TimerControl,0x02);                           //address 2BH        /* 定时器控制寄存器 */   
  193.         resig_write(TimerReload,0x42);                       //address 2CH    /* 定时器初值寄存器 */         
  194.         resig_write(InterruptEn,0x7f);                    //address 06H    /* 中断使能/禁止寄存器 */     
  195.         resig_write(InterruptRq,0x7f);          //address 07H    /* 中断请求标识寄存器 */
  196.         resig_write(MFOUTSelect,0x02);                    //address 26H    /* mf OUT 选择配置寄存器 */     
  197.         resig_write(TxControl,0x5b);                    //address 11H    /* 发送控制寄存器 */
  198.         resig_write(RxControl2,0x01);
  199.         resig_write(RxWait,0x07);
  200.            for(i=0;i<16;i++)
  201.         {
  202.                 Fbuff[i]=0;
  203.                 Jbuff[i]=0;
  204.         }
  205.         lcdclear() ;
  206.         if(temp==0x00)           
  207.         return 1;         
  208.         else
  209.         return 0;         
  210. }
  211. /*********************************************************/
  212. //解释:延时函数,单次延时为5ms
  213. //
  214. //
  215. //输入:要延时的次数
  216. //
  217. //
  218. //输出:
  219. //
  220. //
  221. /********************************************************/
  222. void delay(uchar m)    //单次定时为10ms
  223. {
  224.         TMOD=0x01;
  225.         while(m--)
  226.                 {        
  227.                           TH0=0xfe;
  228.                          TL0=0x33 ;
  229.                         TR0=1;
  230.                         while(!TF0);
  231.                         TF0=0;
  232.                         TR0=0;
  233.                 }
  234.            
  235. }
  236. /*********************************************************/
  237. //解释:卡的回应函数,判断是否有卡在读写器跟前
  238. //
  239. //
  240. //输入:
  241. //
  242. //
  243. //输出:如果有卡在的话返回真值,否则返回0
  244. //
  245. //
  246. /********************************************************/
  247. uchar  request()
  248. {
  249.         uchar  temp;
  250.         resig_write(CRCResultLSB,0x63);
  251.         resig_write(CWConductance,0x3f);
  252.         resig_write(BitFraming,0x07);
  253.         resig_write(ChannelRedundancy,0x03);                  
  254.         temp=resig_read(Control);
  255.         temp=temp&0x7f;
  256.         resig_write(Control,temp);
  257.         resig_read(FIFOLength);
  258.         temp=resig_read(Control);
  259.         temp=temp|0x01;
  260.         resig_write(Control,temp);
  261.         Fbuff[0]=0x52;                           
  262.         fifo_write(1,Fbuff);                                                         
  263.         resig_write(Command,0x1e);               
  264.         delay(1);         
  265.         temp=resig_read(FIFOLength);
  266.         if(temp==0x02)
  267.         return 1;     
  268.         else
  269.         return 0;                           
  270. }
  271. /*********************************************************/
  272. //解释:防衝突函数 (我所做的此函数没有手册的那种功能,只是实现了单卡交易功能)
  273. //
  274. //
  275. //输入:
  276. //
  277. //
  278. //输出:防衝突成功的话返回真值,否则返回0
  279. //
  280. //实现功能:如果有多张卡在读卡器区域,只选中一张,并读取验证这张卡的ID,然后存储
  281. /********************************************************/
  282. uchar card_anticoll()                                                        //防衝突函数
  283. {
  284.         uchar temp,i;
  285.         resig_write(DecoderControl,0x28);                        
  286.         resig_write(Control,0x08);
  287.         resig_write(ChannelRedundancy,0x03);
  288.         temp=resig_read(Control);
  289.         temp=temp|0x01;
  290.         resig_write(Control,temp);                          
  291.         Fbuff[0]=0x93;
  292.         Fbuff[1]=0x20;
  293.         fifo_write(2,Fbuff);
  294.         resig_write(Command,0x1e);
  295.         delay(2);                                                         
  296.         temp=resig_read(FIFOLength);         
  297.         if(temp==0x05)        
  298.         {
  299.                 temp=0;
  300.                 fifo_read(5,UID);
  301.                 delay(1);               
  302.                 for(i=0;i<5;i++)
  303.                 {
  304.                         temp=temp^UID[i];        
  305.                 }
  306.                 if(temp==0)
  307.                 return 1;
  308.                 else
  309.                 return 0;
  310.         }
  311.         else
  312.                  return 0;        
  313. }
  314. /*********************************************************/
  315. //解释:选卡函数
  316. //
  317. //
  318. //输入:
  319. //
  320. //
  321. //输出:选卡成功的话返回真值,否则返回0
  322. //
  323. //实现功能:将要交易的卡号发给卡,如果回应正确则选卡成功
  324. /********************************************************/
  325. uchar   card_select()
  326. {
  327.         uchar i,temp;
  328.         resig_write(ChannelRedundancy,0x0f);
  329.         resig_write(Control,0x08);
  330.         temp=resig_read(Control);
  331.         temp=temp|0x01;
  332.         resig_write(Control,temp);
  333.         Fbuff[0]=0x93;
  334.         Fbuff[1]=0x70;
  335.         for(i=0;i<5;i++)
  336.         {
  337.                 Fbuff[i+2]=UID[i];
  338.         }        
  339.         fifo_write(7,Fbuff);
  340.         resig_write(Command,0x1e);
  341.         delay(2);
  342.         temp=resig_read(FIFOLength);   
  343.         if(temp==0x01)
  344.         return 1          ;
  345.         else
  346.         return 0         ;
  347. }
  348. /*********************************************************/
  349. //解释:三重认证函数
  350. //
  351. //
  352. //输入:要操作的块号
  353. //
  354. //
  355. //输出:如果三重认证的话返回真值,否则返回0
  356. //
  357. //实现功能:这是卡片与读卡器之间的默认通信协议,次程序执行成功后才可以与卡进行交易
  358. /********************************************************/
  359. uchar    card_authtication(uchar m)
  360. {
  361.         uchar i,temp;
  362.         temp=resig_read(Control);
  363.         temp=temp|0x01;
  364.         resig_write(Control,temp);
  365.         Fbuff[0]=0x60;
  366.         Fbuff[1]=m;
  367.         for(i=0;i<4;i++)
  368.         {
  369.                 Fbuff[i+2]=UID[i];
  370.         }
  371.         fifo_write(6,Fbuff);
  372.         resig_write(InterruptEn,0xa5);
  373.         resig_write(Command,0x0c);
  374.         delay(2);
  375.         temp=resig_read(SecondaryStatus);
  376.         temp=temp&0x07;                                                
  377.         if(temp==0)
  378.         {
  379.                 temp=resig_read(Control);
  380.                 temp=temp|0x01;
  381.                 resig_write(Control,temp);
  382.                 resig_write(InterruptEn,0xa4);
  383.                 resig_write(Command,0x14);               
  384.                 delay(2);
  385.                 temp=temp=resig_read(Control);
  386.                 temp&=0x08;
  387.                 if(temp==0x08)
  388.                 return 1;
  389.                 else
  390.                 return 0;               
  391.         }
  392.         return  0;
  393. }
  394. /*********************************************************/
  395. //解释:读卡函数
  396. //
  397. //
  398. //输入:要读的考号
  399. //
  400. //
  401. //输出:读卡成功的话返回真值,否则返回0
  402. //
  403. //实现功能:读取指定卡号的内容,并传送给FIFO
  404. /********************************************************/
  405. uchar card_read(uchar m)
  406. {
  407.         uchar temp;
  408.         temp=resig_read(Control);
  409.         temp=temp|0x01;
  410.         resig_write(Control,temp);
  411.         resig_write(ChannelRedundancy,0x0f);
  412.         temp=resig_read(Control);
  413.         temp=temp|0x01;
  414.         resig_write(Control,temp);
  415.         Fbuff[0]=0x30;
  416.         Fbuff[1]=m;
  417.         fifo_write(2,Fbuff);
  418.         resig_write(Command,0x1e);
  419.         delay(0x04);                                       
  420.         temp=resig_read(FIFOLength);
  421.         if(temp==16)
  422.         {
  423.                
  424.                 fifo_read(16,Jbuff);        
  425.                 return 1;
  426.         }
  427.         else
  428.         return 0;        
  429. }
  430. /*********************************************************/
  431. //解释:写卡函数
  432. //
  433. //
  434. //输入:要写入的卡号
  435. //
  436. //
  437. //输出:写卡成功的话返回真值,否则返回0
  438. //
  439. //实现功能:将发送缓衝区的数据写入指定的块号
  440. /********************************************************/
  441. uchar  card_write(uchar m)
  442. {
  443.         uchar temp;
  444.         temp=resig_read(Control);
  445.         temp=temp|0x01;
  446.         resig_write(Control,temp);
  447.         Fbuff[0]=0xA0;
  448.         Fbuff[1]=m;
  449.         fifo_write(2,Fbuff);
  450.         resig_write(Command,0x1e);
  451.         delay(2);
  452.         temp=resig_read(FIFOLength);
  453.         if(temp==1)
  454.         {
  455.                 temp=resig_read(Control);
  456.                 temp=temp|0x01;
  457.                 resig_write(Control,temp);
  458.                 Fbuff[0]=Jbuff[0];
  459.                 fifo_write(16,Fbuff);
  460.                 resig_write(Command,0x1e);
  461.                 delay(10);        
  462.                 temp=resig_read(FIFOLength);
  463.                 if(temp==1)                        
  464.                 return 1;
  465.                 else
  466.                 return 0;
  467.         }
  468.         else
  469.         return 0;        
  470. }
  471. /*********************************************************/
  472. //解释:加载密匙函数
  473. //
  474. //
  475. //输入:
  476. //
  477. //
  478. //输出:成功的话返回真值,否则返回0
  479. //
  480. //实现功能:建立与卡通信协议的第一步,将密匙发送给卡,验证密匙是否成功,成功的话执行三重认证
  481. /********************************************************/
  482. uchar loadkey()
  483. {
  484.         uchar temp;
  485.         temp=resig_read(Control);
  486.         temp=temp|0x01;
  487.         resig_write(Control,temp);
  488.         fifo_write(12,changekey);
  489.         resig_write(Command,0x19);
  490.         delay(3);
  491.         temp=resig_read(ErrorFlag);
  492.         if(temp==0)
  493.         return 1;
  494.         else
  495.         return 0;
  496. }
  497. /*********************************************************/
  498. //解释:设置卡的暂停态函数
  499. //
  500. //
  501. //输入:
  502. //
  503. //
  504. //输出:执行成功的话返回真值,否则返回0
  505. //
  506. //实现功能:将卡设置为终止交易态,若要再次交易必须从头重新开始认证
  507. /********************************************************/
  508. void  card_halt()
  509. {
  510.         uchar temp;
  511.         resig_write(InterruptEn,0x7f);                        
  512.         resig_write(InterruptRq,0x7f);
  513.         resig_write(Command,0x00);
  514.         temp=resig_read(Control);
  515.         temp=temp|0x01;
  516.         resig_write(Control,temp);
  517.         Fbuff[0]=0x50;
  518.         Fbuff[1]=0x00;
  519.         fifo_write(2,Fbuff);
  520.         resig_write(InterruptEn,0xbd);
  521.         resig_write(Command,0x1e);
  522.         delay(2);
  523. }
  524. /****************按键显示子函数解释********************/
  525. /*********************************************************/
  526. //解释:卡的高两位序列号函数
  527. //
  528. //
  529. //输入:
  530. //
  531. //
  532. //输出:
  533. //
  534. //实现功能:将卡的高两位序列号处理为16进制后存放在相应显示区
  535. /********************************************************/
  536. void  data1deal()     //序列号处理
  537. {
  538.         uchar temp,i;
  539.         for(i=0;i<4;i++)
  540.         Data[i]=0;
  541.         temp=UID[3];
  542.         Data[1]=temp/16;
  543.            Data[0]=temp%16;
  544.         temp=UID[2];
  545.         Data[3]=temp/16;
  546.            Data[2]=temp%16;
  547. }
  548. /*********************************************************/
  549. //解释:显示数据处理函数
  550. //
  551. //
  552. //输入:
  553. //
  554. //
  555. //输出:
  556. //
  557. //实现功能:将要现实的数据处理为10进制后存放在相应显示区
  558. /********************************************************/
  559. void  data2deal(uchar m)     //显示数据及按键处理
  560. {
  561.         uchar i,temp;
  562.         for(i=0;i<4;i++)
  563.         Data[i]=0;
  564.         temp=m;
  565.         Data[0]=temp%10;
  566.         Data[1]=temp%100/10;
  567.         Data[2]=temp/100;        
  568. }
  569. /*********************************************************/
  570. //解释:按键扫描函数
  571. //
  572. //
  573. //输入:
  574. //
  575. //
  576. //输出:输出相应的交易种类以及交易数额(会在说明裡做详细说明)
  577. //
  578. //
  579. /********************************************************/
  580. void keyscan()
  581. {
  582.         if(k2==0||k3==0)
  583.         {
  584.                 if(k2==0)
  585.                 {
  586.                         delay(100);
  587.                         if(k2==0)
  588.                         delay(250);
  589.                         if(keyflag==1)
  590.                         {
  591.                                 if(k2==1)
  592.                                 {
  593.                                         if(count==255)
  594.                                         count=0         ;
  595.                                         else
  596.                                         count++;        
  597.                                 }
  598.                                 if(k2==0)
  599.                                 {
  600.                                         while(!k2)
  601.                                         display();
  602.                                         if(count>=246)
  603.                                         count=count+11;
  604.                                         else
  605.                                         count=count+10;        
  606.                                 }        
  607.                         }
  608.                         else
  609.                         {
  610.                                 chongzhi=1;
  611.                                 shuaka=0;
  612.                         }
  613.                 }
  614.                 if(k3==0)
  615.                 {
  616.                         delay(100);
  617.                         if(k3==0)
  618.                         delay(250);
  619.                         if(keyflag==1)
  620.                         {
  621.                                 if(k3==1)
  622.                                 {
  623.                                         if(count==0)
  624.                                         count=255;
  625.                                         else
  626.                                         count--;        
  627.                                 }
  628.                                 if(k3==0)
  629.                                 {
  630.                                         while(!k3)
  631.                                         display();
  632.                                         if(count<=9)
  633.                                         count=245+count;
  634.                                         else
  635.                                         count=count-10;        
  636.                                 }        
  637.                         }
  638.                         else
  639.                         {
  640.                                 shuaka=1;
  641.                                 chongzhi=0;
  642.                         }
  643.                
  644.                 }        
  645.         }
  646.         if(k4==0)
  647.                 {
  648.                         delay(180);
  649.                         if(k4==0)
  650.                         keyflag=keyflag+1;
  651.                 }
  652. }
  653. /*********************************************************/
  654. //解释:显示函数
  655. //
  656. //
  657. //输入:
  658. //
  659. //
  660. //输出:
  661. //
  662. //
  663. /********************************************************/               
  664. void display()
  665. {        
  666.          unsigned char time=0;
  667.         nWR=0;
  668.         P2=0x0f;
  669.         WR=0;        
  670.         P0=seg[Data[3]];
  671.         WR=1;        
  672.         P2=0X0e;
  673.         time=5000;                        
  674.         while(time--);
  675.          
  676.         nWR=0;
  677.         P2=0x0f;
  678.         WR=0;        
  679.         P0=seg[Data[2]];
  680.         WR=1;        
  681.         P2=0X0d;
  682.         time=5000;                        
  683.         while(time--);         
  684.                   
  685.         nWR=0;
  686.         P2=0x0f;
  687.         WR=0;        
  688.         P0=seg[Data[1]];
  689.         WR=1;        
  690.         P2=0X0b;
  691.         time=5000;                        
  692.         while(time--);
  693.                  
  694.         nWR=0;
  695.         P2=0x0f;
  696.         WR=0;        
  697.         P0=seg[Data[0]];
  698.         WR=1;        
  699.         P2=0X07;
  700.         time=5000;                        
  701.         while(time--);        
  702.         
  703. }
  704. /*********************************************************/
  705. //解释:lcd清屏函数
  706. //
  707. //
  708. //输入:
  709. //
  710. //
  711. //输出:
  712. //
  713. /********************************************************/
  714. void lcdclear()
  715. {
  716.          nWR=0;
  717.         P2=0x0f;
  718.         WR=0;        
  719.         P0=0xff;
  720.         WR=1;        
  721.         P2=0X0e;
  722.          
  723.         nWR=0;
  724.         P2=0x0f;
  725.         WR=0;        
  726.         P0=0xff;
  727.         WR=1;        
  728.         P2=0X0d;

  729.         nWR=0;
  730.         P2=0x0f;
  731.         WR=0;        
  732.         P0=0xff;
  733.         WR=1;        
  734.         P2=0X0b;
  735.                  
  736.         nWR=0;
  737.         P2=0x0f;
  738.         WR=0;        
  739.         P0=0xff;
  740.         WR=1;        
  741.         P2=0X07;

  742. }
  743. /*****************************主函数****************************/
  744. void  main()
  745. {
  746.         uchar temp,p,i,j;

  747.         temp=inti_1702();        
  748.         while(1)
  749.         {
  750.                 switch(temp)
  751.                 {
  752.                         case 0:        p=inti_1702();if(p) temp++;else temp=0;break;
  753.                         case 1: p=request(); if(p) {temp++;led5=~led5;}else temp=1;break;
  754.                         case 2: p=card_anticoll(); if(p){ temp++;led6=~led6;}else temp=1;break;
  755.                         case 3:        p=card_select(); if(p)  temp++; else temp=1;break;
  756.                         case 4:        p=loadkey();if(p){temp++;led7=~led7;} else temp=1;break;
  757.                         case 5:        p=card_authtication(PICC_BLOCK);if(p){ temp++;led8=~led8;}else temp=1;break;
  758.                         case 6: p=card_read(PICC_BLOCK);
  759.                         if(p)
  760.                         {
  761.                                 temp++;
  762.                                 data1deal();
  763.                                 for(i=0;i<2;i++)
  764.                                 for(j=0;j<150;j++)
  765.                                 display();
  766.                                 data2deal(Jbuff[0]);
  767.                                 i=10;
  768.                                 j=200;
  769.                                 while(i--)
  770.                                 {        
  771.                                         while(j--)
  772.                                         {        display();
  773.                                                 keyscan();
  774.                                                 if(shuaka||chongzhi)
  775.                                                 {        data2deal(count);
  776.                                                         display();
  777.                                                         if(keyflag==0x02)
  778.                                                         {
  779.                                                                 if(shuaka)
  780.                                                                 Jbuff[0]=Jbuff[0]-count        ;
  781.                                                                 if(chongzhi)
  782.                                                                 Jbuff[0]=Jbuff[0]+count;
  783.                                                                 keyflag=0;
  784.                                                                 shuaka=0;
  785.                                                                 chongzhi=0;
  786.                                                                 count=0;        
  787.                                                                 j=0;
  788.                                                                 i=0;
  789.                                                         }
  790.                                                           else
  791.                                                         j=100;
  792.                                                 }
  793.                                        
  794.                                         }
  795.                                 }
  796.                                 
  797.                         }
  798.                         else
  799.                         temp=0;break;
  800.                         case 7:        p=card_write(PICC_BLOCK);if(p){temp++;led9=~led9;} else temp=0;break;
  801.                         case 8:        p=card_read(PICC_BLOCK);
  802.                         if(p)
  803.                         {
  804.                                 temp++;
  805.                                 data1deal();
  806.                                 for(i=0;i<2;i++)
  807.                                 for(j=0;j<150;j++)
  808.                                 display();
  809.                                 data2deal(Jbuff[0]);
  810.                                 i=5;
  811.                                 j=200;
  812.                                 while(i--)
  813.                                 {        
  814.                                         while(j--)
  815.                                         display();
  816.                                 }
  817.                                 lcdclear() ;
  818.                         }
  819.                         else
  820.                         temp=0; break;
  821.                         case 9: card_halt();temp=0;break;
  822.                         default: break;
  823.                
  824.                 }
  825.         }
  826. }
复制代码

所有资料51hei提供下载:
基於51的FM1702射頻卡.rar (3.26 MB, 下载次数: 89)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2018-11-6 02:25 | 只看该作者
好资料,51黑有你更精彩!!!
回复

使用道具 举报

板凳
ID:593106 发表于 2020-5-12 17:14 | 只看该作者
感谢楼主分享,学习学习,参考参考
回复

使用道具 举报

地板
ID:313854 发表于 2024-5-14 11:53 | 只看该作者
太感谢了,代码好详细
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表