uart.c

来自「低速误码仪测试代码」· C语言 代码 · 共 58 行

C
58
字号
#include "uart.h"

uint8_t Tx_Data_Pos=0;
uint8_t Tx_Data_Len=0;
uint8_t Rx_Data_Pos=0;
uint8_t Rx_Data_Len=0;

uint8_t Send_Buf[16];
uint8_t Recv_Buf[16];

uint8_t Is_Recv_Complete(void)
{
	return Rx_Data_Len==0;
}	
uint8_t Is_Send_Complete(void)
{
	return Tx_Data_Len==0;
}
void Uart_Init(uint16_t UART_BAUD)
{
	UCSRA = _BV(U2X);
	UCSRB = 0x00;
	UBRRL = (F_CPU / (8UL * UART_BAUD)) - 1;
	UCSRC = (3<<UCSZ0)| _BV(URSEL);
	UCSRB = _BV(TXEN) | _BV(RXEN) |_BV(RXCIE) |_BV(TXCIE);
}

void Uart_Send(uint8_t len)
{
	Tx_Data_Pos=0;
	Tx_Data_Len=len;
	UDR=Send_Buf[0];
	while(Tx_Data_Len>0);
}
void Uart_Recv(uint8_t len)
{
	Rx_Data_Pos=0;
	Rx_Data_Len=len;
	while(Rx_Data_Len>0);
}

SIGNAL(USART_TXC_vect)
{
	if(--Tx_Data_Len>0)
		UDR=Send_Buf[++Tx_Data_Pos];
}
SIGNAL(USART_RXC_vect)
{
	uint8_t c=UDR;
	if(Rx_Data_Len>0)
		{
			Recv_Buf[Rx_Data_Pos++]=c;
			Rx_Data_Len--;
		}
}
	
	

⌨️ 快捷键说明

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