📄 final.c
字号:
#include <reg52.h>
#include <stdio.h>
#include <string.h>
#include <INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define data P0
#define Busy 0x80 //用于检测LCD状态字中的Busy标识
sbit RS=P1^5;
sbit RW=P1^6;
sbit E=P1^7;
uchar imm;
uchar buf=0xff;
uchar msg[15]="Received Data: ";
uchar me[]="Huan Yin Zai Lai";
/******************LCD函数声明*********************/
void LCDInit(void);
void WritedataLCD(uchar WDLCD);
void WriteCommandLCD(uchar WCLCD,bit uBusyC);
void ReadStatusLCD(void);
void ReaddataLCD(uchar RDLCD);
void DisplayOneChar(uchar X, uchar Y, uchar Ddata);
void DisplayListChar(uchar x,uchar y, uchar *listdata);
/********************LCD函数**********************/
//读数据函数: E=高脉冲 RS=1 RW=1
void ReaddataLCD(uchar RDLCD)
{
ReadStatusLCD(); //检测忙
RS=1;
RW=1;
data=RDLCD;
E=1; //若晶振速度太高可以在这后加小的延时
_nop_();
_nop_();
_nop_();
E=0;//重设E=0
}
//写数据函数: E=高脉冲 RS=1 RW=0
void WritedataLCD(uchar WDLCD)
{
ReadStatusLCD(); //检测忙
RS=1;
RW=0;
data=WDLCD;
E=1; //若晶振速度太高可以在这后加小的延时
_nop_();
_nop_();
_nop_();
E=0;//重设E=0
}
//写指令函数: E=高脉冲 RS=0 RW=0
void WriteCommandLCD(uchar WCLCD,bit BusyC) //BusyC为0时忽略忙检测
{
if(BusyC) ReadStatusLCD(); //根据需要检测忙
RS=0;
RW=0;
data=WCLCD;
E=1; //若晶振速度太高可以在这后加小的延时
_nop_();
E=0;//重设E=0
}
//正常读写操作之前必须检测LCD控制器状态:E=1 RS=0 RW=1;DB7: 0 LCD控制器空闲,1 LCD控制器忙。
//检测忙信号,等待LCD空闲函数
void ReadStatusLCD(void)
{
data=0xFF;
E=1;
RS=0;
RW=1;
_nop_();
_nop_();
_nop_();
while(data&Busy); //检测忙信号
E=0;
}
void LCDInit(void) //LCD初始化
{
WriteCommandLCD(0x38,0);
// delay();
WriteCommandLCD(0x38,1); //显示模式设置,开始要求每次检测忙信号
// delay();
WriteCommandLCD(0x08,1); //关闭显示
// delay();
WriteCommandLCD(0x01,1); //显示清屏
// delay();
WriteCommandLCD(0x06,1); // 显示光标移动设置
// delay();
WriteCommandLCD(0x0C,1); // 显示开及光标设置
// delay();
}
//按指定位置显示一个字符
void DisplayOneChar(uchar X, uchar Y, uchar Ddata)
{
Y&=0x01;
X&=0x0F; //限制X不能大于15,Y不能大于1
if(Y)X|=0x40; //当要显示第二行时地址码+0x40;
X|=0x80; //算出指令码
WriteCommandLCD(X,1); //这里检测忙信号,发送地址码
WritedataLCD(Ddata);
}
/**************按指定位置显示一串字符******************/
void DisplayListChar(uchar x,uchar y, uchar *listdata)
{
uchar i=0;
for(i=0;i<strlen(listdata);i++ )
{
if (x<=0x0F) //x坐标应小于0x0F
{
DisplayOneChar(x,y,listdata[i]); //显示单个字符
x++;
}
else if(y==0)
{
y++;
x=x-0x0F;
DisplayOneChar(x,y,listdata[i]);
}
}
}
void delay(void)
{
uchar i,j;
for(i=10;i>0;i--)
for(j=248;j>0;j--);
}
uchar keyscan()
{
uchar h,l,p;
P2=0xf0;
if((P2&0xf0)!=0xf0)
{
delay();
if((P2&0xf0)!=0xf0)
{
h=0xfe;
while((h&0x10)!=0)
{
P2=h;
if((P2&0xf0)!=0xf0)
{
l=(P2&0xf0)|0x0f;
p=(~h)+(~l);
if(p==0x81) return ('0');
if(p==0x41) return ('1');
if(p==0x21) return ('2');
if(p==0x11) return ('3');
if(p==0x82) return ('4');
if(p==0x42) return ('5');
if(p==0x22) return ('6');
if(p==0x12) return ('7');
if(p==0x84) return ('8');
if(p==0x44) return ('9');
if(p==0x24) return ('A');
if(p==0x14) return ('B');
if(p==0x88) return ('C');
if(p==0x48) return ('D');
if(p==0x28) return ('E');
if(p==0x18) return ('F');
}
else h=(h<<1)|0x01;
}
}
}
return (0xff);
}
void initial()
{
PCON=0x80;//SMOD=1;波特率:fosc/32
SCON=0x80;//方式2
REN=1; //允许接收
ES=1; //开串口中断
EA=1; //CPU开中断
}
void send(uchar x)
{
SBUF=x;//发送信号
while(TI==0);//到发送完TI置1
TI=0;//软件清除中断标志
}
void receive()
{
buf=SBUF;
while(RI==0); //到接收完RI置1
RI=0;
msg[14]=buf;
}
void main(void)
{
LCDInit(); //LCD初始化
initial();
while(1)
{
imm=keyscan();
if(imm!=0xff)send(imm);
DisplayListChar(0,0,msg);
DisplayListChar(0,1,me);
}
}
void SerialPort() interrupt 4 using 2
{
if(RI)receive();
else if(TI)send(imm);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -