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

📄 ds1302op.h

📁 时钟芯片ds1302 程序 源程序按版本号放在文件夹中。里面有在Protues中仿真的DSN文件。 打开MPLAB的MCP文件进行编译 将DSN文件载入Protues中 将生成的HEX导入到P
💻 H
字号:
/***********************************
*  Company :  HSLCN
* 
*  Filename:  DS1302op.h
*  Summary :  A declaration File
* 
*  Version :  2.0
*  Author :   lxyppc
*  CreateDate:2007-1-22
* 
*  Copyright (C) 2007 : lxyppc
*  All rights reserved.
************************************/
/*
***************Read me***************
You must set the Reset,Data and Clock before use these functions
and make sure these pins are set as output before use
and set the Reset pin to ligical 0 before use
for the reset pin, it better to set it 0 even when not use it 
for the data pin, you should define it's configure bit
e.g. when you use PORTA 3 bit as the data pin
you should also set the TRISA 3 bit as it's control bit
Example:
 #define DS_RST     RA1
 #define DS_Clock   RA2
 #define DS_Data    RA3
 #define DataDir    TRISA3
//*/
#ifndef	DS1302OP_H
#define	DS1302OP_H
#include <pic.h>
#include "DS1302.h"


unsigned char ReadDSByte(unsigned char addr);
//if there are some problem when use his code
//please set the ASMCode to 0
#define ASMCode     1

//Set the out put port, make sure all the pin is set to putput
//DS_Clock is the clock pin
#define DS_Clock   RB6

//DS_Data is the data pin
#define DS_Data    RB7

//DS_RST is the reset pin, make sure the reset pin is low
//when you call these fuctions shown in below
#define DS_RST     RB5

//DataDir is the Data pin data direction
//So it must be the output control bit of data(DS_Data) pin
#define DataDir    TRISB7

//Basic functions
//It better to use the General function instead of the basic ones
//the general functions defines in below

//Write in byte mode
void    WriteDS(unsigned char addr, unsigned char data);

#define WriteRAM(addr,data)  WriteDS(WriteRAMCMD(addr),data)
#define WriteTime(addr,data)  WriteDS(WriteTimeCMD(addr),data)
//void    WriteRAM(unsigned char address, unsigned char data);
//void    WriteTime(unsigned char address, unsigned char data);

//Read in byte mode
unsigned char    ReadDS(unsigned char address);
#define     ReadRAM(addr) ReadDS(ReadRAMCMD(addr))
#define     ReadTime(addr) ReadDS(ReadTimeCMD(addr))

//unsigned char    ReadRAM(unsigned char address);
//unsigned char    ReadTime(unsigned char address);
//End Basic functions




//Define the charger setting command
#define SetCharger(x) WriteDSTime(DS_Charger,x)
/*
Example:  
    //use 2 Diode and 4K resistor to charge
    SetCharger(Charge_ON & Charge_2D & Charge_R4K);
//*/


//General functions

//Write or read in brust mode
//Write
void    WriteDSAll(unsigned char *ptr,\
unsigned char length,unsigned char opCMD);
//Read
unsigned char    ReadDSAll(unsigned char *addr,\
unsigned char length,unsigned char opCMD);

//Write data to normal RAM in brust mode
//require a output address and the length of these data
#define WriteRAMAll(addr,length) \
WriteDSAll(addr,length,WriteRAMAllCMD)

//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
#define WriteTimeAll(addr) \
WriteDSAll(addr,TimeBrust,WriteTimeAllCMD)


//Read data from normal RAM in brust mode
//require an input address and the length of these data
#define ReadRAMAll(ptr,length) \
ReadDSAll(ptr,length,ReadRAMAllCMD)

//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
#define ReadTimeAll(ptr) \
ReadDSAll(ptr,TimeBrust,ReadTimeAllCMD)

extern unsigned char DataAll;
#define     ReadTime1(addr) \
ReadDSAll(&DataAll,1,ReadTimeCMD(addr))

#define     ReadRAM1(addr) \
ReadDSAll(&DataAll,1,ReadRAMCMD(addr))

