从uint8_t删除/移动n未使用的位
我正在从实时计数器中读取几秒钟作为BCD数据,第7位未使用。
在线使用类似的样本,我能够将uint8_t
bcd数据转换为人类可读(0-59)秒。
#define PCF8563_BCD_LOWER_MASK 0x0f
#define PCF8563_BCD_UPPER_MASK_SEC 0x70
#define PCF8563_BCD_UPPER_SHIFT 4
uint8_t raw_seconds = get_raw_seconds();
int seconds = (raw_seconds & PCF8563_BCD_LOWER_MASK) + (((raw_seconds & PCF8563_BCD_UPPER_MASK_SEC) >> PCF8563_BCD_UPPER_SHIFT) * 10);
我想在几分钟,小时等上做同样的事情(它们都采用BCD格式)。我觉得我知道该怎么办。移动/删除第7位(持续几分钟),然后将BCD转换为十进制 - 但我不知道如何在代码中执行此操作。
从BCD转换为十进制并不是问题所在(这有一个功能),未使用的碎屑的转移/删除使我失望了。
I am reading seconds from a real time counter as BCD data, the 7th bit isn't used for this.
Using similar samples online I was able to convert the uint8_t
BCD data to human readable (0 - 59) seconds.
#define PCF8563_BCD_LOWER_MASK 0x0f
#define PCF8563_BCD_UPPER_MASK_SEC 0x70
#define PCF8563_BCD_UPPER_SHIFT 4
uint8_t raw_seconds = get_raw_seconds();
int seconds = (raw_seconds & PCF8563_BCD_LOWER_MASK) + (((raw_seconds & PCF8563_BCD_UPPER_MASK_SEC) >> PCF8563_BCD_UPPER_SHIFT) * 10);
I'd like to do the same for minutes, hours, etc, (they're all also in BCD format). I feel like I know what to do; shift/remove the 7th bit (for minutes) and convert BCD to Decimal - but I can't figure how to do this in code.
Converting from BCD to decimal isn't exactly the problem (there's a function for that), the shifting/removing of unused bits is throwing me off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多亏了 @mkrieger1的评论,我研究了一些蒙版,解决方案非常简单。
在几个小时和几分钟内,位掩码
0x7f
/01111111
仅用于保留前7位,而如果只需6个,则只需使用0x3f /
00111111
等。代码保持不变,但是掩码会更改。Thanks to @mkrieger1 's comment I looked into bit masking and the solution was very simple.
For hours and minutes, the bit mask
0x7f
/01111111
is used to only keep the first 7 bits, while if you want only 6 you simply use0x3f
/00111111
, etc. The code remains the same, but the masks change.