lcd1602显现程序代码
前些天弄了最小体系板后就想着学习1602的显现程序,惋惜坛子里的或网上的,都没有简略的1602显现程序,无柰在网上下载了一段通过重复修正测验,总算有了下面一段代码:
// – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
// – – 初始化接口
# define LCD_DB P0 // – – P0 = DB0~DB7
sbit LCD_RS=P2^0; // – – p2.0 = RS
sbit LCD_RW=P2^1; // – – p2.1 = RW
sbit LCD_E=P2^2; // – – p2.2 = E
// – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
// – – 界说函数
# define uchar unsigned char
# define uint unsigned int
// – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
// – – 界说子程序函数
void LCD_init(void); // – – 初始化LCD1602函数
void LCD_write_command(uchar command); // – – 向LCD1602写指令函数
void LCD_write_data(uchar dat); // – – 向LCD1602写数据函数
void LCD_set_xy(uchar x,uchar y); // – – 设置LCD1602显现方位 X(0-16),y(1-2)
void LCD_disp_char(uchar x,uchar y,uchar dat); // – – 在LCD1602上显现一个字符
void LCD_disp_string(uchar X,uchar Y,uchar *s); // – – 在LCD1602上显现一个字符串
//void LCD_check_busy(void);//查看忙函数。我没用到此函数,由于通过率极低。
void LCD_delay_10us(uint n); // – – 10微秒的延时子程序
void LCD_delay_50us(uint n); // – – 50微秒的延时子程序
// – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
// – – 初始化LCD1602
void LCD_init(void)
{
LCD_delay_10us(20);
LCD_write_command(0x38); // – – 设置8位格局,2行,5×7
LCD_delay_10us(5);
LCD_write_command(0x0c); // – – 全体显现,关光标,不闪耀
LCD_delay_10us(5);
LCD_write_command(0x06); // – – 设定输入办法,增量不移位
LCD_delay_10us(5);
LCD_write_command(0x01); // – – 铲除屏幕显现
LCD_delay_50us(40);
}
//********************************
// – – 向LCD1602写指令
void LCD_write_command(uchar dat)
{
LCD_delay_10us(5);
LCD_RS=0; // – – 指令
LCD_RW=0; // – – 写入
LCD_DB=dat;
LCD_delay_10us(5);
LCD_E=1; // – – 答应
LCD_delay_10us(5);
LCD_E=0;
}
// – – 向LCD1602写数据
void LCD_write_data(uchar dat)
{
LCD_delay_10us(5);
LCD_RS=1;// – – 数据
LCD_RW=0;// – – 写入
LCD_DB=dat;
LCD_delay_10us(5);
LCD_E=1;// – – 答应
LCD_delay_10us(5);
LCD_E=0;
}
// – – 设置显现方位
void LCD_set_xy(uchar x,uchar y)
{
uchar address;
if(y==1)
{
address=0x80+x; // – – 榜首行方位
} else {
address=0xc0+x; // – – 第二行方位
}
LCD_delay_10us(5);
LCD_write_command(address);
}
// – – 显现一个字符函数
void LCD_disp_char(uchar x,uchar y,uchar dat) // – – LCD_disp_char(0,1,0x38); // – – 显现8
{
LCD_set_xy(x,y);
LCD_delay_10us(5);
LCD_write_data(dat);
}
// – – 显现一个字符串函数
void LCD_disp_string(uchar x,uchar y,uchar *s)
{
LCD_set_xy(x,y);
LCD_delay_10us(5);
while(*s!=‘\0’)
{
LCD_write_data(*s);
s++;
}
}
//********************************
/*******查看忙函数*************
void LCD_check_busy() //实践证明,在我的LCD1602上,查看忙指令通过率极低,以
{ //至于不能正常运用LCD。因而我没有再用查看忙函数。而使
do //用了延时的办法,延时仍是十分好用的。我试了一下,用
{ LCD_E=0; //for循环作延时,一般指令只需1次循就可完结。清屏指令
LCD_RS=0; //要用200次循环便能完结。
LCD_RW=1;
LCD_DB=0xff;
LCD_E=1;
}while(LCD_DB^7==1);
}
******************************/
void LCD_delay_10us(uint n) // – – 10微秒的延时子程序
{
uint i,j;
for(i=n;i》0;i–)
for(j=2;j》0;j–);
}
void LCD_delay_50us(uint n) // – – 50微秒的延时子程序
{
uint i,j;
for(i=n;i》0;i–)
for(j=22;j》0;j–);
}
LCD1602显现程序流程图及句解
让LCD1602显现字符前要输入字符地点的地址,这也是每显现一个字符地址要加一的原因,鄙人面的一段代码里你会理解怎样设置地址,怎样让地址主动加一,怎样清屏,怎样显现字符,期望你能看懂哦
sbit LCD_E=P2^0; //lcd1602操控置位
sbit LCD_RW=P2^1; //lcd1602操控置位
sbit LCD_RS=P2^2; //lcd1602操控置位
void LCD_command(unsigned char com)
{
LCD_E=0;
LCD_RW=0;
LCD_RS=0;
P1=com;
delay_us(100);
LCD_E=1;
delay_us(100);
LCD_E=0;
}
void LCD_date(unsigned char dat)
{
LCD_E=0;
LCD_RW=0;
LCD_RS=1;
P1=dat;
delay_us(100);
LCD_E=1;
delay_us(100);
LCD_E=0;
}
LCD_command(0x38); //设置LCD为16*2显现,5*7点阵,8位数据接口
LCD_command(0x0c); //设置LCD显现开,光标显现关
LCD_command(0x06); //写一个数据后地址加1
LCD_command(0x01); //显现清屏
LCD_command(0x80); //数据指针设置为榜首行榜首位
LCD_date(‘pi’);
LCD_date(‘l’);
LCD_date(‘e’);
LCD_date(‘a’);
LCD_date(‘s’);
LCD_date(‘e’);
LCD_date(‘ ’);
LCD_date(‘c’);
LCD_date(‘h’);
LCD_date(‘e’);
LCD_date(‘c’);
LCD_date(‘k’);
LCD_date(‘ ’);
LCD_date(‘t’);
LCD_date(‘h’);
LCD_date(‘e’);
LCD_command(0x80+0x40); //LCD地址指针指向第二行榜首个方位
LCD_date(‘c’);
LCD_date(‘o’);
LCD_date(‘n’);
LCD_date(‘n’);
LCD_date(‘e’);
LCD_date(‘c’);
LCD_date(‘t’);
LCD_date(‘i’);
LCD_date(‘o’);
LCD_date(‘n’);
LCD_date(‘!’);
LCD_date(‘!’);