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

📄 rfid_u2270.c

📁 找的一个用U2270B开发的射频卡项目文件
💻 C
📖 第 1 页 / 共 3 页
字号:
// $Id: rfid_U2270.c,v 1.9 2007/01/24 21:44:15 tprescott Exp $
/*************************************************************
Project : rfid_U2270.c
Date    : 9/08/2006
Author  : Toby Prescott                   
Company : Atmel                           
Comments: AVR Studio GCC

Revisions: 
	v1.0 - Started            
	v2.1 - 1/19/06 Added 5558 attempt write + changed from ASCII
	v2.2 - 3/8/06 Changed Read routine
	v2.3 - 3/14/06 Fixing 5558 read/write routines
	       3/15/06 Adding 5530 Support + Header Sync
	v2.4 - 4/6/06  Add new working 5558 support
	       5/19/06 Fixed mfg. ID error
	v2.5 - 6/20/06 Add Animal ID Read routine
	v2.6 - 9/19/06 Clean for WinAVR
*************************************************************/

#include "rfid_U2270.h"

// Default RFID Reader Options stored in EEPROM
unsigned char RFfield_E EEMEM = 0;
unsigned char OutputEN_E EEMEM = 1;
unsigned char Standby_E EEMEM = 0;
unsigned char AntTune_E EEMEM = 3;

unsigned char cDataBuff[129] = {0};		// Read Data buffer

//  Runtime values for Reader Write Timings  //
unsigned int U2270_SGap = 300;
unsigned int U2270_Gap = 276;
unsigned int U2270_Zero = 80;
unsigned int U2270_One = 324;  

//  Runtime values for Reader Read Timings  //
unsigned int U2270_ShortL = 50;
unsigned int U2270_ShortH = 200;
unsigned int U2270_LongL = 200;  
unsigned int U2270_LongH = 330;  
unsigned int U2270_TermL = 330;
unsigned int U2270_TermH = 450;

// *******************************************************************************
// Initialization routine for RFID Reader
// Loads last stored values from EEPROM
// ******************************************************************************/
void U2270_InitReader(void)
{
 	U2270_RFfield(eeprom_read_byte(&RFfield_E)); 	
 	U2270_Standby(eeprom_read_byte(&Standby_E));
 	U2270_OutputEn(eeprom_read_byte(&OutputEN_E));
 	U2270_AntTune(eeprom_read_byte(&AntTune_E));
} 

// *******************************************************************************
// Initialization of Runtime Timings for RFID Reader
// Recalls last stored values from EEPROM
// Dependent on Tag type selected     
// ******************************************************************************/
void U2270_InitTimings(unsigned char Target_Tag)
{
 	// x5551 timings
 	if(Target_Tag == 51)
	{
	        U2270_SGap = x5551_Get_SGap();
	        U2270_Gap = x5551_Get_Gap();
	        U2270_Zero = x5551_Get_Zero();
	        U2270_One = x5551_Get_One();
	        U2270_ShortL = x5551_Get_ShortL();
	        U2270_ShortH = x5551_Get_ShortH();
	        U2270_LongL = x5551_Get_LongL();		
	        U2270_LongH = x5551_Get_LongH();		
	        U2270_TermL = x5551_Get_TerminatorL();
	        U2270_TermH = x5551_Get_TerminatorH();
	} 
 	// x5557 timings
	else if(Target_Tag == 57)
	{
	        U2270_SGap = x5557_Get_SGap();
	        U2270_Gap = x5557_Get_Gap();
	        U2270_Zero = x5557_Get_Zero();
	        U2270_One = x5557_Get_One();
	        U2270_ShortL = x5557_Get_ShortL();
	        U2270_ShortH = x5557_Get_ShortH();
	        U2270_LongL = x5557_Get_LongL();		
	        U2270_LongH = x5557_Get_LongH();		
 	}
 	// x5567 timings
	else if(Target_Tag == 67)
	{
	        U2270_SGap = x5567_Get_SGap();
	        U2270_Gap = x5567_Get_Gap();
	        U2270_Zero = x5567_Get_Zero();
	        U2270_One = x5567_Get_One();
	        U2270_ShortL = x5567_Get_ShortL();
	        U2270_ShortH = x5567_Get_ShortH();
	        U2270_LongL = x5567_Get_LongL();		
	        U2270_LongH = x5567_Get_LongH();		
 	}
 	// x5570 timings
 	else if(Target_Tag == 70)
	{
	        U2270_SGap = x5570_Get_SGap();
	        U2270_Gap = x5570_Get_Gap();
	        U2270_Zero = x5570_Get_Zero();
	        U2270_One = x5570_Get_One();
	        U2270_ShortL = x5570_Get_ShortL();
	        U2270_ShortH = x5570_Get_ShortH();
	        U2270_LongL = x5570_Get_LongL();		
	        U2270_LongH = x5570_Get_LongH();		
 	}
 	// x5558 timings
 	else if(Target_Tag == 58)
	{
	        U2270_SGap = x5558_Get_SGap();
	        U2270_Gap = x5558_Get_Gap();
	        U2270_Zero = x5558_Get_Ref();
	        U2270_ShortL = x5558_Get_ShortL();
	        U2270_ShortH = x5558_Get_ShortH();
	        U2270_LongL = x5558_Get_LongL();		
	        U2270_LongH = x5558_Get_LongH();		
 	}
}

