SIGNAL (SIG_INPUT_CAPTURE1) { static unsigned int sync; static unsigned char bitcount; static unsigned char insync = 0; static unsigned int old_cnt; static unsigned char check_liw; static unsigned char liw_detected; static unsigned char first_short; static unsigned char address; unsigned int i; unsigned char fail; TCCR1B ^= 1< 255) { i = 255; } old_cnt = ICR1; fail = 0; if (i < MANCHESTER_0) { // Time too short ////////////////////////////////////// fail = 1; check_liw = 0; address = 0; } else if (i < MANCHESTER_1) { // Time short ////////////////////////////////////////// //DATA if (insync) { if (first_short == 0) { first_short = 1; } else { //Check edge if (bit_is_set(TCCR1B, ICES1)){ sync |= 1< 2){ check_liw++; } else{ check_liw = 0; } if (check_liw == 5) { check_liw = 0; bitcount = 0; insync = 1; first_short = 1; sync = 0; } } else if (i < MANCHESTER_3) { // Time longer ///////////////////////////////////////// if (insync){ fail = 1; } else { //LIW detection if (check_liw == 4) { check_liw = 0; insync = 1; first_short = 0; sync = 0; if (bit_is_set(TCCR1B, ICES1)){ sync = 1; } //First data-bit bitcount = 1; //Set next data-bit } else{ check_liw = 0; } } } else if (i < MANCHESTER_4) { // Time LIW //////////////////////////////////////////// if (check_liw == 2) { check_liw++; if (liw_detected == 1){ address = 1; } liw_detected = 1; //rfid send if (sendflag > 0) { TCNT0 = 255-32; TIFR |= (1>>TOV0); PORTC &= ~0x01; //CFE OFF if (sendflag == 1) { //rfid read senddata[0] = 0x00; //pc senddata[1] = set_parity(rfid_address); //fw senddata[2] = set_parity(rfid_address); //lw senddata[3] = 0x00; senddata[4] = 0x00; senddata[5] = set_parity(0x0a); senddata[6] = 0x00; sendpointer = 111; } //rfid write if (sendflag == 2) { senddata[0] = (rfid_data[0]^rfid_data[1]^rfid_data[2]^rfid_data[3])<<1; //pc senddata[1] = set_parity(rfid_data[0]); senddata[2] = set_parity(rfid_data[1]); senddata[3] = set_parity(rfid_data[2]); senddata[4] = set_parity(rfid_data[3]); senddata[5] = set_parity(rfid_address); senddata[6] = set_parity(0x12); senddata[7] = 0x00; sendpointer = 129; } sendflag = 0; } } else{ check_liw = 0; } insync = 0; } else { // Time too long /////////////////////////////////////// fail = 1; check_liw = 0; address = 0; } if (fail){ insync = 0; } if (insync > 0){ PORTD |= 0x04; } // Set EN3 else{ PORTD &= ~0x04; } // Clear EN3 if ((bitcount == 9)&&(insync)) { if (address == 5) { i = data[0]; i ^= data[1]; i ^= data[2]; i ^= data[3]; i ^= sync; if (i == 0) { rfid_mode = 0; flag = 1; } else{ fail = 1; } insync = 0; } if ((address > 0)&&(address < 5)) { if (check_parity(sync)) { data[address-1] = sync & 0xff; sync = 0; address++; } else { fail = 1; insync = 0; } } bitcount = 0; } } unsigned char rfid_read_tag(unsigned char address, unsigned char *d) { unsigned char i; //activate rfid rfidactive = 1; //wait for rfid tag readout sendtimer = 50; rfid_address = address; rfid_mode = 1; flag = 0; while ((sendtimer > 0)&&(flag == 0)){ ; } //get data if ok if (flag) { for (i=0; i<5; i++) { d[i] = data[i]; data[i] = 0; } } return flag; }