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

📄 main.c

📁 射频卡读卡头的程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
*                                                                           *
* File:         MAIN.C                                                      *
*                                                                           *
* Version:      V3.2                                       		            *
*                                                                           *
* Created:      2003/10/11                                                  *
* Last Change:  2005/06/04                                                   *
* 增加看门狗控制 防止CPU 死机                                               *
* Author:       DavidQi                                                     *
*                                                                           *
* Compiler:     KEIL C51 V6.23A                                             *
*                                                                           *
* Description:  输出 M1序列号                                               *
*                                                                           *
****************************************************************************/
#pragma large
#define __SRC
#include "main.h"
#undef  __SRC

#include <string.h>
#include <intrins.h>
#include <stdio.h>

#include "m500a.h"
#include "cpu.h"
#include "rdio.h"

#define MIS_CHK_OK              (0)
#define MIS_CHK_FAILED          (-1)
#define MIS_CHK_COMPERR         (-2)

// Function: mifs_request
#define IDLE                    0x00
#define ALL                     0x01


// PC Debug Data: 02 30 31 32 03 33     checksum =33 (30^31^32)
// Release Number of the Firmware
uchar code SW_Rel[] = "\n\r MFRC500 V1.0 8.03.2003 \n\r";
uchar code ms[4][2] = {{0x0,0x0},{0x0,0x0},{0x0,0x0},{0x0,0x0}};

// Serial Number of the MFRC500
//uchar Snr_RC500[4];
uchar xdata Snr_RC500[4];
//static uint Crc;

// Local Prototypes
void init(void);
void cmd_execution(void);

// Interrupt Service Routines
void isr_timer0(void);
void isr_UART(void);

//uchar card_snr[4];
uchar  xdata card_snr[4];
char   xdata ErrNum;


//cqbq
uchar idata cardnum[4];    //无符号字符数组变量cardnum[4]被定义为idata 存贮类型
uchar xdata nums;

unsigned char code CmdData_1[]={0x7f,0x40,0x05,0x0,0x0,0x0,0x0};

unsigned char code CmdData_2[]={0x7f,0x42,0x08,0x0,0x0,0x0,0x0,0x0};

uchar xdata SectorNum, BlockNum,CommandNum,Key[6],CardData[16],KeyData[6];

uchar xdata CardSnrNum[6];

uchar xdata SnrErr;

uchar idata ComeCard = 0,GoCard = 0;ChkSum1 = 0;
uchar idata DetCardCnt=0;
bit   bdata SendCardSnrFlag;

//cqbq
bit CopyCardNum ( void )
{
     bit flag_num=1;      //因为默认存贮类型为XDATA,所以表示为 位变量flag_num
                          // 被定义为 XDATA存贮类型
     uchar idata i=0;
     uchar idata *pnt0 = &cardnum;  //无字符指针变量pnt0被定义为 idata 存贮类型,并将数组
                                    // cardnum 的首地址赋给指针变量pnt0,而不是(*pnt0)
     uchar xdata *pnt1 = &SerBuffer[3];

     if (SerBuffer[3]==SerBuffer[4] && SerBuffer[5] == SerBuffer[6] && SerBuffer[3] == SerBuffer[5])
        return flag_num;

     for (i=0;i<4;i++)
     {
         if (*pnt0==*pnt1)
         {
             pnt0++;
             pnt1++;
         }
         else
         {
             nums++;
             if (nums>10)
                flag_num=0;
         }
         break;
     }

     if (nums>10)
     {
        pnt0=&cardnum;
        pnt1=&SerBuffer[3];
        for (i=0;i<4;i++)
        {
            *pnt0++ = *pnt1++;
        }
        nums=0;
     }
     return ( flag_num );
}

void main (void)
{
  init();

  test();

  #ifdef sst89e554
  WDTC = WDTC|0x01;     //Start watchdog timer
  #endif
  
  #ifdef sm8958  
  //WDTC_CLR = 1;        // refresh watchdog timer 
  clrWDT();
  
  #endif

  M500PcdConfig();  	// Initialise the RC500
	                    // must be call in the initialization

//  RC500 E2PROM  0x00--0x0f Product Information Field
//                0x08--0x0b  Product Serial Number

  PcdReadE2(8,4,Snr_RC500); // Read out the MFRC500 serial number and store it

  RingOn();

  SendCardSnrFlag = 1;

  while(1)
  {
    init();
    M500PcdConfig();

    DetCardCnt = 10;
    ComeCard = 0;

    while(DetCardCnt!=0|ComeCard==3)
    {
     SerBuffer[1] = 0x40;
     cmd_execution();
     
      //cqbq
        if ( AccOk )
        {
           SerBuffer[1] = 0x42;
           SerBuffer[3] = 0;
           cmd_execution();

           if ( AccOk )
           {
              if ( !CopyCardNum() )
              {
                 AccOk=0;
                 P1_6=0;
                 GoCard=0;
                 ComeCard=0;

              delay_10ms (5);
              }
            }
        }
        // 

     if(AccOk )
     {
        ComeCard++;
        if(ComeCard == 3)
        {
          P1_6 = 1;
         // ComeCard = 0;
          break;
        }
        else
        {
           if(ComeCard > 3)
           {
              ComeCard = 3;
              P1_6 = 1;
           }
        }
        AccOk = FALSE ;
        GoCard = 0;
     }
     else
     {
         GoCard++;
         if(GoCard == 3)
         {
           P1_6 = 0;
           GoCard = 0;
         }
         ComeCard = 0;

         SendCardSnrFlag = 1;

     }

     if(DetCardCnt != 0 )  DetCardCnt--;

     M500PcdConfig();
     //SerBuffer[1] = 0x45;
     //cmd_execution();
     //delay_10ms(5);
     #ifdef sst89e554
     WDTC = WDTC|0x02;  // refresh watchdog
     WDTD = 0x80;       // Reload timer
     #endif
     
     #ifdef sm8958  
     //WDTC_CLR = 1;        // refresh watchdog timer
  	 clrWDT();       
     #endif 
   }

    #ifdef sst89e554
    WDTC = WDTC|0x02;  // refresh watchdog
    WDTD = 0x80;       // Reload timer
    #endif
    
    #ifdef sm8958  
     //WDTC_CLR = 1;        // refresh watchdog timer
  	 clrWDT();       
     #endif 

    M500PcdConfig();
   // SerBuffer[1] = 0x45;
   // cmd_execution();
   // delay_10ms(5);

// 检测是否有MIFARE卡在读头感应区,有卡将卡号发送到PC机
   /*
    while(!AccOk)
    {
      memcpy(SerBuffer,CmdData_1,7);
      cmd_execution();

	  AccOk = FALSE ;

      memcpy(SerBuffer,CmdData_2,8);

     // 命令码: 0x42:  执行 Anticoll 返回序列号
      cmd_execution();

     }

    AccOk = FALSE ;

    TransHexAsi(&SerBuffer[3],SendBuffer,4);//4字节卡序列号

    SendDataToPc(SendBuffer,8);//发送8字节ASCII码到PC
    */
    AccOk = FALSE ;
    //等待PC 发送命令
   // while(!FrameOk);
    //RingOn();

    //TransData(PcData,SerBuffer,DataLen);//SerBuffer: PC 与MCU交互数据缓冲区

    /*
     cmd_execution() 命令执行程序
     命令码:               命令功能:
     0x40:                  // PICC_REQALL
     0x41:                  // Request
     0x42:                  // Anticoll
     0x43:                  // Select
     0x44:                  // Authentication
     0x45:                  // Halt
	 0x46:                  // Read
     0x47:                  // Write
	 0x48:                  // Increment
     0x49:					// Decrement
     0x4A:					// Restore
	 0x4B:					// Transfer
	 0x4C:					// Load Key
     0x4D:					// comm check char
     0x4E:					// Reset
     0x4F:                 	// Get Info
     0x50:					// Set Control Bit
     0x51:					// Clr Control Bit
     0x52:                 	// Config
     0x53:					// Check Write
     0x54:					// Set Port
     0x55:					// Get Port
     0x56:                	// Authentication2
     0x57:                  // Decrement+Transfer
    */
    CommandNum =  SerBuffer[0]; //命令码
    SectorNum =  SerBuffer[1];  //mifare1卡:扇区号
    BlockNum =  SerBuffer[2];   //mifare1卡:块号
    memcpy(KeyData,&SerBuffer[3],6);//mifare1卡:块密码6字节
    memcpy(CardData,&SerBuffer[3],16);//mifare1卡:待写的16字节数据

    FrameOk = 0;
    if(CommandNum == 0x4c)
    {

       SerBuffer[1] = 0x4c;
       SerBuffer[MODE] = PICC_AUTHENT1A;  //MODE = 3
       SerBuffer[SECNR] = SectorNum;      //SECNR = 4
       memcpy(&SerBuffer[13],KeyData,6);
       cmd_execution();
       if(!ErrNum)
       {
             SerBuffer[3] = 0x0;
             TransHexAsi(&SerBuffer[3],SendBuffer,1);
             SendDataToPc(SendBuffer,2);
             RingOn();
       }

    }

    else
    {
      memcpy(SerBuffer,CmdData_1,7);  //命令码:0x40  执行PICC_REQALL 返回卡型2字节

      cmd_execution();

     if(!ErrNum)
     {

        memcpy(SerBuffer,CmdData_2,8);

        cmd_execution();//命令码:0x42: 执行 Anticoll 返回序列号

        AccOk = FALSE ;

        memcpy(card_snr,&SerBuffer[SERNR],4);
//--------------------------------------------------------------------------
// | 02 | 10 ASCII Data |Characters Checksum | CR | LF | 03 |
//--------------------------------------------------------------------------
        SnrErr = CheckSnrNum();
        if(SnrErr == 0)
        {
          if(SendCardSnrFlag == 1)
          {
           SendSnrNum();   //发送序列号:02+10字节(0xff+0x + + + + )+checksum(2字节)+0d + 0a + 03
           SendCardSnrFlag = 0;
           FlashLED();
          }

        }
/*
        if(!ErrNum)
        {
           SerBuffer[1] = 0x43;

           cmd_execution();// 命令码: 0x43:  选择卡片 返回卡片的 SIZE 字节 0x88h

           AccOk = FALSE ;

           SerBuffer[1] = 0x44;
           SerBuffer[2] = BlockNum;

           SerBuffer[3] = 0 ;

           SerBuffer[4] = SectorNum; //卡扇区号(0-15):= 2(含块8,9,10)

           cmd_execution();//命令码:0x44: 密码认证操作

           if(!ErrNum)
           {
               if(AuthOk == 0)
               {
                   switch (CommandNum)
                   {
                     case 0x46:
                       SerBuffer[1] = 0x46;          //读卡命令
                       SerBuffer[3] = BlockNum ;     //卡块号(0-63): =8(8,9,10)
                       cmd_execution();
                       TransHexAsi(&SerBuffer[3],SendBuffer,16);
                       SendDataToPc(SendBuffer,32);
                       break;

                     case 0x47:
                       SerBuffer[1] = 0x47;   //写卡命令
                       SerBuffer[3] = BlockNum ;      //卡块号

                       memcpy(&SerBuffer[4],CardData,16);// 16字节 写卡数据

                       cmd_execution();
                       if(!ErrNum)
                       {
                         SerBuffer[3] = 0x0;
                         TransHexAsi(&SerBuffer[3],SendBuffer,1);
                         SendDataToPc(SendBuffer,2);
                         RingOn();
                       }
                        break;

                     default:
                        break;

                   }

               }

           }

        }

*/

      }

    }

 //   delay_10ms(50);


    if(ErrNum)
    {
      delay_10ms(5);
     /*
      SerBuffer[3] = ErrNum   ;
      TransHexAsi(&SerBuffer[3],SendBuffer,1);
      SendDataToPc(SendBuffer,2);
     */
    }

  }

}

