et:examples:communication:rs232
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:examples:communication:rs232 [2010/01/18 09:24] – raivo.sell | et:examples:communication:rs232 [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== RS-232 ====== | ====== RS-232 ====== | ||
| - | //Vajalikud teadmised: [HW] [[et: | + | //Vajalikud teadmised: [HW] [[et: |
| ===== Teooria ===== | ===== Teooria ===== | ||
| - | [{{ : | + | [{{ : |
| - | RS-232 on füüsilise andmesideliidese standard mida kasutatakse binaarandmete edastamiseks. Standard on kasutusel peamiselt arvutite jadaportides, | + | RS-232 on füüsilise andmesideliidese standard, mida kasutatakse binaarandmete edastamiseks. Standard on kasutusel peamiselt arvutite jadaportides, |
| - | RS-232 liidest kasutatakse peamiselt koos UART nimelise | + | RS-232 liidest kasutatakse peamiselt koos UART nime kandva |
| - | UART on lahtitõlgituna " | + | UART on lahtitõlgituna " |
| - | [{{ : | + | [{{ : |
| - | Andmete edastamine toimub UART liideses kaadri (inglise keeles //frame//) kaupa, milles | + | Andmete edastamine toimub UART liideses kaadri (inglise keeles //frame//) kaupa, milles on andmebitte |
| - | Peale kaadri struktuuri on veel üks tähtis parameeter - see on boodikiirus (//baud rate//) millega määratakse | + | Peale kaadri struktuuri on veel üks tähtis parameeter - see on boodikiirus (inglise keeles |
| - | Lisainformatsioonina võiks teada, et RS-232 standard sisaldab peale andmesignaalide (RX, TX) veel voo-kontrolli viike DTR, DCD, DSR, RI, RTS ja CTS mida kasutatakse | + | Lisainformatsioonina võiks teada, et RS-232 standard sisaldab peale andmesignaalide (RX, TX) veel andmevoo-kontrolli viike DTR, DCD, DSR, RI, RTS ja CTS, mida kasutatakse |
| ===== Praktika ===== | ===== Praktika ===== | ||
| - | Kodulabori | + | Kodulabori |
| <code c> | <code c> | ||
| // | // | ||
| - | // Kodulabori | + | // Kodulabori |
| - | // Näide kasutab | + | // Näide kasutab |
| // Arvuti terminalis sisestatud tekst kuvatakse LCD-l. | // Arvuti terminalis sisestatud tekst kuvatakse LCD-l. | ||
| // | // | ||
| #include < | #include < | ||
| - | #include < | + | #include < |
| // | // | ||
| Line 55: | Line 55: | ||
| // LCD ekraani seadistamine | // LCD ekraani seadistamine | ||
| - | lcd_alpha_init(LCD_ALPHA_DISP_ON_BLINK); | + | lcd_gfx_init(); |
| // Ekraanil tervituse ütlemine | // Ekraanil tervituse ütlemine | ||
| - | lcd_alpha_puts(" | + | lcd_gfx_write_string(" |
| // Kursori teise rea algusesse viimine | // Kursori teise rea algusesse viimine | ||
| - | lcd_alpha_gotoxy(0, row); | + | lcd_gfx_goto_char_xy(0, row); |
| // Arvutile tere ütlemine | // Arvutile tere ütlemine | ||
| - | usart_send_text(port, "Tere, kirjuta midagi!\r\n" | + | usart_send_string(port, "Tere, kirjuta midagi!\r\n" |
| // Lõputu tsükkel | // Lõputu tsükkel | ||
| Line 71: | Line 71: | ||
| // Jadaliidesest märgi lugemine | // Jadaliidesest märgi lugemine | ||
| if (usart_try_read_char(port, | if (usart_try_read_char(port, | ||
| - | { | + | { |
| // Kas tegu on reavahetuse märgiga? | // Kas tegu on reavahetuse märgiga? | ||
| if (c == ' | if (c == ' | ||
| Line 79: | Line 79: | ||
| // Rea tühjendamine eelmisest teatest | // Rea tühjendamine eelmisest teatest | ||
| - | lcd_alpha_clr_line(row); | + | lcd_gfx_clear_line(row); |
| } | } | ||
| else | else | ||
| { | { | ||
| // Märgi otse ekraanile väljastamine | // Märgi otse ekraanile väljastamine | ||
| - | lcd_alpha_putc(c); | + | lcd_gfx_write_char(c); |
| } | } | ||
| } | } | ||
| } | } | ||
| } | } | ||
| + | |||
| </ | </ | ||
| - | Järgnevalt on toodud UART jadaliidese kasutamise | + | [[et: |
| - | FIXME | + | [{{ : |
| - | (pilt kahest ühendatud kontrollerist) | + | |
| - | <code c> | + | Windows XP operatsioonisüsteemiga on kaasas programm HyperTerminal. See avaneb |
| - | #include <stdio.h> | + | |
| - | #include <avr/io.h> | + | |
| - | #include <util/delay.h> | + | |
| - | #include " | + | |
| - | #include " | + | |
| - | #include " | + | |
| - | + | ||
| - | // Pin configuration | + | |
| - | #define BUTTON_CHAR | + | |
| - | #define BUTTON_NEXT | + | |
| - | #define BUTTON_ENTER | + | |
| - | + | ||
| - | // Characters list | + | |
| - | #define NUM_CHARS 26 | + | |
| - | const char chars[NUM_CHARS] = " ABCDEFGHIJKLMOPQRSTUVWXYZ"; | + | |
| - | + | ||
| - | // General configuration | + | |
| - | #define MAX_MESSAGE | + | |
| - | + | ||
| - | // | + | |
| - | // My message variables | + | |
| - | // | + | |
| - | char my_message[MAX_MESSAGE + 3]; | + | |
| - | unsigned char my_char_index = 0; | + | |
| - | unsigned char my_char_position = 0; | + | |
| - | + | ||
| - | // | + | |
| - | // Incoming message variables | + | |
| - | char rx_message[MAX_MESSAGE + 3]; | + | |
| - | unsigned char rx_char_position = 0; | + | |
| - | + | ||
| - | // | + | |
| - | // Resetting my message | + | |
| - | // | + | |
| - | void reset_my_message() | + | |
| - | { | + | |
| - | // Fill the message with nulls | + | |
| - | for (my_char_position = 0; my_char_position < MAX_MESSAGE + 3; my_char_position++) | + | |
| - | { | + | |
| - | my_message[my_char_position] = 0; | + | |
| - | } | + | |
| - | + | ||
| - | // Reset indexes | + | |
| - | my_char_position = 0; | + | |
| - | my_char_index = 0; | + | |
| - | } | + | |
| - | + | ||
| - | // | + | |
| - | // Displaying my current message | + | |
| - | // | + | |
| - | void display_my_message() | + | |
| - | { | + | |
| - | lcd_gotoxy(0, | + | |
| - | lcd_puts(" | + | |
| - | lcd_gotoxy(0, | + | |
| - | lcd_puts(my_message); | + | |
| - | lcd_gotoxy(my_char_position, | + | |
| - | } | + | |
| - | + | ||
| - | // | + | |
| - | // Resetting received message | + | |
| - | // | + | |
| - | void reset_rx_message() | + | |
| - | { | + | |
| - | // Fill the message with nulls | + | |
| - | for (rx_char_position = 0; rx_char_position < MAX_MESSAGE + 3; rx_char_position++) | + | |
| - | { | + | |
| - | rx_message[rx_char_position] = 0; | + | |
| - | } | + | |
| - | + | ||
| - | // Reset position | + | |
| - | rx_char_position = 0; | + | |
| - | } | + | |
| - | + | ||
| - | // | + | |
| - | // Displaying received message | + | |
| - | // | + | |
| - | void display_rx_message() | + | |
| - | { | + | |
| - | lcd_gotoxy(0, 1); | + | |
| - | lcd_puts(" | + | |
| - | lcd_gotoxy(0, | + | |
| - | lcd_puts(rx_message); | + | |
| - | } | + | |
| - | + | ||
| - | // | + | |
| - | // Main function | + | |
| - | // | + | |
| - | int main(void) | + | |
| - | { | + | |
| - | // Button state variables | + | |
| - | unsigned char button_char_new, | + | |
| - | unsigned char button_next_new, | + | |
| - | unsigned char button_enter_new, | + | |
| - | + | ||
| - | // LCD configuring | + | |
| - | lcd_init(LCD_DISP_ON_BLINK); | + | |
| - | lcd_clrscr(); | + | |
| - | + | ||
| - | // UART configuring | + | |
| - | usart0_init_async(USART_DATABITS_8, | + | |
| - | + | ||
| - | // Buttons configuring | + | |
| - | pin_setup_input(BUTTON_CHAR); | + | |
| - | pin_setup_input(BUTTON_NEXT); | + | |
| - | pin_setup_input(BUTTON_ENTER); | + | |
| - | + | ||
| - | // Reset | + | |
| - | reset_my_message(); | + | |
| - | reset_rx_message(); | + | |
| - | + | ||
| - | // Endless cycle | + | |
| - | while (1) | + | |
| - | { | + | |
| - | // Get new button values | + | |
| - | pin_get_value(BUTTON_CHAR, | + | |
| - | pin_get_value(BUTTON_NEXT, | + | |
| - | pin_get_value(BUTTON_ENTER, | + | |
| - | + | ||
| - | // Char selection button pressed ? | + | |
| - | if (!button_char_new && button_char_old) | + | |
| - | { | + | |
| - | // Change char | + | |
| - | my_char_index++; | + | |
| - | + | ||
| - | // Wrap | + | |
| - | if (my_char_index >= NUM_CHARS) | + | |
| - | { | + | |
| - | my_char_index = 0; | + | |
| - | } | + | |
| - | + | ||
| - | // Update message | + | |
| - | my_message[my_char_position] = chars[my_char_index]; | + | |
| - | + | ||
| - | // Display message text | + | |
| - | display_my_message(); | + | |
| - | + | ||
| - | // Pause | + | |
| - | // | + | |
| - | } | + | |
| - | + | ||
| - | // Next char button pressed ? | + | |
| - | if (!button_next_new && button_next_old) | + | |
| - | { | + | |
| - | // Increase current position | + | |
| - | my_char_position++; | + | |
| - | + | ||
| - | // If message is full, restart | + | |
| - | if (my_char_position >= MAX_MESSAGE) | + | |
| - | { | + | |
| - | reset_my_message(); | + | |
| - | } | + | |
| - | else | + | |
| - | { | + | |
| - | // Fill new places with space | + | |
| - | my_char_index = 0; | + | |
| - | my_message[my_char_position] = chars[my_char_index]; | + | |
| - | } | + | |
| - | + | ||
| - | // Display message text | + | |
| - | display_my_message(); | + | |
| - | + | ||
| - | // Pause | + | |
| - | // | + | |
| - | } | + | |
| - | + | ||
| - | // Enter button pressed ? | + | |
| - | if (!button_enter_new && button_enter_old) | + | |
| - | { | + | |
| - | // Send message if it's not empty | + | |
| - | if ((my_char_position > 0) || (my_message[0] != 0)) | + | |
| - | { | + | |
| - | // Inser line feed and carriage return before terminator | + | |
| - | my_message[my_char_position + 1] = ' | + | |
| - | my_message[my_char_position + 2] = ' | + | |
| - | my_message[my_char_position + 3] = 0; | + | |
| - | + | ||
| - | // Send message | + | |
| - | usart0_send_text(my_message); | + | |
| - | + | ||
| - | // Restart | + | |
| - | reset_my_message(); | + | |
| - | display_my_message(); | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | // Remember old button values | + | |
| - | button_char_old | + | |
| - | button_next_old | + | |
| - | button_enter_old = button_enter_new; | + | |
| - | + | ||
| - | // Got data ? | + | |
| - | if (usart0_has_data()) | + | |
| - | { | + | |
| - | // Read char | + | |
| - | rx_message[rx_char_position++] = usart0_read_char(); | + | |
| - | + | ||
| - | // Show message if it's too long or the delimiter has arrived | + | |
| - | if ((rx_char_position >= MAX_MESSAGE + 3) || | + | |
| - | ((rx_message[rx_char_position - 2] == ' | + | |
| - | { | + | |
| - | // Replace line chars with null terminator | + | |
| - | rx_message[rx_char_position - 2] = 0; | + | |
| - | rx_message[rx_char_position - 1] = 0; | + | |
| - | + | ||
| - | // Display message | + | |
| - | display_rx_message(); | + | |
| - | reset_rx_message(); | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
et/examples/communication/rs232.1263799469.txt.gz · Last modified: (external edit)
