串口输出重定向到 printf

//#include "uart.h"
#include "stdio.h" 

// static int uart_putchar(char c, FILE *stream);

static int uart_putchar(char c, FILE *stream)  // 自定义函数,  static int  uart_putchar(char  c)  也可以。
{
 if (c == '\n')
 uart_putchar('\r', stream); //换行符 

//串口输出字符
 loop_until_bit_is_set(UCSR0A, UDRE0);  //  或 :while (!(UCSR0A & (1<  UDR0 = c;
 return 0;
}

 

static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,_FDEV_SETUP_WRITE);

 

void uart_init(void)
{

//串口初始化
 UCSR0A |= (1<< U2X0);//倍速
 UCSR0B =  (1<  UBRR0L = (unsigned char) USART_BAUD_SELECT;
 UBRR0H = (unsigned char) (USART_BAUD_SELECT>>8);

//重定向
 stdout = &mystdout;   
}

你可能感兴趣的:(嵌入式软件)