📄 1602.c
字号:
#include <at89x51.h>
#define uchar unsigned char
#define uint unsigned int
#define busy 0x80
sbit RS=P3^7;
sbit RW=P3^6;
sbit E=P3^5;
sfr lcd_data=0xa0;
void cls(void);
void uart_init();
void send_string(uchar *str);
void send_byte(uchar da);
void initlcd();
void write_data(uchar x);
void write_instruction(uchar x);
void write_string(uchar x,uchar *p);
void delay(uint i);
uchar code string_to_pc[]="字符型LCD1602和传口测试程序,请输入单个字母或数字发给我!";
uchar code sring_to_lcd[]="LCM 1602,I LOVE";
void main()
{
uart_init();//初始化传口
send_string(string_to_pc);//发送字符串
send_byte(0x0d);//计算机换行
send_byte(0x0a);
initlcd();
write_string(0x80,sring_to_lcd);
while(1);
}
/************************************初始化串口******************************/
void uart_init()
{
TMOD=0x20;
TH1=0XFD;
TL1=0XFD;
PCON=0X00;
TR1=1;
SCON=0X50;
IE=0x90;
}
/************************************发送一串字符*****************************/
void send_string(uchar *str)
{
uchar i;
i=0;
while(*(str+i)!='\0')
{
send_byte(*(str+i));
i++;
}
}
/************************************发送单个字符******************************/
void send_byte(uchar da)
{
EA=0;
SBUF=da;
while(!TI)
TI=0;
EA=1;
}
/************************************初始化LCD******************************/
void initlcd()
{
write_instruction(0x01);
delay(20);
write_instruction(0x38);
delay(20);
write_instruction(0x0f);
delay(20);
write_instruction(0x06);
delay(20);
}
/************************************写命令******************************/
void write_instruction(uchar x)
{
delay(1000);
E=0;
RW=0;
RS=0;
lcd_data=x;
E=1;
delay(20);
E=0;
}
/*void check_busy(void)
{
lcd=0xff;
E=0;
RW=1;
RS=0;
E=1;
delay(100);
while(lcd&busy);
E=0;
}
*/
/************************************延时子程序*****************************/
void delay(uint i)
{
while(i--);
}
/************************************写数据到LCD中******************************/
void write_data(uchar x)
{
delay(1000);
E=0;
RW=0;
RS=1;
E=1;
lcd_data=x;
E=0;
}
/************************************LCD清屏******************************/
void cls(void)
{
write_instruction(0x01);
}
/************************************写字符串******************************/
void write_string(uchar x,uchar *p)
{
write_instruction(x);
while(*p!=0x00)
{
write_data(*p);
p++;
x++;
if(x==0x8f)
{
write_instruction(0xc0);
}
}
}
/************************************串口中断******************************/
uart()interrupt 4
{
uchar temp;
uchar count;
EA=0;
if(RI)
{
RI=0;
temp=SBUF;
if(count==0)
{
cls();
send_byte(0x0d);//计算机换行
send_byte(0x0a);
}
write_data(temp);//写数据到LCD
send_byte(temp);//发送回电脑
count++;
if(count==0x10)//满一屏
{
write_instruction(0xc0);//LCD地址切换到第而行
send_byte(0x0d);//换行
send_byte(0x0a);
}
if(count==0x20)
{
count=0;
}
}
EA=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -