📄 ds1302op.c
字号:
#include "DS1302op.h"
//When read data, the tris bit should be 1, and 0 for write
#define ReadData 1
#define WriteData 0
//after right shift the CMDByte (include carry bit) 8 times
// the bit0 change to bit1
//so I use the bit1 to judge whether it's a read command or not
#define IsReadOP(x) x&0x02
//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; //Transfer data between the inner function
unsigned char DataTemp; //Temp byte for one-byte-write command
unsigned char MyINDF @ 0x00;
//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 WriteByte2();
//Operation of the DS1302
//Require data address,length and the command you want to send
unsigned char OperateDS(unsigned char ptr ,
unsigned char DataLen,unsigned char CMDByte)
{
unsigned char i;
DS_RST=0;
DataDir=WriteData;
DS_RST=1;
asm("movwf _FSR");
WriteByte2();
if(IsReadOP(CMDByte))
{
//Read operation
DataDir=ReadData;
do{
i=8;
do
{
DS_Clock=0;
MyINDF>>=1;
if(DS_Data)
MyINDF|=0x80;
DS_Clock=1;
}while(--i);
FSR++;
}while(--DataLen);
//*/
DataTemp=DataTemp; //The return value
}
else
{
//Write operation
do{
CMDByte=MyINDF;
WriteByte2();
FSR++;
}while(--DataLen);
//*/
}
DS_RST=0;
}
//inner function
void WriteByte2()
//Make sure the clock bit is low and the RST bit is high
{
unsigned char i=0x08;
do
{
DS_Clock=0;
DS_Data=0;
//*
//The first parameter of OperateDS is ptr, restored in w
//The second one is DataLen, restored in ?_OperateDS+0
//The third one is CMDByte, restored in ?_OperateDS+1
asm("BTFSC ?_OperateDS+1,0");
DS_Data=1;
asm("rrf ?_OperateDS+1,f");
//*/
DS_Clock=1;
}while(--i);
}
//end inner function
//*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -