⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iccpdir.lst

📁 mifarea卡程序mifarea卡程序mifarea卡程序
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V8.00   ICCPDIR                                                               04/23/2009 15:56:19 PAGE 1   


C51 COMPILER V8.00, COMPILATION OF MODULE ICCPDIR
OBJECT MODULE PLACED IN source\iccpdir.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE source\iccpdir.c LARGE BROWSE ORDER NOAREGS DEBUG OBJECTEXTEND PRINT(.\iccp
                    -dir.lst) SRC(.\out_sst89e564\iccpdir.SRC)

line level    source

   1          /****************************
   2           *    iccpdir.c             *
   3           * Designe by 刘俐训        *
   4           * T = 0 for ic card        *
   5           * Sync card                *
   6           * last update: 04-13-2007  *
   7           ****************************/
   8          #include <reg52.h>
   9          #include "stimer.h"
  10          #include "string.h"
  11          #include "uart.h"
  12          #include "icc.h"
  13          #include <v51rx2.h>
  14          #include "intrsc.h"
  15          //#include <wdt.h>
  16          
  17          #define DEBUG_ICC       0
  18          
  19          #define ICCIO           P3_3
  20          #define ICCRST          P3_4
  21          #define USERRST         P2_0
  22          
  23          #define ICCUSERCLKC     P1_1
  24          #define ICCUSERCLS      P2_5
  25          #define ICCUSEROVLD     P2_6
  26          #define ICCUSEREJECT    P1_4
  27          #define ICCUSERSCLK     P1_2    // sync clk of user card
  28          #define ICCUSERVCCC     P2_1
  29          
  30          #define ICCA0           P1_6
  31          #define ICCA1           P1_5
  32          #define ICCINH          P1_7
  33          
  34          
  35          #define INFOLEN     176
  36          
  37          #ifndef enable
              #define disable()   EA = 0
              #define enable()    EA = 1
              #endif
  41          
  42          #define IccEnable();    IE1 = 0; EX1 = 1;
  43          #define IccDisable();   EX1 = 0;
  44          
  45          #define StartSend();    TxNotRx = 1;\
  46                                  ICCIO = 0;
  47                                  
  48          typedef struct _ICCATTR
  49          {
  50              unsigned char etu : 2;      // baudrate, 00:9600, 01:38400, 10: onchip9600, 11: Onchip38400
  51              unsigned char autob : 1;    // auto baudrate or not
  52              unsigned char dir : 1;      // pdir or ndir
  53          }ICCATTR;
  54              
C51 COMPILER V8.00   ICCPDIR                                                               04/23/2009 15:56:19 PAGE 2   

  55          static code unsigned char IccEtu[4] = {248, 62, 192, 48};
  56          static ICCATTR attr[4] = {{0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}}; 
  57          static unsigned int data TIME_OUT_MS = 50;                  /* 默认超时时间是50毫秒 */
  58          
  59          // these parameters are calculated from ETU
  60          static unsigned int data ETU = 62;  //248;              // 372 * 4 / 12 = 124 (cycles)
  61          static unsigned char data RLD = 256 - 62;               // bit period
  62          static unsigned char data TxRLDStart = 256 - 62 + 30;   // the 1st bit period
  63          static unsigned char data RxRLDStart = 256 - 62 - 31 + 60;  // the 1st bit period
  64          static unsigned char data RxBits = 8;
  65          
  66          // data direction
  67          static bit dir = 1; // pdir
  68          
  69          // flag of reseting card
  70          static bit fReset = 0;
  71          
  72          // current card id
  73          static unsigned char CardId; 
  74          
  75          /*********************************************************************/
  76          void iccgetputonebit(void);    // call in the user timer interrupt for every bit
  77          
  78          /*********************************************************************/
  79          #ifndef SSBUF_SIZE
  80          #define SSBUF_SIZE  64
  81          #endif
  82          
  83          static unsigned char idata IBUF[SSBUF_SIZE];
  84          static unsigned char data ibufputidx=0;
  85          static unsigned char data ibufgetidx=0;
  86          static bit TxNotRx = 0;
  87          
  88          static bit GetInputData( unsigned char *addr);
  89          
  90          // calculate ETU, RLD and so on.
  91          static void IccInitETU(unsigned int etu);
  92          /*********************************************************************/
  93          
  94          // these parameters are prepared for the bit interrupt functions
  95          static unsigned char data mycharp;
  96          static unsigned char data parityerror;
  97          static unsigned char data parityerrorcnt;
  98          static unsigned char data bitcounter;
  99          static unsigned char data endflag;
 100          static volatile bit RxWorking = 0;
 101          static volatile bit TxParity = 0;
 102          
 103          void iccgetputonebit(void)
 104          {
 105   1          bit bioport;
 106   1      
 107   1          if(TxNotRx)
 108   1          {
 109   2              if(endflag)
 110   2              {
 111   3                  if(endflag == 1)
 112   3                  {
 113   4                      ICCIO = 1;      // stop parity bit
 114   4                      endflag ++;
 115   4                  }
 116   3                  else
C51 COMPILER V8.00   ICCPDIR                                                               04/23/2009 15:56:19 PAGE 3   

 117   3                  {
 118   4                      parityerror = ~ICCIO;
 119   4                      TxNotRx = 0;
 120   4                      DiUserTimer();
 121   4                      IccEnable();
 122   4                  }
 123   3              }
 124   2              else if(!bitcounter)
 125   2              {
 126   3                  ICCIO = TxParity;
 127   3                  endflag = 1;
 128   3              }
 129   2              else
 130   2              {
 131   3                  if(dir)
 132   3                      mycharp >>= 1;
 133   3                  else
 134   3                      mycharp <<= 1;
 135   3                  
 136   3                  ICCIO = CY;
 137   3                  bitcounter --;
 138   3              }
 139   2              return;
 140   2          }
 141   1          else
 142   1          {
 143   2              bioport = dir ? ICCIO : ~ICCIO;             // 首先取得端口数据,保证时间准确
 144   2              
 145   2              if(parityerror)
 146   2              {
 147   3                  if(parityerror == 1)        // parity error bit start
 148   3                  {
 149   4                      ICCIO = 0;   // show error
 150   4                      parityerror ++;
 151   4                  }
 152   3                  else                        // parity error bit end
 153   3                  {
 154   4                      ICCIO = 1;
 155   4                      
 156   4                      parityerror = 0;
 157   4                      DiUserTimer();          // disable user timer interrupt
 158   4                      IccEnable();
 159   4                      RxWorking = 0;
 160   4                  }
 161   3                  return;
 162   3              }
 163   2              
 164   2              // if(!endflag)
 165   2              if(bitcounter)
 166   2              {
 167   3                  if(dir)
 168   3                  {
 169   4                      /*
 170   4                      mycharp >>= 1;
 171   4                      if(bioport)
 172   4                          mycharp |= 0x80;
 173   4                      */
 174   4                      ACC = mycharp;
 175   4                      CY = bioport;
 176   4                      #pragma asm
 177   4                      RRC     A;
 178   4                      #pragma endasm
C51 COMPILER V8.00   ICCPDIR                                                               04/23/2009 15:56:19 PAGE 4   

 179   4                      mycharp = ACC;
 180   4                  }
 181   3                  else
 182   3                  {
 183   4                      /*
 184   4                      mycharp <<= 1;
 185   4                      if(bioport)
 186   4                          mycharp |= 0x01;
 187   4                      */
 188   4                      ACC = mycharp;
 189   4                      CY = bioport;
 190   4                      #pragma asm
 191   4                      RLC     A;
 192   4                      #pragma endasm
 193   4                      mycharp = ACC;
 194   4                  }
 195   3                      
 196   3                  bitcounter --;  /////////////////////////
 197   3                  
 198   3                              return;
 199   3              }
 200   2      
 201   2              ACC = mycharp;
 202   2              if(P == bioport)                            /* 校验正确 */
 203   2              {
 204   3                  FillBuf:
 205   3                  if(ibufputidx != ibufgetidx-1)                              // ;             
 206   3                  {                                                           //  |            
 207   4                      if( (ibufputidx != SSBUF_SIZE-1) || (ibufgetidx != 0) ) //  |            
 208   4                      {                                                       //  |            
 209   5                          IBUF[ibufputidx++] = mycharp;                       //   > fill buf  
 210   5                          if(ibufputidx == SSBUF_SIZE)                        //  |
 211   5                              ibufputidx = 0;                                 //  |
 212   5                      }                                                       //  |            
 213   4                  }                                                           // /
 214   3                  
 215   3                  DiUserTimer();                          // disable user timer interrupt
 216   3                  IccEnable();
 217   3                  RxWorking = 0;
 218   3                  // clear reseting flag
 219   3                  fReset = 0;
 220   3                  return;
 221   3              }
 222   2              
 223   2              // 校验不正确,首先看是否是复位应答的开始字节TS
 224   2              if(fReset && (mycharp == 0x03))
 225   2              {
 226   3                  dir = 0;        // 反向协议
 227   3                  mycharp = 0x3f;
 228   3                  goto FillBuf;
 229   3              }
 230   2              
 231   2              // 设校验错误标志并计数
 232   2              // P2_0 = 1;//////////////////////////////////////////////////////////////////////////////////////
             -///////////////////
 233   2              parityerror = 1;
 234   2              parityerrorcnt ++;
 235   2              // clear reseting flag
 236   2              fReset = 0;
 237   2              return;
 238   2          }
 239   1      }
