找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3082|回复: 3
收起左侧

STC8A8K单片机实现无源RC522模块读写卡程序+资料

  [复制链接]
ID:432059 发表于 2019-9-21 15:48 | 显示全部楼层 |阅读模式
STC最新的STC系列单片机可以实现RFID的读卡,写卡,擦除卡的操作,有源码和上位机

单片机源程序如下:
  1. /****************************************Copyright (c)****************************************************
  2. **                                       
  3. **                                 
  4. **
  5. **--------------File Info---------------------------------------------------------------------------------
  6. ** File name:                             main.c
  7. ** Last modified Date:         
  8. ** Last Version:                  
  9. ** Descriptions:                                                        
  10. **--------------------------------------------------------------------------------------------------------
  11. ** Created by:                        FiYu
  12. ** Created date:                2018-2-1
  13. ** Version:                            1.0
  14. ** Descriptions:                无源RFID MFRC522实验                        
  15. **--------------------------------------------------------------------------------------------------------
  16. ** Modified by:                        
  17. ** Modified date:               
  18. ** Version:                                 
  19. ** Descriptions:               
  20. ** Rechecked by:                        
  21. **********************************************************************************************************/

  22. #include <string.h>
  23. #include "uart.h"        
  24. #include "rc522.h"
  25. #include "UartComm.h"

  26. #define   READ_CARD         0x10
  27. #define   WRITE_CARD        0x11
  28. #define   MODIFY_PASSWORD   0x12
  29. #define   RECE_CMD         FrameReceInfo.buf[1]  //命令
  30. #define   BLOCK_NUM        FrameReceInfo.buf[2]  //块号

  31. /**********************
  32. 引脚别名定义
  33. ***********************/        
  34. sbit LED_B=P0^7;     //RGB蓝色LED用IO口P07

  35. xdata uint8 uartReceBuf[20];
  36. xdata uint8 WriteDat[16];
  37. //M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
  38. //4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
  39. //uint8_t code data2[4]  = {0,0,0,0x01};
  40. uint8 CardKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  41. uint8 NewKey[16];


  42. uint8 xdata g_ucTempbuf[20];  

  43. void RC522_Init(void)
  44. {
  45.         RC522_Reset();
  46.   PcdAntennaOff();
  47.   //PcdAntennaOn();
  48.         M500PcdConfigISOType( 'A' );
  49. }

  50. uint8 CardHanding(void)
  51. {
  52.         uint8 status,i;
  53.         
  54.         status = RC522_Request(PICC_REQIDL, g_ucTempbuf);//寻卡,返回卡的类型:2个字节
  55.         if(status != MI_OK) return 0;        
  56.   if(RECE_CMD == READ_CARD)for(i=0;i<2;i++)U1SendData(g_ucTempbuf[i]);         //发送卡类型,2个字节        只有接收到读卡命令才发送
  57.         status = RC522_Anticoll(g_ucTempbuf);//防冲撞,返回卡的序列号:4字节        
  58.         if(status != MI_OK) return 0;        
  59.         if(RECE_CMD == READ_CARD)for(i=0;i<4;i++)U1SendData(g_ucTempbuf[i]);   //发送卡号,4个字节  只有接收到读卡命令才发送
  60.         status = PcdSelect(g_ucTempbuf);//选定卡片
  61.         if(status != MI_OK) return 0;
  62.         
  63.         switch(RECE_CMD) //解析命令
  64.         {
  65.                 case READ_CARD: // 读卡
  66.                         memcpy(CardKey,&FrameReceInfo.buf[3],6); //拷贝密码
  67.                         status = PcdAuthState(PICC_AUTHENT1A, BLOCK_NUM, CardKey, g_ucTempbuf);//验证卡片密码
  68.                   if(status != MI_OK) return 0;
  69.                   status = PcdRead(BLOCK_NUM, g_ucTempbuf);//读块
  70.                   if(status != MI_OK) return 0;
  71.                   for(i=0;i<16;i++)U1SendData(g_ucTempbuf[i]); //发送读出的内容,16个字节
  72.       PcdHalt();                                 
  73.                   break;
  74.                
  75.                 case WRITE_CARD: //写卡
  76.                         memcpy(CardKey,&FrameReceInfo.buf[3],6);   //拷贝密码
  77.                   memcpy(WriteDat,&FrameReceInfo.buf[9],16); //拷贝待写入的数据
  78.                         status = PcdAuthState(PICC_AUTHENT1A, BLOCK_NUM, CardKey, g_ucTempbuf);//验证卡片密码
  79.                   if(status != MI_OK) return 0;
  80.                   status = PcdWrite(BLOCK_NUM, WriteDat);    //写块
  81.                   if(status != MI_OK) return 0;
  82.                   PcdHalt();
  83.                   break;
  84.                
  85.                 case MODIFY_PASSWORD: //修改密码
  86.       memcpy(CardKey,&FrameReceInfo.buf[3],6); //拷贝待写入的数据
  87.                   for(i=0;i<6;i++)NewKey[i] = FrameReceInfo.buf[9+i];//拷贝新密码
  88.                   for(i=0;i<6;i++)NewKey[i+10] = FrameReceInfo.buf[9+i];
  89.                   NewKey[6] = 0xFF;
  90.                   NewKey[7] = 0x07;
  91.                   NewKey[8] = 0x80;
  92.                   NewKey[9] = 0x69;

  93.                   status=PcdAuthState(PICC_AUTHENT1A,BLOCK_NUM,CardKey,g_ucTempbuf);
  94.       if(status!=MI_OK)  return 0;
  95.       status=PcdWrite(BLOCK_NUM,&NewKey[0]);
  96.       if(status!=MI_OK)
  97.       return 0;                           
  98.       PcdHalt();
  99.                         break;
  100.         }
  101.         return 0;
  102. }
  103. /***************************************************************************
  104. * 描  述 : 主函数
  105. * 入  参 : 无
  106. * 返回值 : 无
  107. **************************************************************************/
  108. int main()           
  109. {     
  110.         P3M1 &= 0xFE;        P3M0 &= 0xFE;                          //设置P3.0为准双向口
  111.         P3M1 &= 0xFD;        P3M0 |= 0x02;                          //设置P3.1为推挽输出
  112.         
  113.         Uart1_Init();
  114.         RC522_Init();
  115.   EA = 1;

  116.   while(1)
  117.   {                     
  118.     if(UartReceFrame() == UART_FRAME_AVAIL)//串口接收到数据?
  119.                 {
  120.                         CardHanding();
  121.                         LED_B = ~LED_B;
  122.                 }
  123.         }
  124. }
复制代码

所有资料51hei提供下载:
无源RFID RC522实验.7z (7.61 MB, 下载次数: 186)
回复

使用道具 举报

ID:266164 发表于 2020-5-8 16:33 | 显示全部楼层
下载来看看如何
回复

使用道具 举报

ID:600469 发表于 2021-3-15 09:09 | 显示全部楼层
真棒  nice
回复

使用道具 举报

ID:34298 发表于 2024-3-7 16:23 | 显示全部楼层
你反应  读不了卡 不知道啥原因
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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