1.查看原理图
图1 LCD1602接口
图2 LCD1602与STC的接口
2.编写代码
图3 时序结构
根据时序结构编写命令和写入数据代码
#include <REGX52.H>
#include "Delay.h"
sbit LCD1602_EN=P2^7;
sbit LCD1602_RS=P2^6;
sbit LCD1602_WR=P2^5;
#define LCD1602_lCD0 P0 //因为是并口通信,一个就行
void LCD1602_WriteCommand(unsigned char command)
{
LCD1602_RS=0; //RS为低电平时表示发送指令
LCD1602_WR=0; //表示开始写,写命令和写数据都是WR=0
LCD1602_lCD0=command;
LCD1602_EN=1;
Delay();
LCD1602_EN=0;
Delay();
}
void LCD1602_WriteData(unsigned char Data)
{
LCD1602_RS=1; //RS为高电平时表示发送的是数据
LCD1602_WR=0; //表示开始写
LCD1602_lCD0=Data;
LCD1602_EN=1;
Delay();
LCD1602_EN=0;
Delay();
}
根据操作流程进行初始化和显示数据代码函数的编写
void LCD1602_Init()
{
LCD1602_WriteCommand(0x38);
LCD1602_WriteCommand(0x0C);
LCD1602_WriteCommand(0x06);
LCD1602_WriteCommand(0x01);
}
void LCD1602_showChar(unsigned char line,unsigned char column,unsigned char C)
{
if(line==1)
{
LCD1602_WriteCommand(0x80|column-1);
}
else
{
LCD1602_WriteCommand(0x80|(column-1)+0x40);
}
LCD1602_WriteData(C);
}
LCD1602.C代码汇总
#include <REGX52.H>
#include "Delay.h"
sbit LCD1602_EN=P2^7;
sbit LCD1602_RS=P2^6;
sbit LCD1602_WR=P2^5;
#define LCD1602_lCD0 P0 //因为是并口通信,一个就行
void LCD1602_WriteCommand(unsigned char command)
{
LCD1602_RS=0; //RS为低电平时表示发送指令
LCD1602_WR=0; //表示开始写,写命令和写数据都是WR=0
LCD1602_lCD0=command;
LCD1602_EN=1;
Delay();
LCD1602_EN=0;
Delay();
}
void LCD1602_WriteData(unsigned char Data)
{
LCD1602_RS=1; //RS为高电平时表示发送的是数据
LCD1602_WR=0; //表示开始写
LCD1602_lCD0=Data;
LCD1602_EN=1;
Delay();
LCD1602_EN=0;
Delay();
}
void LCD1602_Init()
{
LCD1602_WriteCommand(0x38);
LCD1602_WriteCommand(0x0C);
LCD1602_WriteCommand(0x06);
LCD1602_WriteCommand(0x01);
}
第1行和第二行要注意按图修改
void LCD1602_showChar(unsigned char line,unsigned char column,unsigned char C)
{
if(line==1)
{
LCD1602_WriteCommand(0x80|column-1);
}
else
{
LCD1602_WriteCommand(0x80|(column-1)+0x40);
}
LCD1602_WriteData(C);
}
void LCD_ShowString(unsigned char line,unsigned char Column,char *String)
{
unsigned char i;
if(line==1)
{
LCD1602_WriteCommand(0x80|Column-1);
}
else
{
LCD1602_WriteCommand(0x80|(Column-1)+0x40);
}
for(i=0;String[i];i++)
LCD1602_WriteData(String[i]);
}
void LCD_ShowNum(unsigned char line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i=0;
if(line==1)
{
LCD1602_WriteCommand(0x80|Column-1);
}
else
{
LCD1602_WriteCommand(0x80|(Column-1)+0x40);
}
while(i<Length)
{
if(Number>10000)
{
LCD1602_WriteData('0'+(unsigned char)(Number/10000));
Number=Number%10000;
}
else if(Number>1000)
{
LCD1602_WriteData('0'+(unsigned char)(Number/1000));
Number=Number%1000;
}
else if(Number>100)
{
LCD1602_WriteData('0'+(unsigned char)(Number/100));
Number=Number%100;
}
else if(Number>10)
{
LCD1602_WriteData('0'+(unsigned char)(Number/10));
Number=Number%10;
}
else
{
LCD1602_WriteData('0'+(unsigned char)(Number%10));
break;
}
i++;
}
}
void LCD_ShowSignedNum(unsigned char line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i=0;
if(line==1)
{
LCD1602_WriteCommand(0x80|Column-1);
}
else
{
LCD1602_WriteCommand(0x80|(Column-1)+0x40);
}
if(Number<0)
{
LCD1602_showChar(line,Column,'-');
Number=-Number;
}
else
{
LCD1602_showChar(line,Column,'+');
}
while(i<Length)
{
if(Number>10000)
{
LCD1602_WriteData('0'+(unsigned char)(Number/10000));
Number=Number%10000;
}
else if(Number>1000)
{
LCD1602_WriteData('0'+(unsigned char)(Number/1000));
Number=Number%1000;
}
else if(Number>100)
{
LCD1602_WriteData('0'+(unsigned char)(Number/100));
Number=Number%100;
}
else if(Number>10)
{
LCD1602_WriteData('0'+(unsigned char)(Number/10));
Number=Number%10;
}
else
{
LCD1602_WriteData('0'+(unsigned char)(Number%10));
break;
}
i++;
}
}
void LCD_ShowBinNum(unsigned char line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char Binarr[16]={'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
unsigned char i=0;
if(line==1)
{
LCD1602_WriteCommand(0x80|Column-1);
}
else
{
LCD1602_WriteCommand(0x80|(Column-1)+0x40);
}
while(Number)
{
Binarr[i]='0'+(unsigned char)Number%2;
Number=Number/2;
i++;
}
for(i=Length;i>0;i--)
{
LCD1602_WriteData(Binarr[i-1]);
}
}
void LCD_ShowHexNum(unsigned char line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char Hexnum[4]={'0','0','0','0'};
unsigned char temp,i;
if(line==1)
{
LCD1602_WriteCommand(0x80|Column-1);
}
else
{
LCD1602_WriteCommand(0x80|(Column-1)+0x40);
}
while(Number)
{
temp=Number%16;
if(temp<10)
{
Hexnum[i]='0'+temp;
}
else
{
Hexnum[i]='A'+(temp-10);
}
Number=Number/16;
i++;
}
for(i=Length;i>0;i--)
{
LCD1602_WriteData(Hexnum[i-1]);
}
}
LCD1602.h代码展示
#ifndef __LCD1602_H__
#define __LCD1602_H__
void LCD1602_WriteCommand(unsigned char command);
void LCD1602_Init();
void LCD1602_showChar(unsigned char line,unsigned char column,unsigned char C);
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
#endif
3.编写main.c代码
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
void main()
{
LCD1602_Init();
LCD1602_showChar(1,1,'+');
LCD_ShowString(1,3,"Hello");
LCD_ShowNum(1,9,212,4);
LCD_ShowSignedNum(2,1,-212,4);
LCD_ShowBinNum(1,13,32,16);
LCD_ShowHexNum(2,5,15,2);
while(1)
{
unsigned char i,j,k;
LCD1602_WriteCommand(0x18); //移动屏幕命令0x18
i = 10 ;
j = 11;
k = 45;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
}
4.结果展示
LCD1602滚动显示