[C51 code] DS1302 driver

Publisher:Blissful567Latest update time:2016-07-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*********************ds1302.c**********************/

#include "Atmel/AT89X51.h"
#include "link.h"
#include "ds1302.h"
/*******************Variable definition********************/
uchar code WeekTab[] = { //Leap year month and week table
                         (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

Reference address:[C51 code] DS1302 driver

Previous article:[C51 code] DS18B20 driver
Next article:[C51 code] LCD1602 driver

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号