Clock chip ds1302 program, debugging passed (reprint)

Publisher:VolareLatest update time:2014-12-09 Source: laogu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#ifndef _DS1302_H
#define _DS1302_H

#ifndef __C8051F020_H__
#include 8051f020.h>
#endif

#ifndef _MACRO_H
#include "macro.h"
#endif

sbit ACC0 = ACC ^ 0;
sbit ACC7 = ACC ^ 7;

sbit DSRST = P2 ^ 7; //
Clock reset signal line
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 0x8A
#define WEEK_READ             0x8B
#define YEAR_WRI              0x8C
#define YEAR_READ             0x8D
#define WRI_PROTECT_WRI       0x8E
#define WRI_PROTECT_READ      0x8F
#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


/********************************************************************
*
*
名称: OscEnable
*
说明:
*
功能: 时钟停止位使能
* 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);
}

Reference address:Clock chip ds1302 program, debugging passed (reprint)

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

Real-time digital thermometer based on 89S51 (DS1302 18B20)
#include reg51.h #include intrins.h #include math.h #define uchar unsigned char #define uint unsigned int #define S_RST DS_RST=1 #define C_RST DS_RST=0 #define S_CLK DS_CLK=1 #define C_CLK DS_CLK=0 #define Write_Disable RW_DS1302(0x8e,0x80) //写保护 #define Write_Enable RW_DS1302(0x8e,0x00) //允许写入
[Microcontroller]
Real-time digital thermometer based on 89S51 (DS1302 18B20)
[STC MCU Learning] Lesson 14: SPI Communication - Real-time Clock DS1302
1.14.1. Background knowledge about RTC 1.14.1.1 What is a Real Time Clock (RTC)? (1) Distinguishing between time points and time periods Time period: timer/counter; water boiling; not unique Time point: RTC; Alarm, calendar; Unique (2) Why does the microcontroller need a time point? Sometimes the microcontroller n
[Microcontroller]
[STC MCU Learning] Lesson 14: SPI Communication - Real-time Clock DS1302
ST7920 LCD --- DS1302 clock program
sbit LCD12864_EN=P2^7; //E sbit LCD12864_DIN=P2^ 5; //  RS sbit LCD12864_CLK        = P2 ^6  ; //  RW sbit DS1302_CLK  = P2^0;// P1^0; sbit DS1302_IO =P2^1;// P1^1; sbit DS1302_RST =P2^2;// P1^2; unsigned char time ={0x10,0x01,0x27,0x03,0x14,0x00,0x00};// Initialization time year month day week hour minute second     
[Microcontroller]
AT89C2051+LCD1602+DS1302 Real-time Clock Design (c51)
#include #include intrins.h #define uchar unsigned char sbit rs=P3^0;//LCD1602控制脚 sbit rw=P3^1; sbit e=P3^2; sbit io=P3^4; //DS1302 PCI bus pin sbit rst=P3^5; sbit sclk=P3^3; uchar hour,minute,second,year,months,date,day; //Display time register uchar whour,wminute,wsecond,wyear,wmonths,wdate,wday; //Set i
[Microcontroller]
LCD1602&DS1302 clock test program + simulation diagram sharing
comments: 1. Eight-wire drive LCD1602B: PB0-PB7 are data lines, PD3/PD4/PD6 control RS, RW, EN of LCD1602B. 2. Set and read the internal clock of DS1302, and display it through LCD1602. 3. For this experiment, please plug in all 8 short-circuit blocks of JP1 and JP2, and short-circuit blocks of PC2, PC3, PC4, and PC7.
[Microcontroller]
LCD1602&DS1302 clock test program + simulation diagram sharing
DS1302 read and write driver
/**************************  Resources used in the file 1. Port: P0.4, P0.5, P0.6 2. Call delay_ms function ******************************/ /***********************  1302 pin definition ***************************/ sbit T_RST = P0^4; sbit T_DAT = P0^5; sbit T_CLK = P0^6; /********************************  1302 subrout
[Microcontroller]
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号