📄 ds1302op.c
字号:
#include "DS1302op.h"
#define ReadData 1
#define WriteData 0
#define MyINDF INDF
//When read time in brust mode, we must read all the 8 time byte
//at one time, the bytes are second, minute, hour, date, month, day, year
//and include control byte
//so when read time in brust mode, we needn't define the number
//of data bytes to read, just set to 8
#define TimeBrust 8
/*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
unsigned char DataAll;
unsigned char Cnt;
//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 in normal RAM
void WriteRAM(unsigned char addr,unsigned char data)
{
DS_RST=1;
DataAll=WriteRAMCMD(addr);
DS_Clock=0;
WriteByte();
DataAll=data;
WriteByte();
DS_RST=0;
}
//Write a data in the specify addres in Time RAM
void WriteTime(unsigned char addr, unsigned char data)
{
DS_RST=1;
DataAll=WriteTimeCMD(addr);
DS_Clock=0;
WriteByte();
DataAll=data;
WriteByte();
DS_RST=0;
}
void WriteDS(unsigned char addr, unsigned char data)
{
DS_RST=1;
DataAll=addr;
DS_Clock=0;
WriteByte();
DataAll=data;
WriteByte();
DS_RST=0;
}
//Write data to normal RAM in brust mode
//require a output address and the length of these data
void WriteRAMAll(unsigned char *ptr,unsigned char length)
{
unsigned char i;
DS_RST=1;
DataAll=WriteRAMAllCMD;
DS_Clock=0;
WriteByte();
#if ASMCode == 1
//*******ASM code*******
ptr=ptr;
asm("movwf _FSR");
do{
asm("movf 0x0,w");
asm("movwf _DataAll");
asm("incf _FSR,f");
WriteByte();
}while(--length);
//*/
#else
//******* C code********
i=0;
do{
DataAll=ptr[i++];
WriteByte();
}while(--length);
//*/
#endif
DS_RST=0;
}
//Write data in time RAM in brust mode
//because when write time in brust mode,
//we should write the first 8 bytes at one time
//So there is no need to define the length of these data
void WriteTimeAll(unsigned char *ptr)
{
unsigned char i=TimeBrust;
DS_RST=1;
DataAll=WriteTimeAllCMD;
DS_Clock=0;
WriteByte();
#if ASMCode == 1
//*******ASM code******
ptr=ptr;
asm("movwf _FSR");
do{
asm("movf 0x0,w");
asm("movwf _DataAll");
asm("incf _FSR,f");
WriteByte();
}while(--i);
//*/
#else
//********C code*******
ii=0;
do{
DataAll=ptr[ii++];
WriteByte();
}while(--i);
//*/
#endif
DS_RST=0;
}
//Read a data in the specify addres in time RAM
//return the value you read
unsigned char ReadTime(unsigned char addr)
{
DS_RST=1;
DataAll=ReadTimeCMD(addr);
DS_Clock=0;
WriteByte();
ReadByte();
DS_RST=0;
return DataAll;
}
//Read a data in the specify addres in normal RAM
//return the value you read
unsigned char ReadRAM(unsigned char addr)
{
DS_RST=1;
DataAll=ReadRAMCMD(addr);
DS_Clock=0;
WriteByte();
ReadByte();
DS_RST=0;
return DataAll;
}
//Read data from normal RAM in brust mode
//require an input address and the length of these data
void ReadRAMAll(unsigned char *addr,unsigned char length)
{
unsigned char i;
DS_RST=1;
DataAll=ReadRAMAllCMD;
DS_Clock=0;
WriteByte();
#if ASMCode == 1
//*********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);
//*/
#else
//**********C code********
i=0;
do{
ReadByte();
addr[i++]=DataAll;
}while(--length);
//*/
#endif
DS_RST=0;
}
//Read data from time RAM in brust mode
//because when read time in brust mode,
//we should read the first 8 bytes at one time
//So there is no need to define the length of these data
void ReadTimeAll(unsigned char *addr)
{
unsigned char i;
//if this not exsit, PICC will ignore this parameter
DS_RST=1;
DataAll=ReadTimeAllCMD;
DS_Clock=0;
WriteByte();
i=TimeBrust;
#if ASMCode == 1
//********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(--i);
//*/
#else
//********C code**********
do{
ReadByte();
addr[TimeBrust-i]=DataAll;
}while(--i);
//*/
#endif
DS_RST=0;
}
//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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -