|
爱科技、爱创意、爱折腾、爱极致,我们都是技术控
您需要 登录 才可以下载或查看,没有账号?立即注册
x
最近在写数字表头的程序时,试试四线数据传输给1602液晶屏,效果还可以,对于实时要求不高的场合效果还不错。这两个程序通过了STC15W408AS单片机系统的验证,主频11.0592NHz时。请坛友指正!
#ifndef __LCD1602_H__
#define __LCD1602_H__
unsigned char bdata C; //在位寻址区定义字符变量C
sbit C7=C^7;
sbit C6=C^6;
sbit C5=C^5;
sbit C4=C^4;
sbit C3=C^3;
sbit C2=C^2;
sbit C1=C^1;
sbit C0=C^0;
sbit RS=P1^1;
sbit RW=P1^2;
sbit EN=P1^3;
sbit I4=P1^4;
sbit I5=P1^5;
sbit I6=P1^6;
sbit I7=P1^7;
/****************延时***************/
void delay_ms(unsigned int i)
{
unsigned int t;
while(i--)
{
for(t=0;t<123;t++);
}
}
/***用LCD1602高四位数据线写入命令**/
void write_com(unsigned char command)
{
C=command;
RS=0;
RW=0;
EN=0; //写命令
delay_ms(1);
I7=C7;
I6=C6;
I5=C5;
I4=C4;
EN=1;
delay_ms(6); //延时
EN=0; //下降沿将数据写入LCD1602
delay_ms(1);
I7=C3;
I6=C2;
I5=C1;
I4=C0;
EN=1;
delay_ms(1); //延时
EN=0; //下降沿将数据写入LCD1602
}
/**用LCD1602高四位数据线写入数据**/
void write_dat(unsigned char dat)
{
C=dat;
RS=1;
RW=0;
EN=0;
delay_ms(1);
I7=C7;
I6=C6;
I5=C5;
I4=C4;
EN=1;
delay_ms(6); //延时
EN=0;
delay_ms(1);
I7=C3;
I6=C2;
I5=C1;
I4=C0;
EN=1;
delay_ms(1); //延时
EN=0;
}
/********LCD1602初始化*************/
void LCD_init(void)
{
write_com(0x02);
write_com(0x28);
write_com(0x0c);
write_com(0x80);
write_com(0x01); //清屏
}
/*******设置LCD1602列、行**********/
void LCD_sor(unsigned char x,unsigned y)
{
unsigned char address;
if(y==0)
address=0x80+x;
else
address=0xC0+x;
write_com(address);
}
/*******************/
void LCD_str(unsigned x,unsigned char y,unsigned char *str)
{
LCD_sor(x,y);
while((*str)!='\0')
{
write_dat(*str);
str++;
}
}
#endif
另一种方式:
#ifndef __LCD1602_H__
#define __LCD1602_H__
unsigned char C;
/****************延时***************/
void delay_ms(unsigned int i)
{
unsigned int t;
while(i--)
{
for(t=0;t<123;t++);
}
}
/***用LCD1602高四位数据线写入命令**/
void write_com(unsigned char command)
{
unsigned char dat;
C=command;
dat=C&0xF0; //屏蔽数据低四位
P1=0x00; //1602三个使能端低电平
delay_ms(1);
P1=dat|0x08; //1602使能端EN高电平
delay_ms(6); //延时
P1=dat|0x00; //1602使能端EN下降沿将数据写入LCD1602
delay_ms(1);
dat=C&0x0F; //屏蔽数据高四位
dat=dat<<4; //移位至高四位
P1=dat|0x08; //1602使能端EN高电平
delay_ms(1); //延时
P1=dat|0x00; //1602使能端EN下降沿将数据写入LCD1602
}
/**用LCD1602高四位数据线写入数据**/
void write_dat(unsigned char dat)
{
unsigned char a;
C=dat;
a=C&0xF0; //分离高四位
P1=0x02;
delay_ms(1);
P1=a|0x0A;
delay_ms(6); //延时
P1=a|0x02;
delay_ms(1);
a=C&0x0F;
a=a<<4;
P1=a|0x0A;
delay_ms(1); //延时
P1=a|0x02;
}
/********LCD1602初始化*************/
void LCD_init(void)
{
write_com(0x02);
write_com(0x28);
write_com(0x0c);
write_com(0x80);
write_com(0x01); //清屏
}
/*******设置LCD1602列、行**********/
void LCD_sor(unsigned char x,unsigned y)
{
unsigned char address;
if(y==0)
address=0x80+x;
else
address=0xC0+x;
write_com(address);
}
/*******************/
void LCD_str(unsigned x,unsigned char y,unsigned char *str)
{
LCD_sor(x,y);
while((*str)!='\0')
{
write_dat(*str);
str++;
}
}
#endif
|
打赏
-
查看全部打赏
|