AVR microcontroller serial communication program

Publisher:MysticSerenadeLatest update time:2015-07-13 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  The serial port of the microcontroller is often a useful communication interface. For example, if you want to modify some variables inside the microcontroller, you can directly use the serial port communication to send the parameters to the microcontroller, saving the trouble of recompiling and downloading.

    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;
}

Reference address:AVR microcontroller serial communication program

Previous article:AVR microcontroller-digital tube 1
Next article:Ultrasonic ranging module + AVR microcontroller control program

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号