如何在C编程图片中连接变量?

发布于 2024-08-04 19:08:06 字数 410 浏览 10 评论 0原文

我用 hitech C 编程 16f84a pic 来驱动 hd44780 液晶屏。到目前为止,我已经初始化了液晶显示器,并且可以将单个字符和字符串写入液晶显示器。现在我需要做这样的事情:

var = 250;
lcd_write_string("MyVar 的值为:" + var);
所以液晶屏应该显示“MyVar has value: 250”

首先我应该如何连接一个var和一个字符串? 其次,变量var包含一个8位二进制数(十进制0-255)。如果var=23; 8 位数字必须分成 2 部分以表示 ASCII 中的 2 和 3,然后由 lcd 显示。我该怎么做?看来我必须进行基数 10 转换或 if 树来分割所有 2 位数字,然后显示在液晶显示屏上。有没有更简单的方法来解决这个问题?

谢谢!

I programming a 16f84a pic in hitech C to drive a hd44780 lcd. So far I've got the lcd initialized and can write individual characters and strings to the lcd. Now I need to do something like this:

var = 250;
lcd_write_string("MyVar has value: " + var);
so the lcd should show "MyVar has value: 250"

First of all how should I concatenate a var and a string?
second, the variable var contains an 8 bit binary number (0-255 in decimal). If var = 23; the 8 bit number has to be split into 2 to represent the 2 and the 3 in ascii to then be shown by the lcd. how can I do this? It seems I have to do base 10 conversions or a if tree to split all 2 digit numbers to then be shown in the lcd. Is there an easier way around this?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

毁我热情 2024-08-11 19:08:06

为什么不直接使用 printf("%d", var) 呢?

var = 250;
char *static_msg = "MyVar has value:";
char msg[sizeof(static_msg) + 4];

sprintf(msg, "%s %d", static_msg, var);
lcd_write_string(msg);

Why don't you just use printf("%d", var) ?

var = 250;
char *static_msg = "MyVar has value:";
char msg[sizeof(static_msg) + 4];

sprintf(msg, "%s %d", static_msg, var);
lcd_write_string(msg);
爺獨霸怡葒院 2024-08-11 19:08:06

假设您的工具集不包含标准 C 库,或者您想自己弄清楚如何执行此操作,那么我将采用以下方法:

声明一个缓冲区,其中包含 LCD 可以显示的尽可能多的字符。将固定字符串写入该缓冲区的开头。确定变量的字符宽度(我在这里省略了一些数学知识,但我认为这是一个合理的练习)。使用该长度和固定字符串的长度,计算出您的号码的数字将存放在缓冲区中的哪个位置。使用与之前相同的数学结构,将数字一一写入该空间。 C 语言有一个技巧(标准要求),可以让您通过表达式 '0' + digital 计算出十进制数字的字符值。希望您能明白这是如何运作的。最后,按照 lcd_write_string 函数的要求终止缓冲区,并在缓冲区上调用它。

正如其他受访者所指出的,这本质上实现了标准库 printf 系列函数的一小部分。我认为弄清楚实现是值得的,即使您可以(并且应该!)在生产代码中使用该库。

由于这听起来有点像家庭作业,所以我还没有进一步详细说明。如果您需要特定问题的帮助,我将查找问题的更新。

Supposing your toolset does not include the standard C library, or that you want to figure out how to do this yourself, here's the approach I would take:

