从uint8_t删除/移动n未使用的位

发布于 2025-01-27 10:49:12 字数 739 浏览 2 评论 0原文

我正在从实时计数器中读取几秒钟作为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.

PCF seconds bit registers

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 技术交流群。

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

发布评论

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

评论(1

沫尐诺 2025-02-03 10:49:12

多亏了 @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 use 0x3f/00111111, etc. The code remains the same, but the masks change.

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