求助 ds1302读时钟程序
时间:10-02
整理:3721RD
点击:
#include <reg51.h>
#include <intrins.h>
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
sbit dat=P1^0;
sbit scl=P1^1;
sbit ce=P1^2;
sbit ACC_7=ACC^7;
unsigned char m,n;
void delay(unsigned char z)
{
unsigned char x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(unsigned char com)
{
e=0;
delay(1);
rs=0;
delay(1);
rw=0;
delay(1);
P0=com;
delay(5);
e=1;
delay(5);
e=0;
delay(1);
}
void write_data(unsigned char date)
{
e=0;
delay(1);
rs=1;
delay(1);
rw=0;
delay(1);
P0=date;
delay(5);
e=1;
delay(5);
e=0;
delay(1);
}
void init_1602() //1602初始化
{
write_com(0x38);
write_com(0x0F);
write_com(0x06);
write_com(0x01);
}
void write_byte(unsigned char temp)
{
unsigned char i;
for(i=8;i>0;i--)
{
scl=0;
delay(1);
dat=temp&0x01;
temp=temp>>1;
scl=1;
delay(1);
}
}
void write_ds1302(unsigned char addr,unsigned char temp) //写程
{
unsigned char i;
ce=0;
delay(1);
ce=1;
delay(1);
write_byte(addr);
write_byte(temp);
ce=0;
delay(1);
}
unsigned char read_ds1302(unsigned char addr) //读程序
{
unsigned char temp1,temp,i;
ce=0;
delay(1);
ce=1;
delay(1);
write_byte(addr);
for(i=0;i<8;i++)
{
delay(1);
if(dat)
temp|=0x80; //读一位
temp>>=1;
scl=0;
delay(1);
scl=1;
}
ce=0;
scl=0;
delay(1);
// temp1=temp/16;
// temp=temp%16;
// temp=temp+temp1*10;
return (temp);
}
void init_time0() //定时器中断
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
}
main()
{
init_1602();
init_time0();
write_ds1302(0x8E,0x00);
write_ds1302(0x80,0x00);
write_ds1302(0x82,0x00);
write_ds1302(0x84,0x00);
write_ds1302(0x8E,0x80);
while(1);
}
void time0() interrupt 1
{
unsigned char shi,fen=0,miao=0,i,j,m,n;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
i++;
if(i==10)
{
i=0;
j++;
}
if(j==10)
{
j=0;
write_com(0x82);
fen=read_ds1302(0x83);
m=fen/16;
n=fen%16;
fen=n+m*10;
write_data(fen/10+0x30);
write_data(fen%10+0x30);
write_data(' ');
/* shi=read_byte(0x85);
m=fen/10;
n=fen%10;
write_data(m+0x30);
write_data(n+0x30);
write_data(' ');
*/
miao=read_ds1302(0x81);
miao=miao&0x7F;
m=miao/16;
n=miao%16;
miao=n+m*10;
write_data(miao/10+0x30);
write_data(miao%10+0x30);
write_data(' ');
write_data('a');
write_data('0');
}
}
为了验证读出来的分和秒是否正确,请各位帮助。
#include <intrins.h>
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
sbit dat=P1^0;
sbit scl=P1^1;
sbit ce=P1^2;
sbit ACC_7=ACC^7;
unsigned char m,n;
void delay(unsigned char z)
{
unsigned char x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(unsigned char com)
{
e=0;
delay(1);
rs=0;
delay(1);
rw=0;
delay(1);
P0=com;
delay(5);
e=1;
delay(5);
e=0;
delay(1);
}
void write_data(unsigned char date)
{
e=0;
delay(1);
rs=1;
delay(1);
rw=0;
delay(1);
P0=date;
delay(5);
e=1;
delay(5);
e=0;
delay(1);
}
void init_1602() //1602初始化
{
write_com(0x38);
write_com(0x0F);
write_com(0x06);
write_com(0x01);
}
void write_byte(unsigned char temp)
{
unsigned char i;
for(i=8;i>0;i--)
{
scl=0;
delay(1);
dat=temp&0x01;
temp=temp>>1;
scl=1;
delay(1);
}
}
void write_ds1302(unsigned char addr,unsigned char temp) //写程
{
unsigned char i;
ce=0;
delay(1);
ce=1;
delay(1);
write_byte(addr);
write_byte(temp);
ce=0;
delay(1);
}
unsigned char read_ds1302(unsigned char addr) //读程序
{
unsigned char temp1,temp,i;
ce=0;
delay(1);
ce=1;
delay(1);
write_byte(addr);
for(i=0;i<8;i++)
{
delay(1);
if(dat)
temp|=0x80; //读一位
temp>>=1;
scl=0;
delay(1);
scl=1;
}
ce=0;
scl=0;
delay(1);
// temp1=temp/16;
// temp=temp%16;
// temp=temp+temp1*10;
return (temp);
}
void init_time0() //定时器中断
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
}
main()
{
init_1602();
init_time0();
write_ds1302(0x8E,0x00);
write_ds1302(0x80,0x00);
write_ds1302(0x82,0x00);
write_ds1302(0x84,0x00);
write_ds1302(0x8E,0x80);
while(1);
}
void time0() interrupt 1
{
unsigned char shi,fen=0,miao=0,i,j,m,n;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
i++;
if(i==10)
{
i=0;
j++;
}
if(j==10)
{
j=0;
write_com(0x82);
fen=read_ds1302(0x83);
m=fen/16;
n=fen%16;
fen=n+m*10;
write_data(fen/10+0x30);
write_data(fen%10+0x30);
write_data(' ');
/* shi=read_byte(0x85);
m=fen/10;
n=fen%10;
write_data(m+0x30);
write_data(n+0x30);
write_data(' ');
*/
miao=read_ds1302(0x81);
miao=miao&0x7F;
m=miao/16;
n=miao%16;
miao=n+m*10;
write_data(miao/10+0x30);
write_data(miao%10+0x30);
write_data(' ');
write_data('a');
write_data('0');
}
}
为了验证读出来的分和秒是否正确,请各位帮助。