#define WriteTime1(addr,data) \
DataAll=data;\
WriteDSAll(&DataAll,1,WriteTimeCMD(addr))

/*      Read or write RAM
Example:
    unsigned char str[]="Hello World!";
    unsigned char str2[12];
//the string "Hello World!" will write into DS1302
    WriteRAMAll(str,12);
//the str2 = "Hello World!"
    ReadRAMAll(str2,12);    
//*/

/*       Read or write time
Example:
    unsigned char Time[8];
    unsigned char Hour;
    unsigned char Year;
    ReadTimeAll(Time);
    Hour=Time[DS_Hour];
    Date=Time[DS_Year];
//The hour and year will be restored in Hour and Year
//*/

//General functions
//Popurse: you needn't remember where the special address is

//Start or stop the DS1302
#define StopDS1302(x) WriteTime(DS_Second,x|0B10000000)
#define StartDS1302(x) WriteTime(DS_Second,x&0B01111111)
/*
Example:
    //the clock will stop and set the second to 15
    StopClock(0x15);  
    //the clock will start and set second to 16
    StartClock(0x16); 
    //the clock will stop and set the second to the current value
    StopSecond(ReadSecond)
//*/

//Lock or Unlock the DS_1302
#define LockDS1302      WriteTime(DS_Control,0xff)
#define UnlockDS1302    WriteTime(DS_Control,0x00)
#define IsLocked        ReadTime(DS_Control)&0B10000000
/*
Example:
//Lock the device
    LockDS1302
//unlock the device
    UnlockDS1302
    if(IsLocked)
        // locked operation
    else
        // unlocked operation
//*/



//Set the time or calendar

#define SetSecond(x)    WriteTime(DS_Second,x)
#define SetMinute(x)    WriteTime(DS_Minute,x)
#define SetHour(x)      WriteTime(DS_Hour,x)
#define SetDate(x)      WriteTime(DS_Date,x)
#define SetMonth(x)     WriteTime(DS_Month,x)
#define SetDay(x)       WriteTime(DS_Day,x)
#define SetYear(x)      WriteTime(DS_Year,x)

//define the hour is 24 hour or AM/PM
#define Set24Hour(x)    WriteTime(DS_Hour,x&0B00111111)
#define SetAMHour(x)    WriteTime(DS_Hour,x|0B10000000)
#define SetPMHour(x)    WriteTime(DS_Hour,x|0B10100000)
/*
Example:
    //Set time to 17 o'clock
    Set24Hour(0x17);
    //Set time to 7 o'clock PM
    SetPMHour(0x07);
    //Set time to 5 o'clock AM
    SetAMHour(0x05);
//*/

//Read the time or calendar

#define ReadSecond      ReadTime(DS_Second)
#define ReadMinute      ReadTime(DS_Minute)
#define ReadHour        ReadTime(DS_Hour)
#define ReadDate        ReadTime(DS_Date)
#define ReadMonth       ReadTime(DS_Month)
#define ReadDay         ReadTime(DS_Day)
#define ReadYear        ReadTime(DS_Year)
//get the hour format to 24 hour or AM/PM
#define Get24Hour(x)    x&0B00111111
#define Get12Hour(x)    x&0B00011111
//test the hour format
#define Is12Hour(x)     x&0B10000000
#define IsPM(x)         x&0B00100000

/*
Example:
    unsigned char tmp;
    unsigned char Hour;
    bit IsAfternoon;
    bit Is24Mode;
//Read hour to tmp
    tmp=ReadHour;
//if it's in 12 hour mode
    if(Is12Hour(tmp))
    {
    //format it to 12 hour
        Hour=Get12Hour(tmp);
        Is24Mode=0;
    //if it's afternoon
        if(IsPM(tmp))
    //yes, set now is afternoon
            IsAfternoon=1;
        else
    //no, do not set it
            IsAfternoon=0;
    }
//it's in 24 hour mode
    else
    {
    //format it to 24 hour mode, 
    //if it's already in 24 mode, there is no need to do this 
        Hour=Get24Hour(tmp);
        Is24Mode=1;
    }
//*/


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -