程序弄了四天了啊!还是没头绪。伤不起。请指教。谢谢!
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code ss[]="ds1302";
uchar code ss1[]="DAY-4";
uchar code ss2[]="0123456789-";
sbit lcden=P3^4;
sbit rs=P3^5;
sbit wr=P3^6;
sbit wela=P2^7;
sbit dula=P2^6;
sbit sclk=P2^1;
sbit io=P2^0;
sbit rst=P2^5;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
void delay(uchar a)
{
uint i,j;
for(i=a;i>0;i--)
for(j=110;j>0;j--);
}
//**********************************
// DS1302模块
//**********************************
void input_byte(uchar date)
{
uchar i;
ACC=date;
for(i=0;i<8;i++)
{
io=ACC^0;
sclk=0;
sclk=1;
ACC=ACC>>1;
}
}
uchar output_byte(void)
{
uchar i;
for(i=0;i<8;i++)
{
// temp=temp>>1;
// if(io)
// temp=temp|0x80;
// sclk=1;
// sclk=0;
ACC7=io;
sclk=1;
sclk=0;
ACC=ACC>>1;
}
// return temp;
return ACC;
}
void write(uchar add,uchar date1)
{
rst=0; //上电运行之前,rst必须保持低电平
sclk=0; //sclk为低电平时,才能将rst置高
rst=1;
input_byte(add);
input_byte(date1);
sclk=1; //时序图中sclk为高时之后一点rst再置为低
rst=0;
}
uchar read(uchar add)
{
uchar temp;
rst=0;
sclk=0;
rst=1;
input_byte(add);
temp=output_byte;
sclk=1;
rst=0;
return temp;
}
//***************************
//led1602
//***************************
void wrlcd_com(uchar lcd_com)
{
rs=0;
P0=lcd_com;
delay(5);
lcden=1;
delay(2);
lcden=0;
}
void wrlcd_da(uchar lcd_da)
{
rs=1;
P0=lcd_da;
delay(5);
lcden=1;
delay(2);
lcden=0;
}
void lcd_init()
{
dula=0;
wela=0;
lcden=0;
wr=0;
wrlcd_com(0x38);
wrlcd_com(0x0c);
wrlcd_com(0x06);
wrlcd_com(0x01);
wrlcd_com(0x80);
}
//************************
//DS1302初始化
//************************
void ds1302_init()
{
write(0x8e,0x00); //去保护
write(0x80,0x09); //秒
write(0x82,0x08); //分
write(0x84,0x11); //24小时方式
write(0x86,0x19); //日
write(0x88,0x07); //月
write(0x8a,0x04); //星期
write(0x8c,0x12); //年
write(0x8e,0x80); //写保护
}
//************************
//主函数部分
//************************
void main()
{
uchar num;
uchar sec,sec1,sec2;
uchar min,min1,min2;
uchar hour,hour1,hour2;
uchar day,day1,day2;
uchar month,month1,month2;
uchar week;
uchar year,year1,year2;
lcd_init();
delay(10);
ds1302_init();
delay(10);
for(num=0;num<6;num++)
{
wrlcd_da(ss[num]);
delay(20);
}
wrlcd_com(0x80+0x41);
for(num=0;num<4;num++)
{ // wrlcd_com(0x80+0x41+num);
wrlcd_da(ss1[num]);
// wrlcd_da(ss1[num]);
delay(20);
}
while(1)
{
sec=read(0x81); //miao
// delay(1);
sec1=sec&0x0f;
sec2=sec>>4;
min=read(0x83); //fen
min1=min&0x0f;
min2=min>>4;
hour=read(0x85); //shi
hour1=hour&0x0f;
hour2=hour>>4;
day=read(0x87); //tia
day1=day&0x0f;
day2=day>>4;
month=read(0x89); //yue
month1=month&0x0f;
month2=month>>4;
week=read(0x8b); //zhou
week=week&0x0f;
year=read(0x8d); //nian
year1=year&0x0f;
year2=year>>4;
wrlcd_com(0x80+0x4e);
wrlcd_da(ss2[sec2]);
wrlcd_com(0x80+0x4f);
wrlcd_da(ss2[sec1]);
wrlcd_com(0x80+0x4d);
wrlcd_da(':');
wrlcd_com(0x80+0x4b);
wrlcd_da(ss2[min2]);
wrlcd_com(0x80+0x4c);
wrlcd_da(ss2[min1]);
wrlcd_com(0x80+0x4a);
wrlcd_da(':');
wrlcd_com(0x80+0x48);
wrlcd_da(ss2[hour2]);
wrlcd_com(0x80+0x49);
wrlcd_da(ss2[hour1]);
wrlcd_com(0x80+0x0f);
wrlcd_da(ss2[day2]);
wrlcd_com(0x80+0x0e);
wrlcd_da(ss2[day1]);
wrlcd_com(0x80+0x0d);
wrlcd_da('/');
wrlcd_com(0x80+0x0b);
wrlcd_da(ss2[month2]);
wrlcd_com(0x80+0x0c);
wrlcd_da(ss2[month1]);
wrlcd_com(0x80+0x0a);
wrlcd_da('/');
wrlcd_com(0x80+0x08);
wrlcd_da(ss2[year2]);
wrlcd_com(0x80+0x09);
wrlcd_da(ss2[year1]);
}
} |