模拟getchar()的UART驱动程序

时间:2017-03-10 15:57:57

标签: c embedded driver uart getchar

我有一个UART驱动程序,它会读取串行控制台的一串数据,直到用户输入“'”为止。但是,我只想读一个字符。它用于用户输入1-9或a-z的菜单。我试图创建标准C getchar()的等价物。我怎么能这样做呢?

这是UART的寄存器映射。

enter image description here

void getstring(char *str) {

    volatile uint32_t *uart = (volatile uint32_t*) UART;
    char c = 0;

    do {
        while ((uart[2] & (1<<7)) == 0);
        c = uart[0];
        *str++ = c;
    }
    while (c!='l');
}

1 个答案:

答案 0 :(得分:0)

循环在循环中一次检索一个字符。如果你只想要一个字符,那么只需省略循环外部。

char getch() 
{
    volatile uint32_t *uart = (volatile uint32_t*) UART;
    while ((uart[2] & (1<<7)) == 0);
    return uart[0];
}

然后使用此函数重写getstring()是有意义的(更易于维护)。

相关问题