uart.c

来自「AVR Devolpment Board」· C语言 代码 · 共 58 行

C
58
字号
#include "avr/io.h"
#include "uart.h"

#define BAUD  38400
#define UBRR  (F_CPU/16/BAUD-1)


void UART_Init(void)
{
	UCSRA = 0x00;
	UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
	UCSRC = (1<<7)|(1<<UCSZ1)|(1<<UCSZ0);
	UBRRH = UBRR>>8;
	UBRRL = UBRR&0xFF;
	DDRD |= (1<<PD1)|(0<<PD0);
	PORTD|= (1<<PD1)|(1<<PD0);
}


void UART_PutChar(char c)
{
	while(!(UCSRA&(1<<UDRE)));
	UDR    = c;
}


/* Bellow is for printf function. If you want use this function, you must add the
 * include file stdio.h and stdlib.h, then adjust your displayment stream fuinction
 * to use with printf() and set output stream to it. If you want use the float number 
 * opreation, in the project option of the Libraries dialog box, you should add the 
 * library file libm.a and libprintf_flt.a.
 * In the project option of the Custom Options dialog box, you should and Linker Options
 * with <-Wl,-u,vfprintf -lprintf_flt>(except the <>). */

	

//adjust UART stream fuinction to use with printf()
static int UARTsendstream(char c , FILE *stream)
{
	UART_PutChar(c);
	return 0;
}

//----set output stream to LCD-------
static FILE uart_str = FDEV_SETUP_STREAM(UARTsendstream, NULL, _FDEV_SETUP_WRITE);


void UART_Config(void)
{
	UART_Init();
	stdout = &uart_str;
}





⌨️ 快捷键说明

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