📄 lcd1602.h
字号:
/**********************************************/
/**** LCD1602.H ***********/
/**** Written by WangBiao---20060308 *******/
/**********************************************/
#include <AT89X51.H>
#include<intrins.h>
//---------------------------------------------
#define busy 0x80 //用于检查写忙信号
//-------------------------------------------------------------------------------
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//-------------------------------------------------------------------------------
//------------1602 控制引脚定义-----------------
#define DATAPORT P1 // 数据端口
sbit LED =P3^4;
sbit RS =P3^7;
sbit RW =P3^6;
sbit Elcd=P3^5;
//-----------------外部函数--------------------------------------------------
void lcdinit(void);//LCD RESET
void cls(void);//LCD clear
void dispone(unsigned char x,unsigned char y,unsigned char Wdata);//显示一个字符
void dispstr(unsigned char x,unsigned char y,char *ptr);//显示一行字符串
void dispnumb(unsigned char x,unsigned char y,unsigned int n);//显示一组数字"1234"
void delay(unsigned int n);//delay 0.05ms*n
void blink(unsigned char t,unsigned char n);//delay 0.05ms*t,blink n times
//-------------------------------------------------------------------------------
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//------------------内部函数-----------------------------------------------------
void WaitForEnable( void ); //忙信号检测
void LocateXY( char posx,char posy);
void lcdwrite_c( unsigned char CMD,unsigned char AttribC );
void LcdWrite_D( char dataW ) ; //写一个数据
void delay5ms(void);
//---------------------------FOR LCD 1602------------------------------------
//-----------------初始化-------------------------------
void lcdinit(void)
{ lcdwrite_c(0x38,0);//显示模式设置(不测试忙信号)共三次
delay5ms();
lcdwrite_c(0x38,0);
delay5ms();
lcdwrite_c(0x38,0);
delay5ms();
lcdwrite_c(0x38,1);//0x38-8bit,0x28-4bit
lcdwrite_c(0x08,1);//close lcd
lcdwrite_c(0x01,1);//cls
lcdwrite_c(0x06,1);//0x06-cor->right,charN;0x04-cor->left,charN;0x07-charY
lcdwrite_c(0x0c,1);//0x0c-allY,corN,blnN;0x0e-allY,corY,blnN;0x0f-threeY
}
//-----------CLS----------------------------------------
void cls(void)
{
lcdwrite_c(0x01,1);
delay5ms();
}
/*=======================================================
按指定位置显示一个字符
=======================================================*/
void dispone(unsigned char x,unsigned char y,unsigned char Wdata)
{ LocateXY( x, y ); // 定位显示地址
LcdWrite_D( Wdata ); // 写字符
}
/*=======================================================
按指定位置显示字符串
=======================================================*/
void dispstr(unsigned char x,unsigned char y,char *ptr)
{
unsigned char i,l=0;
while (ptr[l] >31){l++;};//取得要显示的个数
for (i=0;i<l;i++)
{ dispone(x++,y,ptr[i]);
if ( x == 16 )
{ x = 0;
y ^= 1;//换行
}
}
}
/*=======================================================
按指定位置显示一个数字表(4位BCD码)
=======================================================*/
unsigned char code number[]="0123456789";//for dis table
void dispnumb(unsigned char x,unsigned char y,unsigned int n)
{ unsigned char digi[4];
unsigned char i;
digi[0]=n/1000;n=n-digi[0]*1000;
digi[1]=n/100;n=n-digi[1]*100;
digi[2]=n/10;n=n-digi[2]*10;
digi[3]=n;
for(i=0;i<4;i++) dispone(x+i,y,number[(digi[i])]);
}
/*=======================================================
显示光标定位
=======================================================*/
void LocateXY( char posx,char posy)
{ unsigned char temp;
temp = posx & 0xf;
posy &= 0x1;
if ( posy )temp |= 0x40;
temp |= 0x80;
lcdwrite_c(temp,0);
}
/*=======================================================
写控制字符子程序: E=1 RS=0 RW=0
=======================================================*/
void lcdwrite_c( unsigned char CMD,unsigned char AttribC )
{ if (AttribC) WaitForEnable(); // 检测忙信号?
RS = 0; RW = 0; _nop_();
DATAPORT =CMD; _nop_(); // 送控制字子程序
Elcd = 1;_nop_();_nop_();Elcd = 0; // 操作允许脉冲信号
}
/*=======================================================*/
/*=======================================================
当前位置写字符子程序: E =1 RS=1 RW=0
=======================================================*/
void LcdWrite_D( char dataW )
{ WaitForEnable(); // 检测忙信号
RS = 1; RW = 0; _nop_();
DATAPORT = dataW; _nop_();
Elcd = 1; _nop_(); _nop_(); Elcd = 0; // 操作允许脉冲信号
}
/*=======================================================
正常读写操作之前必须检测LCD控制器状态: CS=1 RS=0 RW=1
DB7: 0 LCD控制器空闲; 1 LCD控制器忙
========================================================*/
void WaitForEnable( void )
{ DATAPORT = 0xff;
RS =0; RW = 1; _nop_(); Elcd = 1; _nop_(); _nop_();
while( DATAPORT & busy );
Elcd = 0;
}
// -----------------------------------------短延时
void delay5ms(void)
{ unsigned int i = 5552;
while(i--);
}
//delay n*0.05s-----------------------------------------
void delay(unsigned int n) { //delay some time
unsigned int i;
for (i=0;i<n;i++) {
TMOD=0x01;
TH0=0x3c;TL0=0xaf;
TR0=1;
for(TF0=0;TF0==0;);//eaqual wait
}
}
//-----------test LED-----------------------
void blink(unsigned char t,unsigned char n)
{ unsigned char i;
for(i=0;i<n;i++)
{ LED=0;delay(t);LED=1;delay(t); }
}
//----------------LCD1602.h END----------------------------------------
//-----------display for ads1100------------------------------
void display(unsigned char x,unsigned char y,unsigned int n)
{ unsigned char digi[5];
unsigned char i;
digi[0]=n/10000;n=n-digi[0]*10000;
digi[1]=n/1000;n=n-digi[1]*1000;
digi[2]=n/100;n=n-digi[2]*100;
digi[3]=n/10;n=n-digi[3]*10;
digi[4]=n;
for(i=0;i<5;i++) dispone(x+i,y,number[(digi[i])]);
}
//------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -