注意:要增加以下两个头文件才能使用
- #include “stdio.h” //sprintf相关函数支持的头文件
- #include
//多参数函数支持的头文件。
#include "stdio.h" //sprintf相关函数支持的头文件。 #include//多参数函数支持的头文件。 #define UART_PRINTF_EN 1 void bsp_uart1_init(void) { P16_PushPull_Mode; //TX引脚 P02_Quasi_Mode; //RX引脚 SCON_1=0x50;//设置串口1工作在模式1(8位数据,不带奇偶校验) clr_TR3; //停止定时器3 RH3=0xFF; RL3=0x94; //9600 set_TR3; //使能定时器3 set_ES_1; //使能串口1中断 RS485_UART1_REVC; uart1_busy = 0; } void uart_char(u8 id,u8 ch) { switch (id) { case 0: // RS485_UART0_SEND;//使能485发送 // uart0_busy = 1; // SBUF=ch; // while(uart0_busy); // RS485_UART0_REVC;//使能485接收 break; case 1: // RS485_UART1_SEND; uart1_busy = 1; SBUF_1=ch; while(uart1_busy); // RS485_UART1_REVC; break; default: break; } } void uart_string(u8 id,u8 * str) { while(*str){ uart_char(id,*str++); } } #ifdef UART_PRINTF_EN void uart_printf(u8 id,const char * str,...) { char xdata buf[32]; //注意数组缓存的大小,打印字符超过数组大小会导致出现死机或者其它问题 va_list vp; va_start(vp, str); vsprintf(buf,str,vp); va_end(vp); uart_string(id,buf); } #endif void Uart1_ISR(void) interrupt 15 { if(RI_1) { RI_1=0; // uart1_receive_callback(); } if(TI_1) { uart1_busy = 0; TI_1=0; } }



