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

📄 msr.c

📁 非接触式M1卡读写器,读写源程序,可读S50/S70的卡
💻 C
📖 第 1 页 / 共 3 页
字号:


	case 0x52:                  // Config

	  mifs_config(SerBuffer[MODE], SerBuffer[BAUD]);   ///MODE=3;BAUD IS DEFINE AS 4

#ifdef AUTODELAY
	  DelayRate = 0;
#endif
	  Status = OK;
	  break;


	case 0x53:                  // Check Write

	  Status = MIS_CHK_FAILED;

	  if (!(SerBuffer[SERNR]&0x0f))   //authentication necessary
	  {

		if (mifs_request(ALL, tt) != OK)
		{
			if (mifs_request(ALL, tt) != OK)
			{
				break;
			}
		}
		if (mifs_select(&SerBuffer[SERNR], &size) != OK)
		{
			if (mifs_request(ALL, tt) != OK)
			{
				break;
			}
			if (mifs_select(&SerBuffer[SERNR], &size) != OK)
			{
				break;
			}
		}
		if (mifs_authentication(SerBuffer[AUTHMODE],
							  SerBuffer[ADRCHKWR] >> 2) != OK)
		{
			break;
		}
	  }

	  if (mifs_read(SerBuffer[ADRCHKWR], tmp) != OK)
	  {
		break;
	  }

	  if (memcmp(&SerBuffer[DATACHKWR], tmp, 16) != OK)
	  {
		Status = MIS_CHK_COMPERR;
	  }
	  else
	  {
		Status = MIS_CHK_OK;
	  }
	  break;


	case 0x54:                  // Set Port

	  adr = SerBuffer[PORTBYTE];     ///PORTBYTE DEFINE AS 3

	  IN_OUT_0 = adr & 0x01;         ///IN_OUT_0 =P3^5;
	  IN_OUT_1 = adr & 0x02;         ///IN_OUT_1= P1^5;
	  IN_OUT_2 = adr & 0x04;         ///IN_OUT_2=P1^6
	  IN_OUT_3 = adr & 0x08;         ///IN_OUT_3= P1^7;
	  OUT_4    = adr & 0x10;         ///OUT_4 = P3^4;

	  Status = OK;
	  break;


	case 0x55:                  // Get Port

	  SerBuffer[PORTBYTE] = (uchar)IN_OUT_0      |
							(uchar)IN_OUT_1 << 1 |
							(uchar)IN_OUT_2 << 2 |
							(uchar)IN_OUT_3 << 3;
	  SerBuffer[LENGTH] = 1;

	  Status = OK;
	  break;


	case 0x56:                  // Authentication with additional trailer address
	  Status = mifs_authentication_2(SerBuffer[MODE], SerBuffer[SECNR], SerBuffer[AUTHADD]);
	  break;


	case 0x57:                  // Decrement+Transfer

	  Status = mifs_decrement_transfer(SerBuffer[ADR], &SerBuffer[VALUE]);
	  break;


	default:

	  Status = W_CMD_NY_IMPL;
	  break;


  } // end switch (Command)

  SerBuffer[STATUS] = Status;

  if (cmd < 0x48 || cmd > 0x4A)
  {
	EnableTransferCmd = FALSE;
  }
  else
  {
	EnableTransferCmd = TRUE;
  }
}


/****************************************************************************
*                                                                           *
* Function:     auto_baud                                                   *
*                                                                           *
* Input:        -                                                           *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
*   use timer0 to judge the exactly baudrate ,if baudrate out of range      *
*   ,return but do not set autobaud to false for next judge                 *
*                                                                           *
****************************************************************************/

