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

📄 m500auc.c

📁 ISO14443协议的实现
💻 C
📖 第 1 页 / 共 3 页
字号:
			if((timer0 - tt0) > (int)WI*960*DD)
                         {STATE_FLAG=1;return(STimeOut);}//time out
		}
		RI=0;
		ACC = SBUF;
		tt0 = timer0;
                if(T0_T1_FLAG) return(SBUF);
		if(RB8 != P)
		{       EA = 1;
			while((timer0 - tt0) < 10);//delay 10.5 etu
			tt = timer0;
			RXD = 0;
                        EA = 1;
			while(timer0 - tt < 1);	//1 ~ 2 etu
			RXD = 1;
		}
		else
			return(SBUF);

		if(i++ == 3)
			{RI = 0;STATE_FLAG=1;return(-1);}//time out
	}
//ET0 = 0;
}

uchar RstICAnswer(void)               //IC卡复位应答
	{
	uchar kk,i,temp;
        uchar length,TD,Bcc;
        uchar dd0,wi0;
        kk = 0;dd0 = 1;wi0 = 10;
        TC1 = 0;DD = 1;//WI = 10;
        WI = 100;
        forPowerdown = 0;
	if(ID==CPUtype)		//cpu card
		{
                if(ICPWR) {ICPWR = 0;
                    PowerDown = 0;
                    TR2 = 0;
                    delay_1ms(10);}
                ICRST=0;
		delay_1ms(11);//12 //11  // 40000/3579000 = 11ms
		ICRST=1;
		}
	else if(ID==SAM1type)			//sam card
		{
		SAMRST=0;
		delay_1ms(11);
		SAMRST=1;
		}


        tt0 = timer0 = 0;
        temp=RECVbyte0();
	if(STATE_FLAG)
                         return(temp);

	if(temp!=0x3b)
		{
		STATE_FLAG=1;return(temp);//0x88
		}

	toPpc(0,0x3b);

	Len=1;/*length*/

	temp=RECVbyte0();
	if(STATE_FLAG)
                         return(temp);
	toPpc(1,temp);
	Len++;

	length=temp&0x0f;
	Bcc=0;

	T0_T1_FLAG=0;

	TD=temp;
	while(++kk)
		{
		if((TD&0x10)==0x10)
			{
                        temp=RECVbyte0();
			if(STATE_FLAG)
                                         return(temp);
                        if(kk == 1)       //TA1
                        {
                                if(temp==1) dd0=1;
                                if(temp==2) dd0=2;
                                if(temp==3) dd0=4;
                                if(temp==4) dd0=8;
                                if(temp==5) dd0=16;
                        }
                        toPpc(Len,temp);/*TAi*/
			Bcc^=temp;
			Len++;}
		if((TD&0x20)==0x20)
			{
		        temp=RECVbyte0();
			if(STATE_FLAG)
                                         return(temp);
			toPpc(Len,temp);/*TBi*/
			Bcc^=temp;
			Len++;}
		if((TD&0x40)==0x40)
			{
                        temp=RECVbyte0();
			if(STATE_FLAG)
                                         return(temp);
                        if(kk == 1) TC1 = temp;//TC1
                        if(kk == 2) wi0 = temp;//TC2
			toPpc(Len,temp);/*TCi*/
			Bcc^=temp;
			Len++;}
		if((TD&0x80)==0x80)
			{
                        TD=RECVbyte0();
			if(STATE_FLAG)
                                         return(TD);
			Bcc^=TD;
			toPpc(Len,TD);

			if((TD&0x0f)==0x00) T0_T1_FLAG=0;
			if((TD&0x0f)==0x01) T0_T1_FLAG=1;

			Len++;
			if((TD&0xf0)==0x00) break;
			}
		else	break;
		}

		/*recv TK */
	for(i=Len;i<(Len+length);i++)
		{
		temp=RECVbyte0();
		if(STATE_FLAG)
                                 return(temp);
		Bcc^=temp;
		toPpc(i,temp);
		}

	CPU_block_num=0;//can't delete--2000.10.15
	if(ID==CPUtype)//cpu card
		{
		IC_REST_FLAG=1;
		CPU_T0_T1=T0_T1_FLAG;
		CPU_block=0;
		}
	else if(ID==SAM1type)		//sam card
		{
		SAM_REST_FLAG=1;
		SAM_T0_T1=T0_T1_FLAG;
		SAM_block=0;
		}

	Len+=length;
        DD = dd0;WI = wi0;
        delay_1ms(10);
	return 0;
	}
