"Parallel" communication: refers to the transmission of 8 bits of data through parallel lines at the same time. This greatly increases the data transmission speed, but the length of the parallel transmission line is limited. As the length increases, interference will increase, and data will be more prone to errors.
"Serial" communication: It can be described as one lane, while the parallel port has 8 lanes that can transmit 8 bits (one byte) of data at the same time. However, the parallel port is not fast, because the 8-bit channels interfere with each other. The transmission speed is limited. And when a transmission error occurs, 8 bits of data must be retransmitted at the same time. The serial port has no interference, and only one bit of data needs to be retransmitted. Therefore, it is faster than the parallel port.
There are many serial communication protocols, and the commonly used ones for microcontrollers are USART, SPI, TWI, 1-Wire, etc.
Serial communication can be divided into synchronous and asynchronous communication. In layman's terms, synchronous communication means that if you call me to eat, I will go to eat with you if I hear you. If I don't hear you, you keep calling until I tell you.
You heard it, and then we went to eat together. Asynchronous means you called me, and then went to eat by yourself. After I got the message, I might leave immediately, or I might wait until after get off work to go to eat.
Synchronous communication: The sender and receiver use synchronized clocks, and the data on the transmission line at the same time is the information to be transmitted.
Asynchronous communication: The transmission unit is character, characters are asynchronous, and the bits of characters are synchronous
USART: Asynchronous serial communication, commonly used for data transmission between MCU and MCU, MCU and PC.
Baud rate: A parameter that characterizes the communication speed. The unit is bit/second (b/s), which is the number of binary bits transmitted per second. For example, a baud rate of 9600 means that 9600 binary bits of data are transmitted per second. The sender and receiver must use the same baud rate. If the baud rates are different, normal communication will not be possible.
Full-duplex communication: means data can be sent and received at the same time.
Half-duplex communication: refers to the ability to only send or receive data at the same time.
Level conversion between MCU and PC communication: The voltage of MCU is generally TTL level, 0v-5v, and the PC serial port adopts RS-232 protocol, whose voltage range is -15-+15v. The levels are different and communication is impossible. To achieve communication, the level and logic relationship must be converted, and the MAX232 integrated chip is generally used for level conversion.
ATmega16 serial port structure: There is a full-duplex serial port with two communication lines, TXD: data transmission line, RXD: data reception line, the corresponding microcontroller external pins are PD1, PD0
Related registers:
UDR serial port data register,
UCSRA Serial port control and status register A
UCSRB Serial port control and status register B
UCSRC Serial port control and status register C
UBRRH,UBRRL Baud Rate Register
The generator divides the output clock of the baud rate generator by 2, 8 or 16, depending on the operating mode, as shown in the following figure:
For example: system clock frequency f=8MHZ, asynchronous normal mode (16 division), baud rate 9600
Then: UBRR=8000000/16*9600-1=52-1=51; Baud rate register assignment: UBRRH=0; UBRRL=51;
Steps:
1. Set asynchronous mode: UCSRC|=(1< 2. Set the data frame format: 8 data bits, 1 stop bit, UCSRC|=(1< 3. Set the baud rate register: UBRRL=51;UBRRH=0; 4. Enable sending and receiving: UCSRB|=(1< 5. Interrupt always enabled: SREG = 0x80; The microcontroller source program is as follows: #include #include #define fosc 8000000 //Crystal oscillator 8MHZ #define baud 9600 //Baud rate definition /*Port initialization function*/ void init(void) { PORTA = 0xFF; //PA port outputs high level DDRA = 0xFF; //PA port is set to output PORTD = 0X00; //The transmit and receive ports of USART are PD0 and PD1 respectively DDRD|=(1< } /*Serial port initialization function*/ void uart_init(void) { UCSRB = 0x00; UCSRA = 0x00; UCSRC |= (1< //The UCSRC register shares the same I/O address as the UBRRH register. When writing UCSRC, URSEL should be set to 1. UBRRL=51; //Set the baud rate register UBRRH=0; UCSRB |= (1 << TXEN)|(1 << RXEN); //Send and receive enable } /*Send data function*/ void send(unsigned char i) { while(!(UCSRA&(1< UDR=i; } /*The following is the function to receive data*/ unsigned char receive(void) { while(!(UCSRA&(1< return UDR; } /*Main function*/ void main(void) { unsigned char temp; heat(); uart_init(); while(1) { temp=receive(); //Receive data PORTA=~temp; //Invert the received data and send it to the PA port for display send(temp); //Send data to PC } }
//Function: Send data to the MCU through the PC serial port. After receiving the data, the MCU sends it to the PA port for display and then sends it back to the PC.
Previous article:AVRWARE++ Development Notes 1: Overview
Next article:Share: LCD1602 temperature detection display program (ATmega16)
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Show off your prizes [Review of SGP40]
- Now that the galvanometer has been repaired, what can I DIY with it?
- Questions about CSL_CGEM
- How to improve the power of voltage doubler rectifier circuit
- Mesh Wi-Fi system enhances smart home applications and makes connectivity easy
- Good tools can make work more efficient: Keysight Technology promotion season is here!
- Introduction to the SIG852 arbitrary waveform generator based on computer software (similar to a virtual oscilloscope)
- Evaluation information is here~~
- It is recommended that the forum organize the posts of the past year
- RFSOC wireless communication development platform