// *******************************************************************************
// Control routine for RF field ON/OFF (1/0)
// ******************************************************************************/
void U2270_RFfield(unsigned char value)
{
	if(value == ON){PORTD |= CFE;}  		// Change Pin state to activate Field
	else if(value == OFF){PORTD &= ~CFE;}	// Change Pin state to de-activate Field
	eeprom_write_byte(&RFfield_E,value);		// Store status of field
}

// *******************************************************************************
// Control routine for Demodulated Data Output ON/OFF (1/0)
// ******************************************************************************/
void U2270_OutputEn(unsigned char value)
{
	if(value == ON){PORTD &= ~OE;}		// Change Pin state to activate output
	else if(value == OFF){PORTD |= OE;}	// Change Pin state to de-activate output
	eeprom_write_byte(&OutputEN_E,value);	// Store status of output enable
}

// *******************************************************************************
// Control routine for Standby Mode ON/OFF (1/0)
// ******************************************************************************/
void U2270_Standby(unsigned char value)
{
	if(value == ON)
	{
		PORTD &= ~STANDBY;		// Change Pin state to activate Standby mode
		PORTD &= ~CFE;			// Change Pin state to de-activate RF Field in Standby
	}
	else if(value == OFF)
	{
		PORTD |= STANDBY;		// Change Pin state to de-activate Standby mode
		U2270_RFfield(eeprom_read_byte(&RFfield_E));	// Recall the stored state of the RF Field
	}
	eeprom_write_byte(&Standby_E,value);
}   

// *******************************************************************************
// Control routine for Antenna Tune stages
// Stages:
//	High
//	Semi-High
//	Semi-Low
//	Low
// ******************************************************************************/
void U2270_AntTune(unsigned char value)
{
	// Change state of I/O pins appropriately
	if(value == Hi){PORTF &= ~(TUNE1 | TUNE2);}
	else if(value == SHi)
	{
		PORTF |= TUNE1; 
		PORTF &= ~TUNE2;
	 }
	else if(value == SLo)
	{
		PORTF &= ~TUNE1; 
		PORTF |= TUNE2;
	}
	else if(value == Lo){PORTF |= (TUNE1 | TUNE2);}
	eeprom_write_byte(&AntTune_E,value);	// Store status of Tuning Stage
}

//  Return the status for the Reader configuration  //
unsigned char U2270_RFfield_Status(void){return eeprom_read_byte(&RFfield_E);}
unsigned char U2270_OutputEn_Status(void){return eeprom_read_byte(&OutputEN_E);}
unsigned char U2270_Standby_Status(void){return eeprom_read_byte(&Standby_E);}
unsigned char U2270_AntTune_Status(void){return eeprom_read_byte(&AntTune_E);}

// *******************************************************************************
// Standard Block Write Routine Using the U2270 
// Pass in the number of bits to be sent and the data buffer
// ******************************************************************************/
void U2270_PPCWriteBlock(unsigned char cNumBits, unsigned char* cWDataBuff)
{
	unsigned char i,j=0;
	unsigned char tmpDat;
		
	RFIDTimer_Set((unsigned char)(U2270_SGap>>8), (unsigned char)U2270_SGap);	// Load Start Gap Time
	RFIDTimer_WR_Init(); 								// Initialize the Write timer
	PORTD &= ~CFE;   								// Turn CFE off
	
	RFIDTimerFlag = 0;									// Clear timer flag
	while(!RFIDTimerFlag);                                                             // Wait for next edge
	tmpDat = *cWDataBuff++;								// Load in first byte from buffer 
	
	// Loop unit all bits have been sent
	for(i = 0; i < cNumBits; i++)
	{
		if(j == 8)								// Check for all bits in byte
		{         	
			tmpDat = *cWDataBuff++;						// Load new byte
			j=0;                                                            // Clear bit counter
		}
		PORTD |= CFE;   							// Turn CFE on
		RFIDTimerFlag = 0;
		if((tmpDat & 0x80) == 0)						// Check bit value for Zero
		{
			RFIDTimer_Set((unsigned char)(U2270_Zero>>8), (unsigned char)U2270_Zero);	// Send Zero
			while(!RFIDTimerFlag);
		}
		else 
		{
			RFIDTimer_Set((unsigned char)(U2270_One>>8), (unsigned char)U2270_One);		// Send One
			while(!RFIDTimerFlag);
		}
		PORTD &= ~CFE;   							// Turn CFE off
		RFIDTimer_Set((unsigned char)(U2270_Gap>>8), (unsigned char)U2270_Gap);	// Send Gap
		RFIDTimerFlag = 0;
		while(!RFIDTimerFlag);
		tmpDat = tmpDat<<1;							// Shift byte to load next bit
		j++;   									// Increment bit counter
	}	
	PORTD |= CFE;   								// Turn CFE on
	RFIDTimer_Stop();									// Stop the Write Timer   							
}

// *******************************************************************************
// 1of4 Block Write Routine Using the U2270 (x5558)
// Pass in the number of bits to be sent and the data buffer
// ******************************************************************************/
void U2270_QuadWriteBlock(unsigned char cNumBits, unsigned char* cWDataBuff)
{
	unsigned char tmpDat;
	unsigned char i, j=0;
		
	RFIDTimer_Set((unsigned char)(U2270_SGap>>8), (unsigned char)U2270_SGap);	// Start Gap
	RFIDTimer_WR_Init(); 								// Start the Write Timer
	PORTD &= ~CFE;   								// Turn CFE off

	RFIDTimerFlag = 0; 								// Clear timer flag
	while(!RFIDTimerFlag);                                                             // Wait for next edge
	tmpDat = *cWDataBuff++;								// Load starting byte from buffer
	
	// Loop until all bits have been sent		
	for(i = 0; i < (cNumBits>>1); i++)   	// Divide number of bits by 2 for loop
	{
		if(j == 8)								// Check for all bits in byte 
		{
			tmpDat = *cWDataBuff++;						// Load next byte
			j=0;                                                            // Reset bit counter
		}
		PORTD |= CFE;   							// Turn CFE on
		RFIDTimerFlag = 0;								// Clear timer flag
											// ALL timings are based off a Ref time stored in U2270_Zero
		if((tmpDat & 0x80) == 0)						// Check first bit of 2 bit symbol for Zero
		{
			tmpDat = tmpDat<<1; 						// Shift byte to read next bit						
			if((tmpDat & 0x80) == 0) 					// Check second bit for Zero
			{
				RFIDTimer_Set((unsigned char)(U2270_Zero>>8), (unsigned char)U2270_Zero); 		   	// Send ZeroZero
				while(!RFIDTimerFlag);
			}
			else 
			{
				RFIDTimer_Set((unsigned char)((U2270_Zero+(8*16))>>8), (unsigned char)(U2270_Zero+(8*16))); 	// Send ZeroOne	
				while(!RFIDTimerFlag);
			}
		}
		else     								// If first bit of 2 bit symbol is One
		{
			tmpDat = tmpDat<<1;						// Shift byte to read next bit	
			if((tmpDat & 0x80) == 0)					// Check second bit for Zero
			{
				RFIDTimer_Set((unsigned char)((U2270_Zero+(8*32))>>8), (unsigned char)(U2270_Zero+(8*32)));  	// Send OneZero	
				while(!RFIDTimerFlag);
			}
			else
			{
				RFIDTimer_Set((unsigned char)((U2270_Zero+(8*48))>>8), (unsigned char)(U2270_Zero+(8*48)));	// Send OneOne
				while(!RFIDTimerFlag);
			}
		}
		tmpDat = tmpDat<<1;							// Shift byte to load next two bit symbol
		j+=2;									// Increment bit Counter
		PORTD &= ~CFE;   							// Turn CFE off
		RFIDTimer_Set((unsigned char)(U2270_Gap>>8), (unsigned char)U2270_Gap);	// Start Gap
		RFIDTimerFlag = 0;								// Clear timer flag
		while(!RFIDTimerFlag);                                                     // Wait for next edge
	}	
	PORTD |= CFE;   								// Turn CFE on
	RFIDTimer_Stop();   							        // Stop Write Timer
}

// *******************************************************************************
// Routine to synchronize bit periods in a Manchester encoded stream 
// Always sync to rising edge
// ******************************************************************************/
unsigned char U2270_Manchester2TSync(void)
{
	unsigned char level;
	RFIDTimerFlag = 0;			// Clear Timer Flag
	while(!RFIDTimerFlag); 		// Wait for next edge 

⌨️ 快捷键说明

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