2017-09-15 185 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

相關問題