18_LCD1602
LCD1602
介绍
引脚及应用电路
原理图
内部结构框图
存储器结构
时序结构
LCD1602指令集
LCD1602操作流程
字符,字符串
编写LCD1602代码
LCD1602.c
#include <REGX52.H>
sbit LCD_RS=P2^6; //1数据0指令
sbit LCD_RW=P2^5; //1读0写
sbit LCD_E=P2^7; //1数据有效,下降沿执行
#define LCD_DataPort P0
void LCD_Delay1ms(void) //@12.000MHz
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_E=1;
LCD_Delay1ms();
LCD_E=0;
LCD_Delay1ms();
}
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_E=1;
LCD_Delay1ms();
LCD_E=0;
LCD_Delay1ms();
}
void LCD_Init()
{
LCD_WriteCommand(0x38); //八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0C); //显示开,光标关,闪烁关
LCD_WriteCommand(0x06); //数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01); //清屏
}
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1)); //设置光标位置
}
else
{
LCD_WriteCommand(0x80|(Column-1)+0x40);
}
}
void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
int pow(int X,int Y)
{
int Res=1;
while(Y--)
{
Res*=X;
}
return Res;
}
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
LCD_SetCursor(Line,Column);
while(Length--)
{
LCD_WriteData(Number/pow(10,Length)%10+'0');
}
}
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
while(Length--)
{
LCD_WriteData(Number1/pow(10,Length)%10+'0');
}
}
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char SingleNumber;
LCD_SetCursor(Line,Column);
while(Length--)
{
SingleNumber=Number/pow(16,Length)%16;
if(SingleNumber>9)
{
LCD_WriteData(SingleNumber+'A'-10);
}
else
{
LCD_WriteData(SingleNumber+'0');
}
}
}
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char SingleNumber;
LCD_SetCursor(Line,Column);
while(Length--)
{
LCD_WriteData(Number/pow(2,Length)%2+'0');
}
}
LCD1602.h
#ifndef __LCD1602_H__
#define __LCD1602_H__
void LCD_WriteCommand(unsigned char Command);
void LCD_Init();
void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char);
void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned 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
main.c
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
void main()
{
LCD_Init();
LCD_ShowBinNum(1,1,0xA5,8);
LCD_ShowString(1,16,"Welcome to China!");
while(1)
{
Delay(200);
LCD_WriteCommand(0x18);
Delay(200);
}
}
运行效果
![VID_20231126_163649 (2)](H:\通讯软件\QQSave\1607442148\FileRecv\MobileFile\VID_20231126_163649 (2).gif)