Declare a buffer of as many characters as the LCD can display. Write your fixed string into the beginning of that buffer. Determine how many characters wide your variable will be (there's some math I'm leaving out here, but it's a reasonable exercise I think). Using that length, and the length of the fixed string, figure out where in the buffer your number's digits will go. Using the same mathematical constructs as before, write the digits into that space, one by one. There's a trick of the C language (required by the standard) that lets you figure out the character value of a decimal digit by the expression '0' + digit. Hopefully, you see how that works. Finally, terminate the buffer however your lcd_write_string function expects, and call it on the buffer.

As indicated by other respondents, this essentially implements a small part of the standard library's printf family of functions. I think it's worthwhile to figure out the implementation, even if you can (and should!) use the library in production code.

Since this smells vaguely like homework, I'm not elaborating further just yet. If you need help on specific points, I'll look for updates to the question.

甜妞爱困 2024-08-11 19:08:06

如果 C 编译器附带标准 C 库函数,则 sprintf< /a> 可以与 printf 类似地使用,以生成字符串。

sprintf 可用于生成格式化字符串(如 char*),该字符串可能会发送到 lcd_write_string 函数。

If the C compiler comes with the standard C library functions, then sprintf can be used similar to printf, in order to produce a string.

sprintf can be used to produce a formatted string (as char*), which could presumably be sent to the lcd_write_string function.

双马尾 2024-08-11 19:08:06

sprintf() 的问题是您不知道在调用 sprintf() 之前需要分配多少字节。我通常使用以下 ksprintf() 作为替代。

typedef struct __kstring_t {
  size_t l, m;
  char *s;
} kstring_t;

int ksprintf(kstring_t *s, const char *fmt, ...)
{
  va_list ap;
  int l;
  va_start(ap, fmt);
  l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); // not working with glibc 2.0
  va_end(ap);
  if (l + 1 > s->m - s->l) {
    s->m = s->l + l + 2;
    kroundup32(s->m);
    s->s = (char*)realloc(s->s, s->m);
    va_start(ap, fmt);
    l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
  }
  va_end(ap);
  s->l += l;
  return l;
}

要使用它:

kstring_t *str = calloc(1, sizeof(kstring_t));
ksprintf(str, "%s, %d\n", aString, aInteger);
ksprintf(str, "a second line: %s\n", aString2);
free(str->s); free(s);

ksprintf() 不适用于 glibc 2.0,因为 vsnprintf 不返回字符串中的字节数。您需要反复将分配的内存加倍。 Linux 手册页“man snprintf”也给出了一个例子。在你的系统上,你应该检查 vsnprintf() 的行为,它是 C99,而不是 C90。

The problem with sprintf() is you do not know how many bytes you need to allocate before calling sprintf(). I usually use the following ksprintf() as a replacement.

typedef struct __kstring_t {
  size_t l, m;
  char *s;
} kstring_t;

int ksprintf(kstring_t *s, const char *fmt, ...)
{
  va_list ap;
  int l;
  va_start(ap, fmt);
  l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); // not working with glibc 2.0
  va_end(ap);
  if (l + 1 > s->m - s->l) {
    s->m = s->l + l + 2;
    kroundup32(s->m);
    s->s = (char*)realloc(s->s, s->m);
    va_start(ap, fmt);
    l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
  }
  va_end(ap);
  s->l += l;
  return l;
}

To use that:

kstring_t *str = calloc(1, sizeof(kstring_t));
ksprintf(str, "%s, %d\n", aString, aInteger);
ksprintf(str, "a second line: %s\n", aString2);
free(str->s); free(s);

ksprintf() does not work with glibc 2.0 because vsnprintf does not return the number of bytes in the string. You need to repeatedly double the memory in allocation. Linux man page "man snprintf" also gives an example. On your system, you should check the behavior of your vsnprintf(), which is C99, not C90 though.

小霸王臭丫头 2024-08-11 19:08:06

另一种方法是使用 lcd_write_string 作为函数指针并执行以下操作:

printf(lcd_write_string,"MyVar has value: %d",var)

如果 lcd_write_string 声明如下,则这在 CCS Pic C 编译器上可以正常工作

void lcd_write_string (char c);

Another way to do this would be to use lcd_write_string as a function pointer and do this:

printf(lcd_write_string,"MyVar has value: %d",var)

This works correctly on CCS Pic C compiler, if lcd_write_string is declared like

void lcd_write_string (char c);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文