RFID.rar

Wykrywacz transpondera w kocie

Z załączonym kodem ustrojstwo sprawnie czyta tagi w standardzie EM4000. Data rate: clk/64, Encoding: Manchester. Napisałem nowy kod, bo chciałem zrobić kopiarkę zapisującą do T5557, ale akurat karta, którą chciałem klonować (jakaś Indala), gada z inną modulacją – prawdopodobnie PSK. Tak więc nie chciało mi się już tworzyć funkcji zapisującej T5557. Druga posiadana karta jest zgodna z EM4000 i tą odczytuje i dekoduje bez problemu. Numery, jak poprzednio, można zapisywać do EEPROM i czytać programatorem. W EEPROM zapisuje się sam numer, bez kodów kontrolnych i preambuły.

  • RFID.rar
    • Animal_Rfid.c
    • defs.h
    • eprom.c
    • eprom.h


Download file - link to post

RFID.rar > Animal_Rfid.c

//ICC-AVR application builder : 2008-03-14 12:19:25
// Target : T2313
// Crystal: 20.0000Mhz

#include & lt; iotiny2313.h & gt;
#include & lt; intrinsics.h & gt;
#include " defs.h "
#include " eprom.h "

__regvar __no_init volatile unsigned char clock_counter @ 14;
__regvar __no_init volatile unsigned char flags @ 15;

void port_init(void){
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x17;
PORTD = 0x1C;
//PORTD = 0x18;
DDRD = 0x62;
}
//TIMER0 initialize - prescale:1
void timer0_init(void){
TCCR0B = 0x00;
TCNT0 = 0; //set count
OCR0A = FRQ_125; //set carrier freq
OCR0B = FRQ_125; //set
TCCR0A = ((1 & lt; & lt; COM0A0)|(1 & lt; & lt; WGM01)); //config timer toggle OC0A, CTC mode.
}
//TIMER1 initialize - prescale:256
void timer1_init(void){
TCCR1B = 0x00; //stop timer
TCNT1H = 0xC2; //set count value
TCNT1L = 0xF7;
OCR1AH = 0xE1;
OCR1AL = 0x7C; //set compare value to 100 mS - TX off
OCR1BH = 0xC3;
OCR1BL = 0x46; //ENABLE_RF_INPUT_INT 1 mS after carrier
TCCR1A = 0x00;
}

unsigned char parity_line(unsigned char line){ //line parity EM400x mode
unsigned char parity_bit;
unsigned char loop = 0;
parity_bit = (line & 0x01);
for(loop = 1; loop & lt; 5; loop++){
parity_bit ^= ((line & gt; & gt; loop) & 0x01);
}
parity_bit & = 0x01;
return parity_bit;
}

unsigned char parity_column(unsigned char *buffer){
unsigned char loop = 1;
unsigned char xor = buffer[0] & 0x0F;
for(loop = 1; loop & lt; 10; loop++){
xor ^= buffer[loop];
}
xor & = 0x0F;
return xor;
}

#pragma vector = TIMER1_OVF1_vect
__interrupt void timer1_ovf_isr(void){//TIMER1 has overflowed - carrier on
TCNT1H = 0xC2; //set count value
TCNT1L = 0xF7;
LED_G_ON; //Green LED on
OCR1BH = 0xC3;
OCR1BL = 0x46; //ENABLE_RF_INPUT_INT 1 mS after carrier
START_CARRIER;
}
#pragma vector = TIMER1_COMPA_vect
__interrupt void timer1_compa_isr(void){ //timer1 count compare match - carrier off
DISABLE_RF_INPUT_INT;
STOP_CARRIER;
LED_G_OFF;
LED_R_OFF;
flags |= F_NEW;
}
#pragma vector = TIMER1_COMPB_vect
__interrupt void timer1_compb_isr(void){
EIFR |= ( 1 & lt; & lt; INTF0 ); //clear pending interrupt
ENABLE_RF_INPUT_INT; //and enable INT0 - data input
}
#pragma vector = INT0_vect
__interrupt void int0_isr(void){
//external interupt on INT0
if( flags & F_SYNC ){ // sync to incoming data
if(( clock_counter & lt; ( 64 + WINDOW ) ) & & ( clock_counter & gt; ( 64 - WINDOW ))){
flags |= F_DATA_READY;
clock_counter = 64; //in the middle of bit
}
}else{ //F_SYNC
if(( clock_counter & gt; ( 128 - WINDOW ) ) || ( clock_counter & lt; WINDOW )) flags |= F_SYNC;
clock_counter = 64; //phase incorrect
}
LED_R_ALT; //red LED toggle - data indicator
if( flags & F_SPK )SPK_ALT; //speaker
}
#pragma vector = INT1_vect
__interrupt void int1_isr(void){ // external interupt on INT1
flags |= F_BUTTON_INT; // button depressed
}
#pragma vector = TIMER0_COMPA_vect
__interrupt void timer0_compa_isr(void){ //carrier generator
clock_counter ++; //carrier clock
clock_counter & = CLK_MASK; //nie idz w maliny
}
void init_devices(void){ // initialize all peripherals
__disable_interrupt(); // disable all interrupts
port_init();
timer0_init();
timer1_init();
MCUCR = ((1 & lt; & lt; ISC11)|(1 & lt; & lt; ISC00)); //Any edge on INT0 and falling edge on INT 1 generates an interrupt request.
GIMSK |= (1 & lt; & lt; INT1); // INT1 enabled
TIMSK = 0xE1; //enable: TOIE1, OCIE1A, OCIE1B, OCIE0A
EECR & = ~( ( 1 & lt; & lt; EEPM1 ) | ( 1 & lt; & lt; EEPM0 ) ); // erase & write at once
flags = 0;
clock_counter = 0;
__enable_interrupt(); //re-enable interrupts
}

