简体   繁体   中英

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. 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 .

Note: the & 0x03FF is not needed.
Note: Insure the data type of data is at least 16 bits.
Note: If you continue to have issues, insure the 10 bit alignment is as you think. Many A/D modules allow the 10-bit data to be in the upper 10 bits rather than the lower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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