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

📄 main.lst

📁 这是一个rfid读卡器程序
💻 LST
📖 第 1 页 / 共 4 页
字号:
C51 COMPILER V8.02   MAIN                                                                  12/27/2006 08:14:28 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE main.C COMPACT REGFILE(.\HY502B_SPI.ORC) BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /****************************************************************************
   2          * File :  main.c                                                            *
   3          * COPYRIGHT BY HUOYAN LTD.COMPANY                                           *
   4          * Version:      V2.0                                                                    *
   5          *                                                                           *
   6          * Created:      07.Aug.2006                                                 *
   7          * Last Change:  25.DEC.2006                                                 *
   8          *                                                                           *
   9          * Author:       sun ,nil                                                    *
  10          *                                                                           *
  11          * Compiler:     KEIL C51 V7.10                                              *
  12          *                                                                           *
  13          * Description:  AT89S52 Firmware for HY502  Serial Reader                   *
  14          *   This project is designed for HY502XX                                    *
  15          *   OSC use 11.0592MHz crystal.                                                                                     *
  16          ****************************************************************************/
  17          
  18          /*******************  www.ehuoyan.com *************************************/
  19          /*-------------------------------------------------------------------------*/
  20          
  21          #include "main.h"        
  22          #include "hy502.h"
  23          
  24          #define uchar   unsigned char
  25          #define uint    unsigned int                
  26          
  27          #define TIMER2           TRUE
  28          
  29          bit ack;        //应答标志
  30          uchar code cStatus1[]="Ok!";
  31          
  32          
  33          uchar keytype=0x60;  //  密钥标识:60为PICC_AUTHENT1A(61为PICC_AUTHENT1B)
  34          uchar idata       g_cBeeps=0x10;                                
  35          
  36          bit           g_bReceCommandOk; //flag of command receive OK
  37          bit           g_bReceAA;                //flag of last received byte is "0xaa", used in interrupt
  38          bit           g_bCard;                  //flag of card in                
  39          
  40          
  41          
  42          /////////////////////////////////////////////////////////////////////////////////////////////////////
  43          // 延时函数
  44          void delay(unsigned int x)
  45          {
  46   1              while(x)
  47   1              {
  48   2                      x--;
  49   2              }
  50   1      }
  51          
  52          ///////////////////////////////////////////////////////////////////////
  53          // Delay 50us
  54          ///////////////////////////////////////////////////////////////////////   
  55          void delay_50us(unsigned char _50us)
C51 COMPILER V8.02   MAIN                                                                  12/27/2006 08:14:28 PAGE 2   

  56          {
  57   1              uchar i;
  58   1      
  59   1          while(_50us--)
  60   1              {
  61   2                      for(i=0;i<81;i--)
  62   2                      {  _nop_();  }
  63   2      
  64   2          }
  65   1      }
  66          
  67          ///////////////////////////////////////////////////////////////////////
  68          // Delay 1ms
  69          ///////////////////////////////////////////////////////////////////////
  70          void delay_1ms(unsigned char _1ms)
  71          {
  72   1      #ifndef TIMER2
                  RCAP2LH = RCAP2_1ms;
                  T2LH    = RCAP2_1ms;
              
                  TR2 = TRUE;
                  while (_1ms--)
                  {
                          while (!TF2);
                          TF2 = FALSE;
                  }
                  TR2 = FALSE;
              #else
  84   1          while (_1ms--)
  85   1          {
  86   2                  delay_50us(20);
  87   2          }
  88   1      #endif
  89   1      }
  90          
  91          ///////////////////////////////////////////////////////////////////////
  92          // Delay 10ms
  93          ///////////////////////////////////////////////////////////////////////
  94          void delay_10ms(unsigned int _10ms)
  95          {
  96   1      
  97   1      #ifndef TIMER2
                  RCAP2LH = RCAP2_10ms;
                  T2LH    = RCAP2_10ms;
                  
                  TR2 = TRUE;
                  while (_10ms--)
                  {
                          while (!TF2);
                          TF2 = FALSE;
                  }
                  TR2 = FALSE;
              #else
 109   1      
 110   1          while (_10ms--)
 111   1          {
 112   2                      delay_50us(200);        
 113   2          }
 114   1      #endif
 115   1      }
 116          
 117          /******************* SPI FUNCTION DEFINE ************************************/
