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

📄 trh031v1.c

📁 This is a source of 13.560MHz RFID card reader for TRH031M as ATMEGA8-16AU MPU. The title is 3Alogi
💻 C
📖 第 1 页 / 共 4 页
字号:
// *********************************************** 
// Define Name Veriable
// *********************************************** 
#define TRH031MINI     0x0A
#define RESTARTING		 0x0B
#define ICRF_ON			   0x0C
#define ICRF_OFF       0x0D
#define START          0x0E

// *********************************************** 
// 13.56MHz ISO 14443, 15693 reference
// ISO 14443 A Type TRUE
// *********************************************** 
#define REQA           0x04
#define ANTICOLL       0x05
#define SELECT         0x06
#define HLTA           0x07
#define WUPA           0x08

// ISO 14443 A Type FALSE
#define FALSE_REQA     0xA4
#define FALSE_ANTICOLL 0xA5
#define FALSE_SELECT   0xA6
#define FALSE_HLTA     0xA7
#define FALSE_WUPA     0xA8

// ISO 14443 B Type TRUE
#define REQB           0x14
#define ATTRIB         0x15
#define HLTB           0x16
#define WUPB           0x17

// ISO 14443 B Type FALSE
#define FALSE_REQB     0xB4
#define FALSE_ATTRIB   0xB5
#define FALSE_HLTB     0xB6
#define FALSE_WUPB     0xB7

//ISO 15693 TRUE
#define REQUEST        0x24

//ISO 15693 FALSE     
#define FALSE_REQUEST  0xC4

#define TRH031M_OK     0xAA
#define TRH031M_FALSE  0xFF

#define TEST_NUM 1
#define TEST_RUT 64

// *********************************************** 
// read cid_buf[4] from fifo
// *********************************************** 
void read_cid(void)
{
  char i;
	
	suma = 0x00; 	 					// read 5 bytes from fifo
	for(i=0; i<0x05; i++)
	{ 
		cid_buf[i] = rf_read(0x02);		// read cid from fifo
		suma = (suma | cid_buf[i]);	 	// set any data available
	}
}

// *********************************************** 
// cid_buf[4] send to uart
// *********************************************** 
void uart_cid5(void)
{
	uart_puts("CID 5 Bytes = ");		// card id display 
	uart_hex(cid_buf[0]);
	uart_hex(cid_buf[1]);
	uart_hex(cid_buf[2]);
	uart_hex(cid_buf[3]);
	uart_hex(cid_buf[4]);	
	uart_cr();		 			 		// end of string
}

// *********************************************** 
// cid_buf[4] send to uart
// *********************************************** 
void uart_cid9(void)
{
	uart_puts("CID 9 Bytes = ");		// card id display 
	uart_hex(cid_buf[0]);
	uart_hex(cid_buf[1]);
	uart_hex(cid_buf[2]);
	uart_hex(cid_buf[3]);
	uart_hex(cid_buf[4]);	
	uart_hex(cid_buf[5]);
	uart_hex(cid_buf[6]);
	uart_hex(cid_buf[7]);
	uart_hex(cid_buf[8]);
	uart_cr();		 			 		// end of string
}
// *********************************************** 
// IFD send to uart 
// *********************************************** 
void uart_ifd(char leng)
{
	int  i;
	
	for (i=0; i<leng; i++) 
	{
	  uart_putc(tx_buf[i]);    	
	}	
}

// *********************************************** 
// int0 interrupt from falling edge
// *********************************************** 
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
 	//external interupt on INT0
  char i;
	char tdi;
       
	tdi = rf_read(0x04); 		   // get length 	
  if (tdi > 0x20)
	{
    rf_write(0x09, 0x01);	 	 // control =erase fifo 
	  tx_buf[0] = 0xF1;
		tx_buf[1] = 0xF0;
		tx_buf[2] = 0x00;
		uart_ifd(3);  			 		 // send 3 bytes to uart
	}

  else
	{ 	
    tx_buf[0] =0xF1;
		tx_buf[1] =0xF0;	 
		uart_ifd(2);  			 		 // send 2 bytes to uart

	  for(i=0; i<tdi ; i++)
		{
		  tx_buf[i] = rf_read(0x02);	// read data from fifo					 
		}
    uart_ifd(tdi);  		 	 	 // send Byte to Host
	}
  GICR  = 0x00;							 // int0 interrupt disable
}

