Three methods of 51 single-chip analog serial port
After collecting various terminal data, the machine processes and stores it before reporting it to the management station either actively or passively. In such scenarios, a serial port is required for both data collection and communication. However, the commonly used 51 series microcontrollers typically only have one serial port. Therefore, to enable another serial port for reporting, it must be simulated through software.
The analog serial port discussed in this paper uses two I/O pins of the 51 series, such as P1.0 and P1.1. These pins are set to either high or low levels to represent bits in serial communication. For example, the start bit is set to low (0), while the stop bit is set to high (1). The data bits and parity bits are set accordingly based on the specific communication protocol.
The baud rate in serial communication refers to the time duration of each bit. A higher baud rate means a shorter time per bit. For instance, at a baud rate of 9600 bps, each bit takes approximately 0.104 milliseconds. This delay is achieved by executing a certain number of instructions, as each instruction cycle lasts between 1 to 3 machine cycles.
To achieve accurate timing, the MCU often uses a crystal oscillator frequency of 11.0592 MHz. With this frequency, each instruction cycle lasts (12/11.0592) microseconds. For a baud rate of 9600 BPS, the number of instruction cycles needed per bit is exactly 96, which simplifies the timing calculations significantly. Similarly, for 4800 BPS, it would be 192 cycles, and for 19200 BPS, it would be 48 cycles. Other baud rates may not yield integer values, making them less convenient for programming.
Now, let’s take the 11.0592 MHz crystal as an example and discuss three methods for simulating a serial port.
Method 1: Delay Method
This method involves using precise delays to control the timing of each bit. While it is relatively simple to implement, it can be challenging when receiving data due to the need for accurate sampling. It also requires knowing the exact number of instruction cycles for each statement. Although widely used, it is not recommended for Keil C users due to potential inaccuracies.
Method 2: Counter Method
This approach utilizes the internal timer of the 51 series microcontroller. By setting the initial value of the timer, it can generate an overflow after a specific number of instruction cycles. This allows for accurate timing without the need to manually calculate instruction cycles. The program continuously checks the overflow flag to determine when to send or receive the next bit.
Method 3: Interrupt Method
Similar to the counter method, this approach uses interrupts to trigger actions upon timer overflow. This allows for more efficient handling of serial communication, especially in real-time applications. The interrupt routine sets a flag, and the main program checks this flag to proceed with sending or receiving data.
In conclusion, simulating a serial port on a 51 series microcontroller can be done effectively using these three methods. Each has its own advantages and trade-offs, depending on the application requirements. The UART protocol, which uses RXD, TXD, and GND, is commonly used for asynchronous serial communication.
Acrylic Aquariums,Desk Acrylic Tank,Small Acrylic Aquariums,Appearance Acrylic Aquariums
Sensen Group Co., Ltd.  , https://www.sunsunglobal.com