Shop OBEX P1 Docs P2 Docs Learn Events
RFID Gun — Parallax Forums

RFID Gun

sanbogsilentsanbogsilent Posts: 3
edited 2007-04-03 19:02 in General Discussion
I'm trying to attach an RFID system to a airsoft gun for a school project. The idea is that to fire the gun you must be have the RFID tag (we are putting it in a ring or something of that nature) on you before you can fire the gun. We are using a Parallax RFID reader and a PIC16f877a micro controller.

I was wondering if you guys could give me some feed back on my code. (It's in Assembly, which i'm not as proficient with as i need to be.) Or atleast tell me if it makes sense.

The idea for the code is, it's scans the tag compares it's value to the accepted tag value already programed in the controller and if it matches outputs a high on one of the pins.


any and all suggestions are greatly appreciated.


;******************
;****List Files****
;******************
    list    p=16f877a, r=hex

;*********************
;****Include Files****
;*********************

#include <p16f877a.inc>

;********************
;****Config Files****
;********************

;Standard Config, Using RC circuit as timer

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_OFF & _RC_OSC & _LVP_ON & _CPD_OFF    

;********************
;*****Constants******
;********************



;*****************
;****Variables****
;*****************
IDMEMORY        equ 50h    ;Memory block for input ID tags, 50h-5Ch
        cblock IDMEMORY
tag0i    
tag1i    
tag2i    
tag3i    
tag4i    
tag5i    
tag6i    
tag7i    
tag8i    
tag9i    
tag10i    
tag11i    
        endc

ALLOWID        equ    20h    ;Memory block for 'allowed users'
    
        cblock    ALLOWID
tag0    ;0x0A
tag1    ;0x00
tag2    ;0x04
tag3    ;0x01
tag4    ;0x05
tag5    ;0x0A
tag6    ;0x0B
tag7    ;0x06
tag8    ;0x06
tag9    ;0x07
tag10    ;0x0f
tag11    ;0x0D

        endc
        org    0
        goto start
        org5



;******************
;****Main Files****
;******************



start    bsf    SSPCON, 5        ;Enable serial communication
        bsf SSPCON, B'0100'    ;SPI to slave mode, clock is SCK pin, SSbar pin control EN
        bsf STATUS, 5        ;Move to Memory Bank 1
        movlw B'00010000'     ;Move the number that controls PIN C 4 to W
        movwf TRISC        ;Turn on PIN C4 for input
        clrw
        movlw B'00000000'
        movwf TRISD        ;PORT D is all output
        clrw        
        bcf STATUS, 5        ;Go Back to Memory Bank 0
    

        goto LOOP_STORE_DATA

LOOP_STORE_DATA            ;This loop will write the 'allowed' ID to memory starting at 20h
        movlw    0x0A
        movwf    ALLOWID
        clrw
        movlw    0x00
        movwf    ALLOWID+1
        clrw
        movlw    0x04
        movwf    ALLOWID+2
        clrw
        movlw    0x01
        movwf    ALLOWID+3
        clrw
        movlw    0x05
        movwf    ALLOWID+4
        clrw
        movlw    0x0A
        movwf    ALLOWID+5
        clrw
        movlw    0x0B
        movwf    ALLOWID+6
        clrw
        movlw    0x06
        movwf    ALLOWID+7
        clrw
        movlw    0x06
        movwf    ALLOWID+8
        clrw
        movlw    0x07
        movwf    ALLOWID+9
        clrw
        movlw    0x0F
        movwf    ALLOWID+0x0A
        clrw
        movlw    0x0D
        movwf    ALLOWID+0x0B
        clrw
        

        goto    LOOP_MOVE_DATA
    
LOOP_MOVE_DATA                ;This loop repeats 12 times to capture all 12 bytes of ID number

        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf     IDMEMORY+1
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+2
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+3
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+4
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+5
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+6
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+7
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+8
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+9
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+0xA
        
        movf    SSPBUF, W    ;Move Serial Buffer to W
        movwf    IDMEMORY+0xB
        clrw

        goto    LOOP_COMPARE

