打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
int
char 类型:占一个字节,里面存的是字符型,即ASC||码,它只能表示127种字符
0~9:ASC||码表示为:‘0’~‘9’或引用ASC||码的值:48~57
A~Z:ASC||码表示为:‘A’~‘Z’或引用ASC||码的值:65~90
a~z:ASC||码表示为:‘a’~‘z’或引用ASC||码的值:97~122
int类型转换为char类型:
int yy;
0~9:yy+'0'或yy+48
A~Z:yy+'65'或yy+'A'
a~z:yy+'97'或yy+'a'

int 类型:占两个字节,默认为signed int,表示范围为:65536/2,以二进制的形式存储
char类型转换为int类型:
char yy;
0~9:yy-'0'或yy-48
A~Z:yy-'65'或yy-'A'
a~z:yy-'97'或yy-'a'

//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ(void)
{
    char string[16] ;
    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[i] != '\0' )
{
data = data * 10 ;
if( string[i]<'0'||string[i]>'9' )
return -1 ;
data = data + ( string[i]-'0' ) ;
//data = data + ( string[i]) ;
i++ ;
}
return data ;
}

vs

//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ(void)
{
    int string[16] ;
    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[i] != '\0' )
{
data = data * 10 ;
if( string[i]<'0'||string[i]>'9' )
return -1 ;
//data = data + ( string[i]-'0' ) ;
data = data + ( string[i]) ;
i++ ;
}
return data ;
}


注:
S3C2440的UART处理的数据默认为char类型的,即ASC||码,所以int类型的话要注意转换为char类型,否则向PC发送的时候会出现乱码



-----------------------------------------------------------------------------
main函数中的调用:
a = Uart_GetIntNum_GJ();
    Uart_SendByte(a+'0');//发送数字0--9,,2440默认为ASC||码
//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ(void)
{
    char string[16] ;
    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[i] != '\0' )
{
data = data * 10 ;
if( string[i]<'0'||string[i]>'9' )
return -1 ;
data = data + ( string[i]-'0' ) ;//处理数字0--9,
//data = data + ( string[i]) ;
i++ ;
}
return data ;
}

--------------------------------------------------------------------------------------
main函数中的调用:
a = Uart_GetIntNum_GJ();
    Uart_SendByte(a+'a');////发送小写字母
//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ(void)
{
    char string[16] ;
    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[i] != '\0' )
{
data = data * 10 ;
if( string[i]<'0'||string[i]>'9' )
//return -1 ;
data = data + ( string[i]-'a' ) ;//处理小写字母
//data = data + ( string[i]) ;
i++ ;
}
return data ;
}



-------------------------------------------------------------------------------------
main函数中的调用:
a = Uart_GetIntNum_GJ();
     Uart_SendByte(a+'A');////发送大写字母
//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ(void)
{
    char string[16] ;
    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[i] != '\0' )
{
data = data * 10 ;
if( string[i]<'0'||string[i]>'9' )
//return -1 ;
data = data + ( string[i]-'A' ) ;//处理大写字母
//data = data + ( string[i]) ;
i++ ;
}
return data ;
}



-------------------------------------------------------------------------------------
型如测试各函数代码的写法:
<***********************************************>
           SBC2440 Test Program VER1.0
                www.arm9.net
      Build time is: Jul 17 2009  23:30:50
          Image$$RO$$Base  = 0x30000000
          Image$$RO$$Limit = 0x300344c8
          Image$$RW$$Base  = 0x300344c8
          Image$$RW$$Limit = 0x300e2280
          Image$$ZI$$Base  = 0x30095ab0
          Image$$ZI$$Limit = 0x300e2280
<***********************************************>

Please select function : 
0 : Please input 1-16 to select test
1 : Test PWM
2 : RTC time display
3 : Test ADC
4 : Test interrupt and key scan
5 : Test Touchpanel
6 : Test TFT-LCD or VGA1024x768 module
7 : Test IIC EEPROM, if use QQ2440, please remove the LCD
8 : UDA1341 play music
9 : Test SD Card
10 : Test CMOS Camera

实现代码:
main函数中:
Uart_SendByte('\n');
Uart_Printf("<***********************************************>\n");
Uart_Printf("           SBC2440 Test Program VER1.0\n");
Uart_Printf("                www.arm9.net\n");
Uart_Printf("      Build time is: %s  %s\n", __DATE__ , __TIME__  );
    Uart_Printf( "          Image$$RO$$Base  = 0x%x\n", Image$$RO$$Base );
Uart_Printf( "          Image$$RO$$Limit = 0x%x\n", Image$$RO$$Limit );
Uart_Printf( "          Image$$RW$$Base  = 0x%x\n", Image$$RW$$Base );
Uart_Printf( "          Image$$RW$$Limit = 0x%x\n", Image$$RW$$Limit );
    Uart_Printf( "          Image$$ZI$$Base  = 0x%x\n", Image$$ZI$$Base );
    Uart_Printf( "          Image$$ZI$$Limit = 0x%x\n", Image$$ZI$$Limit );
Uart_Printf("<***********************************************>\n");

void Temp_function() { Uart_Printf("\nPlease input 1-16 to select test!!!\n"); }

关键部分:结构体数组,第一个是指向函数的指针,第二个是指向字符串,下面是分别为这俩变量赋值
struct {
void (*fun)(void);
char *tip;
}CmdTip[] = {
{ Temp_function, "Please input 1-16 to select test" } ,
{ BUZZER_PWM_Test, "Test PWM" } ,
{ RTC_Display, "RTC time display" } ,
{ Test_Adc, "Test ADC" } ,
{ KeyScan_Test, "Test interrupt and key scan" } ,
{ Test_Touchpanel, "Test Touchpanel" } ,
{ TFT_LCD_Test, "Test TFT-LCD or VGA1024x768 module" } ,
{ Test_Iic, "Test IIC EEPROM, if use QQ2440, please remove the LCD" } ,
{ PlayMusicTest, "UDA1341 play music" } ,
{ Test_SDI, "Test SD Card" } ,
{ Camera_Test, "Test CMOS Camera"},
{ 0, 0}
};


这里将用户的输入与相应的函数对应起来:
while(1)
{
U8 idx;
Uart_Printf("\nPlease select function : \n");
for(i=0; CmdTip[i].fun!=0; i++)
Uart_Printf("%d : %s\n", i, CmdTip[i].tip);
idx = Uart_GetIntNum_GJ() ;
if(idx<>< font=""><>
{
(*CmdTip[idx].fun)();
Delay(20);
Uart_Init( 0,115200 );
}
}


接受用户的输入:  
//*****************************************************************************
//get a number for the uart
//*****************************************************************************
int Uart_GetIntNum_GJ(void)
{
    char string[16] ;
    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[i] != '\0' )
{
data = data * 10 ;
if( string[i]<'0'||string[i]>'9' )
return -1 ;
data = data + ( string[i]-'0' ) ;
i++ ;
}
return data ;
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
S3C2440之UART
S3C2440的UART详解
要个计算机与单片机的串口通信上位机C语言程序
CC2530 UART串口实验1(UART0串口发送字符串)
在AHK脚本中使用Send发送中文
51单片机串口通信使用类似printf函数的两种办法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服