举报
DS1302时间芯片驱动代码
DS1302时间芯片驱动代码只有读写部分代码
/***********************************************************
File: 1302d.c
Info: Dirver for DS1302
Copyright 2012-2013 Sant-way, Inc.
***********************************************************/
#include /* special function register declarations */
#include /* prototype declarations for I/O functions */
#include
/****************************************************
init 1302 port
****************************************************/
#define uchar unsigned char
#define uint unsigned int
sbit RST=P2^3;
sbit IO=P2^4;
sbit SCLK=P2^5;
sbit WL=P2^7;
sbit DK=P2^6;
sbit K=P2^1;
uchar DS1302_Read(uchar cmd)
{
uchar dat;
RST=0;
SCLK=0;
RST=1;
DS1302_Write_Byte(cmd);
dat=DS1302_Read_Byte();
SCLK=1;
RST=0;
return dat;
}
void DS1302_Write(uchar cmd,uchar dat)
{
RST=0;
SCLK=0;
RST=1;
DS1302_Write_Byte(cmd);
DS1302_Write_Byte(dat);
SCLK=1;
RST=0;
}
void DS1302_Write_Byte(uchar dat)
{
uchar i;
SCLK=0;
// delayus(2)
for(i=0;i<8;i++)
{
IO=dat&0x01;
delayus(2);
SCLK=1;
delayus(2);
SCLK=0;
dat>>=1;
}
}
uchar DS1302_Read_Byte()
{
uchar i,dat=0;
delayus(2);
for(i=0;i<8;i++)
{
dat>>=1;
if(IO==1)
dat|=0x80;
SCLK=1;
delayus(2);
SCLK=0;
delayus(2);
}
return dat;
}
int main(void)
{
/* code */
return 0;
}