C 将 char 读取为二进制

发布于 2024-09-17 19:06:52 字数 395 浏览 4 评论 0原文

这实际上是我正在使用 avr 进行的项目的一部分。我通过 twi 与 DS1307 实时时钟 IC 连接。它将信息报告为一系列 8 个字符。它以以下格式返回:

// Second : ds1307[0]
// Minute : ds1307[1]
// Hour   : ds1307[2]
// Day    : ds1307[3]
// Date   : ds1307[4]
// Month  : ds1307[5]
// Year   : ds1307[6]

我想做的就是利用每一部分时间一点一点地阅读它。我想不出办法来做到这一点。基本上,如果该位是 1,则点亮 LED,但如果是 0,则不会点亮。

我想有一种相当简单的方法可以通过位移来实现这一点,但我无法具体说明执行此操作的逻辑。

This is actually part of a project I'm working on using an avr. I'm interfacing via twi with a DS1307 real-time clock IC. It reports information back as a series of 8 chars. It returns in the format:

// Second : ds1307[0]
// Minute : ds1307[1]
// Hour   : ds1307[2]
// Day    : ds1307[3]
// Date   : ds1307[4]
// Month  : ds1307[5]
// Year   : ds1307[6]

What I would like to do is take each part of the time and read it bit by bit. I can't think of a way to do this. Basically lighting up an led if the bit is a 1, but not if it's a 0.

I'd imagine that there is a rather simple way to do it by bitshifting, but I can't put my finger on the logic to do it.

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

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

发布评论

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

评论(4

风向决定发型 2024-09-24 19:06:52

检查位 N 是否已设置可以使用简单的表达式来完成,例如:

(bitmap & (0x1 << N)) != 0

其中位图是包含位的整数值(例如,在您的情况下为 64 位)。

求秒:

(bitmap & 0xFF)

求分钟:

(bitmap & 0xFF00) >> 8

求小时:

(bitmap & 0xFF0000) >> 16

Checking whether the bit N is set can be done with a simple expression like:

(bitmap & (0x1 << N)) != 0

where bitmap is the integer value (e.g. 64 bit in your case) containing the bits.

Finding the seconds:

(bitmap & 0xFF)

Finding the minute:

(bitmap & 0xFF00) >> 8

Finding the hour:

(bitmap & 0xFF0000) >> 16
尬尬 2024-09-24 19:06:52

如果我的解释正确,以下内容将从最低到最高迭代所有位。即,秒的 8 位,后面是分钟的 8 位,依此类推。

unsigned char i, j;
for (i = 0; i < sizeof(ds1307); i++)
{
  unsigned char value = ds1307[i];  // seconds, minutes, hours etc
  for (j = 0; j < 8; j++)
  {
    if (value & 0x01)
    {
      // bit is 1
    }
    else
    {
      // bit is 0
    }
    value >>= 1;
  }
}

If I'm interpreting you correctly, the following iterates over all the bits from lowest to highest. That is, the 8 bits of Seconds, followed by the 8 bits of Minutes, etc.

unsigned char i, j;
for (i = 0; i < sizeof(ds1307); i++)
{
  unsigned char value = ds1307[i];  // seconds, minutes, hours etc
  for (j = 0; j < 8; j++)
  {
    if (value & 0x01)
    {
      // bit is 1
    }
    else
    {
      // bit is 0
    }
    value >>= 1;
  }
}
辞取 2024-09-24 19:06:52

是的 - 您可以使用 >> 将位右移一位,而 & 1 获取最低有效位的值:(

unsigned char ds1307[7];
int i, j;

for (i = 0; i < 7; i++)
    for (j = 0; j < 8; j++)
        printf("byte %d, bit %d = %u\n", i, j, (ds1307[i] >> j) & 1U);

这将检查从最低有效位到最高有效位的位。顺便说一句,您的示例数组只有 7 个字节,而不是 8 个...)

Yes - you can use >> to shift the bits right by one, and & 1 to obtain the value of the least significant bit:

unsigned char ds1307[7];
int i, j;

for (i = 0; i < 7; i++)
    for (j = 0; j < 8; j++)
        printf("byte %d, bit %d = %u\n", i, j, (ds1307[i] >> j) & 1U);

(This will examine the bits from least to most significant. By the way, your example array only has 7 bytes, not 8...)

花间憩 2024-09-24 19:06:52

本质上,如果以二进制格式显示秒数的 6 个 LED 连接到 PORTA2-PORTA7,您可以PORTA = ds1307[0] 让秒数自动正确点亮。

essentially, if the 6 LEDs to show the seconds in binary format are connected to PORTA2-PORTA7, you can PORTA = ds1307[0] to have the seconds automatically lit up correctly.

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