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

📄 main.c

📁 单片机数据通讯典型应用(光盘内容)
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*                                                                           *
* File:         MAIN.C                                                      *
*                                                                           *
* Version:      1.0                                       	       		    *
*                                                                           *
* Created:      21.06.2002                                                  *
* Last Change:  22.06.2002                                                  *
*                                                                           *
* Author:       Steven Jiang Chang                                               *
*                                                                           *
* Compiler:     KEIL C51 uVision2 V2.23                                              *
*                                                                           *
* Description:  89C52RD2-Firmware for MFRC500 Demo Serial Reader            *
*                                                                           *
****************************************************************************/

#define __SRC
#include "main.h"
#undef  __SRC

#include <string.h>
#include <intrins.h>
#include <stdio.h>
#include <m500a.h>
#include <p89c51rx.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

sbit    RC500RST        	= P3^5;
sbit    RC500_CS         	= P2^7;
sbit    LED	        	= P3^4;
//sbit    LED	        	= P3^4;

// Release Number of the Firmware
uchar code SW_Rel[] = "\n\r MFRC500 V1.0 22.06.02 \n\r";

// Serial Number of the MFRC500 
uchar Snr_RC500[4];

static uint Crc;

// Local Prototypes
void init(void);


 code Nkey_a[6]    = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
 code Nkey_b[6]    = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};


/****************************************************************************
*                                                                           *
* Function:     main                                                        *
*                                                                           *
* Input:        -                                                           *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
*                                                                           *
****************************************************************************/
void 	main (void)
{
  unsigned char counter,counter2;
  unsigned char mfout=2;//readbuf[16];
  unsigned char tt1[2];
  unsigned char status1;
  unsigned char cardserialno[4];
  unsigned char *sak1;
  unsigned char blockdata[16];
  init();
  M500PcdConfig();  	// Initialise the RC500
	                // must be call in the initialisation
  PcdReadE2(8,4,Snr_RC500); // Read out the MFRC500 serial number and store it
    M500PcdMfOutSelect(mfout);

  for (counter=0;counter<20;counter++)
 {
  	status1 = M500PiccRequest(PICC_REQALL, tt1);
  	if (status1==MI_OK)
		status1=M500PiccAnticoll(0,cardserialno);
  	if (status1==MI_OK)
    	status1=M500PiccSelect(cardserialno,sak1);
  	if (status1==MI_OK)
	  	status1 = M500PiccAuth(PICC_AUTHENT1A, cardserialno, 1, 4);
  	if (status1 ==MI_OK)
      	status1=M500PiccRead(4, blockdata);
  	for ( counter2=0;counter2<16;counter2++)
       	blockdata[counter2]=counter;
  	if (status1 ==MI_OK)
	  	status1 = M500PiccWrite(4,blockdata);                
     	
  }
}


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

void init(void)
{
  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

  PCON = 0x80;              	// SMOD = 1;
  SCON = 0x50;              	// Mode 1, 8-bit UART, enable receiption
  
  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;
  
  LED = OFF;
  delay_10ms(50);
  LED = ON;

  IT0 = 1;    			// Config ext0 as edge trigger for RC500
  EX0 = 1; 			// Enable ext0 interrupt for RC500

  EA = TRUE;			// Enable all interrupts

}

void auto_baud (void)   
{   
  //int temp;    
   
  if (AutoBaud && !RXD) // If autobaud is enable and RXD pin is low    
  {   
    TR0 = TRUE;     // Start timer0    
   
    do   
    {   
      if (RXD)      // When RXD pin is high    
      {   
        Capt_L = TL0;   
        Capt_H = TH0;   
   
        do   
        {   
          if (!RXD)   
          {   
            break;   
          }   
        } while (!TF0);   
      }   
    } while (!TF0);   
   
    TR0 = FALSE;   
    TF0 = FALSE;   
   
    Capt = ((uint)Capt_H << 5) | ((uint)Capt_L & 0x001F);   
   
    for (BaudRate = BAUD_CNT; BaudRate >= 0; BaudRate--)   
    {   
      if (Capt >= CmpTable[BaudRate])   
      {   
        break;   
      }   
    }   
   
    if (BaudRate < 0 || BaudRate == BAUD_CNT) // Error baud count    
    {   
      TH0    = 0;   
      TL0    = 0;   
      Capt_L = 0;   
      Capt_H = 0;   
      return;   
    }   
   
    TMOD = 0x21;                    // Timer 0, mode 1, 16-bit counter    
    TH1  = BaudRateTable[BaudRate];   
    TL1  = TH1;   
    TR1  = TRUE;   
   
    TH0  = 0xE0;   
    TL0  = 0;   
    TR0  = TRUE;   
    while (!TF0);                   // Wait 4.4 ms    
    TR0  = FALSE;   
    TF0  = FALSE;   
    ET0  = TRUE;   
   
    AutoBaud = FALSE;       // Baudrate had been choose, disable Autobaud    
   
    SBUF = NAK;   
    while (!TI);   
    TI   = FALSE;   
    RI   = FALSE;   
    ES   = TRUE;   
  }   
}   


/****************************************************************************
*                                                                           *
* Function:     delay_50us                                                  *
*                                                                           *
* Input:        _50us                                                       *
* Output:       -                                                           *
*                                                                           *

⌨️ 快捷键说明

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