This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:examples:communication:rfid [2012/05/16 13:20] – created raivo.sell | en:examples:communication:rfid [2026/02/19 11:31] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== RFID moodulid | + | < |
| - | <note important> | + | ====== RFID modules |
| - | RFID on tehnoloogia andmevahetuseks raadiolainete kaudu lugeja ja elektroonilise identiteedi vahel, mis on paigaldatud mõne objekti külge identifitseerimise ja jälgimise eesmärgil. RFID võimaldab iga objektiga siduda unikaalse ID-numbri. Passiivsed identiteedid, | + | |
| - | ===== Mifare SL031 RFID moodul ===== | + | //Required knowledge: |
| - | Mifare RFID moodul on kõrgsageduslik RFID lugeja, mis töötab sagedusel 13,56 MHz. | + | [HW] [[et: |
| - | Samal sagedusel töötvad ka mitmed Eesti koolides ja ettevõtetes kasutatavad id-kaardid ning RFID-nööbid. | + | [AVR] [[et: |
| - | Moodul SL031 ühendatakse kontrolleriplaadiga järgmiselt: | + | [LIB] [[et: |
| - | VCC – toide + 3.3 V (sideplaadi Xbee liidesest või kontrolleriplaadi ühendusribast), | + | |
| - | IN – energia säästmiseks saab RFID-mooduli saata uinunud olekusse. Selleks saadetakse talle käsk 0x50. RFID-moodul ärkab üles langeva frondi peale IN sisendis. | + | |
| - | TXD – ühendada kontrolleri RXD viiguga (nt. PE0 USART0 kasutamisel suhtlemiseks) | + | |
| - | RXD – ühendada kontrolleri TXD viiguga (nt. PE1) | + | |
| - | OUT – see väljund näitab, et mõni ID on detekteeritav (kaart mooduli ligidal, väljund all) või mitte (kaarti pole, väljund üleval) | + | |
| - | GND – maaühendus | + | |
| - | USART0 kasutamisel RFID mooduliga suhtlemiseks ei tohi RFID mooduli TXD/RXD viigud ISP programmeerimise ajal olla sideplaadiga ühendatud, sest siis tekib konflikt ja programmeerimine ei õnnestu. | + | ===== Theory === |
| - | USART seaded SL031 puhul on vaikimise 115200-8-N-1-N. Arvutiga ühendamise puhul, kui RFID-mooduliga suhtlemiseks kasutatakse USART0 liidest, siis jumperid parempoolsel piikribapaaril tuleb tõsta Xbee pealt RS232_2 peale ja arvuti COM port ühendada sideplaadi RS232_2 pistikusse. | + | 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. |
| - | Andmevahetuse formaat kontrolleri ja RFID-mooduli vahel koosneb mitmest baidist, mis on kindlas järjekorras. Esimene bait on alati sama (kontrollerist saatmise puhul 0xBA). Teine bait näitab, mitu baiti veel pärast seda saadetakse ja seda on näiteks võimalik kasutada andmevahetuse jälgimiseks, | + | ===== Mifare SL031 RFID module ===== |
| + | {{ : | ||
| - | Näiteprogramm saadab iga sekundi järel käsu RFID moodulile, et teada, kas sellel on mõni ID kaart avastataval ligidusel. Kui on, siis kuvab kaardi unikaalset | + | The Mifare |
| + | 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 | ||
| - | ===== Mifare SL031 näitekood | + | |
| + | |||
| + | 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> | <code c> | ||
| Line 30: | Line 42: | ||
| #include < | #include < | ||
| - | usart port = USART(1); | + | usart port = USART(1); |
| - | // Käsk moodulile | + | // Command to module |
| // preamble, len, command, data, checksum | // preamble, len, command, data, checksum | ||
| char SL031CMD_SelectCard[] = {0xBA, | char SL031CMD_SelectCard[] = {0xBA, | ||
| Line 52: | Line 64: | ||
| | | ||
| - | // LCD ekraani algseadistamine | + | // LCD initialization |
| lcd_gfx_init(); | lcd_gfx_init(); | ||
| - | // Taustavalgustuse tööle lülitamine | + | // Turn on backlight |
| lcd_gfx_backlight(true); | lcd_gfx_backlight(true); | ||
| - | // Ekraanile teksti kirjutamine | + | // Write text to screen |
| lcd_gfx_goto_char_xy(3, | lcd_gfx_goto_char_xy(3, | ||
| lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
| Line 66: | Line 78: | ||
| while (1) // | while (1) // | ||
| { | { | ||
| - | // Saada käsk RFID moodulile | + | // Send command to RFID module |
| usart_send_string(port, | usart_send_string(port, | ||
| | | ||
| - | // kuni vastuse andmebaidid kohal | + | // Wait until response bytes arrive |
| while (a < bnr) | while (a < bnr) | ||
| { | { | ||
| - | // loeb USART-ist andmeid ja salvestab | + | // read from USART and store |
| if (usart_try_read_char(port, | if (usart_try_read_char(port, | ||
| { | { | ||
| - | // | + | // |
| a++; | a++; | ||
| - | // | + | // |
| if (a == 2) bnr = resp[1] + 2; | if (a == 2) bnr = resp[1] + 2; | ||
| } | } | ||
| } | } | ||
| - | // kui ID puudub | + | // if no ID - "no tag" |
| if (resp[3] == 0x01) | if (resp[3] == 0x01) | ||
| { | { | ||
| - | // kirjuta seda | + | // write it |
| lcd_gfx_goto_char_xy(4, | lcd_gfx_goto_char_xy(4, | ||
| - | lcd_gfx_write_string(" | + | lcd_gfx_write_string(" |
| } | } | ||
| - | // ID on olemas ja loetav | + | // ID exists and is readable |
| else | else | ||
| { | { | ||
| - | // vaata ainult | + | // view only ID bytes |
| for (a=4; a< | for (a=4; a< | ||
| { | { | ||
| - | // teisendab loetud andmed stringiks | + | // convert read data to string |
| hex_to_ascii(str, | hex_to_ascii(str, | ||
| - | // kirjuta ekraanile | + | // write to display |
| lcd_gfx_goto_char_xy((a-2)*2, | lcd_gfx_goto_char_xy((a-2)*2, | ||
| lcd_gfx_write_string(str); | lcd_gfx_write_string(str); | ||
| } | } | ||
| } | } | ||
| - | // loendurid algusesse | + | // reset counters |
| a = 0; | a = 0; | ||
| bnr = 4; | bnr = 4; | ||
| - | // viide uuendamiseks | + | // refresh delay |
| hw_delay_ms(1000); | hw_delay_ms(1000); | ||
| } | } | ||
| } | } | ||
| - | // Teisendab | + | // Convert a hex byte to ASCII and store in string |
| void hex_to_ascii(char *mass, char byte) | void hex_to_ascii(char *mass, char byte) | ||
| { | { | ||
| unsigned int high = 0; | unsigned int high = 0; | ||
| - | // leiab esimese numbrikoha | + | // find first hex digit |
| while (byte > 0x0F) | while (byte > 0x0F) | ||
| { | { | ||
| Line 121: | Line 133: | ||
| byte -= 0x10; | byte -= 0x10; | ||
| } | } | ||
| - | // esimene numbrikoht, hex teisendada | + | // first digit, hex to ASCII |
| mass[0] = high + 0x30; | mass[0] = high + 0x30; | ||
| - | // kui tähemärk | + | // if letter |
| if (mass[0] >= 0x3A) mass[0] += 0x07; | if (mass[0] >= 0x3A) mass[0] += 0x07; | ||
| - | // teine numbrikoht, hex teisendada | + | // second digit, hex to ASCII |
| mass[1] = byte + 0x30; | mass[1] = byte + 0x30; | ||
| if (mass[1] >= 0x3A) mass[1] += 0x07; | if (mass[1] >= 0x3A) mass[1] += 0x07; | ||
| - | // stringi terminaator | + | // string terminator |
| mass[2] = 0x00; | mass[2] = 0x00; | ||
| } | } | ||
| </ | </ | ||
| - | ===== Parallax RFID moodul | + | ===== Parallax RFID module |
| - | Parallaxi | + | The Parallax |
| {{: | {{: | ||
| - | ===== Parallax RFID 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 330: | Line 342: | ||
| </ | </ | ||
| - | * {{examples: | + | * {{examples: |