本帖最后由 gongmaomao 于 2011-3-18 21:28 编辑
自学单片机,用的开发板上面带的时钟芯片是DS1302,本人写了个程序,程序可以正常运行,但是我想用键盘设定时间,但是每次键盘设定之后推出设定的是,液晶(1602)上显示的都不是键盘上设置的时间,如果我想用键盘设定时间的话,应该把键盘设定之后传给1302的语句写在哪里啊?
这是我写的程序、
#include<reg52.h>
sbit rs=P2^6;
sbit wr=P2^5;
sbit lcd=P2^7;
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
sbit SCLK=P3^6;
sbit DIO=P3^4;
sbit CE= P3^5;
sbit ACC_7 = ACC^7;
#define write_SECOND 0x80
#define write_MINUTE 0x82
#define write_HOUR 0x84
#define write_SECOND 0x80
#define write_YEAR 0x8C
#define write_MONTH 0x88
#define write_DAY 0x86
#define write_PROTECT 0x8E
#define write_wEEK 0x8A
#define uchar unsigned char
uchar num=0,a,s1num=0;
char shi,fen,miao,nian,yue,ri,xingqi,flag;
uchar table1[]=" - - ";
uchar table2[]=" : :";
void delay(int a)
{
int x,y;
for(x=0;x<a;x++)
for(y=0;y<71;y++);
}
void write_com(uchar com)
{
rs=0;
wr=0;
P0=com;
lcd=0;
delay(5);
lcd=1;
delay(5);
lcd=0;
}
void write_date(uchar date)
{
rs=1;
wr=0;
P0=date;
lcd=0;
delay(1);
lcd=1;
delay(1);
lcd=0;
}
void init(void)
{
write_com(0x38);
delay(1);
write_com(0x0c);
delay(1);
write_com(0x06);
delay(1);
write_com(0x01);
write_com(0x80);
delay(1);
for(a=0;a<10;a++)
{
write_date(table1[a]);
delay(20);
}
write_com(0xc0);
delay(2);
for(a=0;a<12;a++)
{
write_date(table2[a]);
delay(40);
}
}
void write1302(uchar addr,uchar dat)
{
unsigned char i,temp;
CE=0;
SCLK=0;
CE=1;
for (i=8;i>0;i--)
{
SCLK=0;
temp=addr;
DIO=temp&0x01;
addr>>= 1;
SCLK=1;
}
for ( i=8; i>0; i-- )
{
SCLK=0;
temp=dat;
DIO=temp&0x01;
dat>>= 1;
SCLK=1;
}
CE=0;
}
uchar Read1302(uchar addr)
{
uchar i,temp,dat1,dat2;
CE=0;
SCLK=0;
CE = 1;
for (i=8;i>0;i--)
{
SCLK=0;
temp=addr;
DIO=temp&0x01;
addr>>=1;
SCLK=1;
}
for (i=8;i>0;i--)
{
ACC_7=DIO;
SCLK = 1;
ACC>>=1;
SCLK = 0;
}
CE=0;
dat1=ACC;
dat2=dat1/16;
dat1=dat1%16;
dat1=dat1+dat2*10;
return dat1;
}
void Initial(void)
{
write1302(write_PROTECT,0X00);
write1302(write_SECOND,miao);
write1302(write_MINUTE,fen);
write1302(write_HOUR,shi);
write1302(write_YEAR,nian);
write1302(write_MONTH,yue);
write1302(write_DAY,ri);
write1302(write_wEEK,xingqi);
write1302(write_PROTECT,0x80);
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0xc0+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_nyr(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{
flag=1;
s1num++;
while(!s1);
if(s1num==1)
{
write_com(0xc0+10);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0xc0+7);
}
if(s1num==3)
{
write_com(0xc0+4);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
flag=0;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0xc0+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0xc0+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0xc0+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0xc0+10);
}
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0xc0+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0xc0+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0xc0+4);
}
}
}
}
}
void main()
{
Initial();
init();
while(1)
{
if(flag==0)
{
miao=Read1302(0x81);
fen=Read1302(0x83);
shi=Read1302(0x85);
ri=Read1302(0x87);
yue=Read1302(0x89);
nian=Read1302(0x8d);
xingqi=Read1302(0x8b);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
write_nyr(8,ri);
write_nyr(5,yue);
write_nyr(2,nian);
write_nyr(11,xingqi);
}
keyscan();
}
} |