简体   繁体   English

如何将2个8位寄存器数据存储到1个16位变量中?

[英]How do I Store 2 8bit Register data into 1 16bit varrible?

I am looking at taking the 10 bit data from my ADC conversion and storing it into 1 16 bit integer data looks like 0x03 ADRESH 0xFF ADRESL. 我正在考虑从ADC转换中获取10位数据并将其存储为1个16位整数数据,如0x03 ADRESH 0xFF ADRESL。 What I am doing right now is 我现在正在做的是

    data = 0x03 & ADRESH;
    data = data << 8;
    data = data & 0x03FF & ADRESL;

will this work how I think it should or am I missing something? 这项工作会以我认为应该的方式进行还是我会丢失一些东西? thanks for the help 谢谢您的帮助

Why don't you use the | 你为什么不用| operator ? 操作员? short data = ((0x03 & ADRESH) << 8) | ADRESL; should work fine. 应该工作正常。

Your code will not work 你的代码不起作用

data = data & 0x03FF & ADRESL;

Should be closer to 应该更接近

data = data | ADRESL;
or 
data |= ADRESL;

It is good that you performed the 8 byte shift in your 16-bit data . 在16位data执行8字节移位是很好的。

Note: the & 0x03FF is not needed. 注意:不需要& 0x03FF
Note: Insure the data type of data is at least 16 bits. 注意:确保的数据类型data是至少16位。
Note: If you continue to have issues, insure the 10 bit alignment is as you think. 注意:如果您仍然遇到问题,请确保10位对齐符合您的想法。 Many A/D modules allow the 10-bit data to be in the upper 10 bits rather than the lower. 许多A / D模块允许10位数据位于高10位而不是低位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM