Created
October 9, 2011 18:13
-
-
Save maniacbug/1273970 to your computer and use it in GitHub Desktop.
Complete example for reading the 125Khz RFID module RDM630
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
// Pin definitions | |
const int rfid_irq = 0; | |
const int rfid_tx_pin = 6; | |
const int rfid_rx_pin = 7; | |
// For communication with RFID module | |
SoftwareSerial rfid(rfid_tx_pin, rfid_rx_pin); | |
// Indicates that a reading is now ready for processing | |
volatile bool ready = false; | |
// Buffer to contain the reading from the module | |
uint8_t buffer[14]; | |
uint8_t* buffer_at; | |
uint8_t* buffer_end = buffer + sizeof(buffer); | |
void rfid_read(void); | |
uint8_t rfid_get_next(void); | |
void setup(void) | |
{ | |
// Open serial connection to host PC to view output | |
Serial.begin(57600); | |
Serial.println("rfid_decimal"); | |
// Open software serial connection to RFID module | |
pinMode(rfid_tx_pin,INPUT); | |
rfid.begin(9600); | |
// Listen for interrupt from RFID module | |
attachInterrupt(rfid_irq,rfid_read,FALLING); | |
} | |
void loop(void) | |
{ | |
if ( ready ) | |
{ | |
// Convert the buffer into a 32-bit value | |
uint32_t result = 0; | |
// Skip the preamble | |
++buffer_at; | |
// Accumulate the checksum, starting with the first value | |
uint8_t checksum = rfid_get_next(); | |
// We are looking for 4 more values | |
int i = 4; | |
while(i--) | |
{ | |
// Grab the next value | |
uint8_t value = rfid_get_next(); | |
// Add it into the result | |
result <<= 8; | |
result |= value; | |
// Xor it into the checksum | |
checksum ^= value; | |
} | |
// Pull out the checksum from the data | |
uint8_t data_checksum = rfid_get_next(); | |
// Print the result | |
Serial.print("Reading: "); | |
Serial.print(result); | |
if ( checksum == data_checksum ) | |
Serial.println(" OK"); | |
else | |
Serial.println(" CHECKSUM FAILED"); | |
// We're done processing, so there is no current value | |
ready = false; | |
} | |
} | |
// Convert the next two chars in the stream into a byte and | |
// return that | |
uint8_t rfid_get_next(void) | |
{ | |
// sscanf needs a 2-byte space to put the result but we | |
// only need one byte. | |
uint16_t result; | |
// Working space to assemble each byte | |
static char byte_chars[3]; | |
// Pull out one byte from this position in the stream | |
snprintf(byte_chars,3,"%c%c",buffer_at[0],buffer_at[1]); | |
sscanf(byte_chars,"%x",&result); | |
buffer_at += 2; | |
return static_cast<uint8_t>(result); | |
} | |
void rfid_read(void) | |
{ | |
// Only read in values if there is not already a value waiting to be | |
// processed | |
if ( ! ready ) | |
{ | |
// Read characters into the buffer until it is full | |
buffer_at = buffer; | |
while ( buffer_at < buffer_end ) | |
*buffer_at++ = rfid.read(); | |
// Reset buffer pointer so it's easy to read out | |
buffer_at = buffer; | |
// Signal that the buffer has data ready | |
ready = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment