单片机1302编程的高手来看看。
#include<reg51.h>#define uint unsigned int
#define uchar unsigned char
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;
sbit IO=P2^5;
sbit RST=P2^6;
sbit SCLK=P2^4;
uchar code table[]="Date: - - ";
uchar code table1[]=" : :";
unsigned char code digit[10]={"0123456789"};
uchar s,f,h,d,w,m,y,num;
void delay(uint z)
{
uint x;
for(;z>0;z--)
for(x=0;x<40;x++) ;
}
void write_com(uchar com)
{RS=0;
EN=0;
P0=com;
delay(5);
EN=1;
delay(5);
EN=0;
}
void write_date(uchar date)
{RS=1;
EN=0;
P0=date;
delay(5);
EN=1;
delay(5);
EN=0;
}
void init()
{ RW=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
write_com(0x00);
delay(50);
{for(num=0;num<11;num++)
write_date(table[num]);
}
write_com(0x80+0x40);
{for(num=0;num<9;num++)
write_date(table1[num]);
}
}
void displayh(uchar x)
{
uchar i,j;
i=x/16;
j=x%16;
write_com(0x40+0x03+0x80);
write_date(digit[i]);
write_date(digit[i]);
delay(50);
}
void displayf(uchar x)
{
uchar i,j;
i=x/16;
j=x%16;
write_com(0x40+0x06+0x80);
write_date(digit[i]);
write_date(digit[j]);
delay(50);
}
void displays(uchar x)
{
uchar i,j;
i=x/16;
j=x%16;
write_com(0x40+0x09+0x80);
write_date(digit[i]);
write_date(digit[j]);
delay(100);
}
void displayd(uchar x)
{
uchar i,j;
i=x/16;
j=x%16;
write_com(0x80+0x0b);
write_date(digit[i]);
write_date(digit[j]);
delay(100);
}
void displaym(uchar x)
{
uchar i,j;
i=x/10;
j=x%10;
write_com(0x88);
write_date(digit[i]);
write_date(digit[j]);
delay(100);
}
void displayy(uchar x)
{
uchar i,j;
i=x/16;
j=x%16;
write_com(0x80+5);
write_date(digit[i]);
write_date(digit[j]);
delay(100);
}
void writecom(uchar com,uchar date)
{uchar i;
RST=0;
SCLK=0;
RST=1;
for(i=0;i<8;i++)
{IO=com&0x01;
SCLK=0;
SCLK=1;
com=com>>1;
}
for(i=0;i<8;i++)
{IO=date&0x01;
SCLK=0;
SCLK=1;
date=date>>1;
}
RST=0;
}
uint read_com(uchar com)
{ uchar redate,i;
redate=0;
RST=0;
SCLK=0;
RST=1;
for(i=0;i<8;i++)
{IO=com&0x01;
SCLK=0;
SCLK=1;
com=com>>1;
}
for(i=0;i<8;i++)
{
SCLK=1;
SCLK=0;
redate=redate|0x01;
}
return(redate);
delay(1);
RST=0;
}
void init1302()
{
writecom(0x80,0x00);//写秒
delay(5);
writecom(0x82,0x00);//写分
delay(5);
writecom(0x84,0x00);//写时
delay(5);
writecom(0x86,0x00);//写日
delay(5);
writecom(0x88,0x00);//写月
delay(5);
writecom(0x8a,0x00);//写星期
delay(5);
writecom(0x8c,0x00);//写年
delay(5);
}
void readtime()
{
s=read_com(0x81);//读秒
delay(5);
f=read_com(0x83);//读分
delay(5);
h=read_com(0x85);//读时
delay(5);
d=read_com(0x87);//读日
delay(5);
m=read_com(0x89);//读月
delay(5);
w=read_com(0x8b);//读星期
delay(5);
y=read_com(0x8d);//读年
delay(5);
}
void starctrl(bit ctrldat)//振荡启动和停止
{
if(ctrldat) writecom(0x8e,0x80);
else writecom(0x80,0x00);
}
void main()
{
init();
init1302();
starctrl(1);
readtime();
while(1)
{
displayy(y);
delay(100);
displaym(m);
delay(100);
displayd(d);
delay(100);
h=read_com(0x85);
displayh(h);
delay(100);
f=read_com(0x83);
displayf(f);
delay(100);
s=read_com(0x81);
displays(s);
delay(100);
}
}
有谁知道该怎么改动才动时间走动了?