Use of AVR MCU ATMEGA8 USART

Publisher:变形金刚Latest update time:2018-07-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In a recent project, the blogger encountered a function that required the use of USART serial port for communication. First of all, there is one very different thing between USART and UART:

UART: universal asynchronous receiver and transmitter

           [Bus Signal]  TX , RX

 

USART: universal synchronous asynchronous receiver and transmitter

           [Bus Signal]  TX, RX, CK    

USART supports synchronous mode, so USART needs to synchronize the signal USART_CK (such as STM32 microcontroller). Usually, the synchronous signal is rarely used, so the general microcontroller UART and USART are used in the same way, both using asynchronous mode.

In the blogger's current project, communication is mainly carried out through the RS485 protocol. RS485 communication is a typical communication protocol for half-duplex asynchronous communication that is very common in industry. Its characteristics are that the communicating devices are mounted on the main line, and differential signals are used to determine the reception and transmission. At the same time, only one function (receiving or sending information) can be performed. Then the blogger's software processing method is as follows;

First, we need to imagine the basic framework of the entire project through 485 communication: define the signal port (because 485 does not have TXRX instead of UART); then initialize USART--set the baud rate, the number of data to be transmitted, enable the IO port, and set whether to set the parity bit, etc. Is there an automatic send and receive setting for signal transmission? If so, do we need to write an interrupt for complete reception and complete transmission? And so on. Different projects may have different requirements. All of these need to be thought out in advance.

(1) Due to half-duplex communication, one thing must be noted: there is always only one signal line. The difference is that the processing results of this signal line may be different at different times. It may be sending data or receiving data.

So naturally, we will think of using an IO port to define the signal port. So the blogger first defines an IO port as the signal processing port, and it is best to write the delay into it:

#define Tx PORTC.1=1;delay_ms(10) //Define C1 as the signal receiving and transmitting line     
#define Rx PORTC.1=0;delay_ms(10)

(2) Set each USART register and initialize it. If CVAVR is used, the code can be automatically generated very conveniently. As shown below.

//USART initialization Universal Synchronous and Asynchronous Serial Receiver and Transmitter (USART)
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600 Data Register UDR USART and Status Register ABC
void uartinit(void)
{
    UCSRA=0x00; //No speed increase                      
    UCSRB=0xD8; //Receive completion interrupt enabled, transmit completion interrupt enabled, receive data enabled, transmit data enabled, 1101 1000
    UCSRC=0x86; //8-bit data bit
    UBRRH=0x00; //Configure baud rate
    UBRRL=0x33;
}

After the configuration is completed, we have a basic communication frame structure, and all communication structures can be parsed and sent through this frame structure.

(3) Configure the communication send and receive interrupts. Again, when using the CVAVR software, you can naturally add the code as follows

// USART Receiver interrupt service routine serial port receive interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void) 
{
    u16 crctemp;
    char status,data;
    status=UCSRA;                              
    data=UDR;
    t0cnt=0;
    if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
    {   
       。 。
    }
}

// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void) //Transmit pin interrupt function
{
    if(--txcnt) //When txcnt==1
    {。 。
    }
    else
    {
    。 。

    RX; //Pull the signal line up.

    } 
}

One thing to note here is that after sending, pay attention to the state of the communication signal line. If it is in the receiving state, keep it. If it is in the state where sending has been completed, pay attention to changing the state of the signal line. The method of changing is the macro definition written by the blogger above. RX.

(4) Use of UDR

It can be clearly seen in the data sheet that UDR is a data buffer register, which plays a "leader" role when sending or receiving data. Once it is filled with the first byte of the interrupt, the data will be sent frame by frame.

Remember to hang up the signal line when sending.
            Tx;
            txbuf[0]=modid; //Mode selection
            txbuf[1]=SBNUM; //Device model
            txbuf[2]=0x02;
            txbuf[3]=0x02;
            txbuf[4]=0x4F;
            txbuf[5]=0x4B;
            crctemp=getcrc16(&txbuf[0],6);
            txbuf[6]=crctemp>>8;
            txbuf[7]=crctemp;        
            txcnt=8;
            UDR=txbuf[0]; //Fill the first letter of the interrupt


Reference address:Use of AVR MCU ATMEGA8 USART

Previous article:Some thoughts on parity check code for AVR ATmega8 serial data protocol transmission
Next article:Exploration of AVR Timer

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号