Atmega128 has two serial ports: USART0 and USART1
2. Formula calculation when U2X=1
UBRRL= (F_CPU/BAUDRATE/8-1)%256;
UBRRH= (F_CPU/BAUDRATE/8-1)/256;
//You can also find it according to the [Baud rate setting example] in the data sheet
//UBRRL = 0x2F; //set baud rate lo
//UBRRH = 0x00; //set baud rate hi
setting generally has errors, the error calculation method is: Error[%]=(BaudRate_ture/BaudRate-1)*100%;
5. Then set UCSR0B:
Bit7-RXCIE0 is the receive end interrupt enable, Bit6-TXCIE0 is the send end interrupt enable, Bit4-RXEN0 is the receive enable, and Bit3-TXEN0 is the send enable. In general, the reception uses the interrupt mode, so the receive end interrupt enable is used.
UCSR0B=0B10011000 //Send receive enable, receive end enable
Another way to write it is UCSR0B|=(1<< RXEN0)|(1<< TXEN0)|(RXCIE0);
If you are more skilled, you can directly set these registers in decimal or hexadecimal:
UCSR0B=0x98; or UCSR0B=152;
Keywords:Atmega128
Reference address:Atmega128 serial port detailed explanation
Taking USART0 as an example
The initialization of the serial port includes:
the selection of transmission mode: synchronous or asynchronous, the default is asynchronous mode, which can be selected by selecting the UMSEL bit in the USART control and status register UCSR0C. UMSEL is 0, which is asynchronous mode.
Baud rate setting: both parties in communication must have the same baud rate, which can be determined by setting the baud rate generation register UBRR0. UBRR0 is a two-byte 16-bit register, which can be divided into UBRR0H and UBRR0L. At the same time, the baud rate doubling bit UX2 in UCSR0A also works. When UX2 is 1, the set baud rate is doubled.
Transmission frame format setting: The default is 8 data bits and one stop bit
Send and receive enable and interrupt: By setting UCSR0B, select the enable of receiving or sending and whether to use interrupts as needed. For USART operations using interrupts, the interrupts must be turned off before initialization. The
control and status registers of USART0 are: UCSR0A, UCSR0B, UCSR0C, the baud rate register is UBRR0, namely UBRR0H and UBRR0L, and the data register is UDR0
The initialization of the serial port includes:
the selection of transmission mode: synchronous or asynchronous, the default is asynchronous mode, which can be selected by selecting the UMSEL bit in the USART control and status register UCSR0C. UMSEL is 0, which is asynchronous mode.
Baud rate setting: both parties in communication must have the same baud rate, which can be determined by setting the baud rate generation register UBRR0. UBRR0 is a two-byte 16-bit register, which can be divided into UBRR0H and UBRR0L. At the same time, the baud rate doubling bit UX2 in UCSR0A also works. When UX2 is 1, the set baud rate is doubled.
Transmission frame format setting: The default is 8 data bits and one stop bit
Send and receive enable and interrupt: By setting UCSR0B, select the enable of receiving or sending and whether to use interrupts as needed. For USART operations using interrupts, the interrupts must be turned off before initialization. The
control and status registers of USART0 are: UCSR0A, UCSR0B, UCSR0C, the baud rate register is UBRR0, namely UBRR0H and UBRR0L, and the data register is UDR0
1. Initialization:
Before setting the baud rate, turn off all uses of USART0, including enabling and interrupts:
UCSR0B=0B00000000; //Turn off USART0
2. Set UCSR0A:
UCSR0A can write Bit0, Bit1, Bit6, and the other 5 bits are status bits. We generally use Bit1-U2X0. When this bit is 1, the baud rate division factor is reduced from 16 to 8, which can effectively double the transmission rate of the asynchronous communication mode. However, this bit only affects asynchronous operations. This bit should be cleared when using synchronous operations.
UCSR0A=0B00000000; //Do not use double-speed transmission
3. Set UCSR0C:
Bit6-UMSEL0: USART0 mode selection, 0 is asynchronous mode, 1 is synchronous mode
Bit5: 4-UPM01: 0: Parity check mode, 00 is disabled, 01 is reserved, 10 is even parity, 11 is odd parity
Bit3-USBS0: Stop bit selection, 0 stop bit is 1 bit, 1 stop bit is 2 bits
Bit2: 1-UCSZ01: 0: Character length, when UCSZ02 is 0, 00 means 5 bits, 01 means 6 bits, 10 means 7 bits, and 11 means 8 bits. When UCSZ02 is 1, 11 means 9 bits. (UCSZ02 is a register in UCSR0B)
Before setting the baud rate, turn off all uses of USART0, including enabling and interrupts:
UCSR0B=0B00000000; //Turn off USART0
2. Set UCSR0A:
UCSR0A can write Bit0, Bit1, Bit6, and the other 5 bits are status bits. We generally use Bit1-U2X0. When this bit is 1, the baud rate division factor is reduced from 16 to 8, which can effectively double the transmission rate of the asynchronous communication mode. However, this bit only affects asynchronous operations. This bit should be cleared when using synchronous operations.
UCSR0A=0B00000000; //Do not use double-speed transmission
3. Set UCSR0C:
Bit6-UMSEL0: USART0 mode selection, 0 is asynchronous mode, 1 is synchronous mode
Bit5: 4-UPM01: 0: Parity check mode, 00 is disabled, 01 is reserved, 10 is even parity, 11 is odd parity
Bit3-USBS0: Stop bit selection, 0 stop bit is 1 bit, 1 stop bit is 2 bits
Bit2: 1-UCSZ01: 0: Character length, when UCSZ02 is 0, 00 means 5 bits, 01 means 6 bits, 10 means 7 bits, and 11 means 8 bits. When UCSZ02 is 1, 11 means 9 bits. (UCSZ02 is a register in UCSR0B)
Eg:
UCSR0C=0B00000110 //Asynchronous mode, parity check disabled, stop bit is 1 bit, data bit is 8 bits
4. Set UBRR:
The setting of UBRR is related to these parameters: U2X0, CPU frequency, baud rate
When U2X0 is 0, that is, in asynchronous normal mode, the calculation formula of UBRR is:
1. Formula calculation when U2X=0
UBRR0L= (F_CPU/BAUDRATE/16-1)%256;
UBRR0H= (F_CPU/BAUDRATE/16-1)/256;
UCSR0C=0B00000110 //Asynchronous mode, parity check disabled, stop bit is 1 bit, data bit is 8 bits
4. Set UBRR:
The setting of UBRR is related to these parameters: U2X0, CPU frequency, baud rate
When U2X0 is 0, that is, in asynchronous normal mode, the calculation formula of UBRR is:
1. Formula calculation when U2X=0
UBRR0L= (F_CPU/BAUDRATE/16-1)%256;
UBRR0H= (F_CPU/BAUDRATE/16-1)/256;
2. Formula calculation when U2X=1
UBRRL= (F_CPU/BAUDRATE/8-1)%256;
UBRRH= (F_CPU/BAUDRATE/8-1)/256;
//You can also find it according to the [Baud rate setting example] in the data sheet
//UBRRL = 0x2F; //set baud rate lo
//UBRRH = 0x00; //set baud rate hi
setting generally has errors, the error calculation method is: Error[%]=(BaudRate_ture/BaudRate-1)*100%;
5. Then set UCSR0B:
Bit7-RXCIE0 is the receive end interrupt enable, Bit6-TXCIE0 is the send end interrupt enable, Bit4-RXEN0 is the receive enable, and Bit3-TXEN0 is the send enable. In general, the reception uses the interrupt mode, so the receive end interrupt enable is used.
UCSR0B=0B10011000 //Send receive enable, receive end enable
Another way to write it is UCSR0B|=(1<< RXEN0)|(1<< TXEN0)|(RXCIE0);
If you are more skilled, you can directly set these registers in decimal or hexadecimal:
UCSR0B=0x98; or UCSR0B=152;
Serial port initialization function:
void uart_init(void) //Serial port 0 initialization
{
UCSR0B=0x00; //disable while setting baud rate
UCSR0A=0B00000000; //Bit1 is 1, then send at double speed U2X=0
UCSR0C=0x06; //B00000110 //Parity mode none, eight data bits, one stop bit
UBRR0L=103; //B00011001 baud rate: 9600 Bps
UBRR0H=0x00; //Error rate: 0.156%
UCSR0B=0x98;
}
{
UCSR0B=0x00; //disable while setting baud rate
UCSR0A=0B00000000; //Bit1 is 1, then send at double speed U2X=0
UCSR0C=0x06; //B00000110 //Parity mode none, eight data bits, one stop bit
UBRR0L=103; //B00011001 baud rate: 9600 Bps
UBRR0H=0x00; //Error rate: 0.156%
UCSR0B=0x98;
}
After initialization, USART0 is read and written.
Bit5-UDRE0 flag of UCSR0A indicates whether the transmit buffer UDR0 is ready to receive new data. UDRE0 is 1, which means the buffer is empty and can receive new data. The UDRE0 flag can also be used to generate a register empty interrupt. After reset, UDRE0 is set, indicating that the transmitter is ready:
when UDRE0 is 1!(UCSR0A&(1<< UDRE0)) is 0
void putchar(uchar c) //Serial port 0 sends characters
{
while(!(UCSR0A&(1<< UDRE0))); //while(!(UCSR0A&32)); Indicates that the transmitter is ready.
UDR0=c;
}
Bit5-UDRE0 flag of UCSR0A indicates whether the transmit buffer UDR0 is ready to receive new data. UDRE0 is 1, which means the buffer is empty and can receive new data. The UDRE0 flag can also be used to generate a register empty interrupt. After reset, UDRE0 is set, indicating that the transmitter is ready:
when UDRE0 is 1!(UCSR0A&(1<< UDRE0)) is 0
void putchar(uchar c) //Serial port 0 sends characters
{
while(!(UCSR0A&(1<< UDRE0))); //while(!(UCSR0A&32)); Indicates that the transmitter is ready.
UDR0=c;
}
Serial port 0 sends a string:
void putstr(uchar *s) //Serial port 0 sends a string
{
while(*s)
{
putchar(*s);
s++;
}
}
void putstr(uchar *s) //Serial port 0 sends a string
{
while(*s)
{
putchar(*s);
s++;
}
}
RXC0 is Bit7 in UCSR0A, which is the status bit of USART0 receiving end. RXC0 is set when there is unread data in the receiving buffer, otherwise it is cleared.
Serial port 0 receives characters:
uchar getchar(void) //Serial port 0 receives characters
{
while(!(UCSR0A&(1<< RXC0)));
return UDR0;
}
uchar getchar(void) //Serial port 0 receives characters
{
while(!(UCSR0A&(1<< RXC0)));
return UDR0;
}
These files are generally included in programs that use the serial port:
#include
#include
#include
#include
avr/io.h contains definitions of various registers, avr/signal.h and avr/interrupt.h contain definitions of interrupts, and avr/delay.h contains delay functions.
#include
#include
#include
#include
avr/io.h contains definitions of various registers, avr/signal.h and avr/interrupt.h contain definitions of interrupts, and avr/delay.h contains delay functions.
Routine:
Serial communication (USART0)
/***************************************************************************/
/*Serial port 0 test program */
/*Target device: ATmega128 */
/*Crystal: RC 8MHZ */
/*Compilation environment: ICCAVR 7.13A */
/*Date: March 14, 2010*/
/*E-Mail:number007cool@163.com */
/***************************************************************************/
/*Serial port 0 test program */
/*Target device: ATmega128 */
/*Crystal: RC 8MHZ */
/*Compilation environment: ICCAVR 7.13A */
/*Date: March 14, 2010*/
/*E-Mail:number007cool@163.com */
/***************************************************************************/
/*********************************Include header files********************************/
#include
#include
#include
#include
/***********************************Macro definition**********************************/
#define fosc 8000000 //Crystal oscillator 8MHZ
#define baud 2400 //Baud rate
#define fosc 8000000 //Crystal oscillator 8MHZ
#define baud 2400 //Baud rate
/****************************************************************************
Function: uart0 initialization program
Entry parameter:
Exit parameter:
************************************************************************/
void uart0_init(void)
{
UCSR0B = 0x00; //Close UART00
UCSR0A = 0x00; //Do not use double-speed transmission (asynchronous)
UCSR0C =(1<
UBRR0L=(fosc/16/(baud+1))%256; //Calculation formula under normal asynchronous conditions
UBRR0H=(fosc/16/(baud+1))/256;
UCSR0B =(1<
}
Function: uart0 initialization program
Entry parameter:
Exit parameter:
************************************************************************/
void uart0_init(void)
{
UCSR0B = 0x00; //Close UART00
UCSR0A = 0x00; //Do not use double-speed transmission (asynchronous)
UCSR0C =(1<
UBRR0H=(fosc/16/(baud+1))/256;
UCSR0B =(1<
/****************************************************************************
Function: uart0 sends single-byte data
Input parameter:
cExit parameter:
****************************************************************************/
void putchar0(unsigned char c)
{
while (!(UCSR0A&(1<
UDR0=c; //Load the data to be sent into the UDR0 register
}
/****************************************************************************
Function: uart0 receives single-byte data
Input parameter:
Exit parameter:
****************************************************************************/
unsigned char getchar0(void)
{
while(!(UCSR0A& (1<
return UDR0;
}
/****************************************************************************
Function: uart0 sends string data
Input parameter: *
sExit parameter:
****************************************************************************/
void puts0(char *s)
{
while (*s)
{
putchar0(*s);
s++;
}
putchar0(0x0a);//Carriage return line feed
//putchar0(0x0d);
}
/****************************************************************************
Function: main program
Input parameter:
Exit parameter:
****************************************************************************/
void main(void)
{
unsigned char i;
Function: uart0 sends single-byte data
Input parameter:
cExit parameter:
****************************************************************************/
void putchar0(unsigned char c)
{
while (!(UCSR0A&(1<
}
/****************************************************************************
Function: uart0 receives single-byte data
Input parameter:
Exit parameter:
****************************************************************************/
unsigned char getchar0(void)
{
while(!(UCSR0A& (1<
}
/****************************************************************************
Function: uart0 sends string data
Input parameter: *
sExit parameter:
****************************************************************************/
void puts0(char *s)
{
while (*s)
{
putchar0(*s);
s++;
}
putchar0(0x0a);//Carriage return line feed
//putchar0(0x0d);
}
/****************************************************************************
Function: main program
Input parameter:
Exit parameter:
****************************************************************************/
void main(void)
{
unsigned char i;
uart0_init(); //UART0 initialization
puts0("HELLO!");
puts0("HELLO!");
while(1)
{
puts0("test ok!");
}
}
{
puts0("test ok!");
}
}
Serial communication (USART1)
/*******************************************************************************/
/*Serial port 1 test program */
/*Target device: ATmega128 */
/*Crystal oscillator: RC 8MHZ */
/*Selected baud rate: 9600 (can also be set separately), after changing the baud rate, you need to unplug the power supply and plug it in again before using it*/
/*Compilation environment: ICCAVR 7.13 */
/*E-Mail:number007cool@163.com */
/*Time: January 14, 2010 */
/***************************************************************************/
/*Serial port 1 test program */
/*Target device: ATmega128 */
/*Crystal oscillator: RC 8MHZ */
/*Selected baud rate: 9600 (can also be set separately), after changing the baud rate, you need to unplug the power supply and plug it in again before using it*/
/*Compilation environment: ICCAVR 7.13 */
/*E-Mail:number007cool@163.com */
/*Time: January 14, 2010 */
/***************************************************************************/
/*********************************Include header files********************************/
#include
#include
#include
#include
/***********************************Macro definition**********************************/
#define fosc 8000000 //Crystal oscillator 8MHZ
#define baud 9600 //Baud rate
#define fosc 8000000 //Crystal oscillator 8MHZ
#define baud 9600 //Baud rate
/****************************************************************************
Function: uart1 initialization program
Entry parameter:
Exit parameter:
************************************************************************/
void uart1_init(void) //USART1 initialization
{
UCSR1B = 0x00; //Close USART1
UCSR1A = 0x00; //Not suitable for double-speed transmission
UCSR1C = (1<
UBRR1L=(fosc/16/(baud+1))%256;//Under asynchronous normal mode, UBRR calculation formula
UBRR1H=(fosc/16/(baud+1))/256;
UCSR1B =(1<
}
Function: uart1 initialization program
Entry parameter:
Exit parameter:
************************************************************************/
void uart1_init(void) //USART1 initialization
{
UCSR1B = 0x00; //Close USART1
UCSR1A = 0x00; //Not suitable for double-speed transmission
UCSR1C = (1<
UBRR1H=(fosc/16/(baud+1))/256;
UCSR1B =(1<
/****************************************************************************
Function: uart1 sends single-byte data
Input parameter:
cExit parameter:
****************************************************************************/
void putchar1(unsigned char c)//Serial port 1 sends characters
{
while (!(UCSR1A&(1<
UDR1=c;
}
/********************************************************************************
Function: uart1 receives single-byte data
Input parameter:
Exit parameter:
****************************************************************************/
unsigned char getchar1(void) //Serial port 1 receives and recovers data
{
while(!(UCSR1A& (1<
return UDR1; //Return the received character
}
/****************************************************************************
Function: uart1 sends string data
Input parameter: *
sExit parameter:
****************************************************************************/
void puts1(char *s)
{
while (*s)
{
putchar1(*s);
s++;
}
putchar1(0x0a);//Carriage return and line
feedputchar1(0x0d);
}
/****************************************************************************
Function: main program
Input parameter:
Exit parameter:
****************************************************************************/
void main(void)
{
unsigned char i;
Function: uart1 sends single-byte data
Input parameter:
cExit parameter:
****************************************************************************/
void putchar1(unsigned char c)//Serial port 1 sends characters
{
while (!(UCSR1A&(1<
}
/********************************************************************************
Function: uart1 receives single-byte data
Input parameter:
Exit parameter:
****************************************************************************/
unsigned char getchar1(void) //Serial port 1 receives and recovers data
{
while(!(UCSR1A& (1<
}
/****************************************************************************
Function: uart1 sends string data
Input parameter: *
sExit parameter:
****************************************************************************/
void puts1(char *s)
{
while (*s)
{
putchar1(*s);
s++;
}
putchar1(0x0a);//Carriage return and line
feedputchar1(0x0d);
}
/****************************************************************************
Function: main program
Input parameter:
Exit parameter:
****************************************************************************/
void main(void)
{
unsigned char i;
uart1_init();
puts1("HELLO!");
puts1("HELLO!");
while(1)
{
puts1("test ok!");
}
}
{
puts1("test ok!");
}
}
Previous article:AVR microcontroller learning experience
Next article:AVR external crystal oscillator whether the test program is oscillating
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- 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
Guess you like
- Design of analog signal source for remote sensing images with wide spectrum coverage
- Summary of commonly used electronic test and measurement instruments
- 【GD32L233C-START Review】7. Temperature sensor driver
- Please recommend a chip that can convert 9V battery to 3.3V
- F4 USB routine opening problem
- Master guide fpga altera to divide the 50MHZ clock to read rom data, mif file
- Today's live broadcast: TI 60G mmWave sensor overview and application introduction
- TMS320F28335-CAN module routine explanation
- STM32 internal temperature sensor experiment summary
- Matlab programming experience