#define FCLK 22118400UL #define BAUDRATE 9600UL /* ref http://csserver.evansville.edu/~blandfor/EE354/SFRegisters.pdf */ void InitUART(void) { EA = 0; TMOD &= 0x0F; /*timer 1 clean*/ TMOD |= 0x20; /*mode 2: 0~255, auto-reload */ REN = 0; /*forbid 8051 receive data*/ SM1 = 1; /* Serial Control Register be set by timer*/ /* BaudRate = OscillatorFreq/(N*256-TH1) if SMOD = 0, N = 384 if SMOD = 1, N = 192 SMOD is PCON.7 , Serial mode bit */ TH1 = 256 - FCLK/(BAUDRATE*12*16); TL1 = 256 - FCLK/(BAUDRATE*12*16); PCON |= 0x80; ES = 1; /*Enable Serial port interrupt */ REN = 1; /*allow 8051 receive data */ EA = 1; /*Enable interrupt*/ TI = 1; /* the transmitting has been done */ RI = 0; /* the receiving is not done yet */ TR1 = 1; /*Timer 1 is turned on*/ }/*InitUART*/ void UartInterrupt(void) interrupt 4 { #if(0) if(RI) { } else { } #endif }/*UartInterrupt*/ char putchar(char c) { /* \n -> \r\n \r -> carriage return, 13 */ if (‘\n‘ == c) { SBUF = 0x0d; while(0 == TI); TI = 0; } SBUF = c; while(0 == TI); TI = 0; return c; }/*putchar*/
Putchar in Keil C, take 8051 as an instance
原文地址:http://blog.csdn.net/u013606170/article/details/45358049