






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The c code for a microcontroller to read data from rfid tags using a specific transceiver. The code includes functions for initializing the i/o pins, sending commands to the transceiver, and reading data from the tags. The code also includes appendices with the tag detection algorithm and example tag ids.
Typology: Study Guides, Projects, Research
1 / 30
This page cannot be seen from the preview
Don't miss anything!























//Francisco Lao and Jian Zhang //ECE445 FALL 2004 //Date: 11-28- //Code for the microcontroller of the wireless key finder system. #include <16F877.h> #device *=16 ADC= #fuses HS #fuses NOWDT #fuses NOPROTECT #fuses NOLVP #fuses NODEBUG #fuses NOPUT #fuses NOBROWNOUT #use delay(clock=20000000) #use fast_io(B) #include <stddef.h> #include <string.h> #define MY_TRISB 0b #define T_BUFFER_SIZE 46 byte data_buffer[T_BUFFER_SIZE]; byte sid1[32]; byte sid2[32]; byte sid3[32]; //Pin 0 is clock. Pin 1 is Data. //It outputs 0 on rising edge of clock. void zero() { output_low(PIN_B1); output_low(PIN_B0); delay_us(100); output_high(PIN_B0); output_low(PIN_B1); delay_us(100); output_low(PIN_B0); output_low(PIN_B1); delay_us(100); } //Pin 0 is clock. Pin 1 is Data
//It outputs 1 on rising edge of clock. void one() { output_high(PIN_B1); output_low(PIN_B0); delay_us(100); output_high(PIN_B1); output_high(PIN_B0); delay_us(100); output_low(PIN_B0); output_high(PIN_B1); delay_us(100); } //It sends a command to the transceiver telling it that Tag-it mode is desired. void Command() { /* address of the cards: 00C27C58 :0000 0000 1100 0010 0111 1100 0101 1000 00C27C5E :0000 0000 1100 0010 0111 1100 0101 1110 00C27C59 :0000 0000 1100 0010 0111 1100 0101 1001 */ int8 i; for(i = 0; i<3; i++) //TEST zero(); one(); zero(); zero(); zero(); one(); } //It sends the Tag-it command. void tag_it() { //first we need to put in: 00 0000 0011 0 00 int8 i; //00 0000 0011 0 00 for(i = 0; i<8; i++) zero(); for(i = 0; i<2; i++) one(); for(i = 0; i<3; i++) zero(); //CRC: 0x817b = 1000 0001 0111 1011 one(); zero(); zero();
int8 i; int8 j; int8 m; Set_Tris_B(0b11100101); i = 1; m = 0; while(m==0) { while(input(PIN_B0)) { if(!input(PIN_B2)) { m=1; for(j=0; j<32; j++) { data_buffer[j] = 0; } } else { while(i < 46) { if(input(PIN_B0)) { while(input(PIN_B0)) { if(i>13) data_buffer[i-14] = input(PIN_B2); } i = i+1; } m=1; } } } } j = 0; for(i = 0; i<32; i++) { if(j == 0) { if(data_buffer[i] != sid1[i]) j = 1; } else { i=32; } }
if(j==1) output_low(PIN_B7); } //It checks from the data received from the transceiver whether or not tag 2 is in the region. //It lights up led 2 if the tag is out of region. void led2() { int8 i; int8 j; int8 m; Set_Tris_B(0b11100101); i = 1; m = 0; while(m==0) { while(input(PIN_B0)) { if(!input(PIN_B2)) { m=1; for(j=0; j<32; j++) { data_buffer[j] = 0; } } else { while(i < 46) { if(input(PIN_B0)) { while(input(PIN_B0)) { if(i>13) data_buffer[i-14] = input(PIN_B2); } i = i+1; } m=1; } } } } j = 0; for(i = 0; i<32; i++) { if(j == 0) {
j = 0; for(i = 0; i<32; i++) { if(j == 0) { if(data_buffer[i] != sid3[i]) j = 1; } else { i=32; } } if(j==1) output_low(PIN_B5); } void main() { Set_Tris_B(MY_TRISB); //sid1 = {0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,1,0, 0,1,1,1, 1,1,0,0, 0,1,0,1, 1,0,0,0}; sid1[0]=0; sid1[1]=0; sid1[2]=0; sid1[3]=0; sid1[4]=0; sid1[5]=0; sid1[6]=0; sid1[7]=0; sid1[8]=1; sid1[9]=1; sid1[10]=0; sid1[11]=0; sid1[12]=0; sid1[13]=0; sid1[14]=1; sid1[15]=0; sid1[16]=0; sid1[17]=1; sid1[18]=1; sid1[19]=1; sid1[20]=1; sid1[21]=1; sid1[22]=0; sid1[23]=0; sid1[24]=0; sid1[25]=1; sid1[26]=0; sid1[27]=1; sid1[28]=1; sid1[29]=0; sid1[30]=0; sid1[31]=0; //sid2 = {0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,1,0, 0,1,1,1, 1,1,0,0, 0,1,0,1, 1,1,1,0}; sid2[0]=0; sid2[1]=0; sid2[2]=0; sid2[3]=0; sid2[4]=0; sid2[5]=0; sid2[6]=0; sid2[7]=0; sid2[8]=1; sid2[9]=1; sid2[10]=0; sid2[11]=0; sid2[12]=0; sid2[13]=0; sid2[14]=1; sid2[15]=0; sid2[16]=0; sid2[17]=1; sid2[18]=1; sid2[19]=1; sid2[20]=1; sid2[21]=1; sid2[22]=0; sid2[23]=0; sid2[24]=0; sid2[25]=1; sid2[26]=0; sid2[27]=1; sid2[28]=1; sid2[29]=1; sid2[30]=1; sid2[31]=0; //sid3 = {0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,1,0, 0,1,1,1, 1,1,0,0, 0,1,0,1, 1,0,0,1); sid3[0]=0; sid3[1]=0; sid3[2]=0; sid3[3]=0; sid3[4]=0; sid3[5]=0; sid3[6]=0; sid3[7]=0; sid3[8]=1; sid3[9]=1; sid3[10]=0; sid3[11]=0; sid3[12]=0; sid3[13]=0; sid3[14]=1; sid3[15]=0; sid3[16]=0; sid3[17]=1; sid3[18]=1; sid3[19]=1; sid3[20]=1; sid3[21]=1; sid3[22]=0; sid3[23]=0;
sid3[24]=0; sid3[25]=1; sid3[26]=0; sid3[27]=1; sid3[28]=1; sid3[29]=0; sid3[30]=0; sid3[31]=1; output_high(PIN_B7); output_high(PIN_B6); output_high(PIN_B5); while(1) { turn_on(); command(); turn_off(); tag_it(); delay_ms(5); turn_off(); led1(); turn_on(); command(); turn_off(); tag_it(); delay_ms(5); turn_off(); led2(); turn_on(); command(); turn_off(); tag_it(); delay_ms(5); turn_off(); led3(); } }
output_low(PIN_B0); delay_us(100); output_high(PIN_B1); output_high(PIN_B0); delay_us(100); output_low(PIN_B0); output_high(PIN_B1); delay_us(100); } //Transceiver telling the microcontroller that tag 1 is in the range. void send_data1() { //transceiver output stream to the PIC //11 0000 0011 0 1 0 0 SID 0000001 000000101 000 00011 0000 0111 1000 1000 1111 0100 int8 i; if (input(PIN_B3)) { for(i = 0; i<2; i++) one(); for(i = 0; i<4; i++) zero(); zero(); zero(); one(); one(); zero(); one(); zero(); zero(); //SID for(i = 0; i<32; i++) { if(sid1[i]=0) zero(); else one(); } for(i = 0; i<6; i++) zero(); one(); // for(i = 0; i<6; i++) zero(); one(); zero();
one(); //000 00011 0000 0111 1000 1000 1111 0100 for(i = 0; i<3; i++) zero(); for(i = 0; i<3; i++) zero(); one(); one(); for(i = 0; i<4; i++) zero(); zero(); for(i = 0; i<3; i++) one(); one(); for(i = 0; i<3; i++) zero(); one(); for(i = 0; i<3; i++) zero(); for(i = 0; i<4; i++) one(); zero(); one(); zero(); zero(); } } //Transceiver telling the microcontroller that the tag is not in the range. void send_fail() { zero(); zero(); zero(); } void main() //test code is for replying that only tag 1 is in range. { int8 i; int8 j; int8 k; int8 l; Set_Tris_B(MY_TRISB); // Port B's I/O //{0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,1,0, 0,1,1,1, 1,1,0,0, 0,1,0,1, 1,0,0,0};
//Francisco Lao and Jian Zhang //ECE445 FALL 2004 //Date: 11-28- //This PIC stimulates the behavior of the transceiver if two tags are possibly in the range #include <16F877.h> #device *=16 ADC= #fuses HS #fuses NOWDT #fuses NOPROTECT #fuses NOLVP #fuses NODEBUG #fuses NOPUT #fuses NOBROWNOUT #use delay(clock=20000000) #use fast_io(B) #include <stddef.h> #include <string.h> #define MY_TRISB 0b00111000 //port B used as output port #define T_BUFFER_SIZE 46 byte data_buffer[T_BUFFER_SIZE]; byte sid1[32]; byte sid2[32]; byte sid3[32]; //Pin 0 is clock. Pin 1 is Data. //It outputs 0 on rising edge of clock. void zero() { output_low(PIN_B1); output_low(PIN_B0); delay_us(100); output_high(PIN_B0); output_low(PIN_B1); delay_us(100); output_low(PIN_B0); output_low(PIN_B1); delay_us(100); } //Pin 0 is clock. Pin 1 is Data. //It outputs 1 on rising edge of clock. void one() {
output_high(PIN_B1); output_low(PIN_B0); delay_us(100); output_high(PIN_B1); output_high(PIN_B0); delay_us(100); output_low(PIN_B0); output_high(PIN_B1); delay_us(100); } //Transceiver telling the microcontroller that tag 1 is in the range. void send_data1() { //transceiver output stream to the PIC //11 0000 0011 0 1 0 0 SID 0000001 000000101 000 00011 0000 0111 1000 1000 1111 0100 int8 i; if (input(PIN_B3)) { for(i = 0; i<2; i++) one(); for(i = 0; i<4; i++) zero(); zero(); zero(); one(); one(); zero(); one(); zero(); zero(); //SID for(i = 0; i<32; i++) { if(sid1[i]==0) zero(); else one(); } // for(i = 0; i<6; i++) zero(); one(); // for(i = 0; i<6; i++) zero();
zero(); one(); zero(); zero(); //SID for(i = 0; i<32; i++) { if(sid2[i]==0) zero(); else one(); } // for(i = 0; i<6; i++) zero(); one(); // for(i = 0; i<6; i++) zero(); one(); zero(); one(); //000 00011 0000 0111 1000 1000 1111 0100 for(i = 0; i<3; i++) zero(); for(i = 0; i<3; i++) zero(); one(); one(); for(i = 0; i<4; i++) zero(); zero(); for(i = 0; i<3; i++) one(); one(); for(i = 0; i<3; i++) zero(); one(); for(i = 0; i<3; i++) zero(); for(i = 0; i<4; i++) one();
zero(); one(); zero(); zero(); } } //Transceiver telling the microcontroller that tag 3 is in the range. void send_data3() { //transceiver output stream to the PIC int8 i; if (input(PIN_B3)) { for(i = 0; i<2; i++) one(); for(i = 0; i<4; i++) zero(); zero(); zero(); one(); one(); zero(); one(); zero(); zero(); //SID for(i = 0; i<32; i++) { if(sid3[i]==0) zero(); else one(); } // for(i = 0; i<6; i++) zero(); one(); // for(i = 0; i<6; i++) zero(); one(); zero(); one();
sid1[8]=1; sid1[9]=1; sid1[10]=0; sid1[11]=0; sid1[12]=0; sid1[13]=0; sid1[14]=1; sid1[15]=0; sid1[16]=0; sid1[17]=1; sid1[18]=1; sid1[19]=1; sid1[20]=1; sid1[21]=1; sid1[22]=0; sid1[23]=0; sid1[24]=0; sid1[25]=1; sid1[26]=0; sid1[27]=1; sid1[28]=1; sid1[29]=0; sid1[30]=0; sid1[31]=0; //{0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,1,0, 0,1,1,1, 1,1,0,0, 0,1,0,1, 1,1,1,0}; sid2[0]=0; sid2[1]=0; sid2[2]=0; sid2[3]=0; sid2[4]=0; sid2[5]=0; sid2[6]=0; sid2[7]=0; sid2[8]=1; sid2[9]=1; sid2[10]=0; sid2[11]=0; sid2[12]=0; sid2[13]=0; sid2[14]=1; sid2[15]=0; sid2[16]=0; sid2[17]=1; sid2[18]=1; sid2[19]=1; sid2[20]=1; sid2[21]=1; sid2[22]=0; sid2[23]=0; sid2[24]=0; sid2[25]=1; sid2[26]=0; sid2[27]=1; sid2[28]=1; sid2[29]=1; sid2[30]=1; sid2[31]=0; //{0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,1,0, 0,1,1,1, 1,1,0,0, 0,1,0,1, 1,0,0,1); sid3[0]=0; sid3[1]=0; sid3[2]=0; sid3[3]=0; sid3[4]=0; sid3[5]=0; sid3[6]=0; sid3[7]=0; sid3[8]=1; sid3[9]=1; sid3[10]=0; sid3[11]=0; sid3[12]=0; sid3[13]=0; sid3[14]=1; sid3[15]=0; sid3[16]=0; sid3[17]=1; sid3[18]=1; sid3[19]=1; sid3[20]=1; sid3[21]=1; sid3[22]=0; sid3[23]=0; sid3[24]=0; sid3[25]=1; sid3[26]=0; sid3[27]=1; sid3[28]=1; sid3[29]=0; sid3[30]=0; sid3[31]=1; i = 0; while(i < 45) { if(input(PIN_B4)) { while(input(PIN_B4)) { if(i>12) data_buffer[i-13] = input(PIN_B5); } i = i+1; } } j = 0; k = 0; for(i = 0; i<32; i++) { if(j == 0) { if(data_buffer[i] != sid1[i]) j = 1; } else { i=32;
for(i = 0; i<32; i++) { if(k == 0) { if(data_buffer[i] != sid2[i]) k = 1; } else { i=32; } } while(1) { if(j==0) send_data1(); if(k==0) send_data2(); if(j==1 && k==1 && l==1) send_fail(); } }