⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd1602.c

📁 基于51单片机的用LCD1602液晶的人机界面代码。可以直接调用。基于keil开发平台
💻 C
字号:
/*
功能:lcd1602的驱动
  		提供的函数为:
				Delayms(uchar i)  延时ms数,单位ms
				Lcdclr(void) LCD清屏函数
				Lcdreset(void) LCD复位函数
				Lcdcur(uchar cur) 光标控制命令
				Lcdwrchar(uchar c,uchar xpos,uchar ypos) Lcd写字符函数
				Lcdwrstring(uchar *s,uchar xpos,uchar ypos) Lcd写字符串函数
作者:荒草
时间:2008年6月6日
修改:无
*/

//#include "spce061a.h"
#include "LCD1602.h"
#include <reg52.h>

#define DPORT P1


sbit RS   = P2^0;
sbit RW   = P2^2;
sbit E    = P2^1;

///功能:延时ms函数,单位是ms
//参数:延时的时间
//返回:无
void Delayms(unsigned char i)
{
	unsigned int j;
	for(;i>0;i--)
	{
		for(j=350;j>0;j--)
		{;}
	}		
}

//功能:检查LCD的状态是否为忙信号
//参数:无
//返回:若忙,则等待
void Waitfree(void)
{	
	unsigned char temp1;
	RS=0;
	RW=1;
	E=0;
	Delayms(5);
	E=1;
	DPORT=0xFF00;
	for(;;)
	{
		temp1=DPORT;
		temp1&=0x80;
		if(temp1==0)
		    break;	
	}
	E=0;
}

//功能:送控制字(不检测忙信号)
//参数:控制字代码:c
//返回:无
void Lcdwcmdn(unsigned char c)
{
	RS=0;
	RW=0;
	E=1;
	DPORT=c;
  	Delayms(3);
	E=0;
}

//功能:送控制字(检测忙信号)
//参数:控制字代码:C
//返回:无
void Lcdwcmd(unsigned char c)
{
	Waitfree();
	Lcdwcmdn(c);
}

//功能:复位LCD
//参数:无
//返回:无
void Lcdreset(void)
{
	Delayms(15);
	Lcdwcmd(0x38);
	Delayms(5);
	Lcdwcmd(0x08);
	Delayms(5);
	Lcdwcmd(0x01);
	Delayms(5);
	Lcdwcmd(0x06);
	Delayms(5);
	Lcdwcmd(0x0c);
	Delayms(5);
}

//功能:光标设置
//参数:光标类型0为关显示,关光标,且不闪烁
//       1为开显示,无光标,无闪烁
//		 2为开显示,开光标,但光标不闪烁
//		 3为开显示,开光标,开闪烁
//返回:无
void Lcdcur(unsigned char cur)
{
	Delayms(5);
	switch(cur)
	{
		case 0:
			{
				Lcdwcmd(0x08); 
				break;
			}
		case 1:
			{
				Lcdwcmd(0x0c);
				break;
			}
		case 2:
			{
				Lcdwcmd(0x0e);
				break;
			}
		case 3:
			{
				Lcdwcmd(0x0f);  
				break;
			}
		default:
			break;
	}
} 

//功能:清屏LCD
//参数:无
//返回:无
void Lcdclr(void)
{
	Lcdwcmd(0x01);
}

//功能:确定显示位置
//参数:光标位置x y
//返回:无
void Lcdpos(unsigned char xpos,unsigned char ypos)
{
	unsigned char temp;
	xpos &=0x0f;
	ypos &=0x01;
	if(ypos==0)
		temp=xpos;
	else temp=xpos+0x40;
	temp |=0x80;
	Lcdwcmd(temp);
}

//功能:送字符
//参数:待显示的字符
//返回:无
void Lcdwr(unsigned char c)
{
	Waitfree();
	RS=1;
	RW=0;
	E=1;
	DPORT=c;
	Delayms(3);
	E=0;
}

//功能:写字符
//参数:待显示的字符
//返回:无
void Lcdwrchar(unsigned char c,unsigned char xpos,unsigned char ypos)
{
 	Lcdpos(xpos,ypos);
	Lcdwr(c);
}

//功能:写字符串
//参数:字符串,位置
//返回:无
void Lcdwrstring(unsigned char *s,unsigned char xpos,unsigned char ypos)
{
	unsigned char i;
	if(*s==0) 
		return;
	for(i=0;;i++)
	{
		if(*(s+i)==0)
			break;
		Lcdwrchar(*(s+i),xpos,ypos);
		xpos++;
		if(xpos>=15) 
			break;
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -