【问题标题】:PIC Bit Masking and Shifting for 4 Bit LCD Control用于 4 位 LCD 控制的 PIC 位掩码和移位
【发布时间】:2013-01-27 06:08:55
【问题描述】:

我有一个关于屏蔽和位移的问题。

我有以下代码:

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
}

“字”为 8 位,通过 4 位 LCD 接口输入。这意味着我必须在发送数据之前将最高有效位和最低有效位分开。

LCD_D 是一个 16 位数字,其中只有我传递给它的最高有效位我想要真正“做”一些事情。我希望保留之前的 12 位,以防他们在做其他事情。

就位掩码和“字”移位而言,我的逻辑在将上下半字节适当地传递给 LCD_D 方面是否正确?

感谢您的帮助!

【问题讨论】:

    标签: bit-manipulation pic masking bit-shift lcd


    【解决方案1】:

    在这两种情况下,除了需要在移位之前将“word”转换为无符号短(16 位)之外,看起来还可以,这样移位就不会在 char 上执行并丢失数据。例如:

    LCD_D = (LCD & 0x0FFF) | ((unsigned short) word << 8);
    

    【讨论】:

    • 感谢您的帮助!演员做到了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多