找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32f103开发板RC522读写程序亲测可用

[复制链接]
跳转到指定楼层
楼主
ID:386213 发表于 2019-12-23 11:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "stm32f10x.h"
  2. #include "./usart/bsp_usart.h"        
  3. #include "./lcd/bsp_ili9341_lcd.h"
  4. #include "./flash/bsp_spi_flash.h"
  5. #include "./SysTick/bsp_SysTick.h"
  6. #include "rc522_config.h"
  7. #include "rc522_function.h"
  8. #include <stdbool.h>

  9. #define READ_WRITE_ENABLE    1    // 1:使能读写卡测试相关代码    0:禁用读写卡测试相关代码

  10. void IC_CMT ( uint8_t * UID, uint8_t * KEY, uint8_t RW, uint8_t * Dat );
  11. /**
  12.   * @brief  IC测试函数
  13.   * @param  无
  14.   * @retval 无
  15.   */
  16. uint8_t buffer[100];
  17. uint8_t buff2[]={0xFF ,0xFF, 0xFF, 0xFF, 0xFF, 0xFF};   // 卡A密钥
  18. uint8_t buff3[]={0x55 ,0xBA, 0xA9, 0x89, 0x50 ,0xBA, 0xA9, 0x89, 0x50 ,0xBA, 0xA9, 0x89, 0x50 ,0xBA, 0xA9, 0x89};    // 写入的数据
  19. void IC_test ( void )
  20. {
  21.         char cStr [ 30 ];
  22.   uint8_t ucArray_ID [ 4 ];    /*先后存放IC卡的类型和UID(IC卡序列号)*/                                                                                         
  23.         uint8_t ucStatusReturn;      /*返回状态*/                                                                                          
  24.   static uint8_t ucLineCount = 0;
  25.   int i;

  26.   while ( 1 )
  27.   {
  28.     /*寻卡*/
  29.                 if ( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK )  
  30.        /*若失败再次寻卡*/
  31.                         ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );                                                               

  32.                 if ( ucStatusReturn == MI_OK  )
  33.                 {
  34.       /*防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)*/
  35.                         if ( PcdAnticoll ( ucArray_ID ) == MI_OK )                                                                  
  36.                         {
  37.                                 sprintf ( cStr, "The Card ID is: %02X%02X%02X%02X",
  38.                   ucArray_ID [ 0 ],
  39.                   ucArray_ID [ 1 ],
  40.                   ucArray_ID [ 2 ],
  41.                   ucArray_ID [ 3 ] );

  42.                 printf ( "%s\r\n",cStr );

  43.           if ( ucLineCount == 0 )
  44.             LCD_SetTextColor(RED);         

  45.           ILI9341_DispStringLine_EN(LINE(ucLineCount) , (char* )cStr );

  46. #if READ_WRITE_ENABLE
  47.           /*******************卡片扇区读写测试****************************/
  48.           buff3[0]++;

  49.           printf ("开始读写卡测试:\n");

  50.           IC_CMT(ucArray_ID, buff2, 0, buff3);    // 写卡

  51.           IC_CMT(ucArray_ID, buff2, 1, buffer);   // 读卡

  52.           printf ("读到的数据为:\n");

  53.           for( i=0; i<16; i++)
  54.           {
  55.             printf ("0x%X  ", buffer[i]);    // 打印读到的数据
  56.           }
  57.           printf ("\n");
  58.           memset(buffer, 0, sizeof(buffer));

  59.           IC_CMT(ucArray_ID, buff2, 1, buffer);   // 需要在读一次下次再能写进去(不知道为什么这样,应该是这样能改变某种模式吧)
  60.           /*******************卡片扇区读写测试 END****************************/  
  61. #endif

  62.           ucLineCount ++;

  63.           if ( ucLineCount == 17 ) ucLineCount = 0;            
  64.                         }               
  65.                 }               
  66.   }        
  67. }
  68. char PcdComMF522 ( uint8_t ucCommand,
  69.                     uint8_t * pInData,
  70.                     uint8_t ucInLenByte,
  71.                     uint8_t * pOutData,
  72.                     uint32_t * pOutLenBit )        ;
  73. uint8_t buff[] = {0x06, 0x61, 0x1, 0x2, 0x3, 0x4 ,0x6, 0x1, 0x2, 0x3, 0x4};

  74. uint32_t bufferlen;
  75. /**
  76.   * @brief  主函数
  77.   * @param  无
  78.   * @retval 无
  79.   */
  80. int main(void)
  81. {        
  82.   /*滴答时钟初始化*/
  83.   SysTick_Init ();

  84.         /*LCD 初始化*/
  85.         ILI9341_Init ();  

  86.         /* USART config */
  87.         USART_Config();

  88.   /*RC522模块所需外设的初始化配置*/
  89.   RC522_Init ();     
  90.         
  91.   printf ( "WF-RC522 Test\n" );
  92.         
  93.   /*其中0、3、5、6 模式适合从左至右显示文字,*/
  94.         ILI9341_GramScan ( 6 );
  95.   LCD_SetFont(&Font8x16);
  96.         LCD_SetColors(BLACK,BLACK);

  97.   /* 清屏,显示全黑 */
  98.   ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);

  99.         /********显示字符串示例*******/
  100.   LCD_SetTextColor(RED);  

  101.   ILI9341_DispStringLine_EN(LINE(18),
  102.        (char* )"Please put the IC card on WF-RC522 antenna area ...");

  103.   LCD_SetTextColor(YELLOW);

  104.   PcdReset ();

  105.   /*设置工作方式*/   
  106.         M500PcdConfigISOType ( 'A' );

  107.   while(1)
  108.   {
  109.     /*IC卡检测        */   
  110.     IC_test ();     

  111.   }

  112. }


  113. /* ------------------------------------------end of file---------------------------------------- */

  114.   /*设置工作方式*/
  115.         M500PcdConfigISOType ( 'A' );
  116.         
  117.   while ( 1 )
  118.   {
  119.     /*IC卡检测*/
  120.     IC_test ();                        
  121.   }            
  122. }
  123. /**************
复制代码

F103_霸道开发板.7z

223.51 KB, 下载次数: 83, 下载积分: 黑币 -5

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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