本帖最后由 july0 于 2018-3-3 21:41 编辑
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#include"24c02.h"
sbit rs=P2^6;
sbit wr=P2^5;
sbit lcden=P2^7;
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
sbit rd=P1^3;
sbit beep=P1^4;
uchar count,s1num;
char miao,shi,fen;
uchar code table[]="2008-9-30 MON";
void delay(uint z)
{
uint x,y;
for(x=z;x--;x>0)
for(y=110;y--;y>0);
}
void di()
{
beep=0;
delay(100);
beep=1;
}
void write_com(uchar com)//命令送给液晶
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void init()
{
void init_24c02();
uchar num;
wr=0;
rd=0;
lcden=0;
fen=0;
shi=0;
miao=0;
count=0;
s1num=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40+6);
write_date(':');
delay(5);
write_com(0x80+0x40+9);
write_date(':');
delay(5);
miao=read_add(1);
fen=read_add(2);
shi=read_add(3);
write_sfm(10,miao);
write_sfm(4,shi);
write_sfm(7,fen);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1);
di();
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3);
write_com(0x80+0x40+4);
if(s1num==4);
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if(s1num!=0);
{
if(s2==0);
delay(5);
if(s2==0);
{
while(!s2);
di();
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(1,miao);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(2,fen);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(3,shi);
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
di();
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(1,miao);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(2,fen);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(3,shi);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
miao++;
count=0;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
write_add(3,shi);
}
write_sfm(7,fen);
write_add(2,fen);
}
write_sfm(10,miao);
write_add(1,miao);
}
}
其中24c02.h的内容是
bit write=0;
sbit sda=P2^0;
sbit scl=P2^1;
void delay0()
{
;;
}
void start()
{
sda=1;
delay0();
scl=1;
delay0();
sda=0;
delay0();
}
void stop()
{
sda=0;
delay0();
scl=1;
delay0();
sda=1;
delay0();
}
void respon()
{
uchar i;
scl=1;
delay0();
while((sda==1)&&(i<250))i++;
scl=0;
delay0();
}
void init_24c02()
{
sda=1;
delay0();
scl=1;
delay0();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay0();
sda=CY;
delay0();
scl=1;
delay0();
}
scl=0;
delay0();
sda=1;
delay0();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay0();
sda=1;
delay0();
for(i=0;i<8;i++)
{
scl=1;
delay0();
k=(k<<1)|sda;
scl=0;
delay0();
}
return k;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respon();
write_byte(address);
respon();
write_byte(date);
respon();
stop();
}
char read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respon();
write_byte(address);
respon();
start();
write_byte(0xa1);
respon();
date=read_byte();
stop();
return date;
}
求解为什么连接上按键后按键部分却不起作用(按键没有问题)
|