📄 ds1302op.h
字号:
/***********************************
* Company : HSLCN
*
* Filename: DS1302op.h
* Summary : A declaration File
*
* Version : 3.0
* Author : lxyppc
* CreateDate:2007-1-25
*
* Copyright (C) 2007 : lxyppc
* All rights reserved.
************************************/
#ifndef DS1302OP_H
#define DS1302OP_H
//Standard defines
#include <pic.h>
//DS1302 special defines
#include "DS1302.h"
/*
***************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 anywhere
for the data pin, you should define it's control bit as well
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 (DataDir)
Example:
#define DS_RST RA1
#define DS_Clock RA2
#define DS_Data RA3
#define DataDir TRISA3
//we use the RA1 as reset pin, RA2 as Clock pin and RA3 as Data pin
//for control the data direction ,we should also define the TRISA3
//as data pin's contrl bit
//We must make sure all the three pin use by the DS1302 is set as
//output
//*/
//Set the out put port, make sure all the pin is set to putput
//DS_Clock is the clock pin
#define DS_Clock RB6 //<need you to define>
//DS_Data is the data pin
#define DS_Data RB7 //<need you to define>
//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 //<need you to define>
//DataDir is the Data pin data direction
//So it must be the output control bit of data(DS_Data) pin
#define DataDir TRISB7 //<need you to define>
//I'll use the bytes define in DS1302op.c to transfer data
//when read or write DS1302 in single byte mode
extern unsigned char DataAll;
extern unsigned char DataTemp;
//Operation of the DS1302
//Require data address,length and the command you want to send
//There is only one function when operate the DS1302
//the return value is the last byte
//you write in or read from DS1302
unsigned char OperateDS(unsigned char *ptr,\
unsigned char DataLen,unsigned char CMDByte);
//define write or read DS1302 in single byte mode
//when it's in byte mode, the length of data is set to 1
//Write 1 byte to DS1302
#define WriteDS(cmd,data) DataTemp=data;\
OperateDS(&DataTemp,1,cmd)
//Read 1 byte from DS1302
#define ReadDS(cmd) OperateDS(&DataTemp,1,cmd)
//define write or read DS1302 in brust mode
//when it's in brust mode, you should set the data's first address
//and the length of the data
//Write DS1302 in brust mode
#define WriteDSAll(ptr,length,cmd) \
OperateDS(ptr,length,cmd)
//Read DS1302 in brust mode
#define ReadDSAll(ptr,length,cmd) \
OperateDS(ptr,length,cmd)
//For easier to use the write single byte command
//I defined the WriteRAM and WriteTime function
//you needn't remember the command, just use the addr as
//normal address
#define WriteRAM(addr,data) WriteDS(WriteRAMCMD(addr),data)
#define WriteTime(addr,data) WriteDS(WriteTimeCMD(addr),data)
//For easier to use the read single byte command
//I defined the ReadRAM and ReadTime function
//you needn't remember the command, just use the addr as
//normal address
#define ReadRAM(addr) ReadDS(ReadRAMCMD(addr))
#define ReadTime(addr) ReadDS(ReadTimeCMD(addr))
//Write or read in brust mode
//For easier to use the write in brust mode command
//I defined the WriteRAMAll and WriteAllTime function
//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)
//For easier to use the read in brust mode command
//I defined the ReadRAMAll and ReadAllTime function
//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)
/* 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
//Define the charger setting command
#define SetCharger(x) WriteDSTime(DS_Charger,x)
//The charger command define in DS1302.h
/*
Example:
//use 2 Diode and 4K resistor to charge
SetCharger(Charge_ON & Charge_2D & Charge_R4K);
//*/
//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 + -