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

📄 main.c

📁 C51下面,用IO口模拟uart的源代码,里面有文档说明.
💻 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)
{
        UART_TX_BIT = 1;   // TX output
        UART_RX_BIT = 1;   // RX input and pull up
}

void SoftUART_PutByte(BYTE psData)
{
        BYTE i;
        BYTE UART_Data;

        UART_Data = psData;

        for(i = 0; i < 10; i++)
        {
                if(i == 0)
                {
                        UART_TX_BIT = 0;   // generate start bit
                }
                else if(i == 9)
                {
                        UART_TX_BIT = 1;   // generate stop bit
                }
                else
                {
	                if(UART_Data & 0x01)
	                {
                                UART_TX_BIT = 1;   // generate HIGH level
                        }
	                else
	                {
                                UART_TX_BIT = 0;   // generate LOW level
                        }

	                UART_Data >>= 1;
                }

                Delay_52us_NOP();
        }
}

BYTE SoftUART_GetByte(void)
{
        BYTE i;
        BYTE UART_Data;

        UART_Data = 0x00;

// 增加超时处理: 未连接/未上电/通讯错误等

        while(UART_RX_BIT);        // Wait for start bit

        Delay_52us_NOP();
        Delay_26us_NOP();

        for(i = 0; i < 8; i++)
        {
                if(UART_RX_BIT)
                {
                        if(i < 8) UART_Data |= 0x80;
	        }
                if(i < 7) UART_Data >>= 1;

                Delay_52us_NOP();

        }

        UART_RX_BIT = 1;

        return UART_Data;
}

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

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];

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

⌨️ 快捷键说明

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