📄 ds1302op.c
字号:
#include "DS1302op.h"
#define ReadData 1
#define WriteData 0
#define MyINDF INDF
/*Define DS1302 command byte
*bit0 is Write and read control, 1 is read, 0 is write
*bit1 - bit5 is address bits, so we must use a shift left command
*to rise the normal address in command byte
*for the Time RAM, only 0-8 is available
*if the address bits set to 11111, start the brust mode
*bit6 is time and RAM control
*1 is for the RAM operation,0 is for the Time operation
*/
//define a global variable, so that the data transfer between
//the functions will be faster
//and can make the asm code easier
unsigned char DataAll;
//declare an inner function, read and write a byte
//before use the write function
//Make sure the clock bit is low and the RST bit is high
void WriteByte();
//before use the read function
//Make sure the clock bit is high and the RST bit is high
void ReadByte();
//Write a data in the specify addres
void WriteDS(unsigned char addr, unsigned char data)
{
DS_RST=1;
DataAll=addr;
DS_Clock=0;
WriteByte();
DataAll=data;
WriteByte();
DS_RST=0;
}
//Read a data in the specify addres
//return the value you read
unsigned char ReadDS(unsigned char addr)
{
DS_RST=1;
DataAll=addr;
DS_Clock=0;
WriteByte();
ReadByte();
DS_RST=0;
return DataAll;
}
//Write or read data in brust mode
//require input or outpur address
//data length and operate command
//*********ASM code***************
#if ASMCode == 1
void WriteDSAll(unsigned char *ptr,\
unsigned char length,unsigned char opCMD)
{
DS_RST=1;
DataAll=opCMD;
DS_Clock=0;
WriteByte();
//*******ASM code*******
ptr=ptr; //Set the correct bank and "movf addr,w"
asm("movwf _FSR");
do{
asm("movf 0x0,w");
asm("movwf _DataAll");
asm("incf _FSR,f");
WriteByte();
}while(--length);
//*/
DS_RST=0;
}
unsigned char ReadDSAll(unsigned char *addr,\
unsigned char length,unsigned char opCMD)
{
DS_RST=1;
DataAll=opCMD;
DS_Clock=0;
WriteByte();
//*********ASM code*******
addr=addr; //Set the correct bank and "movf addr,w"
asm("movwf _FSR");
do{
ReadByte();
asm("movf _DataAll,w");
asm("movwf 0x00");
asm("incf _FSR,f");
}while(--length);
//*/
DS_RST=0;
}
#endif
//************end of asm code type**********
unsigned char ReadDSByte(unsigned char addr)
{
ReadDSAll(&DataAll,1,addr);
}
//inner function
void ReadByte()
//Make sure the clock bit is high and the RST bit is high
{
unsigned char i=0x08;
DataDir=ReadData;
do
{
DS_Clock=0;
DataAll/=2;
if(DS_Data)
DataAll|=0x80;
DS_Clock=1;
}while(--i);
}
void WriteByte()
//Make sure the clock bit is low and the RST bit is high
{
unsigned char i=8;
DataDir=WriteData;
do
{
DS_Clock=0;
DS_Data=0;
if(DataAll&0x01)
DS_Data=1;
asm("rrf _DataAll,f");
DS_Clock=1;
}while(--i);
}
//Write or read in brust mode
//********** C code ******************
#if ASMCode != 1
void WriteDSAll(unsigned char *ptr,\
unsigned char length,unsigned char opCMD)
{
unsigned char i;
DS_RST=1;
DataAll=opCMD;
DS_Clock=0;
WriteByte();
i=0;
do{
DataAll=ptr[i++];
WriteByte();
}while(--length);
DS_RST=0;
}
void ReadDSAll(unsigned char *addr,\
unsigned char length,unsigned char opCMD)
{
unsigned char i;
DS_RST=1;
DataAll=opCMD;
DS_Clock=0;
WriteByte();
i=0;
do{
ReadByte();
addr[i++]=DataAll;
}while(--length);
DS_RST=0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -