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

📄 uart.c.bak

📁 智能充电参考程序 可以控制整个充电过程希望对大家有帮助
💻 BAK
字号:
/****************************************Copyright (c)**************************************************
**                              智 能 充 电 器 开 发 小 组
**                                     OurAVR 论坛
**                                   QQ 群: 26052247 
**
**                               http://www.ouravr.com/bbs
**
**--------------文件信息--------------------------------------------------------------------------------
**文   件   名: UART.c
**创   建   人: 吕海安
**最后修改日期: 2007年01月13日
**描        述: UART的底层函数  FOR AVR MCU / Mega8
**              
**--------------历史版本信息----------------------------------------------------------------------------
** 创建人: 吕海安
** 版  本: v1.0
** 日 期: 2007年07月13日
** 描 述: 原始版本
**
**--------------当前版本修订------------------------------------------------------------------------------
** 修改人: 吕海安
** 日 期: 2008年03月02日
** 描 述: For 智能充电器  FOR AVR MCU / Mega16
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#include "config.h"
#include "UART.h"
  
#define RX_BUFFER_SIZE 8   
INT8S rx_buffer[RX_BUFFER_SIZE];   // USART Receiver buffer

#if RX_BUFFER_SIZE < 256
    INT8U rx_wr_index,rx_rd_index,rx_counter;
#else
    INT16U rx_wr_index,rx_rd_index,rx_counter;
#endif


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

/*********************************************************************************************************
** 函数名称: debug_usart_init
** 功能描述: 串口通信初始化, 38400
** 输入参数: 无
** 输出参数: 无
********************************************************************************************************/
void debug_usart_init(void)
{
    UCSRB = 0x00;
    UCSRA = 0x00;
    UCSRC = BIT(URSEL) | 0x06;
    UBRRL = 0x19;
    UBRRH = 0x00;
    UCSRB = 0x1C;
}

/*********************************************************************************************************
** 函数名称: usart_init
** 功能描述: 串口通信初始化, 9600
** 输入参数: 无
** 输出参数: 无
********************************************************************************************************/
void usart_init(void)
{
    UCSRB = 0x00;
    UCSRA = 0x00;
    UCSRC = BIT(URSEL) | 0x06;
    UBRRL = 0x19;
    UBRRH = 0x00;
    UCSRB = 0x9C;
}

/*********************************************************************************************************
** 函数名称: usart_rx_isr
** 功能描述: USART Receiver interrupt service routine 
** 输入参数: 无
** 输出参数: 无
********************************************************************************************************/
interrupt [USART_RXC] void usart_rx_isr(void)
{
    INT8S status,data;
    status = UCSRA;
    data = UDR;
    if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN)) == 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;
        };
    };
}

/*********************************************************************************************************
** 函数名称: getchar
** 功能描述: Get a character from the USART Receiver buffer
** 输入参数: 无
** 输出参数: INT8S data: 串口接收缓存
********************************************************************************************************/
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_GETCHAR_
#pragma used+
INT8S getchar(void)
{
    INT8S data;
    while (rx_counter == 0);
    data = rx_buffer[rx_rd_index];
    if (++rx_rd_index == RX_BUFFER_SIZE) 
    {
        rx_rd_index = 0;
    }
    Disable();     // 关中断
    --rx_counter;
    Enable();      // 开中断
    return data;
}
#pragma used-
#endif



#define TX_BUFFER_SIZE 8
INT8S tx_buffer[TX_BUFFER_SIZE];  // USART Transmitter buffer

#if TX_BUFFER_SIZE<256
    INT8U tx_wr_index,tx_rd_index,tx_counter;
#else
    INT16U tx_wr_index,tx_rd_index,tx_counter;
#endif

/*********************************************************************************************************
** 函数名称: usart_tx_isr
** 功能描述: USART Transmitter interrupt service routine
** 输入参数: 无
** 输出参数: 无
********************************************************************************************************/
interrupt [USART_TXC] void usart_tx_isr(void)
{      
    if (tx_counter)
    {
        --tx_counter;
        UDR = tx_buffer[tx_rd_index];
        if (++tx_rd_index == TX_BUFFER_SIZE) 
        {
            tx_rd_index = 0;
        }
    };
}  

#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_PUTCHAR_
#pragma used+  
/*********************************************************************************************************
** 函数名称: putchar
** 功能描述: Write a character to the USART Transmitter buffer ,
**           发送采用查询方式
** 输入参数: INT8S c: 要发送的字节
** 输出参数: 无
********************************************************************************************************/ 
void putchar(INT8S c)
{
    while( !(UCSRA & (1<<UDRE)) );
    UDR = c;
}  

/*********************************************************************************************************
** 函数名称: putchar
** 功能描述: Write a character to the USART Transmitter buffer ,
**           发送采用查询方式
** 输入参数: INT8S c: 要发送的字节
** 输出参数: 无
********************************************************************************************************/
/*
void putchar(INT8S c)
{
    while (tx_counter == TX_BUFFER_SIZE);
    Disable();     // 关中断 
    if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
    {
        tx_buffer[tx_wr_index] = c;
        if (++tx_wr_index == TX_BUFFER_SIZE) 
        {
            tx_wr_index = 0;
        }
        ++tx_counter;
    }
    else
    {
        UDR = c;
    }
    Enable();      // 开中断
}  
*/
#pragma used-
#endif



/**************************************************************************************×*****************
**                                 END OF FILE
********************************************************************************************************/

⌨️ 快捷键说明

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