📄 ds1302op.c
字号:
#include "DS1302op.h"
#define ReadData 1
#define WriteData 0
#define IsReadOP(x) x&0x01
//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
//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();
//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)
{
DS_RST=0;
DataDir=WriteData;
DS_RST=1;
DataAll=CMDByte;
WriteByte();
ptr=ptr;
asm("movwf _FSR");
//Set the correct bank and "movf addr,w"
if(IsReadOP(CMDByte))
{
//Read operation
DataDir=ReadData;
do{
ReadByte();
asm("movf _DataAll,w");
asm("movwf 0x00");
asm("incf _FSR,f");
}while(--DataLen);
//*/
}
else
{
//Write operation
do{
asm("movf 0x0,w");
asm("movwf _DataAll");
asm("incf _FSR,f");
WriteByte();
}while(--DataLen);
//*/
}
DS_RST=0;
}
//inner function
void ReadByte()
//Make sure the clock bit is high and the RST bit is high
{
unsigned char i=0x08;
do
{
DS_Clock=0;
DataAll>>=1;
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=0x08;
do
{
DS_Clock=0;
DS_Data=0;
if(DataAll&0x01)
DS_Data=1;
asm("rrf _DataAll,f");
DS_Clock=1;
}while(--i);
}
//end inner function
//*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -