【问题标题】:DE1-SoC displaying LEDsDE1-SoC 显示 LED
【发布时间】:2020-05-18 02:42:49
【问题描述】:

我正在尝试使用 DE1-SoC 板来运行这个程序。它应该允许用户输入一个字符,并在板上的红色 LED 上以二进制形式返回该字母。它使用两个函数来接收用户输入并将执行情况显示到终端。当我运行程序时,随机字符(如 å)得到输出,而不是常规字符。

这是我的代码。

#include "JTAG_UART.h"
#include "address_map_arm.h"

int main(void) {
    /* Declare volatile pointers to I/O registers (volatile means that IO load
       and store instructions will be used to access these pointer locations,
       instead of regular memory loads and stores) */
    volatile int * JTAG_UART_ptr = (int *)JTAG_UART_BASE; // JTAG UART address
volatile int * LED_ptr = (int*)LED_BASE;
    char  text_string[] = "\nJTAG UART example code\n> \0";
    char *str, * c;
  //  char *c_ptr=c;

    /* print a text string */
    for (str = text_string; *str != 0; ++str)
        put_jtag(JTAG_UART_ptr, *str);

    /* read and echo characters */
    while (1) {
        c = get_jtag(JTAG_UART_ptr);
         if (c != 0 && c<123 && c>96){
         *LED_ptr = *c ;
         put_jtag(JTAG_UART_ptr, *c);
        }
           // put_jtag(JTAG_UART_ptr, c);
    }
}

这是我引用的函数的代码。

#include "JTAG_UART.h"

/*******************************************************************************
 * Subroutine to send a character to the JTAG UART
 ******************************************************************************/
void put_jtag(volatile int * JTAG_UART_ptr, char c) {
    int control;
    control = *(JTAG_UART_ptr + 1); // read the JTAG_UART control register
    if (control & 0xFFFF0000)       // if space, echo character, else ignore
        *(JTAG_UART_ptr) = c;
}

/*******************************************************************************
 * Subroutine to read a character from the JTAG UART
 * Returns \0 if no character, otherwise returns the character
 ******************************************************************************/
char get_jtag(volatile int * JTAG_UART_ptr) {
    int data;
    data = *(JTAG_UART_ptr); // read the JTAG_UART data register
    if (data & 0x00008000)   // check RVALID to see if there is new data
        return ((char)data & 0xFF);
    else
        return ('\0');
}

输入一个像'a'这样的字符,它是ASCII中的十进制数字97。应该将自己显示为 01100001,每个“1”代表自己在板上亮起。正如我所说,我有一个逻辑错误,而正在读取输入“a”将显示为 00010000

【问题讨论】:

  • 首先,您应该放弃“草率打字”。你绝对不想对 32 位寄存器使用带符号的 int 类型,将其替换为 uint32_t。仅将 char 用于实际字符,从不用于原始数据 - 为此使用 uint8_t。关于您的十六进制文字,您应该以ul 结束它们,以确保它们始终属于同一类型。一旦您摆脱了潜在的带符号数字错误,您将能够更好地解决问题。
  • 您确定您的编译器没有抱怨将char 分配给char* 这里:c = get_jtag(JTAG_UART_ptr); !?
  • 我的回答没有解决@Lundin 的批评,因为它们没有解决您所询问的具体问题 - 但不要忽视它们,它们解决了其他问题。
  • 不要破坏您的问题。如果您想将其标记为已解决,请单击答案上投票箭头下方的复选标记。

标签: c pointers embedded intel fpga


【解决方案1】:

您已将c 定义为char*,而它显然应该是char

char c ;

然后松开 *c 取消引用:

*LED_ptr = c ;
put_jtag(JTAG_UART_ptr, c);

行:

 c = get_jtag(JTAG_UART_ptr);

应该发出警告; GCC 示例输出:

warning: initialization makes pointer from integer without a cast [-Wint-conversion]

不要忽略(或禁用)警告;至少不要忽略它们,然后在这里提问而不提及警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多