AVR microcontroller serial port USART and PC communication example, explain the principle and procedure

Publisher:sumigLatest update time:2019-10-12 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

"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;


//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.


The microcontroller source program is as follows:

  1. #include

  2. #include

  3. #define fosc 8000000 //Crystal oscillator 8MHZ

  4. #define baud 9600 //Baud rate definition

  5. /*Port initialization function*/

  6. void  init(void)

  7. {

  8. PORTA = 0xFF; //PA port outputs high level

  9. DDRA = 0xFF; //PA port is set to output 

  10. PORTD = 0X00; //The transmit and receive ports of USART are PD0 and PD1 respectively

  11. DDRD|=(1<

  12. }

  13. /*Serial port initialization function*/

  14. void uart_init(void) 

  15. {

  16. UCSRB = 0x00;

  17. UCSRA = 0x00;

  18. UCSRC |= (1<

  19. //The UCSRC register shares the same I/O address as the UBRRH register. When writing UCSRC, URSEL should be set to 1.

  20. UBRRL=51; //Set the baud rate register

  21. UBRRH=0;

  22. UCSRB |= (1 << TXEN)|(1 << RXEN); //Send and receive enable 

  23. }

  24. /*Send data function*/

  25. void send(unsigned char i)

  26. {

  27. while(!(UCSRA&(1<

  28. UDR=i;

  29. }

  30. /*The following is the function to receive data*/

  31. unsigned char  receive(void) 

  32. {

  33. while(!(UCSRA&(1<

  34. return UDR;

  35. }

  36. /*Main function*/

  37. void main(void) 

  38. {

  39. unsigned char temp;

  40. heat();

  41. uart_init();

  42. while(1) 

  43. {

  44.   temp=receive(); //Receive data

  45.   PORTA=~temp; //Invert the received data and send it to the PA port for display   

  46.   send(temp); //Send data to PC

  47. }

  48. }


Keywords:AVR Reference address:AVR microcontroller serial port USART and PC communication example, explain the principle and procedure

Previous article:AVRWARE++ Development Notes 1: Overview
Next article:Share: LCD1602 temperature detection display program (ATmega16)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号