C51 COMPILER V8.00   ICCPDIR                                                               04/23/2009 15:56:19 PAGE 5   

 240          
 241          void TxRxdata(void) interrupt IE1_VECTOR using 3    //2
 242          {
 243   1          if(TxNotRx)
 244   1          {
 245   2              // do nothing
 246   2          }
 247   1          else
 248   1          {
 249   2              InitUserTimer(RLD, RxRLDStart);
 250   2      
 251   2              // mycharp = 0x80;
 252   2              bitcounter = RxBits;
 253   2              RxWorking = 1;
 254   2              
 255   2              IccDisable();   // EX1 = 0;     // 禁止中断
 256   2              parityerror = 0;                // clear parity error flag
 257   2              endflag = 0;
 258   2          }
 259   1      }
 260          
 261          static bit GetInputData( unsigned char *addr )
 262          {
 263   1          if( ibufgetidx == ibufputidx )
 264   1          {
 265   2              return 0;                  //buf empty
 266   2          }
 267   1          else
 268   1          {
 269   2              *addr = IBUF[ibufgetidx++];
 270   2          
 271   2              if(ibufgetidx == SSBUF_SIZE)
 272   2                  ibufgetidx = 0;
 273   2              return 1;
 274   2          }
 275   1      }
 276          
 277          static void IccInitETU(unsigned int etu)
 278          {
 279   1          ETU = etu;
 280   1          RLD = 256 - ETU;
 281   1          TxRLDStart = RLD + 30;
 282   1          if(etu >= 170)
 283   1          {
 284   2              RxRLDStart = RLD + (ETU >> 1) + 60;
 285   2              RxBits = 9;
 286   2          }
 287   1          else
 288   1          {
 289   2              RxRLDStart = RLD - (ETU >> 1) + 60;
 290   2              RxBits = 8;
 291   2          }
 292   1      }
 293          
 294          void IccSelect(unsigned char cardid)
 295          {
 296   1          // inhabit
 297   1          ICCINH = 1;
 298   1          
 299   1          // backup card id
 300   1          CardId = cardid & 0x03;
 301   1          
C51 COMPILER V8.00   ICCPDIR                                                               04/23/2009 15:56:19 PAGE 6   

 302   1          // switch to specified card
 303   1          if(cardid & 0x01)
 304   1              ICCA0 = 1;
 305   1          else
 306   1              ICCA0 = 0;
 307   1          if(cardid & 0x02)
 308   1              ICCA1 = 1;
 309   1          else
 310   1              ICCA1 = 0;
 311   1          
 312   1          // habit
 313   1          ICCINH = 0;
 314   1          
 315   1          // init card etu and direciton
 316   1          IccInitETU(IccEtu[attr[cardid].etu]);
 317   1          dir = attr[cardid].dir;
 318   1      }
 319          
 320          int IccRead( char *buf, int len )
 321          {
 322   1          int data i;
 323   1          unsigned int data starttime;
 324   1      
 325   1          for(i=0; i<len; i++)
 326   1          {
 327   2              starttime = GetTickCount();
 328   2              while(!GetInputData(buf+i))
 329   2              {
 330   3                  if(GetTickCount() - starttime >= TIME_OUT_MS)
 331   3                      return i;
 332   3              }
 333   2          }
 334   1          return i;
 335   1      }
 336          
 337          int IccWrite(char * buf, int len)
 338          {
 339   1          int data i;
 340   1          unsigned int data starttime;
 341   1          unsigned char data trytimes = 3;
 342   1          
 343   1          for(i=0; i<len; i++)
 344   1          {
 345   2              starttime = GetTickCount();
 346   2              SoftDelayCyl(ETU);      // delay 2 etu
 347   2              while(RxWorking)
 348   2              {
 349   3                  while(RxWorking)    // 重复两次是为了防止接收校验出错的重传
 350   3                  {
 351   4                      if(GetTickCount() - starttime >= TIME_OUT_MS)
 352   4                      {
 353   5                          #if DEBUG_ICC
                                  ComWrite("\xff", 1);
                                  delay(3);
                                  #endif
 357   5                          return i;
 358   5                      }
 359   4                  }
 360   3                  SoftDelayCyl(ETU);  // delay 4 etu

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -