ahong2hao 发表于 2011-4-20 17:30:08

UART裸奔Uart_Printf()函数问题;

裸奔程序如下。
如果不添加MANI()函数前的几个uart名字开头函数,程序下载后能运行,添加之后就 不能了,
下载后会提示"Running program at 0x30000000",但马上又显示
<1> Download bootloader to nand flash
<2> Download linux kernel to nand flash
<3> Download linux file system to nand flash
<4> Format the nand flash
<5> Boot the linux system
<6> Download e-boot to nand flash
<7> Download program to RAM and run
<8> Download logo picture(*.bmp or *.BMP)
<9> Download program to nor flash
Please select:
这些,程序不能运行的。为什么呢。
我想用Uart_Printf();函数来输出。如何才能正确使用?谢谢了。


#include "2440addr.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>


//***************************[ UART ]******************************
void Uart_SendByte(int data);
void Uart_Printf(char *fmt,...);
void Uart_TxEmpty(int ch)
{
    if(ch==0)
      while(!(rUTRSTAT0 & 0x4)); //Wait until tx shifter is empty.
         
    else if(ch==1)
      while(!(rUTRSTAT1 & 0x4)); //Wait until tx shifter is empty.
      
    else if(ch==2)
      while(!(rUTRSTAT2 & 0x4)); //Wait until tx shifter is empty.
}

//=====================================================================
char Uart_Getch(void)
{
         
      while(!(rUTRSTAT0 & 0x1)); //Receive data ready
      return RdURXH0();
   
   
    return 0 ;
}

//====================================================================
char Uart_GetKey(void)
{
         
      if(rUTRSTAT0 & 0x1)    //Receive data ready
            return RdURXH0();
      else
            return 0;
   
       
}

//====================================================================
void Uart_GetString(char *string)
{
    char *string2 = string;
    char c;
    while((c = Uart_Getch())!='\r')
    {
      if(c=='\b')
      {
            if( (int)string2 < (int)string )
            {
                Uart_Printf("\b \b");
                string--;
            }
      }
      else
      {
            *string++ = c;
            Uart_SendByte(c);
      }
    }
    *string='\0';
    Uart_SendByte('\n');
}

//=====================================================================
int Uart_GetIntNum(void)
{
    char str;
    char *string = str;
    int base   = 10;
    int minus    = 0;
    int result   = 0;
    int lastIndex;   
    int i;
   
    Uart_GetString(string);
   
    if(string=='-')
    {
      minus = 1;
      string++;
    }
   
    if(string=='0' && (string=='x' || string=='X'))
    {
      base    = 16;
      string += 2;
    }
   
    lastIndex = strlen(string) - 1;
   
    if(lastIndex<0)
      return -1;
   
    if(string=='h' || string=='H' )
    {
      base = 16;
      string = 0;
      lastIndex--;
    }

    if(base==10)
    {
      result = atoi(string);
      result = minus ? (-1*result):result;
    }
    else
    {
      for(i=0;i<=lastIndex;i++)
      {
            if(isalpha(string))
            {
                if(isupper(string))
                  result = (result<<4) + string - 'A' + 10;
                else
                  result = (result<<4) + string - 'a' + 10;
            }
            else
                result = (result<<4) + string - '0';
      }
      result = minus ? (-1*result):result;
    }
    return result;
}

//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ(void)
{
        char string ;
        char *p_string = string ;
        char c;
        int i = 0 ;
        int data = 0 ;

        while(   ( c = Uart_Getch()) != '\r')
        {
                if(c=='\b')                p_string--;
                else                *p_string++=c;

                Uart_SendByte( c ) ;
        }

        *p_string = '\0';

        i = 0 ;
        while( string != '\0' )
        {
                data = data * 10 ;
                if( string<'0'||string>'9' )
                        return -1 ;
                data = data + ( string-'0' ) ;
                i++ ;               
        }       
       
        return data ;
}

void Uart_SendByte(int data)
{
                if(data=='\n')
                {
                        while(!(rUTRSTAT0 & 0x2));
                        // Delay(1);               //because the slow response of hyper_terminal
                        WrUTXH0('\r');
                }
                while(!(rUTRSTAT0 & 0x2));   //Wait until THR is empty.
                //Delay(1);
                WrUTXH0(data);
       
}         

//====================================================================
void Uart_SendString(char *pt)
{
    while(*pt)
      Uart_SendByte(*pt++);
}

//=====================================================================
//If you don't use vsprintf(), the code size is reduced very much.

void Uart_Printf(char *fmt,...)
{
        va_list ap;
        char string;

        va_start(ap,fmt);
       vsprintf(string,fmt,ap);
        Uart_SendString(string);
        va_end(ap);
}













void Main(void)
{
        char ch;
        rGPBCON = 0x015551;
        rGPBUP = 0x7ff;
        rGPBDAT = 0x1e0;
        rGPHCON = 0x00faaa; // 使用 UART0 功能
        rGPHUP = 0x7ff;
        rULCON0 = 0x3; // 设置 UART0 无奇偶校验,一位停止位, 8 位数据
        rUCON0 = 0x245; //PCLK 为时钟源,接收和发送数据为查询或中断方式
        rUFCON0 = 0; //
        rUMCON0 = 0; //
        rUBRDIV0 = 26; // 设置波特率, PCLK 为 50MHz ,波特率为 115.2kHz
        while(!(rUTRSTAT0 & 0x2)); // 等待并判断发送缓存是否为空
        rUTXH0 = 0xaa; // 是空,则发送 0xAA
        while(1)
        {
                       while(!(rUTRSTAT0 & 0x1)); // 等待并判断接收缓存是否准备好
                        ch = rURXH0; // 接收一个字节数据
                        while(!(rUTRSTAT0 & 0x2)); // 等待并判断发送缓存是否为空
                        rUTXH0 = ch; // 发送一个字节数据
                       
                       switch(ch) // 根据所接收数据的不同,执行不同的程序
                        {
                                case 0x11: // 灭 LED
                                rGPBDAT |= 0x1e0;
                                break;
                                case 0x22: // 亮 LED
                                rGPBDAT &= 0x1f;
                                break;
                                case 's': // 蜂鸣器不响
                                rGPBDAT &= 0x1e0;
                               
                                break;
                                case 'a': // 蜂鸣器响;
                                rGPBDAT |= 0x1;
                                break;
                                default: //LED 灭,蜂鸣器不响
                                rGPBDAT = 0x1e0;
                                break;
                        }   
                       
        }
}

谭丙冠 发表于 2011-4-24 11:57:36

WTW11897 发表于 2011-4-24 13:28:07

请问你选择的是哪个模式呀

WTW11897 发表于 2011-4-24 13:41:12

你想用直接用就行了撒。。。
前面还写那么多干嘛?这些函数在2440lib.c里面都有

三生石 发表于 2011-9-9 15:27:53

我好郁闷啊~ 兄弟 , 你这个我复制过去为什么不行!
页: [1]
查看完整版本: UART裸奔Uart_Printf()函数问题;