uartinterface.c

来自「mg128+Ov7620实现图象采集」· C语言 代码 · 共 97 行

C
97
字号
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include "CommonDefs.h"
#include "UartInterface.h" 
#include "UIMgr.h"
#include "Executive.h"



/***********************************************************
	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 */
	UBRR0H = 0x00;

    UBRR0L = 18;  /* 18 for double clocking at 115.2 kbps */  //0x2F
    
	
	/* enable the tx and rx capabilities of the UART...as well 
		as the receive complete interrupt */
	UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);	
	
	/* set up the control registers so the UART works at 8N1 */
	UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);//数据位:8
    
    /* set the baud rate to use the double-speed */
    UCSR0A = (0<<U2X0);		//1
	
}

/***********************************************************
	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 ( !( UCSR0A & (1<<UDRE0)) );
	/* Put data into buffer, sends the data */
	UDR0 = txByte;
}

/***********************************************************
	Function Name: SIG_UART_RECV ISR
	Function Description: This function is responsible for
	handling the interrupt caused when a data byte is 
    received by the UART.
	Inputs:  none
	Outputs: none
	NOTES: This function was originally written in assembly,
    but moved over to C when the setting of the "T" bit at
    the end of the routine was no longer necessary (this
    theoretically allowed the AVRcam to respond to serial
    bytes in the middle of tracking or dumping a frame.
    But it wasn't really needed, and understanding the C
    is easier  :-)
***********************************************************/
SIGNAL(SIG_UART0_RECV)
{
    unsigned char tmpHead;
    /* read the data byte, put it in the serial queue, and
    post the event */
 
    UIMgr_rxFifo[UIMgr_rxFifoHead] = UDR0;

    /* now move the head up */
    tmpHead = (UIMgr_rxFifoHead + 1) & (UI_MGR_RX_FIFO_MASK);//31
    UIMgr_rxFifoHead = tmpHead;
    
    /* write the serial received event to the event fifo */
    Exec_eventFifo[Exec_eventFifoHead] = EV_SERIAL_DATA_RECEIVED;

    /* now move the head up */
    tmpHead = (Exec_eventFifoHead + 1) & (EXEC_EVENT_FIFO_MASK);
    Exec_eventFifoHead = tmpHead;
}

⌨️ 快捷键说明

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