Programming tips: AVR UART data transmission and reception test program

Publisher:心想的45号Latest update time:2016-10-21 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This program implements the UART interrupt mode to send and receive data, and sends the received data immediately to form a loop.

It consists of the test program , the related header file , and the UART data processing program .



/* ************************************************
* File name: main.c
* Function: AVR's UART data receiving and sending test program
* Description: Forward the data received by the RXD port to the TXD port
* The receiving and sending work is automatically completed by the UART interrupt
* Author & Date: Joshua Chan, 2012/03/28
* ************************************************/
#include
#include
#include
#include
#include
#include "uart_test.h"

/* Test program: Send the data received by the serial port as is*/
void main(void)
{
unsigned char data;

fifo_pos_init(); /* Reset position record*/
uart_init(B9600); /* Initialize UART */
_SEI();
while (1) {
if ((data = rx_byte()) != NONE_DATA)
tx_byte(data);
_WDR(); /* Feed the dog*/
}
}




/* ************************************************
* File name: uart_test.h
* Function: AVR's UART data receiving and sending test program
* Description: Forward the data received by the RXD port to the TXD port
* The receiving and sending work is automatically completed by the UART interrupt
* Author & Date: Joshua Chan, 2012/03/28
* ************************************************/
#ifndef _UART_TEST_H
#define _UART_TEST_H

/* Define the UBRR value when FOSC=16.0MHz*/
__flash enum baudrate {
B2400 = 416,
B4800 = 207,
B9600 = 103,
B14400 = 68,
B19200 = 51,
B28800 = 34,
B38400 = 25,
B57600 = 16,
B76800 = 12,
B115200 = 8,
B230400 = 3,
};

#define NONE_DATA '\4' /* No new data flag*/
#define RX_BUF_SIZE 32 /* Receive buffer size*/
#define TX_BUF_SIZE 32 /* Transmit buffer size*/

/* Reset position record*/
extern void fifo_pos_init(void);

/* UART initialization*/
extern void uart_init(unsigned int baud);

/* Read 1 byte from the receive buffer*/
extern unsigned char rx_byte(void);

/* Send 1 byte of data to the transmit buffer and enable the data register empty interrupt*/
extern void tx_byte(unsigned char data);

#endif



/* ************************************************
 * File name: uart_test.c
 * Function: AVR UART data receiving and sending test program
 * Description: Forward the data received by the RXD port to the TXD port
 * Transmission and reception are automatically completed by UART interrupt
 * Author & Date: Joshua Chan, 2012/03/28
 * ************************************************/
#include 
#include 
#include 
#include 
#include 
#include "uart_test.h"

unsigned char rx_fifo[RX_BUF_SIZE]; /* Receive buffer */
unsigned char tx_fifo[TX_BUF_SIZE]; /* Send buffer */
unsigned char rx_pos; /* The position of the last received data in the buffer*/
unsigned char read_pos; /* The position of the last data read in the buffer*/
unsigned char tx_pos; /* The position of the last transmitted data in the buffer*/
unsigned char write_pos; /* The position of the last written data in the buffer*/

/* UART initialization */
void uart_init(unsigned int baud)
{
    /* Disable USART function first */
    UCSR0B = 0x00;
    /* Set baudrate */
    UBRR0H = (unsigned char)(baud>>8);
    UBRR0L = (unsigned char)baud;
    /* Set frame format: 8 data bits, 1 stop bit*/
    UCSR0C = 3 << UCSZ00;
    /* Enable: receive end interrupt, receive, send*/
    UCSR0B = (1<
 
Keywords:AVR Reference address:Programming tips: AVR UART data transmission and reception test program

Previous article:Programming Tips: 16-bit AVR timer compare match interrupt test program
Next article:Programming Tips: 8-bit AVR timer compare match interrupt test program

Recommended ReadingLatest update time:2024-11-16 13:40

Why choose AVR microcontroller to learn microcontroller
  The rapid development of electronic technology, especially the more extensive application of single-chip microcomputers in military, industry, household appliances, smart toys, portable smart instruments and other fields, has greatly improved the function, precision and quality of products, with simple circuits, low
[Microcontroller]
Mini2440 bare metal trial - DMA direct access to achieve Uart (serial port) communication
This can only be used as an opening chapter for your initial understanding of MDA Functionality:         Pass the string data to UTXH0 through DMA0 channel, and then         After the data is transferred, DMA0 generates an interrupt, a beep sound is heard, and the LED lights up.   DMA Basics The commonly u
[Microcontroller]
Mini2440 bare metal trial - DMA direct access to achieve Uart (serial port) communication
Key points for designing PWM functions using AVR timer/counter
1. Key points of timer/counter PWM design  According to the characteristics of PWM, the following points should be noted when using the ATmega128 timer/counter to design output PWM:  1. First, the PWM frequency range to be output should be determined according to the actual situation. This frequency is related to the
[Microcontroller]
Design of electronic anti-lost and anti-theft device based on AVR and RFID
Introduction: Aiming at the development demand of anti-lost products, this paper introduces the software and hardware design and test analysis of an electronic anti-lost and anti-theft device with dual microcomputers and color LCD touch screen. The system is different from existing products. The host has an LCD touch
[Microcontroller]
Design of electronic anti-lost and anti-theft device based on AVR and RFID
Detailed information on the internal structure of the AVR microcontroller's CPU
  After studying microcontrollers for so long, I feel that if I want to go deeper, I have to learn assembly language, or at least understand the internal structure of the microcontroller. Below, I will take ATmega16 as an example to introduce the AVR microcontroller structure and assembly language.   As shown in the
[Microcontroller]
Detailed information on the internal structure of the AVR microcontroller's CPU
Application of TWI bus modular design in intelligent robots
IntroductionThe     modular robot system consists of a main control module and an expansion module. It is mainly used to detect the robot's surrounding environment information and the robot's own motion state, obtain various sensor information in real time, and control the robot's motion. Since there is a lot of data
[Microcontroller]
Application of TWI bus modular design in intelligent robots
3. ARM9 (S3C2440) serial port UART - program example explanation
Serial communication program writing steps UART communication program can use query, interrupt and DMA mode. We introduce the writing of UART communication program by using more interrupt methods. The simple way is to refer to the example program for writing UART communication program. Select the channel through the
[Microcontroller]
Application of AVR single chip microcomputer in digital signal processing of frequency hopping system
The ATmega103 microcontroller is a reduced instruction set (RISC) AVR (ADVANCE RISC) series microcontroller product launched by ATMEL. This is an enhanced RISC structure and an 8-bit microcontroller using CMOS technology. This structure can effectively support high-level languages ​​and extremely dense assembler cod
[Microcontroller]
Application of AVR single chip microcomputer in digital signal processing of frequency hopping system
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号