/****************************************************************************
*                                                                           *
* Function:     init                                                        *
*                                                                           *
* Input:        -                                                           *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
*                                                                           *
****************************************************************************/

void 	init (void)
{

  AuthOk      = 0xff;

  AccOk       = FALSE;
  //EnOnChipSram();       // Enable w77e58 on chip 1k sram

  #ifdef w77e58
  DME0        = TRUE;
  PMR         = 0x41;
  EIE         = 0x0;
  #endif


  #ifdef sm8958

  //OME = 1  : 768片上字节使能位;
  //ALE1 = 0 : ALEALE输出使能;    ALE1 = 1 : ALE输出禁止,可降低EMI;
  SCONF       = 0x02;   //00000010b
  //解锁WDTC
  WDTKEY	  = 0x1e;
  WDTKEY	  = 0xe1;
  //WDTE:  看门狗定时器使能,启动看门狗计数器
  //PS2~PS0: 时钟源分频选择512
  WDTC        = 0x87;//0x86;   //10000110b
  //保护WDTC
  WDTKEY	  = 0xe1;
  WDTKEY	  = 0x1e;

  #endif
  
  #ifdef sst89e554
  AUXR        = 0;
  WDTC        = 0x0a;
  WDTD        = 0x80; //4s
  #endif

  LED         = ON;
  BEEP        = BeepOn;
  BEEP        = BeepOff;
  RC500RST    = FALSE;
  //RC500_CS    = TRUE;	// Enable the CS for RC500

  CmdReceived = FALSE;
  CmdValid    = FALSE;
  Quit        = FALSE;
  LLfReady    = TRUE;
  SendReady   = TRUE;
  Idle        = TRUE;

  RepCnt      = 0;

  RecvState   = RECV_STX;

  EnableTransferCmd = FALSE;

  CheckByteCnt = BCC_CHECKBYTECNT;

#ifdef AUTODELAY
  DelayRate = 0;
  DelayRateLocked = TRUE;
#endif

  FrameEnd    = 0;
  FrameHead   = 0;
  FrameOk     = 0;
  DataNum     = 0;
  ChkSum      = 0;

  PCON = 0x00;              	// SMOD = 1;
  SCON = 0x50;              	// Mode 1, 8-bit UART, enable receiption

//  EIE  = 0x0 ;
//  AutoBaud = TRUE;
  TMOD     = 0x20;      	// Timer 1, mode 2, 8-bit auto reload,
  		  		// Timer 0, mode 0, 13-bit counter
  Capt_L   = 0;
  Capt_H   = 0;

  TH1 = 0xfd ;
  TL1 = 0xfd ;

  ES = 1 ;
  TR1 = 1;

 // LED = 0;
 // delay_10ms(10);
 // LED = 1;


  PX1 = 1;
  IT1 = 1;    			// Config ext1 as edge trigger for RC500
  EX1 = 1; 			// Enable ext1 interrupt for RC500
  RC500RST = TRUE;
  EA = TRUE;			// Enable all interrupts
  OpenIO();
}


/****************************************************************************
*                                                                           *
* Function:     cmd_execution                                               *
*                                                                           *
* Input:        SerBuffer[]                                                 *
* Output:       SerBuffer[]                                                 *
*                                                                           *
* Description:                                                              *
*                                                                           *
*                                                                           *
****************************************************************************/

void 	cmd_execution (void)
{
  uchar xdata Status;
  uchar xdata len;
  uchar xdata cmd;
  uchar xdata adr;
  uchar xdata tmp[16];

⌨️ 快捷键说明

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