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

📄 mifare_tx_rx_c_2.c

📁 Build your own RFID reader with the http://www.elektor.es/ schematic. It works great with tag MiFare
💻 C
📖 第 1 页 / 共 2 页
字号:
// pause
	PORTB=0 ;
    _delay_loop_2(8000) ;

// charge
	PORTB=1 ;
    _delay_loop_2(8000) ;

}





//----------------------------------------------------------------------------------------

void REQAtest(){
int k ;
	lcd_home() ;
	lcd_clrscr() ;
	lcd_puts("REQA test");
	while (1){
		lcd_gotoxy(0,1);  
		MIFAREpauseANDcharge() ;
		TXbyteBUFFER[0]=0x26 ;
		TXbits=7 ;
		TXconvert(30) ;	
		PORTB=TRIGGER ; 
 		TX_RX() ;
		RXsamplesTObits(2) ;

		lcd_putc('>') ;
		GETbytes(2) ;
		for (k=0 ; k<2 ; k++){
	 		LCDbyte_out(RXbyteBUFFER[k]) ; 
			lcd_putc(' ') ;
			} ;
  		lcd_gotoxy(10,1) ;
		lcd_putc('E') ;
		lcd_putc(':') ;
		LCDword_out(RXerror) ;
		}
}

void fastREQAtest(){
	lcd_home() ;
	lcd_clrscr() ;
	lcd_puts("Fast REQA Test");
	lcd_gotoxy(0,1);  
	lcd_puts("no report !");
	while (1){
		MIFAREpauseANDcharge() ;
		TXbyteBUFFER[0]=0x26 ;
		TXbits=7 ;
		TXconvert(30) ;	
		PORTB=TRIGGER ; 
 		TX_RX() ;
		}
}

void fastWUPAtest(){
	lcd_home() ;
	lcd_clrscr() ;
	lcd_puts("Fast WUPA Test");
	lcd_gotoxy(0,1);  
	lcd_puts("no report !");
	while (1){
		MIFAREpauseANDcharge() ;
		TXbyteBUFFER[0]=0x52 ;
		TXbits=7 ;
		TXconvert(30) ;	
		PORTB=TRIGGER ; 
 		TX_RX() ;
		}
}

void RDtest(uint8_t adr) {
// simply read page (adr) and display first 8 bytes
	lcd_home() ;
	lcd_clrscr() ;
	int RXpayload ;
	while(1) {
		MIFAREpauseANDcharge() ;
		TXbyteBUFFER[0]=0x26 ;
		TXbits=7 ;
		TXconvert(30) ;	
		TX_RX() ;


		TXbyteBUFFER[0]=0x30 ;
		TXbyteBUFFER[1]=adr ;
		TXbits=16 ;
		appendCRC() ;
		TXconvert(IObytes-1) ;
		PORTB=TRIGGER ; // falling edge on PORTB.1 trigger
 		TX_RX() ;
		RXpayload=16 ;
		RXsamplesTObits(RXpayload+2) ;
		GETbytes(RXpayload+2) ; // include CRC bytes
		CRCchk() ;

		lcd_home() ;
		int k ;
		for (k=0 ; k<8 ; k++){
	 		LCDbyte_out(RXbyteBUFFER[k]) ; 
			} ;
  		lcd_gotoxy(0,1);  
		lcd_putc('e') ; lcd_putc(':') ; 	LCDword_out(RXerror) ;
		}
	} ;




//----------------------------------------------------------------------------------------




void MIFAREreadN(int N) {
	int RXpayload ;
	int k ;

	uart_puts("READ ") ; 
	word_out(N) ;
	uart_puts(" >> ") ;

	TXbyteBUFFER[0]=0x30 ;
	TXbyteBUFFER[1]=N ;
	TXbits=16 ;
	appendCRC() ;
	TXconvert(IObytes-1) ;
 	TX_RX() ;
	RXpayload=16 ;
	RXsamplesTObits(RXpayload+2) ;

	GETbytes(RXpayload+2) ; // include CRC bytes
	CRCchk() ;
	for (k=0 ; k<RXpayload ; k++){
	 	byte_out(RXbyteBUFFER[k]) ; 
		uart_putc(' ') ;
		} ;
	uart_putc('e') ; uart_putc(':') ; 	word_out(RXerror) ;
	uart_putc(13) ; uart_putc(10) ; 
	} ;


