就一个lcd1602显示的程序,才学没多久,在proteus上没问题,但是实物就不行了下面是程序和模拟
#include<reg52.h>
#define DB P0 //
sbit RS=P2^0; //
sbit RW=P2^1; //
sbit E=P2^2; //
#define uchar unsigned char
#define uint unsigned int
uchar table1[7]="time is";
uchar table2[7]="lcd1602";
void delay(uint t) //延时函数
{
uint i,j;
for(i=t;i>0;i--)
for(j=11;j>0;j--);
}
void writec(uchar dat)//写指令的函数
{
RS=0;
RW=0;
DB=dat;
delay(5);
E=1;
delay(5);
E=0;
}
void writed(uchar dat)//写数据的函数
{
RS=1;
RW=0;
DB=dat;
delay(5);
E=1;
delay(5);
E=0;
}
void set_xy(uchar x,uchar y)
{
uchar address;
if(y==1)
{
address=0x80+x; // - - 第一行位置
}
else {
address=0xc0+x; // - - 第二行位置
}
delay(5);
writec(address);/地址确定之后需要写入写指令程序
}
void init(void) //1602初始化
{
RW=0;
writec(0x38);
writec(0x0c);
writec(0x06);
writec(0x01);
}
void main()
{
uchar i,j;
init();
while(1)
{
set_xy(4,1);//选择数据开始的位置 set函数内部有writec函数 内部设置了rs=0,0x80表示在第一行第一位开始写字母
for(i=0;i<7;i++)
{
writed(table1[i]);//写数据
}
set_xy(4,2);
for(j=0;j<7;j++)
{
writed(table2[j]);
}
}
}
|