我想在{strong> 16x2 LCD 显示屏上显示特定行和列的字母8051
MCU 。例如:
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);
}
答案 0 :(得分: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
}
鉴于(来自数据表):
代码将DB7设置为1(0x80),表示设置DDRAM地址指令。其他位是地址位,但显示RAM中的位置多于显示宽度,因此只有0x00到0x0f和0x40到0x4f指的是可见的显示位置。因此,如果选择第二行,则在((row == 0) ? 0x00 : 0x40
)中屏蔽0x40,然后在((pos & 0x0f)
)中屏蔽字符位置。虽然我使用了逐位操作,但表达式同样可以算术地执行:
0x80 + (row == 0) ? 0x00 : 0x40 + (pos & 0x0f)
在这两种情况下,& 0x0f
都可以确保命令不会被修改,即使位置超出范围,也会将字符放在显示屏上。
不太简洁,但也许更容易理解:
// 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 ) ;