forked from asetyde/EM4095
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathem4095.ino
201 lines (194 loc) · 6.33 KB
/
em4095.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <SoftwareSerial.h>
#define DELAYVAL 384 //384 //standard delay for manchster decode
#define TIMEOUT 1000 //standard timeout for manchester decode at 160mhz
// pin configuration
int demodOut=2;
int shd=4;
int mod=10;
int rdyClk=12;
byte tagData[5]; //Holds the ID numbers from the tag
//Manchester decode. Supply the function an array to store the tags ID in
bool decodeTag(unsigned char *buf){
unsigned char i = 0;
unsigned short timeCount;
unsigned char timeOutFlag = 0;
unsigned char row, col;
unsigned char row_parity;
unsigned char col_parity[5];
unsigned char dat;
unsigned char j;
while(1){
timeCount = 0;
while(0 == digitalRead(demodOut)){ //watch for demodOut to go low
if(timeCount >= TIMEOUT){ //if we pass TIMEOUT milliseconds, break out of the loop
break;
}
else{
timeCount++;
}
}
if (timeCount >= 600){
return false;
}
timeCount = 0;
delayMicroseconds(DELAYVAL);
if(digitalRead(demodOut)){
for(i = 0; i < 8; i++){ // 9 header bits
timeCount = 0; //restart counting
while(1 == digitalRead(demodOut)){ //while DEMOD out is high
if(timeCount == TIMEOUT){
timeOutFlag = 1;
break;
}
else{
timeCount++;
}
}
if(timeOutFlag){
break;
}
else{
delayMicroseconds(DELAYVAL);
if( 0 == digitalRead(demodOut)){
break;
}
}
}//end for loop
if(timeOutFlag){
timeOutFlag = 0;
return false;
}
if(i == 8){ //Receive the data
timeOutFlag = 0;
timeCount = 0;
while(1 == digitalRead(demodOut)){
if(timeCount == TIMEOUT){
timeOutFlag = 1;
break;
}
else{
timeCount++;
}
if(timeOutFlag){
timeOutFlag = 0;
return false;
}
}
col_parity[0] = col_parity[1] = col_parity[2] = col_parity[3] = col_parity[4] = 0;
for(row = 0; row < 11; row++){
row_parity = 0;
j = row >> 1;
for(col = 0, row_parity = 0; col < 5; col++){
delayMicroseconds(DELAYVAL);
if(digitalRead(demodOut)){
dat = 1;
}
else{
dat = 0;
}
if(col < 4 && row < 10){
buf[j] <<= 1;
buf[j] |= dat;
}
row_parity += dat;
col_parity[col] += dat;
timeCount = 0;
while(digitalRead(demodOut) == dat){
if(timeCount == TIMEOUT){
timeOutFlag = 1;
break;
}
else{
timeCount++;
}
}
if(timeOutFlag){
break;
}
}
if(row < 10){
if((row_parity & 0x01) || timeOutFlag){ //Row parity
timeOutFlag = 1;
break;
}
}
}
if( timeOutFlag || (col_parity[0] & 0x01) || (col_parity[1] & 0x01) || (col_parity[2] & 0x01) || (col_parity[3] & 0x01)){ //Column parity
timeOutFlag = 0;
return false;
}
else{
return true;
}
}//end if(i==8)
return false;
}//if(digitalRead(demodOut))
} //while(1)
}
//function to compare 2 byte arrays. Returns true if the two arrays match, false of any numbers do not match
bool compareTagData(byte *tagData1, byte *tagData2){
for(int j = 0; j < 5; j++){
if (tagData1[j] != tagData2[j]){
return false; //if any of the ID numbers are not the same, return a false
}
}
return true; //all id numbers have been verified
}
//function to transfer one byte array to a secondary byte array.
//source -> tagData
//destination -> tagDataBuffer
void transferToBuffer(byte *tagData, byte *tagDataBuffer){
for(int j = 0; j < 5; j++){
tagDataBuffer[j] = tagData[j];
}
}
bool scanForTag(byte *tagData){
static byte tagDataBuffer[5]; //A Buffer for verifying the tag data. 'static' so that the data is maintained the next time the loop is called
static int readCount = 0; //the number of times a tag has been read. 'static' so that the data is maintained the next time the loop is called
boolean verifyRead = false; //true when a tag's ID matches a previous read, false otherwise
boolean tagCheck = false; //true when a tag has been read, false otherwise
tagCheck = decodeTag(tagData); //run the decodetag to check for the tag
if (tagCheck == true){ //if 'true' is returned from the decodetag function, a tag was succesfully scanned
readCount++; //increase count since we've seen a tag
if(readCount == 1){ //if have read a tag only one time, proceed
transferToBuffer(tagData, tagDataBuffer); //place the data from the current tag read into the buffer for the next read
}
else if(readCount == 2){ //if we see a tag a second time, proceed
verifyRead = compareTagData(tagData, tagDataBuffer); //run the checkBuffer function to compare the data in the buffer (the last read) with the data from the current read
if (verifyRead == true){ //if a 'true' is returned by compareTagData, the current read matches the last read
readCount = 0; //because a tag has been succesfully verified, reset the readCount to '0' for the next tag
return true;
}
}
}
else{
return false;
}
return true;
}
void setup(){
//set pin modes on RFID pins
pinMode(mod, OUTPUT);
pinMode(shd, OUTPUT);
pinMode(demodOut, INPUT);
pinMode(rdyClk, INPUT);
//set shd and MOD low to prepare for reading
digitalWrite(shd, LOW);
digitalWrite(mod, LOW);
Serial.begin(9600);
Serial.println("Welcome. Please swipe your RFID Tag.");
}
void loop(){
//scan for a tag - if a tag is sucesfully scanned, return a 'true' and proceed
if(scanForTag(tagData) == true){
Serial.print("RFID Tag ID:"); //print a header to the Serial port.
//loop through the byte array
for(int n=0;n<5;n++){
Serial.print(tagData[n],DEC); //print the byte in Decimal format
if(n<4){//only print the comma on the first 4 nunbers
Serial.print(",");
}
}
Serial.print("\n\r");//return character for next line
}
}