/* * AVR串口实现printf函数 编译器 avrstudio,avr-gcc 使用硬件:avrmega16单片机 */ #include <stdarg.h> #include <stdlib.h> #include <avr/io.h> typedef unsigned char uint8; static void usart_init(void) { UCSRA = 0x02; /*倍速*/ UCSRB = 0x18; /*允许接收和发送*/ UCSRC = 0x06; /*8位数据*/ UBRRH = 0x00; UBRRL = 103; } static void put_char(uint8 data) { if (data == '/r') put_char(0x09); while ( !(UCSRA & (1<<UDRE)) ) // 不为空,等之 ; UDR = data; } /* */ static void myprintf(const char* fmt,...) { const char* s; int d; char buf[16]; va_list ap; va_start(ap,fmt); // 将ap指向fmt(即可变参数的第一个?下一个?) while (*fmt) { if (*fmt != '%') { put_char(*fmt++); // 正常发送 continue; } switch (*++fmt) // next % { case 's': s = va_arg(ap,const char*); // 将ap指向者转成char*型,并