// *********************************************** 
// RFID Card Reader TRH031M initialize
// rfid register setup to 14443A
// *********************************************** 
void rf_14443a_set(void) 
{ 
  rf_write(0x31, 0x00); 	  	// CWConduct
	rf_write(0x11, 0x5B); 	 		// TXControl Register
	rf_write(0x12, 0x3F); 	 		// CWConductance
	rf_write(0x19, 0x03); 	 		// rxcontrol1 0111_0011
	rf_write(0x1C, 0x18); 	 		// rxthreshold, for analog
  rf_write(0x15, 0x10); 	 		// Modwidth(AType, Default : 13) 

	rf_write(0x14, 0x01); 	 		// Coder A Set.
	rf_write(0x1A, 0x08); 	 		// DeCoder A Set.
	rf_write(0x21, 0x06); 	 		// RxWait Set.
	
	if (auto_mode != 0xAF)
	{
		uart_puts("14443A register set !!");
		uart_cr();	 						  // end of string	 
	}
	
	rf_write(0x1E, 0x81);				// rx control#2 =14443A decoding mode 1
//  rf_write(0x1F, 0x85);			// 14443A colision length =0x85 
//	rf_write(0x22, 0x03);			// Redundancy =parity odd, parity enable
//  xx,CRC3309,CRC8x,RxCRCEn,TxCRCEn,ParityOdd,ParityEn
	rf_write(0x0a, 0x00); 	 		// Error clear
}

// *********************************************** 
// get 14443A type RFID card
// *********************************************** 
void rf_reqa_exec(void)
{
  char i;
	char t;

	rf_write(0x31, 0x01); 		// Type select =14443A
	rf_write(0x0F, 0x07); 		// bit framing =tx last bit is 7 
	rf_write(0x22, 0x03);			// redundance control =odd parity																		  		
	rf_write(0x09, 0x01); 		// control =erase fifo
	rf_write(0x02, 0x26); 		// transmit data
	rf_write(0x01, 0x1E); 		// Transceive
	delay_10us(t_rec);		 		// delay =500us

  t =rf_read(0x01);					// get response from fifo
	if (t ==0x1E)   					// ATQA Response ?
	{     
		uart_fail("14443A ATQA"); // No ATQA Response
		rf_write(0x01, 0x00);		// command =	 
		return;
	}
	
	if (rf_read(0x04) ==0x02) // ATQA Response O.K ?
	{
		for(i=0; i<2; i++) 
		{
		  cid_buf[i] = rf_read(0x02);		// get cid from fifo
		}

		uart_puts("14443A REQA Response ID 2 Bytes = ");
		uart_hex(cid_buf[0]);		  // hexa cid send to uart 
		uart_hex(cid_buf[1]);		 	// hexa cid send to uart 
		uart_cr();							 	// end of string		
	}

	else											 	// No ATQA Response
	{     
		uart_fail("14443A REQA");
		rf_write(0x01, 0x00);			// command =idle
	}
}

// *********************************************** 
// read ID from rfid card
// *********************************************** 
void rf_rdid_exec(void)
{
  char i;

	rf_write(0x31, 0x01); 		 // Option Select =14443A
	rf_write(0x1A, 0x28);			 // ZeroAfterColl activation   
	rf_write(0x09, 0x01);			 // Clear FIFO
	rf_write(0x02, 0x93);			 // 93 -> FIFO
	rf_write(0x02, 0x20);			 // NVB -> FIFO
	rf_write(0x01, 0x1E);			 // Transceive
	delay_10us(t_rec);		 		 // delay =500us

	//Display_SFR();
  if (rf_read(0x04) ==0x05)	 // UID Read O.K ?
	{    
		//printf("ERR =%02X \n", (int)Read(0x0A));
		suma = 0x00;						 // clear flag							 
		for(i=0; i<0x05; i++)
		{ 
			cid_buf[i] = rf_read(0x02); // read data from fifo
			suma =(suma | cid_buf[i]);	// check any bit available
		}
		
		uart_puts("14443A ");
		uart_cid5();	 				 				// CID send to uart		 
   	rf_write(0x01, 0x00);	 				// command =idle		 
		return;
	}

	else		 							 			// UID Read Fail 
	{		
 		uart_fail("14443A UID" ); // Fail Indicator
		rf_write(0x01, 0x00);			// command =idle
	}
}

