main.LST
上传用户:njxy551199
上传日期:2022-04-06
资源大小:1990k
文件大小:15k
源码类别:

RFID编程

开发平台:

C/C++

  1. C51 COMPILER V8.08   MAIN                                                                  03/14/2010 21:12:48 PAGE 1   
  2. C51 COMPILER V8.08, COMPILATION OF MODULE MAIN
  3. OBJECT MODULE PLACED IN main.OBJ
  4. COMPILER INVOKED BY: C:KeilC51BINC51.EXE main.c BROWSE DEBUG OBJECTEXTEND
  5. line level    source
  6.    1          #include <stdio.h>
  7.    2          #include <intrins.h>
  8.    3          #include "main.h"        
  9.    4          #include "hy502.h"
  10.    5          #include "lcdd.h"
  11.    6          
  12.    7          
  13.    8          // SPI 总线状态定义
  14.    9          #define SPI_RDY  0xF0;            // 准备
  15.   10          #define spibusy  0xaa             // 忙
  16.   11          #define spiread  0xbb             // 读
  17.   12          #define spiwrite 0xcc             // 写
  18.   13          
  19.   14          
  20.   15          
  21.   16          /////////////////////////////////////////////////////////////////////////////////////////////////////
  22.   17          // 延时函数
  23.   18          void delay(unsigned int x)
  24.   19          {
  25.   20   1              while(x)
  26.   21   1              {
  27.   22   2                      x--;
  28.   23   2              }
  29.   24   1      }
  30.   25          
  31.   26           
  32.   27          
  33.   28          ///////////////////////////////////////////////////////////////////////
  34.   29          // Delay 1ms
  35.   30          ///////////////////////////////////////////////////////////////////////
  36.   31          void delay_1ms (uint _1ms)
  37.   32          {
  38.   33   1      
  39.   34   1        RCAP2LH = RCAP2_1ms;
  40.   35   1        T2LH    = RCAP2_1ms;
  41.   36   1        ET2 = 0;      // Disable timer2 interrupt
  42.   37   1        T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
  43.   38   1      
  44.   39   1        while (_1ms--)
  45.   40   1        {
  46.   41   2              while (!TF2);
  47.   42   2              TF2 = FALSE;
  48.   43   2        }
  49.   44   1        TR2 = FALSE;
  50.   45   1      
  51.   46   1      }
  52.   47          
  53.   48          ///////////////////////////////////////////////////////////////////////
  54.   49          // Delay 10ms
  55.   50          ///////////////////////////////////////////////////////////////////////
  56.   51          void delay_10ms(unsigned int _10ms)
  57.   52          {
  58.   53   1        RCAP2LH = RCAP2_10ms;
  59.   54   1        T2LH    = RCAP2_10ms;
  60.   55   1        ET2 = 0;      // Disable timer2 interrupt
  61. C51 COMPILER V8.08   MAIN                                                                  03/14/2010 21:12:48 PAGE 2   
  62.   56   1        T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
  63.   57   1      
  64.   58   1        while (_10ms--)
  65.   59   1        {
  66.   60   2              while (!TF2)
  67.   61   2              {
  68.   62   3               if(g_bReceCommandOk)            // change by lin 2006-08-21
  69.   63   3                {
  70.   64   4                      TR2 = FALSE;
  71.   65   4                      TF2 = FALSE;
  72.   66   4                      return;
  73.   67   4                 }
  74.   68   3              }
  75.   69   2              TF2 = FALSE;
  76.   70   2        }
  77.   71   1        TR2 = FALSE;
  78.   72   1      
  79.   73   1      }
  80.   74          
  81.   75           
  82.   76          /******************* SPI FUNCTION DEFINE ************************************/
  83.   77          /*****************************************************************************
  84.   78          *function: send a byte over SPI bus
  85.   79           发送一个字节
  86.   80          *****************************************************************************/
  87.   81          unsigned char SPIRWByte(unsigned char cSendByte)
  88.   82          {
  89.   83   1              unsigned char data i = 8;
  90.   84   1              unsigned char cRecByte;
  91.   85   1              while (i--)
  92.   86   1              {
  93.   87   2                      cRecByte += cRecByte;
  94.   88   2                      SCL = 0;
  95.   89   2                      MOSI = (bit)(cSendByte & 0x80);
  96.   90   2                      cSendByte += cSendByte;
  97.   91   2                      cRecByte |= (unsigned char) MISO ;
  98.   92   2                      SCL = 1;
  99.   93   2              }
  100.   94   1              SCL = 1;
  101.   95   1              return cRecByte;
  102.   96   1      }
  103.   97          
  104.   98          
  105.   99          /***** 查询SPI总线状态 *********/
  106.  100          unsigned char spi_cStatus(void)
  107.  101          {
  108.  102   1              unsigned char cStatus;
  109.  103   1              NSS=0;
  110.  104   1              cStatus=SPIRWByte(spibusy);
  111.  105   1      
  112.  106   1              cStatus=SPIRWByte(0xFF);
  113.  107   1              NSS=1;
  114.  108   1      
  115.  109   1              return cStatus;
  116.  110   1      }
  117.  111          
  118.  112          /***** 读SPI总线数据********/
  119.  113          unsigned char SPI_Read(unsigned char *cP)
  120.  114          {
  121.  115   1              unsigned char cCnt,cStatus;
  122.  116   1              unsigned char cCheckSum = 0;
  123.  117   1      
  124. C51 COMPILER V8.08   MAIN                                                                  03/14/2010 21:12:48 PAGE 3   
  125.  118   1              for (cCnt=0; cCnt<100; cCnt++)
  126.  119   1              {
  127.  120   2                      cStatus=spi_cStatus(); //查询SPI总线状态
  128.  121   2                      if(cStatus==0xF0)
  129.  122   2                      {
  130.  123   3                              cCnt=253;
  131.  124   3                      }
  132.  125   2                      delay(10);        
  133.  126   2              }
  134.  127   1      
  135.  128   1              if(cCnt==254)  //卡操作结束,可以回传数据
  136.  129   1              {  
  137.  130   2                      NSS=0;
  138.  131   2              cCnt=SPIRWByte(spiread);
  139.  132   2              
  140.  133   2      
  141.  134   2                  cP[0]=0x01;                            
  142.  135   2                 for (cCnt=0; cCnt<cP[0]; cCnt++)
  143.  136   2                 {
  144.  137   3                      cP[cCnt] = SPIRWByte(0xFF);
  145.  138   3                      cCheckSum ^= cP[cCnt];
  146.  139   3                    if(cP[0]>32)
  147.  140   3                      {
  148.  141   4                         NSS=1;
  149.  142   4                         return  FAILURE;
  150.  143   4                      }       
  151.  144   3                 }
  152.  145   2                                       delay(10);
  153.  146   2                 cP[cCnt] = SPIRWByte(0xFF);
  154.  147   2                                       delay(10);
  155.  148   2                 NSS=1;
  156.  149   2                 if (cCheckSum == cP[cCnt])  
  157.  150   2                         {
  158.  151   3                              
  159.  152   3                         return SUCCESS;
  160.  153   3                      }
  161.  154   2                 else  return FAILURE;
  162.  155   2      
  163.  156   2                 
  164.  157   2             }
  165.  158   1              else return FAILURE;
  166.  159   1      }
  167.  160          
  168.  161          /***** 写SPI总线数据 *********/
  169.  162          unsigned char SPI_Write(unsigned char *cP)
  170.  163          {
  171.  164   1              unsigned char i,cStatus;
  172.  165   1              unsigned char cCheckSum = 0;
  173.  166   1      
  174.  167   1                      NSS=0;
  175.  168   1                      cStatus=SPIRWByte(spiwrite);
  176.  169   1      
  177.  170   1                      for(i=0; i<cP[0]; i++)  // cP[0] 表示数据包的长度
  178.  171   1                      {
  179.  172   2                              cCheckSum ^= cP[i];
  180.  173   2                              cStatus=SPIRWByte(cP[i]);
  181.  174   2                      }
  182.  175   1                      cStatus=SPIRWByte(cCheckSum);
  183.  176   1                      NSS=1;
  184.  177   1              return cStatus;
  185.  178   1      }
  186.  179          
  187. C51 COMPILER V8.08   MAIN                                                                  03/14/2010 21:12:48 PAGE 4   
  188.  180          
  189.  181          //-----Reset rf module -------//
  190.  182          void Reset_HY502(void)
  191.  183          {
  192.  184   1              RST_HY=0;                         //Reset HY502 复位模块 
  193.  185   1          delay_10ms(50);          
  194.  186   1              RST_HY=1;     
  195.  187   1      }
  196.  188             
  197.  189          /************************** main function start here *******************************/
  198.  190          void main()
  199.  191          {                       
  200.  192   1              
  201.  193   1       
  202.  194   1              
  203.  195   1              uchar idata str1[32];
  204.  196   1              uchar idata cStatus;                   
  205.  197   1              uchar *cPa;
  206.  198   1               int idata flag =0;
  207.  199   1      
  208.  200   1              float idata money=210.5;                                //      赋初值
  209.  201   1      
  210.  202   1              InitializeSystem();                // 初始化系统        
  211.  203   1              Reset_HY502();       
  212.  204   1              delay_1ms(500); 
  213.  205   1              LCD_Init();
  214.  206   1             Display_full_screen_color (BLACK);
  215.  207   1              disintlcd();    
  216.  208   1           
  217.  209   1                      
  218.  210   1                         while(sig)  ;
  219.  211   1                        
  220.  212   1               memcpy(&ComWriteBlock4[10],&money,4);
  221.  213   1      
  222.  214   1                    cPa = ComWriteBlock4;
  223.  215   1                      SPI_Write(cPa);
  224.  216   1                      delay_1ms(100);                                           // delay for module execution
  225.  217   1                      cStatus = SPI_Read(g_cReceBuf);
  226.  218   1      
  227.  219   1                      while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
  228.  220   1                       {
  229.  221   2                       cPa =ComWriteBlock4;
  230.  222   2                       SPI_Write(cPa);
  231.  223   2                       delay_1ms(100);
  232.  224   2                       cStatus = SPI_Read(g_cReceBuf);
  233.  225   2                       delay_1ms(100);        
  234.  226   2                               
  235.  227   2                       
  236.  228   2                       }
  237.  229   1                                                                
  238.  230   1              
  239.  231   1      
  240.  232   1              while (1)
  241.  233   1              {
  242.  234   2                        if (flag == 1) break;            
  243.  235   2                        while(sig);              
  244.  236   2                        delay_1ms(70);           //预留卡靠近的时间              
  245.  237   2                        while (!sig)
  246.  238   2                        {
  247.  239   3      
  248.  240   3               
  249.  241   3               
  250. C51 COMPILER V8.08   MAIN                                                                  03/14/2010 21:12:48 PAGE 5   
  251.  242   3               
  252.  243   3               cPa =ComReadBlock4;                            // 读取钱包         
  253.  244   3              SPI_Write(ComReadBlock4);
  254.  245   3              delay_1ms(100);                                                 
  255.  246   3             cStatus = SPI_Read(g_cReceBuf);
  256.  247   3                              if ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
  257.  248   3                       {
  258.  249   4                              cStatus  =  SPI_Write(ComHaltCard);
  259.  250   4                              DrawString (45,150,"读卡失败",BLUE,GREEN,NORMAL)        ; 
  260.  251   4                              delay_1ms(500);
  261.  252   4                              DrawString (45,150,"                ",GREEN,GREEN,NORMAL)       ;       
  262.  253   4                                continue;
  263.  254   4                                              
  264.  255   4                       
  265.  256   4                       }
  266.  257   3               memcpy(&money,&g_cReceBuf[2],4);
  267.  258   3                money = money - 1.5;
  268.  259   3               if (money <0)               //余额不够,重新初始化赋值
  269.  260   3                 {
  270.  261   4                          cStatus  =  SPI_Write(ComHaltCard);
  271.  262   4                          DrawString (45,150,"初始化",BLUE,GREEN,NORMAL)      ;    
  272.  263   4                          delay_1ms(500);
  273.  264   4                          DrawString (45,150,"           ",GREEN,GREEN,NORMAL)        ; 
  274.  265   4                              flag = 1;       
  275.  266   4                          break;
  276.  267   4      
  277.  268   4                 }
  278.  269   3      
  279.  270   3               
  280.  271   3      
  281.  272   3               memcpy(&ComWriteBlock4[10],&money,4); 
  282.  273   3             cPa =ComWriteBlock4;
  283.  274   3                SPI_Write(cPa);
  284.  275   3                delay_1ms(100);                                                 
  285.  276   3              cStatus = SPI_Read(g_cReceBuf);
  286.  277   3               while ((g_cReceBuf[1] != cPa[1]) || cStatus != SUCCESS)
  287.  278   3                       {
  288.  279   4                       cPa =ComWriteBlock4;
  289.  280   4                       SPI_Write(cPa);
  290.  281   4                       delay_1ms(100);
  291.  282   4                       cStatus = SPI_Read(g_cReceBuf);
  292.  283   4                       delay_1ms(100);        
  293.  284   4                               
  294.  285   4                       
  295.  286   4                       }
  296.  287   3      
  297.  288   3              ///////////////////////
  298.  289   3      
  299.  290   3              
  300.  291   3              
  301.  292   3                      
  302.  293   3           sprintf(str1,  "%0.1f", money); 
  303.  294   3                       SPI_Write(ComHaltCard);                                
  304.  295   3         //设置为休眠状态
  305.  296   3                 
  306.  297   3              DrawString(125, 208,"AC7A3E74",BLACK, WHITE,  NORMAL);
  307.  298   3                      DrawString(125, 244,str1,BLACK, WHITE,  NORMAL);
  308.  299   3                      DrawString(180, 244,"元",BLACK,WHITE,  NORMAL);
  309.  300   3      
  310.  301   3              delay_1ms(2000);
  311.  302   3               
  312.  303   3                       DispOneColor(120,200,230,231,WHITE)    ;
  313. C51 COMPILER V8.08   MAIN                                                                  03/14/2010 21:12:48 PAGE 6   
  314.  304   3                       DispOneColor(120,236,230,267,WHITE)    ;                                               
  315.  305   3                       DispOneColor(120,200,230,231,WHITE)    ;//baibiao
  316.  306   3                                        
  317.  307   3                                      
  318.  308   3                 }
  319.  309   2                }
  320.  310   1      
  321.  311   1               
  322.  312   1      
  323.  313   1       
  324.  314   1      
  325.  315   1      }                                                   
  326.  316          /*****************************************************************************
  327.  317          *system initialize
  328.  318          *UART baudrate = 19200bps, baud generator is T1
  329.  319          *****************************************************************************/
  330.  320          void InitializeSystem()
  331.  321          {
  332.  322   1        
  333.  323   1              ET2 = 0;
  334.  324   1              T2CON = 0x04;
  335.  325   1          PCON = 0x80;
  336.  326   1          SCON = 0x70;
  337.  327   1          TMOD = 0x21;        //TMOD = 0x22;
  338.  328   1      
  339.  329   1              TH1   = BAUD_19200;  //默认波特率
  340.  330   1              TL1   = TH1;
  341.  331   1              TR1   = TRUE;             // 波特率发生器
  342.  332   1      
  343.  333   1              ET1=0;
  344.  334   1              EA=1;
  345.  335   1              EX0=0;;
  346.  336   1              IT0 = 1;
  347.  337   1              TR2=0;
  348.  338   1              ES = TRUE;
  349.  339   1              g_bReceCommandOk=0;                                                             
  350.  340   1      
  351.  341   1      }
  352. MODULE INFORMATION:   STATIC OVERLAYABLE
  353.    CODE SIZE        =   2974    ----
  354.    CONSTANT SIZE    =   1166    ----
  355.    XDATA SIZE       =   ----    ----
  356.    PDATA SIZE       =   ----    ----
  357.    DATA SIZE        =      7      87
  358.    IDATA SIZE       =     90      39
  359.    BIT SIZE         =      3    ----
  360. END OF MODULE INFORMATION.
  361. C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)