LCD1602动态显示

仿真元原理图如下:
LCD1602动态显示_第1张图片

/*-------------------------------------
功能:LCD1602局部动态显示
描述:在屏幕的第二行固定位置显示count:
后面接着动态显示按键次数(00 ~ 99)
Author: Zhang Kaizhou
Date: 2019-6-5 10:50:22
-------------------------------------*/
#include 
#include 
#define uchar unsigned char
#define uint unsigned int
	
uchar code table0[] = {"Press time"}; // 每行的字符数据
uchar code table1[] = {"Count:00"};
uchar code table2[] = {"0123456789"};
sbit lcdrs = P1^2; // LCD数据/命令选择端
sbit lcdrw = P1^3; // LCD读写选择端
sbit lcden = P1^4; // LCD使能端,高脉冲有效
sbit k1 = P1^5;
uchar num, ge = 0, shi = 0;

void init();
void displayInit();
void keyScan();
void writeCommand(uchar command);
void delay(uchar xms);
void writeData(uchar dat);

void main()
{
	init();
	displayInit();
	while(1)
	{	
		keyScan();
	}
}

/*初始化LCD1602的设置*/
void init()
{
	lcden = 0; // 拉低使能端,准备产生使能高脉冲信号
	writeCommand(0x38); // 显示模式设置(16x2, 5x7点阵,8位数据接口)
	writeCommand(0x0c); // 开显示,不显示光标
	writeCommand(0x06); // 写一个字符后地址指针自动加1
	writeCommand(0x01); // 显示清零,数据指针清零
}

/*LCD上电界面*/
void displayInit()
{
	writeCommand(0x80); // 将数据指针定位到第一行首
	for(num = 0; num < strlen(table0); num++)
	{
		writeData(table0[num]);
		delay(5);
	}
	
	writeCommand(0x80 + 0x40); // 将数据指针定位到第二行首
	for(num = 0; num < strlen(table1); num++)
	{
		writeData(table1[num]);
		delay(5);
	}
}

/*按键扫描*/
void keyScan()
{
	writeCommand(0x80 + 0x40 + strlen(table1) - 0x01);
	if(!k1)
	{
		delay(5);
		if(!k1)
		{	
			while(!k1);
			ge++;
			if(ge == 10)
			{
				ge = 0;
				shi++;
				if(shi == 10)
				{
					shi = 0;
					displayInit();
				}else{
					writeCommand(0x80 + 0x40 + strlen(table1) - 0x02);
					writeData(table2[shi]);
					writeCommand(0x80 + 0x40 + strlen(table1) - 0x01);
					writeData(table2[ge]);
				}
			}else{
				writeData(table2[ge]);
			}
		}
	}
}

/*写指令函数*/
void writeCommand(uchar command)
{
	lcdrs = 0; // 命令选择
	lcdrw = 0;
	P0 = command;
	delay(5);
	
	lcden = 1; // 产生一个正脉冲使能信号
	delay(5);
	lcden = 0;
}

/*写数据函数*/
void writeData(uchar dat)
{
	lcdrs = 1; // 数据选择
	lcdrw = 0;
	P0 = dat;
	delay(5);
	
	lcden = 1;
	delay(5);
	lcden = 0;
}

/*延时函数*/
void delay(uchar xms)
{
	uint i, j;
	for(i = xms; i > 0; i--)
		for(j = 110; j > 0; j--);
}

你可能感兴趣的:(C51)