PcdComTransceive.c
上传用户:hbtfgg
上传日期:2021-12-10
资源大小:16k
文件大小:3k
源码类别:

RFID编程

开发平台:

C/C++

  1. #include<reg52.h>
  2. #include<rc500.h>
  3. idata struct TranSciveBuffer{uchar MfCommand;
  4.                              uchar MfLength;
  5.                              uchar MfData[19];
  6.                             };
  7. char PcdComTransceive(struct TranSciveBuffer *pi)
  8. {
  9.    bit recebyte=0;
  10.    char status;
  11.    uchar irqEn=0x00;
  12.    uchar waitFor=0x00;
  13.    uchar lastBits;
  14.    uchar n;
  15.    uint i;
  16.    switch(pi->MfCommand)
  17.    {
  18.       case PCD_IDLE:
  19.          irqEn = 0x00;
  20.          waitFor = 0x00;
  21.          break;
  22.       case PCD_WRITEE2:
  23.          irqEn = 0x11;
  24.          waitFor = 0x10;
  25.          break;
  26.       case PCD_READE2:
  27.          irqEn = 0x07;
  28.          waitFor = 0x04;
  29.          recebyte=1;
  30.          break;
  31.       case PCD_LOADCONFIG:
  32.       case PCD_LOADKEYE2:
  33.       case PCD_AUTHENT1:
  34.          irqEn = 0x05;
  35.          waitFor = 0x04;
  36.          break;
  37.       case PCD_CALCCRC:
  38.          irqEn = 0x11;
  39.          waitFor = 0x10;
  40.          break;
  41.       case PCD_AUTHENT2:
  42.          irqEn = 0x04;
  43.          waitFor = 0x04;
  44.          break;
  45.       case PCD_RECEIVE:
  46.          irqEn = 0x06;
  47.          waitFor = 0x04;
  48.          recebyte=1;
  49.          break;
  50.       case PCD_LOADKEY:
  51.          irqEn = 0x05;
  52.          waitFor = 0x04;
  53.          break;
  54.       case PCD_TRANSMIT:
  55.          irqEn = 0x05;
  56.          waitFor = 0x04;
  57.          break;
  58.       case PCD_TRANSCEIVE:
  59.          irqEn = 0x3D;
  60.          waitFor = 0x04;
  61.          recebyte=1;
  62.          break;
  63.       default:
  64.          pi->MfCommand=MI_UNKNOWN_COMMAND;
  65.          break;
  66.    }
  67.    if(pi->MfCommand!=MI_UNKNOWN_COMMAND)
  68.    {
  69.       WriteRC(RegPage,0x00);
  70.       WriteRC(RegInterruptEn,0x7F);
  71.       WriteRC(RegInterruptRq,0x7F);
  72.       WriteRC(RegCommand,PCD_IDLE);
  73.       SetBitMask(RegControl,0x01);
  74.       WriteRC(RegInterruptEn,irqEn|0x80);
  75.       for(i=0;i<pi->MfLength;i++)
  76.       {
  77.          WriteRC(RegFIFOData,pi->MfData[i]);
  78.       }
  79.       WriteRC(RegCommand,pi->MfCommand);
  80.       i=0x2000;
  81.       do
  82.       {
  83.          n=ReadRC(RegInterruptRq);
  84.          i--;
  85.       }
  86.       while((i!=0)&&!(n&irqEn&0x20)&&!(n&waitFor));
  87.       status=MI_COM_ERR;
  88.       if((i!=0)&&!(n&irqEn&0x20))
  89.       {
  90.          if(!(ReadRC(RegErrorFlag)&0x17))
  91.          {
  92.             status=MI_OK;
  93.             if(recebyte)
  94.             {
  95.                n=ReadRC(RegFIFOLength);
  96.                lastBits=ReadRC(RegSecondaryStatus)&0x07;
  97.                 if(lastBits)
  98.                 {
  99.                    pi->MfLength=(n-1)*8+lastBits;
  100.                 }
  101.                 else
  102.                 {
  103.                    pi->MfLength=n*8;
  104.                 }
  105.                 if(n==0)
  106.                 {
  107.                    n=1;
  108.                 }
  109.                 for(i=0;i<n;i++)
  110.                 {
  111.                       pi->MfData[i]=ReadRC(RegFIFOData);
  112.                 }
  113.             }
  114.          }
  115.       }
  116.       else if(n&irqEn&0x20)
  117.       {
  118.           status=MI_NOTAGERR;
  119.       }
  120.       else
  121.       {
  122.           status=MI_COM_ERR;
  123.       }
  124.       WriteRC(RegInterruptEn,0x7F);
  125.       WriteRC(RegInterruptRq,0x7F);
  126.    }
  127.    return status;
  128. }