帮忙破译简写C
我正在尝试用为我们编写的固件来解决一些问题。 我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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...
What this does is:
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.
将变量“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.
他通过每 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.