This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:examples:communication:zigbee [2012/04/05 15:07] – illo | et:examples:communication:zigbee [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| ====== ZigBee ====== | ====== ZigBee ====== | ||
| - | //Vajalikud teadmised: [HW] [[et: | + | //Vajalikud teadmised: |
| + | [HW] [[et: | ||
| + | [AVR] [[et: | ||
| + | [LIB] [[et: | ||
| + | [LIB] [[et: | ||
| ===== Teooria ===== | ===== Teooria ===== | ||
| Line 23: | Line 28: | ||
| //Lisame ZigBee mooduli toe | //Lisame ZigBee mooduli toe | ||
| #include < | #include < | ||
| + | #include < | ||
| // USART liidese määramine | // USART liidese määramine | ||
| - | usart port = USART(1); | + | static |
| // LEDid ja nupud määrata | // LEDid ja nupud määrata | ||
| - | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | + | pin leds[3] = { PIN(H, 5), PIN(H, 4), PIN(H, 3) }; |
| - | pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | + | pin buttons[3] = { PIN(H, 0), PIN(H, 1), PIN(H, 2) }; |
| // ZigBee moodulite max arv võrgus + broadcast aadress + 1 | // ZigBee moodulite max arv võrgus + broadcast aadress + 1 | ||
| Line 42: | Line 48: | ||
| int main(void) | int main(void) | ||
| { | { | ||
| - | | + | uint8_t adr = 1; |
| - | + | ||
| - | // seadista LEDide ja nuppude I/O pordid | + | |
| - | for (int i=0; i<3; i++) | + | |
| - | { | + | |
| - | pin_setup_output(leds[i]); | + | |
| - | pin_setup_input(buttons[i]); | + | |
| - | } | + | |
| - | | + | // seadista LEDide ja nuppude I/O pordid |
| - | | + | for (int i=0; i<3; i++) |
| - | USART_DATABITS_8, | + | { |
| - | USART_STOPBITS_ONE, | + | pin_setup_output(leds[i]); |
| - | USART_PARITY_NONE, | + | pin_setup_input(buttons[i]); |
| - | | + | } |
| - | | + | // USART liidese seadistamine ZigBee side jaoks |
| - | | + | usart_init_async(port, |
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| - | | + | // LCD ekraani algseadistamine |
| - | | + | lcd_gfx_init(); |
| - | | + | // Ekraani puhastamine |
| - | | + | lcd_gfx_clear(); |
| - | | + | // Taustavalgustuse tööle lülitamine |
| - | | + | lcd_gfx_backlight(true); |
| - | lcd_gfx_goto_char_xy(0, | + | |
| - | lcd_gfx_write_string(" | + | |
| - | | + | //Oota kuni teisi ZigBee mooduleid otsitakse läbiümbrusest |
| - | lcd_gfx_write_string(" | + | lcd_gfx_goto_char_xy(0, |
| + | lcd_gfx_write_string(" | ||
| - | // Otsib ümbrusest teisiZigBee mooduleid | + | lcd_gfx_goto_char_xy(0, 1); |
| - | // täidab nodelist massiivi leitud moodulite infoga. | + | lcd_gfx_write_string(" |
| - | zigbee_find_nodes(port, nodelist, ZIGBEE_MAX_NODES); | + | |
| - | | + | // Otsib ümbrusest teisiZigBee mooduleid |
| - | | + | // täidab nodelist massiivi leitud moodulite infoga. |
| - | | + | zigbee_find_nodes(port, nodelist, ZIGBEE_MAX_NODES); |
| - | | + | // |
| - | // (mis reale kirjutab, kust võtab adr, mitu tk max) | + | lcd_gfx_goto_char_xy(0, |
| - | zigbee_lcd_show_nodes(2, | + | lcd_gfx_write_string(" |
| - | hw_delay_ms(3000); | + | |
| - | | + | |
| - | + | ||
| - | // Kuvab ekraanil ühendamise teate | + | |
| - | | + | |
| - | lcd_gfx_write_string(" | + | |
| - | lcd_gfx_goto_char_xy(0, | + | |
| - | //kuvab ainult aadressi tagumise osa (8 tähemärki) | + | |
| - | lcd_gfx_write_string((nodelist + adr)-> | + | |
| - | + | ||
| - | // Seadista ZigBee saatma infot valitud ZigBee nodele, | + | |
| - | // antud juhul esimesele [0] | + | |
| - | // (mis porti kasutab, kust võtab adr) | + | |
| - | zigbee_set_destination(port, | + | |
| - | | + | // Kuvab ekraanil leitud moodulite nimekirja |
| - | | + | // (mis reale kirjutab, kust võtab adr, mitu tk max) |
| - | | + | zigbee_lcd_show_nodes(48, |
| + | hw_delay_ms(3000); | ||
| + | lcd_gfx_clear(); | ||
| - | | + | // Kuvab ekraanil ühendamise teate |
| - | lcd_gfx_write_string(" | + | lcd_gfx_goto_char_xy(0, |
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | //kuvab ainult aadressi tagumise osa (8 tähemärki) | ||
| + | lcd_gfx_write_string((nodelist + adr)-> | ||
| - | lcd_gfx_goto_char_xy(0, 2); | + | // Seadista ZigBee saatma infot valitud ZigBee nodele, |
| - | | + | // antud juhul esimesele [0] |
| + | // (mis porti kasutab, kust võtab adr) | ||
| + | zigbee_set_destination(port, & | ||
| - | // Hoia meeles eelmist nuppu selleks, et vältida olukorda, | + | lcd_gfx_clear(); |
| - | // kus nuppu all hoides tuleb sada nupuvajutusi järjest. | + | // Joonistab ekraanile infot |
| - | // Algatuseks olgu see -1 ehk ükski nupp pole all. | + | lcd_gfx_goto_char_xy(0, |
| - | | + | lcd_gfx_write_string(" |
| - | // Lõputu tsükkel moodulite andmetevahetuse näitamiseks | + | lcd_gfx_goto_char_xy(0, 1); |
| - | while (true) | + | lcd_gfx_write_string(" |
| - | { | + | |
| - | int8_t button; //muutuja nupuvajutuse registreerimiseks | + | |
| - | // Oota nuppu 1 millisekund | + | lcd_gfx_goto_char_xy(0, |
| - | | + | lcd_gfx_write_string(" |
| - | | + | // Hoia meeles eelmist nuppu selleks, et vältida olukorda, |
| - | if (previousButton == -1 && button != -1) | + | // kus nuppu all hoides tuleb sada nupuvajutusi järjest. |
| - | { | + | // Algatuseks olgu see -1 ehk ükski |
| - | | + | int8_t previousButton = -1; |
| - | | + | |
| - | // A täht on esimene | + | |
| - | | + | |
| - | } | + | |
| - | | + | // Lõputu tsükkel moodulite andmetevahetuse näitamiseks |
| - | | + | while (true) |
| - | { | + | { |
| - | // loe bait, teisenda leds massiivi indeksiks | + | int8_t button; //muutuja nupuvajutuse registreerimiseks |
| - | //ja muuda väljundit. | + | |
| - | pin_toggle(leds[usart_read_char(port) - ' | + | |
| - | } | + | |
| - | | + | // Oota nuppu 1 millisekund |
| - | previousButton = button; | + | button = wait_button(1); |
| - | } | + | |
| + | // Kui eelmises tsüklis oli nupp üleval ja nüüd on all | ||
| + | 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 | ||
| + | usart_send_char(port, | ||
| + | } | ||
| + | |||
| + | // Loe USART' | ||
| + | if (usart_has_data(port)) | ||
| + | { | ||
| + | // loe bait, teisenda leds massiivi indeksiks | ||
| + | //ja muuda väljundit. | ||
| + | pin_toggle(leds[usart_read_char(port) - ' | ||
| + | } | ||
| + | |||
| + | // Jäta meelde mis nupp oli hetkel alla vajutatud | ||
| + | previousButton = button; | ||
| + | } | ||
| } | } | ||
| Line 150: | Line 156: | ||
| 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; |
| - | do | + | do |
| - | { | + | { |
| - | // vaata kas mõni nupp on all | + | // vaata kas mõni nupp on all |
| - | for (uint8_t i=0; i<3; i++) | + | for (uint8_t i=0; i<3; i++) |
| - | { | + | { |
| - | if (!pin_get_value(buttons[i])) | + | if (!pin_get_value(buttons[i])) |
| - | { | + | { |
| - | button_nr = i; | + | button_nr = i; |
| - | break; | + | break; |
| - | } | + | } |
| - | } | + | } |
| - | | + | // oota 1 millisekund |
| - | hw_delay_ms(1); | + | hw_delay_ms(1); |
| - | + | ||
| - | // suurenda millisekundi loendurit | + | |
| - | counter++; | + | |
| - | } while (button_nr == -1 && (counter < ms)); | + | |
| - | + | ||
| - | return button_nr; | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | ==== TH Suvekool ==== | + | |
| - | + | ||
| - | <code c> | + | |
| - | #include < | + | |
| - | #include < | + | |
| - | #include < | + | |
| - | + | ||
| - | //Lisame ZigBee mooduli toe | + | |
| - | #include < | + | |
| - | + | ||
| - | // USART liidese määramine ZigBee jaoks | + | |
| - | usart port = USART(1); | + | |
| - | + | ||
| - | // LEDid ja nupud määrata | + | |
| - | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | + | |
| - | pin buttons[3] = { PIN(C, 2), PIN(C, 1), PIN(C, 0) }; | + | |
| - | + | ||
| - | // Teiste leitavate zigbee moodulite aadresside massiiv | + | |
| - | zigbee_node_t nodelist[6]; | + | |
| - | + | ||
| - | + | ||
| - | int main(void) | + | |
| - | { | + | |
| - | | + | |
| - | + | ||
| - | // seadista LEDide ja nuppude I/O pordid | + | |
| - | for (int i=0; i<3; i++) | + | |
| - | { | + | |
| - | pin_setup_output(leds[i]); | + | |
| - | pin_setup_input(buttons[i]); | + | |
| - | } | + | |
| - | + | ||
| - | // USART liidese seadistamine ZigBee side jaoks | + | |
| - | usart_init_async(port, | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | + | ||
| - | + | ||
| - | // Otsib ümbrusest teisiZigBee mooduleid | + | |
| - | // täidab nodelist massiivi leitud moodulite aadressidega | + | |
| - | pin_set(leds[0]); | + | |
| - | zigbee_find_nodes(port, | + | // suurenda millisekundi loendurit |
| - | // võtab aega | + | counter++; |
| - | pin_set(leds[1]); | + | } |
| - | // Seadista ZigBee saatma infot valitud ZigBee nodele, | + | while (button_nr == -1 && (counter < ms)); |
| - | // | + | |
| - | zigbee_set_destination(port, | + | |
| - | // | + | |
| - | //Kustuta LED-id -> ühendus olemas | + | return button_nr; |
| - | pin_set(leds[2]); | + | |
| - | + | ||
| - | // Lõputu tsükkel moodulite andmetevahetuse näitamiseks | + | |
| - | while (true) | + | |
| - | { | + | |
| - | // vaata kas mõni nupp on alla vajutatud | + | |
| - | for (uint8_t i=0; i<3; i++) if (!pin_get_value(buttons[i])) button = i; | + | |
| - | + | ||
| - | // kui nuppu on vajutatud | + | |
| - | if (button != -1) | + | |
| - | { | + | |
| - | //////////////////// | + | |
| - | } | + | |
| - | } | + | |
| } | } | ||
| </ | </ | ||