This is an old revision of the document!


Table of Contents

RS-232

Vajalikud teadmised: [HW] Controller module, [AVR] USART, [LIB] Serial Interface, [LIB] Alphanumeric LCD

Teooria

Cable RS-232. Left plug is „male” and the right one is „female” plug.“

RS-232 is a standard of physical data interface, which is used for delivering binary data. The standard is used mainly in serial ports of computers, which are also called “COM” ports in everyday language. Nowadays is the RS-232 largely replaced by USB interface, but due to its simplicity it is still used very successfully in hobby applications, especially if there are USB – RS-232 converters. The RS-232 standard determines the plugs, electrical parameters and meanings of the signals but not the protocol.

The RS-232 interface is used mainly with UART hardware data transmission module which’s protocol is standardized, but it does not determine the plugs and other things. So the RS-232 enhances the UART. Since the UART is usually one module of periphery of the microcontroller, which’s digital input-output does not correspond to the electrical parameters of the RS-232, they are connected together with a special leveling-converter. One best known leveling-converters between RS-232 and TLL/CMOS is MAX232.

UART means universal asynchronous receiver/transmitter. USART is almost the same but with that difference that the data is sent with clock signal. The UART may be called also a serial interface. The serial interface is a data transferring mechanism, where each bit is transmitted one by one. For example, to transmit 1 bait, 8 bits are transmitted with certain interval. That means that on the serial interface line, which is one pin of the microcontroller, the value of voltage is changed after certain time once low and then high. Usually there are two devices connected to the serial interface. One is transmitting the information (by changing the value of the pin) and the other is receiving it (by registering the value of the pin). Transmitting pin is TX, and receiving pin is RX. The info is moving always to one direction on one line. For sending data to the other direction other line is used. If data is moved on two lines on the same time, it is called full duplex bus.

the frame of UART, where S is start-bit , 0-7 are data-bits, P is parity-bit (if existing) and T is stop-bit (or 2).

Transmitting data is done by frames of the UART interface, in which is 5-9 data bits (depending on the configuration). Most common is 8 bits (1 bait). In addition to the data bits also extra bits are transmitted with the frame, which are used to recognize the moments of arrival and ending of the data on the receiver’s side. The first is called start-bit and it is always 0. The second is called stop-bit (or bits), which is always 1. Before the stop-bit also parity bit may come. It is use to control regularity. The parity-bit shows whether in the amount of the data-bits is odd or even number of ones. Which reading it has depends on the configuration of the UART interface. The parity-bit is usually not used anymore and it can be banned in configuration. Like the parity-bit can be configured, also can the amount of data-bits and stop-bits.

In addition to the frame structure, there is one more important parameter – it is baud rate, with which the number of transmitted symbols in one second is determined. Baud shows the number of symbols. When we are dealing with UART then 1 baud is 1 bit and that is why we talked about bits when we were talking about frame. Basically it does not matter which baud rate is used for data transmitting, but there is an amount of commonly used baud rates, which should be used. For example: 9600 bps, 19200bps, 38400 bps, 57600 bps, 115200 bps;

In addition it is worth to know that the RS-232 standard includes in addition to the data-signals (RX, TX) also data flow control pins DTR, DCD, DSR, RI, RTS and CTS, which is used for controlling the communication between the devices. For example they can be used to let know whether it is ready to receive data or not. Since the RS-232 interface’s original goal is to connect the computers to a modem, some signals are (were) useful rather for showing the state of the telephone lines.

Praktika

Kodulabori Kontrollermooduli plaadil on RS-232 isa-tüüpi pesa. Selle kaudu saab kontrolleri arvutiga või teise kontrolleriga ühendada. Arvutiga ühendamiseks tuleb kasutada tavalist pööramata kaablit, mille üks pistik on ema-tüüpi, teine isa-tüüpi. Teise kontrolleriga ühendamiseks tuleb kasutada kaablit, kus RX ja TX ning vookontrolli signaalid on risti keeratud ja mõlemad pistikud on ema-tüüpi. Pööratud kaablit nimetatakse ka nullmodemi kaabliks. Järgnevalt on toodud UART jadaliidese kasutamise näiteprogramm. Programm saadab käivitades RS-232 liidese kaudu tervituse ja kuvab sõnumeid, mis saabuvad. Kasutatud on LCD ja USART teeke.

//
// Kodulabori Kontrollerimooduli arvutiga RS-232 kaudu liidestamine.
// Näide kasutab Digitaalset sisend-väljundmoodulit koos LCD ekraaniga.
// Arvuti terminalis sisestatud tekst kuvatakse LCD-l.
//
#include <homelab/usart.h>
#include <homelab/module/lcd_alpha.h>
 
//
// USART liidese määramine
//
usart port = USART(0);
 
//
// Põhiprogramm
//
int main(void)
{
	char c;
	unsigned char row = 1;
 
	// USART liidese seadistamine
	usart_init_async(port,
		USART_DATABITS_8,
		USART_STOPBITS_ONE,
		USART_PARITY_NONE,
		USART_BAUDRATE_ASYNC(9600));
 
	// LCD ekraani seadistamine
	lcd_alpha_init(LCD_ALPHA_DISP_ON_BLINK);
 
	// Ekraanil tervituse ütlemine
	lcd_alpha_write_string("Ootan teadet");
 
	// Kursori teise rea algusesse viimine
	lcd_alpha_goto_xy(0, row);
 
	// Arvutile tere ütlemine
	usart_send_string(port, "Tere, kirjuta midagi!\r\n");
 
	// Lõputu tsükkel
	while (true)
	{
		// Jadaliidesest märgi lugemine
		if (usart_try_read_char(port, &c))
		{		
			// Kas tegu on reavahetuse märgiga?
			if (c == '\r')
			{
				// Rea vahetamine
				row = 1 - row;
 
				// Rea tühjendamine eelmisest teatest
				lcd_alpha_clear_line(row);
			}
			else
			{
				// Märgi otse ekraanile väljastamine
				lcd_alpha_write_char(c);
			}
		}
	}
}
HyperTerminal aken

Windows XP operatsioonisüsteemiga on kaasas programm HyperTerminal. See avaneb Start menüüst Accessories → Communications → HyperTerminal valides. Ühenduse seadeteks valida 9600 bps, 1 start- ja 1 stoppbitt ilma paarsuse- ja vookontrollita. Kui HyperTerminal on avatud mikrokontrolleri tööle panemise ajal, tekib ekraanile tervitussõnum. Aknas sisestatud tähed kuvatakse aga alfabeetilise LCD ekraanil. Klahvi Enter vajutus muudab rida LCD ekraanil.

en/examples/communication/rs232.1267820818.txt.gz · Last modified: 2020/07/20 09:00 (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