// *********************************************** 
// read 14443A ID
// *********************************************** 
void rf_reqa_rdid_exec(void)
{
  char i;
	char t;
    
	rf_write(0x0F, 0x07); 			// BITFRAME   |0| RxAlign  | 0 | TxLastBits | 
	rf_write(0x22, 0x03); 			// ChannelRedundancy |0|0|CRC3309|CRC8|RxCRCEn|TxCRCEn|ParityOdd|ParityEn|
	rf_write(0x09, 0x01); 			// Control |0|0|StandBy|PowerDown|Crypto1On|TStopNow|TStartNow|FlushFIFO|
	rf_write(0x02, 0x26); 			// transmit data
	rf_write(0x01, 0x1E); 			// Transceive
	delay_10us(t_rec);	 				// delay time =500us 

	t = rf_read(0x01);
	if (t ==0x1E)   						// ATQA Response O.K ?
	{     
		uart_fail("14443A ATQA");
		rf_write(0x01, 0x00);
		return;
	}
		
	if (rf_read(0x04) ==0x02)   // ATQA Response O.K
	{
		rf_write(0x1A, 0x28);			// ZeroAfterColl activation   
		rf_write(0x09, 0x01);			// Clear FIFO
		rf_write(0x02, 0x93);			// 93 -> FIFO
		rf_write(0x02, 0x20);			// NVB -> FIFO
		rf_write(0x01, 0x1E);			// Transceive
		delay_10us(20);						// delay 200us
	
		if (rf_read(0x04) ==0x05) // UID Read O.K ?
		{
			for(i=0; i<5; i++)
			{
			  cid_buf[i] = rf_read(0x02);
			}
			uart_puts("14443A ");
			uart_cid5();
			//printf("ERR = %02X  \n ",  (int)Read(0x0A));
		  rf_write(0x01, 0x00);
			return;
		}
		else		 				 			 		 // UID Read Fail 
		{
			uart_fail("14443A UID");
			rf_write(0x01, 0x00);
			return;
		}
	}
	else    	 				 			 		// No ATQA Response	
	{ 
		uart_fail("14443A ATQA");
		rf_write(0x01, 0x00);
		return;
	}
}

// *********************************************** 
// 
// *********************************************** 
void rf_reqa_rdid_auto(void)
{
  char i;
    
	rf_write(0x0F, 0x07); 		// bit frame = last bit 7 
	rf_write(0x22, 0x03); 		// Redundancy =odd parity
	rf_write(0x09, 0x01); 		// Control =flush fifo
	rf_write(0x02, 0x26); 		// transmit data =0x26
	rf_write(0x01, 0x1E); 		// Transceive
	delay_10us(100);		 			// delay for receive time =1000us
		
	if (rf_read(0x04) ==0x02) // ATQA Response O.K ?
	{
		rf_write(0x1A, 0x28);		// ZeroAfterColl activation   
		rf_write(0x09, 0x01);		// Clear FIFO
		rf_write(0x02, 0x93);		// 93 -> FIFO
		rf_write(0x02, 0x20);		// NVB -> FIFO
		rf_write(0x01, 0x1E);		// Transceive
		delay_10us(100);	 			// delay for receive time =1000us
	    
		if (rf_read(0x0A) != 0x00) // get error flag ?
		{ 
		   rf_write(0x01, 0x00);	 // command =idle
		   return; 								 // skip read cid
		}
		
		else if (rf_read(0x04) ==0x05)		// UID read O.K ?
		{    
			for (i=0; i<5; i++)
			{
			  cid_buf[i] = rf_read(0x02);	 // get cid from fifo
			}
			uart_puts("14443A ");
			uart_cid5();	 				 				// 5 bytes cid send	to uart 
			uart_cr(); 	 				 					// end of string	 	
		  rf_write(0x01, 0x00);					// command =idle
			return;
		}

		else		 				 							// UID Read Fail ? 
		{
	  	uart_fail("14443A ID");
			rf_write(0x01, 0x00);	 			// command =idle 
			return;
		}
	}
	
	else    	 				 							// No ATQA Response
	{ 
		uart_fail("14443A ATQA");
		rf_write(0x01, 0x00);		 			// command =idle
	}
}

// *********************************************** 
// ISO 14443A register setup
// *********************************************** 
void iso_14443a_reg(void)
{
  //Write(0x01, 0x3F);      // Init command
	rf_write(0x31, 0x00); 		// CWConduct
  rf_write(0x11, 0x5B); 		// CWConduct
	rf_write(0x12, 0x3F); 		// CWConduct
	rf_write(0x19, 0x03); 		// rxcontrol1 0111_0011
	rf_write(0x1C, 0x18); 		// rxthreshold, for analog
  rf_write(0x15, 0x10); 		// Modwidth(AType, Default : 13) 
	rf_write(0x1E, 0x81); 
	rf_write(0x14, 0x01); 		// Coder A Set.
	rf_write(0x1A, 0x08); 		// DeCoder A Set.
	rf_write(0x21, 0x06); 		// RxWait Set.
}

// *********************************************** 
// get ATQA from RFID card
// *********************************************** 
char func_reqa(void)
{
	char i;
	
	rf_write(0x0F, 0x07);     // BITFRAME = last bit 7
	rf_write(0x1F, 0x87);     // AThresh high/ Athersh low
	rf_write(0x22, 0x00);     // Redundancy = Parity disable
	rf_write(0x09, 0x01);     // Control =Flush FIFO
	rf_write(0x21, 0x06);     // rx wait time =0x06
	rf_write(0x02, 0x26);     // tx data =0x26 =REQA
	rf_write(0x01, 0x1E); 		// Transceive
	delay_10us(t_rec);	 			// delay timer =500us

	if (rf_read(0x04) ==0x02)	// get length	 
	{
		for (i=0; i <2; i++)
		{
		  atqa[i] =rf_read(0x02);
		}
		uart_puts("REQA response = ");
		uart_hex(atqa[0]);
		uart_hex(atqa[1]);
		uart_cr();
	 	return(4);					 		 // 4 =ATQA
	}	
	
	else
	{
		uart_fail("ATQA");
		rf_write(0x01, 0x00);		 // command =idle
		return(1);		 					 // 1 =error 
	}
	
}

// *********************************************** 
// anti coilsion select
// *********************************************** 
void func_anticol_select(void)
{
	char i;
	char byte_cnt =0x00;
	char bit_cnt =0x00;
	char anti_col =1;
	char coll_pos;
	char nvb =0x20;

  for (i=0;i<5;i++)
	{
	  cid[i] = 0x00; 					// erase cid
	}

  rf_write(0x22, 0x03);			// redundance =odd parity
  rf_write(0x1F, 0x87);			// colision width =8, low width =7

  while(anti_col)
	{
    rf_write(0x1A, 0x28);		// Zero After Coll activation
		rf_write(0x09, 0x01);		// Clear FIFO
		rf_write(0x02, 0x93);		// 93 -> FIFO
		rf_write(0x02, nvb);		// NVB -> FIFO

    for (i=0;i<byte_cnt;i++)
		{
		  rf_write(0x02, cid[i]);
		}
			
		if(bit_cnt != 0x00)			// available bit count ? 
		{
			rf_write(0x02, cid[byte_cnt]);
			rf_write(0x0F, (bit_cnt<<4) | bit_cnt); // Tx Last Bits/RxAlign auf nb_bi
		}

		rf_write(0x01, 0x1E);		// tranceive			
    delay_ms(t_idl);	 			// delay time for receive

    if((rf_read(0x0A) & 0x01) && (loop_cnt < 32))
		{
			for (i =byte_cnt; i<5; i++)
			{
			  cid[i] += rf_read(0x02); // read data from fifo
			}	
			coll_pos = rf_read(0x0B);	 // get colision position
			byte_cnt += (coll_pos /8);
			bit_cnt += (coll_pos %8);

			if(bit_cnt >= 8)
			{
				byte_cnt++;
				bit_cnt -= 8;
			}

			//	NVB += ((ByteCount<<4) + BitCount);
 			nvb =0x20 +((byte_cnt <<4) + bit_cnt);
      rf_write(0x0F, 0x00);			// Reset RxAlign

⌨️ 快捷键说明

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