'*****************************************************************************
'* Name : RFID Library *
'* Author : David John Barker *
'* Notice : Copyright (c) 2012 Mecanique *
'* : All Rights Reserved *
'* Date : 03/03/2013 *
'* Version : 1.0 *
'* Notes : *
'* : *
'*****************************************************************************
Module RFID
' Uart interface...
imports UartTypes ' uart structures
private _uBRG As U2BRG ' baud rate generator
private _uSta as USTAReg absolute Pic.U2STA ' status and control register
private _mode as UMODEReg absolute Pic.U2MODE ' uart mode
private _rcRegister As U2RXREG ' recieve register
private _txRegister As U2TXREG ' transmit register
' RFID interface...
private _Rts as D2 ' RFID RTS line
private const RFID_OK = &H86
private const RFID_WRITE_EEPROM = &H50
private const RFID_STATUS = &H53
private const RFID_WRITE_BLOCK = &H57
private const RFID_READ_BLOCK = &H52
private const RFID_UID = &H55
'****************************************************************************
'* Name : SetBaudrate *
'* Purpose : Sets the hardware USART baudrate *
'****************************************************************************
private Sub SetBaudrate()
_mode.Value = &H8800 ' mode (UARTEN = 1, RTSMD = 1)
_mode.BRGH = 1
_uSta.UTXEN = 1
_uBRG =((_clock * 1000000 / 2) / 4 / 9600) - 1
End Sub
'****************************************************************************
'* Name : ReadByte *
'* Purpose : Read a Byte from the hardware USART *
'* : Waits for USART data, returns byte in RCREG(x) *
'****************************************************************************
private Function ReadByte() As _rcRegister
' clear any possible overruns
_uSta.OERR = 0
' now read data...
Do
ClearWDT
Loop Until _uSta.URXDA = 1 ' have data = 1
End Function
'****************************************************************************
'* Name : WriteByte *
'* Purpose : Write a byte value to the hardware USART *
'* : Wait until ready to send is enabled, then send byte *
'****************************************************************************
private Sub WriteByte(value As byte)
Do
ClearWDT
Loop Until _uSta.TRMT = 1 ' busy = 0
_txRegister = value
End Sub
'****************************************************************************
'* Name : WaitForLow *
'* Purpose : Wait for the RTS line to go low *
'****************************************************************************
private inline sub WaitForLow()
while _Rts = 1
end while
end sub
'****************************************************************************
'* Name : WaitForHigh *
'* Purpose : Wait for the RTS line to go high *
'****************************************************************************
private inline sub WaitForHigh()
while _Rts = 0
end while
end sub
'****************************************************************************
'* Name : ReadArray *
'* Purpose : Read an array from the RFID *
'****************************************************************************
private sub ReadArray(byref array() as byte)
dim index as byte = 0
while index <= ubound(array) and _rts = 1
array(index) = ReadByte
index += 1
end while
end sub
'****************************************************************************
'* Name : WriteArray *
'* Purpose : Write and array to the RFID *
'****************************************************************************
private sub WriteArray(byref array() as byte)
dim index as byte = 0
while index <= ubound(array)
WriteByte(array(index))
index += 1
end while
end sub
'****************************************************************************
'* Name : WriteEEPROM *
'* Purpose : Write a data value to EEPROM *
'****************************************************************************
public function WriteEEPROM(address as byte, data as byte) as boolean
WaitForLow()
WriteByte(RFID_WRITE_EEPROM)
WriteByte(address)
WriteByte(data)
WaitForHigh()
return ReadByte() = RFID_OK
end function
'****************************************************************************
'* Name : WriteBlock *
'* Purpose : Write a block of data to the RFID tag *
'****************************************************************************
public function WriteBlock(cardBlock as byte, byref array() as byte) as boolean
WaitForLow()
WriteByte(RFID_WRITE_BLOCK)
WriteByte(cardBlock)
WriteByte(0)
WriteArray(array)
WaitForHigh()
return ReadByte() = RFID_OK
end function
'****************************************************************************
'* Name : ReadBlock *
'* Purpose : Read a block of data from the RFID tag *
'****************************************************************************
public function ReadBlock(cardBlock as byte, byref array() as byte) as boolean
WaitForLow()
WriteByte(RFID_READ_BLOCK)
WriteByte(cardBlock)
WriteByte(0)
WaitForHigh()
ReadBlock = (ReadByte = RFID_OK)
if ReadBlock then ReadArray(array)
end function
'****************************************************************************
'* Name : ReadUID *
'* Purpose : Read the Unique ID (UID) from the RFID tag *
'****************************************************************************
public function ReadUID(byref array() as byte) as boolean
WaitForLow()
WriteByte(RFID_UID)
WaitForHigh()
ReadUID = (ReadByte = RFID_OK)
if ReadUID then ReadArray(array)
end function
'****************************************************************************
'* Name : InRange *
'* Purpose : Return true if an RFID tag is in range, false otherwise *
'****************************************************************************
public function InRange() as boolean
WaitForLow()
WriteByte(RFID_STATUS)
WaitForHigh()
InRange = (ReadByte = RFID_OK)
end function
' module initialisation...
private inline Sub Main()
RPINR19 = RPINR19 and &HFFE0 or &B00001 ' map Uart2 RX to A1 (RB.1)
RPOR0 = RPOR0 and &HFFE0 or &B00101 ' map Uart2 TX to A2 (RB.0)
SetBaudrate()
End Sub
End Module
Users browsing this forum: No registered users and 1 guest