如何在LCD显示屏中选择行列
问题描述:
我想在特定行列中显示字母16x2液晶屏显示屏8051
MCU。例如:如何在LCD显示屏中选择行列
Display "R" at 2nd column in first row
Display "W" at 3rd column in second row
我使用这些例程用于LCD:
#include<reg51.h>
/* Data pins connected to port P1 of 8051 */
#define Data_Port_Pins (P1)
sbit Register_Select_Pin = P2^0; /* Register Pin of LCD connected to Pin 0 of Port P2 */
sbit Read_Write_Pin = P2^1; /* Read/Write Pin of LCD connected to Pin 1 of Port P2 */
sbit Enable_Pin = P2^2; /* EN pin connected to pin 2 of port P2 */
/* Function for creating delay in milliseconds */
void Delay(unsigned int wait)
{
volatile unsigned i, j;
for(i = 0; i < wait; i++)
for(j = 0; j < 1200; j++);
}
/* Function to send command instruction to LCD */
void LCD_Command (unsigned char command)
{
Data_Port_Pins = command;
Register_Select_Pin =0;
Read_Write_Pin=0;
Enable_Pin =1;
Delay (2);
Enable_Pin =0;
}
/* Function to send display data to LCD */
void LCD_Data (unsigned char Data)
{
Data_Port_Pins = Data;
Register_Select_Pin=1;
Read_Write_Pin=0;
Enable_Pin =1;
Delay(2);
Enable_Pin =0;
}
/* Function to prepare the LCD and get it ready */
void LCD_Initialization()
{
LCD_Command (0x38);
LCD_Command (0x0e);
LCD_Command (0x01);
LCD_Command (0x81);
}
这是我的尝试:
这有什么意义吗?
void LCD_Position(char row, char column)
{
unsigned char cmd = 0x80 ; /* Start address */
if(row != 0) /*If second row selected ...*/
{
cmd += 0x40 ; /*add start address of second row */
}
cmd += row & 0x0f ;
LCD_Command (cmd);
}
答
请参阅相关LCD设备的数据表。对于常用的1602型号模块(初始化顺序显示的是您所使用的),您可以使用设置DDRAM地址指令来设置下一个数据写入的位置。
在2行显示模式下,第1行从地址0x00开始,第2行从0x40开始。
void LCD_Position(int row, int pos)
{
LCD_Command(0x80 | // Set DDRAM Address
(row == 0) ? 0x00 : 0x40 | // Row selector
(pos & 0x0f)) ; // Position in row
}
鉴于(从数据片):
代码设置指示集DDRAM编辑部地址指令DB7为1(0x80的)。其他位是地址位,但是显示RAM中的位置比显示器的宽度更多,所以只有0x00到0x0f和0x40到0x4f是指可见的显示位置。因此,如果选择第二行,则在((row == 0) ? 0x00 : 0x40
)中屏蔽0x40,然后在((pos & 0x0f)
)中屏蔽字符位置。虽然我已经使用逐位操作,所述表达可以同样进行算术执行:
0x80 + (row == 0) ? 0x00 : 0x40 + (pos & 0x0f)
在两种情况下& 0x0f
确保命令不被修改,并且该字符被放置在显示器上,即使如果从位置-of范围。
较少简洁,但也许更容易理解:
// Set DDRAM Address command - start of row 0
unsigned char cmd = 0x80 ;
// If second row selected ...
if(row != 0)
{
// ... add start address of second row
cmd += 0x40 ;
}
// Add row offset. Masked to protect other
// bits from change if row is out of range.
cmd += row & 0x0f ;
// Write command
LCD_Command(cmd) ;
你尝试过什么?目前看起来你只是贴上了你所得到的问题。另外,如果不知道你连接的LCD是什么,这里没有人可以帮助你。 – Ross
我可以在LCD上编写消息显示程序,我已经写好了。并用模拟器测试它的工作正常。在那之后,我尝试编写程序来在特定的行和列上显示字母..但我不知道如何在特定的行和列上显示字母我使用16 * 2 LCD – Parth786
在'Delay()'中,声明'i'和'j'' volatile'('volatile unsigned i,j;') – Clifford