PDA

View Full Version : Strange reading from RFID..need help



tacbanon
- 17th November 2011, 07:42
Hi, Sorry to post this here but I've been trying to read from an rfid module on PortC.7 and wanted it to send the data to pc using CDC. My problem is everytime I swipe the card it gives me different values..


'************************************************* ***************
'* Name : RFID_Test.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 11/14/2011 *
'* Version : 1.0 *
'* Notes : Pic18F4550, RFID, Servo Motor, Button *
'* : Interrupt on Hardware usart Recieve *
'************************************************* ***************


asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50


DEFINE OSC 48
DEFINE HSER_RCSTA 90h ' enable serial port,
define HSER_TXSTA 24h ' enable transmit,
define HSER_SPBRG 25 ' set baudrate to 9600
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

TRISC = 000000 ' PORTC.7 is the RX input, PORTC.6 is the TX output


' Serial communication definition
' ===============================
'
ADCON1 = 001111 'Set up ADCON1 register no matter what you're doing!!!!!!
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
INCLUDE "cdc_desc.bas" 'Descriptor file for CDC serial demo
INCLUDE "MODEDEFS.BAS" ' Include Shiftin/out modes
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
' Variable definition
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::

RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)
TRISC = 100000
PortC = 000000
led var PORTC.0
led1 var PORTC.1
led2 var PORTC.2
holdoff var word
SerialData var byte[15]
DataRec var byte[15]
buffer var byte[20]
flag1 var byte
flag1 = 0
cnt var byte
cnt1 var byte
cnt = 5
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RX_INT, _Getbytes, PBP, no
INT_Handler USB_INT, _DoUSBSERVICE, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM


;----[Initialise USB and Interrupts]----------------------------------------
PAUSE 100 ; Allow VUSB to stabilize
USBINIT ; initialize the USB driver
USBSERVICE ; service it once
UIE = $7F ; enable USB interrupts
UEIE = $9F ; enable USB Error interrupts
@ INT_ENABLE USB_INT




@ INT_ENABLE RX_INT ; enable RX_INT interrupts





USBInit
USBService
Mainloop:

USBService ' Must service USB regularly
Lcdout $fe, 1 ' Clear screen
Lcdout $fe, 128, "Val: "
toggle led 'toggle led every loop for heartbeat
pause 100
if flag1 = 1 then
Gosub readser
endif
'*****************INSERT**********************
cnt1 = 1
USBIn 3, buffer, cnt1, Mainloop
toggle led1
Lcdout $fe, 128, "Hello World"
pause 200
'*****************INSERT**********************

goto Mainloop






;--------['ISR for RX_int interrupt]-------------------------------------

Getbytes:
'###########################################


While RCIF = 1 ' clear the buffer
HSERIN 100,error,[SerialData] ' take it
Wend
High led2 'led to confirm program went to RX ISR
flag1 = 1



@ INT_RETURN


;----[Interrupt handler -- Service USB]-------------------------------------
DoUSBSERVICE:
USBSERVICE ; Run the SERVICE routines
@ INT_RETURN


error:
Toggle led1
@ INT_RETURN


usbtx:
USBOut 3, DataRec,5,usbtx
return






readser:
serin2 PortC.7, 84,[ DataRec]
Lcdout $fe, 1 ' Clear screen
Lcdout $fe, 128, "Val: "
'Lcdout $fe, $c0, STR DataRec\13
if DataRec > 0 then
LCdout $fe, $c0,#DataRec
gosub usbtx
pause 500
Lcdout $fe, 1 ' Clear screen
Lcdout $fe, 128, "Val:"
flag1=0
Low Led2
ENdif
Return

end



Hardwre: Pic18F4550,@20Mhz, PBP2.6
I appreciate any help.

tacbanon
- 20th November 2011, 02:49
Hi, can anyone point me to the right direction on how to capture data(codes from previous post) from HSERIN 100,error,[SerialData] to variable DataRec? I did some variations just to make it display on the LCD, it seems I can not. I appreciate any help(it's drving me nuts:().


Getbytes:
'###########################################

cnt=0
While RCIF = 1 ' clear the buffer
HSERIN 10,error,[Serialdata] ' take it
DataRec[cnt]=SerialData[cnt]
HSEROUT [DataRec[cnt]] ' take out
cnt=cnt+1
Wend
LCdout $fe, $c0, DataRec
Toggle led2 'led to confirm program went to RX ISR
flag1 = 1



Thanks in advance,
tacbanon

mackrackit
- 20th November 2011, 11:08
Something like this might work better, I think.
http://melabs.com/samples/PBP-mixed/usart.htm
(http://melabs.com/samples/PBP-mixed/usart.htm)

But if you go with what you have can you display "Serialdata" to the LCD?

tacbanon
- 20th November 2011, 14:47
Hello Dave, thanks for replying. I also tried to display Serialdata to LCD but no luck..this moved me to copy the content to DataRec variable but I also realized that this does not working also...I will check out the link you gave.

thanks and regards,
tacbanon