帮忙破译简写C

发布于 2024-08-01 16:07:05 字数 528 浏览 3 评论 0原文

我正在尝试用为我们编写的固件来解决一些问题。 我对 C 不太熟悉,我认为这里有一些我不明白的简写。 我不明白代码与注释有何关系,特别是如何从中获得 70 毫秒。 你能帮忙翻译成英文吗?

// so the button has to be held for 70 ms to be considered being pressed
// and then has to be released for 70ms to be considered un-pressed
State=(State<<1) | !input(USER_BUTTON) | 0xe000;
if(State==0xe000)
{
    Debounced_Button_Pressed =  TRUE;
    time_button_held++;
}
else if (State==0xffff)
{
    Debounced_Button_Pressed =  FALSE;
}

这是一个定时器中断函数,显然每 4.4 毫秒触发一次,

谢谢。

I'm trying to figure out some things with some firmware that was written for us. I'm not all that familiar with C and I think there's some shorthand going on here that I'm just not getting. I don't understand how the code relates to the comments, particularly how you get 70ms from any of that. Can you help translate into English?

// so the button has to be held for 70 ms to be considered being pressed
// and then has to be released for 70ms to be considered un-pressed
State=(State<<1) | !input(USER_BUTTON) | 0xe000;
if(State==0xe000)
{
    Debounced_Button_Pressed =  TRUE;
    time_button_held++;
}
else if (State==0xffff)
{
    Debounced_Button_Pressed =  FALSE;
}

This is within a timer interrupt function and apparently fires every 4.4ms

Thanks.

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

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

发布评论

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

评论(3

梦年海沫深 2024-08-08 16:07:05

让我们一次一步......

State=(State<<1) | !input(USER_BUTTON) | 0xe000;

它的作用是:

  • 将状态一向左移动(扔掉最高位,移动所有内容,将低位设置为 0)
  • 如果输入为 0,则设置低位(关闭)
  • 强制打开前 3 位。

因此,这里有 13 位不是强制启用的,它们形成了 USER_BUTTON 输入的最后 13 个样本的某种历史记录。

然后 if 语句仅检查所有 13 个位是否全部关闭(给出 0xe000)或打开(给出 0xffff)。 如果关闭,则按钮已被按下 13 个样本; 如果打开,则表示已未按下 13 个样本。

这样得到的去抖时间为 4.4ms * 13 = 57.2ms - 与注释有点偏离,或者计时器间隔更接近 5.385ms。

Let's take this one step at a time...

State=(State<<1) | !input(USER_BUTTON) | 0xe000;

What this does is:

  • Shift state one to the left (throw out the top bit, move everything over, set the low bit to 0)
  • Set the low bit if the input is 0 (off)
  • Force the top 3 bits on.

So, there are 13 bits here that are not forced on, and they form a sort of history of the last 13 samples of the USER_BUTTON input.

The if statement then just checks whether all 13 of those bits are off (giving 0xe000) or on (giving 0xffff). If off, the button's been pressed for 13 samples; if on, it's been un-pressed for 13 samples.

This then gives a debounce time of 4.4ms * 13 = 57.2ms - a bit off from the comment, or the timer interval's closer to 5.385ms.

独木成林 2024-08-08 16:07:05

将变量“State”视为 16 位。 << 运算符每次将其左移一位,而 | 当 input(USER_BUTTON) 为 false 时(! 是非运算符),运算符用于设置最低有效位。 然后检查仅检查最后 13 个输入情况是全真还是全假。

Think of the varialbe "State" as 16 bits. The << operator shifts it left by one each time, and the | operator is used to set the least significant bit whenever input(USER_BUTTON) is false (! is the not operator). The checks then just check if the last 13 cases of input were all true or all false.

说谎友 2024-08-08 16:07:05

他通过每 4.4 毫秒将开关状态的样本转换为一个整数来消除开关的抖动。 然后,他可以通过查看该整数的内容是否与某个十六进制值匹配来区分有效的新闻和噪音。 看起来他也可以通过与不同的值进行比较来判断是否已经按照他的定义发布了。

He's debouncing a switch by shifting samples of the switch's state into an integer every 4.4ms. He can then tell a valid press from noise by seeing if the contents of that integer match a certain hex value. It looks like he can also tell if it has been released according to his definition by comparing it to a different value.

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