【发布时间】: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