STC microcontroller control DS1302 program (C program)

#include "REG51.H"

#include "INTRINS.H"

Typedef unsigned char BYTE;

Sbit SCLK = P1^0; //DS1302 clock port P1.0

Sbit IO = P1^1; //DS1302 data port P1.1
Sbit RST = P1^2; //DS1302 chip select port P1.2

//second minute hour day month week year
BYTE code init[] = {0x00, 0x00, 0x20, 0x01, 0x01, 0x05, 0x10};
BYTE data now[7];

Void DS1302_Initial();
Void DS1302_SetTime(BYTE *p);
Void DS1302_GetTime(BYTE *p);

Void main()
{
DS1302_Initial(); //Initialize DS1302
DS1302_SetTime(init); //Set the initial time

DS1302_GetTime(now); //Read the current time
While (1);
}

/**************************************
Delay X microseconds (STC12C5A60S2@12M)
Different working environments, you need to adjust this function
This delay function is calculated using a 1T instruction cycle, which is different from the traditional 12T MCU.
**************************************/
Void Delay()
{
_nop_();
_nop_();
}

/**************************************
Read 1 byte data from DS1302
**************************************/
BYTE DS1302_ReadByte()
{
BYTE i;
BYTE dat = 0;

For (i=0; i>= 1; //The data is shifted to the right by one
If (IO) dat |= 0x80; //read data
SCLK = 1; //clock line is pulled high
Delay (); / / delay waiting
}

Return dat;
}

/**************************************
Write 1 byte of data to the DS1302
**************************************/
Void DS1302_WriteByte(BYTE dat)
{
Char i;

For (i=0; i>= 1; //remove the data
IO = CY; //Send to port
SCLK = 1; //clock line is pulled high
Delay (); / / delay waiting
}
}

/**************************************
Read the data of an address of DS1302
**************************************/
BYTE DS1302_ReadData(BYTE addr)
{
BYTE dat;

RST = 0;
Delay();
SCLK = 0;
Delay();
RST = 1;
Delay();
DS1302_WriteByte(addr); //Write address
Dat = DS1302_ReadByte(); //Read data
SCLK = 1;
RST = 0;

Return dat;
}

/**************************************
Write data to an address of the DS1302
**************************************/
Void DS1302_WritEDAta(BYTE addr, BYTE dat)
{
RST = 0;
Delay();
SCLK = 0;
Delay();
RST = 1;
Delay();
DS1302_WriteByte(addr); //Write address
DS1302_WriteByte(dat); //Write data
SCLK = 1;
RST = 0;
}

/**************************************
Write initial time
**************************************/
Void DS1302_SetTime(BYTE *p)
{
BYTE addr = 0x80;
BYTE n = 7;

DS1302_WriteData(0x8e, 0x00); //Allow write operation
While (n--)
{
DS1302_WriteData(addr, *p++);
Addr += 2;
}
DS1302_WriteData(0x8e, 0x80); //Write protection
}

/**************************************
Read current time
**************************************/
Void DS1302_GetTime(BYTE *p)
{
BYTE addr = 0x81;
BYTE n = 7;

While (n--)
{
*p++ = DS1302_ReadData(addr);
Addr += 2;
}
}

/**************************************
Initialize the DS1302
**************************************/
Void DS1302_Initial()
{
RST = 0;
SCLK = 0;
DS1302_WriteData(0x8e, 0x00); //Allow write operation
DS1302_WriteData(0x80, 0x00); //clock start
DS1302_WriteData(0x90, 0xa6); //One diode +4K resistor charging
DS1302_WriteData(0x8e, 0x80); //Write protection
}

DIY Breadboard KIT

Diy Breadboard Kit,Breadboard And Jumper Wire Kit,Large Breadboard Kit,Breadboard Prototyping Kit

Cixi Zhongyi Electronics Factory , https://www.zybreadboard.com