C51 COMPILER V8.02   MAIN                                                                  12/27/2006 08:14:28 PAGE 3   

 118          /*****************************************************************************
 119          *function: send a byte over SPI bus
 120           发送一个字节
 121          *****************************************************************************/
 122          unsigned char SPIRWByte(unsigned char cSendByte)
 123          {
 124   1              unsigned char data i = 8;
 125   1              unsigned char cRecByte;
 126   1              while (i--)
 127   1              {
 128   2                      cRecByte += cRecByte;
 129   2                      SCL = 0;
 130   2                      MOSI = (bit)(cSendByte & 0x80);
 131   2                      cSendByte += cSendByte;
 132   2                      cRecByte |= (unsigned char) MISO ;
 133   2                      SCL = 1;
 134   2              }
 135   1              SCL = 1;
 136   1              return cRecByte;
 137   1      }
 138          
 139          // SPI 总线状态定义
 140          #define SPI_RDY  0xF0;            // 准备
 141          #define spibusy  0xaa             // 忙
 142          #define spiread  0xbb             // 读
 143          #define spiwrite 0xcc             // 写
 144          
 145          /***** 查询SPI总线状态 *********/
 146          unsigned char spi_cStatus(void)
 147          {
 148   1              unsigned char cStatus;
 149   1              NSS=0;
 150   1              cStatus=SPIRWByte(spibusy);
 151   1      
 152   1              cStatus=SPIRWByte(0xFF);
 153   1              NSS=1;
 154   1      
 155   1              return cStatus;
 156   1      }
 157          
 158          /***** 读SPI总线数据 *********/
 159          unsigned char SPI_Read(unsigned char *cP)
 160          {
 161   1              unsigned char cCnt,cStatus;
 162   1              unsigned char cCheckSum = 0;
 163   1      
 164   1              for (cCnt=0; cCnt<100; cCnt++)
 165   1              {
 166   2                      cStatus=spi_cStatus(); //查询SPI总线状态
 167   2                      if(cStatus==0xF0)
 168   2                      {
 169   3                              cCnt=253;
 170   3                      }
 171   2                      delay(10);        
 172   2              }
 173   1      
 174   1              if(cCnt==254)  //卡操作结束,可以回传数据
 175   1              {
 176   2                      NSS=0;
 177   2                      cCnt=SPIRWByte(spiread);
 178   2                      cP[0]=2;
 179   2                 for (cCnt=0; cCnt<cP[0]; cCnt++)
C51 COMPILER V8.02   MAIN                                                                  12/27/2006 08:14:28 PAGE 4   

 180   2                 {
 181   3                      cP[cCnt] = SPIRWByte(0xFF);
 182   3                      cCheckSum ^= cP[cCnt];
 183   3                      if(cP[0]>32)
 184   3                      {
 185   4                         NSS=1;
 186   4                         return  FAILURE;
 187   4                      }
 188   3                 }
 189   2                 cP[cCnt] = SPIRWByte(0xFF);
 190   2      
 191   2                 NSS=1;
 192   2                 if (cCheckSum == cP[cCnt])
 193   2                 {
 194   3                      LED_YELLOW =0;
 195   3                      return SUCCESS;
 196   3      
 197   3                 }
 198   2              }
 199   1              return FAILURE;
 200   1      }
 201          
 202          /***** 写SPI总线数据 *********/
 203          unsigned char SPI_Write(unsigned char *cP)
 204          {

⌨️ 快捷键说明

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