How to compare strings/array? RFID Project
I am trying to make an RFID reader using Parallax's RFID Reader Module and a PIC 16F628A.
I am using PBP and am having troubles figuring out how to convert over the sample code found at Parallax to work in PBP.
Sample code: http://www.parallax.com/dl/src/prod/RFID1.BS2
The main trouble I am having is getting the comparison to work. I'm not sure what the best way would be to compare the scanned RFID 10 byte string array against a set of known tags.
Below is the part of the code I believe is not working right.
Does anybody have any idea on what I can do to fix this or perhaps someone has a better way of doing it? Thanks in advance!
-Dan
' -----[ EEPROM Data ]-----------------------------------------------------
Tag1 DATA "100050D77C" ' valid tags
Tag2 DATA "1000504B02"
Tag3 DATA "1000502DC9"
Tag4 DATA "100050803A"
Tag5 DATA "100050C472"
' -----[ Program Code ]----------------------------------------------------
Main:
LOW portb.3 ' activate the reader
SERIN2 portb.2, T2400, [WAIT($0A), STR buf\10] ' wait for hdr + ID
HIGH portb.3 ' deactivate reader
Check_List:
FOR tagNum = 1 TO LastTag ' scan through known tags
FOR idx = 0 TO 9 ' scan bytes in tag
READ (tagNum - 1 * 10 + idx), char ' get tag data from table
IF (char <> buf(idx)) THEN Bad_Char ' compare tag to table
NEXT
GOTO Tag_Found ' all bytes match!
Bad_Char: ' try next tag
NEXT
Video of working RFID Project + uOLED
i thought id post it here.. since this thread helped in the trick math operation.
My Board runs a PIC16F877A @ 20Mhz
Video Here
Code:
define OSC 20
ADCON1=7
CMCON=7
LastTag CON 3
X VAR byte
tagNum VAR byte ' from EEPROM table
idx VAR Byte ' tag byte index
char VAR Byte ' character from table
buf var byte(10)
'*****************************************************************
SYMBOL PWRLED = PORTB.2 'Software controlled power LED
SYMBOL RFID = PORTC.5 'RFID /ENABLE
'*****************************************************************
Tag1 DATA "0415146D53" ' 2 cards registered...
Tag3 DATA "30700D48A5"
'*****************************************************************
HIGH PWRLED
HIGH RFID
PAUSE 1000
serout2 PORTC.0,84,[$55] ' uOLED Initialize
'*****************************************************************
Reset:
gosub cls
PAUSE 1000
'*****************************************************************
Main:
LOW rfid ' activate the reader
SERIN2 PORTC.4,396,[WAIT(10),str BUF\10] ' wait for hdr + ID
HIGH rfid ' deactivate reader
Check_List:
FOR tagNum = 1 TO lasttag
FOR idx = 0 TO 9
READ (((tagNum - 1) * 10) + idx), char 'The Tricky Order of Operation
IF (char <> buf(idx)) THEN Bad_Char
NEXT
GOTO Tag_found
Bad_Char:
NEXT
Bad_tag:
FOR x = 1 TO 2
serout2 PORTC.0,84,[$73,$6,$4,$0,$F0," ACCESS",$00] 'cmd,column,row,font,Color(msb:lsb),“string”,$00
serout2 PORTC.0,84,[$73,$6,$8,$0,$F0," DENIED",$00]
NEXT
PAUSE 4000
GOTO reset
Tag_Found:
for x = 0 to 2
serout2 PORTC.0,84,[$73,$6,$4,$0,$1F," ACCESS",$00] 'cmd,column,row,font,Color(msb:lsb),“string”,$00
serout2 PORTC.0,84,[$73,$6,$8,$0,$1F," GRANTED",$00]
next
pause 4000
GOTO reset
CLS:
FOR X = 0 TO 2
serout2 PORTC.0,84,[$45]
next
return
Re: How to compare strings/array? RFID Project
Quote:
Originally Posted by
dan-tron
EUREKA!
Thanks guys! You have been a huge help. I have finally figured it out.
My problem was more like 3 problems...
1. I have never done EEPROM stuff before so I was lost to begin with.
2. As Bruce pointed out, the mathematical order of operations in one line was not correct. I guess the BS2 thinks a little differently from PBP in that respect.
3. My PicBasic Pro compiler was Version 2.30 form 2000. It seems that v2.30 supports the PIC 16F628 but the PIC 16F628A was implemented in a later version. I have upgraded my PBP to version 2.47 and now the EEPROM features work correctly on the 16F628A. When I successfully tested a 16F84A using the same code, that lead me to question the compiler.
So for future generations and the benefit of other people like me, here's my WORKING code that is tested successfully on a PIC 16F628A with a 20MHz resonator compiled using PBP v2.47. It reads data from the Parallax RFID Reader Module, compares it against known values stored in EEPROM and allows or denies access accordingly. Enjoy.
Code:
CMCON = 7
DEFINE OSC 20 'Set oscillator in MHz
' -----[ Variables ]-------------------------------------------------------
buf VAR Byte(10) ' RFID bytes buffer
tagNum VAR Byte ' from EEPROM table
idx VAR Byte ' tag byte index
char VAR Byte ' character from table
' -----[ EEPROM Data ]-----------------------------------------------------
Tag1 DATA "100050A4B7"
Tag2 DATA "1000508E0A"
Tag3 DATA "10005091DC"
Tag4 DATA "100050203A"
Tag5 DATA "100050DA36"
' -----[ Initialization ]--------------------------------------------------
HIGH portb.3 ' turn off RFID reader
LOW portb.6 ' lock the door!
Low portb.4 'Turn off LED
' -----[ Program Code ]----------------------------------------------------
Main:
LOW portb.3 ' activate the reader
SERIN2 portb.2, 396, [WAIT($0A), STR buf\10] ' wait for hdr + ID
HIGH portb.3 ' deactivate reader
Check_List:
FOR tagNum = 1 to 5 ' scan through known tags
FOR idx = 0 TO 9 ' scan bytes in tag
READ (((tagNum-1) * 10) + idx), char ' get tag data from table
IF (char <> buf(idx)) THEN Bad_Char ' compare tag to table
NEXT
GOTO Tag_Found ' all bytes match!
Bad_Char: ' try next tag
NEXT
Bad_Tag:
tagNum = 0
FREQOUT portb.5, 1000 */ $100, 115 */ $100 ' groan
PAUSE 1000
GOTO Main
Tag_Found:
HIGH portb.6 ' remove latch
High portb.4 ' Light LED
FREQOUT portb.5, 2000 */ $100, 880 */$100 ' beep
LOW portb.6 ' restore latch
Low portb.4 ' LED OFF
GOTO Main
hello all,
i would like to see the schematic of the above code if it is possible.
One more thing that i would like to incude at the code is to identify the name of the target id.
For example, once you pass the RFID chip from the reader, on the display to give you the name of the carrier.
if i have a chip with a target id : 123456789012 then i would like once i pass it from the reader to give a name like ASTANAPANE.
I guess that on the code i have to corespond the name to the target id.
How can we do that.
I have bought the modules from SPARKFUN and the RFID chips also from them.
I would like to make a small project for my room.
thanks for any suggestions.
Best Regards
Re: How to compare strings/array? RFID Project
Quote:
Check_List:
FOR tagNum = 1 to 5 ' scan through known tags
FOR idx = 0 TO 9 ' scan bytes in tag
READ (((tagNum-1) * 10) + idx), char ' get tag data from table
IF (char <> buf(idx)) THEN Bad_Char ' compare tag to table
NEXT
GOTO Tag_Found ' all bytes match!
Bad_Char: ' try next tag
NEXT
whenever the PIC identifies a tag stored in the EEPROM it still holds onto the "tagNum" variable. from there you can have your program branch off with the "tagNum" variable to display whatever you want. for argument sake lets say your third RFID badge is yours then:
Code:
Tag_Found:
IF tagNum = 3 then displayName
GOTO Main
displayName:
Serout2 portc.0,84,["ASTANAPANE"] 'assuming your display is on portc.0 @ 9600 baud
pause 5000 'Pause 5 sec then clear display
gosub CLRLCD
Goto main
Hope this helps... Cheers
Re: How to compare strings/array? RFID Project
thank you very much for the help....
i ordered the reader and the ID-12 from SPARKFUN, and i wait for them next week. Once i get them i will start to built my project.
I will make schematics and PCBs and i will post them here.
A lot of thanks for the code. I only need a little bit help of the code you made corresponding to the schematic.
If you dont have time i will read the code and identify the Pins for the pic16f628.
thank you very much again for your help.
I dont see the time to have the reader and the rfid chips with me.
Kind Regards,
Re: How to compare strings/array? RFID Project
i have also one more question please.
I need to use the ID-12 with a display also. Can i use the pic16f628a to run both?
thank you very much.
Re: How to compare strings/array? RFID Project
Quote:
Originally Posted by
astanapane
i have also one more question please.
I need to use the ID-12 with a display also. Can i use the pic16f628a to run both?
thank you very much.
Go read your manual about LCDOUT, it explains in a thorough treatise exactly how to do that. I am perceiving this to be a school project, and it would be rather easy to hand you the code, I think that would do you more harm than good.
What you learn here you can then teach to someone else, that is how this forum works, a great many of the members ARE engineers, others are just interested hobbyist, and IF you are a student, as I believe, I do not want to enable you to fail and NO I do not mean fail the class. Write your code, Make the mistakes, Show us the mistakes, and receive help.
Re: How to compare strings/array? RFID Project
sorry Joe.
i dont want to start that with student or not. If you think that i'm a student you might be right.
A student that is graduated from E.E.E. and i didnt like it then gradueated with MSc DATA COMS with Distinction and i only know to do CISCO. If someone will ask me about cisco i will help him with all my stength and examples.
Sorry i didnt like a lot my first degree thats why i dont know how to use programming very well.
Right now i;m working for a reseller company of Stratasys which makes the FORTUS RP systems.
I only want to make a small project for my father's home.
I'm not a programmer or student sorry....i just do not have enouph time to spend on this.
I will try by my self and i will ask for your help.
thanks.