⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1302op.c

📁 时钟芯片ds1302 程序 源程序按版本号放在文件夹中。里面有在Protues中仿真的DSN文件。 打开MPLAB的MCP文件进行编译 将DSN文件载入Protues中 将生成的HEX导入到P
💻 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 + -