The serial communication function of the AVR microcontroller is very powerful. Let me briefly talk about some of my experiences when learning it.
The experimental board is AVR-ATMEGA128A microcontroller, crystal oscillator 8MHz.
1. Initialization: Before setting the baud rate, close all uses of USART0, including enabling and interrupts.
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 and should be cleared when using synchronous operations.
3. Set UCSR0C: Bit6-UMSEL0:USART0 mode selection 0 for asynchronous mode, 1 for synchronous mode.
Bit5:4-UPM01:0: Parity check mode, 00 disabled, 01 reserved, 10 even check, 11 odd check.
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).
4. Set UBRR: The setting of UBRR is related to these parameters: U2X0, CPU frequency, baud rate
5. Then set UCSR0B: Bit7-RXCIE0 is the reception end interrupt enable, Bit6-TXCIE0 is the transmission end interrupt enable, Bit4-RXEN0 is the reception enable, and Bit3-TXEN0 is the transmission enable. Generally, reception uses the interrupt mode, so the reception end interrupt enable is used.
Here is the source code:
/*
* usart.c
*
* Created: 2011/7/2 15:48:36
* Author: YIN
*/
#include
#include
#include
volatile unsigned char temp;
void init()
{
UCSR0B=0x00;
UBRR0H=0x00;
UBRR0L=51;
UCSR0A =0x00;
UCSR0C=0x06;
UCSR0B=0x98;
sei();
DDRF=0xff;
PORTF=0xff;
DDRD=0xf0;
DDRA=0xff;
}
void delay()
{
unsigned int i;
i=100;
while(i-- );
}
int main(void)
{
init();
while (1); //wait for interrupt
}
SIGNAL (SIG_UART0_RECV) //Send the data sent by the host computer back to the host computer for display
{
temp=UDR0;
PORTA=~temp;
UDR0=temp;
}
Previous article:AVR microcontroller-digital tube 1
Next article:Ultrasonic ranging module + AVR microcontroller control program
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- 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
- Today at 10:00 AM, live broadcast with prizes: ADI's smoke detector integrated solution based on ADPD188BI
- 【ART-Pi Review】I: Hardware Appreciation and Related Information Collection and Arrangement
- Annual sports statistics
- EEWORLD University Hall----TI Industrial Application Seminar
- Transfer: Cortex-M development from the perspective of files (2) - Link files
- 【GD32E503 Review】——05.LCD Part 1: Testing the screen refresh speed
- Brief Analysis of RT-Thread System Equipment Framework
- Xintang: Secure In-System-Programming protects firmware burning security
- My EEWorld 2018 year-end summary
- Some problems encountered when using the serial port to download programs for stm32f407