缘由https://ask.csdn.net/questions/7955623
#include "reg52.h"
sbit LCD1602_RS = P3^5;//RS端数据命令选择(H/L)
sbit LCD1602_RW = P3^6;//RW端读写选择(H/L)
sbit LCD1602_EN = P3^7;//EN端使能信号上升沿25ns
void PanDuan1602(/*LCD1602忙判断*/)
{
LCD1602_RS = 0;/*根据规定,RS为低电平,RW为高电平时,可以读状态*/
LCD1602_RW = 1;
P0 = 0xFF;
while(P0 & 0x80){LCD1602_EN = 0;P0 = 0xFF;LCD1602_EN = 1;}
}
void Xie_MinLin_ShuJu(unsigned char MinLin_ShuJu,bit XZ)/*LCD1602写入8位命令/数据函数*/
{
unsigned char y=0;
PanDuan1602();/*忙则等待*/
LCD1602_RS = XZ;
LCD1602_RW = 0;
while(++y<47);//地址建立时间
P0 = MinLin_ShuJu;/*写入命令*/
LCD1602_EN = 1;/*拉高使能端 数据被传输到LCD1602内*/
LCD1602_EN = 0;/*拉低使能以便于下一次产生上升沿*/
}
void Xie_String(unsigned char *ShuJu,unsigned char w,bit x)/*LCD1602写入8位字符串数据子函数*/
{
Xie_MinLin_ShuJu((x?0xC0:0x80) + w, 0);//1表示第二行0表示第一行
while (*ShuJu != '\0') Xie_MinLin_ShuJu(*ShuJu++, 1);
}
void LCD1602ChuShiHua(/*LCD1602初始化子程序*/)
{
LCD1602_RS = LCD1602_RW = LCD1602_EN = 0;
Xie_MinLin_ShuJu(0x38, 0);/*显示模式设置16*2显示,5*7点阵,8位数据接口*/
Xie_MinLin_ShuJu(0x06, 0);/*显示光标移动设置读写一字节后地址指针加1*/
Xie_MinLin_ShuJu(0x0C, 0);/*显示开及光标设置*/
Xie_MinLin_ShuJu(0x01, 0);/*显示清屏*/
}
unsigned char JianPanShaoMiao(/*使用行列反转扫描法*/)
{
unsigned char H=15,L=240;static unsigned char Ys=0;
P1=15;
if(P1!=15&&++Ys==0)
{
H=P1;
P1=240;
L=P1;
while(P1!=240);
}
P1 = 255;
return (H+L);
}
void main()
{//缘由https://ask.csdn.net/questions/7955623
unsigned char jz=0,sj[8]={" "};
LCD1602ChuShiHua();
Xie_String("Key Values:",0,0);Xie_String("HJ7447",4,1);
while(1)
{
jz=JianPanShaoMiao();
if(jz!=255)
{
sj[0]=jz/100+'0';
sj[1]=jz/10%10+'0';
sj[2]=jz%10+'0';
Xie_String(sj,11,0);
}
}
}
unsigned char duli16()
{//缘由http://www.51hei.com/bbs/dpj-229608-1.html
unsigned char jz=255;
static unsigned char Ys=0;
P1=239;
if(P1!=239&&++Ys==0){jz=P1;while(P1!=239);}
else P1=223;
if(P1!=223&&++Ys==0){jz=P1;while(P1!=223);}
else P1=191;
if(P1!=191&&++Ys==0){jz=P1;while(P1!=191);}
else P1=127;
if(P1!=127&&++Ys==0){jz=P1;while(P1!=127);}
P1 = 255;
return jz;
}