📄 app.c
字号:
#include "avr/io.h"
#include "stdio.h"
#define BAUD 38400
#define UBRR (F_CPU/16/BAUD-1)
void UART_Init(void)
{
UCSRA = 0x00;
UCSRB = (1<<RXEN)|(1<<TXEN);
UCSRC = (1<<URSEL)|(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;
}
unsigned char UART_GetChar(void)
{
while(!(UCSRA&(1<<RXC)));
return UDR;
}
/* 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;
}
static int UARTreceivestream(FILE * stream)
{
return UART_GetChar();
}
//----set output and input stream to uart-------
static FILE uart_str = FDEV_SETUP_STREAM(UARTsendstream, UARTreceivestream, _FDEV_SETUP_RW);
void UART_Config(void)
{
UART_Init();
stdout = &uart_str;
stdin = &uart_str;
}
int main(void)
{
int a=0,b=0;
UART_Config();
printf("AVR UART transmit and receive without isr test:\r\n");
while(1)
{
printf("Please input a:a=");
scanf("%d",&a);
printf("%d\r\nPlease input b:b=",a);
scanf("%d",&b);
printf("%d\r\na+b=%d\r\n",b,a+b);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -