12864反白问题
时间:10-02
整理:3721RD
点击:
我的程序是设定按一下键之后会出现光标和反白,可是事实并不是这样,下面是我的源程序,求各位指点,谢谢。
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs=P3^5;
sbit lcden=P3^4;
sbit lcdwr=P3^6;
sbit lcdpsb=P3^7;
sbit dula=P2^6;
sbit wela=P2^7;
sbit sclk=P0^0;
sbit tio=P0^1;
sbit trst=P0^2;
sbit key1=P3^0;
uchar num,k;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*ds1302*/
void ds_write(uchar com,uchar date)
{
uchar i;
trst=0;
sclk=0;
trst=1;
for(i=0;i<8;i++)
{
sclk=0;
tio=com&0x01;
sclk=1;
com>>=1;
}
for(i=0;i<8;i++)
{
sclk=0;
tio=date&0x01;
sclk=1;
date>>=1;
}
}
uchar ds_read(uchar com)
{
uchar i,date;
trst=0;
sclk=0;
trst=1;
for(i=0;i<8;i++)
{
sclk=0;
tio=com&0x01;
sclk=1;
com>>=1;
}
for(i=0;i<8;i++)
{
sclk = 0; //拉低时钟总线,产生下降沿,DS1302把数据放到TIO上
date >>= 1; //右移一位
if(tio) date |= 0x80;//读取数据,从最低位开始
sclk = 1; //拉高时钟总线,以备下一次产生下降沿
}
return date;
}
/*数据转化BCD*/
uchar D_B(uchar date)
{
uchar dat1,dat2;
dat1=date/10;
dat2=date%10;
date=dat2+dat1*16;
return date;
}
/*BCD转化数据*/
uchar B_D(uchar date)
{
uchar dat1,dat2;
dat1=date/16;
dat2=date%16;
date=dat2+dat1*10;
return date;
}
/*向ds1302写数据*/
void write_ds1302()
{
ds_write(0x8e, 0);//清除写保护
ds_write(0x80, D_B(50));//30秒(并且进行BCD码转换)
ds_write(0x82, D_B(59));//15分
ds_write(0x84, D_B(23));//19时
ds_write(0x86, D_B(30));
ds_write(0x88, D_B(7));
ds_write(0x8a, D_B(36));
ds_write(0x8e, 0x80);//开写保护
}
/*向ds1302读取数据*/
void read_ds1302()
{
uchar miao,fen,shi,nian,yue,ri;
ds_write(0x8e, 0); //清除写保护
miao = B_D(ds_read(0x81));//读秒寄存器(并且进行BCD码转换)
fen = B_D(ds_read(0x83));//读分寄存器
shi = B_D(ds_read(0x85));//读时寄存器
ri = B_D(ds_read(0x87));
yue = B_D(ds_read(0x89));
nian = B_D(ds_read(0x8b));
ds_write(0x8e, 0x80);//开写保护
}
/*12864*/
/*写指令*/
void write_com(uchar com)
{
lcdrs=0;
lcdwr=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/*写数据*/
void write_dat(uchar dat)
{
lcdrs=1;
lcdwr=0;
lcden=0;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/*设定显示位置*/
void lcd_pos(uchar x,uchar y)
{
char pos;
if(x==0)
{x=0x80;}
if(x==1)
{x=0x90;}
if(x==2)
{x=0x88;}
if(x==3)
{x=0x98;}
pos=x+y;
write_com(pos);
}
/*初始化设定*/
void lcd_init()
{
// wela=1;
// P0=0;
wela=0;
// dula=1;
// P0=0;
dula=0;
lcdpsb=1;
write_com(0x30);
delay(5);
write_com(0x0c);
delay(5);
write_com(0x06);
delay(5);
write_com(0x01);
delay(5);
}
void keyscan()
{
if(key1==0)
{
delay(5);
if(key1==0)
{
while(!key1)
num++;
k=1;
switch(num)
{
case 1:write_com(0x0f);
lcd_pos(1,4);
break;
}
}
}
}
void display_1302()
{
keyscan();
if(k==0)
{
uchar shi,fen,miao,nian,yue,ri;
write_com(0x90);
write_dat(miao/10+0x30);
write_dat(miao%10+0x30);
write_dat(':');
write_dat(fen/10+0x30);
write_dat(fen%10+0x30);
write_dat(':');
write_dat(shi/10+0x30);
write_dat(shi%10+0x30);
write_com(0x80);
write_dat('2');
write_dat('0');
write_dat(nian/10+0x30);
write_dat(nian%10+0x30);
write_dat('-');
write_dat(yue/10+0x30);
write_dat(yue%10+0x30);
write_dat('-');
write_dat(ri/10+0x30);
write_dat(ri%10+0x30);
}
}
void main()
{
lcd_init();
write_ds1302();
while(1)
{
read_ds1302();
display_1302();
}
}
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs=P3^5;
sbit lcden=P3^4;
sbit lcdwr=P3^6;
sbit lcdpsb=P3^7;
sbit dula=P2^6;
sbit wela=P2^7;
sbit sclk=P0^0;
sbit tio=P0^1;
sbit trst=P0^2;
sbit key1=P3^0;
uchar num,k;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*ds1302*/
void ds_write(uchar com,uchar date)
{
uchar i;
trst=0;
sclk=0;
trst=1;
for(i=0;i<8;i++)
{
sclk=0;
tio=com&0x01;
sclk=1;
com>>=1;
}
for(i=0;i<8;i++)
{
sclk=0;
tio=date&0x01;
sclk=1;
date>>=1;
}
}
uchar ds_read(uchar com)
{
uchar i,date;
trst=0;
sclk=0;
trst=1;
for(i=0;i<8;i++)
{
sclk=0;
tio=com&0x01;
sclk=1;
com>>=1;
}
for(i=0;i<8;i++)
{
sclk = 0; //拉低时钟总线,产生下降沿,DS1302把数据放到TIO上
date >>= 1; //右移一位
if(tio) date |= 0x80;//读取数据,从最低位开始
sclk = 1; //拉高时钟总线,以备下一次产生下降沿
}
return date;
}
/*数据转化BCD*/
uchar D_B(uchar date)
{
uchar dat1,dat2;
dat1=date/10;
dat2=date%10;
date=dat2+dat1*16;
return date;
}
/*BCD转化数据*/
uchar B_D(uchar date)
{
uchar dat1,dat2;
dat1=date/16;
dat2=date%16;
date=dat2+dat1*10;
return date;
}
/*向ds1302写数据*/
void write_ds1302()
{
ds_write(0x8e, 0);//清除写保护
ds_write(0x80, D_B(50));//30秒(并且进行BCD码转换)
ds_write(0x82, D_B(59));//15分
ds_write(0x84, D_B(23));//19时
ds_write(0x86, D_B(30));
ds_write(0x88, D_B(7));
ds_write(0x8a, D_B(36));
ds_write(0x8e, 0x80);//开写保护
}
/*向ds1302读取数据*/
void read_ds1302()
{
uchar miao,fen,shi,nian,yue,ri;
ds_write(0x8e, 0); //清除写保护
miao = B_D(ds_read(0x81));//读秒寄存器(并且进行BCD码转换)
fen = B_D(ds_read(0x83));//读分寄存器
shi = B_D(ds_read(0x85));//读时寄存器
ri = B_D(ds_read(0x87));
yue = B_D(ds_read(0x89));
nian = B_D(ds_read(0x8b));
ds_write(0x8e, 0x80);//开写保护
}
/*12864*/
/*写指令*/
void write_com(uchar com)
{
lcdrs=0;
lcdwr=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/*写数据*/
void write_dat(uchar dat)
{
lcdrs=1;
lcdwr=0;
lcden=0;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/*设定显示位置*/
void lcd_pos(uchar x,uchar y)
{
char pos;
if(x==0)
{x=0x80;}
if(x==1)
{x=0x90;}
if(x==2)
{x=0x88;}
if(x==3)
{x=0x98;}
pos=x+y;
write_com(pos);
}
/*初始化设定*/
void lcd_init()
{
// wela=1;
// P0=0;
wela=0;
// dula=1;
// P0=0;
dula=0;
lcdpsb=1;
write_com(0x30);
delay(5);
write_com(0x0c);
delay(5);
write_com(0x06);
delay(5);
write_com(0x01);
delay(5);
}
void keyscan()
{
if(key1==0)
{
delay(5);
if(key1==0)
{
while(!key1)
num++;
k=1;
switch(num)
{
case 1:write_com(0x0f);
lcd_pos(1,4);
break;
}
}
}
}
void display_1302()
{
keyscan();
if(k==0)
{
uchar shi,fen,miao,nian,yue,ri;
write_com(0x90);
write_dat(miao/10+0x30);
write_dat(miao%10+0x30);
write_dat(':');
write_dat(fen/10+0x30);
write_dat(fen%10+0x30);
write_dat(':');
write_dat(shi/10+0x30);
write_dat(shi%10+0x30);
write_com(0x80);
write_dat('2');
write_dat('0');
write_dat(nian/10+0x30);
write_dat(nian%10+0x30);
write_dat('-');
write_dat(yue/10+0x30);
write_dat(yue%10+0x30);
write_dat('-');
write_dat(ri/10+0x30);
write_dat(ri%10+0x30);
}
}
void main()
{
lcd_init();
write_ds1302();
while(1)
{
read_ds1302();
display_1302();
}
}