This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:examples:communication:bluetooth:eunistone [2012/10/22 13:53] – rellermaa | et:examples:communication:bluetooth:eunistone [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Bluetooth ====== | ||
| + | Vajalikud teadmised: \\ | ||
| + | [HW] [[et: | ||
| + | [AVR] [[et: | ||
| + | [LIB] [[et: | ||
| + | [LIB] [[et: | ||
| + | |||
| + | **BTBee Pro mooduli puhul vaata siia:** | ||
| + | [[et/ | ||
| + | **Bluetooth Bee mooduli puhul vaata siia:** | ||
| + | [[et/ | ||
| + | \\ | ||
| + | \\ | ||
| + | **Täielik AT käskude nimekiri (en):** \\ | ||
| + | [[http:// | ||
| + | |||
| + | |||
| + | ===== Teooria ===== | ||
| + | |||
| + | {{ :: | ||
| + | |||
| + | Bluetooth on tööstusstandard seadmete omavaheliseks traadita ühenduseks. Selle eesmärgiks on juhtmeühenduste asendamine mobiiltelefonide, | ||
| + | |||
| + | ===== Praktika ===== | ||
| + | |||
| + | Bluetooth Bee moodul sobib Kodulabori sidemooduli plaadile „XBee" | ||
| + | |||
| + | Bluetoot Bee moodul loeb käsu õigeks, lõpetatud ASCII märkidega CR (13 ehk ’\r’) ja LF (10 ehk ’\n’). Moodulit on võimalik testimiseks ühendada ka arvuti jadaliidesesse. Suhtlemiseks näiteks Hyper Terminali abil tuleb seal valida // | ||
| + | |||
| + | Bloetooth mooduli arvutiga ühendamiseks on kõige lihtsam panna moodul //slave// olekusse. Selleks tuleb moodul eelnevalt vastavalt seadistada saates Bluetooth moodulile järgmised AT käsud: | ||
| + | ^ Käsk ^ Seletus ^ | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | |||
| + | Kui ühendus on loodud, tuleks Bluetooth moodul panna " | ||
| + | |||
| + | Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid Bluetooth Bee vahendusel kontrollerile. | ||
| + | |||
| + | Järgnev kood näitab lühidalt, kuidas luua ühendus Kodulabori kontrolleri ning arvuti vahel. | ||
| + | <code c> | ||
| + | #define F_CPU 14745600 | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // | ||
| + | // Select USART channel | ||
| + | usart port = USART(1); | ||
| + | |||
| + | // | ||
| + | // Initialize bluetooth module | ||
| + | // | ||
| + | uint8_t Bluetooth_Connect() | ||
| + | { | ||
| + | char dump; | ||
| + | char str[10]; | ||
| + | _delay_ms(500); | ||
| + | lcd_gfx_clear(); | ||
| + | // Software reset | ||
| + | do | ||
| + | { | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | } while (strcmp(str," | ||
| + | |||
| + | // set request password 123456 | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Set to discoverable | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Register local services | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Auto accept all connection requests | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Connect using password 123456 | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | while( UCSR1A & (1<< | ||
| + | dump = UDR1; | ||
| + | |||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Put bluetooth module in transparent mode | ||
| + | // | ||
| + | uint8_t Bluetooth_transparent() | ||
| + | { | ||
| + | char str[50]; | ||
| + | |||
| + | do | ||
| + | { | ||
| + | usart_has_data(port); | ||
| + | usart_read_string(port, | ||
| + | }while(!strstr(str," | ||
| + | usart_send_string(port," | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Main program | ||
| + | // | ||
| + | int main(void) | ||
| + | { | ||
| + | char text[15]; | ||
| + | char dump; | ||
| + | char c; | ||
| + | int a = 0; | ||
| + | int connect = 0; | ||
| + | |||
| + | // Initialize LCD | ||
| + | lcd_gfx_init(); | ||
| + | lcd_gfx_clear(); | ||
| + | |||
| + | // Initialize USART | ||
| + | usart_init_async(port, | ||
| + | USART_STOPBITS_ONE, | ||
| + | USART_PARITY_NONE, | ||
| + | USART_BAUDRATE_ASYNC(115200)); | ||
| + | |||
| + | lcd_gfx_write_string(" | ||
| + | //Connect to Bluetooth on PC | ||
| + | while(!Bluetooth_Connect()); | ||
| + | |||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | //Wait for terminal connection from PC | ||
| + | Bluetooth_transparent(); | ||
| + | |||
| + | // | ||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | while(1) | ||
| + | { | ||
| + | //Read USART data and mirror it to LCD and back to the terminal | ||
| + | while(!(UCSR1A & (1<< | ||
| + | dump = UDR1; | ||
| + | //If ' | ||
| + | if(dump == ' | ||
| + | lcd_gfx_clear(); | ||
| + | else | ||
| + | { | ||
| + | lcd_gfx_write_char(dump); | ||
| + | usart_send_char(port, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||