Parallel and serial basic communication mode
1. Parallel communication mode
It is usually the data bytes that are transmitted at the same time with multiple data lines.
The parallel communication control is simple, the transmission speed is fast, because the transmission line is many, the long-distance transmission time high cost and the receiver's each time receives the difficulty.
2. Serial communication mode
is to divide the data bytes into one-bit form and transmit them on a single transmission line one by one.
The serial communication transmission line is few, the long-distance transmission cost is low, and may use the telephone network and so on off-the-shelf equipment, but the data transmission control is more complex than the parallel communication.
3. Asynchronous Serial communication mode
Asynchronous communication refers to the sending and receiving of communication devices using their respective clocks to control the transmission and reception of the data. In order to reconcile the two sides, the sending and receiving device's clock is required to be as consistent as possible.
Asynchronous communication is transmitted in units of characters (composed frames), and the gap between characters and characters is arbitrary, but each character is transmitted at a fixed time, that is, there is not necessarily an integer multiple of "bit interval" between characters, but the distance between the characters of the same word is an integer multiple of "bit interval".
Data format for asynchronous communication:
Asynchronous communication is not required to send and receive the clock of the strict consistency, easy to implement, the device cost is small, but each character to append two to two bits for the start and end, there are gaps between the frames, so the transmission efficiency is not high.
4. Synchronous Serial Communication mode
Synchronous communication is to establish the sender clock to the receiver clock direct control, so that both sides reached full synchronization. At this point, the distance between the bits of the transmitted data is an integer multiple of the "bit interval", while the transmitted characters do not leave gaps between them, that is, the synchronization relationship is maintained, and the character synchronization is maintained. The sender's synchronization with the receiver can be implemented in two ways.
Synchronous communication is a specific bit combination of "01111110" as the start and end of the frame of the flag, the transmission of a frame of data can be any bit. So the transmission is more efficient, but the hardware device is more complex than asynchronous communication.
Character-oriented synchronization format:
Bit-oriented synchronization format:
5. The format of serial communication
Simplex: Data transmission can only be in one direction and cannot be reversed.
Half-duplex: refers to data transmission can be in two directions, but requires time-sharing.
Full Duplex: The exponential data can be transmitted in both directions at the same time.
6. Error checking of serial communication
Parity: When data is sent, the 1 bits trailing by the data bits are parity bits (1 or 0). Odd check, the number of "1" in the data and the number of the check digit "1" should be odd, even if the number of "1" in the data and the number of the check digit "1" should be even. When the character is received, the number of "1" is checked, if inconsistencies are found, there is a mistake in the process of transmitting data.
Code and Checksum: the sender sums (or each byte xor) the data block that is being sent, producing a check character (checksum) of one byte appended to the end of the data block. The receiver receives the data at the same time the data block (except check byte) sum (or each byte xor), the result is compared with the sender's "checksum", the matching is error-free, otherwise it is believed that there is a mistake in the transmission process.
Cyclic redundancy check: a kind of mathematical operation is used to realize the cyclic check between the valid information and the check bit, which is often applied to the transmission of the disk information, the integrity check of the storage area, etc. This kind of calibration method has strong error correction ability and is widely used in synchronous communication.
7. Serial Communication Interface
RS-232
The RS-232C interface specifies a 25-pin connector with a clear definition of the size of the connector and the placement of each pin. (Yangtou)
MAX232: Converts the TTL level from 0V and 5V to between 3v~15v or -3v~-15v.
Discrete components achieve RS-232 level and TTL level conversion circuitry.
TXD for low Q3 conduction pcrxd (if PC interrupts accepted), TXD send high level, Q3 cut-off, pcrxd internal high resistance, PCTXD usually -3~-15v, pull down pcrxd.
The PC sends the signal: When the PCTXD is low, the Q4 cutoff, the RXD is high, and when the Pctxd is high, Q4 conduction, Rxd is Q4 down. Bidirectional conversion.
MAX232 chip achieves RS232 level and TTL level conversion
CH340 usb-com Conversion Chip
8. Relationship between baud rate and timer initial value
The bit rate is the number of bits per second (bps) that transmits the binary code. If 240 characters are transmitted per second, and each character format contains 10 bits (one starting bit, one stop bit, 8 data bits), then the bit rate is:10 bits x240/sec = 2400 bps
Baud rate of mode 0 = FOSC/12
Baud rate of Mode 2 = (2^SMOD/64) · Fosc
Baud rate of Mode 1 = (2^SMOD/32) • (T1 overflow rate)
Baud rate of Mode 3 = (2^SMOD/32) • (T1 overflow rate)
Fosc is the crystal oscillator frequency. The Smod is the highest bit of the Pcon register. T1 overflow rate is the frequency of timer T1 overflow.
T1 Overflow Rate =fosc/{$ * [256-(TH1)]}
Mode 1, when the timer is full overflow, automatically enter the interrupt service program, and then we need to manually again to the timer to install the initial value.
Mode 2, when the timer is full overflow, the single-chip opportunity automatically installed its initial value, and no need to enter the interrupt service program for any processing. The timer overflow rate will be absolutely stable.
10. Registers
Power Management Register Pcon (no bit addressing)
Bit sequence number |
D7 |
D6 |
D5 |
D4 |
D3 |
D2 |
D1 |
D0 |
Bit symbol |
Smod |
(SMOD0) |
(LVDF) |
(p0f) |
GF1 |
GF0 |
Pd |
Idl |
smod-this bit is related to serial port communication baud rate
Smod=0: Baud rate is normal when serial port mode is 123
Smod=1: The baud rate doubles when the serial port mode is 123.
Serial Port Control register Scon (addressable)
Bit sequence number |
D7 |
D6 |
D5 |
D4 |
D3 |
D2 |
D1 |
D0 |
Bit symbol |
SM0 |
SM1 |
SM2 |
REN |
TB8 |
RB8 |
TI |
RI |
Serial port operating mode
SM0 |
SM1 |
Way |
Function description |
0 |
0 |
0 |
Synchronous shift Register mode (typically used to extend I/O ports) |
0 |
1 |
1 |
10-bit asynchronous transceiver (8-bit data) with variable baud rate |
1 |
0 |
2 |
11-bit asynchronous transceiver (9-bit data), fixed baud rate |
1 |
1 |
3 |
11-bit asynchronous transceiver (9-bit data) with variable baud rate |
sm2-Multi-machine communication control bit
Mainly used in mode 2 and Mode 3. When the receiver is Sm2=1, the received RB8 can be used to control whether the RI is activated (rb8=0 does not activate RI, the information received is discarded, the data received at Rb8=1 is entered into sbuf, and the RI is activated to read the data from the SBUF in the interrupt service). When sm2=0, regardless of the received RB8 of 0 and 1, the received data can be entered into sbuf, and activate Ri (that is, at this time RB8 does not have the ability to control RI activation). By controlling the SM2, multi-machine communication can be realized.
In mode 0 o'clock, the SM2 must be 0. At Mode 1 o'clock, if sm2=1, RI is only set to 1 if a valid stop bit is received.
ren-allow serial receive bits
When the software is ren=1, the serial port receives the data, and if the software is ren=0, it is forbidden to receive it.
tb8-in mode 2 or Mode 3, is the nineth bit of data sent
Software can be used to specify its role. Can be used as a parity bit for data, or as a flag bit for address frame/data frames in multi-machine communication.
In mode 0 and mode 1, the bit is not used.
rb8-in mode 2 or Mode 3, is the nineth digit to receive the data
Flag bit as parity bit or address frame/data frame. At Mode 1 o'clock, if sm2=0, the RB8 is the received stop bit.
ti-send interrupt flag bit
In mode 0 o'clock, when the serial sends the 8th bit of data at the end, or in other ways, the start of the serial send stop bit, the internal hardware makes Ti 1, and interrupts the request to the CPU. In the Interrupt service program, it must be cleared by the software 0 to cancel the interrupt request.
ri-receive interrupt flag bit
In mode 0 o'clock, when the serial receives the 8th bit of data at the end, or in other ways, the middle of the serial receive stop bit, the RI is set 1 by the internal hardware and an interrupt request is made to the CPU. You must also use the software to clear 0 in the Interrupt Service program to cancel the interrupt request.
11. Steps
The serial port should be initialized before it works, mainly by setting the timer 1 to generate the baud rate, serial port control, and interrupt control. The steps are as follows:
① determine how the T1 works (programming Tmod registers) ② calculate the initial value of T1, load TH1, Tl1③ start T1 (Tcon bit in programming TR1) ④ determine serial port control (programmed Scon Register)
⑤ serial port in the interrupt mode to operate the interrupt setting (programming IE, IP register)
10-Day Learning microcontroller DAY4 serial port communication