Differences

This shows you the differences between two versions of the page.

Link to this comparison view

et:projects:communicator [2009/11/17 14:34] – tekitatud mikk.leiniet: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 <stdio.h>
 +#include <avr/io.h>
 +#include <util/delay.h>
 +#include "pin.h"
 +#include "lcd.h"
 +#include "usart.h"
 +
 +// Pin configuration
 +#define BUTTON_CHAR     PORTPIN(C, 0)
 +#define BUTTON_NEXT     PORTPIN(C, 1)
 +#define BUTTON_ENTER    PORTPIN(C, 2)
 +
 +// Characters list
 +#define NUM_CHARS 26
 +const char chars[NUM_CHARS] = " ABCDEFGHIJKLMOPQRSTUVWXYZ";
 +
 +// General configuration
 +#define MAX_MESSAGE     16
 +
 +//
 +// 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, 0);
 + lcd_puts("                 ");
 + lcd_gotoxy(0, 0);
 + lcd_puts(my_message);
 + lcd_gotoxy(my_char_position, 0);
 +}
 +
 +//
 +// 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, 1);
 + lcd_puts(rx_message);
 +}
 +
 +//
 +// Main function
 +//
 +int main(void)
 +{
 + // Button state variables
 + unsigned char button_char_new,  button_char_old  = 1;
 + unsigned char button_next_new,  button_next_old  = 1;
 + unsigned char button_enter_new, button_enter_old = 1;
 +
 + // LCD configuring
 + lcd_init(LCD_DISP_ON_BLINK);
 + lcd_clrscr();
 +
 + // UART configuring
 + usart0_init_async(USART_DATABITS_8, USART_STOPBITS_1, USART_PARITY_NONE, 9600);
 +
 + // 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,  button_char_new);
 + pin_get_value(BUTTON_NEXT,  button_next_new);
 + pin_get_value(BUTTON_ENTER, button_enter_new);
 +
 + // 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
 + //_delay_ms(100);
 + }
 +
 + // 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
 + //_delay_ms(100);
 + }
 +
 + // 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] = '\r';
 + my_message[my_char_position + 2] = '\n';
 + 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_char_new;
 + button_next_old  = button_next_new;
 + 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] == '\r') && (rx_message[rx_char_position - 1] == '\n')))
 + {
 + // 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();
 + }
 + }
 + }
 +}
 +</code>
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0