uartinterface.c

来自「The AVRcam source files were built using」· C语言 代码 · 共 75 行

C
75
字号
/***********************************************************
    Module Name: UartInterface.c
    Module Date: 04/10/2004
    Module Auth: John Orlando 
    Copyright (c) 2004 John Orlando  All Rights Reserved 

    Description: This module is responsible for providing an
    interface to the UART hardware available on the mega8.
    This interface is an interrupt-driven interface.
    ***********************************************************/

/*	Includes */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include "CommonDefs.h"
#include "UartInterface.h"
#include "UIMgr.h"
#include "Executive.h"

/*  Local Variables */

/*  Local Structures and Typedefs */

/*  Extern Variables */

/*  Definitions */

/***********************************************************
    Function Name: UartInt_init
    Function Description: This function is responsible for
    initializing the UART interface on the mega8.  This
    interface is set to communicate at 115.2 Kbps, with an
    8N1 protocol.
    Inputs:  none
    Outputs: none
    ***********************************************************/
void UartInt_init(void)
{
    /* set up the baud rate registers so the UART will operate
        at 115.2 Kbps */
    UBRRH = 0x00;
    UBRRL = 0x08;

    /* enable the tx and rx capabilities of the UART...as well
        as the receive complete interrupt */
    UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);

    /* set up the control registers so the UART works at 8N1 */
    UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);

}

/***********************************************************
    Function Name: UartInt_txByte
    Function Description: This function is responsible for
    transmitting a single byte on the uart.
    Inputs:  txByte - the byte to send
    Outputs: none
    NOTES: When the TX UDRE (data register empty) is set, there
    is puposefully no interrupt...thus, to send a string of
    data out, the calling routine needs to hold up the entire
    application while this takes place (or just send one
    byte at a time at strtegically timed intervals, like
    the stats data is sent out :-)
    ***********************************************************/
void UartInt_txByte(unsigned char txByte)
{
    /* Wait for empty transmit buffer */
    while ( !( UCSRA & (1<<UDRE)) );
    /* Put data into buffer, sends the data */
    UDR = txByte;
}

⌨️ 快捷键说明

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