#ifndef _DS1302_H
#define _DS1302_H
#ifndef __C
#include
#endif
#ifndef _MACRO_H
#include "macro.h"
#endif
sbit ACC0 = ACC ^ 0;
sbit ACC7 = ACC ^ 7;
sbit DSRST = P2 ^ 7; //
sbit DSDIO = P2 ^ 6; // Clock data line
sbit DSCLK = P2 ^ 5; // Clock line
#define SEC_WRI 0x80
#define SEC_READ 0x81
#define MIN_WRI 0x82
#define MIN_READ 0x83
#define HR_WRI 0x84
#define HR_READ 0x85
#define DATE_WRI 0x86
#define DATE_READ 0x87
#define MON_WRI 0x88
#define MON_READ 0x89
#define WEEK_WRI 0x
#define WEEK_READ 0x8B
#define YEAR_WRI 0x
#define YEAR_READ 0x8D
#define WRI_PROTECT_WRI 0x8E
#define WRI_PROTECT_READ 0x
#define CLOCK_MULTI_BYTE_WRI 0xBE
#define CLOCK_MULTI_BYTE_READ 0xBF
#define RAM_MULTI_BYTE_WRI 0xFE
#define RAM_MULTI_BYTE_READ 0xFF
#define DSRSTLOW() DSRST = 0; _nop_(); _nop_(); _nop_(); _nop_();\
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \
_nop_(); _nop_(); _nop_(); _nop_(); _nop_()
#define DSRSTHIGH() DSRST = 1; _nop_(); _nop_(); _nop_(); _nop_();\
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \
_nop_(); _nop_(); _nop_(); _nop_(); _nop_()
#define DSCLKLOW() DSCLK = 0; _nop_(); _nop_(); _nop_(); _nop_();\
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \
_nop_(); _nop_(); _nop_(); _nop_(); _nop_()
#define DSCLKHIGH() DSCLK = 1; _nop_(); _nop_(); _nop_(); _nop_();\
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \
_nop_(); _nop_(); _nop_(); _nop_(); _nop_()
extern uchar xdata dsWriDsI ;
extern uchar xdata dsWriCh ;
extern uchar xdata dsReadDsK ;
extern uchar xdata dsReadCl ;
extern uchar xdata ds1302SettimerBuffer[];
extern uchar xdata ds1302GettimerBuffer[];
void WriDs(uchar addr, uchar dat);
uchar ReadDs(uchar addr);
void SetTimer(uchar *pSetTimer);
void GetTimer(uchar *pGetTimer);
void OscEnable();
void Osc24();
void ChargeEnable();
void DsInit();
#endif
#include "DS1302.h"
#include
/********************************************************************
*
*
* 说明:
* 功能: 时钟停止位使能
* call: v_W1302
* in: no
* return:no
*********************************************************************/
void OscEnable()
{
WriDs(0x80,0);
}
/********************************************************************
*
* name: Osc24
* state:
* fun: use 24 hour record
* call: v_W1302
* in: no
* return:no
*********************************************************************/
void Osc24()
{
WriDs(0x84,0);
}
/********************************************************************
*
* 名称: ChargeEnable
* state:
* fun : 涓流充电使能
* Call : v_W1302
* in: no
* Return value : None
********************************************************************/
void ChargeEnable()
{
WriDs(0x90,0xa5); /* Trickle charge, a diode, a 2k resistor */
}
/************************************************************************
*
* Name : DsInit()
* Description :
* fun : Clock chip initialization
* call: No
* in: no
* return: no
*********************************************************************/
void DsInit()
{
//T_CLK = 0;
//T_RST = 0;
//_nop_();
//T_RST = 1;
DSCLKLOW();
DSRSTLOW();
DSRSTHIGH();
OscEnable(); /* Clock stop bit enable */
WriDs(0x8e,0x0); /* Allow writing of single data byte */
ChargeEnable(); /* Charge enable */
Osc24();
}
/**************************************************
* Prototype: void WriteDS(uchar addr, uchar dat)
* Function: write data to ds1302 clock chip
* Description: None
* Parameter: dat write data; addr write address
* Return: None
******************************************************/
void WriDs(uchar addr, uchar dat)
{
//uchar dsWriDsI;
//uchar dsWriCh = 0x01;
dsWriDsI = 0;
dsWriCh = 0x01;
EA = 0;
DSCLKLOW();
DSRSTLOW();
DSRSTHIGH();
for(dsWriDsI = 0; dsWriDsI < 8; dsWriDsI ++)
{
if(addr & dsWriCh)
{
DSDIO = 1;
}
else
{
DSDIO = 0;
}
dsWriCh = dsWriCh << 1;
DSCLKHIGH();
DSCLKLOW();
}
dsWriCh = 0x01;
for(dsWriDsI = 0; dsWriDsI < 8; dsWriDsI ++)
{
if(dat & dsW riCh)
{
DSDIO = 1;
}
else
{
DSDIO = 0;
}
dsWriCh = dsWriCh << 1;
DSCLKHIGH();
DSCLKLOW();
}
DSRSTLOW();
EA = 1;
}
/**************************************************
* Prototype: uchar ReadDS(uchar addr)
* Function: read ds clock content
* Description: None
* Parameter: internal register address
* Return: register content
**************************************************/
uchar ReadDs(uchar addr)
{
//uchar dsReadDsK;
//uchar dsReadCl;
dsReadDsK = 0;
dsReadCl = 0;
DSRSTLOW();
DSCLKLOW();
DSRSTHIGH();
dsReadCl = 0x01;
for(dsReadDsK = 0; dsReadDsK < 8; dsReadDsK ++)
{
if(addr & dsReadCl)
{
DSDIO = 1;
}
else
{
DSDIO = 0;
}
dsReadCl = dsReadCl << 1;
DSCLKHIGH();
DSCLKLOW();
}
dsReadCl = 0;
for(dsReadDsK = 0; dsReadDsK < 8; dsReadDsK ++)
{
dsReadCl = dsReadCl >> 1;
if(DSDIO)
{
dsReadCl = dsReadCl + 0x80;
}
DSCLKHIGH();
D SCLKLOW();
}
DSRSTLOW();
return(dsReadCl);
}
void SetTimer(uchar *pSetTimer)
{
WriDs(YEAR_WRI, *(pSetTimer + 0));
WriDs(MON_WRI , *(pSetTimer + 1));
WriDs(DATE_WRI, *(pSetTimer + 2));
WriDs(HR_WRI , *(pSetTimer + 3));
WriDs(MIN_WRI , *(pSetTimer + 4));
WriDs(SEC_WRI , *(pSetTimer + 5));
}
void GetTimer(uchar *pGetTimer)
{
*(pGetTimer + 0) = ReadDs(YEAR_READ);
*(pGetTimer + 1) = ReadDs(MON_READ);
*(pGetTimer + 2) = ReadDs( DATE_READ);
*(pGetTimer + 3) = ReadDs(HR_READ);
*(pGetTimer + 4) = ReadDs(MIN_READ);
*(pGetTimer + 5) = ReadDs(SEC_READ);
}
Previous article:Research and Design of UART between TMS320VC5402 and PC
Next article:Features and Applications of Voice Recording and Playing Chip ISD5116
Recommended ReadingLatest update time:2024-11-16 22:43
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- A Practical Tutorial on ASIC Design (Compiled by Yu Xiqing)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Some unused boards
- 【2022 Digi-Key Innovation Design Competition-Smart Study Room Based on Raspberry Pi
- 【NXP Rapid IoT Review】+ Trial Computer Programming & Mobile Phone Control
- TMS320C6747 Fixed-point/Floating-point Digital Signal Processor
- Chip Manufacturing 7-Chip Manufacturing
- FPGA water lamp design vhdl source program
- USB packet capture module protocol
- The PWM wave output of TL494 in the boost circuit is abnormal. Please help me
- About the problem of AGND and DGND of microcontroller
- "Summer Benefits to Celebrate Dragon Boat Festival" Selected Resources for the Grab-a-Hot Event