S3C2440在MDK4.22下使用printf向串口打印调试

来源:互联网 发布:python的set函数 编辑:程序博客网 时间:2024/05/22 02:31

背景知识:

串口的基本知识已经在上一篇讲过了。这里重点讲解如何在MDK4.22下使用printf函数,这样的话就可以很方便的打印调试信息,追踪。

这个知识来源于MDK自带的帮助手册。有现成的代码提供。

实现方式有2种,使用标准C库下裁剪合适的函数,使用微库C下裁剪合适的函数。

微库下的情况,在魔术棒那里要勾选上使用微库。然后需要定义如下结构和改写如下函数--FILE stdout fputc ferror。

标准库的情况,也是需要关注FILE stdout fputc ferror。注意网上很多文章说,在标准库下,需要关掉半主机模式,我尝试过,关掉后,需要定义_sys_exit函数,可以达到效果,但是如果不关掉半主机模式,和微库一样也只定义该定义的,也可以达到效果。不知道,是不是MDK版本升级后,已经统一了两种模式。


具体代码:

uart.c

#include "S3C2440.h"#include "uart.h"void init_uart0(void) {rULCON0 = 0x03;  rUCON0 = (0x05);//15---12   11-1098765 43-21-0//not pclk/npclkTpulseRpulsetimeout disablerx error int disable   loop dis  break disint or pollint or poll  rUFCON0 = 0x00;  rUMCON0 = 0x0; rUBRDIV0 = UART_BRDIV;} struct __FILE  {  int handle;  /* Whatever you require here. If the only file you are using is */  /* standard output using printf() for debugging, no file handling */  /* is required. */  };  /* FILE is typedef’ d in stdio.h. */  FILE __stdout;  int fputc(int ch, FILE *f) {          WrUTXH0_L(ch);          /* Loop until the end of transmission */     while(!(rUTRSTAT0 & TXD0READY)) ;    return ch; } int ferror(FILE *f) {  /* Your implementation of ferror */  return EOF;}

main.c

#include "S3C2440.h"#include "uart.h"#include <stdio.h>int main(void){init_uart0();printf("hello world\r\n");}

最终可以在UART0上打印hello world,这样以后程序就可以拿来复用了!


原创粉丝点击