ATMega8 interrupt mode UART test program

Publisher:ChanheroLatest update time:2017-11-21 Source: eefocusKeywords:ATMega8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include
#include
#include

#define uchar unsigned char
#define uint unsigned int

uchar g_bTxdPos=0; //Send positioning counter

uchar g_bTxdLen=0; //Waiting for the number of bytes to be sent
uchar g_bRxdPos=0; //Receive positioning counter
uchar g_bRxdLen=0; //Waiting for the number of bytes to be received

uchar g_aSendBuf[16]; //Send data buffer
uchar g_aRecvBuf[16]; //Receive data buffer

//Receive interrupt
SIGNAL (SIG_ UART _RECV)
{
    uchar c=UDR; 
    if(g_bRxdLen>0)
    {
        g_aRecvBuf[g_bRxdPos++]=c;
        g_bRxdLen--;
    }
}

//Send interrupt
SIGNAL (SIG_UART_TRANS)
{
    if(--g_bTxdLen>0)
        UDR=g_aSendBuf[++g_bTxdPos];
}

//Is reception completed
uchar IsRecvComplete(void)
{
    return g_bRxdLen==0;
}

//Send data of specified length from send buffer
void SendToUart(uchar size)
{
    g_bTxdPos=0;
    g_bTxdLen=size;
    UDR=g_aSendBuf[0];
    while(g_bTxdLen>0);
}

//Receive data of specified length to receive buffer
void RecvFromUart(uchar size,uchar bwait)
{
    g_bRxdPos=0;
    g_bRxdLen=size;
    if(bwait)
        while(g_bRxdLen>0);
}

int main( void )
{
    uchar i;

  //uart initialization
    //receive enable, transmit enable, receive interrupt enable, transmit interrupt enable
    UCSRB=(1<    UBRRL=25; // baud=9600 UBRR=CK/(baud*16) -1   

    sei();//Total interrupt enabledwhile
    
    (1)
    {
        //Asynchronously receive 16 bytes of dataRecvFromUart
        (16,0);

        //Wait for reception to completewhile
        (!IsRecvComplete());
        
        //Copy the received data to the send bufferfor
        (i=0;i<16;i++)
              g_aSendBuf=g_aRecvBuf;
        
        //Send back the received dataSendToUart
        (16);
    }
}


Keywords:ATMega8 Reference address:ATMega8 interrupt mode UART test program

Previous article:AVR-GCC about reti()
Next article:AVR simple and universal serial port configuration method

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

Design and implementation of high-power DC motor control system based on ATmega8
  1. Introduction   As the main device for electromechanical energy conversion, DC motors are widely used in all walks of life. With the rapid development of computer electronic technology, the control method of motors has also undergone tremendous changes. The analog control method has been basically replaced by th
[Microcontroller]
Design and implementation of high-power DC motor control system based on ATmega8
S3C2440 UART driver code template (RealView MDK)
A good memory is worse than a bad pen. To facilitate code review and code reuse in the future, the S3C2440 UART driver code is posted here. The friendly MINI2440 development board is used, and the development environment is RealView MDK 4.22. It should be noted that in this code, the initialization of GPIO is complete
[Microcontroller]
atmega8 Example: Delay of system library function
/***********************************************************  * Library Description: ATMEGE8 Delay Library Function  * Version: v1.0                                                                                           *                                                                                            ***
[Microcontroller]
Problem of opening HAL_UART_Receive_IT() multiple times using HAL library in STM32
Hardware platform: STM32F429  Software platform: Keil 5 Using HAL library  I borrowed the routine of Zhengdian Atom, and I would like to express my gratitude here  . HAL_UART_Receive_IT(&UART1_Handler, (u8 *)aRxBuffer, 1);1 This function is first called once when initializing uart_init(115200).  Then, in the serial p
[Microcontroller]
Problem of opening HAL_UART_Receive_IT() multiple times using HAL library in STM32
ATMEGA8 Ultrasonic Distance Measurement Program
After the ultrasound was developed a few days ago, the accuracy can be improved to 1cm and then placed on the cart I made an ultrasonic obstacle shield. Now the car can track with photoelectric tracking and ultrasonic obstacle shield. Ultrasound Procedure   /********************************************************
[Microcontroller]
ATMEGA8 single chip LED dynamic display program design
// There are 4 seven-segment LEDs in the circuit . With this program, the refresh period of each LED is 200/4=50 Hz .       //Change the value of DispResult in real time in the program, and the display will change accordingly.       #include iom8v.h       #include macros.h       unsigned char        table ={0xc0
[Microcontroller]
PIC microcontroller simulates asynchronous serial communication UART source program
Introduction: Use TMR0 to implement timing query. It can be implemented on any PIC with interrupt. This method can be used to expand multiple serial ports. ;|--------------------------------------------------------------| ;| Implement duplex USART base on normal I/O pin | ;| Using TIMER0 interrupt for bit timing | ;|
[Microcontroller]
Heating Control System Based on ATmgea8 Single Chip Microcomputer
l Introduction Temperature is one of the main controlled parameters in industrial production, and various temperature control systems related to it are widely used in metallurgy, chemical industry, machinery, food and other fields. The temperature measurement and heating control system introduced in this arti
[Microcontroller]
Heating Control System Based on ATmgea8 Single Chip Microcomputer
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号