This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:examples:communication:rfid [2009/11/05 15:31] – external edit 127.0.0.1 | en:examples:communication:rfid [2026/02/19 11:31] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== RFID sensor | + | < |
| + | ====== RFID modules | ||
| - | ===== Ühendusskeem | + | //Required knowledge: |
| + | [HW] [[et: | ||
| + | [AVR] [[et: | ||
| + | [LIB] [[et: | ||
| + | |||
| + | ===== Theory | ||
| + | |||
| + | RFID is a technology for data exchange via radio waves between a reader and an electronic identity attached to an object for identification and tracking. RFID allows each object to be associated with a unique ID number. Passive tags without a battery can be read when passing close to the reader. | ||
| + | |||
| + | ===== Mifare SL031 RFID module ===== | ||
| + | {{ : | ||
| + | |||
| + | The Mifare RFID module is a high-frequency RFID reader operating at 13.56 MHz. | ||
| + | At the same frequency, many ID cards and RFID tokens used in Estonian schools and companies also work. | ||
| + | The SL031 module connects to the controller board as follows: | ||
| + | * VCC - supply + 3.3 V (from the Xbee interface of the communication board or the controller board header). Be sure not to use higher supply voltage. The module data pins tolerate + 5 V. | ||
| + | * IN - to save power, the RFID module can be put to sleep by sending command 0x50. The module wakes up on a falling edge on IN. | ||
| + | * TXD - connect to controller RXD (e.g., PE0 for USART0) | ||
| + | * RXD - connect to controller TXD (e.g., PE1) | ||
| + | * OUT - indicates whether an ID is detectable (card near the module, output low) or not (no card, output high) | ||
| + | * GND - ground | ||
| + | |||
| + | |||
| + | |||
| + | When using USART0 to communicate with the RFID module, the RFID TXD/RXD pins must not be connected to the communication board during ISP programming, | ||
| + | |||
| + | Default USART settings for SL031 are 115200-8-N-1-N. When connecting to a computer, if USART0 is used to communicate with the RFID module, the jumpers on the right header pair must be moved from Xbee to RS232_2 and the computer COM port connected to the RS232_2 connector on the communication board. | ||
| + | |||
| + | The data exchange format between the controller and the RFID module consists of several bytes in a fixed order. The first byte is always the same (0xBA when sent by the controller). The second byte indicates how many bytes follow and can be used to track when all data has been sent. The last byte is a checksum that allows verifying correct reception. Different commands are available to write/read memory, put the module to sleep, etc. | ||
| + | |||
| + | The example program sends a command to the RFID module every second to check for a nearby ID card. If found, it displays the unique card ID on the display. Each byte in the exchange is in hex and must be converted to ASCII characters for display. | ||
| + | |||
| + | ===== Mifare SL031 example code ===== | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | usart port = USART(1); | ||
| + | |||
| + | // Command to module - select card | ||
| + | // preamble, len, command, data, checksum | ||
| + | char SL031CMD_SelectCard[] = {0xBA, | ||
| + | |||
| + | void hex_to_ascii(char *mass, char byte); | ||
| + | |||
| + | int main (void) | ||
| + | { | ||
| + | char resp[14]; | ||
| + | char str[4]; | ||
| + | int a = 0; | ||
| + | int bnr = 4; | ||
| + | |||
| + | // USART1 setup | ||
| + | usart_init_async(port, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | // LCD initialization | ||
| + | lcd_gfx_init(); | ||
| + | |||
| + | // Turn on backlight | ||
| + | lcd_gfx_backlight(true); | ||
| + | |||
| + | // Write text to screen | ||
| + | lcd_gfx_goto_char_xy(3, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | while (1) // | ||
| + | { | ||
| + | // Send command to RFID module | ||
| + | usart_send_string(port, | ||
| + | |||
| + | // Wait until response bytes arrive | ||
| + | while (a < bnr) | ||
| + | { | ||
| + | // read from USART and store | ||
| + | if (usart_try_read_char(port, | ||
| + | { | ||
| + | // next byte | ||
| + | | ||
| + | // second byte shows how many bytes follow | ||
| + | if (a == 2) bnr = resp[1] + 2; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // if no ID - "no tag" response | ||
| + | if (resp[3] == 0x01) | ||
| + | { | ||
| + | // write it | ||
| + | lcd_gfx_goto_char_xy(4, | ||
| + | lcd_gfx_write_string(" | ||
| + | } | ||
| + | // ID exists and is readable | ||
| + | else | ||
| + | { | ||
| + | // view only ID bytes | ||
| + | for (a=4; a< | ||
| + | { | ||
| + | // convert read data to string | ||
| + | hex_to_ascii(str, | ||
| + | // write to display | ||
| + | lcd_gfx_goto_char_xy((a-2)*2, | ||
| + | lcd_gfx_write_string(str); | ||
| + | } | ||
| + | } | ||
| + | // reset counters | ||
| + | a = 0; | ||
| + | bnr = 4; | ||
| + | // refresh delay | ||
| + | hw_delay_ms(1000); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Convert a hex byte to ASCII and store in string | ||
| + | void hex_to_ascii(char *mass, char byte) | ||
| + | { | ||
| + | unsigned int high = 0; | ||
| + | |||
| + | // find first hex digit | ||
| + | while (byte > 0x0F) | ||
| + | { | ||
| + | high++; | ||
| + | byte -= 0x10; | ||
| + | } | ||
| + | // first digit, hex to ASCII | ||
| + | mass[0] = high + 0x30; | ||
| + | // if letter | ||
| + | if (mass[0] >= 0x3A) mass[0] += 0x07; | ||
| + | // second digit, hex to ASCII | ||
| + | mass[1] = byte + 0x30; | ||
| + | if (mass[1] >= 0x3A) mass[1] += 0x07; | ||
| + | // string terminator | ||
| + | mass[2] = 0x00; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Parallax RFID module ===== | ||
| + | |||
| + | The Parallax RFID module is a low-frequency RFID reader that operates at ~170 kHz. | ||
| {{: | {{: | ||
| - | ===== Näitekood | + | ===== Parallax RFID example code ===== |
| - | Järgnev kood kasutab | + | The following code uses an RFID reader and LCD display. |
| <code c> | <code c> | ||
| Line 196: | Line 342: | ||
| </ | </ | ||
| - | * {{examples: | + | * {{examples: |