51 single-chip microcomputer and PC serial port Asynchronous Communication

Source: Internet
Author: User

There are many ways of communication between single-chip microcomputer and PC. The following program mainly uses the asynchronous serial communication of 51 single-chip microcomputer.
I. 51 Serial Communication pins of Single-chip Microcomputer

P3.1 is the txd pin (Transfer Data) of the microcontroller, and p3.2 is the rxd pin (receive data) of the microcontroller ).

The txd pin can be used to output data from the CPU. The rxd pin can read data from the serial data line.

Ii. 51 MCU serial communication control register scon, power control register pcon

Scon:


Sm0 and SM1 control the serial communication mode.

Sm0 SM1 Work Mode Description Baud Rate
0 0 0 Shift Register Fosc/12
0 1 1 10-bit asynchronous transceiver (8-bit data) Variable
1 0 2 11-bit asynchronous transceiver (9-bit data) Fosc/64 or fosc/32
1 1 3 11-bit asynchronous transceiver (9-bit data) Variable

Among them, method 1 is used in many cases.

SM2 is a multi-host communication control bit, SM2 = 1, allows multi-host communication, = 0 is not allowed, realize point-to-point communication. We will not discuss it here.

Tb8 is used to store the 9th-bit sent data. In Methods 2 and 3, apart from the start bit, data bit, and stop bit, the sent data also has a check bit, which is stored in tb8.

Rb8 is used to store 9th bits of received data. After the received code is sent, the data bit is stored in sbuf, and the check bit is stored in rb8. Through analysis, we can determine whether the accepted data is correct.

Ti indicates the flag for sending interrupt requests. When the sending data buffer is empty, Ti uses hardware to set 1 to notify the CPU that data has been sent, and software must be cleared.

RI indicates the sign used to receive interrupt requests. The data on the bus is sent to the MCU through the rxd pin serially, and the serial data is converted into parallel data through the shift register, and sent to the sbuf. If the received sbuf is full, set hardware to 1 to notify the CPU that a frame of data has been received and can be read. Software resetting is also required.

In pcon, only the highest bit smod is related to serial communication.

Smod = 1, and the baud rate doubles based on the original;

Otherwise, do not double.

Iii. Calculation of baud rate

For methods 1 and 3, the baud rate is calculated as follows: (2 ^ smod/32) * (overflow rate of T1 );

For method 2, fosc * (2 ^ smod/64 );

The method 1 is determined by the crystal oscillator and is a fixed value.

T1 is the timer 1. When the baud rate generator is used, it is generally used in the Mode 2, that is, the 8-bit counting mode automatically loaded. Th, Tl each has eight bits, but only some TL count, so the maximum count value is 255. After overflow, the number in TH is automatically loaded into TL.

Therefore, T1 overflow rate = fosc/{12x (256-th1 )};

So we can list the equation for the baud rate on the right. There are also many baud rate computing tools available on the Internet.

 

In conclusion, there are several steps for serial communication:

1. determine how the timer T1 works, that is, programming the tmod register;

2. Load the initial values of T1, that is, assign values to Th1 and TL1;

3. determine how the serial port works, that is, programming scon and pcon registers;

4. Start the T1 timer, that is, to make tr1 = 1;

5. If you use interrupt query, write the interrupt service function.

 

The above is probably the basic knowledge of serial communication. With this knowledge, you can write a program to practice it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.