void doMIFAREreqa() {
	MIFAREpauseANDcharge() ;

	TXbyteBUFFER[0]=0x26 ;
	TXbits=7 ;
	TXconvert(30) ;	
 	TX_RX() ;
	RXsamplesTObits(2) ;

	uart_puts("REQA:") ; 
	GETbytes(2) ;
	int k ;
	for (k=0 ; k<2 ; k++){
	 	byte_out(RXbyteBUFFER[k]) ; 
		uart_putc(' ') ;
		} ;
	uart_putc('e') ; uart_putc(':') ; 	word_out(RXerror) ;
	uart_putc(13) ; uart_putc(10) ; 
	}


void MIFAREreadTEST() {
	doMIFAREreqa() ;
	MIFAREreadN(0) ;
	MIFAREreadN(4) ;
	MIFAREreadN(8) ;
	MIFAREreadN(12) ;
	uart_putc(13) ; uart_putc(10) ; 
	uart_putc(13) ; uart_putc(10) ; 
	}

void xMIFAREwrite5(){
	doMIFAREreqa() ;
	MIFAREreadN(0) ;

	TXbyteBUFFER[0]=0xA2 ;
	TXbyteBUFFER[1]=0x05 ;
	TXbyteBUFFER[2]=0x10 ;
	TXbyteBUFFER[3]=0x20 ;
	TXbyteBUFFER[4]=0x30 ;
	TXbyteBUFFER[5]=0x40 ;
	TXbits=48 ;
	appendCRC() ;
	TXconvert(IObytes-1) ;
	//	PORTB=3 ; //  trigger
 	TX_RX() ;
	// ignore answer !!
	uart_putc(13) ; uart_putc(10) ; 
	} ;


//----------------------------------------------------------------------------------------
// routines started by user via uart:


void write_command(){
int k ;
char c ;
uint8_t WRdata[4] ;
uint8_t WRadr ;

	uart_crlf() ;
	uart_puts("enter 4 bytes:") ;
	for (k=0 ; k<4 ; k++ ){
			WRdata[k]=uart_geth() ;
		uart_crlf() ;
		}
	uart_puts("enter ADR:") ;
	WRadr=uart_geth() ;
	uart_crlf() ;
	uart_puts("WR :") ;
	for (k=0 ; k<4 ; k++ ){
		byte_out(	WRdata[k]) ; uart_blank() ;
		}
	uart_puts(" @ :") ;
	byte_out(WRadr) ;
	uart_crlf() ;
	uart_puts("ok ? (Y/N):") ;
	c=uart_getc() ;
	if ( (c=='y') | (c=='Y') ) { 
		uart_puts("WRITE !!") ; uart_crlf() ;
		doMIFAREreqa() ;
		MIFAREreadN(0) ;		// so we reach active state of MIFARE card

		TXbyteBUFFER[0]=0xA2 ;
		TXbyteBUFFER[1]=WRadr ;
		TXbyteBUFFER[2]=WRdata[0] ;
		TXbyteBUFFER[3]=WRdata[1] ;
		TXbyteBUFFER[4]=WRdata[2] ;
		TXbyteBUFFER[5]=WRdata[3] ;
		// bytes 1..5 are filled by parameters entered via uart !!		
		TXbits=48 ;
		appendCRC()	 ;
		TXconvert(IObytes-1) ;
		PORTB=3 ; //  trigger
 		TX_RX() ;
		longDELAY(200) ; // wait eeprom write time !!
	
		// ignore answer !!
		} ;
	uart_putc(13) ; uart_putc(10) ; 
	MIFAREreadTEST() ;
}

void read_command(){
	MIFAREreadTEST() ;
	}
	
void continous_command(){
	while ( !uart_chkc() ) { 
		MIFAREreadTEST() ; 
		} ;
	} 


void seq_command() {
	int k ;
	int RXpayload ;
	
	doMIFAREreqa() ;

	uart_puts("9320:") ; 

	TXbyteBUFFER[0]=0x93 ;
	TXbyteBUFFER[1]=0x20 ;
	TXbits=16 ;
	TXconvert(IObytes-1) ;
 	TX_RX() ;
	RXpayload=5 ;
	RXsamplesTObits(RXpayload) ;
	GETbytes(RXpayload) ; 
	for (k=0 ; k<RXpayload ; k++){
	 	byte_out(RXbyteBUFFER[k]) ; 
		uart_putc(' ') ;
		} ;

	uint8_t BCC ;
	BCC=0 ;
	for (k=0 ; k<5 ; k++){
	 	BCC ^= RXbyteBUFFER[k] ;
		} ;
	uart_puts("BCC1=") ; 	byte_out(BCC) ; 	uart_putc(' ') ;

	uart_putc('e') ; uart_putc(':') ; 	word_out(RXerror) ;
	uart_putc(13) ; uart_putc(10) ; 


	uart_puts("9370:") ; 
	TXbyteBUFFER[0]=0x93 ;
	TXbyteBUFFER[1]=0x70 ;
	TXbyteBUFFER[2]=RXbyteBUFFER[0] ;
	TXbyteBUFFER[3]=RXbyteBUFFER[1] ;
	TXbyteBUFFER[4]=RXbyteBUFFER[2] ;
	TXbyteBUFFER[5]=RXbyteBUFFER[3] ;
	TXbyteBUFFER[6]=RXbyteBUFFER[4] ;
	TXbits=7*8 ;
	appendCRC() ;
	TXconvert(IObytes-1) ;
 	TX_RX() ;
	RXpayload=1 ;
	RXsamplesTObits(RXpayload+2) ;
	GETbytes(RXpayload+2) ; // include CRC bytes
	CRCchk() ;
	for (k=0 ; k<RXpayload ; k++){
	 	byte_out(RXbyteBUFFER[k]) ; 
		uart_putc(' ') ;
		} ;
	uart_putc('e') ; uart_putc(':') ; 	word_out(RXerror) ;
	uart_putc(13) ; uart_putc(10) ; 

	uart_puts("9520:") ; 
	TXbyteBUFFER[0]=0x95 ;
	TXbyteBUFFER[1]=0x20 ;
	TXbits=16 ;
	TXconvert(IObytes-1) ;
	PORTB=3 ; // raising edge on PORTB.1 
 	TX_RX() ;
	RXpayload=5 ;
	RXsamplesTObits(RXpayload) ;
	GETbytes(RXpayload) ; 
	for (k=0 ; k<RXpayload ; k++){
	 	byte_out(RXbyteBUFFER[k]) ; 
		uart_putc(' ') ;
		} ;

	BCC=0 ;
	for (k=0 ; k<5 ; k++){
	 	BCC ^= RXbyteBUFFER[k] ;
		} ;
	uart_puts("BCC0=") ; 	byte_out(BCC) ; 	uart_putc(' ') ;


	uart_putc('e') ; uart_putc(':') ; 	word_out(RXerror) ;
	uart_putc(13) ; uart_putc(10) ; 


	uart_puts("9570:") ; 
	TXbyteBUFFER[0]=0x95 ;
	TXbyteBUFFER[1]=0x70 ;
	TXbyteBUFFER[2]=RXbyteBUFFER[0] ;
	TXbyteBUFFER[3]=RXbyteBUFFER[1] ;
	TXbyteBUFFER[4]=RXbyteBUFFER[2] ;
	TXbyteBUFFER[5]=RXbyteBUFFER[3] ;
	TXbyteBUFFER[6]=RXbyteBUFFER[4] ;
	TXbits=7*8 ;
	appendCRC() ;
	TXconvert(IObytes-1) ;
 	TX_RX() ;
	RXpayload=1 ;
	RXsamplesTObits(RXpayload+2) ;
	GETbytes(RXpayload+2) ; // include CRC bytes
	CRCchk() ;
	for (k=0 ; k<RXpayload ; k++){
	 	byte_out(RXbyteBUFFER[k]) ; 
		uart_putc(' ') ;
		} ;
	uart_putc('e') ; uart_putc(':') ; 	word_out(RXerror) ;
	uart_putc(13) ; uart_putc(10) ; 

	uart_putc(13) ; uart_putc(10) ; 
	uart_putc(13) ; uart_putc(10) ; 


	} ;

