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

📄 uart.c

📁 avr dual uart communication api
💻 C
字号:
/*
	uart0 : debug
	uart1 : dsp board communication
	      : Need Protocol
*/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

#include "uart.h"

#ifdef ENABLE_SERIAL


// UART0 global variables
volatile bool UART_CharReceived = false;
volatile bool UART_RxBufOverflow = false;
volatile u08  UART_RxChar = 0;

// UART1 global variables
volatile bool UART1_CharReceived = false;
volatile bool UART1_RxBufOverflow = false;
volatile u08  UART1_RxChar = 0;

#ifdef UART_TX_ASYNC
volatile u08 *pUART_TxBuffer = NULL;	// if != NULL transmit is going
#endif




/*
#define UDR UDR0
#define UCR UCSR0B
#define USR UCSR0A
#define RXCIE RXCIE0
#define RXEN RXEN0
#define TXEN TXEN0
#define UBRR UBRR0
#define UDRE UDRE0

#define UDR1 UDR1
#define UCR1 UCSR1B
#define USR1 UCSR1A
#define RXCIE RXCIE1
#define RXEN RXEN1
#define TXEN TXEN1
#define UBRR UBRR1
#define UDRE UDRE1
*/


// ---- UART core functions --------------------------------------------

// Initialisation
void UART_Init(void)
{
	UART_CharReceived	= false;
	UART_RxBufOverflow	= false;
#ifdef UART_TX_ASYNC
	pUART_TxBuffer		= NULL;
#endif
	// enable RxD/TxD and interrupts
/*
#ifdef UART_TX_ASYNC
#define UART_FLAGS (BV(RXCIE0) | BV(TXCIE0) | BV(RXEN0) | BV(TXEN0))
#else
#define UART_FLAGS (BV(RXCIE0) | BV(RXEN0) | BV(TXEN0))
#endif
*/	
	//outp(UART_FLAGS, UCSR0B);
	//outp((u08)UART_BAUD_SELECT, UBRR0);		// set baud rate
	UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
  UCSR0C = USART_FRM_8BIT_CH|USART_FRM_1STOP_CH|USART_FRM_NOPARITY_CH;
	
	UBRR0L = USART_BAUD_L;
  UBRR0H = USART_BAUD_H;
	sei();						// enable interrupts
}



// Receiving part
// UART Receive Complete Interrupt Function

SIGNAL(SIG_UART0_RECV)      
{
    if (UART_CharReceived)			// buffer overflow
		UART_RxBufOverflow = true;
		
    UART_RxChar = inp(UDR0);			// Store received character
    UART_CharReceived = true;			// Now, and only now we can indicate
							//  that the UART has received a character
}
/*
// move to main
SIGNAL(SIG_UART1_RECV)      
{
    if (UART_CharReceived)			// buffer overflow
		UART_RxBufOverflow = true;
    
    UART_RxChar = inp(UDR1);			// Store received character
    UART_CharReceived = true;			// Now, and only now we can indicate
							//  that the UART has received a character
}
*/
// Retrieve received character or wait until receive
u08 UART_ReceiveByte(void)
{
	register u08 temp;
    	while(!UART_CharReceived);	// wait for UART indicates that a character
						//  has been received. Watchdog protected
    	temp = UART_RxChar;		// first we have to take received character
	UART_CharReceived = false;	// and only then report that buffer is free
    	return temp;			// return received character
}
/*
u08 UART1_ReceiveByte(void)
{
	register u08 temp1;
    	
    	while(!UART1_CharReceived);	// wait for UART indicates that a character
								//  has been received. Watchdog protected
	temp1 = UART1_RxChar;			// first we have to take received character
	UART1_CharReceived = false;	// and only then report that buffer is free
    	
    	return temp1;				// return received character
}
*/

// Checks and clear Rx buffer overflow bug
bool UART_CheckRxBuf(void)
{
	register bool temp;
	temp = UART_RxBufOverflow;
	UART_RxBufOverflow = false;
	return temp;
}



// Transmit part

#ifdef UART_TX_ASYNC

// UART Transmit Complete Interrupt Function
SIGNAL(SIG_UART0_TRANS)
{
	if (pUART_TxBuffer != 0)			// Test if a string is being sent
	{
		if (*pUART_TxBuffer != 0)		// Test the end of string
			outp(*pUART_TxBuffer++, UDR0); // Send next character in string
		else
			pUART_TxBuffer = 0;
	}
}
#endif	// UART_TX_ASYNC

#ifdef UART1_TX_ASYNC

// UART Transmit Complete Interrupt Function
SIGNAL(SIG_UART1_TRANS)
{
	if (pUART1_TxBuffer != 0)			// Test if a string is being sent
	{
		if (*pUART1_TxBuffer != 0)		// Test the end of string
			outp(*pUART1_TxBuffer++, UDR1); // Send next character in string
		else
			pUART1_TxBuffer = 0;
	}
}
#endif	// UART_TX_ASYNC


void UART_SendByte(u08 Data)
{
	WDR;

#ifdef UART_TX_ASYNC
	while (pUART_TxBuffer);				// wait for prev transmit end
#endif

	// wait for UART to become available
	while ((inp(UCSR0A) & _BV(UDRE)) != _BV(UDRE));
	// send character
    outp(Data, UDR0);
}

void UART1_SendByte(u08 Data)
{
	WDR;

#ifdef UART1_TX_ASYNC
	while (pUART1_TxBuffer);				// wait for prev transmit end
#endif

	// wait for UART to become available
	while ((inp(UCSR1A) & _BV(UDRE1)) != _BV(UDRE1));
	// send character
    outp(Data, UDR1);
}

void UART_PrintfEndOfLine(void)
{
	UART_SendByte('\r');
	UART_SendByte('\n');
}

void UART1_PrintfEndOfLine(void)
{
	UART1_SendByte('\r');
	UART1_SendByte('\n');
}


//------------------------------------------------------------------------------------
//		Following functions is needed only for debugging

#if defined(DEBUG_RF)

static void UART_PrintfU4(u08 Data)
{
//	 Send 4-bit hex value 
	u08 Character = Data & 0x0f;
	Character += '0';
	if (Character > '9')
		Character += 'A' - '0' - 10;
	UART_SendByte(Character);
}




void UART_Printfu08(u08 Data)
{
//     Send 8-bit hex value 
    UART_PrintfU4(Data >> 4);
    UART_PrintfU4(Data);
}


void UART_Printfu16(u16 Data)
{
//     Send 16-bit hex value 
    UART_Printfu08(Data >> 8);
    UART_Printfu08(Data);
}

//---------------------------------------------------//
static void UART1_PrintfU4(u08 Data)
{
//	 Send 4-bit hex value 
	u08 Character = Data & 0x0f;
	Character += '0';
	if (Character > '9')
		Character += 'A' - '0' - 10;
	UART1_SendByte(Character);
}

void UART1_Printfu08(u08 Data)
{
//     Send 8-bit hex value 
    UART1_PrintfU4(Data >> 4);
    UART1_PrintfU4(Data);
}

void UART1_Printfu16(u16 Data)
{
//     Send 16-bit hex value 
    UART1_Printfu08(Data >> 8);
    UART1_Printfu08(Data);
}
//------------------------------------------------------//

void UART_Printfu32(u32 Data)
{
//    Send 32-bit hex value 
    UART_Printfu16(Data >> 16);
    UART_Printfu16(Data);
}

#endif // defined...

//------------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------//

void UART_Puts(u08* pBuf)
{
	register u08 c;
	while ((c = *pBuf++))
	{
		if (c == '\n')
			UART_SendByte('\r');		// for stupid terminal program
		UART_SendByte(c);
	}
}

void UART_Puts_p(u08* p)
{
	register u08 c;
	while ((c = pgm_read_byte(p++)))
	{
		if (c == '\n')
			UART_SendByte('\r');		// for stupid terminal program
		UART_SendByte(c);
	}
}


void UART_Putsln(u08* pBuf)
{
	UART_Puts(pBuf);
	UART_PrintfEndOfLine();
}

// UART1 SETTING
void UART1_Puts(u08* pBuf1)
{
	register u08 c;
	while ((c = *pBuf1++))
	{
		if (c == '\n')
			UART1_SendByte('\r');		// for stupid terminal program
		UART1_SendByte(c);
	}
}

void UART1_Puts_p(u08* p)
{
	register u08 c;
	while ((c = pgm_read_byte(p++)))
	{
		if (c == '\n')
			UART1_SendByte('\r');		// for stupid terminal program
		UART1_SendByte(c);
	}
}


void UART1_Putsln(u08* pBuf1)
{
	UART1_Puts(pBuf1);
	UART1_PrintfEndOfLine();
}

#endif // ENABLE_SERIAL

⌨️ 快捷键说明

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