找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3868|回复: 1
收起左侧

MH523芯片 MH1608C单片机开发程序源码

[复制链接]
ID:400401 发表于 2018-9-20 09:52 | 显示全部楼层 |阅读模式
MH1608C开发程序    RC523芯片
0.png

单片机源程序如下:
  1. /**
  2. ****************************************************************
  3. * @file rc523.c
  4. *
  5. * @brief  rc523 driver.
  6. *
  7. * @author
  8. *
  9. *
  10. ****************************************************************
  11. */

  12. /*
  13. * INCLUDE FILES
  14. ****************************************************************
  15. */
  16. #include "define.h"

  17. #include<stdlib.h>
  18. #include<stdio.h>
  19. #include<string.h>

  20. #include "stc_new_8051.h"
  21. #include "spi.h"
  22. #include "uart.h"
  23. #include "timer.h"
  24. #include "iso14443_4.h"
  25. #include "mh523.h"


  26. #define FIFO_SIZE        64
  27. #define FSD 256 //Frame Size for proximity coupling Device


  28. #define        READ_REG_CTRL        0x80
  29. #define        TP_FWT_302us        2048
  30. #define TP_dFWT        192

  31. #define MAX_RX_REQ_WAIT_MS        5000 // 命令等待超时时间100ms


  32. transceive_buffer XDATA mf_com_data;

  33. /**
  34. ****************************************************************
  35. * @brief pcd_init()
  36. *
  37. * 初始化芯片
  38. *
  39. * @param:   
  40. * @return:
  41. * @retval:
  42. ****************************************************************
  43. */
  44. void pcd_init()
  45. {
  46.         pcd_poweron();
  47.         mdelay(5);
  48.        
  49.         pcd_reset();
  50.         mdelay(5);
  51.         pcd_config('A');       
  52. }


  53. /**
  54. ****************************************************************
  55. * @brief pcd_config()
  56. *
  57. * 配置芯片的A/B模式
  58. *
  59. * @param: u8 type   
  60. * @return:
  61. * @retval:
  62. ****************************************************************
  63. */
  64. char pcd_config(u8 type)
  65. {
  66.         pcd_antenna_off();
  67.         mdelay(7);

  68.         if ('A' == type)
  69.         {
  70.             clear_bit_mask(Status2Reg, BIT3);
  71.                 clear_bit_mask(ComIEnReg, BIT7); // 高电平
  72.         write_reg(ModeReg,0x3D);        // 11 // CRC seed:6363
  73.         write_reg(RxSelReg, 0x86);//RxWait
  74.                 write_reg(RFCfgReg, 0x58); //
  75.                 write_reg(TxASKReg, 0x40);//15  //typeA
  76.                 write_reg(TxModeReg, 0x00);//12 //Tx Framing A
  77.                 write_reg(RxModeReg, 0x00);//13 //Rx framing A
  78.                 write_reg(0x0C, 0x10);        //^_^

  79.                 //兼容配置
  80.                 {
  81.                         u8 backup, adc;
  82.                         backup = read_reg(0x37);
  83.                         write_reg(0x37, 0x00);       
  84.                         adc = read_reg(0x37);
  85.                        
  86.                         if (adc == 0x11)
  87.                         {
  88.                                 write_reg(0x26, 0x48);
  89.                                 write_reg(0x37, 0x5E);
  90.                                 write_reg(0x17, 0x88);
  91.                                 write_reg(0x38, 0x6B);                       
  92.                                 write_reg(0x3A, 0x23);
  93.                                 write_reg(0x29, 0x12);//0x0F); //调制指数
  94.                                 write_reg(0x3b, 0x25);
  95.                         }                       
  96.                         else if (adc == 0x12)
  97.                         {
  98.                                 // 以下寄存器必须按顺序配置
  99.                                 write_reg(0x37, 0x5E);
  100.                                 write_reg(0x26, 0x48);
  101.                                 write_reg(0x17, 0x88);
  102.                                 write_reg(0x29, 0x12);//0x0F); //调制指数       
  103.                                 write_reg(0x35, 0xED);
  104.                                 write_reg(0x3b, 0xA5);
  105.                                 write_reg(0x37, 0xAE);
  106.                                 write_reg(0x3b, 0x72);
  107.                                
  108.                         }
  109.                         write_reg(0x37, backup);
  110.                 }
  111.                        
  112.         }
  113.         else if ('B' == type)
  114.         {
  115.                 write_reg(Status2Reg, 0x00);        //清MFCrypto1On
  116.                 clear_bit_mask(ComIEnReg, BIT7);// 高电平触发中断
  117.     write_reg(ModeReg, 0x3F);        // CRC seed:FFFF
  118.     write_reg(RxSelReg, 0x88);        //RxWait
  119.                 //Tx
  120.                 write_reg(GsNReg, 0xF8);        //调制系数
  121.                 write_reg(CWGsPReg, 0x3F);        //
  122.                 write_reg(ModGsPReg, 0x12);        //调制指数
  123.                 write_reg(AutoTestReg, 0x00);
  124.                 write_reg(TxASKReg, 0x00);        // typeB
  125.                 write_reg(TypeBReg, 0x13);
  126.                 write_reg(TxModeReg, 0x83);        //Tx Framing B
  127.                 write_reg(RxModeReg, 0x83);        //Rx framing B
  128.                 write_reg(BitFramingReg, 0x00);        //TxLastBits=0

  129.                 //兼容配置
  130.                 {
  131.                         u8 backup, adc;
  132.                         backup = read_reg(0x37);
  133.                         write_reg(0x37, 0x00);
  134.                         adc = read_reg(0x37);

  135.                         if (adc == 0x11)
  136.                         {
  137.                                 write_reg(0x37, 0x5E);
  138.                                 write_reg(0x17, 0x88);
  139.                                 write_reg(0x3A, 0x23);               
  140.                                 write_reg(0x38, 0x6B);       
  141.                                 write_reg(0x29, 0x12);//0x0F); //调制指数
  142.                                 write_reg(0x3b, 0x25);
  143.                         }
  144.                         else if (adc == 0x12)
  145.                         {       
  146.                                 write_reg(0x37, 0x5E);
  147.                                 write_reg(0x26, 0x48);
  148.                                 write_reg(0x17, 0x88);
  149.                                 write_reg(0x29, 0x12);
  150.                                 write_reg(0x35, 0xED);
  151.                                 write_reg(0x3b, 0xE5);//改为E5 write_reg(0x3b, 0xA5);
  152.                                 //write_reg(0x37, 0xAE);//去掉
  153.                                 //write_reg(0x3b, 0x72);//去掉
  154.                         }
  155.                         write_reg(0x37, backup);
  156.                 }
  157.         }
  158.         else
  159.         {
  160.                 return USER_ERROR;
  161.         }
  162.        
  163.         pcd_antenna_on();
  164.         mdelay(2);
  165.        
  166.         return MI_OK;
  167. }

  168. /**
  169. ****************************************************************
  170. * @brief pcd_com_transceive()
  171. *
  172. * 通过芯片和ISO14443卡通讯
  173. *
  174. * @param: pi->mf_command = 芯片命令字
  175. * @param: pi->mf_length  = 发送的数据长度
  176. * @param: pi->mf_data[]  = 发送数据
  177. * @return: status 值为MI_OK:成功
  178. * @retval: pi->mf_length  = 接收的数据BIT长度
  179. * @retval: pi->mf_data[]  = 接收数据
  180. ****************************************************************
  181. */
  182. char pcd_com_transceive(struct transceive_buffer *pi)
  183. {
  184.         u8 XDATA recebyte;
  185.         char XDATA status;
  186.         u8 XDATA irq_en;
  187.         u8 XDATA wait_for;
  188.         u8 XDATA last_bits;
  189.         u8 XDATA j;
  190.         u8 XDATA val;
  191.         u8 XDATA err;
  192.        
  193.         u8 irq_inv;
  194.         u16  len_rest;
  195.         u8  len;
  196.         u8 WATER_LEVEL;
  197.        
  198.         len = 0;
  199.         len_rest = 0;
  200.         err = 0;
  201.         recebyte = 0;
  202.         irq_en = 0;
  203.         wait_for = 0;

  204.         switch (pi->mf_command)
  205.         {
  206.           case PCD_IDLE:
  207.              irq_en   = 0x00;
  208.              wait_for = 0x00;
  209.              break;
  210.           case PCD_AUTHENT:   
  211.                 irq_en = IdleIEn | TimerIEn;
  212.                 wait_for = IdleIRq;
  213.                 break;
  214.           case PCD_RECEIVE:
  215.              irq_en   = RxIEn | IdleIEn;
  216.              wait_for = RxIRq;
  217.              recebyte=1;
  218.              break;
  219.           case PCD_TRANSMIT:
  220.              irq_en   = TxIEn | IdleIEn;
  221.              wait_for = TxIRq;
  222.              break;
  223.           case PCD_TRANSCEIVE:   
  224.                  irq_en = RxIEn | IdleIEn | TimerIEn | TxIEn;
  225.              wait_for = RxIRq;
  226.              recebyte=1;
  227.              break;
  228.           default:
  229.              pi->mf_command = MI_UNKNOWN_COMMAND;
  230.              break;
  231.         }

  232.         WATER_LEVEL = read_reg(WaterLevelReg);
  233.           
  234.         if (pi->mf_command != MI_UNKNOWN_COMMAND
  235.                 && (((pi->mf_command == PCD_TRANSCEIVE || pi->mf_command == PCD_TRANSMIT) && pi->mf_length > 0)
  236.                 || (pi->mf_command != PCD_TRANSCEIVE && pi->mf_command != PCD_TRANSMIT))
  237.                 )
  238.         {               
  239.                 write_reg(CommandReg, PCD_IDLE);
  240.                
  241.                 irq_inv = read_reg(ComIEnReg) & BIT7;
  242.                 write_reg(ComIEnReg, irq_inv |irq_en | BIT0);//使能Timer 定时器中断
  243.                 write_reg(ComIrqReg, 0x7F); //Clear INT
  244.                 write_reg(DivIrqReg, 0x7F); //Clear INT
  245.                 //Flush Fifo
  246.                 set_bit_mask(FIFOLevelReg, BIT7);
  247.                 if (pi->mf_command == PCD_TRANSCEIVE || pi->mf_command == PCD_TRANSMIT || pi->mf_command == PCD_AUTHENT)
  248.                 {
  249.                         #if (NFC_DEBUG)
  250.                         printf(" PCD_tx:");
  251.                         #endif
  252.                         for (j = 0; j < pi->mf_length; j++)
  253.                         {
  254.                                
  255.                                 #if (NFC_DEBUG)
  256.                                 printf("%02x ", (u16)pi->mf_data[j]);
  257.                                 #endif
  258.                         }
  259.                         #if (NFC_DEBUG)
  260.                         printf("\n");
  261.                         #endif
  262.                
  263.                         len_rest = pi->mf_length;
  264.                         if (len_rest >= FIFO_SIZE)
  265.                         {
  266.                                 len = FIFO_SIZE;
  267.                         }else
  268.                         {
  269.                                 len = len_rest;
  270.                         }
  271.                        
  272.                         for (j = 0; j < len; j++)
  273.                         {
  274.                                 write_reg(FIFODataReg, pi->mf_data[j]);
  275.                         }
  276.                         len_rest -= len;//Rest bytes
  277.                         if (len_rest != 0)
  278.                         {
  279.                                 write_reg(ComIrqReg, BIT2); // clear LoAlertIRq
  280.                                 set_bit_mask(ComIEnReg, BIT2);// enable LoAlertIRq
  281.                         }

  282.                         write_reg(CommandReg, pi->mf_command);
  283.                         if (pi->mf_command == PCD_TRANSCEIVE)
  284.                     {   
  285.                                 set_bit_mask(BitFramingReg,0x80);  
  286.                         }
  287.                
  288.                         while (len_rest != 0)
  289.                         {
  290.                                 while(INT_PIN == 0);//Wait LoAlertIRq               
  291.                                 if (len_rest > (FIFO_SIZE - WATER_LEVEL))
  292.                                 {
  293.                                         len = FIFO_SIZE - WATER_LEVEL;
  294.                                 }
  295.                                 else
  296.                                 {
  297.                                         len = len_rest;
  298.                                 }
  299.                                 for (j = 0; j < len; j++)
  300.                                 {
  301.                                         write_reg(FIFODataReg, pi->mf_data[pi->mf_length - len_rest + j]);
  302.                                 }

  303.                                 write_reg(ComIrqReg, BIT2);//在write fifo之后,再清除中断标记才可以
  304.                        
  305.                                 //printf("\n8 comirq=%02bx,ien=%02bx,INT= %bd \n", read_reg(ComIrqReg), read_reg(ComIEnReg), (u8)INT_PIN);
  306.                                 len_rest -= len;//Rest bytes
  307.                                 if (len_rest == 0)
  308.                                 {
  309.                                         clear_bit_mask(ComIEnReg, BIT2);// disable LoAlertIRq
  310.                                         //printf("\n9 comirq=%02bx,ien=%02bx,INT= %bd \n", read_reg(ComIrqReg), read_reg(ComIEnReg), (u8)INT_PIN);
  311.                                 }
  312.                         }
  313.                         //Wait TxIRq
  314.                         while (INT_PIN == 0);
  315.                         val = read_reg(ComIrqReg);
  316.                         if (val & TxIRq)
  317.                         {
  318.                                 write_reg(ComIrqReg, TxIRq);
  319.                                 if(pi->mf_command == PCD_TRANSMIT)
  320.                                 {
  321.                                         return 0;
  322.                                 }
  323.                         }
  324.                 }
  325.                 if (PCD_RECEIVE == pi->mf_command)
  326.                 {       
  327.                         set_bit_mask(ControlReg, BIT6);// TStartNow
  328.                 }
  329.        
  330.                 len_rest = 0; // bytes received
  331.                 write_reg(ComIrqReg, BIT3); // clear HoAlertIRq
  332.                 set_bit_mask(ComIEnReg, BIT3); // enable HoAlertIRq
  333.        
  334.                 //等待命令执行完成
  335.                 while(INT_PIN == 0);
  336.                
  337.        
  338.                 while(1)
  339.                 {
  340.                         while(0 == INT_PIN);
  341.                         val = read_reg(ComIrqReg);
  342.                         if ((val & BIT3) && !(val & BIT5))
  343.                         {
  344.                                 if (len_rest + FIFO_SIZE - WATER_LEVEL > 255)
  345.                                 {
  346.                                         #if (NFC_DEBUG)
  347.                                         printf("AF RX_LEN > 255B\n");
  348.                                         #endif
  349.                                         break;
  350.                                 }
  351.                         for (j = 0; j <FIFO_SIZE - WATER_LEVEL; j++)
  352.                         {
  353.                                         pi->mf_data[len_rest + j] = read_reg(FIFODataReg);
  354.                         }
  355.                                 write_reg(ComIrqReg, BIT3);//在read fifo之后,再清除中断标记才可以
  356.                                 len_rest += FIFO_SIZE - WATER_LEVEL;
  357.                         }
  358.                         else
  359.                         {
  360.                                 clear_bit_mask(ComIEnReg, BIT3);//disable HoAlertIRq
  361.                                 break;
  362.                         }                       
  363.                 }


  364.                 val = read_reg(ComIrqReg);
  365.                 #if (NFC_DEBUG)
  366.                 printf(" INT:fflvl=%d,rxlst=%02bx ,ien=%02bx,cirq=%02bx\n", (u16)read_reg(FIFOLevelReg),read_reg(ControlReg)&0x07,read_reg(ComIEnReg), val);//XU
  367.                 #endif
  368.                 write_reg(ComIrqReg, val);// 清中断
  369.                
  370.                 if (val & BIT0)
  371.                 {//发生超时
  372.                         status = MI_NOTAGERR;
  373.                 }
  374.                 else
  375.                 {
  376.                         err = read_reg(ErrorReg);
  377.                        
  378.                         status = MI_COM_ERR;
  379.                         if ((val & wait_for) && (val & irq_en))
  380.                         {
  381.                                 if (!(val & ErrIRq))
  382.                                  {//指令执行正确
  383.                                     status = MI_OK;

  384.                                     if (recebyte)
  385.                                     {
  386.                                                 val = 0x7F & read_reg(FIFOLevelReg);
  387.                                               last_bits = read_reg(ControlReg) & 0x07;
  388.                                                 if (len_rest + val > MAX_TRX_BUF_SIZE)
  389.                                                 {//长度过长超出缓存
  390.                                                         status = MI_COM_ERR;
  391.                                                         #if (NFC_DEBUG)
  392.                                                         printf("RX_LEN > 255B\n");
  393.                                                         #endif
  394.                                                 }
  395.                                                 else
  396.                                                 {       
  397.                                                         if (last_bits && val) //防止spi读错后 val-1成为负值
  398.                                                 {
  399.                                                    pi->mf_length = (val-1)*8 + last_bits;
  400.                                                 }
  401.                                                 else
  402.                                                 {
  403.                                                    pi->mf_length = val*8;
  404.                                                 }
  405.                                                         pi->mf_length += len_rest*8;

  406.                                                         #if (NFC_DEBUG)
  407.                                                         printf(" RX:len=%02x,dat:", (u16)pi->mf_length);
  408.                                                         #endif
  409.                                                 if (val == 0)
  410.                                                 {
  411.                                                    val = 1;
  412.                                                 }
  413.                                                 for (j = 0; j < val; j++)
  414.                                                 {
  415.                                                                 pi->mf_data[len_rest + j] = read_reg(FIFODataReg);
  416.                                                 }                                       

  417.                                                         #if (NFC_DEBUG)
  418.                                                     for (j = 0; j < pi->mf_length/8 + !!(pi->mf_length%8); j++)
  419.                                                 {
  420.                                                         //        if (j > 4)
  421.                                                         //        {
  422.                                                         //                printf("..");
  423.                                                         //                break;
  424.                                                         //        }
  425.                                                         //        else
  426.                                                         //        {
  427.                                                                         printf("%02X ", (u16)pi->mf_data[j]);
  428.                                                         //        }
  429.                                                                 //printf("%02X ", (u16)pi->mf_data[j]);
  430.                                                 }
  431.                                                         //printf("l=%d", pi->mf_length/8 + !!(pi->mf_length%8));
  432.                                                         printf("\n");
  433.                                                         #endif
  434.                                                 }
  435.                                     }
  436.                                  }                                       
  437.                                  else if ((err & CollErr) && (!(read_reg(CollReg) & BIT5)))
  438.                                  {//a bit-collision is detected                                        
  439.                                     status = MI_COLLERR;
  440.                                     if (recebyte)
  441.                                     {
  442.                                                 val = 0x7F & read_reg(FIFOLevelReg);
  443.                                               last_bits = read_reg(ControlReg) & 0x07;
  444.                                                 if (len_rest + val > MAX_TRX_BUF_SIZE)
  445.                                                 {//长度过长超出缓存
  446.                                                         #if (NFC_DEBUG)
  447.                                                         printf("COLL RX_LEN > 255B\n");
  448.                                                         #endif
  449.                                                 }
  450.                                                 else
  451.                                                 {
  452.                                                 if (last_bits && val) //防止spi读错后 val-1成为负值
  453.                                                 {
  454.                                                    pi->mf_length = (val-1)*8 + last_bits;
  455.                                                 }
  456.                                                 else
  457.                                                 {
  458.                                                    pi->mf_length = val*8;
  459.                                                 }               
  460.                                                         pi->mf_length += len_rest*8;
  461.                                                         #if (NFC_DEBUG)
  462.                                                         printf(" RX: pi_cmd=%02x,pi_len=%02x,pi_dat:", (u16)pi->mf_command, (u16)pi->mf_length);
  463.                                                         #endif
  464.                                                 if (val == 0)
  465.                                                 {
  466.                                                    val = 1;
  467.                                                 }
  468.                                                         for (j = 0; j < val; j++)
  469.                                                 {
  470.                                                                 pi->mf_data[len_rest + j +1] = read_reg(FIFODataReg);                               
  471.                                                 }                               
  472.                                                         #if (NFC_DEBUG)
  473.                                                 for (j = 0; j < pi->mf_length/8 + !!(pi->mf_length%8); j++)
  474.                                                 {
  475.                                                                 printf("%02X ", (u16)pi->mf_data[j+1]);
  476.                                                 }
  477.                                                         printf("\n");
  478.                                                         #endif
  479.                                                 }
  480.                                     }
  481.                                         pi->mf_data[0] = (read_reg(CollReg) & CollPos);
  482.                                         if (pi->mf_data[0] == 0)
  483.                                         {
  484.                                                 pi->mf_data[0] = 32;
  485.                                         }
  486.                                 #if(NFC_DEBUG)
  487.                                         printf("\n COLL_DET pos=%02x\n", (u16)pi->mf_data[0]);
  488.                                 #endif
  489.                                         pi->mf_data[0]--;// 与之前版本有点映射区别,为了不改变上层代码,这里直接减一;

  490.                                 }
  491.                                 else if ((err & CollErr) && (read_reg(CollReg) & BIT5))
  492.                                 {
  493.                                         //printf("COLL_DET,but CollPosNotValid=1\n");               
  494.                                 }
  495.                                 //else if (err & (CrcErr | ParityErr | ProtocolErr))
  496.                                 else if (err & (ProtocolErr))
  497.                                 {
  498.                                         #if (NFC_DEBUG)
  499.                                         printf("protocol err=%b02x\n", err);
  500.                                         #endif
  501.                                         status = MI_FRAMINGERR;                               
  502.                                 }
  503.                                 else if ((err & (CrcErr | ParityErr)) && !(err &ProtocolErr) )
  504.                                 {
  505.                                         //EMV  parity err EMV 307.2.3.4               
  506.                                         val = 0x7F & read_reg(FIFOLevelReg);
  507.                                       last_bits = read_reg(ControlReg) & 0x07;
  508.                                         if (len_rest + val > MAX_TRX_BUF_SIZE)
  509.                                         {//长度过长超出缓存
  510.                                                 status = MI_COM_ERR;
  511.                                                 #if (NFC_DEBUG)
  512.                                                 printf("RX_LEN > 255B\n");
  513.                                                 #endif
  514.                                         }
  515.                                         else
  516.                                         {
  517.                                         if (last_bits && val)
  518.                                         {
  519.                                            pi->mf_length = (val-1)*8 + last_bits;
  520.                                         }
  521.                                         else
  522.                                         {
  523.                                            pi->mf_length = val*8;
  524.                                         }
  525.                                                 pi->mf_length += len_rest*8;
  526.                                         }
  527.                                         #if (NFC_DEBUG)
  528.                                         printf("crc-parity err=%b02x\n", err);
  529.                                         printf("l=%d\n", pi->mf_length );
  530.                                         #endif


  531.                                        
  532.                                         status = MI_INTEGRITY_ERR;
  533.                                 }                               
  534.                                 else
  535.                                 {
  536.                                         #if (NFC_DEBUG)
  537.                                         printf("unknown ErrorReg=%02bx\n", err);
  538.                                         #endif
  539.                                         status = MI_INTEGRITY_ERR;
  540.                                 }
  541.                         }
  542.                         else
  543.                         {   
  544.                                 status = MI_COM_ERR;
  545.                                 #if (NFC_DEBUG)
  546.                                 printf(" MI_COM_ERR\n");
  547.                                 #endif
  548.                         }
  549.                 }
  550.        
  551.                 set_bit_mask(ControlReg, BIT7);// TStopNow =1,必要的;
  552.                 write_reg(ComIrqReg, 0x7F);// 清中断0
  553.                 write_reg(DivIrqReg, 0x7F);// 清中断1
  554.                 clear_bit_mask(ComIEnReg, 0x7F);//清中断使能,最高位是控制位
  555.                 clear_bit_mask(DivIEnReg, 0x7F);//清中断使能,最高位是控制位
  556.                 write_reg(CommandReg, PCD_IDLE);

  557.         }
  558.         else
  559.         {
  560.                 status = USER_ERROR;
  561.                 #if (NFC_DEBUG)
  562.                 printf("USER_ERROR\n");
  563.                 #endif
  564.         }
  565.         #if (NFC_DEBUG)
  566.                 printf(" pcd_com: sta=%bd,err=%02bx\n", status, err);
  567.         #endif       
  568.         return status;
  569. }

  570. void pcd_reset()
  571. {       
  572.         #if(NFC_DEBUG)
  573.                 printf("pcd_reset\n");
  574.         #endif
  575.         write_reg(CommandReg, PCD_RESETPHASE); //软复位数字芯片
  576. }

  577. void pcd_antenna_on()
  578. {
  579.         write_reg(TxControlReg, read_reg(TxControlReg) | 0x03); //Tx1RFEn=1 Tx2RFEn=1
  580. }

  581. void pcd_antenna_off()
  582. {
  583.         write_reg(TxControlReg, read_reg(TxControlReg) & (~0x03));
  584. }
  585. /////////////////////////////////////////////////////////////////////
  586. //设置PCD定时器
  587. //input:fwi=0~15
  588. /////////////////////////////////////////////////////////////////////
  589. void pcd_set_tmo(u8 fwi)
  590. {
  591.         write_reg(TPrescalerReg, (TP_FWT_302us) & 0xFF);
  592.         write_reg(TModeReg, BIT7 | (((TP_FWT_302us)>>8) & 0xFF));

  593.         write_reg(TReloadRegL, (1 << fwi)  & 0xFF);
  594.         write_reg(TReloadRegH, ((1 << fwi)  & 0xFF00) >> 8);
  595. }



  596. void pcd_delay_sfgi(u8 sfgi)
  597. {
  598.         //SFGT = (SFGT+dSFGT) = [(256 x 16/fc) x 2^SFGI] + [384/fc x 2^SFGI]
  599.         //dSFGT =  384 x 2^FWI / fc
  600.                 write_reg(TPrescalerReg, (TP_FWT_302us + TP_dFWT) & 0xFF);
  601.                 write_reg(TModeReg, BIT7 | (((TP_FWT_302us + TP_dFWT)>>8) & 0xFF));

  602.                 if (sfgi > 14 || sfgi < 1)
  603.                 {//FDTA,PCD,MIN = 6078 * 1 / fc
  604.                         sfgi = 1;
  605.                 }

  606.                 write_reg(TReloadRegL, (1 << sfgi) & 0xFF);
  607.                 write_reg(TReloadRegH, ((1 << sfgi) >> 8) & 0xFF);

  608.                 write_reg(ComIrqReg, 0x7F);//清除中断
  609.                 write_reg(ComIEnReg, BIT0);
  610.                 clear_bit_mask(TModeReg,BIT7);// clear TAuto
  611.                 set_bit_mask(ControlReg,BIT6);// set TStartNow
  612.                
  613.                 while(!INT_PIN);// wait new INT
  614.                 //set_bit_mask(TModeReg,BIT7);// recover TAuto
  615.                 pcd_set_tmo(g_pcd_module_info.ui_fwi); //recover timeout set
  616.                
  617. }


  618. void pcd_lpcd_config_start(u8 delta, u32 t_inactivity_ms, u8 skip_times, t_detect_us)
  619. {
  620.         u8 XDATA WUPeriod;
  621.         u8 XDATA SwingsCnt;
  622. #if (NFC_DEBUG)
  623.         printf("pcd_lpcd_config_start\n");
  624. #endif
  625.         WUPeriod = t_inactivity_ms * 32.768 / 256  + 0.5;
  626.         SwingsCnt = t_detect_us * 27.12 / 2 / 16 + 0.5;

  627.         write_reg(0x01,0x0F); //先复位寄存器再进行lpcd

  628.         write_reg(0x14, 0x8B);        // Tx2CW = 1 ,continue载波发射打开
  629.         write_reg(0x37, 0x00);//恢复版本号
  630.         write_reg(0x37, 0x5e);        // 打开私有寄存器保护开关
  631.         write_reg(0x3c, 0x30 | delta);        //设置Delta[3:0]的值, 开启32k
  632.         write_reg(0x3d, WUPeriod);        //设置休眠时间       
  633.         write_reg(0x3e, 0x80 | ((skip_times & 0x07) << 4) | (SwingsCnt & 0x0F));        //开启LPCD_en设置,跳过探测次数,探测时间
  634.         write_reg(0x37, 0x00);        // 关闭私有寄存器保护开关
  635.         write_reg(0x03, 0x20);        //打开卡探测中断使能
  636.         write_reg(0x01, 0x10);        //PCD soft powerdown

  637.         //具体应用相关,本示例工程配置为高电平为有中断
  638.         clear_bit_mask(0x02, BIT7);
  639. }

  640. /*
  641.         lpcd功能开始函数
  642. */
  643. void pcd_lpcd_start()
  644. {
  645. #if (NFC_DEBUG)
  646.         printf("pcd_lpcd_start\n");
  647. #endif

  648.         write_reg(0x01,0x0F); //先复位寄存器再进行lpcd
  649.        
  650.         write_reg(0x37, 0x00);//恢复版本号
  651.         if (read_reg(0x37) == 0x10)
  652.         {
  653.                 write_reg(0x01, 0x00);        // idle
  654.         }
  655.         write_reg(0x14, 0x8B);        // Tx2CW = 1 ,continue载波发射打开
  656.        
  657.         write_reg(0x37, 0x5e);        // 打开私有寄存器保护开关

  658.         //write_reg(0x3c, 0x30);        //设置Delta[3:0]的值, 开启32k //0 不能使用
  659.         //write_reg(0x3c, 0x31);        //设置Delta[3:0]的值, 开启32k
  660.         //write_reg(0x3c, 0x32);        //设置Delta[3:0]的值, 开启32k
  661.         //write_reg(0x3c, 0x33);        //设置Delta[3:0]的值, 开启32k
  662.         //write_reg(0x3c, 0x34);        //设置Delta[3:0]的值, 开启32k
  663.         //write_reg(0x3c, 0x35);        //设置Delta[3:0]的值, 开启32k XU
  664.         write_reg(0x3c, 0x37);        //设置Delta[3:0]的值, 开启32k XU
  665.         //write_reg(0x3c, 0x3A);        //设置Delta[3:0]的值, 开启32k XU
  666.         //write_reg(0x3c, 0x3F);        //设置Delta[3:0]的值, 开启32k XU
  667.        
  668.         write_reg(0x3d, 0x0d);        //设置休眠时间       
  669.         write_reg(0x3e, 0x95);        //设置连续探测次数,开启LPCD_en
  670.         write_reg(0x37, 0x00);        // 关闭私有寄存器保护开关
  671.         write_reg(0x03, 0x20);        //打开卡探测中断使能
  672.         write_reg(0x01, 0x10);        //PCD soft powerdown               

  673.         //具体应用相关,配置为高电平为有中断
  674.         clear_bit_mask(0x02, BIT7);
  675.        
  676. }

  677. void pcd_lpcd_end()
  678. {
  679. #if (NFC_DEBUG)
  680.         printf("pcd_lpcd_end\n");
  681. #endif
  682.         write_reg(0x01,0x0F); //先复位寄存器再进行lpcd
  683. }

  684. u8 pcd_lpcd_check()
  685. {
  686.         if (INT_PIN && (read_reg(DivIrqReg) & BIT5)) //TagDetIrq
  687.         {
  688.                 write_reg(DivIrqReg, BIT5); //清除卡检测到中断
  689.                 pcd_lpcd_end();
  690.                 return TRUE;
  691.         }
  692.         return FALSE;
  693. }

  694. #if 0
  695. void page45_lock()
  696. {
  697.         write_reg(VersionReg, 0);
  698. }

  699. //打开芯片的page4私有寄存器的写保护
  700. void page4_unlock()
  701. {
  702.         write_reg(VersionReg, 0x5E);
  703. }
  704. //打开芯片的page5私有寄存器的写保护
  705. void page5_unlock();
  706. {
  707.         write_reg(VersionReg, 0xAE);
  708. }
  709. #endif

  710. void pcd_set_rate(u8 rate)
  711. {
  712.         u8 val,rxwait;
  713.         switch(rate)
  714.         {
  715.                 case '1':
  716.                         clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
  717.                         clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
  718.                         write_reg(ModWidthReg, 0x26);//Miller Pulse Length

  719.                         write_reg(RxSelReg, 0x88);
  720.                        
  721.                         break;

  722.                 case '2':
  723.                         clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
  724.                         set_bit_mask(TxModeReg, BIT4);
  725.                         clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
  726.                         set_bit_mask(RxModeReg, BIT4);
  727.                         write_reg(ModWidthReg, 0x12);//Miller Pulse Length
  728.                         //rxwait相对于106基本速率需增加相应倍数
  729.                         val = read_reg(RxSelReg);
  730.                         rxwait = ((val & 0x3F)*2);
  731.                         if (rxwait > 0x3F)
  732.                         {
  733.                                 rxwait = 0x3F;
  734.                         }                       
  735.                         write_reg(RxSelReg,(rxwait | (val & 0xC0)));
  736.                        
  737.                         break;

  738.                 case '4':                       
  739.                         clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
  740.                         set_bit_mask(TxModeReg, BIT5);
  741.                         clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
  742.                         set_bit_mask(RxModeReg, BIT5);
  743.                         write_reg(ModWidthReg, 0x0A);//Miller Pulse Length
  744.                         //rxwait相对于106基本速率需增加相应倍数
  745.                         val = read_reg(RxSelReg);
  746.                         rxwait = ((val & 0x3F)*4);
  747.                         if (rxwait > 0x3F)
  748.                         {
  749.                                 rxwait = 0x3F;
  750.                         }                       
  751.                         write_reg(RxSelReg,(rxwait | (val & 0xC0)));       

  752.                         break;
  753.                        
  754.                        
  755.                 default:
  756.                         clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
  757.                         clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
  758.                         write_reg(ModWidthReg, 0x26);//Miller Pulse Length
  759.                        
  760.                         break;
  761.         }

  762.         {//不同速率选择不同带宽
  763.                 u8 adc;
  764.                 write_reg(0x37, 0x00);
  765.                 adc = read_reg(0x37);

  766.                 if (adc == 0x12)
  767.                 {
  768.                         if (rate == '8' || rate == '4')//848k,424k
  769.                         {
  770.                                 write_reg(0x3B, 0x25);
  771.                         }
  772.                         else if (rate == '2' || rate == '1')// 212k, 106k
  773.                         {
  774.                                 write_reg(0x3B, 0xE5);
  775.                         }
  776.                 }
  777.         }
  778. }

  779. #if 0
  780. void calculate_crc(u8 *pin, u8 len, u8 *pout)
  781. {
  782.         u8 XDATA i, n;

  783.         clear_bit_mask(DivIrqReg, 0x04);
  784.         write_reg(CommandReg, PCD_IDLE);
  785.         set_bit_mask(FIFOLevelReg, 0x80);

  786.         for (i = 0; i < len; i++)
  787.         {
  788.                 write_reg(FIFODataReg, *(pin + i));
  789.         }
  790.         write_reg(CommandReg, PCD_CALCCRC);
  791.         i = 0xFF;
  792.         do
  793.         {
  794.                 n = read_reg(DivIrqReg);
  795.                 i--;
  796.         }while((i!=0) && !(n&0x04));

  797.         #if (NFC_DEBUG)
  798. ……………………

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

所有资料51hei提供下载:
N12_SDK_STC_V1.9.0-MH523.zip (202.2 KB, 下载次数: 30)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:174087 发表于 2019-4-4 09:36 | 显示全部楼层
老哥能不能请教下,523芯片在已经能读A卡的情况下,如何设置才能读B卡,程序是不是只需要修改“配置读卡类型”部分的函数就可以呢?我从有些资料上看读B卡对天线的要求还有挺高的,不知道你这个程序调试的时候有没有针对天线进行设置呢
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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