找回密码
 立即注册

QQ登录

只需一步,快速开始

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

这是一个使用stm32使用mtr4048 读取UID的代码,有点乱,但是肯定调试好了

[复制链接]
跳转到指定楼层
楼主
ID:618258 发表于 2019-9-30 13:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是一个使用stm32使用mtr4048 读取身份证UID的代码,有点乱,但是肯定没问题

单片机源程序如下:
  1. //头文件调用
  2. #include "usually.h"
  3. #include "usart.h"
  4. #include "rtc.h"
  5. #include "stm32f10x_spi.h"
  6. //#include "define.h"
  7. #include "iso14443_4.h"
  8. #include "iso14443a.h"
  9. #include "mh523.h"
  10. #include "stdio.h"
  11. #include "mfrc522.h"
  12. //声明变量
  13. extern struct Data_Time  timer;
  14. void delay_t(int time)
  15. {
  16.         volatile int d = time * 10;
  17.         int i = 0;
  18.         for(;i < time; i++)
  19.                 d = d+1;
  20. }
  21. //函数申明
  22. void Init_LED(void);
  23. void Init_NVIC(void);
  24. void Delay_Ms(uint16_t time);  
  25. void Delay_Us(uint16_t time);


  26. #define SPI_FLASH_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
  27. #define SPI_FLASH_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)

  28. void SPI_FLASH_Init(void)
  29. {
  30.   SPI_InitTypeDef  SPI_InitStructure;
  31.   GPIO_InitTypeDef GPIO_InitStructure;

  32.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE);
  33.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

  34.   /* SCK */
  35.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  36.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  38.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  39.   /* MISO */
  40.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  41.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  42.   /* MOS */
  43.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  44.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  45.   /* CS  */
  46.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  47.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  48.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  49.   SPI_FLASH_CS_HIGH();

  50.   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  51.   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  52.   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  53.   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  54.   SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  55.   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  56.   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  57.   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  58.   SPI_InitStructure.SPI_CRCPolynomial = 7;
  59.   SPI_Init(SPI1, &SPI_InitStructure);

  60.   SPI_Cmd(SPI1, ENABLE);
  61. }


  62. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  63. ** 函数名称: main
  64. ** 功能

  65. 描述: 主函数入口
  66. ** 参数描述:无
  67. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  68. int main(void)
  69. {
  70.         uint8_t t=0;       
  71.         int ret = 0;
  72.         int fd;
  73.                 u8 status = 0;       
  74.         int block = 0;
  75.         u8 preaddata[16];
  76.                 int j = 0, i = block - block % 4;
  77.                 u8 pkey[] = {0x20, 0x10, 0x03, 0x22, 0x00, 0x00};
  78.         u8 psak;
  79.         u8  tag_type[16] = {0};

  80.         SystemInit();                                //系统时钟配置
  81.         Init_NVIC();                                //中断向量表注册函数
  82.         //Init_Usart();                                //串口引脚配置
  83.         //Usart_Configure(115200);        //串口配置 设置波特率为115200
  84.         Init_LED();                                        //LED初始化
  85.         SPI_FLASH_Init();

  86.         #if 1
  87.         //Init_RTC();                                        //内部RTC初始化
  88.         //Time_Update(2013,6,26,0,0,0);

  89. //        pcd_lpcd_start();
  90.         //sleep(1);
  91.          i = 0;
  92.         //for(;;)
  93.         //{
  94.         //        ret = ReadRawRC        (0x0 + i++);
  95.         //        status = ret;
  96.         //}
  97.                 PcdReset();
  98.      //PcdAntennaOff();
  99.      //PcdAntennaOn();  
  100.          __loop_mian:       
  101.         M500PcdConfigISOType('B');

  102.                 ///////////////////////////////////////
  103.          //status = PcdRequest(0x52, tag_type);

  104.          status = pcd_request_b(0x52, tag_type);
  105.         if (status != 0)
  106.         {                                       
  107.                 delay_t(100);
  108.                 PcdAntennaOff();
  109.                 goto __loop_mian;
  110.         }
  111.         #if 1

  112.                  status = pcd_attri_b(0x52, tag_type);
  113.         if (status != 0)
  114.         {                                       
  115.                 delay_t(100);
  116.                 goto __loop_mian;
  117.         }
  118.                  status = get_idcard_num(0x52, tag_type);
  119.         if (status != 0)
  120.         {                                       
  121.                 delay_t(100);
  122.                 goto __loop_mian;
  123.         }
  124.                 goto __loop_mian;
  125. /////////////////////////////////
  126.         status = PcdAnticoll(tag_type);
  127.         if(status != 0)
  128.         {
  129.                 delay_t(100);
  130.                 goto __loop_mian;
  131.         }

  132.         #if 1
  133. //        printf("ID %02X%02X%02X%02X\n", tag_type[0], tag_type[1], tag_type[2], tag_type[3]);
  134. ////////////////////////////
  135.         status =  PcdSelect(tag_type);
  136.                 if(status != 0)
  137.                         {
  138.                                 delay_t(100);
  139.                 goto __loop_mian;
  140.                         }

  141. //        printf("psak %02X\n", psak);


  142.         status = PcdAuthState(0x60, 7, pkey,tag_type );
  143.         if(status != 0)
  144.         {
  145.                                        
  146.                                         delay_t(100);
  147.                 goto __loop_mian;
  148.         }


  149.                        
  150.                         PcdRead(4,preaddata);
  151.                         if(status != 0)
  152.                         {
  153.                                
  154.                                         delay_t(100);
  155.                                         goto __loop_mian;
  156.                         }
  157.         LED1 = ~LED1;
  158.                                                 delay_t(10000);
  159.         LED1 = ~LED1;
  160.         #endif
  161.         goto __loop_mian;
  162.         #endif
  163.         #endif
  164. }
  165. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  166. ** 函数名称: LED_Init
  167. ** 功能描述: LED IO引脚配置
  168. ** 参数描述:无
  169. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  170. void Init_LED(void)
  171. {
  172.         GPIO_InitTypeDef GPIO_InitStructure;                                                                                        //定义一个GPIO结构体变量

  173.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);        //使能各个端口时钟,重要!!!
  174.        
  175.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;             //配置LED端口挂接到6、12、13端口
  176.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                   //通用输出推挽
  177.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;                   //配置端口速度为50M
  178.           GPIO_Init(GPIOA, &GPIO_InitStructure);                                           //将端口GPIOD进行初始化配置
  179. }
  180. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  181. ** 函数名称: NVIC_Configuration
  182. ** 功能描述: 系统中断配置
  183. ** 参数描述:无
  184. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  185. void Init_NVIC(void)
  186. {        
  187.         NVIC_InitTypeDef NVIC_InitStructure;

  188.         #ifdef  VECT_TAB_RAM                                                                          //向量表基地址选择

  189.           NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);                          //将0x20000000地址作为向量表基地址(RAM)
  190.         #else  

  191.           NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);                 //将0x08000000地址作为向量表基地址(FLASH)  
  192.         #endif
  193.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);                //先占优先级1位,从优先级3位

  194.         NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;                //RTC全局中断
  195.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;        //先占优先级1位,从优先级3位
  196.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        //先占优先级0位,从优先级4位
  197.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                //使能该通道中断
  198.         NVIC_Init(&NVIC_InitStructure);                //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  199. }
  200. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  201. ** 函数名称: Delay_Ms_Ms
  202. ** 功能描述: 延时1MS (可通过仿真来判断他的准确度)                       
  203. ** 参数描述:time (ms) 注意time<65535
  204. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  205. void Delay_Ms(uint16_t time)  //延时函数
  206. {
  207.         uint16_t i,j;
  208.         for(i=0;i<time;i++)
  209.                   for(j=0;j<10260;j++);
  210. }
  211. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  212. ** 函数名称: Delay_Ms_Us
  213. ** 功能描述: 延时1us (可通过仿真来判断他的准确度)
  214. ** 参数描述:time (us) 注意time<65535                                 
  215. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  216. void Delay_Us(uint16_t time)  //延时函数
  217. {
  218.         uint16_t i,j;
  219.         for(i=0;i<time;i++)
  220.                   for(j=0;j<9;j++);
  221. }
  222. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  223. End:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D
  224. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
复制代码

所有资料51hei提供下载:
测试程序:RTC万年历实验(串口接收 波特率115200).7z (209.35 KB, 下载次数: 7)

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

使用道具 举报

沙发
ID:618258 发表于 2019-9-30 13:32 | 只看该作者
有问题可以@我
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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