|
发表于 2011-11-15 06:02:57
|
显示全部楼层
here is the ds1302 module
============ds1302.h==============
//ds1302 interface
/*
DS1302+1602LCD??????
?Edward??,??????????,????????????
??????:
STC89C52?1602?????????4??????
??????:
?????????????;
???12/24???;
??????;
2???,???1??;
???????;
????????;
???????????????(??2??????30?31?);
??S0?Set/OK,S1???,S2??????,S3???;
?Keil μVision2?Keil μVision4???????;
2011?8???
*/
#ifndef unchar
#define unchar unsigned char
#endif
#ifndef NOP
#define NOP() _nop_()
#endif
//hardware configuration
sbit DS1302_CE=P2^5; //DS1302 DS1302_CE(LCD_RST)?
sbit DS1302_IO=P2^7; //DS1302 I/O?
sbit DS1302_CL=P2^6; //DS1302 SDS1302_CLK?
#define ds1302_delay(n) {NOP(); NOP(); NOP(); NOP();}
//end hardware configuration
void ds1302write(unchar cmd,dat);//DS1302??
unchar ds1302read(unchar cmd);//DS1302??
unchar ds1302readbyte(void);//DS1302??1???,???????
void ds1302burstread(unchar *s);//DS1302????
void ds1302init(void);
============ds1302.c=============
/*
DS1302+1602LCD??????
?Edward??,??????????,????????????
??????:
STC89C52?1602?????????4??????
??????:
?????????????;
???12/24???;
??????;
2???,???1??;
???????;
????????;
???????????????(??2??????30?31?);
??S0?Set/OK,S1???,S2??????,S3???;
?Keil μVision2?Keil μVision4???????;
2011?8???
*/
#include<reg52.h>
#include<intrins.h>
#include "ds1302.h" //we use ds1302
//#define unchar unsigned char
//#define ds1302_delay(n) {NOP(); NOP(); NOP(); NOP();}
//sbit DS1302_CE=P2^5; //DS1302 DS1302_CE(LCD_RST)?
//sbit DS1302_IO=P2^7; //DS1302 I/O?
//sbit DS1302_CL=P2^6; //DS1302 SDS1302_CLK?
void ds1302write(unchar cmd,dat)//DS1302??
{
unchar i,t;
DS1302_CE=0;
DS1302_CL=0;
_nop_();
DS1302_CE=1;
for(i=8;i>0;i--)//????
{
DS1302_CL=0;
t=cmd;
DS1302_IO=(bit)(t&0x01);
cmd>>=1;
DS1302_CL=1;
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
}
for(i=8;i>0;i--)//????
{
DS1302_CL=0;
t=dat;
DS1302_IO=(bit)(t&0x01);
dat>>=1;
DS1302_CL=1;
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
}
DS1302_CE=0;
DS1302_CL=0;
}
unchar ds1302read(unchar cmd)//DS1302??
{
unchar i,t;
DS1302_IO=1;
DS1302_CE=0;
DS1302_CL=0;
_nop_();
DS1302_CE=1;
for(i=8;i>0;i--)//????
{
t=cmd;
DS1302_IO=(bit)(t&0x01);
cmd>>=1;
DS1302_CL=1;
//_nop_();
//_nop_();
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
DS1302_CL=0;
//_nop_();
}
//_nop_();//?????????????????
NOP();
t=0; //reset the temp variable
for(i=8;i>0;i--)//????
{
//ACC>>=1;
t>>=1;
//A7=DS1302_IO;
if (DS1302_IO) t |= 0x80;
else t |= 0x00;
DS1302_CL=1;
//_nop_();
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
DS1302_CL=0;
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
}
DS1302_CE=0;
return(t/*ACC*/);
}
unchar ds1302readbyte()//DS1302??1???,???????
{
unchar i, t=0;
for(i=8;i>0;i--)//????
{
//ACC=ACC>>1;
t >>= 1;
//A7=DS1302_IO;
if (DS1302_IO) t |= 0x80;
else t |= 0x00;
DS1302_CL=1;
//_nop_();
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
DS1302_CL=0;
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
}
return(t/*ACC*/);
}
void ds1302burstread(unchar *s)//DS1302????
{
unchar i, t;
DS1302_CE=0;
DS1302_CL=0;
_nop_();
DS1302_CE=1;
//ACC=0xbf;
t=0xbf;
for(i=8;i>0;i--)//????
{
DS1302_IO=t & 0x01/*A0*/;
DS1302_CL=1;
//_nop_();
//_nop_();
//_nop_();
//_nop_();
//_nop_();
ds1302_delay(0);
DS1302_CL=0;
//_nop_();
//_nop_();
t/*ACC*/>>=1;
}
for(i=8;i>0;i--)
{
*s=ds1302readbyte();
s++;
}
DS1302_CE=0;
}
void ds1302init(void) {
ds1302write(0x8e,0x00);//?1302???
ds1302write(0x80,0x00);//???
ds1302write(0x82,0x30);//???
ds1302write(0x84,0x12);//???,????
ds1302write(0x86,0x17);//???
ds1302write(0x88,0x08);//???
ds1302write(0x8a,0x03);//????
ds1302write(0x8c,0x11);//???
ds1302write(0x8e,0x80);//??1302??
} |
|