2017-09-15 187 views
-2

我想在特定行列中显示字母16x2液晶屏显示屏8051MCU。例如:如何在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); 

    } 
+1

你尝试过什么?目前看起来你只是贴上了你所得到的问题。另外,如果不知道你连接的LCD是什么,这里没有人可以帮助你。 – Ross

+0

我可以在LCD上编写消息显示程序,我已经写好了。并用模拟器测试它的工作正常。在那之后,我尝试编写程序来在特定的行和列上显示字母..但我不知道如何在特定的行和列上显示字母我使用16 * 2 LCD – Parth786

+1

在'Delay()'中,声明'i'和'j'' volatile'('volatile unsigned i,j;') – Clifford

回答

0

请参阅相关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 
} 

鉴于(从数据片):

enter image description here

代码设置指示集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) ; 
+0

我知道地址0x00(命令0x80)将光标设置为第一行的开头,地址0x40(命令0xC0)将地址设置为第二行的开头。但我不明白你的功能如何工作.... – Parth786

+0

@ Parth786:那么首先它工作?我只是读数据表,并假设你现有的'LCD_Command()'工作(这反过来可能依赖于你修复'Delay()'函数);我没有测试过它。其次,我希望不必解释数据表中已经表达的东西,而对代码的理解实际上是一个不同的问题。如果有帮助,会更新答案。 – Clifford

+0

我没有看到发布代码的选项,所以我编辑了帖子。 – Parth786

相关问题