質問

I have a question regarding both masking and bit shifting.

I have the following code:

void WriteLCD(unsigned char word, unsigned commandType, unsigned usDelay)
{
    // Most Significant Bits
    // Need to do bit masking for upper nibble, and shift left by 8.
    LCD_D = (LCD & 0x0FFF) | (word << 8);
    EnableLCD(commandType, usDelay); // Send Data

    // Least Significant Bits
    // Need to do bit masking for lower nibble, and shift left by 12.
    LCD_D = (LCD & 0x0FFF) | (word << 12);
    EnableLCD(commandType, usDelay); // Send Data
}

The "word" is 8 bits, and is being put through a 4 bit LCD interface. Meaning I have to break the most significant bits and least significant bits apart before I send the data.

LCD_D is a 16 bit number, in which only the most significant bits I pass to it I want to actually "do" something. I want the previous 12 bits preserved in case they were doing something else.

Is my logic in terms of bit masking and shifting the "word" correct in terms of passing the upper and lower nibbles appropriately to the LCD_D?

Thanks for the help!

役に立ちましたか?

解決

Looks ok apart from needing to cast "word" to an unsigned short (16 bit) before the shift, in both cases, so that the shift is not performed on a char and looses the data. eg:

LCD_D = (LCD & 0x0FFF) | ((unsigned short) word << 8);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top