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

📄 main.c

📁 MRFIC读写器-51汇编源程序及C51源程序.zip
💻 C
📖 第 1 页 / 共 2 页
字号:

// MAIN.C

#include "main.h"

//--------------------------------------------------------------------

// 读写卡片操作数据: 发送 接收

idata BYTE tmpData[32],CommArray[64];

//--------------------------------------------------------------------

void main(void)
{
        BYTE i,rt;
        BYTE tmpKeyType,tmpBlockNo,tmpKey[6];
        BYTE tmpValue[4],tmpBalance[4];
        idata BYTE psData[16];

	// Initialize SoftUART
        Init_SoftUART();

        Delay_10ms();         // 根据具体硬件调整延时,确保读卡模块可靠复位后,系统开始工作 !!!

        tmpKeyType = 0;       // KEY A
        tmpBlockNo = 4;       // Block 4
        for(i = 0; i < 6; i++) tmpKey[i] = 0xFF;

        for(;;)
        {
                rt = CardReady(psData);
                if(rt != 0) rt = CardReady(psData);
                if(rt != 0) rt = CardReady(psData);
                if(rt == 0)
                {

//--------------------------------------------------------------------
//write
                        for(i = 0; i < 16; i++) psData[i] = i;
                        rt = WriteCard(tmpBlockNo,tmpKeyType,tmpKey,psData);

//read
                        for(i = 0; i < 16; i++) psData[i] = 0x00;
                        rt = ReadCard(tmpBlockNo,tmpKeyType,tmpKey,psData);

//--------------------------------------------------------------------
//write
                        for(i = 0; i < 16; i++) psData[i] = 0x00;
                        rt = WriteCard(tmpBlockNo,tmpKeyType,tmpKey,psData);

//read
                        for(i = 0; i < 16; i++) psData[i] = 0xFF;
                        rt = ReadCard(tmpBlockNo,tmpKeyType,tmpKey,psData);

//--------------------------------------------------------------------
//write
                        for(i = 0; i < 16; i++) psData[i] = 0xFF;
                        rt = WriteCard(tmpBlockNo,tmpKeyType,tmpKey,psData);

//read
                        for(i = 0; i < 16; i++) psData[i] = 0x00;
                        rt = ReadCard(tmpBlockNo,tmpKeyType,tmpKey,psData);

//--------------------------------------------------------------------
// Purse
                        for(i = 0; i < 4; i++) tmpValue[i] = 0x00;
                        for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;

// FormatPurse
                        tmpValue[0] = 100;
                        rt = FormatPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpValue);
                        for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;
                        rt = ReadPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpBalance);

// Increase
                        tmpValue[0] = 10;
                        rt = Increase(tmpBlockNo,tmpKeyType,tmpKey,tmpValue);
                        for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;
                        rt = ReadPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpBalance);

// Decrease
                        tmpValue[0] = 20;
                        rt = Decrease(tmpBlockNo,tmpKeyType,tmpKey,tmpValue);
                        for(i = 0; i < 4; i++) tmpBalance[i] = 0x00;
                        rt = ReadPurse(tmpBlockNo,tmpKeyType,tmpKey,tmpBalance);

//--------------------------------------------------------------------

                }
        }
}

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

void ChangeHexToDbb(BYTE *psData,BYTE *pdData,BYTE length)
{
	BYTE i;
	BYTE tmp,t1,t2;

	for(i = 0; i < length; i++)
	{
	        tmp = psData[i];

	        t1 = tmp >> 4;
	        if(t1 <= 9) t1 += '0';
	        else t1 += 'A' - 10;

	        t2 = tmp & 0x0F;
		if(t2 <= 9) t2 += '0';
	        else t2 += 'A' - 10;

	        pdData[2 * i]     = t1;
	        pdData[2 * i + 1] = t2;
	}

	pdData[2 * length] = 0x03;
}

void ChangeDbbToHex(BYTE *psData,BYTE *pdData,BYTE length)
{
	BYTE i;
	BYTE t1,t2;

	for(i = 0; i < length; i += 2)
	{
	        t1 = psData[i];
		t2 = psData[i+1];

	        if     (t1 >= 'A' && t1 <= 'F') t1 = t1 + 10 - 'A';
	        else if(t1 >= 'a' && t1 <= 'f') t1 = t1 + 10 -'a';
	        else if(t1 >= '0' && t1 <= '9') t1 = t1 - '0';

	        if     (t2 >= 'A' && t2 <= 'F') t2 = t2 + 10 - 'A';
	        else if(t2 >= 'a' && t2 <= 'f') t2 = t2 + 10 - 'a';
		else if(t2 >= '0' && t2 <= '9') t2 = t2 - '0';

	        pdData[i / 2] = (t1 << 4) + t2;
	}
}

BYTE XORCheck(BYTE *psData,BYTE length)
{
	BYTE i;
	BYTE XORData;

	XORData = 0;

	for(i = 0; i < length; i++)
	{
	        XORData ^= psData[i];
	}

	return XORData;
}

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

void Delay_10ms(void)
{
        BYTE i;

        for(i = 0; i < 200; i++) Delay_52us_NOP();
}

void Delay_52us_NOP(void)
{
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}

void Delay_26us_NOP(void)
{
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();
}

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

void Init_SoftUART(void)
{
        PCON = 0x80;
        SCON = 0x50;

        T2CON = 0x30;               // Timer 2 16-bit auto reload
        
        RCAP2L = 0xEE;              // 0xEE for 19200
        RCAP2H = 0xFF;
        TL2   = 0xEE;               // 0xEE for 19200
        TH2   = 0xFF;

        TR2   = TRUE;               // Timer 2 run

//        ES = TRUE;                  // Enable serial interrupt

}

void SoftUART_PutByte(BYTE psData)
{
        SBUF = psData;

        while(!TI);
        TI = FALSE;
}

BYTE SoftUART_GetByte(void)
{
        while(!RI);
        RI = FALSE;

        return SBUF;
}

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

BYTE CardReady(BYTE *CardSNO)
{
        BYTE i;

//--------------------------------------------------------------------
// Commamd: 0x60 0x04 0x01 0x61 0x00 0x00 BCC 0x03
// send 15 byte  7 * 2 + 1 = 15
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x04;
        tmpData[2] = 0x01;
        tmpData[3] = 0x61;
        tmpData[4] = 0x00;
        tmpData[5] = 0x00;
        tmpData[6] = XORCheck(tmpData,6);

        ChangeHexToDbb(tmpData,CommArray,7);

        for(i = 0; i < 15; i++)
        {
                SoftUART_PutByte(CommArray[i]);
        }

//--------------------------------------------------------------------
// Right: 0x60 0x05 SNR0 …SNR3 0x00 BCC 0x03
// recieve 17 byte  8 * 2 + 1 = 17
// Error: 0x60 0x01 0x01 BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
//--------------------------------------------------------------------

        for(i = 0; i < 17; i++)
        {
                CommArray[i] = SoftUART_GetByte();
                if(CommArray[i] == 0x03) break;
        }

        if(CommArray[0] != '6') return 1;
        if(i < 10) return 1;

        ChangeDbbToHex(&CommArray[4],tmpData,8);

        for(i = 0; i < 4; i++) CardSNO[i] = tmpData[i];

//--------------------------------------------------------------------

        return 0;

}

BYTE  ReadCard(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *CardData)
{
        BYTE i;

//--------------------------------------------------------------------
// Command: 0x60 0x0A 0x01 0x62 KEYTYPE BlockNo KEY0 … KEY5 BCC 0x03
// send 27 byte  13 * 2 + 1 = 27
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x0A;
        tmpData[2] = 0x01;
        tmpData[3] = 0x62;
        tmpData[4] = KeyType;

⌨️ 快捷键说明

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