本实验是使用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_end把ap指针清为NULL。函数体内可以多次遍历这些参数,但是都必须以va_start开始,并以va_end结尾。
vsprintf(string,fmt,ap);实现向字符数组中写指定格式的内容。
以下是具体的实验代码:
#define
#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
#define LED1_OFF
void delay(int times)//延时函数
{
}
void Uart0_init(void)
{
}
void Uart_sendByte(int data)//串口发送字节
{
}
void Uart_sendString(char *pt)//串口发送字符串
{
}
void Uart_printf(char *fmt,...)
{
}
void Main(void)//主函数
{
}