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

📄 main.c

📁 本程序是基于FM1702的射频卡读写器的单片机控制的C语言程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*                                                                           *
* File:         MAIN.C                                                      *
*                                                                           *
* Version:      1.0                                       	       		    *
*                                                                           *
* Created:      2007.11.30                                                  *
* Last Change:  2007                                                        *
*                                                                           *
* Author:       HeXing                                                      *
* QQ: 271885846                                                             *
* EMail: 271885846@qq.com                                                   *
* Compiler:     KEIL C51 uVision2 V2.23                                     *
*                                                                           *
* Description:  89C52-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; //RC500复位端口
sbit    RC500_CS         	= P2^7; //RC500选通端口 Chip Select
sbit    LED					= P1^1; //LED 显示
sbit	BEEP				= P1^0;//蜂鸣器

uint AbsoluteBlock = 8; //对绝对块8进行操作, 取值范围为 0 <= AbsoluteBlock < 64

// 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};

/*
 存储结构
1、 M1卡分为16个扇区,每个扇区由4块(块0、块1、块2、块3)组成,
    (我们也将16个扇区的64个块按绝对地址编号为0~63,
2、 第0扇区的块0(即绝对地址0块),它用于存放厂商代码,已经固化,不可更改。
3、 每个扇区的块0、块1、块2为数据块,可用于存贮数据。
   数据块可作两种应用:
★ 用作一般的数据保存,可以进行读、写操作。
★ 用作数据值,可以进行初始化值、加值、减值、读值操作。
4、 每个扇区的块3为控制块,包括了密码A、存取控制、密码B。
5、 每个扇区的密码和存取控制都是独立的,
	可以根据实际需要设定各自的密码及存取控制。
	存取控制为4个字节,共32位,扇区中的每个块(包括数据块和控制块)
	的存取条件是由密码和存取控制共同决定的,在存取控制中每个块都有相应的三个控制位,
	定义如下:
    
          块0:   C10   C20   C30
          块1:   C11   C21   C31
          块2:   C12   C22   C32
          块3:   C13   C23   C33
*/

/****************************************************************************
*                                                                           *
* 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];
  int i;

  init();
  M500PcdConfig();  	// Initialise the RC500 初始化RC500,寄存器
	                // must be call in the initialisation
  PcdReadE2(8,4,Snr_RC500); // 读RC500芯片的序列号,保存到Snr_RC500. Read out the MFRC500 serial number and store it

	printf("RC500的序列号:0x");
	for(counter=0;counter<4;counter++)
	{
		i = Snr_RC500[counter]<<4>>4;//因为printf(%x) 只可以16进制显示整数,所以必须把uchar左移4位再右移4位,下同
		printf("%X", i);
	}
	printf("\r\n");

  M500PcdMfOutSelect(mfout); // Route serial output to pin, for test

  printf("进入轮循, 等待读卡...\r\n");

  while(1)//for (counter=0;counter<20;counter++)
 {
	
  	status1 = M500PiccRequest(PICC_REQALL, tt1);//寻卡
  	if (status1==MI_OK)//如果找到卡
	{
		if(tt1[0]==2)
			printf("发现Mifare Pro 卡!\r\n");//type = 0;MifareProCard;     // Mifare Pro 卡
		else if(tt1[0]==4)
			printf("发现Mifare One 卡!\r\n");//type= 1;//MifareOneCard;       // Mifare One 卡
		else if(tt1[0]==16)
			printf("发现Mifare Light卡!\r\n");//type= 2;//MifareLightCard;   // Mifare Light 卡
		else
			return;			//未知
		

		status1=M500PiccAnticoll(0,cardserialno);//

		printf("卡号:0x");
		for(counter=0;counter<4;counter++)
		{
			i = cardserialno[counter]<<4>>4;//因为printf(%x) 只可以16进制显示整数,所以必须把uchar左移4位再右移4位,下同
			printf("%X ", i);
		}
		printf("\r\n");		
	}
	else
	{
		//printf("没有发现卡\r\n");
		delay_10ms(50);
		continue;//提前终止此轮循环, 并跳到while入口处
	}

	//选择卡
  	if (status1==MI_OK)
	{
    	status1=M500PiccSelect(cardserialno,sak1);//选择卡
		//printf("通过M500PiccAnticoll()!\r\n");
	}
	else
	{
		delay_10ms(50);
		continue;
	}

	//授权
  	if (status1==MI_OK)
	{	
		//对第八块(绝对地址)进行授权, 也就是第三个扇区第一块
	  	status1 = M500PiccAuth(PICC_AUTHENT1A, cardserialno, 1, AbsoluteBlock);
		//printf("通过M500PiccSelect()!\r\n");
	}
	else
	{
		printf("选卡失败\r\n");
		continue;
	}

	//授权通过则读取该块数据
  	if (status1 ==MI_OK)
	{
      	status1=M500PiccRead(AbsoluteBlock, blockdata);
		//printf("通过M500PiccAuth()!\r\n");
		printf("绝对块地址 %d 读取的16个字节数据:0x", AbsoluteBlock);
		for(counter=0;counter<16;counter++)
		{
			i = blockdata[counter]<<4>>4;//因为printf(%x) 只可以16进制显示整数,所以必须把uchar左移4位再右移4位,下同
			printf("%X,", i);
		}
		printf("\r\n");
	}
	else
	{
		printf("授权失败,可能该块 %d 为只读块,或者您提供的读写密码不正确!\r\n", AbsoluteBlock);
		delay_10ms(50);
		continue;
	}


	//把读出的16个字节数据均加1, 写回卡中对应位置!
  	for ( counter2=0;counter2<16;counter2++)
       	blockdata[counter2] += 0x0001;

	//立即写!
  	if (status1 ==MI_OK)
	{
	  	status1 = M500PiccWrite(AbsoluteBlock,blockdata);                
		//printf("通过M500PiccRead()!\r\n");

		printf("正在对绝对块地址 %d 写入的16个字节数据:0x", AbsoluteBlock);
		for(counter=0;counter<16;counter++)
		{
			i = blockdata[counter]<<4>>4;//因为printf(%x) 只可以16进制显示整数,所以必须把uchar左移4位再右移4位,下同
			printf("%X,", i);
		}
		printf("\r\n");
	}

  	if (status1 ==MI_OK)
	{
//		printf("通过M500PiccWrite()!\r\n");
		printf("数据已经成功写入绝对块 %d \r\n", AbsoluteBlock);
	}

	printf("---------------------------------------\r\n");

	  LED = OFF;
	  BEEP = OFF;
	  delay_10ms(50);
	  LED = ON;
	  BEEP = ON;
  }

	while(1){};
}


/****************************************************************************
*                                                                           *
* 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;

⌨️ 快捷键说明

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