PDA

View Full Version : Parallax RFID Reader code example



dan-tron
- 26th August 2007, 18:45
I was having troubles getting the Parallax RFID Reader module to work in my project, which you can read about in my thread here...
http://www.picbasic.co.uk/forum/showthread.php?t=6963
My main problem turned out to be that my compiler was too old and the EEPROM READ comand was not working properly. Here's my WORKING code that was 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!



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

mackrackit
- 26th August 2007, 20:41
Thanks for sharing.

ecoli-557
- 9th April 2013, 00:00
Hello All-
I am trying to use the example given above in Dan-Tron's post but using different PIC but I am having trouble. I can read from the Parallax reader and display the info on the LCD but when it gets to the actual compare it breaks down. Even when it reads and compares all 10 chars.
I have stared and work with this all weekend and I have added remarks and hopefully enough data so if anyone spots where I am a complete loser and can't see the problem I would appreciate it very much.
The full code is listed below:



'************************************************* *********************
'* Name : RFID.PBP *
'* Authors : Steve Scott *
'* *
'* *
'* : All Rights Reserved *
'************************************************* *********************
'Design Notion
'Ver 0.1 - initial code
'wait for $0A then recieve either 10 bytes of data or stop once it recieves $0D, which ever comes first.
'tag var BYTE[10] ' Creates a byte array of 10 bytes
'SERIN Rx, T2400,[WAIT ($0A),STR tag\10\$0D]
'$0A-digit1-digit2-digit3-digit4-digit5-digit6-digit7-digit8-digit9-digit10-$0D
'2400 baud, 8 bits, no parity, 1 stop, least significant bit 1st.
'Want to 'enroll' new cards to be authorized from a central location and broadcast updated card information
'via a mesh network around the house.
'PROBLEMS-
'I can read the card just fine (I think) and I can read the EEPROM location for all of the stored cards
'BUT, when I do it with the compare it does not work.
'I put an LCD on the system to see information because I used a serial port to interface a USB (FT232RL)
'and I don't seem to be able to get serial data to a realterm client on the Win 7 PC (no hyperterminal in Win7).
'I have debugging information as REMarked statements where applicable - maybe this will jar my brain......
'
'************************************************* ************************************************** *************
'Processor used is Microchip PIC18F8680 running 40Mhz oscillator.
'RFID is Parallax serial unit
'Using PBP3 GOLD and MicroCode Studio
'MPLAB USB programmer
'************************************************* ************************************************** *************
'Internals Overview:

'************************************************* ************************************************** *************
'************************************************* ************************************************** *************
'Assign address values for all of the configuration registers for the purpose of table reads
'_DEVID1 EQU H'3FFFFE' ' This is hardcoded by manufacturer to indicate processor info
'_DEVID2 EQU H'3FFFFF' ' This is hardcoded by manufacturer to indicate processor info

'I like this new command!
#CONFIG
__CONFIG _CONFIG1H, _OSC_ECIO_1H & _OSCS_OFF_1H
; Oscillator switching not enabled, EC w/OSC2 as RA6
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_45_2L
; Brown out enabled at 4.5 volts, power-up timer enabled
__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
; Watchdog timer disabled with postscaler = 1:1
; __CONFIG _CONFIG3L, _MODE_MC_3L & _WAIT_OFF_3L
; Disable external wait for table functions, set to microcontroller mode
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _ECCPMX_PORTE_3H & _CCP2MX_OFF_3H
; Use /MCLR instead of as RG5, Set ECCPMX, CCP2MX to Port E
__CONFIG _CONFIG4L, _STVR_ON_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
; Turn off debug pins, stack full/underflow will cause reset
;Not used - @ __CONFIG _CONFIG4H,
__CONFIG _CONFIG5L, _CP0_ON_5L & _CP1_ON_5L & _CP2_ON_5L & _CP3_ON_5L
; Code Protect On for all of memory (Blocks 0 - 3)
__CONFIG _CONFIG5H, _CPB_ON_5H & _CPD_ON_5H
; Code Protect On for all of memory (EEPROM and Boot)
;'@ __CONFIG _CONFIG6L, _WRT0_ON_6L & _WRT1_ON_6L & _WRT2_ON_6L & _WRT3_ON_6L
; Flash program memory protect On for all of memory
;'@ __CONFIG _CONFIG6H, _WRTB_ON_6H & _WRTC_ON_6H & _WRTD_ON_6H
; Flash program memory protect On for all of memory (EEPROM, Boot and Config Register)
;'@ __CONFIG _CONFIG7L, _EBTR0_ON_7L & _EBTR1_ON_7L & _EBTR2_ON_7L & _EBTR3_ON_7L
; Disable Table Read protection
;'@ __CONFIG _CONFIG7H, _EBTRB_ON_7H
; Disable Boot Block Table Read protection

;Set ID Bits For Automatic Configuration
; __IDLOCS _IDLOC0, $49 ; Configure ascii to say 'Imagine!'
; __IDLOCS _IDLOC1, $6D ;
; __IDLOCS _IDLOC2, $61 ;
; __IDLOCS _IDLOC3, $67 ;
; __IDLOCS _IDLOC4, $69 ;
; __IDLOCS _IDLOC5, $6E ;
; __IDLOCS _IDLOC6, $65 ;
; __IDLOCS _IDLOC7, $21 ;
#ENDCONFIG

'Add INCLUDE files here...................
include "modedefs.bas"

'************************************************* ************************************************** *************
' Define Declarations
DEFINE OSC 40 ' A 40MHz oscillator is used for this processor
' LCD stuff here
DEFINE LCD_DREG PORTJ
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTG
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTJ
DEFINE LCD_EBIT 7
DEFINE LCD_RWREG PORTG
DEFINE LCD_RWBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 300 ' Stop 300ms
' DEBUG stuff here even though it does not seem to work with the USB bridge
define DEBUG_REG PORTC ' Sets the serial/USB bridge output to PORTC
define DEBUG_BIT 6 ' Sets the output to bit 6 - TXD
define DEBUG_BAUD 9600 ' Sets baud
define BEBUG_MODE 0 ' Sets 0=true or 1=inverted
'************************************************* ************************************************** *************
' Constant Declarations
'************************************************* ************************************************** *************
' Alias Declarations
STAT1 VAR PORTA.6 ' STAT1 Status LED - active high
STAT2 VAR PORTA.4 ' STAT2 Status LED - active low
RFID_en var PORTA.5 ' Low ENABLES reader
RFID_data var PORTB.1 ' Serial data from reader
Lock var PORTB.0 ' Controlled lock
Lock_LED var PORTA.4 ' System ready

'************************************************* ************************************************** *************
' -----[ Variables ]-------------------------------------------------------
x var byte ' Junk counter variable
buf VAR byte [10] ' RFID bytes buffer
cardNum VAR Byte ' from EEPROM table
idx VAR Byte ' card byte index
char VAR Byte ' character from table

' -----[ EEPROM Data ]-----------------------------------------------------
'The card data was taken from reading and displaying the card data on the LCD

'Reads and compares only the 1st 8 chars
card1 DATA "5400826125"
'Reads and compares only the 1st 2 chars
card2 DATA "6500017041"
'Reads and compares only the 1st 2 chars
card3 DATA "0603222230"
'Reads and compares only the 1st 2 chars
card4 DATA "6500021219"
'Reads and compares ALL 10 chars
card5 DATA "5400819186"

' Jump to program initialization
GoTo Initialization

'************************************************* ************************************************** *************
'************************************************* ************************************************** *************
' Start program execution by initializing all required variables and machine states
Initialization:
'Hookay Boyz, here we go again!
LCDOUT $FE, $01 ' Initialize the LCDOUT function for PBP
LCDOUT $FE, $80 ' Go to the first character position of the first line
LCDOUT "Initializating... " ' Write text on first line
LCDOUT $FE, $C0 ' Go to the first character position of the second line
LCDOUT " " ' Move cursor to 2nd line and write text
stat1=1
INTCON = 0 'Disable all interrupts and clear all interrupt flags
TRISA.4 = 0 'Set all externally accessible mainboard I/O ports, this is an OUTPUT
TRISA.5 = 0
TRISA.6 = 0
TRISB.0 = 0
TRISB.1 = 1
TRISC.5 = 1
TRISG.1 = 1
TRISG.3 = 0
TRISG.4 = 0
TRISD = $FF
TRISE = $FF
TRISF = $FF
TRISH = $FF
TRISJ = $0 ' Set as OUTPUTS
ADCON1 = %00111011 'Set AN0 - AN3 to be the only analog inputs, External VRef+/VRef
CMCON = %00000111 'Turn off Comparator for PortF.1 & PortF.2
PSPCON.4 = 0 'Turn off Parallel Slave Port functionality on Port D
CCP1CON = %00000000 'Turn off CCP1 function
CCP2CON = %00000000 'Turn off CCP2 function

' Setup the main system loop timer
T0CON = %01000000 'Set Timer 0 to disabled, 8-bit operation, internal ClkOut clock, prescaled as
'constant'd above
INTCON.5 = 0 'Disable the interrupt for the main system loop
'timer (Timer 0)
' -----[ Port Initialization ]--------------------------------------------------
stat1=0
stat2=1
high rfid_en ' turn off RFID reader PORT A5 is the reciever port
LOW lock ' lock active
high lock_led ' Turn off LED
goto Main

'------------------------------------------------------------------------------------------------------------

'************************************************* ************************************************** *************
Main:
'************************************************* ************************************************** *************
lcdout $FE,1, " READY "
pause 500
low lock_led ' Turn ON door LED
low rfid_en ' activate the reader
pause 500
SERIN2 rfid_data, 396, [WAIT($0A),STR buf\10]
high lock_led ' Turn OFF door LED
pause 500
high rfid_en ' deactivate reader
for idx=0 to 9
lcdout $FE,$80+idx, hex1 buf[idx]
next idx
lcdout $FE,$80+12,"CARD"
'At this point we have sucessfully displayed the read card
Check_List:
FOR cardNum= 5 to 5 ' scan through known cards - OR USE AS A DEBUG
for idx=0 to 9
read (((cardNum-1)*10)+idx), char
lcdout $FE,$C0+idx, char
next idx
lcdout $FE,$C0+12,"EEPROM"," ",dec1 cardnum
pause 500
'At this point we sucessfully display the 1st line is the read card 10 digits
'The second line is the 1st EEPROM value
FOR idx= 0 TO 9 ' scan bytes in card
READ (((cardNum-1)*10)+idx), char ' get card data from table
lcdout $FE,$94+idx, char
lcdout $FE,$94+12,"COMPARE"
lcdout $FE,$D4, "B4 compare, idx=",dec1 idx
pause 500
IF (char <> buf(idx)) THEN BadChar ' compare card to table
pause 500
'This seems to be where it breaks down. I can read from EEPROM and can read a card
'but I don't seem to get this to work.
'On some cards it gets only 2 chars then breaks to a new cardNum and thus data
'On a TAG it will read all 10 chars but NEVER falls through to the CARDFOUND label!
'It flows on to BadChar and since there is no other card it falls through and says
'INVALID CARD - even if it reads and compares ALL 10 chars - like the last card!!
NEXT idx
'I never get to this next line even if it reads and compares ALL 10 chars!!!!!
lcdout $FE,$D4, "@ last idx,CARDFOUND"
pause 2000
GOTO CardFound ' all bytes match!

BadChar: ' try next card
NEXT cardnum

BadCard:
FREQOUT PORTC.5, 1000 */ $100, 115 */ $100 ' groan
lcdout $FE,$D4, "Invalid CARD "
pause 5000
GOTO Main

CardFound:
HIGH lock ' Open lock
low lock_led ' Light LED
FREQOUT PORTC.5, 2000 */ $100, 880 */$100 ' beep
LOW lock ' Reset lock
high lock_led ' LED OFF
GOTO Main



Stop ' Kill the program if it gets this far

Demon
- 9th April 2013, 00:10
Just a guess, do you need a PAUSE after READ?

Robert

ecoli-557
- 19th April 2013, 22:16
Thanks Robert. It ended up not being a PUSE after a READ but it was the bad data I was getting from the LCD - maybe it was too slow - I don't know.
Out of desperation, I used the SEROUT as a DEBUG since the DEBUG doesn't seem to work with a USB bridge.
I used the SEROUT data and it worked 1st time!
Thanks for the help and interest,
Regards!