⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 ATMEGA128的BOOTLOADER
💻 C
字号:
/*****************************************************
Project : UART
Version : 1.0
Date    : 2005-7-13
Author  : Liu.Qian
Company : HuanXiang GCT
Comments: Creat
*****************************************************/
#include "config.h"
// USART Receiver buffer
volatile uint8_t rx_buffer[RX_BUFFER_SIZE];

#if RX_BUFFER_SIZE<256
volatile uint8_t rx_wr_index,rx_rd_index,rx_counter;
#else
volatile uint16_t rx_wr_index,rx_rd_index,rx_counter;
#endif

// This flag is set on USART Receiver buffer overflow
volatile uint8_t rx_buffer_overflow;

// USART Transmitter buffer
volatile uint8_t tx_buffer[TX_BUFFER_SIZE];

#if TX_BUFFER_SIZE<256
volatile uint8_t tx_wr_index,tx_rd_index,tx_counter;
#else
volatile uint16_t tx_wr_index,tx_rd_index,tx_counter;
#endif

// This flag is set on tx over
volatile uint8_t tx_over;


/*****************************************************************************************
Function:    SIG_USARTx_RECV
Description: 接收中断
Calls:       无
Called By:   ...
Globel RD:   rx_buffer,rx_counter,rx_wr_index
Globel WR:   rx_counter,rx_wr_index,tx_over
Input:       无
Output:      无
Return:      uint8_t 字符
Others:      无
Auther:      Liu.Qian
Time:        2005-7-8
*****************************************************************************************/
SIGNAL(SIG_USARTx_RECV)
{
    uint8_t status,data;
    status=UCSRxA;
    data=UDRx;
    if ((status & (_BV(FE) | _BV(DOR) | _BV(UPE)))==0)
    {
        rx_buffer[rx_wr_index]=data;
        if (++rx_wr_index == RX_BUFFER_SIZE)
        {
            rx_wr_index=0;
        }
        if (++rx_counter == RX_BUFFER_SIZE)
        {
            rx_counter=0;
            rx_buffer_overflow=1;
        }
    }
}

/*****************************************************************************************
Function:    SIG_USARTx_TRANS
Description: 发送中断
Calls:       无
Called By:   ...
Globel RD:   tx_buffer,tx_counter,tx_rd_index
Globel WR:   tx_counter,tx_rd_index,tx_over
Input:       无
Output:      无
Return:      uint8_t 字符
Others:      无
Auther:      Liu.Qian
Time:        2005-7-8
*****************************************************************************************/
SIGNAL(SIG_USARTx_TRANS)
{
    if (tx_counter)
    {
        --tx_counter;
        UDRx=tx_buffer[tx_rd_index];
        if (++tx_rd_index == TX_BUFFER_SIZE)
        {
            tx_rd_index=0;
        }
    }
    else
    {
        tx_over=1;
    }
}

/*****************************************************************************************
Function:    Ugetchar
Description: 接收一个字符
Calls:       无
Called By:   ...
Globel RD:   rx_buffer,rx_counter,rx_rd_index
Globel WR:   rx_counter,rx_rd_index
Input:       无
Output:      无
Return:      uint8_t 字符
Others:      无
Auther:      Liu.Qian
Time:        2005-7-8
*****************************************************************************************/
uint8_t Ugetchar()
{
    uint8_t data;
    uint8_t sreg;
    while (rx_counter==0);
    data=rx_buffer[rx_rd_index];
    if (++rx_rd_index == RX_BUFFER_SIZE)
    {
        rx_rd_index=0;
    }
    sreg = SREG;
    --rx_counter;
    SREG = sreg;
    return data;
}

/*****************************************************************************************
Function:    Uputchar
Description: 发送一个字符
Calls:       无
Called By:   ...
Globel RD:   tx_buffer,tx_counter,tx_wr_index
Globel WR:   tx_over,tx_counter,tx_wr_index
Input:       uint8_t 字符
Output:      无
Return:      无
Others:      无
Auther:      Liu.Qian
Time:        2005-7-8
*****************************************************************************************/
void Uputchar(uint8_t c)
{
    uint8_t sreg;
    while (tx_counter == TX_BUFFER_SIZE);
    sreg = SREG;
    cli();
    tx_over=0;
    if (tx_counter || ((UCSRxA & _BV(UDRE))==0))
    {
        tx_buffer[tx_wr_index]=c;
        if (++tx_wr_index == TX_BUFFER_SIZE)
        {
            tx_wr_index=0;
        }
        ++tx_counter;
    }
    else
    {
        UDRx=c;
    }
    SREG = sreg;
}

/*****************************************************************************************
Function:    putstrf
Description: 发送flash字符串
Calls:       Uputchar
Called By:   ...
Globel RD:   无
Globel WR:   无
Input:       无
Output:      无
Return:      无
Others:      无
Auther:      Liu.Qian
Time:        2005-7-8
*****************************************************************************************/
void putstrf(const prog_uint8_t *str)
{
    for(;;)
    {
        if (*str=='\0')
        {
            return;
        }
        Uputchar(*str);
        str++;
    }
}

/*****************************************************************************************
Function:    putstr
Description: 发送RAM字符串
Calls:       Uputchar
Called By:   ...
Globel RD:   无
Globel WR:   无
Input:       无
Output:      无
Return:      无
Others:      无
Auther:      Liu.Qian
Time:        2005-7-8
*****************************************************************************************/
void putstr(uint8_t *str)
{
    for(;;)
    {
        if (*str=='\0')
        {
            return;
        }
        Uputchar(*str);
        str++;
    }
}

/*****************************************************************************************
Function:    flush
Description: 等待发送完成
Calls:       无
Called By:   ...
Globel RD:   tx_over
Globel WR:   无
Input:       无
Output:      无
Return:      无
Others:      无
Auther:      Liu.Qian
Time:        2005-7-8
*****************************************************************************************/
void flush()
{
    while(tx_over==0);
}

// end of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -