#include "Atmel/AT89X51.h"
#include "link.h"
#include "ds1302.h"
/*******************Variable definition********************/
uchar code
(3 << 5) + 31, //January
(6 << 5) + 29, //February
(0 << 5) + 31, //March
(3 << 5) + 30, //April
(5 << 5) + 31, //May
(1 << 5) + 30, //June
(3 << 5) + 31, //July
(6 << 5) + 31, //August
(1 << 5) + 30, //September
(4 << 5) + 31, //October
(0 << 5) + 30, //November
(2 << 5) + 31 //December
};
float DataArrayYear[5];
float DataArrayMonth[3];
float DataArrayDay[3];
float TimeArray[9];
float WeekArray[7][4]={"Mon.","Tue.","Wed.","Thu.","Fri.","Sat.","Sun."};
systemtime Nowtime;
/****************************************************************
*Function: write data to ds1302
*Input parameter: data uchar Content
*Output parameter: None
****************************************************************/
void DS1302Write_f(uchar Content )
{
uchar i ;
for( i = 8 ; i > 0 ; i-- )
{
if( Content & 0x01 )
{
DIO=0x01;
}
else
{
DIO=0x00;
}
Content >>= 1 ;
SCLK=1;
SCLK=0;
}
}
/****************************************************************
*Function function: read data from ds1302
*Input parameter: None
*Export parameter: uchar ReadValue
****************************************************************/
uchar DS1302Read_f( void )
{
uchar i, ReadValue ;
DIO=1;
for( i = 8 ; i > 0 ; i-- )
{
ReadValue >>= 1 ;
if( DIO )
{
ReadValue |= 0x80 ;
}
else
{
ReadValue &= 0x7f ;
}
SCLK=1;
SCLK=0;
}
return ReadValue ;
}
/********************************************************************
*Function function: write a byte of content from the address specified by DS1302
*Input parameter: address of data to be written: Address
specific value of data to be written: Content
*Export parameter: None
***********************************************************************/
void DS1302Writebyte( uchar Address, uchar Content )
{
RST=0;
SCLK=0;
RST=1;
DS1302Write_f( Address ) ;
DS1302Write_f( Content ) ;
RST=0;
SCLK=1;
}
/****************************************************************
*Function: Read a byte of content from the address specified by DS1302
*Input parameter: Address of the data to be read: Address
*Output parameter: uchar ReadValue
********************************************************************/
uchar DS1302Readbyte( uchar Address )
{
uchar ReadValue ;
RST=0;
SCLK=0;
RST=1;
DS1302Write_f( Address );
ReadValue = DS1302Read_f();
RST=0;
SCLK=1;
return ReadValue ;
}
/****************************************************************
*Function: Read DS1302 time
*Entry parameter: None
*Exit parameter: None
************************************************************/
void DS1302Gettime(void)
{
Nowtime.Year = BCD2DEC(DS1302Readbyte(READ_YEAR)); //YearNowtime.Month
= BCD2DEC(DS1302Readbyte(READ_MONTH)); //MonthNowtime.Day
= BCD2DEC(DS1302Readbyte(READ_DAY)); //DayNowtime.Week
= BCD2DEC(DS1302Readbyte(READ_WEEK));
//WeekNowtime.Hour = BCD2DEC(DS1302Readbyte(READ_HOUR));
//HourNowtime.Minute = BCD2DEC(DS1302Readbyte(READ_MINUTE));
//MinuteNowtime.Second = BCD2DEC(DS1302Readbyte(READ_SECOND)); //seconds
}
/****************************************************************
*Function: Week adjustment function
*Entry parameter: Year: y; Month: m; Day: d
*Exit parameter: Day of the week uchar week
****************************************************************/
/*****************Week adjustment function*******************/
uchar WeekDay20(uchar y, uchar m, uchar d)
{
uchar week, day;
day = WeekTab[m - 1]; //Month table
week = day >> 5; //Month week number
day &= 0x1f; //Month day numberif
((m < 3) && (y & 0x03))
{ //Ordinary yearif
(m == 2) day--; //Ordinary year month day number
week++; //Ordinary year month table + 1
}
y = y + (y >> 2); //Year + year/4
week = (week + y + d + 2) % 7; //(week=year+year/4+month table+2day)%7
return week; //Return week
}
/****************************************************************
*Function: Initialize the value written into the DS1302 clock register (only need to be called once in the main program)
*Input parameter: None
*Exit parameter: None
************************************************************/
void DS1302Initial(void)
{
if(DS1302Readbyte( 0xc1) != 0xf0 )
{
DS1302Writebyte( WRITE_PROTECT,0x00) ; //Allow write operation
DS1302Writebyte( WRITE_YEAR, 0x09 ) ; //Year
DS1302Writebyte( WRITE_WEEK, 0x05 ) ; //Week
DS1302Writebyte( WRITE_MONTH, 0x05 ) ; //Month
DS1302Writebyte( WRITE_DAY, 0x16 ) ; //Day
DS1302Writebyte( WRITE_HOUR, 0x14 ) ; //Hour
DS1302Writebyte( WRITE_MINUTE, 0x41 ) ; //Minute
DS1302Writebyte( WRITE_SECOND, 0x00 ) ; //Second
DS1302Writebyte( 0x90, 0x00 ) ; //No charging
DS1302Writebyte( 0xc0, 0xf0 ) ; //Judge whether to initialize once and write the mark
DS1302Writebyte( WRITE_PROTECT, 0x80 ) ; //Prohibit write operation
DS1302Writebyte( 0xc1, 0xf0 );
}
}
/****************************************************************
*Function: data is converted into ASCII code
*Input parameter: None
*Exit parameter: None
******************************************************************/
void DS1302DataConvert(void)
{
DS1302Gettime();
DataArrayYear[0] ='2';
DataArrayYear[1] ='0';
DataArrayYear[2] =Nowtime.Year/10+'0';
DataArrayYear[3] =Nowtime.Year%10+'0';
DataArrayYear[4] ='\0';
DataArrayMonth[0] =Nowtime.Month/10+'0';
DataArrayMonth[1] =Nowtime.Month%10+'0';
DataArrayMonth[2] ='\0';
DataArrayDay[0] =Nowtime.Day/10+'0';
DataArrayDay[1] =Nowtime.Day%10+'0';
DataArrayDay[2] ='\0';
TimeArray[0] =Nowtime.Hour/10+'0';
TimeArray[1] =Nowtime.Hour%10+'0';
TimeArray[2] =':';
TimeArray[3] =Nowtime.Minute/10+ '0';
TimeArray[4] =Nowtime.Minute%10+'0';
TimeArray[5] =':';
TimeArray[6] =Nowtime.Second/10+'0';
TimeArray[7] =Nowtime. Second%10+'0';
TimeArray[8] ='\0';
}
/****************************** ************************************
*Function: data conversion and LCD display
*Input parameter: none
*Output parameter: none
********************************* *********************************/
/*void DS1302Disp(void)
{
DS1302DataConvert();
LCDDispListChar(0, 0,DataArrayYear);
DispMychar(4,0,0);
LCDDispListChar(5,0,DataArrayMonth);
DispMychar(7,0,1);
LCDDispListChar(8,0,DataArrayDay);
DispMychar(10,0,2);
LCDDispListChar(12,0,WeekArray[WeekDay20(Nowtime.Year,Nowtime.Month,Nowtime.Day)-1]);
LCDDispListChar(0,1,"Time:");
LCDDispListChar(6,1,TimeArray);
}
* /
/*********************ds1302.h**********************/
#ifndef __ds1302_h__
#define __ds1302_h__
#include
#include "Atmel/AT89X51.h"
#include "LINK.h"
/**********************Macro definition********************/
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define WRITE_DAY 0x86
#define WRITE_MONTH 0x88
#define WRITE_WEEK 0x8A
#define WRITE_YEAR 0x8C
#define WRITE_PROTECT 0x8E
#define READ_SECOND 0x81
#define READ_MINUTE 0x83 #define READ_HOUR
0x85 #define READ_DAY
0x87
#define READ_MONTH 0x89
#define READ_WEEK 0x8B
# define READ_YEAR 0x8D
#define BCD2DEC(X) (((X&0x70)/16)*10 + (X&0x0F)) //Macro used to convert BCD code to decimal
#define DEC2BCD(X) ((X/10)*16 + (X%10)) // Macro for converting decimal to BCD
/*******************Port definition********************/
sbit RST =P2^3;
sbit SCLK=P2^4;
sbit DIO =P2^5;
/****DS1302的声明函数****/
extern void DS1302Writebyte(uchar addr,uchar dat);
extern uchar DS1302Readbyte(uchar addr);
extern void DS1302Initial(void);
extern uchar WeekDay20(uchar y, uchar m, uchar d);
extern void DS1302Gettime(void);
extern void DS1302DataConvert(void);
extern void DS1302Disp(void);
extern void DS1302DataConvert(void);
/********************biànliàngdìngyì************************/
extern fly DataArrayYear [5];
extern float DataArrayMonth[3];
extern float DataArrayDay[3];
extern float TimeArray[9];
extern float WeekArray[7][4];
typedef struct // Defined time type structure, all numbers are decimal
{
uchar Second;
uchar Minute;
uchar Hour;
uchar Week;
uchar Day;
uchar Month;
uchar Year;
}systemtime;
extern systemtime Nowtime;
#endif
Previous article:[C51 code] DS18B20 driver
Next article:[C51 code] LCD1602 driver
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- 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
- Why is the output filter capacitor of the WM8918 audio decoding DAC so large?
- DLP projection face changing mask
- Become a Xilinx FPGA Design Expert (Basics)
- A brief history of channel coding
- Connected cars, where is the future?
- What PCB design practice materials are there?
- EEWORLD University ---- Live playback: The most important component of the analog world - Signal chain and power supply: USB Type-C? PD special session
- Problems connecting Bluetooth module with CC2541
- microchip zero-drift operational amplifier
- Matter Development Guide (VI): Network Configuration and Lighting-App Examples