Atmega128 serial port detailed explanation

Publisher:Aq123456258Latest update time:2016-01-11 Source: eefocusKeywords:Atmega128 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Atmega128 has two serial ports: USART0 and USART1
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
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)
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;

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;
}
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;
}
Serial port 0 sends a string:
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;
}
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.

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 */
/***************************************************************************/
/*********************************Include header files********************************/
#include
#include
/***********************************Macro definition**********************************/
#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 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;
  uart0_init(); //UART0 initialization
  puts0("HELLO!");
 while(1)
    {    
        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 */
/***************************************************************************/
/*********************************Include header files********************************/
#include
#include
/***********************************Macro definition**********************************/
#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 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;
  uart1_init();
  puts1("HELLO!");
 while(1)
    {    
        puts1("test ok!");        
 }

Keywords:Atmega128 Reference address:Atmega128 serial port detailed explanation

Previous article:AVR microcontroller learning experience
Next article:AVR external crystal oscillator whether the test program is oscillating

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号