How to write the communication code between serial port and PC based on AVR
Based on AVR serial port and PC communication code how to write, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
In order to continue to improve the function according to this code in the future, I specially put the code here and learn with you.
/ * *
AVR clock: 8.00MHz
* baud rate 9600 (51) 8-bit data, 1 bit stop, when baud rate is set to 19200, UBRR=25,4800=103
* Note: the baud rate setting of 9600 is the best, and other data are unstable. Reference manual for UBRR values corresponding to different baud rates.
* the PC sends a character to the development board, and the development board writes back its capital letters to the PC
* * /
# include
# include
Void USART_Init (unsigned int baud)
{
/ * set baud rate, check the value of baud data manual * /
UBRRH = (unsigned char) (baud > > 8)
UBRRL = (unsigned char) baud
/ * receiver and transmitter enable * /
UCSRB = (1)