⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1302.c

📁 倒车雷达的原理图与源码
💻 C
字号:
/*
***************************************************************************************************
* Copyright (C),2007
* Author        : Zhongsan Yan
* Email         : yanzhongsan@gmail.com
* Date          : 2007-10-17
* File name     : DS1302.c
* Description   : DS1302 driver file
* Version       : V 1.0
* Others        : This file is the driver of DS1302
***************************************************************************************************
*/

//Header files
#include "includes.h"
#include "HT1621B.h"
#include "DS1302.h"
#include "Delay.h"

/*
***************************************************************************************************
* Function name : DS1302BCDToDec
* Description   : Conver BCD code to Decimal number
* Note          : None
* Parameters    : None
* Returns       : None
* Attribute     : Public function
* Others        : Application routin
***************************************************************************************************
*/

/*
***************************************************************************************************
* Function name : Init_DS1302
* Description   : Init DS1302
* Input         : None
* Output        : None
* Others        : Init the RTC chip, Enable the clock oscillator
***************************************************************************************************
*/
void Init_DS1302(void)
{
    DS1302WriteByte(0x00,DS_CLOCK_CONTL);//写允许
    DS1302WriteByte(0x06,DS_CLOCK_SEC);//时钟振荡开始
    DS1302WriteByte(0x80,DS_CLOCK_CONTL);//写保护
}

/*
***************************************************************************************************
* Function name : DS1302ReadByte
* Description   : Read a byte from DS1302
* Input         : addr,the addres of DS1302 want to read
* Output        : The data read from DS1302
* Others        : The command byte is always input starting with the LSB (bit 0).
***************************************************************************************************
*/
UCHAR_8 DS1302ReadByte(UCHAR_8 addr)
{
    UCHAR_8 counter;

    addr |= 0x01;

    //Enable the communication interface
    SET_DS_RST_HIGH;

    //Set the data pin as output
    SET_DS_DATA_OUT;
    for (counter=0;counter<8;counter++)
    {
        SET_DS_SCLK_LOW;
        if (TESTBIT(addr,counter))
        {
            SET_DS_DATA_HIGH;
        }
        else
        {
            SET_DS_DATA_LOW;
        }
        SET_DS_SCLK_HIGH;
    }

    addr = 0x00;
    //Set the data pin as input
    SET_DS_DATA_IN;
    for (counter=0;counter<8;counter++)
    {
        SET_DS_SCLK_LOW;
        addr >>= 1;

        if (DS_DATA_IS_HIGH)
        {
            addr |= 0x80;
        }

        SET_DS_SCLK_HIGH;
    }

    SET_DS_SCLK_LOW;
    SET_DS_RST_LOW;
    return (addr);
}

/*
***************************************************************************************************
* Function name : DS1302WriteByte
* Description   : Write a byte to DS1302
* Input         : data,the data write to ds1302,addr:address of ds1302
* Output        : None
* Others        : The command byte is always input starting with the LSB (bit 0).
***************************************************************************************************
*/
void DS1302WriteByte(UCHAR_8 data,UCHAR_8 addr)
{
    UCHAR_8 counter;

    addr &= 0xFE;

   //Enable the communication interface
    SET_DS_RST_HIGH;

    //Set the data pin as output
    SET_DS_DATA_OUT;

    //Send the address
    for (counter=0;counter<8;counter++)
    {
        SET_DS_SCLK_LOW;
        if (TESTBIT(addr,counter))
        {
            SET_DS_DATA_HIGH;
        }
        else
        {
            SET_DS_DATA_LOW;
        }
        SET_DS_SCLK_HIGH;
    }

    //Send the data
    for (counter=0;counter<8;counter++)
    {
        SET_DS_SCLK_LOW;
        if (TESTBIT(data,counter))
        {
            SET_DS_DATA_HIGH;
        }
        else
        {
            SET_DS_DATA_LOW;
        }
        SET_DS_SCLK_HIGH;
    }

    SET_DS_SCLK_LOW;
    SET_DS_RST_LOW;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -