This is an old revision of the document!


Serielles Interface

Diese Bibliothek liefert Funktionen für das AVR asynchrone serielle Interface

Datentypen

  • usart
    Datentyp für USART Interfacekontroll-, Status- und Datenregisteradressen. usart Variablen sollten zu Beginn des Programms initialisiert werden. Für die Initialisierung gibt es eine Macrofunktion USART dessen einziger Parameter für den Index des Interface ist. (0 oder 1).
  • adc_usart_databits
    Data bits count enumeration data type. Options:
    • USART_DATABITS_5 - 5 data bits.
    • USART_DATABITS_6 - 6 data bits.
    • USART_DATABITS_7 - 7 data bits.
    • USART_DATABITS_8 - 8 data bits.
    • USART_DATABITS_9 - 9 data bits.
  • usart_stopbits
    Stop bits count enumeration data type. Options:
    • USART_STOPBITS_ONE - One stop bit.
    • USART_STOPBITS_TWO - Two stop bits.
  • usart_parity
    Parity mode enumeration data type. Options:
    • USART_PARITY_NONE - Disabled.
    • USART_PARITY_EVEN - Even parity.
    • USART_PARITY_ODD - Odd parity.

Funktionen

  • USART_BAUDRATE_ASYNC(baud)

Macrofunktion um den USART Baudratenregisterwert im asynchronen Modus zu berechnen. Parameter:

  • baud - Gewünschte Baudrate
  • Gibt Baudratenregisterwert wieder.
  • void usart_init_async(usart port, usart_databits data_bits, usart_stopbits stop_bits, usart_parity parity, usart_baudrate baudrate)

Initialisiert asynchrones USART. Parameter:

  • port - USART interface variable.
  • data_bits - Data bits.
  • stop_bits - Stop bits.
  • parity - Parity mode.
  • baudrate - Baud rate register value (can be calculated with USART_BAUDRATE_ASYNC macro function).
  • void usart_send_char(usart port, char symbol)

Blocking character transmission. Functions wait until transmit buffer empties before writing a character to the buffer. Parameters:

  • port - USART interface variable.
  • symbol - Character to be sent.
  • void usart_send_string(usart port, char *text)

Blocking string transmission. Parameters:

  • port - USART interface variable.
  • text - Pointer to strin (char array). String has to end with null character.
  • bool usart_has_data(usart port)

Checks for data in receive buffer. Parameters:

  • port - USART interface variable.
  • Returns true when there is a character in receive buffer, and false when not.
  • char usart_read_char(usart port)

Reads a character from receive buffer. Before reading user must check if there actually is a received character. Parameters:

  • port - USART interface variable.
  • Returns character.
  • bool usart_try_read_char(usart port, char *symbol)

Reads a character from receive buffer if there is any. Parameters:

  • port - USART interface variable.
  • symbol - Pointer to character variable. If there is a character in receive buffer, it is written to the pointed variable.
  • Returns true when there was a character in receive buffer, and false when not.

Beispiel

USART Interface ist konfiguriert um 8 Data Bits, ein Stop Bit, 9600 bps Baudrate und kein Paritätsmodus zu nutzen. Das Programm sendet einen String, wartet bis die Buchstaben empfangen wurden und ließt sie dann aus.

#include <homelab/usart.h>
 
// Use USART interface 0.
usart port = USART(0);
 
int main(void)
{	
	char c;
 
	// Interface initialization.
	usart_init_async(port,
		USART_DATABITS_8,
		USART_STOPBITS_ONE,
		USART_PARITY_NONE,
		USART_BAUDRATE_ASYNC(9600));
 
	// String sending.
	usart_send_string(port, "Hello\n");
 
	// Waiting for incoming data.
	while (!usart_has_data(port)) {}
 
	// Read out the received character.
	c = usart_read_char(port);
}
de/software/homelab/library/usart.1281880053.txt.gz · Last modified: (external edit)
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