Putchar in Keil C, take 8051 as an instance

來源:互聯網
上載者:User

標籤:8051   uart   單片機   


 In Keil C, it is necessary to implement char putchar(char c), or the powerful function printf would not work.

We should notice in here : new line command for serial output be  "\r\n" (line feed:LF, 0x0a + carriage return:CR, 0x0d), not only "\n" in Keil C.


I do not want to write too much tedious word in here, below is my putchar code for 8051, use timer 1 as interrupt:


 



#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*/


If you want to use the code, you should modify the FCLK macro constant for your micro chip, I use stc89c52rc, crystal be 22.1184MHz.

The baud rate, for most use, be 9600, you could modify the parameter in your serial communication software. (I suggest sscom)

Putchar in Keil C, take 8051 as an instance

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.