This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:communication:rs232 [2010/02/08 14:12] – Links to et:software:library:usart changed to et:software:homelab:library:usart mikk.leini | en:examples:communication:rs232 [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== RS-232 ====== | ||
| + | //Necessary knowledge: [HW] [[en: | ||
| + | |||
| + | ===== Theory ===== | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | 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, | ||
| + | |||
| + | The RS-232 interface is used mainly with UART hardware data transmission module which protocol is standardized, | ||
| + | |||
| + | UART means universal asynchronous receiver/ | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | 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, | ||
| + | |||
| + | Furthermore, | ||
| + | |||
| + | ===== Practice ===== | ||
| + | |||
| + | The Controller module board is equipped with one RS-232 type male plug. Through that can controller be connected to computer or to an other controller. For connecting to a computer a usual not inverted cable must be used, which one end is male and other one is female. For connection to an other controller a cable must be used where RX and TX and current control signals are perpendicularly inverted and both plugs are female. The inverted cable is also called zero modem cable. | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // Connecting the Controller module of the HomeLab to a computer through RS-232. | ||
| + | // The example is using digital input-output module with LCD. | ||
| + | // The text inserted in the terminal of the computer is displayed on the LCD. | ||
| + | // | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // | ||
| + | // Determining USART interface. | ||
| + | // | ||
| + | usart port = USART(0); | ||
| + | |||
| + | // | ||
| + | // Main program | ||
| + | // | ||
| + | int main(void) | ||
| + | { | ||
| + | char c; | ||
| + | unsigned char row = 1; | ||
| + | |||
| + | // The set-up of the USART interface. | ||
| + | usart_init_async(port, | ||
| + | USART_DATABITS_8, | ||
| + | USART_STOPBITS_ONE, | ||
| + | USART_PARITY_NONE, | ||
| + | USART_BAUDRATE_ASYNC(9600)); | ||
| + | |||
| + | // The set-up of the LCD. | ||
| + | lcd_alpha_init(LCD_ALPHA_DISP_ON_BLINK); | ||
| + | |||
| + | // Displaying welcome message on the screen. | ||
| + | lcd_alpha_write_string(" | ||
| + | |||
| + | // Putting the cursor in the beginning of the second row. | ||
| + | lcd_alpha_goto_xy(0, | ||
| + | |||
| + | // Saying hello to the computer. | ||
| + | usart_send_string(port, | ||
| + | |||
| + | // Endless loop | ||
| + | while (true) | ||
| + | { | ||
| + | // Reading the sign from the serial interface. | ||
| + | if (usart_try_read_char(port, | ||
| + | { | ||
| + | // Are we dealing with the sign of changing the row? | ||
| + | if (c == ' | ||
| + | { | ||
| + | // Changing the row. | ||
| + | row = 1 - row; | ||
| + | |||
| + | // Emptying the row from the previous message. | ||
| + | lcd_alpha_clear_line(row); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | // Issuing the sign directly to the screen. | ||
| + | lcd_alpha_write_char(c); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | With Windows XP OS comes a program called HyperTerminal. It is opened from the //Start// menu by selecting // | ||