Putchar in Keil C, take 8051 as an instance
来源:互联网 发布:淘宝店全场包邮 编辑:程序博客网 时间:2024/06/17 21:57
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)
0 0
- Putchar in Keil C, take 8051 as an instance
- HowTo compile native C codes to a library for iOS development in Xcode – take Mosquitto as an exampl
- Clone Object as instance in OgreMax
- warning: integer overflow in expression (take this as a lesson)
- putchar.c
- [C/C++] An instance of using EPoll
- An AES (Rijndael) Implementation in C/C++ (as specified in FIPS-197)
- How long does it take to become proficient in Java as you are new in programing
- Save an userform as an image in EXCEL
- An easy instance of using the template class in C++
- 【C语言】getchar,putchar
- Oracle 10g RAC sqlplus / as sysdba Connected to an idle instance
- contextTypes was defined as an instance property on MyButton. Use a static property to define contex
- 学习记录: Server Error in '/' Application. Object reference not set to an instance of an object
- An instance of getting the maximum and minimum values in an array
- not being configured as an application in IIS
- How to call different methods as given in an array
- PHPExcel_Reader_Exception: is not recognised as an OLE file in Classes
- SLF4J和Logback日志框架详解
- Hadoop Installation on Linux
- Spring线程池开发实战
- Java输出数组的内容
- 分布式稀疏矩阵的数据结构(data structure for distributed sparse matrix)
- Putchar in Keil C, take 8051 as an instance
- HMM
- Jade学习笔记
- POJ 2774 解题报告
- DOS下的SVGA编程
- Python 格式化日期
- scanf的格式
- 转义字符
- 变量的地址和指针