This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:examples:communication:bluetooth [2011/04/04 21:49] – Bluetooth õpetus ja näiteprogramm lisatud eero.valjaots | et:examples:communication:bluetooth [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| //Vajalikud teadmised: [HW] [[et: | //Vajalikud teadmised: [HW] [[et: | ||
| + | |||
| + | **Eunistone mooduli puhul vaata siia:** | ||
| + | [[et/ | ||
| ===== Teooria ===== | ===== Teooria ===== | ||
| Line 29: | Line 32: | ||
| Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid Bluetooth Bee vahendusel kontrollerile. Ühenduse katkestamiseks tuleb seda teha arvutist või tekitada kontrolleriga Bluetooth Bee PIO0 sisendisse tõusev front. | Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid Bluetooth Bee vahendusel kontrollerile. Ühenduse katkestamiseks tuleb seda teha arvutist või tekitada kontrolleriga Bluetooth Bee PIO0 sisendisse tõusev front. | ||
| + | |||
| + | Järgnev kood selgitab mooduli kasutamist analoogselt ZigBee näitele. Siin seatakse Bluetooth Bee ülemaks seadmeks (seda on vaja teha ainult esimesel korral, nagu ka pin-koodi, mooduli nime jm. seadete sisestust) ja luuakse selle abil ühendus mõne muu seadmega. | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| - | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| Line 54: | Line 58: | ||
| char btadr[16] = {' | char btadr[16] = {' | ||
| ',',' | ',',' | ||
| - | |||
| int main(void) | int main(void) | ||
| { | { | ||
| - | char tekst[24]; | + | |
| - | int a = 0; | + | int a = 0; |
| - | // seadista LEDide ja nuppude I/O pordid | + | |
| for (int i=0; i<3; i++) | for (int i=0; i<3; i++) | ||
| { | { | ||
| Line 81: | Line 84: | ||
| lcd_gfx_backlight(true); | lcd_gfx_backlight(true); | ||
| - | // Bluetooth Bee seadistamine ülemaks seadmeks | + | |
| - | // seda on vaja teha ainult üks kord, | + | // seda on vaja teha ainult üks kord, |
| - | // järgmine kord moodul juba mäletab seadeid | + | // järgmine kord moodul juba mäletab seadeid |
| lcd_gfx_clear(); | lcd_gfx_clear(); | ||
| - | // saada moodulile käsk | + | |
| - | usart_send_string(port," | + | usart_send_string(port," |
| - | // loeb kuni õige vastus tuleb | + | // loeb kuni õige vastus tuleb |
| - | while (tekst[9] != ' | + | while (tekst[9] != ' |
| - | { | + | { |
| - | //loe üks mooduli saadetud string | + | //loe üks mooduli saadetud string |
| - | bluetooth_read_string(port, | + | bluetooth_read_string(port, |
| - | //kirjuta ekraanile | + | //kirjuta ekraanile |
| - | lcd_gfx_goto_char_xy(0, | + | lcd_gfx_goto_char_xy(0, |
| - | | + | lcd_gfx_write_string(tekst); |
| - | a++; | + | a++; |
| - | } | + | } |
| - | hw_delay_ms(3000); | + | hw_delay_ms(3000); |
| - | // Ühendamine soovitud seadmega | + | |
| - | lcd_gfx_clear(); | + | lcd_gfx_clear(); |
| - | //saada käsk | + | //saada käsk |
| - | usart_send_string(port," | + | usart_send_string(port," |
| - | // saada ühendatava seadme aadress | + | // saada ühendatava seadme aadress |
| - | usart_send_string(port, | + | usart_send_string(port, |
| - | // lõpeta käsk | + | // lõpeta käsk |
| - | usart_send_string(port," | + | usart_send_string(port," |
| - | //oota vastuseid | + | //oota vastuseid |
| - | for (a=0; a<8; a++) | + | for (a=0; a<8; a++) |
| - | { | + | { |
| - | bluetooth_read_string(port, | + | bluetooth_read_string(port, |
| - | lcd_gfx_goto_char_xy(0, | + | lcd_gfx_goto_char_xy(0, |
| - | | + | lcd_gfx_write_string(tekst); |
| - | } | + | } |
| - | hw_delay_ms(3000); | + | hw_delay_ms(3000); |
| // Joonistab ekraanile infot | // Joonistab ekraanile infot | ||
| - | lcd_gfx_clear(); | + | |
| - | lcd_gfx_goto_char_xy(0, | + | lcd_gfx_goto_char_xy(0, |
| lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
| lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
| Line 124: | Line 127: | ||
| lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
| lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
| - | lcd_gfx_goto_char_xy(0, | + | |
| lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
| // Hoia meeles eelmist nuppu selleks, et vältida olukorda, | // Hoia meeles eelmist nuppu selleks, et vältida olukorda, | ||
| - | // kus nuppu all hoides tuleb sada nupuvajutusi järjest. | + | |
| // Algatuseks olgu see -1 ehk ükski nupp pole all. | // Algatuseks olgu see -1 ehk ükski nupp pole all. | ||
| int8_t previousButton = -1; | int8_t previousButton = -1; | ||
| Line 143: | Line 146: | ||
| if (previousButton == -1 && button != -1) | if (previousButton == -1 && button != -1) | ||
| { | { | ||
| - | // teisenda nupu indeks liites A täht | + | |
| - | // ja saada teisele moodulile | + | |
| // A täht on esimene nupp, B täht teine nupp jne | // A täht on esimene nupp, B täht teine nupp jne | ||
| usart_send_char(port, | usart_send_char(port, | ||
| Line 153: | Line 156: | ||
| { | { | ||
| // loe bait, teisenda leds massiivi indeksiks | // loe bait, teisenda leds massiivi indeksiks | ||
| - | //ja muuda väljundit. | + | |
| pin_toggle(leds[usart_read_char(port) - ' | pin_toggle(leds[usart_read_char(port) - ' | ||
| } | } | ||
| Line 166: | Line 169: | ||
| int8_t wait_button(uint16_t ms) | int8_t wait_button(uint16_t ms) | ||
| { | { | ||
| - | // Vaikimisi -1 tähendab, et ükski nupp pole vajutatud. | + | |
| int8_t button_nr = -1; | int8_t button_nr = -1; | ||
| uint16_t counter = 0; | uint16_t counter = 0; | ||
| Line 193: | Line 196: | ||
| void bluetooth_read_string(usart port, char *text, char chr) | void bluetooth_read_string(usart port, char *text, char chr) | ||
| { | { | ||
| - | char buf = 0; | + | |
| - | int end = 0; | + | int end = 0; |
| - | int lnd = 0; | + | int lnd = 0; |
| - | // salvestab vajaliku osa saadud stringist | + | |
| - | while (end < 2) | + | while (end < 2) |
| - | { | + | { |
| - | // oota saabuvat baiti | + | // oota saabuvat baiti |
| - | while (!usart_has_data(port)); | + | while (!usart_has_data(port)); |
| - | // lae ringi saadud bait | + | // lae ringi saadud bait |
| - | buf = usart_read_char(port); | + | buf = usart_read_char(port); |
| - | // kui lubatud salvestada | + | // kui lubatud salvestada |
| - | if (end == 1) | + | if (end == 1) |
| - | { | + | { |
| - | // lõpeta CR peale, muul juhul salvesta | + | |
| - | if (buf == 13) end = 2; | + | |
| - | else text[lnd] = buf; | + | |
| - | // kohaloendur | + | // kohaloendur |
| - | lnd++; | + | lnd++; |
| - | } | + | |
| - | // kui on õige tähemärk, siis salvestama | + | |
| - | if (buf == chr) end = 1; | + | |
| } | } | ||
| - | // stringi terminaator lõppu | + | // kui on õige tähemärk, siis salvestama |
| - | text[lnd-1] = 0; | + | if (buf == chr) end = 1; |
| + | } | ||
| + | | ||
| + | text[lnd-1] = 0; | ||
| } | } | ||
| </ | </ | ||