A method for realizing serial port RS232 communication based on single chip microcomputer

Through the in-machine timer and IO pin in the microcontroller, the serial port signal is simulated, and the problem of insufficient serial port interfaces of the conference host equipment is solved, and communication expansion is achieved without increasing hardware costs.

CN116049058BActive Publication Date: 2025-08-19GUANGZHOU BAOLUN ELECTRONICS CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310121066.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-02-15
Publication Date
2025-08-19
Estimated Expiration
2043-02-15

AI Technical Summary

Technical Problem

Due to the limited number of physical serial interfaces integrated by existing conference host devices, it is impossible to connect to peripheral devices, which increases the hardware cost of the serial interface.

Method used

By implementing the serial port RS232 communication method based on the microcontroller, the serial port signal is simulated by using the microcontroller timer and general IO pins, the transmit and receive pins are divided, the level state is detected for interrupt capture, and data acquisition and transmission are completed.

Benefits of technology

Add communication serial port without adding microcontroller physical serial ports to reduce hardware costs and reduce the use of network expansion equipment.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116049058B_ABST
    Figure CN116049058B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for implementing serial port RS232 communication based on a single-chip microcomputer, comprising the following steps: Step 1: determining the timeout time of the first timing channel in the timer within the single-chip microcomputer; Step 2: dividing the sending pin and the receiving pin, and in an idle state, setting the sending pin to a high level 1, and detecting the level state of the receiving pin to determine whether the timer enters a capture receive interrupt; Step 3: obtaining the number of low level 0s and the number of high level 1s; Step 5: repeating Steps 3 and 4 until the timer reaches the timeout time, and combining the collected 1s and 0s into a byte of data in the order of the detected levels; Step 6: setting the second timing channel of the timer to an output comparison working mode to determine whether the sending pin outputs a high level or a low level. The present invention can add a communication serial port without adding a physical serial port to the single-chip microcomputer itself, thereby reducing the hardware cost of the single-chip microcomputer.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the technical field of forming virtual serial port communication, in particular to a method for realizing serial port RS232 communication based on a single chip microcomputer. Background Art

[0002] Most conference host devices that support online meetings, such as all-in-one conference machines and central control hosts, support peripheral devices such as external sensors, and generally use some standard serial ports to achieve communication connections with peripheral devices. For example, standard serial ports such as RS232 and RS485 are used to focus on the communication protocol, thereby completing related scene linkage by connecting peripheral devices. However, in the current existing technology, in order to be able to connect with peripheral devices through serial ports, these conference host devices themselves need to have physical serial ports. However, due to the limited number of physical serial ports integrated in the conference host devices themselves, some conference host devices do not even have physical serial ports integrated. This limitation of the current device itself not having or having a limited number of integrated serial ports will result in the inability to connect to peripheral devices, which will bring a poor user experience. Increasing the number of serial ports in the hardware will increase the hardware cost. Summary of the Invention

[0003] In view of the deficiencies in the prior art, the purpose of the present invention is to provide a method for implementing serial port RS232 communication based on a single chip microcomputer, which can solve the problems described in the background technology.

[0004] The technical solution for achieving the purpose of the present invention is: a method for realizing serial port RS232 communication based on a single chip microcomputer, comprising the following steps:

[0005] Step 1: Determine the timeout time T of the first timing channel in the timer in the single-chip microcomputer according to the baud rate M of the serial port RS232 and the number of data bits N in the asynchronous serial data format, where N=5-8, and the timeout time T=(N+1)*(1 / M);

[0006] Step 2: Divide the general IO pins of the microcontroller into sending pins and receiving pins. In the idle state, set the sending pin to high level 1.

[0007] Detect the level status of the receiving pin. If it is detected that the receiving pin changes from a high level to a low level for the first time, it is determined that the current is the start bit of the RS232 serial port. When the start bit is detected, the timer enters the capture receive interrupt, that is, the timer starts timing, and the condition for entering the capture receive interrupt next time is set to detect the rising edge of the receiving pin level.

[0008] Step 3: When the next rising edge of the receive pin is detected, the current timer timing is read, and the timing time of the last capture receive interrupt is subtracted from the current timer timing to obtain the first current actual timing difference. The result of dividing the current actual timing difference by the single binary bit transmission time is the number of low-level 0s from the last interrupt to the current terminal, where the single binary bit transmission time = 1 / M;

[0009] Step 4: After calculating the number of low levels, set the condition for the next entry into the capture receive interrupt as detecting the falling edge of the receive pin level. After detecting the falling edge of the receive pin level, read the current timer time again, and subtract the current timer time from the last entry into the capture receive interrupt time to obtain the second current actual timing time difference. The result of dividing the current actual timing time difference by the single binary bit transmission time is the number of high level 1s from the last interrupt to the current terminal.

[0010] Step 5: Repeat steps 3 and 4, that is, by detecting the receiving pin level and sequentially setting the corresponding rising edge or falling edge as the condition for entering the capture receive interrupt, until the timer reaches the timeout time, the collected 1 and 0 are pieced together into a byte of data in the order of the detected levels, thereby completing one byte of data collection;

[0011] Step 6: Repeat steps 3 to 5 to complete the reception of multiple bytes of data, and set the second timing channel of the timer to output comparison mode to determine whether the sending pin outputs a high level or a low level, thereby controlling the sending pin to output a high level 1 or a low level 0 through programming.

[0012] Furthermore, M=11520 bit / s, N=8, the asynchronous serial data format of the serial port RS232 is 1 start bit, 8 data bits, and 1 stop bit, the single binary bit transmission time is 8.68 us, and the timeout period of the timer is 78 us.

[0013] The beneficial effects of the present invention are as follows: the present invention can increase communication serial ports without increasing the physical serial ports of the microcontroller itself, so that the microcontroller is no longer limited by the number of serial ports integrated into it, reducing the hardware cost of the microcontroller, and the simulated serial port can reduce the use of network expansion serial port devices, thereby reducing equipment costs. BRIEF DESCRIPTION OF THE DRAWINGS

[0014] Figure 1 It is a schematic diagram of the process of the present invention;

[0015] Figure 2 This is the data waveform diagram of two bytes of data 0x55 and 0xaa. DETAILED DESCRIPTION

[0016] Below, with reference to the accompanying drawings and specific embodiments, the present invention will be further described:

[0017] like Figure 1-Figure 2 As shown, a method for realizing serial port RS232 communication based on a single chip microcomputer includes the following steps:

[0018] Step 1: Determine the timeout time T of the first timing channel in the MCU timer based on the baud rate M of the RS232 serial port and the number of data bits N in the asynchronous serial data format, where N = 5-8. Here, the timeout time T = (N+1)*(1 / M).

[0019] The RS232 serial port uses an asynchronous serial data format for communication, which includes a start bit, data bits, and stop bits. The start bit is 1, the stop bit is 1, and the data bits are 5-8. Assuming the current RS232 baud rate is 115200 symbol / s (binary bits / second), and the asynchronous serial data format consists of 1 start bit, 8 data bits, 1 stop bit, and no parity bit, the single-bit transmission time required to transmit each binary bit is 1 / 115200 ≈ 0.00000868 seconds (seconds) = 8.68 microseconds (microseconds). This means the single-bit transmission time = 1 / baud rate M. Therefore, the timer timeout T = 8.68 * 9 = 78 microseconds (microseconds). A typical timer will have at least two timing channels, and the timeout can be set on the first timing channel (i.e., CH1). The timeout period of 78us means that after 78us, the timer enters the timeout terminal, clears the current timing and restarts the timing, and completes the transmission of one byte of data within this time period.

[0020] Step 2: Divide the general IO pins of the microcontroller into transmit pins and receive pins. In the idle state, set the transmit pin to high level 1, that is, when there is no data to be sent, the transmit pin always remains high level 1.

[0021] Detect the level status of the receiving pin. If the receiving pin is detected to be low for the first time after a high level, it is determined that the current is the start bit of the RS232 serial port. When the start bit is detected, the timer enters the capture receive interrupt, which means the timer starts timing, which also starts the timeout timer. The condition for entering the capture receive interrupt next time is set to detect a rising edge of the receiving pin level, that is, the receiving pin level changes from low to high.

[0022] Step 3: When the next rising edge of the receive pin is detected, the current timer timing is read, and the timing time of the current timer is subtracted from the timing time of the last capture receive interrupt to obtain the first current actual timing time difference. The current actual timing time difference is divided by the single binary bit transmission time (i.e. 8.68us), and the result is the number of low-level 0s from the last interrupt to the current terminal.

[0023] Step 4: After calculating the number of low-level interrupts, set the condition for the next capture / receive interrupt to detecting a falling edge on the receive pin. After detecting a falling edge on the receive pin, read the current timer again and subtract the time from the last capture / receive interrupt to obtain the second current actual time difference. Divide the current actual time difference by the single-bit transmission time (i.e., 8.68µs) to obtain the number of high-level 1s from the last interrupt to the current terminal.

[0024] Step 5: Repeat steps 3 and 4, that is, by detecting the receiving pin level and sequentially setting the corresponding rising or falling edge as the condition for entering the capture receive interrupt, until the timer reaches the timeout period. This also means that 78us have passed since the start bit was detected, indicating that one byte of serial port data transmission is complete. The collected 1s and 0s are pieced together into a byte of data in the order of the detected levels, thus completing one byte of data collection. Since the RS232 serial port transmits data byte by byte, the above steps can be repeated to complete the reception of multiple bytes of data.

[0025] Step 6: Repeat steps 3 to 5 to complete the reception of multiple bytes of data, and set the second timing channel (CH2) of the timer to output comparison mode to determine whether the sending pin outputs a high level or a low level, so that the sending pin can be controlled by software programming to output a high level 1 or a low level 0.

[0026] refer to Figure 2 , Figure 2The following waveform diagram shows two bytes of data, 0x55 and 0xaa. TTL levels represent the levels received or transmitted by the microcontroller. For example, when transmitting the byte 0x55, the binary representation of 0x55 is 01010101. A low level is first output for 8.68µs, representing the start bit. A high level is then output for 8.68µs, representing the least significant bit of the data in 0x55. Following this, a low level is output for 8.68µs, representing the least significant two bits of the data in 0x55. This continues in this manner until all eight binary bits of 0x55 are transmitted, completing the output of a single byte. This allows for byte-by-byte simulated serial port output, allowing the addition of communication serial ports without adding physical serial ports to the microcontroller (MCU).

[0027] The present invention can increase the communication serial port without increasing the physical serial port of the single-chip microcomputer itself, so that the single-chip microcomputer is no longer limited by the number of serial ports integrated by itself, reducing the hardware cost of the single-chip microcomputer, and the simulated serial port can reduce the use of network expansion serial port devices, thereby reducing equipment costs.

[0028] The embodiment disclosed in this specification is merely an illustration of one aspect of the present invention. The scope of protection of the present invention is not limited to this embodiment. Any other functionally equivalent embodiments fall within the scope of protection of the present invention. Those skilled in the art can make various other corresponding changes and modifications based on the technical solutions and concepts described above, and all such changes and modifications should fall within the scope of protection of the claims of the present invention.

Claims

1. A method for realizing serial port RS232 communication based on a single chip microcomputer, characterized in that: The steps include: Step 1: Determine the timeout time T of the first timing channel in the timer in the MCU according to the baud rate M of the serial port RS232 and the number of data bits N in the asynchronous serial data format, where N = 5-8, where the timeout time T = (N+1)*(1 / M); Step 2: Divide the general IO pins of the microcontroller into sending pins and receiving pins. In the idle state, set the sending pin to high level 1. Detect the level status of the receiving pin. If it is detected that the receiving pin changes from a high level to a low level for the first time, it is determined that the current is the start bit of the RS232 serial port. When the start bit is detected, the timer enters the capture receive interrupt, that is, the timer starts timing, and the condition for entering the capture receive interrupt next time is set to detect the rising edge of the receiving pin level. Step 3: When the next rising edge of the receive pin is detected, the current timer timing is read, and the timing time of the last capture receive interrupt is subtracted from the current timer timing to obtain the first current actual timing difference. The result of dividing the first current actual timing difference by the single binary bit transmission time is the number of low-level 0s from the last interrupt to the current interrupt, where the single binary bit transmission time = 1 / M; Step 4: After calculating the number of low levels, set the condition for the next entry into the capture receive interrupt as detecting the falling edge of the receive pin level. After detecting the falling edge of the receive pin level, read the timing of the current timer again, and subtract the timing of the last entry into the capture receive interrupt from the timing of the current timer to obtain the second current actual timing time difference. The result of dividing the second current actual timing time difference by the single binary bit transmission time is the number of high levels 1 from the last interrupt to the current interrupt. Step 5: Repeat steps 3 and 4, that is, by detecting the receiving pin level and sequentially setting the corresponding rising edge or falling edge as the condition for entering the capture receive interrupt, until the timer reaches the timeout time, the collected 1 and 0 are pieced together into a byte of data in the order of the detected levels, thereby completing one byte of data collection; Step 6: Repeat steps 3 to 5 to complete the reception of multiple bytes of data, and set the second timing channel of the timer to output comparison mode to determine whether the sending pin outputs a high level or a low level, thereby controlling the sending pin to output a high level 1 or a low level 0 through programming.

2. The method for realizing serial port RS232 communication based on single chip microcomputer according to claim 1, wherein: The M=11520 bit / s, N=8, the asynchronous serial data format of the serial port RS232 is 1 start bit, 8 data bits, and 1 stop bit. The single binary bit transmission time is 8.68us, and the timer timeout period is 78us.

Citation Information

Patent Citations

  • Circuit and method for automatically detecting Baud rate of multiple paths of serial communication interfaces

    CN102315983A

  • Method for achieving serial port function based on GPIO pin timer and terminal

    CN111984567A