LOOP_COMPARE    ;The comparison loop subtracts the stored input IDMEMORY
                ;segments from the previously hardcoded ALLOWMEMORY
           clrw
           clrf    40h                ;40h is used for the 'scratch' work
           movf    ALLOWID, W             ;Recall first known byte
          subwf   IDMEMORY, W        ;Subtract first known bit from input bit
          movwf    40h
        btfsc   40h, 0                      ;If the value of 40h is zer, that bit passes the test
          goto    LOOP_MOVE_DATA        ;and will skip this step
                                           ;If subtraction not = 0, then will restart the checking process
        clrw
        clrf    40h
        movf    ALLOWID+1, W
        subwf   IDMEMORY+1, W       
          movwf    40h
        btfss   40h, 0
        goto    LOOP_MOVE_DATA

        clrw
        clrf   40h
        movf   ALLOWID+2, W
        subwf  IDMEMORY+2, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA
                
        clrw
        clrf   40h
        movf   ALLOWID+3, W
        subwf  IDMEMORY+3, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA 

        clrw
        clrf   40h
        movf   ALLOWID+4, W
        subwf  IDMEMORY+4, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA 

        clrw
        clrf   40h
        movf   ALLOWID+5, W
        subwf  IDMEMORY+5, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA 

        clrw
        clrf   40h
        movf   ALLOWID+6, W
        subwf  IDMEMORY+6, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA 

        clrw
        clrf   40h
        movf   ALLOWID+7, W
        subwf  IDMEMORY+7, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA 

        clrw
        clrf   40h
        movf   ALLOWID+8, W
        subwf  IDMEMORY+8, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA 

        clrw
        clrf   40h
        movf   ALLOWID+9, W
        subwf  IDMEMORY+9, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA

        clrw
        clrf   40h
        movf   ALLOWID+0x0A, W
        subwf  IDMEMORY+0x0A, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA

        clrw
        clrf   40h
        movf   ALLOWID+0x0B, W
        subwf  IDMEMORY+0x0B, W       
          movwf    40h
        btfss  40h, 0
        goto   LOOP_MOVE_DATA   

        goto LOOP_ACTIVATE

LOOP_ACTIVATE    ;This will activate the firing mechanism

        movlw    B'11111111'
        movwf    PORTD        ;ouput a high to PORTD, all pins
        nop
        nop
        nop
        nop
        goto    LOOP_MOVE_DATA    ;check the tag again
end
    
        


        
        


Comments

  • Harrison.Harrison. Posts: 484
    edited 2007-04-02 23:53
    Problem #1 is you are using the internal RC clock for your clock source. This probably won't work extremely well since serial usually requires a relatively stable and 'precise' clock source (be it a resonator or crystal). I have used the internal clock for serial projects before and found that sending is not heavily affected by the internal rc oscillator, but serial receiving is highly sensitive to the imprecision of the internal rc oscillator.

    Problem #2 is that you seem to be using the hardware spi port and not the hardware uart. The parallax RFID reader uses asynchronous serial @ 2400 bps. This means that you have to set the SPBRG to a precalculated baud rate (look at the datasheet for a formula). Then use the TXREG & RXREG to get your serial data. Note that you must wait for each byte to come in, not just expect that all of them will be ready when you read from the register.

    You may want to look at using a higher level language than assembly. There are plenty of 'free' compilers out there (limited by size of code, etc). I would recommend that you try to look at Parallax micros (the BS2, Propeller, or SX) since they all come with a free high level language compiler that is easy to use.

    Harrison
  • sanbogsilentsanbogsilent Posts: 3
    edited 2007-04-03 00:18
    ok, thank you for the input, i think we're going to try and get a compiler.

    Do you have any suggestions on where to find a free compiler on-line?
  • crgwbrcrgwbr Posts: 614
    edited 2007-04-03 19:02
    I suggest you get an SX and program it in SX/B. It's faster than a PIC, and comes free with the SX/B complier. The architecture is similar, so it shouldn't be too hard to adapt.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

    Microsoft: "You've got questions. We've got dancing paper clips."
Sign In or Register to comment.