|
就一個lcd1602顯示的程序,才學(xué)沒多久,在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) //延時函數(shù)
{
uint i,j;
for(i=t;i>0;i--)
for(j=11;j>0;j--);
}
void writec(uchar dat)//寫指令的函數(shù)
{
RS=0;
RW=0;
DB=dat;
delay(5);
E=1;
delay(5);
E=0;
}
void writed(uchar dat)//寫數(shù)據(jù)的函數(shù)
{
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);//選擇數(shù)據(jù)開始的位置 set函數(shù)內(nèi)部有writec函數(shù) 內(nèi)部設(shè)置了rs=0,0x80表示在第一行第一位開始寫字母
for(i=0;i<7;i++)
{
writed(table1[i]);//寫數(shù)據(jù)
}
set_xy(4,2);
for(j=0;j<7;j++)
{
writed(table2[j]);
}
}
}
|
|