Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个retarget.c是如何工作的_C_Embedded_Int_Fgets_Uart - Fatal编程技术网

这个retarget.c是如何工作的

这个retarget.c是如何工作的,c,embedded,int,fgets,uart,C,Embedded,Int,Fgets,Uart,有人能给我解释一下这个retarget.c是如何工作的吗? 我正在尝试向微控制器的uart发送整数和从uart发送整数,我已经成功地使用fgets获取字符(16位),并在uart中断服务例程中使用atoi函数返回整数,但我正在尝试使用scanf获取整数,我想我需要更改下面概述的重定目标文件 #include <stdio.h> #include <time.h> #include <rt_misc.h> #define AHB_LED_BASE

有人能给我解释一下这个retarget.c是如何工作的吗? 我正在尝试向微控制器的uart发送整数和从uart发送整数,我已经成功地使用fgets获取字符(16位),并在uart中断服务例程中使用atoi函数返回整数,但我正在尝试使用scanf获取整数,我想我需要更改下面概述的重定目标文件

#include <stdio.h> 
#include <time.h>
#include <rt_misc.h>

#define AHB_LED_BASE                0x50000000
#define AHB_UART_BASE               0x51000000

#pragma import(__use_no_semihosting)

struct __FILE { 
    unsigned char * ptr;
    };

FILE __stdout =     {(unsigned char *)  AHB_UART_BASE};
FILE __stdin =      {(unsigned char *)  AHB_UART_BASE};

int fputc(int ch, FILE *f)
{
  return(uart_out(ch));

}

int fgetc(FILE *f)
{ 
    return(uart_in());
}

int ferror(FILE *f)
{
  return 0; 
}

int uart_out(int ch)
{
    int* UARTPtr;
    UARTPtr = (int*)AHB_UART_BASE;
    *UARTPtr = (int)ch;
    return(ch);
}

int uart_in()
{
    int ch;
    int* UARTPtr;
    UARTPtr = (int*)AHB_UART_BASE;  
    ch = *UARTPtr;
    uart_out(ch);
    return((int)ch);
}

void _ttywrch(int ch)
{ 
  fputc(ch,&__stdout); 
}

void _sys_exit(void) {


    while(1); 

}


//------------------------------------------------------------------------------
// Cortex-M0 
//------------------------------------------------------------------------------

#include <stdio.h>
#include <time.h>
#include <rt_misc.h>
#include <stdlib.h>

#define AHB_LED_BASE                0x50000000
#define AHB_UART_BASE               0x51000000


void UART_ISR(void)
{
            int sample;

            printf("the value entered is %d\n", sample);
}

//////////////////////////////////////////////////////////////////
// Main Function
//////////////////////////////////////////////////////////////////

int main() {

    {

        int sample;
        scanf ("%d",&sample);

    }
}
#包括
#包括
#包括
#定义AHB_LED_底座0x50000000
#定义AHB_UART_基准0x51000000
#pragma导入(使用、不使用、半托管)
结构文件{
无符号字符*ptr;
};
文件uu stdout={(无符号字符*)AHB_UART_BASE};
文件uu stdin={(无符号字符*)AHB_UART_BASE};
int fputc(int ch,文件*f)
{
返回(uart_out(ch));
}
int fgetc(文件*f)
{ 
返回(uart_in());
}
int ferror(文件*f)
{
返回0;
}
内部uart_输出(内部通道)
{
int*UARTPtr;
UARTPtr=(int*)AHB_UART_基础;
*UARTPtr=(int)ch;
返回(ch);
}
国际货币单位
{
int-ch;
int*UARTPtr;
UARTPtr=(int*)AHB_UART_基础;
ch=*UARTPtr;
通用异步收发器输出(ch);
返回((int)ch);
}
void\u ttywrch(内部通道)
{ 
fputc(中国和美国标准时间);
}
作废(系统)退出(作废){
而(1),;
}
//------------------------------------------------------------------------------
//皮质-M0
//------------------------------------------------------------------------------
#包括
#包括
#包括
#包括
#定义AHB_LED_底座0x50000000
#定义AHB_UART_基准0x51000000
无效UART\U ISR(无效)
{
int样本;
printf(“输入的值是%d\n”,示例);
}
//////////////////////////////////////////////////////////////////
//主要功能
//////////////////////////////////////////////////////////////////
int main(){
{
int样本;
scanf(“%d”和样本);
}
}

需要使用
scanf
功能,因为它必须在缓冲区中向前扫描,以查看字段何时结束。例如,在查找数字时,需要在数字后拉一个字符以查看数字的结尾。当它看到非数字字符时,需要将其放回流中,以便下次调用
getc
将获得它

大概是这样的:

struct __FILE
{ 
    unsigned char * ptr;
    int unchar; /* place to keep the character put back in the stream */
};

FILE __stdout =     {(unsigned char *)  AHB_UART_BASE, -1};
FILE __stdin =      {(unsigned char *)  AHB_UART_BASE, -1};

int fgetc(FILE *f)
{ 
    int c;

    if (f->unchar == -1)
    {
        c = uart_in();  /* just read a character */
    }
    else
    {
        c = f->unchar;  /* reuse the character put back by ungetc */
        f->unchar = -1; /* mark it as used */
    }
    return c;
}

int fungetc(int c, FILE *f)
{
    unsigned char uc = c; /* POSIX says that it is converted first to unsigned char */
    f->unchar = (int )uc; /* put back the character */
    return (int )uc;
}

我的答案中添加了更多的文本和注释。您好,谢谢您的回复,但对于我在主要问题retarget.c下面添加的代码,它仍然返回0。在putty/hyperterminal上输入单个字符后,Uart ISR被激活,我是否应该考虑增加输入缓冲区的大小?