?? ds1302.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 + -