AVR serial port transmission and reception using circular linked list program source code

Publisher:cocolangLatest update time:2016-10-08 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Download the entire project:
http://www.rayfile.com/files/062cd042-d7a5-11df-8a51-0015c55db73d/
 
Key code:
UART.h:
/********** Serial port transmit and receive circular linked list implementation********
* Version..........: 1.0
* Author..........: Chen Lidong
* Target.......: ATmega128
* File name..........: UART.h
* Compiler.....: IAR for AVR V5.5
* Creation time.....: 2010.10.14
* Last modified.....: 2010.10.14
******************************************/
#ifndef __UART_H__
#define __UART_H__

#include 
#include "../main.h"    /* F_CPU */

#define UART_BAUD                9600         //波特率


#define UART_TXBUF_LENGTH        500
#define UART_RXBUF_LENGTH        500

struct UART_BUF
{
    volatile unsigned char data;
    volatile struct UART_BUF *next;
};

extern void UART_Init(void);
extern void UART_TxByte(unsigned char _data);
extern void _enable_uart_interrupt(void);
extern void _disable_uart_interrupt(void);

extern volatile struct UART_BUF UART_TxBuf[UART_TXBUF_LENGTH], UART_RxBuf[UART_RXBUF_LENGTH];
extern volatile struct UART_BUF *p_TxBuf_Head, *p_TxBuf_Tail, *p_RxBuf_Head, *p_RxBuf_Tail;


#endif /* __UART_H__ */
 
UART.c:
/********** Serial port transmit and receive circular linked list implementation********
* Version..........: 1.0
* Author..........: Chen Lidong
* Target.......: ATmega128
* File name..........: UART.c
* Compiler.....: IAR for AVR V5.5
* Creation time.....: 2010.10.14
* Last modified.....: 2010.10.14
******************************************/
#include "UART.h"

volatile struct UART_BUF UART_TxBuf[UART_TXBUF_LENGTH], UART_RxBuf[UART_RXBUF_LENGTH];
volatile struct UART_BUF *p_TxBuf_Head, *p_TxBuf_Tail, *p_RxBuf_Head, *p_RxBuf_Tail;

void UART_Init(void)
{
    UBRR0L = (F_CPU / 16L / UART_BAUD - 1);
    UCSR0B_RXEN0 = 1;
    UCSR0B_TXEN0 = 1;
    UCSR0B_RXCIE0 = 1;
    UCSR0B_TXCIE0 = 1;

    unsigned int i = 0;
    // 
    for (i = 0; i < UART_TXBUF_LENGTH - 1; i++)
    {
        UART_T xBuf[i].next = &UART_TxBuf[i + 1];
    }
    UART_TxBuf[UART_TXBUF_LENGTH - 1].next = &UART_TxBuf[0];
    p_TxBuf_Head = &UART_TxBuf[0];
    p_TxBuf_Tail = &UART_TxBuf[0];
    //
    for (i = 0; i < UART_RXBUF_LENGTH - 1; i++)
    {
        UART_RxBuf[i].next = &UART_RxBuf[i + 1];
    }
    UART_RxBuf[UART_RXBUF_LENGTH - 1].next = &UART_RxBuf[0];
    p_RxBuf_Head = &UART_RxBuf[0];
    p_RxBuf_Tail = &UART_ RxBuf[0];

    __enable_interrupt();
}

void UART_TxByte(unsigned char _data)
{
    if (UCSR0A_UDRE0 == 1 && p_TxBuf_Head == p_TxBuf_Tail)
    {
        UDR0 = _data;
    }
    else
    {
        if (p_TxBuf_Tail->next != p_TxBuf_Head)
        {
            p_TxBuf _Tail->data = _data;
            p_TxBuf_Tail = p_TxBuf_Tail->next;
        }
    }
}

void _enable_uart_interrupt(void)
{
    UCSR0B_RXCIE0 = 1;
    UCSR0B_TXCIE0 = 1;
}

void _disable_uart_interrupt(void)
{
    UCSR0B_RXCIE0 = 0;
    UCSR0B_TXCIE0 = 0;
}

#pragma vector = USART0_TXC_vect
__interrupt void USART0_TXC(void)
{
    if (UCSR0A_UDRE0 == 1 && p_TxBuf_Head != p_TxBuf_Tail)
    {
        UDR0 = p_TxBuf_Head->data;
        p_TxBuf_Head = p_TxBuf_Head->next;
    }
}

#pragma vector = USART0_RXC_vect
__interrupt void USART0_RXC(void)
{
    if (p_RxBuf_Tail->next != p_RxBuf_Head)
    {
        p_RxBuf_Tail->data = UDR0;
        p_RxBuf_Tail = p_RxBuf_Tail->next;
    }
}
 
 
Effect screenshots:
Sent a file
AVR serial port transmission and reception using circular linked list to achieve the program source code - Ear - Ear's Blog
Keywords:AVR Reference address:AVR serial port transmission and reception using circular linked list program source code

Previous article:AVR serial port transmission and reception using ring buffer program source code
Next article:AVR matrix keyboard program source code V3.5 (with continuous key function and combination key function)

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

How to make program debugging easier with AVR microprocessors
  introduction   With the development of technology, the design and application of embedded systems have had a great impact on people's lives and will gradually change people's future lifestyles. Developing applications on a specific operating system allows developers to ignore many underlying hardware details, maki
[Microcontroller]
AVR 8515 MCU remote control decoding program
This program implements K1D and China Telecom set-top box remote control decoding. I just switched from 51 MCU to AVR and learned C programming. I spent a day to correct some sentence formats and finally finished it. The effect is very good. Share it with everyone. I only post the decoded part of the file here, forget
[Microcontroller]
AVR Serial Communication
I used M8 to make a 485 communication. During debugging, I found that the data received by the PC was abnormal, while the data received by another identical board was normal. I measured the pin waveform of the PC serial port receiving data, it is normal, but the received data is wrong, strange! At this time, the ope
[Microcontroller]
Basic knowledge of C language for AVR microcontrollers
1. Basic syntax introduction  A simple AVR program  #include iom16v.h    #include macros.h   void main()  {   PORTA = 0x0F; //Assign values ​​to the PA port, making the lower four bits of the PA port 1 and the upper four bits 0   while(1)    ;  }The purpose of this program is to set the value of the PA port to 0x0
[Microcontroller]
Design of AVR microcontroller for solar cell controller
Introduction: The solar controller designed here has stable performance, overcharge and overdischarge protection and temperature compensation. After testing, the system shows good control effect, which not only improves the working efficiency of solar cells, but also protects the batteries used. In terms of the use of
[Microcontroller]
Design of AVR microcontroller for solar cell controller
AVR microcontroller water light and button anti-interference
First time to do it. All are emulated by avr microcontroller ATMEGA16. There are 3 buttons to control the running light program and 1 microcontroller button for anti-interference. The left and right buttons must be released. Microcontroller source code: #include iom16v.h void delay(unsigned int x) {   int i,j;   fo
[Microcontroller]
AVR microcontroller water light and button anti-interference
Design of intelligent treadmill controller based on AVR microcontroller
1 Introduction The electric treadmill is the mainstream product among fitness equipment at present. It uses a motor to drive the running belt to make people run or walk passively at different speeds. In terms of human strength, it saves a stretching action compared to running and walking on the ground, which ca
[Microcontroller]
Design of intelligent treadmill controller based on AVR microcontroller
Using AVR microcontroller to control GSM module to send and receive text messages
GSM modules are useful when our projects require remote access. With these modules we can do everything that our normal mobile phones can do, such as making/receiving calls, sending/receiving SMS, connecting to the internet using GPRS, etc. You can also connect a normal microphone and speaker to this module to talk to
[Microcontroller]
Using AVR microcontroller to control GSM module to send and receive text messages
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号