void auto_baud(void)
{
  if (AutoBaud && !RXD)
  {
	TR0 = TRUE;

	do
	{
	  if (RXD)
	  {
		Capt_L = TL0;
		Capt_H = TH0;
 
		do
		{
		  if (!RXD)
		  {
			break;
		  }
		} while (!TF0);
	  }
	} while (!TF0);

	TR0 = FALSE;
	TF0 = FALSE;

	TOGGLE_WD();

	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) ////+1 new add
	{
	  TH0    = 0;
	  TL0    = 0;
	  Capt_L = 0;
	  Capt_H = 0;
	  return;
	}

	TOGGLE_WD();

	TMOD = 0x21;                    // Timer 0, mode 1, 16-bit counter
	RCAP2LH = BaudRateTable[BaudRate]; 
	T2LH = RCAP2LH;
	TR2  = TRUE;

	TH0  = 0xF0;
	TL0  = 0x28;
	TR0  = TRUE;
	while (!TF0);                   // Wait 4.4 ms
	TR0  = FALSE;
	TF0  = FALSE;
	ET0  = TRUE;

	AutoBaud = FALSE;

	SBUF = NAK;						//#define  NAK  0x15 // Not acknowledge
	while (!TI);
	TI   = FALSE;
	RI   = FALSE;
	ES   = TRUE;
  }

}

#pragma noaregs

/****************************************************************************
*                                                                           *
* Function:     delay_50us                                                  *
*                                                                           *
* Input:        _50us                                                       *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
*use common to get delay                                                    *
*                                                                           *
****************************************************************************/

void delay_50us(uchar _50us)
{

  while (_50us--)
  {
	DELAY_us(50);
  }
}


/****************************************************************************
*                                                                           *
* Function:     delay_1ms                                                   *
*                                                                           *
* Input:        _1ms                                                        *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* use common function to get delay time.                                    *
*                                                                           *
****************************************************************************/

void delay_1ms(uchar _1ms)
{
  TOGGLE_WD();

  while (_1ms--)
  {
	delay_50us(20);
  }

  TOGGLE_WD();
}


/****************************************************************************
*                                                                           *
* Function:     delay_10ms                                                  *
*                                                                           *
* Input:        _10ms                                                       *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* use basic function to get delay.    *
*                                                                           *
****************************************************************************/

void delay_10ms(uint _10ms)
{
  TOGGLE_WD();

  while (_10ms--)
  {
	delay_50us(19);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(20);
	if (CmdValid || CmdReceived) return;
	delay_50us(19);
	if (CmdValid || CmdReceived) return;

	TOGGLE_WD();
  }
}


#ifdef NOP_DELAY

/****************************************************************************
*                                                                           *
* Function:     delay_50us_NOP                                              *
*                                                                           *
* Input:        -                                                           *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* use nop to get exactly time delay                                         *
*                                                                           *
****************************************************************************/

void delay_50us_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_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
  _nop_();_nop_();_nop_();_nop_();  
}

#endif


/****************************************************************************
*                                                                           *
* Function:     delay_8us_NOP                                               *
*                                                                           *
* Input:        -                                                           *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* use nop to get exactly delay                                              *
*                                                                           *
****************************************************************************/

void delay_8us_NOP(void)
{
  _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
  
}

#pragma aregs


/****************************************************************************
*                                                                           *
* Function:     xtoa_h                                                      *
*                                                                           *
* Input:        _byte                                                       *
* Output:       ASCII High-Nibble                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* transfer higher of byte to ascii    *
*                                                                           *
****************************************************************************/

uchar xtoa_h(uchar _byte)
{
  uchar nibble = _byte >> 4;

  return ((nibble > 9)? nibble + 'A' - 10 : nibble + '0');
}


/****************************************************************************
*                                                                           *
* Function:     xtoa_l                                                      *
*                                                                           *
* Input:        _byte                                                       *
* Output:       ASCII Low-Nibble                                            *
*                                                                           *
* Description:                                                              *
*                                                                           *
*  transfer lower part to ascii     *
*                                                                           *
****************************************************************************/

uchar xtoa_l(uchar _byte)
{
  uchar nibble = _byte & 0x0F;

  return ((nibble > 9)? nibble + 'A' - 10 : nibble + '0');
}


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

⌨️ 快捷键说明

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