__C_task void main(void){
unsigned char bit_buff;
unsigned char buff[12];
unsigned char buffer_counter;
unsigned char received_bits;
unsigned char switch_decoder = FIND_PREAMBLE;
unsigned char main_switch = START;
unsigned char bit = 0;
unsigned char tmp = 0;
unsigned char mem_start_adr = 0;
init_devices();

mem_start_adr = read_eprom( EEPROM_PTR ); //stard address for write identification number

if( mem_start_adr & gt; 112 ){ //set back to 0 when full
write_eprom( EEPROM_PTR, 0 );
mem_start_adr = 0;
}

while(1){
switch(main_switch){
case START:
received_bits = 0;
buffer_counter = 0;
for( tmp = 0; tmp & lt; 11; tmp ++ ) buff[ tmp ] = 0; // buff init
tmp = 0;
main_switch = RECEIVING_DATA;
switch_decoder = FIND_PREAMBLE;
flags & = 0x80; //rest flags except speaker
CARRIER_UNLOCK; //unlock carrier timing
START_TIMER1;
break;
case RECEIVING_DATA:
if( ( flags & F_DATA_READY ) == 0 )break; //wait for data bit
flags & = ~F_DATA_READY; //clear flag
if( INPUT_BIT != 0 ){
bit = 1; //set local variable low
}else{
bit = 0; //set local variable high
}
switch(switch_decoder){
case FIND_PREAMBLE: //search for 9 h bits
if( bit == 1 ){
tmp++;
if( tmp & gt; 8 ){ //got it
CARRIER_LOCK; //do not switch off carrier - interrupt disable
bit_buff = 0;
switch_decoder = PREAMBLE_FOUND;
}
}else{
tmp = 0;
}
break;
case PREAMBLE_FOUND: //4 bits receiving
bit_buff |= ( bit & lt; & lt; ( 4 - received_bits ) ); //reversed sequence for CRC - & gt; MSB first
received_bits++;
if( received_bits & gt; 4 ){
received_bits = 0;
if( buffer_counter & lt; 10 ){
if( parity_line( bit_buff ) == 0 ){
buff[ buffer_counter++ ] = ( bit_buff & gt; & gt; 1);
switch_decoder = PREAMBLE_FOUND;
bit_buff = 0;
}else{
main_switch = START;
}
}else{
if((bit_buff & 0x01) != 0){
main_switch = START;
}else{
switch_decoder = LAST_LINE;
}
}
}
break;
case LAST_LINE: //parity check
bit_buff = ( bit_buff & gt; & gt; 1);
bit_buff & = 0x0F;
if( parity_column( buff ) == bit_buff ){
main_switch = CRC_OK;
}else{
main_switch = START;
}
break;
default:
break;
}//switch_decoder end
break;
case CRC_OK:
STOP_TIMER1;
DISABLE_RF_INPUT_INT;
STOP_CARRIER;
LED_G_OFF;
LED_R_OFF;
LED_B_ON; //end of receive cycle
break;
case SAVE_BUFFER: //save to eeprom
bit_buff = 0;
for( tmp = 0; tmp & lt; 9; tmp +=2 ) {
buff[ bit_buff++ ] = (( buff[ tmp ] & lt; & lt; 4 ) | buff[ tmp + 1 ]);
}
for( tmp = 0; tmp & lt; 5; tmp ++ ) {
write_eprom( mem_start_adr + tmp, buff[ tmp ] );
}
mem_start_adr += 8;
write_eprom( EEPROM_PTR, mem_start_adr );
LED_Y_ON;
main_switch = STOP;
break;
case STOP:
__no_operation(); //end of work
break;
default:
break;
} //main_switch end
if( flags & F_BUTTON_INT ){
flags & = ~F_BUTTON_INT;
if( ( PIND & ( 1 & lt; & lt; PD3 ) ) == 0 ){
if ( main_switch == CRC_OK ){
main_switch = SAVE_BUFFER;
} else {
flags ^= F_SPK; // enable/disable speaker
}
}
}
if( (flags & F_NEW) != 0 )main_switch = START;
}
}


RFID.rar > defs.h

#define START_CARRIER ( TCCR0B = 0x01 )
#define STOP_CARRIER ( TCCR0B = 0x00 )
#define START_TIMER1 ( TCCR1B = 0x04 )
#define STOP_TIMER1 ( TCCR1B = 0x00 )
#define INPUT_BIT ( PIND & ( 1 & lt; & lt; PD2 ) )
#define CARRIER_UNLOCK ( TIMSK |= ( 1 & lt; & lt; OCIE1A ) )
#define CARRIER_LOCK ( TIMSK & = ~( 1 & lt; & lt; OCIE1A ) )

#define CLK_MASK 0x7F
#define WINDOW 12

//Flags
#define F_SYNC 0x01
#define F_BAD_EDGE 0x02
#define F_LOCK 0x04
#define F_DATA_READY 0x08
#define F_NEW 0x10
#define F_BUTTON_INT 0x20
#define F_LAST 0x40
#define F_SPK 0x80

#define ENABLE_RF_INPUT_INT ( GIMSK |= ( 1 & lt; & lt; INT0 ))
#define DISABLE_RF_INPUT_INT ( GIMSK & = ~( 1 & lt; & lt; INT0 ))

//Leds
#define LED_G_ON ( PORTB |= ( 1 & lt; & lt; PB0 ) )
#define LED_G_OFF ( PORTB & = ~( 1 & lt; & lt; PB0 ) )
#define LED_R_ON ( PORTB |= ( 1 & lt; & lt; PB1 ) )
#define LED_R_OFF ( PORTB & = ~( 1 & lt; & lt; PB1 ) )
#define LED_R_ALT ( PORTB ^= ( 1 & lt; & lt; PB1 ) )
#define LED_B_ON ( PORTB |= ( 1 & lt; & lt; PB4 ) )
#define LED_B_OFF ( PORTB & = ~( 1 & lt; & lt; PB4 ) )
#define LED_B_ALT ( PORTB ^= ( 1 & lt; & lt; PB4 ) )
#define LED_Y_ON ( PORTD |= ( 1 & lt; & lt; PD6 ) )
#define LED_Y_OFF ( PORTD & = ~( 1 & lt; & lt; PD6 ) )
#define LED_Y_ALT ( PORTD ^= ( 1 & lt; & lt; PD6 ) )

//speaker
#define SPK_ALT ( PORTD ^= ( 1 & lt; & lt; PD5 ) )

//decoder
#define INIT 0
#define FIND_PREAMBLE 1
#define PREAMBLE_FOUND 2
#define CONTROL_BIT 3
#define LAST_LINE 4

//main switch
#define START 0
#define RECEIVING_DATA 1
#define CRC_OK 2
#define SAVE_BUFFER 3
#define STOP 4

#define F_INPUT_BIT 0x04

#define CRC_START 0x00
#define EEPROM_PTR 0x7F

#define FRQ_125 0x4f
#define FRQ_134 0x4b


RFID.rar > eprom.c

#include " eprom.h "

void write_eprom(unsigned char adres,unsigned char val){
while (EECR & 0x02)__no_operation();
EEAR=adres;
EEDR=val;
__disable_interrupt();
EECR |= 0x04;
EECR |= 0x02;
__enable_interrupt();
return;
}
unsigned char read_eprom(unsigned char adres){
while(EECR & 0x02)__no_operation();
EEAR = adres;
EECR |= 0x01;
return EEDR;
}


RFID.rar > eprom.h

#include & lt; iotiny2313.h & gt;
#include & lt; intrinsics.h & gt;

void write_eprom(unsigned char,unsigned char);
unsigned char read_eprom(unsigned char);
void save_error_counters(void);