Basic understanding of serial communication
The baud rate is the rate at which the binary data bits are sent, and it is customary to baud that we send a binary data for the duration of the =1/baud. Before the communication, SCM 1 and microcontroller 2 first to clearly agreed that their communication baud rate, must be consistent, send and receive both sides to normal communication.
After we have agreed on the speed, we have to consider the second question, when is the data starting and when is it over? Both early and late end receive errors. At the time of the UART communication, a byte is 8 bits, specifies that when no communication signal occurs, the communication line remains high, when the data is sent, first send a 0 to indicate the starting bit, and then send 8 bits of data, the data bit is first low and high, the number of bits sent after the first and then send a 1 stop bit, So we're going to send 8 bits of data, actually we send 10 bits, more out of two bits where one is the starting bit and one is the stop bit. And the receiver has maintained a high level, once a low level is detected, ready to start accepting data, accept 8-bit data, then detect the stop bit, and then prepare the next data receive.
Serial data transmission, in fact, is a time domain, which is the corresponding relationship of the signal with the change of time. For example, in the single-chip transmission pin, the left is the first occurrence, the right is after the occurrence, the data bit switching time is the baud rate of one second, if you can understand the concept of the time domain, behind a lot of communication sequence diagram is easy to understand.
On our computer, there will generally be a 9-pin serial interface, this serial interface is called RS232 interface, it and the UART communication is related, but since the notebook computer does not have a 9-pin serial port, and SCM communication more and more tend to use the USB virtual serial port.
Nine-pin serial port of foreman and female head
Male Upper 5 lower 4, Upper 5 from left to right for 18.104.22.168.5, next 4 from left to right 22.214.171.124;
Female head 5 down 4, Upper 5 from left to right for 126.96.36.199.1, Lower 4 from left to right 188.8.131.52;
RS232 interface A total of 9 pins, respectively defined is: 1, carrier detection dcd;2, receive data rxd;3, send data txd;4, data Terminal ready dtr;5, Signal ground sg;6, data ready dsr;7, Request send rts;8, clear send cts;9, Ringing hints ri. We want this serial port and our MCU to communicate, we only need to care about the 2 foot rxd, 3 feet txd and 5 feet GND can be
Although the names of these three pins are the same as the serial names on our microcontroller, they are not directly connected to the MCU, which is why? As we learn more and more, we have to slowly know that not all circuits are 5V high and 0V represents low. For the RS232 standard, it is a counter-logic, also known as negative logic. Why is it called negative logic? The voltage of its txd and rxd, the -3v~-15v voltage represents the 1,+3~+15v voltage represented is 0. The low level represents 1, and the high level represents 0, so it is called negative logic. Therefore, the computer's 9-pin RS232 serial port is not directly connected with a single-chip microcomputer, need to use a level conversion chip MAX232 to complete
This chip can achieve the standard RS232 serial port level into our microcontroller can recognize and withstand the UART 0v/5v level. From here everyone seems to slowly a little understand, in fact, RS232 serial port and UART serial port, their protocol type is the same, but the level standard is different, and MAX232 this chip is the role of the middleman, it converts the UART level to RS232 level, The RS232 level is also converted to the UART level, which enables a communication connection between the standard RS232 interface and the single-chip UART.
With the development of technology, industrial and RS232 serial communication, but the application of commercial technology, has been slowly using the USB to UART technology to replace the RS232 serial port, the vast majority of laptops have no serial port this thing, then we want to achieve a single-chip and computer communication between the How to do?
We only need to add a USB to the serial port chip on the circuit, can successfully realize the USB communication protocol and standard UART serial communication protocol conversion, on our Development Board, we use ch340t this chip
We need to use jumper caps to shorten the middle and bottom stitches together. ch340t the right side of the circuit is very simple, the power, crystal vibration, 6 feet and 7 feet of DP and DM respectively connected to the USB port 2 data pin up, 3 feet and 4 feet through the jumper to our microcontroller txd and rxd up.
The 3-pin position in the ch340t circuit adds a 4148 diode, which is a small trick. Because stc89c52 this SCM download program needs cold start, that is, the first point to download the power, power instantaneous single-chip opportunity to detect the need to download the program. Although the MCU VCC is controlled by a switch, but because the ch340t 3 feet is the output pin, if there is no this diode, switch after the stage MCU in the case of power loss, CH340T's 3 feet and the microcontroller's P3.0 (that is, RXD) pin together, there is a current through the pin into the back-stage circuit and to the rear-level capacitor charge, resulting in a certain magnitude of voltage, the voltage value, although only about two or three volts, but may affect the normal cold start. When diodes are added, the communication is not affected on one hand, and this undesirable effect can be eliminated in the other. This place can be a temporary understanding, if you do this kind of circuit, you can refer to.
uart serial baud rate, commonly used values are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200 equal rate. IO port analog UART serial Communication program is a simple demo program, we use the Serial debugging assistant issued a data, data plus 1, and then automatically return.
Serial debugging assistant, here we directly use the Stc-isp software comes with the serial debugging assistant, first the use of the serial debugging assistant to everyone to say, 11-6 shows. The first step to select the Serial assistant menu, the second step to select the hexadecimal display, the third step to select the hexadecimal send, the fourth step to select the COM port, this COM port and its own computer Device Manager in the COM port consistent, baud rate according to our program set a good choice, our program let a data bit duration is 1/ 9,600 seconds, then this place choose the baud rate is selected 9600, check the bit selected N, data bit 8, stop bit 1. The essence of the
Serial debugging assistant is to use the UART communication interface on the computer, send the data to our SCM, or we can receive the data sent by our SCM to the debugging assistant interface.
because of the first contact communication technology, so I put the back of the IO Analog serial communication program to explain, we can look at my explanation while watching the program, the underlying principle thoroughly understand.
The variable definition section goes without saying, look directly at main function. First of all, the baud rate of the communication set, where we configured the baud rate is 9600, then the serial port debugging assistant has to be 9600. When configuring the baud rate, we are using the Timer T0 mode 2. In mode 2, no longer TH0 represents high 8 bits, TL0 represents low 8 bits, and only TL0 is counting, and when TL0 overflows, it will not only make TF0 1, but also automatically reload the contents of TH0 into TL0. This has the advantage that we can put the desired timer initial value in advance TH0, when the TL0 overflow, TH0 automatically re-feed the initial value TL0, fully automatic, do not need to re-assign the program to TL0, configuration is very simple, we can see the program and calculate the initial value.
When the baud rate is set, open the interrupt and wait for the data sent by the serial debugging assistant. When the data is received, the first is to low-level detection while (PIN_RXD), if there is no low level indicates no data, once the low level is detected, enter the start receive function Startrxd (). The receiving function starts at the start of the half-baud rate cycle, and beginners may not be very clear here. We look back at our figure 11-2 inside the serial port data, if the data bit level changes in the time to read, because the timing of errors and signal stability of the problem is easy to read the wrong data, so we want to signal the most stable time to read the data. In addition to the position where the signal is changing, the other positions are stable, so we are now agreeing to read the level state in the middle of the signal so that we are sure to read it correctly.
Once the starting signal is read, we set the current state to receive state, and turn on the timer interrupt, the first time is half a cycle into the interrupt, the starting position of two times to judge, confirm that the starting bit is low level, not a disturbance signal. Each subsequent 1/9,600 seconds into an interrupt, and the state of the pin read into the rxdbuf inside. Waiting to receive the completion, we will add this Rxdbuf 1, and then through the TXD pin sent out, the same need to first send a start bit, and then send 8 data bits, and then send the end bit, after sending, the program runs to while (PIN_RXD), waiting for the start of the second round of signal reception.
The common communication transmission mode can be divided into single-work communication, half-duplex communication and full-duplex communication.
Simplex communication means that only one direction is allowed to transmit information in another direction, while the other party cannot return the message. Such as: TV Remote control, radio base, etc.
Half-duplex communication refers to the data can be spread between the two sides, but at the same time only one party to the other party, such as: Walkie-talkie
Full-duplex communication means that data is sent at the same time that it can receive data, both synchronously, just like our phone, and we can hear each other's voice as well.
IO port analog serial communication, so that we understand the nature of the serial communication, but our microcontroller program needs to constantly detect the scan MCU IO port received data, a large number of single-chip computer running time. At this time, there will be smart thinking, in fact, we are not very concerned about the process of communication, we only need a result of the communication, the final data received will be OK. In this way we can do a hardware module inside the microcontroller, let it automatically receive data, received, notify us about it, our 51 microcontroller inside there is such a UART module, to use it correctly, of course, the corresponding special function register must be configured well first.
51 MCU UART serial port structure by the serial port control register Scon, send and receive circuit three parts, first to understand the serial control register Scon.
Scon bit assignment for serial controller (address: 0x98)
Bit: symbol: RESET value: 0:ri:0;1:ti:0;2:rb8:0;3:tb8:0;4:ren:0;5:sm2:0;6:sm1:0;7:sm0:0;
0-bit RI: Receive interrupt flag bit when the receiving circuit receives the middle position of the stop bit, RI is set by hardware 1 and must be zeroed by software
1-bit TI: Send interrupt flag bit, when the sending circuit sent to the stop bit in the middle position, ti by hardware 1, must be cleared through the software.
2-bit RB8: the 9th bit of data received in modes 2 and 3 (rarely used), and Mode 1 is used to receive the stop bit.
3-bit TB8: the 9th bit of data to be sent in modes 2 and 3 (rarely used).
4-bit ren: Enable serial reception. The software is set to enable the receiver, and the software zeroing is forbidden.
5-bit SM2: multi-machine communication control bit (rarely used), Mode 1 direct zeroing.
6-bit SM1 and 7-bit SM0:
These two together determine the serial communication mode 0~ mode 3 A total of 4 modes. Our most commonly used is pattern 1, that is sm0=0,sm1=1, below we focus on the mode 1, the other mode withheld.
For the four modes of the serial port, mode 1 is the most commonly used, which is the 1-bit starting bit we mentioned earlier, the 8 bit data bit and the 1 bit stop bit. Below we will detail the work of mode 1 and the use of the method, as for the other 3 modes and this is the same, really encountered the need to use the time we go to consult the relevant information on the line.
When we use the IO port to simulate serial communication, the baud rate of the serial port is expressed by using the interrupt of the timer T0. In the hardware serial module, there is a dedicated baud rate generator to control the speed of sending and receiving data. For the STC89C52 microcontroller, this baud rate generator can only be generated by the timer T1 or timer T2, and cannot be generated by the timer T0, this and our simulation of the communication is completely different concept.
If you use timer 2, you need to configure additional registers, the default is to use Timer 1, we mainly use timer T1 as the baud rate generator to explain, Mode 1 baud rate generator must use the timer T1 mode 2, that is, automatic reload mode, the timer overload value calculation formula is:
TH1 = TL1 = 256-Crystal oscillator value/12/2/16/baud rate
And the baud rate is also related to a register, is a power management register Pcon, his highest bit can increase the baud rate by one times, that is, if write Pcon |= 0x80, the calculation formula becomes:
TH1 = TL1 = 256-Crystal oscillator value/12/16/baud rate
The meaning of the numbers in the formula here, 256 is the overflow value of the 8-bit timer, that is, the overflow value of the TL1, the crystal value on our Development Board is 11059200,12 is said 1 machine cycle equals 12 clock cycle, it is worth paying attention to this 16, we have to highlight. In the IO port analog serial communication to receive data, the acquisition is the middle position of this data, and actually the serial port module than our simulation to be more complex and accurate. He took the way to collect a signal 16 times, of which the 7th, 8, 9 times out, three times two times if it is high, then it is determined that this bit of data is 1, if two times is low, then it is assumed that this bit is 0, so that once the accidental interference read the wrong data, The final data is still guaranteed to be correct.
The transmitting and receiving circuit of serial communication has 2 identical sbuf registers in physics, their address is also 0x99, but one is used to send buffer, one is used to do receive buffer. That is to say, there are 2 rooms, the number of two rooms is the same, one of them only out of people, the other one only into people, so that we can achieve the UART full duplex communication, without interference between each other. But logically, we only operate sbuf each time, the single-chip opportunity automatically according to it is performed "read" or "write" operation to choose whether to receive Sbuf or send Sbuf, behind through the program, we will thoroughly understand the problem.
In general, the basic steps we have to write a serial communication program are as follows:
1, configure the serial port for mode 1.
2, configure the timer T1 Mode 2, that is, automatic reload mode.
3, according to the baud rate calculation TH1 and TL1 initial value, if necessary can be used to double the baud rate pcon.
4, open the Timer control register TR1, let the timer run up.
Here also to pay special attention to, that is, when using T1 to do the baud rate generator, do not make T1 interrupt.
Let us first look at the IO port analog serial communication directly to the use of the hardware UART module when the program code to see if the program is not a lot simpler, because most of the work of hardware modules for us to do. The program functions and IO ports are simulated exactly the same.
Let go of the Chinese characters we use, then our usual characters include 0~9 numbers, a~z/a~z letters, and various punctuation marks. So how do we show them in the microcontroller system? The ASCII code (American Standard Code for information interchange, i.e., the US Information Interchange standards Codes) can accomplish this mission: we know that in a single-chip microcomputer, a byte of data can have a total of 0~255 256 values, We take the 0~127. A total of 128 values give it another layer of meaning
We send a lowercase a in the character format, return a hexadecimal 0x61, the digital tube is also displayed in the 61,ASCII code table character a corresponds to the decimal is 97, equals 16 binary 0x61; we then send a number 1 in character format, returning a hexadecimal 0x31, The digital tube is also displayed in the 31,ascii table character 1 corresponds to the decimal is 49, equals 16 binary 0x31. It should be clear to everyone: the so-called hexadecimal send and hexadecimal receive, are based on the real value of the byte data, and the character format send and the character format receive, is the ASCII code table in the form of characters, but it is actually the final transmission of a byte of data. This form, of course, does not need to be remembered, understand it, use the time to come and check the line.
In-depth understanding of 51 single-chip computer serial communication