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

📄 rfid_u2270.c

📁 找的一个用U2270B开发的射频卡项目文件
💻 C
📖 第 1 页 / 共 3 页
字号:
	
	level = (PIND & 0x10);		// Check logic level of data line
	level = (PIND & 0x10);          // Redundancy check
	
	//  Make sure edge time is a Manchester long time  //
	if(RdTime > U2270_LongL && RdTime < U2270_LongH && level == 0 )
	{
		PORTF &= ~0x02;
        Timer_delay_us(2);			
		PORTF |= 0x02;
		return 1;		// Logic value of current bit
	} 
	else return SeqTermErr;		// Edge time and level error
}

// *******************************************************************************
// Routine to decode a Manchester bit 
// Pass in the previous bit logic value 
// ******************************************************************************/
unsigned char U2270_ManchesterDecode(unsigned char cBit)
{
	RFIDTimerFlag = 0;							// Clear Timer Flag					 
	while(!RFIDTimerFlag); 						// Wait for next edge
	
	//  Check edge time and determine next Logic value  //
	if(RdTime > U2270_LongL && RdTime < U2270_LongH && cBit == 1){return 0;}
	else if(RdTime > U2270_LongL && RdTime < U2270_LongH && cBit == 0){return 1;}
	else if(RdTime > U2270_ShortL && RdTime < U2270_ShortH) 	// Next edge time is short
	{
		RFIDTimerFlag = 0;						// Clear Timer flag 
		while(!RFIDTimerFlag); 					// Wait for next edge
		if(RdTime > U2270_ShortL && RdTime < U2270_ShortH && cBit == 1){return 1;}
		else if(RdTime > U2270_ShortL && RdTime < U2270_ShortH && cBit == 0){return 0;}
		else 
		{
				//PORTF &= ~0x02;			
				//PORTF |= 0x02;
                return BitErr;      // Un-paired short time between two long times 
		}					
	}
	else {return BitErr;}						// Edge time outside limits
}

// *******************************************************************************
// Routine to decode a BiPhase bit 
// ******************************************************************************/
unsigned char U2270_BiPhaseDecode(void)
{
	RFIDTimerFlag = 0;							// Clear Timer Flag					 
	while(!RFIDTimerFlag); 						// Wait for next edge
	
	//  Check edge time and determine next Logic value  //
	if(RdTime > U2270_LongL && RdTime < U2270_LongH){return 1;}
	else if(RdTime > U2270_ShortL && RdTime < U2270_ShortH) 	// Next edge time is short
	{
		RFIDTimerFlag = 0;						// Clear Timer flag 
		while(!RFIDTimerFlag); 					// Wait for next edge
		if(RdTime > U2270_ShortL && RdTime < U2270_ShortH){return 0;}
		else 
		{
                return BitErr;  // Un-paired short time between two long times 
		}					
	}
	else {return BitErr;}						// Edge time outside limits
}

// *******************************************************************************
// Routine to find Decode the Dual Pattern Bit
// ******************************************************************************/
unsigned char U2270_DualPatternDecode(void)
{
	RFIDTimerFlag = 0;							// Clear Timer Flag					 
	while(!RFIDTimerFlag); 						// Wait for next edge
	
	//  Check edge time and determine next Logic value  //
	if(RdTime > U2270_ShortL-30 && RdTime < U2270_ShortH+20)
	{
		RFIDTimerFlag = 0;						// Clear Timer flag 
		while(!RFIDTimerFlag); 					// Wait for next edge
		if(RdTime > U2270_ShortL-20 && RdTime < U2270_ShortH+20){return 1;}
		else {return BitErr;}					// Un-paired short time between two long times 
	}
	else if(RdTime > U2270_LongL && RdTime < U2270_LongH+30)
	{
		RFIDTimerFlag = 0;						// Clear Timer flag 
		while(!RFIDTimerFlag); 					// Wait for next edge
		return 0;
	}
	else {return BitErr;}						// Edge time outside limits
}

// *******************************************************************************
// Routine to find the Manchester violation from 555x terminator 
// ******************************************************************************/
unsigned char U2270_Check555xSequenceTerm(void)
{
	RFIDTimerFlag = 0;						// Clear Timer Flag
	while(!RFIDTimerFlag); 					// Wait for next edge 
	
	//Wait for next edge that falls in the Term time (t1) //
	if(RdTime > (U2270_TermL-20) && RdTime < U2270_TermH) 
	{
		PORTF &= ~0x02;
		RFIDTimerFlag = 0;                 		// Clear Timer Flag
		while(!RFIDTimerFlag); 				// Wait for next edge 
		
		//  Make sure next edge is a short time (t2)  //                                                     
		if(RdTime > U2270_ShortL && RdTime < U2270_LongL+20) 
		{
			PORTF |= 0x02;
			RFIDTimerFlag = 0;                 	// Clear Timer Flag
			while(!RFIDTimerFlag); 			// Wait for next edge
	
			//  Terminator found, Return bit logic value  //
			if(RdTime > (U2270_LongH) && RdTime < U2270_TermH)
            {
			    PORTF &= ~0x02;			
			    PORTF |= 0x02;
                return 1;
            }
			else if(RdTime < U2270_LongH && RdTime > U2270_LongL)
			{
				PORTF &= ~0x02;						
				//  Bit logic value is zero, Synchronize to mid-bit edge(t4)  //
				RFIDTimerFlag = 0;		// Clear Timer Flag 
				while(!RFIDTimerFlag); 	// Wait for next edge
				PORTF |= 0x02;
                return 0;
			}
			else return SeqTermErr;			// Edge Time does not fit limit
		}
		else return SeqTermErr;                         // Edge Time does not fit limit
	}
	else return SeqTermErr;                                 // Edge Time does not fit limit
} 

// *******************************************************************************
// Routine to find the 5558 SOF 
// ******************************************************************************/
unsigned char U2270_x5558SOFSync(unsigned char nEdgeCount)
{
	unsigned char cError=2, edgeCount=0,i=0;
	
    while(i<20 && edgeCount<nEdgeCount)
    {
        RFIDTimerFlag = 0;                 // Clear Timer Flag
    	while(!RFIDTimerFlag); 		// Wait for next edge 	
    	if(RdTime > U2270_ShortL && RdTime < U2270_ShortH){edgeCount++;}// Make sure next edge time is short //
        else {edgeCount=0;}
        i++;				
    }
    if(edgeCount==nEdgeCount)
    {
        i=0;
        while(i<20 && cError==2)
        {
            RFIDTimerFlag = 0;                 // Clear Timer Flag
        	while(!RFIDTimerFlag); 		// Wait for next edge 	
            
            if(RdTime > U2270_LongH-50 && RdTime < (U2270_LongH+(U2270_LongH>>1)))
        	{
        		PORTF &= ~0x02;
        		RFIDTimerFlag = 0;		// Clear Timer Flag
        		while(!RFIDTimerFlag); 	// Wait for next edge
				 
        		// Make sure next edge time is short //
        		if(RdTime > U2270_ShortL && RdTime < U2270_ShortH)
        		{
        				cError = 1;		//SOF Found, return success 
                    	PORTF |= 0x02;
        		}
        		else {cError = SeqTermErr;}         // SOF not found, return error code
        	}
            i++;
        }
    }
    return cError;
}        

// *******************************************************************************
// Routine to find the 5557 synchronization pattern
// Pass in the read data buffer pointer and the number of bits needed 
// ******************************************************************************/
unsigned char U2270_Check5557SequenceTerm(unsigned char *inDataPtr, unsigned int numBits)
{
	unsigned char tmp1, tmp2;
	unsigned char tmp3, tmp4;
	unsigned char i, cBits=0, cBytes=0;
	unsigned char *dataPtr;
	unsigned int k, j;
	
	j = numBits;				// Store number of bits needed
	j = j>>3;                               // Convert to number of bytes
	dataPtr = inDataPtr;			// Store beginning pointer in temp pointer	
	
	//  Loop until 2x number of bits have been checked  //
	for(k=0; k<(numBits*2); k++)
	{
		tmp1 = *(inDataPtr+cBytes);		// Store first byte to be checked
		tmp2 = *(inDataPtr+cBytes+j);		// Store the byte that is exactly 2x first byte
		tmp2 = tmp2<<2;
		tmp2 = tmp2 | (*(inDataPtr+cBytes+j+1)>>6); // Shift and take first two from next bit to account for synch pattern
		for(cBits=0; cBits<8;)                  // Check all bits in the byte for patten match
		{
			tmp3 = tmp1 & 0x80;		// Look at the MSB of byte in question
			tmp4 = tmp2 & 0x80;             // these bytes are separated by exactly the number of bits needed
			if(tmp3 == tmp4)		// If both MSB are the same, Pattern not possible
			{
				tmp1 = tmp1<<1;		// Shift byte in question to put next bit
				tmp2 = tmp2<<1;         // in the MSB position
				cBits++;                // Increment number of bits checked
			}
			else				// First opposite bits found, Pattern possible
			{
				if(cBits == 7)
				{
					cBytes++;
					cBits = 0;
					tmp1 = *(inDataPtr+cBytes);		// Store first byte to be checked
					tmp2 = *(inDataPtr+cBytes+j);		// Store the byte that is exactly 2x first byte
					tmp2 = tmp2<<2;
					tmp2 = tmp2 | (*(inDataPtr+cBytes+j+1)>>6); // Shift and take first two from next bit to account for synch pattern
				}
				else
				{
					tmp1 = tmp1<<1;		// Shift byte in question to put next bit
					tmp2 = tmp2<<1;         // in the MSB position
					cBits++;         	// Increment number of bits checked
				}
				tmp3 = tmp1 & 0x80;	// Look at the MSB of byte in question
				tmp4 = tmp2 & 0x80;     // these bytes are separated by exactly the number
				if(tmp3 == tmp4){;}     // Pattern not possible, bits already shifted 
				else 			// Found Pattern match
				{
					cBits++;        // Increment bits checked
					
					//  Reload data with beginning of data located at the inital pointer location  //
					for(i=0; i<j; i++)
					{
						tmp1 = inDataPtr[cBytes+i]<<cBits;		// Shift bytes to start of data
						tmp1 = tmp1 | inDataPtr[cBytes+i+1]>>(8-cBits); // found by previous search
						inDataPtr[i] = tmp1;				// Store back at beginning pointer location
					}
					k = (numBits*2)+1;					// break search loop
					cBits = 8;						// break search loop
					return 0;						// return successful 
				}
			}
		}
		cBytes++;				// Increment bytes checked
	}
	return SeqTermErr; 				// Pattern not found, Return error code 
}       

// *******************************************************************************
// Routine to find the Atmel header pattern in a string of data
// Pass in the read data buffer pointernumber of bits needed, and the header 
// ******************************************************************************/
unsigned char U2270_HeaderSearch(unsigned char *inDataPtr, unsigned int numBits, unsigned char cHeader)
{
	unsigned char tmp1, tmp2;
	unsigned char i, cBits=0, cBytes=0;
	unsigned char *dataPtr;
	unsigned int k, j;
	
	j = numBits;				// Store number of bits needed
	j = j>>3;                               // Convert to number of bytes
	dataPtr = inDataPtr;			// Store beginning pointer in temp pointer	
	
	//  Loop until 2x number of bits have been checked  //
	for(k=0; k<(numBits*2); k++)
	{
		tmp1 = inDataPtr[cBytes];      	// Store first byte to be checked
		tmp2 = inDataPtr[cBytes+1];    	// Store the next byte that will be shifted into first
		
		for(cBits=0; cBits<7;)         	// Check all bits in the byte for header match
		{
			if(tmp1 == cHeader)	// Found matching Header
			{
				//  Reload data with beginning of data located at the inital pointer location  //
				for(i=0; i<j; i++)
				{
					tmp1 = inDataPtr[cBytes+i]<<cBits;               // Shift bytes to start of data
					tmp1 = tmp1 | inDataPtr[cBytes+i+1]>>(8-cBits);  // found by previous search
					inDataPtr[i] = tmp1;                             // Store back at beginning pointer location
				}
				
				//  Load remaining buffer with all zeros  //
				for(i=(unsigned char)(j); i< (unsigned char)((j+1)*2); i++)
				{
					inDataPtr[i] = 0x00;
				}
				k = (numBits*2)+1;					// break search loop
				cBits = 8;                                              // break search loop
				return 0;                                               // return successful
			}
			else								// Header not found						
			{
				tmp1 = tmp1<<1;                				// Shift first byte right
				if(tmp2&0x80){tmp1 = tmp1|0x01;}                        // Move MSB of second byte to LSB of first
				tmp2 = tmp2<<1;                                         // Shift second byte right
				cBits++;                                                // Increment bits checked
			}
		}
		cBytes++;			// Increment bytes checked
	}
	return SeqTermErr;			// Pattern not found, Return error code  
}
  
// *******************************************************************************
// Routine to find the ISO 11784/785 header pattern in a string of data
// Pass in the read data buffer pointernumber of bits needed
// ******************************************************************************/
unsigned char U2270_AnimalIDHeaderSearch(unsigned char *inDataPtr, unsigned int numBits)
{
	unsigned char tmp1, tmp2;
	unsigned char i, cBits=0, cBytes=0;
	unsigned char *dataPtr;
	unsigned int k, j;
	

⌨️ 快捷键说明

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