arm体系编程

本文详细介绍了在ARM体系中如何初始化UART并实现串口通信的基本功能,包括发送字符、接收字符、字符串输入输出及十进制十六进制字符串转换为整数的方法。通过定义内存映射寄存器地址,结合UART控制寄存器配置,实现了STUDIO库的替代函数,便于进行串口交互。
 

#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; 
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值