找回密码
 立即注册

QQ登录

只需一步,快速开始

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

关于M1卡的stm32源码与资料分享

[复制链接]
跳转到指定楼层
楼主
见附件

单片机源程序如下:
  1. #include "stm32f10x.h"
  2. #include "bsp_SysTick.h"
  3. #include "bsp_usart1.h"
  4. #include "rc522_config.h"
  5. #include "rc522_function.h"
  6. #include <stdbool.h>
  7. #include "bsp_lcd.h"

  8. unsigned char    DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  9. unsigned char                  data[16] = {0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x01,0xFE,0x01,0xFE};
  10. unsigned char                  data1[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
  11. unsigned char                 money_ok[4]={0x00,0x00,0x01,0x00};





  12.         char cStr [ 30 ];
  13.                 char cStr1 [ 30 ]={1,2,3,4,5,6,7,8,9};
  14.   u8 ucArray_ID [ 20 ];                                                                                             //先后存放IC卡的类型和UID(IC卡序列号)
  15.         u8 ucStatusReturn;                                                                                               //返回状态
  16.   static u8 ucLineCount = 0;
  17.   static u8 ucLineCount1 = 0;
  18.         unsigned char status;


  19. int Display(void){
  20.                 printf ( "%s\r\n",cStr );
  21.                 sprintf ( cStr, " %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X"//%02X%02X%02X%02X"
  22.                                                                                         ,ucArray_ID [ 0 ], ucArray_ID [ 1 ], ucArray_ID [ 2 ], ucArray_ID [ 3 ] , ucArray_ID [ 4 ]
  23.                                                                                         ,ucArray_ID [ 5 ], ucArray_ID [ 6 ], ucArray_ID [ 7 ], ucArray_ID [ 8 ] , ucArray_ID [ 9 ]
  24.                                                                                         ,ucArray_ID [ 10 ], ucArray_ID [ 11 ], ucArray_ID [ 12 ], ucArray_ID [ 13 ] , ucArray_ID [ 14 ]
  25.                                                                                         ,ucArray_ID [ 15 ]
  26.                 //, ucArray_ID [ 16 ], ucArray_ID [ 17 ], ucArray_ID [ 18 ] , ucArray_ID [ 19 ]
  27.                          );
  28.                 if ( ucLineCount == 0 )
  29.                         ILI9341_Clear ( 0, 0, 240, 271, macBACKGROUND);       
  30.                                
  31.                 ILI9341_DispString_EN ( 0, ucLineCount * 32, cStr, macBACKGROUND, macBLUE );
  32.                                
  33.                 ucLineCount ++;
  34.                                
  35.          if ( ucLineCount == 8 ) ucLineCount = 0;
  36.                
  37.         }



  38. void IC_test ( void )
  39. {

  40.         while(1){
  41.        
  42.                
  43.                 ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );//寻卡
  44.     if (ucStatusReturn != MI_OK){
  45.                 continue;}       
  46.                 /******************************屏蔽******************************/
  47.                
  48.                
  49.                 status = PcdAnticoll ( ucArray_ID );//防碰撞
  50. //                if (status != MI_OK){
  51. //                        continue;}       

  52.                 status = PcdSelect(ucArray_ID);//选定卡片
  53. //                if (status != MI_OK){
  54. //                        continue;}

  55.                 status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, ucArray_ID);//验证卡片密码
  56. //                if (status != MI_OK){
  57. //                        continue;}       
  58.                
  59.                
  60.                
  61.                
  62.                
  63. //                if(ucLineCount%2==1){
  64. //                        status = PcdWrite(1, data1);//写块1,数据为1122
  65. //                                if (status != MI_OK)
  66. //                                        {    continue;    }}
  67. //                else
  68. //                {
  69. //                status = PcdWrite(1, data);//写块1,数据为00FF,钱包模式
  70. //                        if (status != MI_OK)
  71. //                        {    continue;    }
  72. //                status = PcdWrite(2, data);//写块2,数据为00FF,钱包模式
  73. //                        if (status != MI_OK)
  74. //                        {    continue;    }
  75. //                status = PcdRead(1, ucArray_ID);//读块
  76. //                        Display();
  77. //                status = PcdRead(2, ucArray_ID);//读块
  78. //                        Display();
  79.                                
  80.                  
  81.                
  82.                
  83. /******************************************************************************************************       
  84.                 printf ( "%s\r\n",cStr );
  85.                 sprintf ( cStr, " %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X"//%02X%02X%02X%02X"
  86.                                                                                         ,ucArray_ID [ 0 ], ucArray_ID [ 1 ], ucArray_ID [ 2 ], ucArray_ID [ 3 ] , ucArray_ID [ 4 ]
  87.                                                                                         ,ucArray_ID [ 5 ], ucArray_ID [ 6 ], ucArray_ID [ 7 ], ucArray_ID [ 8 ] , ucArray_ID [ 9 ]
  88.                                                                                         ,ucArray_ID [ 10 ], ucArray_ID [ 11 ], ucArray_ID [ 12 ], ucArray_ID [ 13 ] , ucArray_ID [ 14 ]
  89.                                                                                         ,ucArray_ID [ 15 ]
  90.                 //, ucArray_ID [ 16 ], ucArray_ID [ 17 ], ucArray_ID [ 18 ] , ucArray_ID [ 19 ]
  91.                          );
  92.                 if ( ucLineCount == 0 )
  93.                         ILI9341_Clear ( 0, 0, 240, 271, macBACKGROUND);       
  94.                                
  95.                 ILI9341_DispString_EN ( 0, ucLineCount * 32, cStr, macBACKGROUND, macBLUE );
  96.                                
  97.                 ucLineCount ++;
  98.                                
  99.          if ( ucLineCount == 6 ) ucLineCount = 0;
  100.                 ******************************************************************************************************/
  101.                
  102.                
  103.                
  104.                 status = PcdValue(PICC_INCREMENT,2,money_ok);//充值
  105. //                status = PcdValue(PICC_INCREMENT,1,money_ok);//充值
  106. //                status = PcdValue(PICC_DECREMENT,1,money_ok);//扣款
  107.                
  108.                
  109. //                if (status != MI_OK)
  110. //        {   continue;    }
  111. //                status = PcdRead(1, ucArray_ID);//读块1
  112. //                if (status != MI_OK)
  113. //                {    continue;    }
  114. //                Display();
  115.                
  116.                 status = PcdRead(2, ucArray_ID);//读块2
  117.                 if (status != MI_OK)
  118.                 {    continue;    }
  119.                 Display();
  120.                         PcdHalt();        //休眠,无效,为自动寻卡模式;
  121.                
  122.                        
  123. }
  124. }



  125. /********************************************************
  126. void IC_test ( void )
  127. {
  128.         char cStr [ 30 ];
  129.   u8 ucArray_ID [ 4 ];                                                                                             //先后存放IC卡的类型和UID(IC卡序列号)
  130.         u8 ucStatusReturn;                                                                                               //返回状态
  131.   static u8 ucLineCount = 0;
  132.        
  133.        
  134.   while ( 1 )
  135.   {
  136.                 if ( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK )                                    //寻卡
  137.                         ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );                                                                 //若失败再次寻卡

  138.                 if ( ucStatusReturn == MI_OK  )
  139.                 {
  140.                         if ( PcdAnticoll ( ucArray_ID ) == MI_OK )                                                                   //防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)
  141.                         {
  142.                                 sprintf ( cStr, "The Card ID is: %02X%02X%02X%02X", ucArray_ID [ 0 ], ucArray_ID [ 1 ], ucArray_ID [ 2 ], ucArray_ID [ 3 ] );
  143.                                
  144.                                
  145.                                 printf ( "%s\r\n",cStr );
  146.                                

  147.                                 if ( ucLineCount == 0 )
  148.                                   ILI9341_Clear ( 0, 0, 240, 271, macBACKGROUND);       
  149.                                
  150.                                 ILI9341_DispString_EN ( 0, ucLineCount * 16, cStr, macBACKGROUND, macBLUE );
  151.                                
  152.                                 ucLineCount ++;
  153.                                
  154.                                 if ( ucLineCount == 17 ) ucLineCount = 0;
  155.                                
  156.                                
  157.                         }
  158.                        
  159.                 }
  160.                
  161.   }
  162.        
  163.        
  164. }
  165. ********************************************************/
  166. void ELE_money ( void )
  167. {
  168.                 while(1){
  169.        
  170.                
  171.                 ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );//寻卡
  172.     if (ucStatusReturn != MI_OK){
  173.                 continue;}       
  174.                
  175.                 status = PcdAnticoll ( ucArray_ID );//防碰撞
  176.                 if (status != MI_OK){
  177.                         continue;}       

  178.                 status = PcdSelect(ucArray_ID);//选定卡片
  179.                 if (status != MI_OK){
  180.                         continue;}

  181.                 status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, ucArray_ID);//验证卡片密码
  182.                 if (status != MI_OK){
  183.                         continue;}       
  184.                 if(ucLineCount%2==1){
  185.                         status = PcdWrite(1, data1);//写块1,数据为1122
  186.                                 if (status != MI_OK)
  187.                                         {    continue;    }}
  188.                 else
  189.                 {
  190.                 status = PcdWrite(1, data);//写块1,数据为00FF,钱包模式
  191.                         if (status != MI_OK)
  192.                         {    continue;    }
  193.                 status = PcdWrite(2, data);//写块2,数据为00FF,钱包模式
  194.                         if (status != MI_OK)
  195.                         {    continue;    }
  196.                 status = PcdRead(1, ucArray_ID);//读块
  197.                        
  198.                         Display();
  199.                        
  200.                 status = PcdRead(2, ucArray_ID);//读块
  201.                        
  202.                         Display();
  203. }}
  204. }

  205. void IC_test1 ( void )
  206. {
  207.         while ( 1 )
  208.   {
  209.                 if ( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK )                                    //寻卡
  210.                         ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );                                                                 //若失败再次寻卡

  211.                 if ( ucStatusReturn == MI_OK  )
  212.                 {
  213.                         if ( PcdAnticoll ( ucArray_ID ) == MI_OK )                                                                   //防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)
  214.                         {
  215.                                 if(PcdSelect(ucArray_ID)==MI_OK)
  216.                                 {
  217.                                                 if(PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, ucArray_ID))
  218.                                                 {
  219.                                                                         status = PcdValue(PICC_INCREMENT,2,money_ok);//充值
  220.                                                                         status = PcdValue(PICC_DECREMENT,1,money_ok);//扣款
  221.                                                                         if(PcdRead(1, ucArray_ID)==MI_OK)
  222.                                                                         {
  223.                                                                                         Display();
  224.                                                                         }
  225.                                                                         if(PcdRead(2, ucArray_ID)==MI_OK)
  226.                                                                         {
  227.                                                                                         Display();
  228.                                                                         }
  229.                                                                        
  230.                                                 }                               
  231.                                 }
  232.                         }                       
  233.                 }               
  234.   }
  235. }





  236. /**
  237.   * @brief  主函数
  238.   * @param  无
  239.   * @retval 无
  240.   */
  241. int main ( void )
  242. {
  243.         SysTick_Init ();   //滴答时钟初始化
  244.        
  245. ……………………

  246. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
RFID.rar (324.74 KB, 下载次数: 24)
M1卡介绍.pdf (409.84 KB, 下载次数: 19)




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

使用道具 举报

沙发
ID:464886 发表于 2019-1-8 20:40 | 只看该作者
谢谢分享
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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