#define UTXHO *((volatile int *)0x7f005020)
#define UTRSTAT0 *((volatile int *)0x7f005010)
#define URXH0 *((volatile int *)0x7f005024)
#define GPACON *((volatile int *)0x7f008000)
#define UBRDIV0*((volatile int *)0x7f005028)
#define UDIVSLOTO *((volatile int *)0x7f00502c)
#define ULCON0 *((volatile int *)0x7f005000)
#define UCON0 *((volatile int *)0x7f005004)
初始化
void uart_init(void)
{
GPACON=0x22; //uart r/t
UBRDIV0=34; //configure baud rate (integer)
UDIVSLOTO=0x1fff; //divisor
ULCON0=0x3; //uart line control
UCON0=0xc05; //uart control
}
实现STDIO库的几个基本函数
void uart_putchar(char c)
{
while((UTRSTAT0&(1<<2)) == 0);
UTXHO=c;
}
char uart_getchar(void)
{
while((UTRSTAT0&(1<<0))==0);
return URXH0;
}
void uart_putchar(char c)
{
while((UTRSTAT0&(1<<2)) == 0);
UTXHO=c;
}
char uart_getchar(void)
{
while((UTRSTAT0&(1<<0))==0);
return URXH0;
}
char *mygets(char *s)
{
int i=0;
while( ( *(s+i )= uart_getchar()) != '\r')
{
if(*(s+i) == '\b')
{
if(i == 0 )
continue ;
uart_putchar('\b');
uart_putchar(' ');
uart_putchar('\b');
i--;
}
else
uart_putchar(*(s+i));
i++;
}
return s ;
}
十进制十六进制字符串转换为整形
int hex_atoi( char *s)
{
int num=0,i=0;
if((*s == 'o')&&(*(s+1) == 'x'))
{
s += 2;
while(*s)
{
if((*s < '9')&&(*s > '0'))
{
num *= 16;
num += *s;
s++;
}
else if( (*s>'a')&&(*s < 'z'))
{
num *= 16;
num += *s -'a'+10;
s++;
}
else if((*s >'A')&&(*s <'Z'))
{
num *=16;
num +=*s - 'A'+10 ;
s++;
}
}
}
else
{
while(*s) {
num *= 10;
num += *s;
s++;
}
}
return num;
}
char *myputs(char *s)
{
int i=0;
while(*(s+i) != '\0')
{
if(*(s+i) = '\b')
{
i++ ;
continue;
}
uart_putchar(*(s+i));
i++ ;
}
return s;
}
用于解析命令的函数
char **analys(char *s)
{
char *str[5] = {NULL};
int stat = 0,i = 0,j=0;
while(*(s+i) != '\0')
{
if((stat=0)&&(*(s+i)!= SPACE))
{
stat = 1 ;
str[j++] =s + i++;
}
else { if(( stat == 1)&&(*(s+i)==SPACE) )
{
stat =0;
*(s + i++) = '\0';
}
else i++;
}
}
return str;
}