This shows you the differences between two versions of the page.
| et:projects:communicator [2009/11/17 14:34] – tekitatud mikk.leini | et:projects:communicator [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Suhtluskanal ====== | ||
| + | Järgnevalt on toodud UART jadaliidese kasutamise näide. Programm võimaldab edastada tekstisõnumeid AVR ja teise AVR-i või arvuti vahel. Kasutatud on viikude, LCD ja USART teeke. | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #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, | ||
| + | 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(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||