mini2440 UART Print实验

来源:互联网 发布:ipc软件下载 编辑:程序博客网 时间:2024/04/20 14:57

本实验是使用UART 实现打印功能,考虑到以后程序需要使用该功能显示,所以进行本次实验。主要难点在于UART Print函数的编写,必须调用标准库函数#include <stdarg.h>,下面是该函数具体解释:

va_list完成可变参数的操作

具体实现如下

n 由于无法列出传递函数的所有实参的类型和数目时,用省略号指定参数表

void Uart_Printf(char *fmt,...)

n 函数参数的传递原理

函数参数是以数据结构:栈的形式存取,从右至左入栈.

n 获取省略号指定的参数

在函数体中声明一个va_list,然后用va_start函数来获取参数列表中的参数,使用完毕后调用va_end()结束。

va_list ap;

char string[256];

va_start(ap,fmt);

vsprintf(string,fmt,ap);

Uart_SendString(string);

va_end(ap);

va_start使ap指向第一个可选参数。va_endap指针清为NULL。函数体内可以多次遍历这些参数,但是都必须以va_start开始,并以va_end结尾。

vsprintf(string,fmt,ap);实现向字符数组中写指定格式的内容。

 

以下是具体的实验代码:

#define    GLOBAL_CLK              1

#include <stdlib.h>

#include <string.h>

#include "def.h"

#include "option.h"

#include "2440addr.h"

#include "2440lib.h"

#include "2440slib.h"

#include "mmu.h"

#include "profile.h"

#include "memtest.h"

#include <stdarg.h>

 

 

 

#define LED1_ON   ~(1<<5)

 

#define LED1_OFF   (1<<5)

 

void delay(int times)//延时函数

{

    int i;

    for(;times>0;times--)

      for(i=0;i<400;i++);

}

 

void Uart0_init(void)

{

      

       rGPHCON = 0xa0; //设置GPH2GPH3TXD0(发送),RXD0(接收)

 

       rGPHUP  = 0xf0;//禁止GPH2GPH3上拉功能

 

      rULCON0 = 0x3; //设置UART0无奇偶校验,一位停止位,8位数据

 

       rUCON0 = 0x245; //PCLK为时钟源,接收和发送数据为中断方式

 

       rUFCON0 = 0; //不使用FIFO

 

       rUMCON0 = 0; //不使用流控制

 

       rUBRDIV0 = 26; //((UART_CLK/(波特率*16)-1)设置波特率,PCLK50MHz,波特率为115.2kHz

}

 

void Uart_sendByte(int data)//串口发送字节

{

       if(data=='\n')

       {

              while(!(rUTRSTAT0 & 0x2));

 

              rUTXH0='\r';

       }

       else

       {

              while(!(rUTRSTAT0 & 0x2));

             

              rUTXH0=data;             

       }

}

 

void Uart_sendString(char *pt)//串口发送字符串

{

       while(*pt)

              Uart_sendByte(*pt++);

}

 

void Uart_printf(char *fmt,...)

{

       va_list ap;

 

       char string[256];

 

       va_start(ap,fmt);

 

       vsprintf(string,fmt,ap);

 

       Uart_sendString(string);

 

       va_end(ap);

} 

 

void Main(void)//主函数

{ 

          

       Uart0_init();//UART0初始化

       Uart_printf("UART pritf功能测试!\n");

       Uart_printf("测试成功!\n");

      

 

}