由于您想要一种非常精确定义的位控制方式,因此最安全的方法是使用 read-modify-write。
volatile uint32_t *reg_address;
uint32_t data;
data = *reg_address;
data |= (1<<LCD_RS_BIT_POS); // Set bit
// data &= ~(1<<LCD_RS_BIT_POS); // Clear bit
*reg_address = data;
另一种方法是将位设置/位清除地址位置添加到 LCD 端口寄存器映射(这是 Verilog 代码):
if (cpu_select && cpu_write)
case (cpu_address[4:2])
3'h0 : lcd_reg <= cpu_wdata[7:0]; // Direct write
3'h1 : lcd_reg <= lcd_reg | cpu_wdata[7:0]; // set bit(s) write
3'h2 : lcd_reg <= lcd_reg & ~cpu_wdata[7:0]; // Clear bit(s) write
....
endcase
您要求设置更多位。
一种基本方法是清除所有要更改的位,然后使用所需值再次设置它们。
例如:您有一个 4 位值(位 4、5、6、7)一个 R/W 位 (2) 加上一个地址位 (0)。你把它们变成一个面具:0b11110101。
// Write to LCD
data = *reg_address;
data &= ~0xF5; // Clear all the bits
if (address==1)
data |= (data_nibble<<4) | (1<<LCD_WRT_BIT_POS) |(1<<LCD_ADR_BIT_POS) ;
else // Address 0
data |= (data_nibble<<4) | (1<<LCD_WRT_BIT_POS);
*reg_address = data;
但是....
在您的情况下,您可能只需要单独控制“E”信号。而且你知道它应该从低开始。整个过程变得更简单:
uint32_t data;
// Set the data, R/Wnot and RS all at the same time
if (write_data)
data = nibble_data<<4; // R/Wn=0, RS=0. E=0
else // Write command
data = nibble_data<<4 | (1<<RS_BIT_POS); // R/Wn=0, RS=1, E=0
*reg_address = data;
// Pulse E high and low:
*reg_address = data | (1<<E_BIT_POS); // E high
some_kind_of_wait_routine(); // Wait a while
*reg_address = data; // E low
some_kind_of_wait_routine(); // Wait a while to guarantee minimum low time!
对于读取,您不需要设置数据,但您必须将读取线设置为高电平。然而,液晶显示器可以在不读取的情况下进行操作。