void collision_command() {
	int k ;
	int RXpayload ;
	while ( !uart_chkc() ) { 
		doMIFAREreqa() ;
		
		uart_puts("9320:") ; 
		TXbyteBUFFER[0]=0x93 ;
		TXbyteBUFFER[1]=0x20 ;
		TXbits=16 ;
		TXconvert(IObytes-1) ;
 		TX_RX() ;
		RXpayload=5 ;
		RXsamplesTObits(RXpayload) ;
		GETbytes(RXpayload) ; 
		for (k=0 ; k<RXpayload ; k++){
	 		byte_out(RXbyteBUFFER[k]) ; 
			uart_putc(' ') ;
			} ;
		uint8_t BCC ;
		BCC=0 ;
		for (k=0 ; k<5 ; k++){
	 		BCC ^= RXbyteBUFFER[k] ;
			} ;
		uart_puts("BCC1=") ; 	byte_out(BCC) ; 	uart_putc(' ') ;
		uart_putc('e') ; uart_putc(':') ; 	word_out(RXerror) ;
		uart_putc(13) ; uart_putc(10) ; 


		uart_puts("93 64 ") ;
		TXbyteBUFFER[0]=0x93 ;
		TXbyteBUFFER[1]=0x64 ; // 64 means 6*8+4=52 bits of address TXed
		TXbyteBUFFER[2]=RXbyteBUFFER[0] ;
		TXbyteBUFFER[3]=RXbyteBUFFER[1] ;
		TXbyteBUFFER[4]=RXbyteBUFFER[2] ;
		TXbyteBUFFER[5]=RXbyteBUFFER[3] ;
		TXbyteBUFFER[6]=RXbyteBUFFER[4] ; // LSB transmitted first, only 4 bits transmitted !
		TXbits=48+4 ;
		TXconvert(IObytes-1) ;
		PORTB=TRIGGER ; // raising edge on PORTB.1 
		TX_RX() ;
		RXpayload=2 ;
		int BITSrequested=9*RXpayload+1 ;
		RXsamplesTObits(RXpayload) ;
		uart_putc('>') ; uart_putc('>') ; 
		GETbytes(4) ; // include CRC bytes
		for (k=0 ; k<BITSrequested ; k++){
		 	hex_out(RXbitBUFFER[k]) ; 
			} ;
   		uart_putc(13) ; uart_putc(10) ; 
		uart_putc(13) ; uart_putc(10) ; 
		}
	}

void uart_interpreter(){
char c ;
	uart_crlf() ;
	uart_crlf() ;
	uart_puts("ELEKTOR ISO14443");
	uart_crlf() ;
	while(1){
		uart_putc(13) ; uart_putc(10) ; 
		uart_puts("R=read,W=write,C=contious-read,T=collision_sequence,S=state_sequence >>") ;
		c=uart_getc() ;
		if ( (c=='w') | (c=='W') )      { uart_crlf() ; write_command() ; }
		else if ( (c=='r') | (c=='R') ) { uart_crlf() ; read_command() ; }
		else if ( (c=='c') | (c=='C') ) { uart_crlf() ; continous_command() ; }
		else if ( (c=='t') | (c=='T') ) { uart_crlf() ; collision_command() ; }
		else if ( (c=='s') | (c=='S') ) { uart_crlf() ; seq_command() ; }
	}
}

//----------------------------------------------------------------------------------------
// REQAtest() ;
// fastREQAtest() ;
// fastWUPAtest()
// IDtest() ;

#define jumperJA 0x04
#define jumperJB 0x08
#define jumperJC 0x10
#define jumperJD 0x20
#define jumperJE 0x40

void LCDset(){
	lcd_init(LCD_DISP_ON);
 	lcd_clrscr();
//            0123456789012345       
    lcd_puts("ELEKTOR ISO14443");
	lcd_gotoxy(0,1);  
    int option= ~(PIND | 0b11 ) ;
	lcd_puts("option= ") ; LCDbyte_out(option) ;
    longDELAY(1000) ;   
}




//----------------------------------------------------------------------------------------




int main(){

   // 13.56MHz/9600/16=88.28....
  UCSRB |= ( 1 << TXEN );					// UART TX einschalten
  UCSRB |= ( 1 << RXEN );					// UART RX einschalten
  UCSRC |= ( 1 << URSEL )|( 3<<UCSZ0 );	    // Asynchron 8N1
  UBRRH  = 0;                               // Highbyte ist 0
  UBRRL  = 88;                              // Lowbyte ist 88 dezimal

  DDRA =0b00000000 ;
  PORTA=0b00000000 ; // pull-ups off

  DDRB =0b00001111 ;
  PORTB=0b00001111 ;

  DDRD =0b00000010 ; // TXD is out only
  PORTD=0b11111111 ; // pull ups on

  int option= ~(PIND | 0b11 ) ;
  if ( option & jumperJA)     { LCDset() ; fastREQAtest() ; }
  else if ( option & jumperJB){ LCDset() ; REQAtest() ; }
  else if ( option & jumperJC){ LCDset() ; fastWUPAtest() ; }
  else if ( option & jumperJD){ LCDset() ; RDtest(0) ; }
  else if ( option & jumperJE){ LCDset() ; RDtest(4) ; }
  else { uart_interpreter() ; } ;
  return(0) ;
 
 }

//--------------------------------------------------------------------------------

⌨️ 快捷键说明

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