找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM8单片机+RC522电路原理图+读写卡源代码

[复制链接]
跳转到指定楼层
楼主
基于stm8的ds1302模块千万别把数据写在第3个区,不然卡就锁住了,只能写在第2个区

电路原理图如下:




stm8单片机源码:
  1. /* Includes ------------------------------------------------------------------*/
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "stm8s.h"
  4. #include "stm8s_clk.h"
  5. #include "intrinsics.h"
  6. #include "stm8s_uart1.h"
  7. #include "uart.h"
  8. #include "rc522.h"
  9. #include "string.h"
  10. #include "IWDG.h"
  11. #include "tim2.h"
  12. #include "1602i2c.h"
  13. void Delay(u32 nCount);
  14. extern u8 RxBuffer[RxBufferSize];
  15. extern u8 UART_RX_NUM;
  16. extern int time2;
  17. extern int tim2test;
  18. extern int tim2test1;
  19. unsigned char CT[2];//卡类型
  20. unsigned char SN[4]; //卡号
  21. unsigned char M1UID[8]={0x2e,0x55,0,0,0,0,0,0};//读卡器向上发送,读到卡后自动发
  22. extern unsigned char controlflag;//接收到控制LED beep指令标志   01表示收到
  23. extern unsigned char sendflag;//发送标志  =1表示还未发送过 =0表示已经发送一次  等待回复  如果超时重新发送
  24. unsigned char write[16] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};
  25. unsigned char read[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  26. extern unsigned char receive[8];
  27. u8 read_flag=0;//读卡标志位  1;读到卡  0;还没读到卡
  28. u8 time_flag=0;//超时标志 0;未超时   1:超时
  29. //unsigned char key[6] = {0x19,0x84,0x07,0x15,0x76,0x14};
  30. unsigned char key[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
  31. int sum=0;
  32. //unsigned char key[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
  33. /* Private macro -------------------------------------------------------------*/
  34. #define countof(a) (sizeof(a) / sizeof(*(a)))
  35. #define  BufferSize (countof(Tx_Buffer)-1)
  36. /* Private variables ---------------------------------------------------------*/
  37. u8 Tx_Buffer[] = "请刷卡";
  38. u8 Rx_Buffer[BufferSize];
  39. u32 FLASH_ID ;
  40. /* Private defines -----------------------------------------------------------*/
  41. /* Private function prototypes -----------------------------------------------*/
  42. void cardNo2String(u8 *cardNo, u8 *str);
  43. /* Private functions ---------------------------------------------------------*/
  44. #define ReadWriteAddr 3                       //读写扇区
  45. void SN_UID(void)
  46. {
  47.   unsigned int tempUID;
  48.    M1UID[2]=SN[0];
  49.    M1UID[3]=SN[1];
  50.    M1UID[4]=SN[2];
  51.    M1UID[5]=SN[3];
  52.    tempUID=M1UID[1]+M1UID[2]+M1UID[3]+M1UID[4]+M1UID[5];
  53.    M1UID[6]=tempUID&0x00ff;
  54.    M1UID[7]=tempUID>>8;
  55.   
  56. }
  57. void rxfromPIC(void)
  58. {
  59.   
  60.   u8 i;
  61.   sum=receive[1]+receive[2]+receive[3]+receive[4]+receive[5];

  62.   if((receive[0]==0x7e)&&(receive[1]==0x55)&&(receive[6]==(sum&0x00ff))&&(receive[7]==(sum&0xff00)>>8))
  63.   {
  64.    
  65.     read_flag=0;
  66.     if(receive[2]==0x01)
  67.     {

  68.     }
  69.     else if(receive[2]!=0x01)
  70.     {
  71.      
  72.     }
  73.      if(receive[3]==0x01)
  74.      {
  75.      
  76.      }
  77.     else if(receive[3]!=0x01)
  78.     {
  79.       

  80.     }
  81.    
  82.     if(receive[4]==0x01)
  83.     {
  84.       
  85.     }
  86.       receive[0]=0x7e;
  87.      UART1_SendString(receive, 8);
  88.      for(i=2;i<8;i++)
  89.         {
  90.          M1UID[i]=0;
  91.         }
  92.      tim2test1=0;//启动计时
  93.   }
  94.   
  95.   for(i=0;i<8;i++)
  96.   {
  97.     receive[i]=0;
  98.   }
  99.   if(tim2test1>5000)//超时全部关闭
  100.   {
  101.     tim2test1=0;
  102.           //  GPIO_HIGH(GPIOD, GPIO_PIN_3);
  103.       //  GPIO_HIGH(GPIOD, GPIO_PIN_7);
  104.         // GPIO_LOW(GPIOD, GPIO_PIN_2);
  105.   }
  106. }
  107. void main(void)
  108. {
  109.   unsigned char  status;

  110.   /*设置内部时钟16M为主时钟*/
  111.   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);

  112.   status = memcmp(read,write,16);//清0
  113.   InitRc522();//rc522初始化
  114.   sendflag=1;
  115.   I2C1602_Configuration();
  116.   init1602_i2c();
  117.   __enable_interrupt();//开启总中断
  118.   
  119.   //////////////
  120.   write[0] =1;//
  121.   write[1] =3;//
  122.   write[2] =2;//
  123.   write[3] =6;//
  124.   write[4] =7;//
  125.   write[5] =7;//
  126.   write[6] =6;//
  127.   write[7] =0;//
  128.   write[8] =2;//
  129.   write[9] =8;//
  130. write[10] =5;//
  131. ///////////////// 要写入的值,最多16个///////

  132.   while(1)
  133.   {

  134.     status = PcdRequest(PICC_REQALL,CT);     // 扫描卡
  135.     status= PcdAnticoll(SN);  
  136.     //防冲撞
  137.      rxfromPIC();
  138.    if (status==MI_OK)
  139.     {
  140.         SN_UID();
  141.         status = PcdSelect(SN);               //选择要操作的卡
  142.         if (status==MI_OK)
  143.         {   
  144.             if( MI_OK == PcdAuthState(0x61,ReadWriteAddr,key,SN) )        //验证A密码   
  145.              {     
  146.               //    status =  PcdWrite(2,write);//写入M1卡第2块块数据
  147.              status = PcdRead(2,read);//读取M1卡第2块块数据
  148.                 if(MI_OK == status)
  149.                 {

  150.               zl_i2c(0x80);
  151.               sj_i2c(0x30+ read[0]);
  152.               sj_i2c(0x30+ read[1]);
  153.               sj_i2c(0x30+ read[2]);
  154.               sj_i2c(0x30+ read[3]);
  155.               sj_i2c(0x30+ read[4]);
  156.               sj_i2c(0x30+ read[5]);
  157.               sj_i2c(0x30+ read[6]);
  158.               sj_i2c(0x30+ read[7]);
  159.               sj_i2c(0x30+ read[8]);
  160.               sj_i2c(0x30+ read[9]);
  161.               sj_i2c(0x30+ read[10]);
  162.                 }
  163.              }
  164.         }
  165.     }

  166.      
  167.    
  168. }
  169.   
  170.   
  171.   
  172. }

  173. void Delay(u32 nCount)
  174. {
  175.   /* Decrement nCount value */
  176.   while (nCount != 0)
  177.   {
  178.     nCount--;
  179.   }
  180. }


  181. void Hex2String(u8 hex,u8 *str)
  182. {
  183.   str[0] = (hex / 100) + '0';
  184.   str[1] = (hex % 100 / 10) + '0';
  185.   str[2] = (hex % 10) + '0';
  186. }

  187. void cardNo2String(u8 *cardNo, u8 *str)
  188. {
  189.     u8 Count = 0;
  190.     for(Count = 0; Count < 4; Count++)
  191.     {
  192.         Hex2String(cardNo[Count], str + Count * 4);
  193.         if(Count == 3)
  194.         {
  195.           str[15] = '\n';
  196.         }
  197.         else
  198.         {
  199.           str[Count * 4 + 3] = ':';
  200.         }
  201. ……………………

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

所有资料51hei提供下载:
第5周任务及代码.rar (15.54 MB, 下载次数: 203)




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

使用道具 举报

沙发
ID:447252 发表于 2018-12-16 15:19 | 只看该作者
谁有剩下的代码,能不能发份给我,多谢了,可发我QQ邮箱:156585370
回复

使用道具 举报

板凳
ID:487821 发表于 2019-5-15 11:51 | 只看该作者
解压错误,别下载了
回复

使用道具 举报

地板
ID:487821 发表于 2019-5-15 12:14 | 只看该作者
用win zip可以解压了
回复

使用道具 举报

5#
ID:607641 发表于 2020-1-5 10:45 | 只看该作者
谢谢正在学习
回复

使用道具 举报

6#
ID:233711 发表于 2020-1-5 15:55 | 只看该作者
很好的资料
回复

使用道具 举报

7#
ID:607641 发表于 2020-1-9 13:29 | 只看该作者
已经下载正在学习
回复

使用道具 举报

8#
ID:969958 发表于 2021-10-5 15:40 | 只看该作者
nice,learning...
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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