//======================================================================================
uchar trans_t0(void)//CPU_ICC:T=0
	{
	uchar i,tempch;
	uchar casef;//Rlen;


//ET0 = 0;
	led = 0;
	LcLe=fromPpc(4);

	if((Len==4)|(Len==5)) {casef=0x12;}
	else casef=0x34;

	if(Len==4) toPpc(4,0);

  for (i=0;i<5;i++)
	{
        tempch = fromPpc(i);
	status=SENDbyte0(tempch);
	if (STATE_FLAG) return status;
	}

  if(!TR2)
     if(forPowerdown)
           TR2 = 1;
  while(1)
  {
  sw1=RECVbyte0();
  if(STATE_FLAG)
  		{
		sw1=RECVbyte0();
		if(STATE_FLAG)	return(sw1);
		}
	if(sw1!=0x60) break;
	}

Rlen=0;
ins=fromPpc(1);

if(sw1==ins)
	{
	if(casef==0x34)
		{
		for (i=5;i<5+LcLe;i++)
			{
                        tempch = fromPpc(i);
			status=SENDbyte0(tempch);
			if (STATE_FLAG)
                        	 return(status);
			}
//		sw1=RECVbyte0();
//		if(STATE_FLAG)
//                 return(sw1);
		}
	else if(casef==0x12)
			{
			for(i=0;i<LcLe;i++)
				{
				tempch=RECVbyte0();
				if(STATE_FLAG)
                                 return(tempch);
				toPpc(Rlen,tempch);
				Rlen++;
				}
//			sw1=RECVbyte0();
//			if(STATE_FLAG)
//                         return(sw1);
			}
		sw1=RECVbyte0();
		if(STATE_FLAG)
                 return(sw1);
		}

  while(1)
	{
	while(sw1 == 0x60)
        	sw1=RECVbyte0();
	ins=fromPpc(1);
	if(sw1==ins)
		{
		for(i=0;i<LcLe;i++)
			{
			tempch=RECVbyte0();
			if(STATE_FLAG)
                         return(tempch);
			toPpc(Rlen,tempch);
			Rlen++;
			}
		sw1=RECVbyte0();
		if(STATE_FLAG)
                 return(sw1);
		continue;
		}
	sw2=RECVbyte0();
	if(STATE_FLAG)
         return(sw2);

////
/*
if((sw1==0x61)||(sw1==0x6c))
		{
		if(sw1==0x61)
			{
			toPpc(0,0);
			toPpc(1,0xc0);
			toPpc(2,0);
			toPpc(3,0);
			}
		toPpc(4,sw2);
		LcLe=sw2;
                tt0 = timer0 = 0;
		for(i=0;i<5;i++)
			{
			status=SENDbyte0(fromPpc(i));
			if(STATE_FLAG) return(status);
			}
		sw1=RECVbyte0();
		if(STATE_FLAG) return(sw1);
		continue;
		}
*/
////
//	if(((sw1&0xf0)!=0x90)&&((sw1&0xf0)!=0x60))
//		{STATE_FLAG=1;return(0x88);}/*sw1!=9x.6x---error*/
//	else
//		{
                toPpc(Rlen,sw1);Rlen++;toPpc(Rlen,sw2);Rlen++;
                break;
//                }/*sw1=9x.6x but !=61.6c*/
	}
        ET0 = 0;
  Len=Rlen;return 0;
  }



//==================================================================================
/*T=1通信协议*/
uchar trans_t1(void)
	{
	uchar Bcc,temp,i;
	led = 0;
	Bcc=NAD;/*BCC*/
	status=SENDbyte0(NAD);/*send NAD*/
	if(STATE_FLAG==0)
		{
		if((pcb&0x80)==0)//I_block
			{
			if(CPU_block_num) {pcb|=0x40;}
			else	pcb&=0xbf;
			}
		Bcc^=pcb;
		//CPU_block_num^=1;
		CPU_block_num = ~CPU_block_num;

		status=SENDbyte0(pcb);/*send PCB*/
		}

	if(!STATE_FLAG)
		{
			Bcc^=Len;
			status=SENDbyte0(Len);/*send Len*/
		}

	if(STATE_FLAG) return(status);

	for(i=0;i<Len;i++)
			{
			temp=fromPpc(i);
			status=SENDbyte0(temp);/*send INF*/
			if(STATE_FLAG==1) return(status);
			Bcc^=temp;
			}

	status=SENDbyte0(Bcc);/*send BCC*/
		if(STATE_FLAG==1) return(status);



/*接收响应*/
	NAD=RECVbyte0();/*recv NAD*/
		if(STATE_FLAG==1) return(NAD);
		//if(status!=(swapchar(ct1))) {STATE_FLAG=1;return(0x88);}/*NAD error*/

	Bcc=NAD;/*BCC*/

	pcb=RECVbyte0();/*recv PCB*/
		if(STATE_FLAG==1) return(pcb);
	Bcc^=pcb;

	Len=RECVbyte0();/*recv Len*/
		if(STATE_FLAG==1) return(Len);
	Bcc^=Len;

	temp=0;
	if(pcb&0x80!=0)//is R_block or S_block(not I_block) --  save NAD.PCB.Len
		{
		toPpc(0,NAD);//save 0
		toPpc(1,pcb);//save PCB
		toPpc(2,Len);//save Len
		temp=3;
		command=SNotIblock;//retrun not I_block
		}

	Len+=temp;//adjust

	for(i=0;i<Len-temp;i++)//RECV INF
		{
		status=RECVbyte0();
			if(STATE_FLAG==1) return(status);
		Bcc^=status;
		toPpc(temp+i,status);//save INF
		}

	status=RECVbyte0();//recv Bcc
		if(STATE_FLAG==1) return(status);

	toPpc(Len,status);//save Bcc

	//if(status!=Bcc) {STATE_FLAG=1;return(status);}

        ET0 = 0;
	return 0;

	}
//==================================================================================

#pragma noaregs



void delay_50us(unsigned char _50us)
{
	while(_50us--)
	{
	  _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_();_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_();

	 }
}
void delay_1ms(uchar _1ms)
{

#ifndef NO_TIMER2

  RCAP2LH = RCAP2_1ms;
  T2LH    = RCAP2_1ms;

  TR2 = TRUE;
  while (_1ms--)
  {
	while (!TF2);
	TF2 = FALSE;
  }
  TR2 = FALSE;

#else

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

#endif
}

/*"int1(IC_SW)中断服务函

⌨️ 快捷键说明

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